character_abilities.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. --- Abilities of the characters that come from their equipped materia.
  2. --- Command of the Enemy Skill materia (see Game.Commands).
  3. Characters.E_SKILL_COMMAND = 13
  4. --- ID of the attack of the first enemy skill. There are 24, in the order of the Enemy Skill materia.
  5. Characters.FIRST_E_SKILL_ATTACK = 72
  6. --- Gets the level of a materia given its AP.
  7. --
  8. -- @param materia Definition of the materia (an entry of Game.Materia).
  9. -- @param ap AP of the materia.
  10. -- @return The level (1 or more), or 0 if the AP are less than needed for the first level.
  11. local function get_materia_level(materia, ap)
  12. local level = 0
  13. for l = 1, #materia.levels_ap do
  14. if materia.levels_ap[l] <= ap then level = l end
  15. end
  16. return level
  17. end
  18. --- See get_materia_level, the stats need it too.
  19. Characters.get_materia_level = get_materia_level
  20. --- Gets the magic, summons and enemy skills a character can use with the materia that is equipped.
  21. --
  22. -- Every ability is listed once, even if more than one materia gives it.
  23. --
  24. -- @param char_id ID of the character.
  25. -- @return A table with these lists, sorted by attack ID:
  26. -- * magic: Attack IDs of the spells.
  27. -- * magic_groups: The same spells, in lists with the ones that come from the same materia. The
  28. -- groups are sorted by their first spell.
  29. -- * summon: Tables {id = attack ID, times = times it can be used per battle (255 is no limit)}.
  30. -- * e_skill: Attack IDs of the enemy skills that were learned.
  31. Characters.get_abilities = function(char_id)
  32. local result = { magic = {}, magic_groups = {}, summon = {}, e_skill = {} }
  33. local character = Characters[char_id]
  34. if character == nil then return result end
  35. local magic_seen = {}
  36. local e_skill_seen = {}
  37. local summon_by_id = {}
  38. for _, part in ipairs({ "weapon", "armor" }) do
  39. local equipment = character[part]
  40. -- Slots are 0-7.
  41. for slot = 0, 7 do
  42. local equipped = equipment.materia[slot]
  43. local materia = (equipped ~= nil) and Game.Materia[equipped.id] or nil
  44. if materia ~= nil then
  45. local level = get_materia_level(materia, equipped.ap or 0)
  46. if materia.type == Materia.TYPE.MAGIC and materia.magic ~= nil then
  47. local group = {}
  48. for spell_level, spell in ipairs(materia.magic) do
  49. if level >= spell_level and not magic_seen[spell] then
  50. magic_seen[spell] = true
  51. result.magic[#result.magic + 1] = spell
  52. group[#group + 1] = spell
  53. end
  54. end
  55. if #group > 0 then result.magic_groups[#result.magic_groups + 1] = group end
  56. elseif materia.type == Materia.TYPE.SUMMON and materia.summon ~= nil and level > 0 then
  57. local times = materia.times[level] or 1
  58. local existing = summon_by_id[materia.summon]
  59. if existing == nil then
  60. existing = { id = materia.summon, times = times }
  61. summon_by_id[materia.summon] = existing
  62. result.summon[#result.summon + 1] = existing
  63. elseif times > existing.times then
  64. existing.times = times
  65. end
  66. elseif materia.type == Materia.TYPE.COMMAND and materia.command ~= nil then
  67. for _, command in ipairs(materia.command) do
  68. if command == Characters.E_SKILL_COMMAND and equipped.skills ~= nil then
  69. for skill = 1, 24 do
  70. local attack = Characters.FIRST_E_SKILL_ATTACK + skill - 1
  71. if equipped.skills[skill] == true and not e_skill_seen[attack] then
  72. e_skill_seen[attack] = true
  73. result.e_skill[#result.e_skill + 1] = attack
  74. end
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end
  82. table.sort(result.magic)
  83. table.sort(result.magic_groups, function(a, b) return a[1] < b[1] end)
  84. table.sort(result.e_skill)
  85. table.sort(result.summon, function(a, b) return a.id < b.id end)
  86. return result
  87. end
  88. --- Gets the commands a character has in battle, in the order the battle menu shows them.
  89. --
  90. -- The Attack command (or the materia that replace it, like Slash-All or Flash) goes first. Then
  91. -- come the commands of the materia, Item (unless W-Item is equipped), Magic and Summon (if the
  92. -- character has spells or summons, and no W-Magic or W-Summon is equipped). The Limit command is
  93. -- not included, as it only appears in battle when the limit bar is full.
  94. --
  95. -- @param char_id ID of the character.
  96. -- @return List of IDs of commands (see Game.Commands).
  97. Characters.get_commands = function(char_id)
  98. local commands = { 1 }
  99. local character = Characters[char_id]
  100. if character == nil then return commands end
  101. local w_magic, w_summon, w_item = false, false, false
  102. for _, part in ipairs({ "weapon", "armor" }) do
  103. local equipment = character[part]
  104. for slot = 0, 7 do
  105. local equipped = equipment.materia[slot]
  106. local materia = (equipped ~= nil) and Game.Materia[equipped.id] or nil
  107. if materia ~= nil and materia.type == Materia.TYPE.COMMAND and materia.command ~= nil then
  108. local level = get_materia_level(materia, equipped.ap or 0)
  109. local command
  110. for l, id in ipairs(materia.command) do
  111. if level >= l then command = id end
  112. end
  113. if command ~= nil then
  114. if command == 1 or command == 20 or (command >= 24 and command <= 27) then
  115. commands[1] = command
  116. else
  117. commands[#commands + 1] = command
  118. end
  119. if command == 21 then w_magic = true end
  120. if command == 22 then w_summon = true end
  121. if command == 23 then w_item = true end
  122. end
  123. end
  124. end
  125. end
  126. local abilities = Characters.get_abilities(char_id)
  127. if not w_item then commands[#commands + 1] = 4 end
  128. if #abilities.magic > 0 and not w_magic then commands[#commands + 1] = 2 end
  129. if #abilities.summon > 0 and not w_summon then commands[#commands + 1] = 3 end
  130. local function substitute(id) return id == 1 or id == 20 or (id >= 24 and id <= 27) end
  131. table.sort(commands, function(l, r)
  132. if substitute(l) ~= substitute(r) then return substitute(l) end
  133. return l < r
  134. end)
  135. return commands
  136. end
  137. --- Gets what the independent materia (Long Range, Mega All, Counter Attack, Cover, Pre-Emptive, EXP
  138. -- Plus and Gil Plus) of a character do in battle.
  139. --
  140. -- @param char_id ID of the character.
  141. -- @return A table: long_range (boolean), mega_all (times per battle, 0 if none), counter_attack and
  142. -- cover (chance in percent, 0 if none), preemptive (percent points added to the chance of a pre-emptive
  143. -- attack), exp_plus and gil_plus (multipliers, 1 if none), and encounter (multiplier of the random
  144. -- encounters of the field, from Enemy Away and Enemy Lure).
  145. Characters.get_independent = function(char_id)
  146. local result = {
  147. long_range = false, mega_all = 0, counter_attack = 0, cover = 0, preemptive = 0,
  148. exp_plus = 1, gil_plus = 1, encounter = 1,
  149. }
  150. local character = Characters[char_id]
  151. if character == nil then return result end
  152. for _, part in ipairs({ "weapon", "armor" }) do
  153. for slot = 0, 7 do
  154. local equipped = character[part].materia[slot]
  155. local materia = (equipped ~= nil) and Game.Materia[equipped.id] or nil
  156. if materia ~= nil and materia.type == Materia.TYPE.INDEPENDENT and materia.ability ~= nil then
  157. local level = math.max(1, get_materia_level(materia, equipped.ap or 0))
  158. local param = materia.ability.param or {}
  159. -- The parameter of the level, or the last one if the materia has more levels.
  160. local value = param[level] or param[#param] or 0
  161. local id = materia.ability.id
  162. if id == Materia.ABILITY.LONG_RANGE then result.long_range = true
  163. elseif id == Materia.ABILITY.MEGA_ALL then result.mega_all = math.max(result.mega_all, value)
  164. elseif id == Materia.ABILITY.COUNTER_ATTACK then result.counter_attack = math.max(result.counter_attack, value)
  165. elseif id == Materia.ABILITY.COVER then result.cover = math.max(result.cover, value)
  166. elseif id == Materia.ABILITY.PREEMPTIVE then result.preemptive = result.preemptive + value
  167. -- The parameters of EXP Plus and Gil Plus are sixteenths: 24 is x1.5 and 32 is x2.
  168. -- Enemy Away halves the encounters (a quarter when mastered) and Enemy Lure doubles them (x4).
  169. -- TODO: Unverified, the parameters are 7 and 14.
  170. elseif id == Materia.ABILITY.ENCOUNTER_DOWN then result.encounter = result.encounter * 0.5 ^ (value / 7)
  171. elseif id == Materia.ABILITY.ENCOUNTER_UP then result.encounter = result.encounter * 2 ^ (value / 7)
  172. elseif id == Materia.ABILITY.EXP_PLUS then result.exp_plus = math.max(result.exp_plus, value / 16)
  173. elseif id == Materia.ABILITY.MONEY_PLUS then result.gil_plus = math.max(result.gil_plus, value / 16)
  174. end
  175. end
  176. end
  177. end
  178. return result
  179. end
  180. --- Gets the multiplier of the random encounters of the field that the party causes with its Enemy Away and
  181. -- Enemy Lure materia (1 if it has none). The engine asks for it in every encounter check.
  182. Party.get_encounter_modifier = function()
  183. local modifier = 1
  184. for slot = 1, Party.MAX do
  185. if Party[slot] ~= nil and Characters[Party[slot]] ~= nil then
  186. modifier = modifier * Characters.get_independent(Party[slot]).encounter
  187. end
  188. end
  189. return modifier
  190. end
  191. --- Status of death (see BattleStatus). The characters with no HP are dead.
  192. local STATUS_DEATH = 0
  193. --- Checks if an attack revives (cures the death status).
  194. local function revives(attack)
  195. for _, effect in ipairs(attack.status or {}) do
  196. -- Mode 1 is cure.
  197. if effect.status == STATUS_DEATH and effect.mode == 1 then return true end
  198. end
  199. return false
  200. end
  201. --- Gets the maximum of a stat (HP or MP) of a character.
  202. -- @param stat The stat, with the fields max (if it was calculated) and base.
  203. local function get_stat_max(stat)
  204. return stat.max or stat.base
  205. end
  206. --- Casts a spell from the menu, out of battle: the cure and life families.
  207. --
  208. -- The spell heals (or revives) the target and the caster pays the MP. If the spell can't be cast
  209. -- (not enough MP, the target is dead for a cure, alive for a life, or already has all its HP), it
  210. -- does nothing and costs nothing.
  211. --
  212. -- The amounts are the ones of the battle: cures heal the magic base times the power / 16, and life
  213. -- spells a fraction of the maximum HP (power / 32).
  214. --
  215. -- @param caster_id ID of the character that casts.
  216. -- @param attack_id ID of the attack (spell).
  217. -- @param target_id ID of the character that receives it.
  218. -- @return True if it was cast, and the HP that were restored.
  219. Characters.cast_menu_spell = function(caster_id, attack_id, target_id)
  220. local attack = Game.Attacks[attack_id]
  221. local caster = Characters[caster_id]
  222. local target = Characters[target_id]
  223. if attack == nil or caster == nil or target == nil then return false end
  224. local cost = attack.mp or 0
  225. if caster.stats.mp.current < cost then return false end
  226. local hp = target.stats.hp
  227. local hp_max = get_stat_max(hp)
  228. local dead = hp.current <= 0
  229. local power = attack.power or 0
  230. local amount = 0
  231. if revives(attack) then
  232. -- Life: only works on dead characters. A quarter of the HP if the power is unknown.
  233. if not dead then return false end
  234. amount = hp_max * ((power > 0) and power or 8) / 32
  235. elseif attack.dmg_formula == 2 and attack.dmg_modifier == 5 then
  236. -- Cure: doesn't work on dead characters, or on the ones with all their HP.
  237. if dead or hp.current >= hp_max then return false end
  238. local mag = caster.stats.mag.total or caster.stats.mag.base or 1
  239. local level = caster.level or 1
  240. local base = 6 * (mag + ((mag + level) / 32) * ((mag * level) / 32))
  241. amount = base * power / 16 * (15 + math.random()) / 16
  242. else
  243. return false
  244. end
  245. amount = math.max(1, math.min(math.floor(amount), hp_max - hp.current))
  246. hp.current = hp.current + amount
  247. caster.stats.mp.current = caster.stats.mp.current - cost
  248. return true, amount
  249. end