--- Abilities of the characters that come from their equipped materia. --- Command of the Enemy Skill materia (see Game.Commands). Characters.E_SKILL_COMMAND = 13 --- ID of the attack of the first enemy skill. There are 24, in the order of the Enemy Skill materia. Characters.FIRST_E_SKILL_ATTACK = 72 --- Gets the level of a materia given its AP. -- -- @param materia Definition of the materia (an entry of Game.Materia). -- @param ap AP of the materia. -- @return The level (1 or more), or 0 if the AP are less than needed for the first level. local function get_materia_level(materia, ap) local level = 0 for l = 1, #materia.levels_ap do if materia.levels_ap[l] <= ap then level = l end end return level end --- See get_materia_level, the stats need it too. Characters.get_materia_level = get_materia_level --- Gets the magic, summons and enemy skills a character can use with the materia that is equipped. -- -- Every ability is listed once, even if more than one materia gives it. -- -- @param char_id ID of the character. -- @return A table with these lists, sorted by attack ID: -- * magic: Attack IDs of the spells. -- * magic_groups: The same spells, in lists with the ones that come from the same materia. The -- groups are sorted by their first spell. -- * summon: Tables {id = attack ID, times = times it can be used per battle (255 is no limit)}. -- * e_skill: Attack IDs of the enemy skills that were learned. Characters.get_abilities = function(char_id) local result = { magic = {}, magic_groups = {}, summon = {}, e_skill = {} } local character = Characters[char_id] if character == nil then return result end local magic_seen = {} local e_skill_seen = {} local summon_by_id = {} for _, part in ipairs({ "weapon", "armor" }) do local equipment = character[part] -- Slots are 0-7. for slot = 0, 7 do local equipped = equipment.materia[slot] local materia = (equipped ~= nil) and Game.Materia[equipped.id] or nil if materia ~= nil then local level = get_materia_level(materia, equipped.ap or 0) if materia.type == Materia.TYPE.MAGIC and materia.magic ~= nil then local group = {} for spell_level, spell in ipairs(materia.magic) do if level >= spell_level and not magic_seen[spell] then magic_seen[spell] = true result.magic[#result.magic + 1] = spell group[#group + 1] = spell end end if #group > 0 then result.magic_groups[#result.magic_groups + 1] = group end elseif materia.type == Materia.TYPE.SUMMON and materia.summon ~= nil and level > 0 then local times = materia.times[level] or 1 local existing = summon_by_id[materia.summon] if existing == nil then existing = { id = materia.summon, times = times } summon_by_id[materia.summon] = existing result.summon[#result.summon + 1] = existing elseif times > existing.times then existing.times = times end elseif materia.type == Materia.TYPE.COMMAND and materia.command ~= nil then for _, command in ipairs(materia.command) do if command == Characters.E_SKILL_COMMAND and equipped.skills ~= nil then for skill = 1, 24 do local attack = Characters.FIRST_E_SKILL_ATTACK + skill - 1 if equipped.skills[skill] == true and not e_skill_seen[attack] then e_skill_seen[attack] = true result.e_skill[#result.e_skill + 1] = attack end end end end end end end end table.sort(result.magic) table.sort(result.magic_groups, function(a, b) return a[1] < b[1] end) table.sort(result.e_skill) table.sort(result.summon, function(a, b) return a.id < b.id end) return result end --- Gets the commands a character has in battle, in the order the battle menu shows them. -- -- The Attack command (or the materia that replace it, like Slash-All or Flash) goes first. Then -- come the commands of the materia, Item (unless W-Item is equipped), Magic and Summon (if the -- character has spells or summons, and no W-Magic or W-Summon is equipped). The Limit command is -- not included, as it only appears in battle when the limit bar is full. -- -- @param char_id ID of the character. -- @return List of IDs of commands (see Game.Commands). Characters.get_commands = function(char_id) local commands = { 1 } local character = Characters[char_id] if character == nil then return commands end local w_magic, w_summon, w_item = false, false, false for _, part in ipairs({ "weapon", "armor" }) do local equipment = character[part] for slot = 0, 7 do local equipped = equipment.materia[slot] local materia = (equipped ~= nil) and Game.Materia[equipped.id] or nil if materia ~= nil and materia.type == Materia.TYPE.COMMAND and materia.command ~= nil then local level = get_materia_level(materia, equipped.ap or 0) local command for l, id in ipairs(materia.command) do if level >= l then command = id end end if command ~= nil then if command == 1 or command == 20 or (command >= 24 and command <= 27) then commands[1] = command else commands[#commands + 1] = command end if command == 21 then w_magic = true end if command == 22 then w_summon = true end if command == 23 then w_item = true end end end end end local abilities = Characters.get_abilities(char_id) if not w_item then commands[#commands + 1] = 4 end if #abilities.magic > 0 and not w_magic then commands[#commands + 1] = 2 end if #abilities.summon > 0 and not w_summon then commands[#commands + 1] = 3 end local function substitute(id) return id == 1 or id == 20 or (id >= 24 and id <= 27) end table.sort(commands, function(l, r) if substitute(l) ~= substitute(r) then return substitute(l) end return l < r end) return commands end --- Gets what the independent materia (Long Range, Mega All, Counter Attack, Cover, Pre-Emptive, EXP -- Plus and Gil Plus) of a character do in battle. -- -- @param char_id ID of the character. -- @return A table: long_range (boolean), mega_all (times per battle, 0 if none), counter_attack and -- cover (chance in percent, 0 if none), preemptive (percent points added to the chance of a pre-emptive -- attack), exp_plus and gil_plus (multipliers, 1 if none), and encounter (multiplier of the random -- encounters of the field, from Enemy Away and Enemy Lure). Characters.get_independent = function(char_id) local result = { long_range = false, mega_all = 0, counter_attack = 0, cover = 0, preemptive = 0, exp_plus = 1, gil_plus = 1, encounter = 1, } local character = Characters[char_id] if character == nil then return result end for _, part in ipairs({ "weapon", "armor" }) do for slot = 0, 7 do local equipped = character[part].materia[slot] local materia = (equipped ~= nil) and Game.Materia[equipped.id] or nil if materia ~= nil and materia.type == Materia.TYPE.INDEPENDENT and materia.ability ~= nil then local level = math.max(1, get_materia_level(materia, equipped.ap or 0)) local param = materia.ability.param or {} -- The parameter of the level, or the last one if the materia has more levels. local value = param[level] or param[#param] or 0 local id = materia.ability.id if id == Materia.ABILITY.LONG_RANGE then result.long_range = true elseif id == Materia.ABILITY.MEGA_ALL then result.mega_all = math.max(result.mega_all, value) elseif id == Materia.ABILITY.COUNTER_ATTACK then result.counter_attack = math.max(result.counter_attack, value) elseif id == Materia.ABILITY.COVER then result.cover = math.max(result.cover, value) elseif id == Materia.ABILITY.PREEMPTIVE then result.preemptive = result.preemptive + value -- The parameters of EXP Plus and Gil Plus are sixteenths: 24 is x1.5 and 32 is x2. -- Enemy Away halves the encounters (a quarter when mastered) and Enemy Lure doubles them (x4). -- TODO: Unverified, the parameters are 7 and 14. elseif id == Materia.ABILITY.ENCOUNTER_DOWN then result.encounter = result.encounter * 0.5 ^ (value / 7) elseif id == Materia.ABILITY.ENCOUNTER_UP then result.encounter = result.encounter * 2 ^ (value / 7) elseif id == Materia.ABILITY.EXP_PLUS then result.exp_plus = math.max(result.exp_plus, value / 16) elseif id == Materia.ABILITY.MONEY_PLUS then result.gil_plus = math.max(result.gil_plus, value / 16) end end end end return result end --- Gets the multiplier of the random encounters of the field that the party causes with its Enemy Away and -- Enemy Lure materia (1 if it has none). The engine asks for it in every encounter check. Party.get_encounter_modifier = function() local modifier = 1 for slot = 1, Party.MAX do if Party[slot] ~= nil and Characters[Party[slot]] ~= nil then modifier = modifier * Characters.get_independent(Party[slot]).encounter end end return modifier end --- Status of death (see BattleStatus). The characters with no HP are dead. local STATUS_DEATH = 0 --- Checks if an attack revives (cures the death status). local function revives(attack) for _, effect in ipairs(attack.status or {}) do -- Mode 1 is cure. if effect.status == STATUS_DEATH and effect.mode == 1 then return true end end return false end --- Gets the maximum of a stat (HP or MP) of a character. -- @param stat The stat, with the fields max (if it was calculated) and base. local function get_stat_max(stat) return stat.max or stat.base end --- Casts a spell from the menu, out of battle: the cure and life families. -- -- The spell heals (or revives) the target and the caster pays the MP. If the spell can't be cast -- (not enough MP, the target is dead for a cure, alive for a life, or already has all its HP), it -- does nothing and costs nothing. -- -- The amounts are the ones of the battle: cures heal the magic base times the power / 16, and life -- spells a fraction of the maximum HP (power / 32). -- -- @param caster_id ID of the character that casts. -- @param attack_id ID of the attack (spell). -- @param target_id ID of the character that receives it. -- @return True if it was cast, and the HP that were restored. Characters.cast_menu_spell = function(caster_id, attack_id, target_id) local attack = Game.Attacks[attack_id] local caster = Characters[caster_id] local target = Characters[target_id] if attack == nil or caster == nil or target == nil then return false end local cost = attack.mp or 0 if caster.stats.mp.current < cost then return false end local hp = target.stats.hp local hp_max = get_stat_max(hp) local dead = hp.current <= 0 local power = attack.power or 0 local amount = 0 if revives(attack) then -- Life: only works on dead characters. A quarter of the HP if the power is unknown. if not dead then return false end amount = hp_max * ((power > 0) and power or 8) / 32 elseif attack.dmg_formula == 2 and attack.dmg_modifier == 5 then -- Cure: doesn't work on dead characters, or on the ones with all their HP. if dead or hp.current >= hp_max then return false end local mag = caster.stats.mag.total or caster.stats.mag.base or 1 local level = caster.level or 1 local base = 6 * (mag + ((mag + level) / 32) * ((mag * level) / 32)) amount = base * power / 16 * (15 + math.random()) / 16 else return false end amount = math.max(1, math.min(math.floor(amount), hp_max - hp.current)) hp.current = hp.current + amount caster.stats.mp.current = caster.stats.mp.current - cost return true, amount end