save.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. --- Generates a control key for savegames.
  2. --
  3. -- It's a random character string, used to differenciate savegames.
  4. generate_control_key = function()
  5. ControlKey = ""
  6. for i = 1, 8 do
  7. ControlKey = ControlKey .. string.char(math.random(97, 97 + 25))
  8. end
  9. end
  10. --- Saves the game.
  11. --
  12. -- @param slot The slot to save at (0-15).
  13. -- @param force If false, exisiting game data will not be overwritten if control keys are different.
  14. save_game = function(slot, force)
  15. -- Only the slots that have something are written below, so what the savemap had before has to
  16. -- be removed: used items, unequipped materia...
  17. savemap_manager:clear_contents()
  18. savemap_manager:set_control_key(ControlKey)
  19. -- Settings of the config menu, and the colours of the windows.
  20. UiContainer.ConfigMenu:save_settings()
  21. savemap_manager:set_money(Inventory.money)
  22. savemap_manager:set_game_time(123456) -- TODO Actual data
  23. savemap_manager:set_countdown_time(0) -- TODO Actual data
  24. for i = 1, 50 do
  25. if Inventory.key[i] == true then
  26. savemap_manager:set_key_item(i - 1, true)
  27. end
  28. end
  29. savemap_manager:set_party(Party[1] or -1, Party[2] or -1, Party[3] or -1)
  30. -- The leader of the field is not always the first member of the party.
  31. savemap_manager:set_setting("field_leader", Party.get_leader() or -1)
  32. for i = 1, MAX_INVENTORY_SLOTS do
  33. if Inventory[i] ~= nil then
  34. savemap_manager:set_item(i - 1, Inventory[i].item, Inventory[i].quantity)
  35. end
  36. end
  37. for i = 1, MAX_MATERIA_SLOTS do
  38. if Materia[i] ~= nil then
  39. savemap_manager:set_materia(i - 1, Materia[i].id, Materia[i].ap)
  40. if Materia[i].id == Materia.ENEMY_SKILL_ID then
  41. for s = 1, 24 do
  42. if Materia[i].skills[s] == true then
  43. savemap_manager:set_e_skill_materia(i - 1, s - 1, true)
  44. end
  45. end
  46. end
  47. end
  48. end
  49. -- TODO: Materia stash, when implemented.
  50. local player = entity_manager:get_player_entity()
  51. local x, y, z = player:get_position()
  52. 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
  53. for i = 0, 11 do
  54. if Characters[i] ~= nil and Characters[i].char_id ~= nil then
  55. local c = Characters[i]
  56. 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)
  57. savemap_manager:set_character_stat(i, 0, c.stats.str.base, c.stats.str.bonus)
  58. savemap_manager:set_character_stat(i, 1, c.stats.vit.base, c.stats.vit.bonus)
  59. savemap_manager:set_character_stat(i, 2, c.stats.mag.base, c.stats.mag.bonus)
  60. savemap_manager:set_character_stat(i, 3, c.stats.spr.base, c.stats.spr.bonus)
  61. savemap_manager:set_character_stat(i, 4, c.stats.dex.base, c.stats.dex.bonus)
  62. savemap_manager:set_character_stat(i, 5, c.stats.lck.base, c.stats.lck.bonus)
  63. savemap_manager:set_character_stat(i, 6, c.stats.hp.base, c.stats.hp.current)
  64. savemap_manager:set_character_stat(i, 7, c.stats.mp.base, c.stats.mp.current)
  65. for l = 1, 4 do
  66. savemap_manager:set_character_limit_learned(i, l - 1, 0, c.limit.learned[l][1], c.limit.uses[l])
  67. savemap_manager:set_character_limit_learned(i, l - 1, 1, c.limit.learned[l][2], c.limit.uses[l])
  68. end
  69. for m = 0, 7 do
  70. for _, part in ipairs({"weapon", "armor"}) do
  71. local is_weapon = (part == "weapon")
  72. local materia = c[part].materia[m]
  73. if materia ~= nil then
  74. savemap_manager:set_character_materia(i, is_weapon, m, materia.id, materia.ap)
  75. if materia.id == Materia.ENEMY_SKILL_ID then
  76. for s = 1, 24 do
  77. if materia.skills[s] == true then
  78. savemap_manager:set_character_e_skill_materia(i, is_weapon, m, s - 1, true)
  79. end
  80. end
  81. end
  82. end
  83. end
  84. end
  85. -- TODO: Character statuses
  86. end
  87. end
  88. for b = 0, Banks.COUNT do
  89. for a = 0, Banks.SIZE do
  90. if (Banks[b] ~= nil and Banks[b][a] ~= nil and Banks[b][a] ~= 0) then
  91. savemap_manager:set_data(b, a, tonumber(Banks[b][a]))
  92. end
  93. end
  94. end
  95. savemap_manager:save(slot, force or true)
  96. end
  97. --- Loads the characters from a saved game. Everything save_game saves of them.
  98. --
  99. -- The characters that already exist are updated in place, so the data that isn't saved stays.
  100. --
  101. -- @param slot The slot to load (0-15).
  102. local function load_characters(slot)
  103. -- The savemap has room for 11 characters.
  104. for c = 0, 10 do
  105. if savemap_manager:get_slot_character_char_id(slot, c) ~= -1 then
  106. local character = Characters[c] or {}
  107. Characters[c] = character
  108. character.char_id = savemap_manager:get_slot_character_char_id(slot, c)
  109. character.name = savemap_manager:get_slot_character_name(slot, c)
  110. character.level = savemap_manager:get_slot_character_level(slot, c)
  111. character.enabled = savemap_manager:is_slot_character_enabled(slot, c)
  112. character.locked = savemap_manager:is_slot_character_locked(slot, c)
  113. character.kills = savemap_manager:get_slot_character_kills(slot, c)
  114. character.back_row = savemap_manager:is_slot_character_back_row(slot, c)
  115. character.exp = savemap_manager:get_slot_character_exp(slot, c)
  116. character.exp_to_next = savemap_manager:get_slot_character_exp_to_next(slot, c)
  117. -- Stats: strength, vitality, magic, spirit, dexterity and luck, then HP and MP.
  118. character.stats = character.stats or {}
  119. local stat_names = { "str", "vit", "mag", "spr", "dex", "lck" }
  120. for index, name in ipairs(stat_names) do
  121. character.stats[name] = {
  122. base = savemap_manager:get_slot_character_stat_base(slot, c, index - 1),
  123. bonus = savemap_manager:get_slot_character_stat_extra(slot, c, index - 1),
  124. total = 0,
  125. }
  126. end
  127. -- For HP and MP the "extra" value is the current one. The maximum is calculated.
  128. character.stats.hp = {
  129. current = savemap_manager:get_slot_character_stat_extra(slot, c, 6),
  130. base = savemap_manager:get_slot_character_stat_base(slot, c, 6),
  131. max = 65535,
  132. }
  133. character.stats.mp = {
  134. current = savemap_manager:get_slot_character_stat_extra(slot, c, 7),
  135. base = savemap_manager:get_slot_character_stat_base(slot, c, 7),
  136. max = 65535,
  137. }
  138. -- Limit breaks. Levels are 0-3 in the savemap, and 1-4 in the game.
  139. character.limit = {
  140. current = savemap_manager:get_slot_character_limit_level(slot, c),
  141. bar = savemap_manager:get_slot_character_limit_bar(slot, c),
  142. learned = {},
  143. uses = {},
  144. }
  145. for l = 1, 4 do
  146. character.limit.learned[l] = {
  147. [1] = savemap_manager:is_slot_character_limit_learned(slot, c, l - 1, 0),
  148. [2] = savemap_manager:is_slot_character_limit_learned(slot, c, l - 1, 1),
  149. }
  150. character.limit.uses[l] = savemap_manager:get_slot_character_limit_uses(slot, c, l - 1)
  151. end
  152. -- Equipment and its materia (slots 0-7).
  153. local accessory = savemap_manager:get_slot_character_accessory_id(slot, c)
  154. character.accessory = (accessory >= 0) and accessory or nil
  155. character.weapon = { id = savemap_manager:get_slot_character_weapon_id(slot, c), materia = {} }
  156. character.armor = { id = savemap_manager:get_slot_character_armor_id(slot, c), materia = {} }
  157. for _, part in ipairs({ "weapon", "armor" }) do
  158. local is_weapon = (part == "weapon")
  159. for m = 0, 7 do
  160. local id = savemap_manager:get_slot_character_materia_id(slot, c, is_weapon, m)
  161. if id >= 0 then
  162. local materia = { id = id, ap = savemap_manager:get_slot_character_materia_ap(slot, c, is_weapon, m) }
  163. if id == Materia.ENEMY_SKILL_ID then
  164. materia.ap = 0
  165. materia.skills = {}
  166. for s = 1, 24 do
  167. materia.skills[s] = savemap_manager:get_slot_character_materia_e_skill_learned(slot, c, is_weapon, m, s - 1)
  168. end
  169. end
  170. character[part].materia[m] = materia
  171. end
  172. end
  173. end
  174. -- TODO: Status
  175. character.status = {}
  176. Characters.recalculate_stats(c)
  177. end
  178. end
  179. end
  180. --- Loads a game.
  181. --
  182. -- It loads everything save_game saves, and then goes to the field where the game was saved.
  183. --
  184. -- @param slot The slot to load (0-15).
  185. load_game = function(slot)
  186. -- The game that was loaded keeps the control key of its slot, so it can be saved there again
  187. -- without being taken for another playthrough.
  188. ControlKey = savemap_manager:get_slot_control_key(slot)
  189. -- Populate banks
  190. for b = 0, Banks.COUNT do
  191. for a = 0, Banks.SIZE do
  192. Banks[b][a] = savemap_manager:get_slot_data(slot, b, a)
  193. end
  194. end
  195. -- Settings of the config menu, and the colours of the windows.
  196. UiContainer.ConfigMenu:load_settings(slot)
  197. Inventory.money = savemap_manager:get_slot_money(slot)
  198. -- TODO: Game time
  199. -- TODO: Countdown
  200. for p = 1, 3 do
  201. Party[p] = savemap_manager:get_slot_party_member(slot, p - 1)
  202. if Party[p] == -1 then
  203. Party[p] = nil
  204. end
  205. end
  206. -- Games saved before the leader was saved have the first member as leader.
  207. Party.leader = nil
  208. if savemap_manager:is_slot_setting_set(slot, "field_leader") then
  209. local leader = savemap_manager:get_slot_setting(slot, "field_leader")
  210. if leader >= 0 then Party.leader = leader end
  211. end
  212. -- The battles and dialogs read the party from the text manager.
  213. export_party_members()
  214. -- The inventory and the materia are 1-based in the game, and 0-based in the savemap.
  215. for i = 1, MAX_INVENTORY_SLOTS do
  216. local quantity = savemap_manager:get_slot_item_at_pos_qty(slot, i - 1)
  217. if quantity > 0 then
  218. Inventory[i] = {
  219. item = savemap_manager:get_slot_item_at_pos_id(slot, i - 1),
  220. quantity = quantity,
  221. }
  222. else
  223. Inventory[i] = nil
  224. end
  225. end
  226. for i = 1, MAX_MATERIA_SLOTS do
  227. local id = savemap_manager:get_slot_materia_at_pos_id(slot, i - 1)
  228. if id >= 0 then
  229. Materia[i] = { id = id, ap = savemap_manager:get_slot_materia_at_pos_ap(slot, i - 1) }
  230. if id == Materia.ENEMY_SKILL_ID then
  231. Materia[i].ap = 0
  232. Materia[i].skills = {}
  233. for s = 1, 24 do
  234. Materia[i].skills[s] = savemap_manager:is_slot_materia_at_pos_e_skill_learned(slot, i - 1, s - 1)
  235. end
  236. end
  237. else
  238. Materia[i] = nil
  239. end
  240. end
  241. -- TODO: Stash (same as materia)
  242. for i = 1, MAX_KEY_SLOTS do
  243. Inventory.key[i] = savemap_manager:get_slot_key_item(slot, i - 1)
  244. end
  245. load_characters(slot)
  246. export_character_names()
  247. -- TODO: Location
  248. -- TODO: Update character stats
  249. -- TODO: Handle world map when implemented
  250. console("camera_free false")
  251. load_field_map_request(savemap_manager:get_slot_location_field(slot), "")
  252. System["MapChanger"].location_name = savemap_manager:get_slot_location_name(slot)
  253. System["MapChanger"].current_map_name = savemap_manager:get_slot_location_field(slot)
  254. -- TODO: For now, assume the PC is Cloub, but it must be saved.
  255. local player = nil
  256. while player == nil do
  257. entity_manager:set_player_entity("Cloud")
  258. player = entity_manager:get_player_entity()
  259. script:wait(0)
  260. end
  261. --local player = entity_manager:get_entity("Cloud")
  262. if player ~= nil then
  263. player:set_position(savemap_manager:get_slot_location_x(slot), savemap_manager:get_slot_location_y(slot), savemap_manager:get_slot_location_z(slot))
  264. player:set_move_triangle_id(savemap_manager:get_slot_location_triangle(slot))
  265. player:set_rotation(savemap_manager:get_slot_location_angle(slot))
  266. background2d:autoscroll_to_entity(player)
  267. local x, y, z = player:get_position()
  268. end
  269. MenuSettings.pause_available = true
  270. end