save.lua 13 KB

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