| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- --- Generates a control key for savegames.
- --
- -- It's a random character string, used to differenciate savegames.
- generate_control_key = function()
- ControlKey = ""
- for i = 1, 8 do
- ControlKey = ControlKey .. string.char(math.random(97, 97 + 25))
- end
- end
- --- Saves the game.
- --
- -- @param slot The slot to save at (0-15).
- -- @param force If false, exisiting game data will not be overwritten if control keys are different.
- save_game = function(slot, force)
- savemap_manager:set_control_key(ControlKey)
- -- Settings of the config menu, and the colours of the windows.
- UiContainer.ConfigMenu:save_settings()
- savemap_manager:set_money(Inventory.money)
- savemap_manager:set_game_time(123456) -- TODO Actual data
- savemap_manager:set_countdown_time(0) -- TODO Actual data
- for i = 1, 50 do
- if Inventory.key[i] == true then
- savemap_manager:set_key_item(i - 1, true)
- end
- end
- savemap_manager:set_party(Party[1] or -1, Party[2] or -1, Party[3] or -1)
- -- The leader of the field is not always the first member of the party.
- savemap_manager:set_setting("field_leader", Party.get_leader() or -1)
- for i = 1, MAX_INVENTORY_SLOTS do
- if Inventory[i] ~= nil then
- savemap_manager:set_item(i - 1, Inventory[i].item, Inventory[i].quantity)
- end
- end
- for i = 1, MAX_MATERIA_SLOTS do
- if Materia[i] ~= nil then
- savemap_manager:set_materia(i - 1, Materia[i].id, Materia[i].ap)
- if Materia[i].id == Materia.ENEMY_SKILL_ID then
- for s = 1, 24 do
- if Materia[i].skills[s] == true then
- savemap_manager:set_e_skill_materia(i - 1, s - 1, true)
- end
- end
- end
- end
- end
- -- TODO: Materia stash, when implemented.
- local player = entity_manager:get_player_entity()
- local x, y, z = player:get_position()
- savemap_manager:set_location(x, y, z, player:get_move_triangle_id(), player:get_rotation(), System["MapChanger"].current_map_name, System["MapChanger"].location_name) -- TODO Actual data
- for i = 0, 11 do
- if Characters[i] ~= nil and Characters[i].char_id ~= nil then
- local c = Characters[i]
- savemap_manager:set_character_info(i, c.char_id, c.name, c.enabled, c.locked, c.level, c.kills, c.back_row, c.exp, c.exp_to_next, c.limit.current, c.limit.bar, c.weapon.id, c.armor.id, c.accessory or -1)
- savemap_manager:set_character_stat(i, 0, c.stats.str.base, c.stats.str.bonus)
- savemap_manager:set_character_stat(i, 1, c.stats.vit.base, c.stats.vit.bonus)
- savemap_manager:set_character_stat(i, 2, c.stats.mag.base, c.stats.mag.bonus)
- savemap_manager:set_character_stat(i, 3, c.stats.spr.base, c.stats.spr.bonus)
- savemap_manager:set_character_stat(i, 4, c.stats.dex.base, c.stats.dex.bonus)
- savemap_manager:set_character_stat(i, 5, c.stats.lck.base, c.stats.lck.bonus)
- savemap_manager:set_character_stat(i, 6, c.stats.hp.base, c.stats.hp.current)
- savemap_manager:set_character_stat(i, 7, c.stats.mp.base, c.stats.mp.current)
- for l = 1, 4 do
- savemap_manager:set_character_limit_learned(i, l - 1, 0, c.limit.learned[l][1], c.limit.uses[l])
- savemap_manager:set_character_limit_learned(i, l - 1, 1, c.limit.learned[l][2], c.limit.uses[l])
- end
- for m = 0, 7 do
- for _, part in ipairs({"weapon", "armor"}) do
- local is_weapon = (part == "weapon")
- local materia = c[part].materia[m]
- if materia ~= nil then
- savemap_manager:set_character_materia(i, is_weapon, m, materia.id, materia.ap)
- if materia.id == Materia.ENEMY_SKILL_ID then
- for s = 1, 24 do
- if materia.skills[s] == true then
- savemap_manager:set_character_e_skill_materia(i, is_weapon, m, s - 1, true)
- end
- end
- end
- end
- end
- end
- -- TODO: Character statuses
- end
- end
- for b = 0, Banks.COUNT do
- for a = 0, Banks.SIZE do
- if (Banks[b] ~= nil and Banks[b][a] ~= nil and Banks[b][a] ~= 0) then
- savemap_manager:set_data(b, a, tonumber(Banks[b][a]))
- end
- end
- end
- savemap_manager:save(slot, force or true)
- end
- --- Loads the characters from a saved game. Everything save_game saves of them.
- --
- -- The characters that already exist are updated in place, so the data that isn't saved stays.
- --
- -- @param slot The slot to load (0-15).
- local function load_characters(slot)
- -- The savemap has room for 11 characters.
- for c = 0, 10 do
- if savemap_manager:get_slot_character_char_id(slot, c) ~= -1 then
- local character = Characters[c] or {}
- Characters[c] = character
- character.char_id = savemap_manager:get_slot_character_char_id(slot, c)
- character.name = savemap_manager:get_slot_character_name(slot, c)
- character.level = savemap_manager:get_slot_character_level(slot, c)
- character.enabled = savemap_manager:is_slot_character_enabled(slot, c)
- character.locked = savemap_manager:is_slot_character_locked(slot, c)
- character.kills = savemap_manager:get_slot_character_kills(slot, c)
- character.back_row = savemap_manager:is_slot_character_back_row(slot, c)
- character.exp = savemap_manager:get_slot_character_exp(slot, c)
- character.exp_to_next = savemap_manager:get_slot_character_exp_to_next(slot, c)
- -- Stats: strength, vitality, magic, spirit, dexterity and luck, then HP and MP.
- character.stats = character.stats or {}
- local stat_names = { "str", "vit", "mag", "spr", "dex", "lck" }
- for index, name in ipairs(stat_names) do
- character.stats[name] = {
- base = savemap_manager:get_slot_character_stat_base(slot, c, index - 1),
- bonus = savemap_manager:get_slot_character_stat_extra(slot, c, index - 1),
- total = 0,
- }
- end
- -- For HP and MP the "extra" value is the current one. The maximum is calculated.
- character.stats.hp = {
- current = savemap_manager:get_slot_character_stat_extra(slot, c, 6),
- base = savemap_manager:get_slot_character_stat_base(slot, c, 6),
- max = 65535,
- }
- character.stats.mp = {
- current = savemap_manager:get_slot_character_stat_extra(slot, c, 7),
- base = savemap_manager:get_slot_character_stat_base(slot, c, 7),
- max = 65535,
- }
- -- Limit breaks. Levels are 0-3 in the savemap, and 1-4 in the game.
- character.limit = {
- current = savemap_manager:get_slot_character_limit_level(slot, c),
- bar = savemap_manager:get_slot_character_limit_bar(slot, c),
- learned = {},
- uses = {},
- }
- for l = 1, 4 do
- character.limit.learned[l] = {
- [1] = savemap_manager:is_slot_character_limit_learned(slot, c, l - 1, 0),
- [2] = savemap_manager:is_slot_character_limit_learned(slot, c, l - 1, 1),
- }
- character.limit.uses[l] = savemap_manager:get_slot_character_limit_uses(slot, c, l - 1)
- end
- -- Equipment and its materia (slots 0-7).
- local accessory = savemap_manager:get_slot_character_accessory_id(slot, c)
- character.accessory = (accessory >= 0) and accessory or nil
- character.weapon = { id = savemap_manager:get_slot_character_weapon_id(slot, c), materia = {} }
- character.armor = { id = savemap_manager:get_slot_character_armor_id(slot, c), materia = {} }
- for _, part in ipairs({ "weapon", "armor" }) do
- local is_weapon = (part == "weapon")
- for m = 0, 7 do
- local id = savemap_manager:get_slot_character_materia_id(slot, c, is_weapon, m)
- if id >= 0 then
- local materia = { id = id, ap = savemap_manager:get_slot_character_materia_ap(slot, c, is_weapon, m) }
- if id == Materia.ENEMY_SKILL_ID then
- materia.ap = 0
- materia.skills = {}
- for s = 1, 24 do
- materia.skills[s] = savemap_manager:get_slot_character_materia_e_skill_learned(slot, c, is_weapon, m, s - 1)
- end
- end
- character[part].materia[m] = materia
- end
- end
- end
- -- TODO: Status
- character.status = {}
- Characters.recalculate_stats(c)
- end
- end
- end
- --- Loads a game.
- --
- -- It loads everything save_game saves, and then goes to the field where the game was saved.
- --
- -- @param slot The slot to load (0-15).
- load_game = function(slot)
- -- The game that was loaded keeps the control key of its slot, so it can be saved there again
- -- without being taken for another playthrough.
- ControlKey = savemap_manager:get_slot_control_key(slot)
- -- Populate banks
- for b = 0, Banks.COUNT do
- for a = 0, Banks.SIZE do
- Banks[b][a] = savemap_manager:get_slot_data(slot, b, a)
- end
- end
- -- Settings of the config menu, and the colours of the windows.
- UiContainer.ConfigMenu:load_settings(slot)
- Inventory.money = savemap_manager:get_slot_money(slot)
- -- TODO: Game time
- -- TODO: Countdown
- for p = 1, 3 do
- Party[p] = savemap_manager:get_slot_party_member(slot, p - 1)
- if Party[p] == -1 then
- Party[p] = nil
- end
- end
- -- Games saved before the leader was saved have the first member as leader.
- Party.leader = nil
- if savemap_manager:is_slot_setting_set(slot, "field_leader") then
- local leader = savemap_manager:get_slot_setting(slot, "field_leader")
- if leader >= 0 then Party.leader = leader end
- end
- -- The battles and dialogs read the party from the text manager.
- export_party_members()
- -- The inventory and the materia are 1-based in the game, and 0-based in the savemap.
- for i = 1, MAX_INVENTORY_SLOTS do
- local quantity = savemap_manager:get_slot_item_at_pos_qty(slot, i - 1)
- if quantity > 0 then
- Inventory[i] = {
- item = savemap_manager:get_slot_item_at_pos_id(slot, i - 1),
- quantity = quantity,
- }
- else
- Inventory[i] = nil
- end
- end
- for i = 1, MAX_MATERIA_SLOTS do
- local id = savemap_manager:get_slot_materia_at_pos_id(slot, i - 1)
- if id >= 0 then
- Materia[i] = { id = id, ap = savemap_manager:get_slot_materia_at_pos_ap(slot, i - 1) }
- if id == Materia.ENEMY_SKILL_ID then
- Materia[i].ap = 0
- Materia[i].skills = {}
- for s = 1, 24 do
- Materia[i].skills[s] = savemap_manager:is_slot_materia_at_pos_e_skill_learned(slot, i - 1, s - 1)
- end
- end
- else
- Materia[i] = nil
- end
- end
- -- TODO: Stash (same as materia)
- for i = 1, MAX_KEY_SLOTS do
- Inventory.key[i] = savemap_manager:get_slot_key_item(slot, i - 1)
- end
- load_characters(slot)
- export_character_names()
- -- TODO: Location
- -- TODO: Update character stats
- -- TODO: Handle world map when implemented
- console("camera_free false")
- load_field_map_request(savemap_manager:get_slot_location_field(slot), "")
- System["MapChanger"].location_name = savemap_manager:get_slot_location_name(slot)
- System["MapChanger"].current_map_name = savemap_manager:get_slot_location_field(slot)
- -- TODO: For now, assume the PC is Cloub, but it must be saved.
- local player = nil
- while player == nil do
- entity_manager:set_player_entity("Cloud")
- player = entity_manager:get_player_entity()
- script:wait(0)
- end
- --local player = entity_manager:get_entity("Cloud")
- if player ~= nil then
- player:set_position(savemap_manager:get_slot_location_x(slot), savemap_manager:get_slot_location_y(slot), savemap_manager:get_slot_location_z(slot))
- player:set_move_triangle_id(savemap_manager:get_slot_location_triangle(slot))
- player:set_rotation(savemap_manager:get_slot_location_angle(slot))
- background2d:autoscroll_to_entity(player)
- local x, y, z = player:get_position()
- end
- MenuSettings.pause_available = true
- end
|