character_effects.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. --- Elemental and status effects a character gets from the equipment and the equipped materia.
  2. --
  3. -- Used by the status menu, and usable by anything that needs to know what a character resists or
  4. -- inflicts.
  5. --- Element IDs, as stored in the item and materia data.
  6. Characters.ELEMENT = {
  7. FIRE = 0, ICE = 1, BOLT = 2, EARTH = 3, POISON = 4, GRAVITY = 5, WATER = 6, WIND = 7,
  8. HOLY = 8, RESTORATIVE = 9, CUT = 10, HIT = 11, PUNCH = 12, SHOOT = 13, SHOUT = 14, HIDDEN = 15,
  9. }
  10. --- How an element affects a character. Lower is stronger.
  11. Characters.ELEMENT_MODE = {
  12. ABSORB = 0,
  13. NULLIFY = 1,
  14. HALF = 2,
  15. }
  16. --- Status IDs, as stored in the item and materia data (bit position in the kernel).
  17. Characters.STATUS = {
  18. DEATH = 0, NEAR_DEATH = 1, SLEEP = 2, POISON = 3, SADNESS = 4, FURY = 5, CONFUSION = 6,
  19. SILENCE = 7, HASTE = 8, SLOW = 9, STOP = 10, FROG = 11, SMALL = 12, SLOW_NUMB = 13,
  20. PETRIFY = 14, REGEN = 15, BARRIER = 16, MBARRIER = 17, REFLECT = 18, DUAL = 19, SHIELD = 20,
  21. D_SENTENCE = 21, MANIPULATE = 22, BERSERK = 23, PEERLESS = 24, PARALYZED = 25, DARKNESS = 26,
  22. }
  23. --- Element used in the materia data for "no element".
  24. local NO_ELEMENT = Characters.ELEMENT.HIDDEN
  25. --- Materia level (1-based) for an amount of AP.
  26. local function materia_level(definition, ap)
  27. local level = 0
  28. for _, level_ap in ipairs(definition.levels_ap) do
  29. if level_ap > ap then break end
  30. level = level + 1
  31. end
  32. return math.max(level, 1)
  33. end
  34. --- Materia level (1-based) for an amount of AP, see {@see Characters.get_linked_slots}.
  35. Characters.get_materia_level = materia_level
  36. --- Mode an Elemental materia gives to armor, by materia level.
  37. local function elemental_armor_mode(level)
  38. if level >= 3 then return Characters.ELEMENT_MODE.ABSORB end
  39. if level == 2 then return Characters.ELEMENT_MODE.NULLIFY end
  40. return Characters.ELEMENT_MODE.HALF
  41. end
  42. --- Adds an element resistance, keeping the strongest one.
  43. local function add_resistance(defense, element, mode)
  44. if mode == nil or mode > Characters.ELEMENT_MODE.HALF then return end
  45. if defense[element] == nil or mode < defense[element] then
  46. defense[element] = mode
  47. end
  48. end
  49. --- Gets the materia linked to a slot.
  50. --
  51. -- Slots are linked in pairs: 1-2, 3-4, 5-6 and 7-8. A pair is linked if the first slot has a 1 in the
  52. -- slots of the item (the menus draw the link the same way).
  53. --
  54. -- @param item The weapon or armor definition.
  55. -- @param slot Slot of the item, 1-8.
  56. -- @return List of slots (1-8) linked to it.
  57. local function linked_slots(item, slot)
  58. local linked = {}
  59. local first = (slot % 2 == 1) and slot or slot - 1
  60. if item.slots[first] == 1 and #(item.slots) > first then
  61. table.insert(linked, slot == first and first + 1 or first)
  62. end
  63. return linked
  64. end
  65. --- Gets the slots linked to a slot of a weapon or armor.
  66. --
  67. -- @param item The weapon or armor definition.
  68. -- @param slot Slot of the item, 1-8.
  69. -- @return List of slots (1-8).
  70. Characters.get_linked_slots = linked_slots
  71. --- Applies the link materia (Elemental, Added Effect) of a weapon or armor.
  72. local function apply_link_materia(char_id, part, result)
  73. local equipment = Characters[char_id][part]
  74. local item = Game.Items[equipment.id]
  75. if item == nil or item.slots == nil then return end
  76. for slot = 1, #(item.slots) do
  77. local equipped = equipment.materia[slot - 1]
  78. local definition = equipped ~= nil and Game.Materia[equipped.id] or nil
  79. if definition ~= nil and definition.link ~= nil then
  80. local link = definition.link.id
  81. for _, partner_slot in ipairs(linked_slots(item, slot)) do
  82. local partner = equipment.materia[partner_slot - 1]
  83. local partner_def = partner ~= nil and Game.Materia[partner.id] or nil
  84. if partner_def ~= nil then
  85. if link == Materia.LINK.ELEMENTAL then
  86. for _, element in ipairs(partner_def.elements or {}) do
  87. if element ~= NO_ELEMENT then
  88. if part == "weapon" then
  89. result.elements.attack[element] = true
  90. else
  91. add_resistance(
  92. result.elements.defense, element,
  93. elemental_armor_mode(materia_level(definition, equipped.ap))
  94. )
  95. end
  96. end
  97. end
  98. elseif link == Materia.LINK.ADDED_EFFECT then
  99. local target = (part == "weapon") and result.status.attack or result.status.defense
  100. for _, status in ipairs(partner_def.status or {}) do
  101. target[status] = true
  102. end
  103. end
  104. end
  105. end
  106. end
  107. end
  108. end
  109. --- Calculates the elemental and status effects of a character.
  110. --
  111. -- Takes into account the weapon, the armor, the accessory and the linked materia:
  112. -- * Weapon elements, and Elemental materia linked in the weapon: elements the attacks have.
  113. -- * Armor and accessory elements: resistances. Elemental materia linked in the armor gives half
  114. -- damage (level 1), nullifies (level 2) or absorbs (level 3 or higher).
  115. -- * Added Effect materia: weapon statuses are inflicted, armor statuses are protected against.
  116. -- * Accessory statuses: protected against.
  117. --
  118. -- @param char_id ID of the character.
  119. -- @return A table:
  120. -- elements = {attack = {[element] = true}, defense = {[element] = mode}}
  121. -- status = {attack = {[status] = true}, defense = {[status] = true}}
  122. Characters.get_effects = function(char_id)
  123. local result = {
  124. elements = {attack = {}, defense = {}},
  125. status = {attack = {}, defense = {}},
  126. }
  127. local character = Characters[char_id]
  128. if character == nil then return result end
  129. -- Weapon
  130. local weapon = Game.Items[character.weapon.id]
  131. if weapon ~= nil then
  132. for _, element in ipairs(weapon.elements or {}) do
  133. result.elements.attack[element] = true
  134. end
  135. end
  136. -- Armor
  137. local armor = Game.Items[character.armor.id]
  138. if armor ~= nil then
  139. for _, element in ipairs(armor.elements or {}) do
  140. add_resistance(result.elements.defense, element.id, element.mode)
  141. end
  142. end
  143. -- Accessory
  144. local accessory = character.accessory ~= nil and Game.Items[character.accessory] or nil
  145. if accessory ~= nil then
  146. for _, element in ipairs(accessory.elements or {}) do
  147. if type(element) == "table" then
  148. add_resistance(result.elements.defense, element.id, element.mode)
  149. else
  150. -- Older data without a mode.
  151. add_resistance(result.elements.defense, element, Characters.ELEMENT_MODE.NULLIFY)
  152. end
  153. end
  154. for _, status in ipairs(accessory.status or {}) do
  155. result.status.defense[status] = true
  156. end
  157. end
  158. -- Materia
  159. apply_link_materia(char_id, "weapon", result)
  160. apply_link_materia(char_id, "armor", result)
  161. return result
  162. end