effect_mesh.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. --- Meshes of the battle effects that change shape, like the ground of Ifrit or the mountain of Ramuh.
  2. --
  3. -- The vertices of the original game are in ff7.exe: a mesh has the vertices of its first shape and
  4. -- a list of key shapes. The vertices move toward a key shape in a straight line over some ticks,
  5. -- one step per tick, and the triangles don't change. There are two ways to say when each key is
  6. -- reached:
  7. -- * every `key_ticks` ticks the next key starts (Ifrit, function 0x594022), or
  8. -- * a schedule, a list of {key, ticks, hold}: move to the key (a number of the list of keys, from
  9. -- 1) in `ticks` ticks and wait `hold` ticks (Ramuh, function 0x598f74, with the durations of
  10. -- the table at 0x8ba970).
  11. --
  12. -- The data is written by the installer to game/battle/effects/effect_meshes.lua (Game.EffectMeshes) and drawn
  13. -- with the dynamic meshes of the battle manager: one for the triangles with the texture and one for
  14. -- the ones with colours at the vertices and no texture.
  15. EffectMesh = {}
  16. EffectMesh.__index = EffectMesh
  17. --- Gets the data of a mesh.
  18. --
  19. -- @param name Name of the mesh, like "ifrit_ground".
  20. -- @return The data, nil if it isn't installed.
  21. function EffectMesh.get_data(name)
  22. return Game ~= nil and Game.EffectMeshes ~= nil and Game.EffectMeshes[name] or nil
  23. end
  24. --- Divides like the original code: whole numbers, rounding toward zero.
  25. local function divide(a, b)
  26. if a >= 0 then return math.floor(a / b) end
  27. return -math.floor(-a / b)
  28. end
  29. --- Gets the moves of a mesh from the durations of the executable (function 0x598f74 of Ramuh).
  30. --
  31. -- The function goes through the words T, starting at 0 with nothing to wait: when the wait is over it
  32. -- takes T[i] as the new wait and, if i is even and the wait is not 0, aims at the shape i / 2 for that
  33. -- time, and then i goes up. The shape moves only while i is odd (the odd words are the waits between
  34. -- moves) and the effect ends when i gets to 8.
  35. --
  36. -- @param tail The words (16 are kept by the installer).
  37. -- @return List of {start, ticks, key} (key from 1).
  38. function EffectMesh.schedule_from_tail(tail)
  39. local moves = {}
  40. local i, remaining, time = 0, 0, 0
  41. while true do
  42. while remaining == 0 do
  43. if i >= 8 then return moves end
  44. remaining = tail[i + 1]
  45. if remaining ~= 0 and i % 2 == 0 then
  46. moves[#moves + 1] = {start = time, ticks = remaining, key = math.floor(i / 2) + 1}
  47. end
  48. i = i + 1
  49. end
  50. time = time + 1
  51. remaining = remaining - 1
  52. end
  53. end
  54. --- Creates a mesh.
  55. --
  56. -- @param data Data of the mesh (see EffectMesh.get_data).
  57. -- @param params Table with: texture (name of the image), key_ticks (ticks between key shapes, if there
  58. -- is no schedule), schedule (list of {key, ticks, hold}), blend (0 opaque, 1 additive, 2 alpha) and uv (position and size of the image in
  59. -- the texture page of the original game, {u, v, w, h}, 0, 0, 128, 128 by default).
  60. -- @return The mesh, nil if it couldn't be created.
  61. function EffectMesh.new(data, params)
  62. local name = battle_manager:create_dynamic_mesh(params.texture or "", params.blend or 0)
  63. if name == nil or name == "" then return nil end
  64. local self = setmetatable({}, EffectMesh)
  65. self.data = data
  66. self.name = name
  67. self.key_ticks = params.key_ticks or 4
  68. local uv = params.uv or {}
  69. self.uv_u, self.uv_v, self.uv_w, self.uv_h = uv.u or 0, uv.v or 0, uv.w or 128, uv.h or 128
  70. -- Triangles with colours at the vertices have a mesh of their own, with no texture.
  71. if data.gouraud ~= nil and #data.gouraud > 0 then
  72. self.gouraud_name = battle_manager:create_dynamic_mesh("", 0)
  73. end
  74. -- The schedule is a list of moves with the tick they start.
  75. if params.schedule == "tail" then
  76. -- The durations are the 16 words that follow the table of key shapes in the executable.
  77. self.moves = EffectMesh.schedule_from_tail(data.tail)
  78. elseif params.schedule ~= nil then
  79. self.moves = {}
  80. local start = 0
  81. for _, step in ipairs(params.schedule) do
  82. self.moves[#self.moves + 1] = {start = start, ticks = step.ticks, key = step.key}
  83. start = start + step.ticks + (step.hold or 0)
  84. end
  85. end
  86. self.current = {}
  87. self.delta = {}
  88. for i = 1, #data.vertices do
  89. self.current[i] = data.vertices[i]
  90. self.delta[i] = 0
  91. end
  92. self.ticks = 0
  93. return self
  94. end
  95. --- Sets the step of every vertex to reach a key shape in some ticks.
  96. function EffectMesh:aim(key, ticks)
  97. local target = self.data.keys[key]
  98. for i = 1, #self.current do
  99. self.delta[i] = divide(target[i] - self.current[i], ticks)
  100. end
  101. end
  102. --- Moves the vertices until the mesh has done a number of ticks.
  103. --
  104. -- @param ticks Ticks since the mesh started.
  105. function EffectMesh:advance(ticks)
  106. while self.ticks < ticks do
  107. local t = self.ticks
  108. local moving = false
  109. if self.moves ~= nil then
  110. for _, move in ipairs(self.moves) do
  111. if t == move.start then self:aim(move.key, move.ticks) end
  112. if t >= move.start and t < move.start + move.ticks then moving = true end
  113. end
  114. elseif t < #self.data.keys * self.key_ticks then
  115. if t % self.key_ticks == 0 then self:aim(math.floor(t / self.key_ticks) + 1, self.key_ticks) end
  116. moving = true
  117. end
  118. if moving then
  119. for i = 1, #self.current do self.current[i] = self.current[i] + self.delta[i] end
  120. end
  121. self.ticks = t + 1
  122. end
  123. end
  124. --- Draws the mesh.
  125. --
  126. -- @param to_scene Function that gets the position of a point of the mesh in the effect and
  127. -- returns it in the scene (x, y, z).
  128. -- @param scale Size of the mesh (1 is the original one): a number, or a table with the factor of x, y
  129. -- and z (negative to flip an axis).
  130. -- @param brightness Factor of the grey level of the textured triangles (1 by default).
  131. function EffectMesh:draw(to_scene, scale, brightness)
  132. local current = self.current
  133. local sx, sy, sz = scale, scale, scale
  134. if type(scale) == "table" then sx, sy, sz = scale[1], scale[2], scale[3] end
  135. brightness = brightness or 1
  136. local function position(vertex)
  137. local v = vertex * 3
  138. return to_scene(current[v + 1] * sx, current[v + 2] * sy, current[v + 3] * sz)
  139. end
  140. -- Triangles with texture. Their grey level is 128 for the original colour.
  141. local triangles = self.data.triangles
  142. local out, n = {}, 0
  143. for t = 0, math.floor(#triangles / 10) - 1 do
  144. local b = t * 10
  145. local grey = triangles[b + 10] / 128 * brightness
  146. for k = 0, 2 do
  147. local x, y, z = position(triangles[b + 1 + k])
  148. out[n + 1], out[n + 2], out[n + 3] = x, y, z
  149. out[n + 4] = (triangles[b + 4 + 2 * k] - self.uv_u) / self.uv_w
  150. out[n + 5] = (triangles[b + 5 + 2 * k] - self.uv_v) / self.uv_h
  151. out[n + 6], out[n + 7], out[n + 8], out[n + 9] = grey, grey, grey, 1
  152. n = n + 9
  153. end
  154. end
  155. battle_manager:set_dynamic_mesh(self.name, out)
  156. -- Triangles with colours.
  157. if self.gouraud_name ~= nil then
  158. local g = self.data.gouraud
  159. out, n = {}, 0
  160. for t = 0, math.floor(#g / 12) - 1 do
  161. local b = t * 12
  162. for k = 0, 2 do
  163. local x, y, z = position(g[b + 1 + k])
  164. out[n + 1], out[n + 2], out[n + 3] = x, y, z
  165. out[n + 4], out[n + 5] = 0, 0
  166. out[n + 6], out[n + 7], out[n + 8], out[n + 9] = g[b + 4 + 3 * k] / 255, g[b + 5 + 3 * k] / 255, g[b + 6 + 3 * k] / 255, 1
  167. n = n + 9
  168. end
  169. end
  170. battle_manager:set_dynamic_mesh(self.gouraud_name, out)
  171. end
  172. end
  173. --- Speed of the shards in the first tick for each unit of the cross product of two edges: 0x60 / 4096
  174. -- (the code of the executable multiplies by 0x60 and shifts 12 bits, function 0x591455).
  175. EffectMesh.SHATTER_SPEED = 96 / 4096
  176. --- Draws the shards of a mesh that breaks (the ice of Shiva, function 0x591e14 of the executable).
  177. --
  178. -- Only the triangles of the first list (data.shards of them) are kept; the quads disappear when the
  179. -- ice breaks. Every triangle goes along its normal (the cross product of two of its edges, divided
  180. -- by 4096 and multiplied by 96 and with the sign changed: that is its speed in the first tick) and it loses an eighth of
  181. -- the speed every tick, so after n ticks it has moved speed * (1 - 0.875 ^ n) / 0.125.
  182. --
  183. -- @param to_scene Function (x, y, z) -> scene, as in draw, for the points after the turn and the flip.
  184. -- @param scale Size of the mesh.
  185. -- @param brightness Factor of the grey level.
  186. -- @param rotation Degrees around X, Y and Z, nil for none (the turn of the whole mesh).
  187. -- @param ticks Ticks since it started to break, nil if it is whole.
  188. function EffectMesh:draw_shatter(to_scene, scale, brightness, rotation, ticks)
  189. local data = self.data
  190. local verts = data.vertices
  191. local triangles = data.triangles
  192. local count = math.floor(#triangles / 10)
  193. local limit = (ticks ~= nil and data.shards ~= nil) and data.shards or count
  194. local factor = 0
  195. if ticks ~= nil then factor = (1 - 0.875 ^ ticks) / 0.125 end
  196. local out, n = {}, 0
  197. for t = 0, limit - 1 do
  198. local b = t * 10
  199. local ids = {triangles[b + 1], triangles[b + 2], triangles[b + 3]}
  200. local dx, dy, dz = 0, 0, 0
  201. if ticks ~= nil then
  202. local function vertex(i) return verts[i * 3 + 1], verts[i * 3 + 2], verts[i * 3 + 3] end
  203. local x0, y0, z0 = vertex(ids[1])
  204. local x1, y1, z1 = vertex(ids[2])
  205. local x2, y2, z2 = vertex(ids[3])
  206. local e1x, e1y, e1z, e2x, e2y, e2z = x1 - x0, y1 - y0, z1 - z0, x2 - x1, y2 - y1, z2 - z1
  207. local cx, cy, cz = e1y * e2z - e1z * e2y, e1z * e2x - e1x * e2z, e1x * e2y - e1y * e2x
  208. local speed = EffectMesh.SHATTER_SPEED
  209. dx, dy, dz = -cx * speed * factor, -cy * speed * factor, -cz * speed * factor
  210. end
  211. local grey = triangles[b + 10] / 128 * brightness
  212. for k = 0, 2 do
  213. local v = ids[k + 1] * 3
  214. local x, y, z = verts[v + 1] + dx, verts[v + 2] + dy, verts[v + 3] + dz
  215. if rotation ~= nil then x, y, z = EffectSprite.rotate(x, y, z, rotation[1], rotation[2], rotation[3]) end
  216. -- The effect is seen from the other side than the data (a turn of 180 degrees around the
  217. -- vertical axis).
  218. local px, py, pz = to_scene(-x * scale, y * scale, -z * scale)
  219. out[n + 1], out[n + 2], out[n + 3] = px, py, pz
  220. out[n + 4] = (triangles[b + 4 + 2 * k] - self.uv_u) / self.uv_w
  221. out[n + 5] = (triangles[b + 5 + 2 * k] - self.uv_v) / self.uv_h
  222. out[n + 6], out[n + 7], out[n + 8], out[n + 9] = grey, grey, grey, 1
  223. n = n + 9
  224. end
  225. end
  226. battle_manager:set_dynamic_mesh(self.name, out)
  227. end
  228. --- Removes the mesh.
  229. function EffectMesh:remove()
  230. if self.name ~= nil then battle_manager:remove_dynamic_mesh(self.name) end
  231. if self.gouraud_name ~= nil then battle_manager:remove_dynamic_mesh(self.gouraud_name) end
  232. self.name, self.gouraud_name = nil, nil
  233. end