system.lua 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. MAX_INVENTORY_SLOTS = 320
  2. MAX_KEY_SLOTS = 50
  3. MAX_MATERIA_SLOTS = 200
  4. --- System related parameters and utilities
  5. System = {
  6. --- Indicates if the game is paused.
  7. paused = false,
  8. --- Indicates if the menu was available when paused.
  9. paused_menu_settings_available = false,
  10. --- Pauses the game.
  11. pause = function(self)
  12. if MenuSettings.pause_available == false then
  13. do return end
  14. end
  15. if self.paused == false then
  16. self.paused = true
  17. entity_manager:set_paused(true)
  18. self.paused_menu_settings_available = MenuSettings.available
  19. MenuSettings.available = false
  20. audio_manager:play_sound("Cursor")
  21. UiContainer.PauseMenu:show()
  22. end
  23. end,
  24. --- Unpauses the game.
  25. unpause = function(self)
  26. if self.paused == true then
  27. self.paused = false
  28. entity_manager:set_paused(false)
  29. MenuSettings.available = self.paused_menu_settings_available
  30. audio_manager:play_sound("Cursor")
  31. UiContainer.PauseMenu:hide()
  32. end
  33. end,
  34. --- Checks if the game is paused.
  35. --
  36. -- @return True if the game is paused, false if not.
  37. is_paused = function(self)
  38. return self.paused
  39. end
  40. }
  41. --- Stands for the model of the script entities that have none: the opcodes that move or hide a
  42. -- model are run by them in the original game and do nothing. Any method called on it is ignored.
  43. NullEntity = setmetatable({}, {__index = function() return function() end end})
  44. Timer = {
  45. -- TODO When the game clock is implemented, dont use FPS, call this every second.
  46. FPS = 250,
  47. time = 0,
  48. frames = 0,
  49. update = function()
  50. if Timer.time > 0 or Timer.frames > 0 then
  51. Timer.frames = Timer.frames - 1
  52. if Timer.frames == 0 then
  53. Timer.time = Timer.time - 1
  54. dialog:update_timer(Timer.time)
  55. if Timer.time > 0 then
  56. Timer.frames = Timer.FPS
  57. end
  58. Banks[1][20]--[[timer_hours]] = math.floor(Timer.time / 3600)
  59. Banks[1][21]--[[timer_minutes]] = math.floor(Timer.time / 60) % 60
  60. Banks[1][22]--[[timer_seconds]] = Timer.time % 60
  61. end
  62. Banks[1][23]--[[timer_frames]] = Timer.frames
  63. end
  64. end,
  65. set = function(time)
  66. Timer.time = tonumber(time)
  67. Timer.frames = Timer.FPS
  68. dialog:update_timer(Timer.time)
  69. Banks[1][20]--[[timer_hours]] = math.floor(Timer.time / 3600)
  70. Banks[1][21]--[[timer_minutes]] = math.floor(Timer.time / 60) % 60
  71. Banks[1][22]--[[timer_seconds]] = Timer.time % 60
  72. Banks[1][23]--[[timer_frames]] = Timer.frames
  73. end
  74. }
  75. --- Export character names to the text manager.
  76. --
  77. -- Used to compose dialog with character names. Must be called on start and when a character is
  78. -- renamed.
  79. export_character_names = function()
  80. for c = 0, 8 do
  81. if Characters[c] ~= nil then
  82. text_manager:set_character_name(c, Characters[c].name)
  83. end
  84. end
  85. end
  86. --- Export party members to the text manager.
  87. --
  88. -- Used to compose dialog with party character names. Must be called when the party formation is
  89. -- changed in any way.
  90. export_party_members = function()
  91. if Party[1] == nil then
  92. c1 = -1
  93. else
  94. c1 = Party[1]
  95. end
  96. if Party[2] == nil then
  97. c2 = -1
  98. else
  99. c2 = Party[2]
  100. end
  101. if Party[3] == nil then
  102. c3 = -1
  103. else
  104. c3 = Party[3]
  105. end
  106. text_manager:set_party(c1, c2, c3)
  107. end
  108. --- Restores the HP and MP of every character, also the ones that are not in the party, and clears
  109. -- their status (MHMMX).
  110. restore_all_characters = function()
  111. for id, character in pairs(Characters) do
  112. if type(id) == "number" and type(character) == "table" and character.stats ~= nil
  113. and character.stats.hp ~= nil and character.stats.mp ~= nil then
  114. Characters.recalculate_stats(id)
  115. character.stats.hp.current = character.stats.hp.max
  116. character.stats.mp.current = character.stats.mp.max
  117. character.status = {}
  118. end
  119. end
  120. end
  121. --- Sets the current party.
  122. --
  123. -- @param character1 The first member character ID, the party leader.
  124. -- @param character2 The second member character ID, can be nil.
  125. -- @param character3 The third member character ID, can be nil.
  126. set_party = function(character1, character2, character3)
  127. Party[1] = character1
  128. Party[2] = character2
  129. Party[3] = character3
  130. export_party_members()
  131. end
  132. --- Checks if a a character ID actually belongs to a character.
  133. --
  134. -- @param character The ID to check.
  135. -- @return True if the ID is a valid character ID, false otherwise.
  136. is_character = function(character)
  137. if Characters[character] == nil then
  138. return false
  139. end
  140. return true
  141. end
  142. --- Adds a character to the curent party.
  143. --
  144. -- @param character ID of the character to add.
  145. add_party_character = function(character)
  146. if Characters[character] == nil then
  147. print("Tried add to party invalid character \"" .. character .. "\".");
  148. return
  149. end
  150. if Characters[character].locked == true then
  151. print("Tried add to party locked character \"" .. character .. "\".");
  152. return
  153. end
  154. -- Check if character already in party
  155. for i = 1, 3 do
  156. if Party[i] == character then
  157. print("Character \"" .. character .. "\" already in party.");
  158. return
  159. end
  160. end
  161. -- Try to add in first free slot.
  162. for i = 1, 3 do
  163. if Party[i] == nil then
  164. Party[i] = character
  165. export_party_members()
  166. return
  167. end
  168. end
  169. end
  170. --- Removes a character from the curent party.
  171. --
  172. -- @param character ID of the character to remove.
  173. remove_party_character = function(character)
  174. if Characters[character] == nil then
  175. print("Tried remove from party invalid character \"" .. character .. "\".");
  176. return
  177. end
  178. if Characters[character].locked == true then
  179. print("Tried remove from party locked character \"" .. character .. "\".");
  180. return
  181. end
  182. for i = 1, 3 do
  183. if Party[i] == character then
  184. Party[i] = nil
  185. export_party_members()
  186. return
  187. end
  188. end
  189. end
  190. --- Locks a character in the party.
  191. --
  192. -- Locked characters can't be manually swapped in or out of the party.
  193. --
  194. -- @param character ID of the character to lock.
  195. set_party_lock = function(character, lock)
  196. if Characters[character] == nil then
  197. print("Tried set/unset party lock to invalid character \"" .. character .. "\".");
  198. return
  199. end
  200. Characters[character].locked = lock;
  201. end
  202. --- Checks if a character is locked in the party.
  203. --
  204. -- Locked characters can't be manually swapped in or out of the party.
  205. --
  206. -- @param character ID of the character to check for locks.
  207. get_party_lock = function(character)
  208. if Characters[character] == nil then
  209. print("Tried get party lock from invalid character \"" .. character .. "\".");
  210. return false
  211. end
  212. return Characters[character].locked
  213. end
  214. --- Enables or disables a character is the PHS.
  215. --
  216. -- Disabled characters don't appear in the PHS.
  217. --
  218. -- @param character ID of the character to enable or disable.
  219. -- @param enable True to enable, false to disable.
  220. set_character_enable = function(character, enable)
  221. if Characters[character] == nil then
  222. print("Tried enable/disable invalid character \"" .. character .. "\".");
  223. return
  224. end
  225. Characters[character].enabled = enable;
  226. end
  227. --- Checks if a character is enabled in the PHS.
  228. --
  229. -- Disabled characters don't appear in the PHS.
  230. --
  231. -- @param character ID of the character to check.
  232. -- @return True if enabled, false if disables.
  233. get_character_enable = function(character)
  234. if Characters[character] == nil then
  235. print("Tried get enabled from invalid character \"" .. character .. "\".");
  236. return false
  237. end
  238. return Characters[character].enabled;
  239. end
  240. --- Checks if a character is in the current party (IFPRTYQ).
  241. --
  242. -- @param character ID of the character to check.
  243. -- @return True if the character is in one of the party slots.
  244. IFPRTYQ = function(character)
  245. for i = 1, 3 do
  246. if Party[i] == character then return true end
  247. end
  248. return false
  249. end
  250. --- Checks if a character is available as a party member (IFMEMBQ).
  251. --
  252. -- A character is available when it is enabled in the PHS, whether it is in the party or not.
  253. --
  254. -- @param character ID of the character to check.
  255. -- @return True if the character is enabled.
  256. IFMEMBQ = function(character)
  257. return Characters[character] ~= nil and Characters[character].enabled == true
  258. end
  259. --- Adds items to the inventory.
  260. --
  261. -- Inventory quantities will be capped at 99.
  262. --
  263. -- @param item Item ID.
  264. -- @param quantity Quantity to add.
  265. Inventory.add_item = function(item, quantity)
  266. -- TODO: Also check weapons, armors and accessories.
  267. if Game.Items[item] == nil then
  268. print("Tried add invalid item \"" .. item .. "\".");
  269. return
  270. end
  271. for i = 1, MAX_INVENTORY_SLOTS do
  272. if Inventory[i] ~= nil and Inventory[i].item == item then
  273. -- Item already in inventory, add quantity
  274. Inventory[i].quantity = Inventory[i].quantity + quantity
  275. if Inventory[i].quantity > 99 then
  276. Inventory[i].quantity = 99
  277. end
  278. return
  279. end
  280. end
  281. -- Item not in inventory, search for the first available position.
  282. for i = 1, MAX_INVENTORY_SLOTS do
  283. if Inventory[i] == nil then
  284. Inventory[i] = {item = item, quantity = quantity}
  285. if Inventory[i].quantity > 99 then
  286. Inventory[i].quantity = 99
  287. end
  288. return
  289. end
  290. end
  291. -- If reached this point, it means that the item is not in inventory, and all slots are full.
  292. -- TODO: The orignal game does nothing, but in here?
  293. return
  294. end
  295. --- Checks how many items of a type are in the inventory.
  296. --
  297. -- @param item The item ID.
  298. -- @return Number of the items in the inventory.
  299. Inventory.get_item_quantity = function(item)
  300. local qty = 0
  301. for i = 1, MAX_INVENTORY_SLOTS do
  302. if Inventory[i] ~= nil and Inventory[i].item == item then
  303. qty = qty + Inventory[i].quantity
  304. end
  305. end
  306. return qty
  307. end
  308. --- Removes items from the inventory.
  309. --
  310. -- Negative quantities can't remain in the inventory. If there are less than requested to remove,
  311. -- all of them will be removed.
  312. --
  313. -- @param item Item ID.
  314. -- @param quantity Quantity to remove.
  315. Inventory.remove_item = function(item, quantity)
  316. for i = 1, MAX_INVENTORY_SLOTS do
  317. if Inventory[i] ~= nil and Inventory[i].item == item then
  318. -- Item already in inventory, add quantity
  319. Inventory[i].quantity = Inventory[i].quantity - quantity
  320. if Inventory[i].quantity <= 0 then
  321. Inventory[i] = nil
  322. end
  323. return
  324. end
  325. end
  326. end
  327. --- Adds a key item.
  328. --
  329. -- @param item ID of the key item (0-50).
  330. Inventory.add_key_item = function(item)
  331. Inventory.key[item] = true
  332. return
  333. end
  334. --- Removes a key item.
  335. --
  336. -- @param item ID of the key item (0-50).
  337. Inventory.remove_key_item = function(item)
  338. Inventory.key[item] = false
  339. return
  340. end
  341. --- Checks if the party has a key item.
  342. --
  343. -- @param item ID of the key item (0-50).
  344. -- @return True if the party has the key item, false otherwise.
  345. Inventory.has_key_item = function(item)
  346. return Inventory.key[item]
  347. end
  348. --- Adds money.
  349. --
  350. -- @param amount The money to add.
  351. Inventory.add_money = function(amount)
  352. Inventory.money = Inventory.money + amount
  353. if Inventory.money > 99999999 then
  354. Inventory.money = 99999999
  355. end
  356. end
  357. --- Removes money.
  358. --
  359. -- Negative quantities can't remain as money. If there are less than requested to remove, all of
  360. -- it will be removed.
  361. --
  362. -- @param amount Money to remove.
  363. Inventory.remove_money = function(amount)
  364. Inventory.money = Inventory.money - amount
  365. if Inventory.money < 0 then
  366. Inventory.money = 0
  367. end
  368. end
  369. --- Copies the amount of gil the party has into data banks
  370. --
  371. -- As the gil amount is a four-byte value, in the original game the arguments require two
  372. -- destination addresses to place two two-byte values into. Address 1 takes the lower two bytes of
  373. -- the gil amount, while address 2 takes the higher two bytes.
  374. --
  375. -- @param b1 Bank of the first destination (lower two bytes).
  376. -- @param b2 Bank of the second destination (higher two bytes).
  377. -- @param a1 Address in the first bank.
  378. -- @param a2 Address in the second bank.
  379. Inventory.money_to_banks = function(b1, b2, a1, a2)
  380. Banks[b1][a1] = Inventory.money % 65536
  381. Banks[b2][a2] = math.floor(Inventory.money / 65536)
  382. end
  383. --- Sorts the items in the inventory.
  384. --
  385. -- @param criteria {@see Inventory.ORDER.CUSTOM} (unimplemented), {@see Inventory.ORDER.FIELD.},
  386. -- {@see Inventory.ORDER.BATTLE}, {@see Inventory.ORDER.THROW}, {@see Inventory.ORDER.TYPE},
  387. -- {@see Inventory.ORDER.NAME}, {@see Inventory.ORDER.MOST} or {@see Inventory.ORDER.LEAST}. Any
  388. -- othe rvalue and the items will not be sorted.
  389. Inventory.sort = function(criteria)
  390. if criteria == Inventory.ORDER.CUSTOM then
  391. print("Custom order unimplemented")
  392. elseif criteria == Inventory.ORDER.FIELD then
  393. table.sort(Inventory, function (left, right)
  394. if Game.Items[left.item].menu ~= Game.Items[right.item].menu then
  395. return Game.Items[left.item].menu > Game.Items[right.item].menu
  396. end
  397. return left.item < right.item
  398. end
  399. )
  400. elseif criteria == Inventory.ORDER.BATTLE then
  401. table.sort(Inventory, function (left, right)
  402. if Game.Items[left.item].battle ~= Game.Items[right.item].battle then
  403. return Game.Items[left.item].battle > Game.Items[right.item].battle
  404. end
  405. return left.item < right.item
  406. end
  407. )
  408. elseif criteria == Inventory.ORDER.THROW then
  409. table.sort(Inventory, function (left, right)
  410. if Game.Items[left.item].throw ~= Game.Items[right.item].throw then
  411. return Game.Items[left.item].throw > Game.Items[right.item].throw
  412. end
  413. return left.item < right.item
  414. end
  415. )
  416. elseif criteria == Inventory.ORDER.TYPE then
  417. table.sort(Inventory, function (left, right)
  418. return left.item < right.item
  419. end
  420. )
  421. elseif criteria == Inventory.ORDER.NAME then
  422. table.sort(Inventory, function (left, right)
  423. return Game.Items[left.item].name < Game.Items[right.item].name
  424. end
  425. )
  426. elseif criteria == Inventory.ORDER.MOST then
  427. table.sort(Inventory, function (left, right)
  428. if left.quantity ~= right.quantity then
  429. return left.quantity > right.quantity
  430. end
  431. return left.item < right.item
  432. end
  433. )
  434. elseif criteria == Inventory.ORDER.LEAST then
  435. table.sort(Inventory, function (left, right)
  436. if left.quantity ~= right.quantity then
  437. return left.quantity < right.quantity
  438. end
  439. return left.item < right.item
  440. end
  441. )
  442. end
  443. end
  444. Characters.recalculate_stats = function(id)
  445. -- Initialize
  446. local stats = Characters[id].stats
  447. stats.str.total = stats.str.base
  448. stats.vit.total = stats.vit.base
  449. stats.mag.total = stats.mag.base
  450. stats.spr.total = stats.spr.base
  451. stats.dex.total = stats.dex.base
  452. stats.lck.total = stats.lck.base
  453. stats.hp.max = stats.hp.base
  454. stats.mp.max = stats.mp.base
  455. -- Weapon bonus. HP and MP are percents of base, the rest absolutes. The stats are keyed by name,
  456. -- so they are walked with pairs (ipairs only walks the array part and found nothing).
  457. local weapon = Game.Items[Characters[id].weapon.id]
  458. for stat, value in pairs(weapon.stats or {}) do
  459. if stat == "str" then
  460. stats.str.total = stats.str.total + value
  461. elseif stat == "vit" then
  462. stats.vit.total = stats.vit.total + value
  463. elseif stat == "mag" then
  464. stats.mag.total = stats.mag.total + value
  465. elseif stat == "spr" then
  466. stats.spr.total = stats.spr.total + value
  467. elseif stat == "dex" then
  468. stats.dex.total = stats.dex.total + value
  469. elseif stat == "lck" then
  470. stats.lck.total = stats.lck.total + value
  471. elseif stat == "hp" then
  472. stats.hp.max = stats.hp.max + (value * stats.hp.max / 100)
  473. elseif stat == "mp" then
  474. stats.mp.max = stats.mp.max + (value * stats.mp.max / 100)
  475. end
  476. end
  477. -- Armor bonus. HP and MP are percents of bsae, the rest absolutes.
  478. local armor = Game.Items[Characters[id].armor.id]
  479. for stat, value in pairs(armor.stats or {}) do
  480. if stat == "str" then
  481. stats.str.total = stats.str.total + value
  482. elseif stat == "vit" then
  483. stats.vit.total = stats.vit.total + value
  484. elseif stat == "mag" then
  485. stats.mag.total = stats.mag.total + value
  486. elseif stat == "spr" then
  487. stats.spr.total = stats.spr.total + value
  488. elseif stat == "dex" then
  489. stats.dex.total = stats.dex.total + value
  490. elseif stat == "lck" then
  491. stats.lck.total = stats.lck.total + value
  492. elseif stat == "hp" then
  493. stats.hp.max = stats.hp.max + (value * stats.hp.max / 100)
  494. elseif stat == "mp" then
  495. stats.mp.max = stats.mp.max + (value * stats.mp.max / 100)
  496. end
  497. end
  498. -- Accesory bonus
  499. local accessory = Game.Items[Characters[id].accessory]
  500. if (accessory ~= nil) then
  501. for stat, value in pairs(accessory.stats or {}) do
  502. if stat == "str" then
  503. stats.str.total = stats.str.total + value
  504. elseif stat == "vit" then
  505. stats.vit.total = stats.vit.total + value
  506. elseif stat == "mag" then
  507. stats.mag.total = stats.mag.total + value
  508. elseif stat == "spr" then
  509. stats.spr.total = stats.spr.total + value
  510. elseif stat == "dex" then
  511. stats.dex.total = stats.dex.total + value
  512. elseif stat == "lck" then
  513. stats.lck.total = stats.lck.total + value
  514. elseif stat == "hp" then
  515. stats.hp.max = stats.hp.max + (value * stats.hp.max / 100)
  516. elseif stat == "mp" then
  517. stats.mp.max = stats.mp.max + (value * stats.mp.max / 100)
  518. end
  519. end
  520. end
  521. -- Get a list of materias
  522. local materias = {}
  523. for m = 0, 7 do
  524. if Characters[id].weapon.materia[m] ~= nil then
  525. table.insert(materias, Game.Materia[Characters[id].weapon.materia[m].id])
  526. end
  527. if Characters[id].armor.materia[m] ~= nil then
  528. table.insert(materias, Game.Materia[Characters[id].armor.materia[m].id])
  529. end
  530. end
  531. -- Get materia bonuses. All are percents of base.
  532. for _, materia in ipairs(materias) do
  533. for stat, value in pairs(materia.stats or {}) do
  534. if stat == "str" then
  535. stats.str.total = stats.str.total + (value * stats.str.base / 100)
  536. elseif stat == "vit" then
  537. stats.vit.total = stats.vit.total + (value * stats.vit.base / 100)
  538. elseif stat == "mag" then
  539. stats.mag.total = stats.mag.total + (value * stats.mag.base / 100)
  540. elseif stat == "spr" then
  541. stats.spr.total = stats.spr.total + (value * stats.spr.base / 100)
  542. elseif stat == "dex" then
  543. stats.dex.total = stats.dex.total + (value * stats.dex.base / 100)
  544. elseif stat == "lck" then
  545. stats.lck.total = stats.lck.total + (value * stats.lck.base / 100)
  546. elseif stat == "hp" then
  547. stats.hp.max = stats.hp.max + (value * stats.hp.max / 100)
  548. elseif stat == "mp" then
  549. stats.mp.max = stats.mp.max + (value * stats.mp.max / 100)
  550. end
  551. end
  552. end
  553. -- Round down all basic stats
  554. stats.str.total = math.floor(stats.str.total)
  555. stats.vit.total = math.floor(stats.vit.total)
  556. stats.mag.total = math.floor(stats.mag.total)
  557. stats.spr.total = math.floor(stats.spr.total)
  558. stats.dex.total = math.floor(stats.dex.total)
  559. stats.lck.total = math.floor(stats.lck.total)
  560. stats.hp.max = math.floor(stats.hp.max)
  561. stats.mp.max = math.floor(stats.mp.max)
  562. -- HP<->MP materia swap the maximum HP and MP. TODO: Unverified: its levels (it is done at any level).
  563. for _, materia in ipairs(materias) do
  564. if materia ~= nil and materia.type == Materia.TYPE.INDEPENDENT and materia.ability ~= nil
  565. and materia.ability.id == Materia.ABILITY.HP_MP then
  566. stats.hp.max, stats.mp.max = stats.mp.max, stats.hp.max
  567. break
  568. end
  569. end
  570. -- The current values can't be over the maximums.
  571. if stats.hp.current ~= nil then stats.hp.current = math.min(stats.hp.current, math.max(1, stats.hp.max)) end
  572. if stats.mp.current ~= nil then stats.mp.current = math.min(stats.mp.current, math.max(0, stats.mp.max)) end
  573. if stats.str.total < 1 then
  574. stats.str.total = 1
  575. end
  576. if stats.vit.total < 1 then
  577. stats.vit.total = 1
  578. end
  579. if stats.mag.total < 1 then
  580. stats.mag.total = 1
  581. end
  582. if stats.spr.total < 1 then
  583. stats.spr.total = 1
  584. end
  585. if stats.dex.total < 1 then
  586. stats.dex.total = 1
  587. end
  588. if stats.lck.total < 1 then
  589. stats.lck.total = 1
  590. end
  591. if stats.hp.max < 1 then
  592. stats.hp.max = 1
  593. end
  594. if stats.mp.max < 1 then
  595. stats.mp.max = 1
  596. end
  597. -- Calculate composed stats.
  598. stats.atk = stats.str.total + weapon.power
  599. stats.acc = weapon.accuracy
  600. stats.def = stats.vit.total + armor.defense.physical
  601. stats.eva = armor.evasion.physical
  602. stats.matk = stats.mag.total
  603. stats.mdef = stats.spr.total + armor.defense.magical
  604. stats.meva = armor.evasion.magical
  605. -- Cap composed stats. TODO: Max caps?
  606. if stats.atk < 1 then
  607. stats.atk = 1
  608. end
  609. if stats.acc < 1 then
  610. stats.acc = 1
  611. end
  612. if stats.def < 1 then
  613. stats.def = 1
  614. end
  615. if stats.eva < 1 then
  616. stats.eva = 1
  617. end
  618. if stats.matk < 1 then
  619. stats.matk = 1
  620. end
  621. if stats.mdef < 1 then
  622. stats.mdef = 1
  623. end
  624. if stats.meva < 1 then
  625. stats.meva = 1
  626. end
  627. -- Cap current HP/MP
  628. if stats.hp.current > stats.hp.max then
  629. stats.hp.current = stats.hp.max
  630. end
  631. if stats.mp.current > stats.mp.max then
  632. stats.mp.current = stats.mp.max
  633. end
  634. -- Assign
  635. Characters[id].stats = stats
  636. end
  637. --- Sends the materia that doesn't fit in a piece of equipment back to the materia inventory.
  638. --
  639. -- @param char_id ID of the character.
  640. -- @param part "weapon" or "armor".
  641. -- @param slots Number of materia slots the equipment will have. Slots are 0-7 in the character.
  642. Characters.remove_excess_materia = function(char_id, part, slots)
  643. local materia = Characters[char_id][part].materia
  644. for m = slots, 7 do
  645. if materia[m] ~= nil then
  646. Materia.add(materia[m].id, materia[m].ap)
  647. materia[m] = nil
  648. end
  649. end
  650. end
  651. --- Equips an item on a character.
  652. --
  653. -- Works for weapons, armors and accessories. If there is a previously equiped item, it
  654. -- will be unequiped and added to the inventory. The equiped item will be removed form the
  655. -- inventory. For armors and weapons, if the newly equiped item has less materia slots than
  656. -- the previuos one, remainig materia will be unequiped and added to the materia inventory.
  657. --
  658. -- @param char_id ID of the character to equip the item to.
  659. -- @param item_id ID of the item to equip.
  660. -- @return True if the item was equipped. False if the character or item don't exist, if
  661. -- the item is not in the inventory, or if the item is not equippable or not equippable by
  662. -- the selected character. In any of those cases, an error message will be printed.
  663. Characters.equip = function(char_id, item_id)
  664. -- Error checks
  665. if Characters[char_id] == nil then
  666. print("Tried to equip item on wrong character: " .. tostring(char_id))
  667. return false
  668. elseif Game.Items[item_id] == nil then
  669. print("Tried to equip invalid item: " .. tostring(item_id))
  670. return false
  671. elseif Inventory.get_item_quantity(item_id) < 1 then
  672. print("Tried to equip item not in inventory: " .. item_id .. " (" .. Game.Items[item_id].name .. ")")
  673. return false
  674. end
  675. local type = Game.Items[item_id].type
  676. if type ~= Inventory.ITEM_TYPE.WEAPON and type ~= Inventory.ITEM_TYPE.ARMOR and type ~= Inventory.ITEM_TYPE.ACCESSORY then
  677. print("Tried to equip non-equipable item: " .. item_id .. " (" .. Game.Items[item_id].name .. ")")
  678. return false
  679. end
  680. local equipable = false
  681. for _, u in ipairs(Game.Items[item_id].users) do
  682. if u == char_id then
  683. equipable = true
  684. break
  685. end
  686. end
  687. if equipable == false then
  688. print("Tried to equip item " .. tostring(item_id) .. " (" .. Game.Items[item_id].name .. ") on a non valid character: " .. tostring(char_id) .. " (" .. Characters[char_id].name .. ")")
  689. return false
  690. end
  691. -- All is good, equip.
  692. if type == Inventory.ITEM_TYPE.ACCESSORY then
  693. -- Equip accessory. If any was equipped, send it to the inventory.
  694. if Characters[char_id].accessory ~= nil then
  695. Inventory.add_item(Characters[char_id].accessory, 1)
  696. end
  697. Characters[char_id].accessory = item_id
  698. Inventory.remove_item(item_id, 1)
  699. elseif type == Inventory.ITEM_TYPE.WEAPON then
  700. -- Weapon. Remove excess materia, send previuos one to the inventory.
  701. Characters.remove_excess_materia(char_id, "weapon", #(Game.Items[item_id].slots))
  702. Inventory.add_item(Characters[char_id].weapon.id, 1)
  703. Characters[char_id].weapon.id = item_id
  704. Inventory.remove_item(item_id, 1)
  705. elseif type == Inventory.ITEM_TYPE.ARMOR then
  706. -- Armor. Remove excess materia, send previuos one to the inventory.
  707. Characters.remove_excess_materia(char_id, "armor", #(Game.Items[item_id].slots))
  708. Inventory.add_item(Characters[char_id].armor.id, 1)
  709. Characters[char_id].armor.id = item_id
  710. Inventory.remove_item(item_id, 1)
  711. else
  712. return false
  713. end
  714. -- The stats change with the equipment (and the maximum HP and MP, so the current ones may have to be capped).
  715. Characters.recalculate_stats(char_id)
  716. return true
  717. end
  718. --- Removes all materia from a character.
  719. --
  720. -- @param id ID of the character to remove materia from.
  721. Characters.remove_materia = function(id)
  722. -- TODO: Implement
  723. end
  724. --- Restores all materia to a character.
  725. --
  726. -- @param id ID of the character to restore materia to.
  727. Characters.restore_materia = function(id)
  728. -- TODO: Implement
  729. end
  730. Materia.add = function(id, ap)
  731. local ap = ap or 0
  732. if Game.Materia[id] == nil then
  733. print("Tried to add invalid materia \"" .. tostring(id) .. "\".");
  734. return
  735. end
  736. -- TODO Cap AP?
  737. for i = 1, MAX_MATERIA_SLOTS do
  738. if Materia[i] == nil then
  739. Materia[i] = {id = id, ap = ap}
  740. if Materia[i].id == Materia.ENEMY_SKILL_ID then
  741. Materia[i].ap = 0
  742. Materia[i].skills = {}
  743. for s = 1, 24 do
  744. Materia[i].skills[s] = false
  745. end
  746. end
  747. return
  748. end
  749. end
  750. -- If reached this point, it means that the materia wasn't added because all slots are full.
  751. -- TODO: Do something.
  752. return
  753. end
  754. --- Splits the party in teams.
  755. --
  756. -- @param number Number of teams (only 2 or 3).
  757. Party.split_teams = function(number)
  758. if number < 2 then
  759. number = 2
  760. elseif number > 3 then
  761. number = 3
  762. end
  763. -- TODO: Implement.
  764. end
  765. --- Displays a menu to choose a party of three.
  766. Party.choose_party = function()
  767. -- TODO: Implement
  768. end
  769. --- Special event. Removes all materia from the party, equipped and unequipped.
  770. Party.steal_materia = function()
  771. -- TODO: Implement.
  772. -- TODO: How is this reversed?
  773. end
  774. --- Integer division of the field scripts (DIV, DIV2).
  775. --
  776. -- @param a Dividend.
  777. -- @param b Divisor. The result is the dividend if it is zero.
  778. -- @return The integer part of a / b.
  779. function math_div(a, b)
  780. if b == 0 then return a end
  781. return math.floor(a / b)
  782. end
  783. --- Implementation of OR bit operation.
  784. --
  785. -- @param a First operand.
  786. -- @param b Second operand.
  787. -- @return a | b.
  788. function bit_or(a, b)
  789. local r, m, s = 0, 2 ^ 31
  790. repeat
  791. s, a, b = a + b + m, a % m, b % m
  792. r, m = r + m * 1 % (s - a - b), m / 2
  793. until m < 1
  794. return r
  795. end
  796. --- Implementation of XOR bit operation.
  797. --
  798. -- @param a First operand.
  799. -- @param b Second operand.
  800. -- @return a ^^ b.
  801. function bit_xor(a, b)
  802. local r, m, s = 0, 2 ^ 31
  803. repeat
  804. s, a, b = a + b + m, a % m, b % m
  805. r, m = r + m * 3 % (s - a - b), m / 2
  806. until m < 1
  807. return r
  808. end
  809. --- Implementation of AND bit operation.
  810. --
  811. -- @param a First operand.
  812. -- @param b Second operand.
  813. -- @return a & b.
  814. function bit_and(a, b)
  815. local r, m, s = 0, 2 ^ 31
  816. repeat
  817. s, a, b = a + b + m, a % m, b % m
  818. r, m = r + m * 4 % (s - a - b), m / 2
  819. until m < 1
  820. return r
  821. end
  822. --- Obtains a bit from an address in a data bank
  823. --
  824. -- @param bank The bank (0-14)
  825. -- @param address The bank address (0-254)
  826. -- @param bit The bit to get (0-7)
  827. -- @return The bit value (1-0)
  828. function get_bank_bit(bank, address, bit)
  829. if bank < 0 or bank > Banks.COUNT or address < 0 or address > Banks.SIZE or bit < 0 or bit > 7 then
  830. print("ERROR: Tried to check invalid bit " .. tostring(bit) .. " from bank " .. tostring(bank) .. " at address " .. tostring(address) .. ".")
  831. return 0
  832. end
  833. local val = Banks[bank][address]
  834. return get_byte_nth_bit(val, bit)
  835. end
  836. --- Sets a bit in an address in a data bank
  837. --
  838. -- @param bank The bank (0-14)
  839. -- @param address The bank address (0-254)
  840. -- @param bit The bit to set (0-7)
  841. -- @param value The bit value (1-0)
  842. function set_bank_bit(bank, address, bit, value)
  843. if bank < 0 or bank > Banks.COUNT or address < 0 or address > Banks.SIZE or bit < 0 or bit > 7 then
  844. print("ERROR: Tried to set invalid bit " .. tostring(bit) .. " from bank " .. tostring(bank) .. " at address " .. tostring(address) .. ".")
  845. return
  846. end
  847. if value ~= 1 and value ~= 0 then
  848. print("ERROR: Tried to set bit to invalid value " .. tostring(value) .. " from bank " .. tostring(bank) .. " at address " .. tostring(address) .. ".")
  849. return
  850. end
  851. local val = Banks[bank][address]
  852. Banks[bank][address] = set_byte_nth_bit(val, bit, value)
  853. end
  854. --- Obtains a bit from an unsigned byte
  855. --
  856. -- @param value The value (0-254)
  857. -- @param bit The bit to get (0-7)
  858. -- @return The bit value (1-0)
  859. function get_byte_nth_bit(value, n)
  860. if n < 0 or n > 7 then
  861. print("ERROR: Tried to get invalid bit " .. tostring(bit) .. " from value " .. tostring(value) .. ".")
  862. return 0
  863. end
  864. if value < 0 then
  865. print("ERROR: Tried to get bit " .. tostring(bit) .. " from negative value " .. tostring(value) .. ".")
  866. return 0
  867. end
  868. if value > 254 then
  869. print("ERROR: Tried to get bit " .. tostring(bit) .. " from value " .. tostring(value) .. " that exceeds byte size.")
  870. return 0
  871. end
  872. -- Convert to binary string
  873. local s = ""
  874. local temp = value
  875. for i = 1, 8 do
  876. s = s .. tostring(temp % 2)
  877. temp = math.floor(temp / 2)
  878. end
  879. return tonumber(string.sub(s, n + 1, n + 1))
  880. end
  881. --- Obtains a bit from an unsigned byte
  882. --
  883. -- @param value The value (0-254)
  884. -- @param bit The bit to get (0-7)
  885. -- @return The bit value (1-0)
  886. function set_byte_nth_bit(value, n, bit)
  887. if bit ~= 0 and bit ~= 1 then
  888. print("ERROR: Tried to set bit " .. tostring(bit) .. " to an invalid value " .. tostring(bit) .. " from value " .. tostring(value) .. ".")
  889. return value
  890. end
  891. if n < 0 or n > 7 then
  892. print("ERROR: Tried to set invalid bit " .. tostring(bit) .. " to " .. tostring(bit) .. " from value " .. tostring(value) .. ".")
  893. return value
  894. end
  895. if value < 0 then
  896. print("ERROR: Tried to set bit " .. tostring(bit) .. " to " .. tostring(bit) .. "from negative value " .. tostring(value) .. ".")
  897. return value
  898. end
  899. if value > 254 then
  900. print("ERROR: Tried to set bit " .. tostring(bit) .. " to " .. tostring(bit) .. "from value " .. tostring(value) .. " that exceeds byte size.")
  901. return value
  902. end
  903. -- Convert to binary string.
  904. local s = ""
  905. local temp = value
  906. for i = 1, 8 do
  907. s = s .. tostring(temp % 2)
  908. temp = math.floor(temp / 2)
  909. end
  910. -- Replace the requested bit.
  911. s = string.sub(s, 1, n) .. tostring(bit) .. string.sub(s, n + 2)
  912. -- Convert back to a number.
  913. val = 0
  914. for i = 1, 8 do
  915. val = val + (tonumber(string.sub(s, i, i )) * 2 ^ (i - 1))
  916. end
  917. return math.floor(val)
  918. end
  919. set_byte_nth_bit(25, 2, 1)