battle_fx.lua 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  1. if UiContainer == nil then UiContainer = {} end
  2. --- Battle visual effects.
  3. --
  4. -- 3D effects and sprites made from the assets of the original game, floating
  5. -- numbers. Everything is made with the sprites and text areas of the BattleFx screen, moved from
  6. -- here every frame. Positions of the scene are projected to the UI with camera_manager.
  7. UiContainer.BattleFx = {
  8. --- Size of the pools of the screen. Must match battle_fx.xml.
  9. SPRITES = 40,
  10. NUMBERS = 8,
  11. --- Height, in scene units, above the position of a combatant where effects are shown.
  12. BODY_HEIGHT = 1.6,
  13. --- Colours of the floating numbers.
  14. COLOUR_DAMAGE = {1, 1, 1},
  15. COLOUR_HEAL = {0.4, 1, 0.5},
  16. COLOUR_MP = {0.5, 0.7, 1},
  17. COLOUR_MISS = {0.7, 0.7, 0.7},
  18. --- Active numbers.
  19. numbers = {},
  20. --- Active screen flash.
  21. flash_state = nil,
  22. --- Game time of the last update.
  23. last_time = 0,
  24. --- Sprite sheets of the original effects: the image (in images/), its size in pixels, the
  25. -- frame list of the sprite file (Game.EffectSprites, from game/battle/effects/effect_sprites.lua) and
  26. -- the frames to play, in order.
  27. SHEETS = {
  28. -- Small star flare that grows and fades. 4 x 2 frames of 32 pixels.
  29. sparkle = {image = "battle_fx/common_iyashi01.png", width = 128, height = 128,
  30. sprites = "common_iyashi2", frames = {1, 2, 4, 5, 6, 7, 8, 9}},
  31. -- Big star flare. Frames of 64 pixels.
  32. flare = {image = "battle_fx/common_iyashi00.png", width = 256, height = 128,
  33. sprites = "common_iyashi", frames = {1, 2, 3, 4, 5, 6}},
  34. },
  35. --- Active animated sprites, by pool index.
  36. sprites = {},
  37. --- Creates an animated sprite, that plays the frames of a sheet over its life.
  38. --
  39. -- @param p Table with sheet (name in SHEETS), x, y (UI), size (UI units), life, delay, vx, vy
  40. -- (UI units per second) and tint (r, g, b).
  41. spawn_sprite = function(self, p)
  42. local sheet = self.SHEETS[p.sheet]
  43. local list = Game.EffectSprites and Game.EffectSprites[sheet.sprites]
  44. if list == nil then do return end end
  45. local slot = nil
  46. for i = 1, self.SPRITES do
  47. if self.sprites[i] == nil then slot = i break end
  48. end
  49. if slot == nil then do return end end
  50. -- Sprites follow the combatant they were made for, because the camera can move (see
  51. -- get_position).
  52. if p.anchor == nil and self.spawn_anchor ~= nil then
  53. p.anchor = self.spawn_anchor
  54. p.ax, p.ay = self:project(p.anchor)
  55. end
  56. p.slot = slot
  57. p.sheet_data = sheet
  58. p.list = list
  59. p.age = 0
  60. p.delay = p.delay or 0
  61. self.sprites[slot] = p
  62. local widget = ui_manager:get_widget("BattleFx.S" .. tostring(slot))
  63. widget:set_visible(false)
  64. end,
  65. --- Advances the animated sprites.
  66. update_sprites = function(self, dt)
  67. for slot, p in pairs(self.sprites) do
  68. p.age = p.age + dt
  69. local widget = ui_manager:get_widget("BattleFx.S" .. tostring(slot))
  70. local u = (p.age - p.delay) / p.life
  71. if u >= 1 then
  72. widget:set_visible(false)
  73. self.sprites[slot] = nil
  74. elseif u >= 0 then
  75. local sheet = p.sheet_data
  76. if not p.started then
  77. p.started = true
  78. widget:set_image(sheet.image)
  79. widget:set_additive(true)
  80. local tint = p.tint or {1, 1, 1}
  81. widget:set_colour(tint[1], tint[2], tint[3])
  82. widget:set_width(0, p.size)
  83. widget:set_height(0, p.size)
  84. widget:set_visible(true)
  85. end
  86. p.x = p.x + (p.vx or 0) * dt
  87. p.y = p.y + (p.vy or 0) * dt
  88. local frame = p.list[sheet.frames[math.min(#sheet.frames, math.floor(u * #sheet.frames) + 1)]]
  89. widget:set_image_region(
  90. frame.u / sheet.width, frame.v / sheet.height,
  91. (frame.u + frame.w) / sheet.width, (frame.v + frame.h) / sheet.height
  92. )
  93. local shift_x, shift_y = 0, 0
  94. if p.anchor ~= nil and p.ax ~= nil then
  95. local nx, ny = self:project(p.anchor)
  96. if nx ~= nil then shift_x, shift_y = nx - p.ax, ny - p.ay end
  97. end
  98. widget:set_x(0, p.x - p.size / 2 + shift_x)
  99. widget:set_y(0, p.y - p.size / 2 + shift_y)
  100. widget:set_alpha(1)
  101. end
  102. end
  103. end,
  104. --- Removes the animated sprites.
  105. clear_sprites = function(self)
  106. for slot, _ in pairs(self.sprites) do ui_manager:get_widget("BattleFx.S" .. tostring(slot)):set_visible(false) end
  107. self.sprites = {}
  108. end,
  109. --- Healing: star flares that pop up around the target and rise.
  110. heal_sparkles = function(self, x, y, power)
  111. -- A big flare on the target.
  112. for i = 1, 2 do
  113. self:spawn_sprite({sheet = "flare", x = x, y = y - 15, size = 55 + 10 * power, life = 0.7, delay = 0.25 * (i - 1), vy = -25})
  114. end
  115. -- Small sparkles around, at different times.
  116. for i = 1, 12 + 4 * power do
  117. self:spawn_sprite({
  118. sheet = "sparkle", x = x + (math.random() - 0.5) * 90, y = y + 20 - math.random() * 70,
  119. size = 10 + 8 * math.random(), life = 0.5 + 0.3 * math.random(), delay = math.random() * 0.9,
  120. vy = -30 - 30 * math.random(),
  121. })
  122. end
  123. end,
  124. --- Size of the effect models relative to the original ones. Must match
  125. -- BattleManager::ATTACK_SCALE.
  126. MODEL_UNIT = 0.0032,
  127. --- Active 3D pieces of the effects, made from the meshes extracted from magic.lgp.
  128. pieces = {},
  129. --- False if the meshes of the effects aren't installed, so they aren't asked for again.
  130. models_available = true,
  131. --- Creates a 3D piece of an effect.
  132. --
  133. -- Meshes are static parts of the original effects. Each effect animates them from the update
  134. -- function: scale, position and opacity. The mesh stands up with rx = 90, as the original
  135. -- ones are lying along the Z axis of the model.
  136. --
  137. -- @param p Table with mesh (name from game/battle/effects/effects.lua), x, y, z (scene position), life
  138. -- (seconds), delay (seconds before it appears), blend (Entity.BLEND_*, additive by default),
  139. -- rx, ry, rz (rotation of the mesh), and update, a function(piece, u, entity) called every
  140. -- frame with u from 0 to 1 over the life of the piece.
  141. spawn_piece = function(self, p)
  142. if not self.models_available then do return end end
  143. p.age = 0
  144. p.delay = p.delay or 0
  145. p.life = p.life or 1
  146. self.pieces[#self.pieces + 1] = p
  147. end,
  148. --- Creates the entity of a piece, when its delay ends.
  149. start_piece = function(self, p)
  150. if not self.models_available then return false end
  151. local name = battle_manager:add_effect_model(p.mesh, p.x, p.y, p.z, 1)
  152. if name == nil or name == "" then
  153. -- Not installed. Don't try again with any piece.
  154. self.models_available = false
  155. return false
  156. end
  157. p.entity_name = name
  158. p.entity = entity_manager:get_entity(name)
  159. -- Some meshes have no texture in the files: the original code binds one when it draws them.
  160. if p.texture ~= nil then p.entity:set_texture(p.texture) end
  161. p.entity:set_blend(p.blend or Entity.BLEND_ADDITIVE)
  162. p.entity:set_model_rotation(p.rx or 0, p.ry or 0, p.rz or 0)
  163. p.entity:set_alpha(0)
  164. return true
  165. end,
  166. --- Advances the 3D pieces and removes the finished ones.
  167. update_pieces = function(self, dt)
  168. local alive = {}
  169. for _, p in ipairs(self.pieces) do
  170. p.age = p.age + dt
  171. local keep = true
  172. if p.age >= p.delay then
  173. if p.entity == nil then keep = self:start_piece(p) end
  174. if keep then
  175. local u = math.min(1, (p.age - p.delay) / p.life)
  176. local ok = pcall(p.update, p, u, p.entity)
  177. if not ok or u >= 1 then keep = false end
  178. end
  179. end
  180. if keep then alive[#alive + 1] = p
  181. elseif p.entity_name ~= nil then battle_manager:remove_effect_model(p.entity_name)
  182. end
  183. end
  184. self.pieces = alive
  185. end,
  186. --- Removes the 3D pieces.
  187. clear_pieces = function(self)
  188. for _, p in ipairs(self.pieces) do
  189. if p.entity_name ~= nil then battle_manager:remove_effect_model(p.entity_name) end
  190. end
  191. self.pieces = {}
  192. end,
  193. --- Fades in quickly, stays and fades out, for the opacity of a piece. u goes from 0 to 1.
  194. fade = function(u, fade_in, fade_out)
  195. if u < fade_in then return u / fade_in end
  196. if u > 1 - fade_out then return (1 - u) / fade_out end
  197. return 1
  198. end,
  199. --- Effects with meshes by the element of the attack. Each one gets the position of the target
  200. -- in the scene and the strength of the spell (1 for the basic ones).
  201. MODEL_EFFECTS = {
  202. -- Fire: columns of flames that grow around the target.
  203. [0] = function(self, x, y, z, power)
  204. local count = 5 + math.floor(power * 2)
  205. for i = 1, count do
  206. local angle = (i / count) * 2 * math.pi + math.random() * 0.5
  207. local radius = (i == 1) and 0 or (0.5 + 0.5 * math.random()) * power
  208. local width = 11 + 5 * power
  209. local height = 1 + 0.5 * power
  210. self:spawn_piece({
  211. mesh = "magic_faira_" .. tostring((i - 1) % 8 + 1),
  212. x = x + math.cos(angle) * radius, y = y + math.sin(angle) * radius, z = z,
  213. rx = 90, life = 0.9 + 0.3 * math.random(), delay = 0.05 * i,
  214. update = function(piece, u, entity)
  215. local grow = math.min(1, u / 0.35)
  216. local unit = self.MODEL_UNIT
  217. entity:set_scale(
  218. width * unit * (0.5 + 0.5 * grow), width * unit * (0.4 + 1.2 * grow) * height,
  219. width * unit * (0.5 + 0.5 * grow)
  220. )
  221. entity:set_alpha(self.fade(u, 0.2, 0.5))
  222. end,
  223. })
  224. end
  225. end,
  226. -- Ice: shards that rise from the ground.
  227. [1] = function(self, x, y, z, power)
  228. local count = 4 + math.floor(power * 2)
  229. for i = 1, count do
  230. local angle = (i / count) * 2 * math.pi
  231. local radius = (i == 1) and 0 or (0.3 + 0.5 * math.random()) * power
  232. local size = 12 + 6 * power * math.random()
  233. self:spawn_piece({
  234. mesh = "magic_freeze_ice_1",
  235. x = x + math.cos(angle) * radius, y = y + math.sin(angle) * radius, z = z + 0.2,
  236. rx = 0, life = 1.1, delay = 0.06 * i, blend = Entity.BLEND_ADDITIVE,
  237. update = function(piece, u, entity)
  238. local grow = math.min(1, u / 0.25)
  239. local unit = self.MODEL_UNIT
  240. entity:set_scale(size * unit * grow, size * unit * grow * 1.6, size * unit * grow)
  241. entity:set_alpha(self.fade(u, 0.15, 0.45))
  242. end,
  243. })
  244. end
  245. end,
  246. -- Bolt: a funnel of light that falls on the target.
  247. [2] = function(self, x, y, z, power)
  248. local width = 14 + 6 * power
  249. for i = 1, 3 do
  250. self:spawn_piece({
  251. mesh = "magic_thunder_thund_3",
  252. x = x, y = y, z = z + 2.6,
  253. rx = 90, life = 0.35, delay = 0.12 * (i - 1),
  254. update = function(piece, u, entity)
  255. local unit = self.MODEL_UNIT
  256. local drop = 1 - math.min(1, u / 0.5)
  257. entity:set_position(x, y, z + 0.2 + 2.4 * drop)
  258. entity:set_scale(width * unit * (0.6 + 0.8 * u), width * unit * 4, width * unit * (0.6 + 0.8 * u))
  259. entity:set_alpha(self.fade(u, 0.1, 0.5))
  260. end,
  261. })
  262. end
  263. end,
  264. },
  265. --- Spawns a sphere made of two copies of the half-sphere mesh of the barriers, that grows
  266. -- around the target, holds and fades out.
  267. --
  268. -- @param mesh Mesh name (radius 10 in the original units).
  269. -- @param radius Final radius in scene units.
  270. barrier_sphere = function(self, mesh, x, y, z, radius, life)
  271. for _, rx in ipairs({90, -90}) do
  272. self:spawn_piece({
  273. mesh = mesh, x = x, y = y, z = z + 0.9, rx = rx, life = life,
  274. update = function(piece, u, entity)
  275. local s = radius / 10 * (0.7 + 0.3 * math.min(1, u / 0.3))
  276. entity:set_scale(s, s, s)
  277. entity:set_alpha(self.fade(u, 0.15, 0.5) * 0.8)
  278. end,
  279. })
  280. end
  281. end,
  282. --- Spawns a flat ring on the ground that expands and fades out.
  283. --
  284. -- @param mesh Mesh name.
  285. -- @param mesh_radius Radius of the mesh in the original units.
  286. -- @param radius Final radius in scene units.
  287. ground_ring = function(self, mesh, mesh_radius, x, y, z, radius, life, delay)
  288. self:spawn_piece({
  289. mesh = mesh, x = x, y = y, z = z + 0.05, rx = 90, life = life, delay = delay or 0,
  290. update = function(piece, u, entity)
  291. local s = radius / mesh_radius * (0.3 + 0.7 * math.min(1, u / 0.5))
  292. entity:set_scale(s, s, s)
  293. entity:set_alpha(self.fade(u, 0.15, 0.5))
  294. end,
  295. })
  296. end,
  297. --- Effects of specific attacks, by attack id (the position in Game.Attacks). They are used
  298. -- before the ones by element. Each one gets the position of the target and a flag that is
  299. -- true when it's the first target of the attack. The ones that have sprites, instead of
  300. -- meshes, get the position in the UI too.
  301. ATTACK_EFFECTS = {
  302. -- Regen: green sparkles that keep rising around the target.
  303. [3] = function(self, x, y, z, power, ux, uy)
  304. for i = 1, 16 do
  305. self:spawn_sprite({
  306. sheet = "sparkle", x = ux + (math.random() - 0.5) * 70, y = uy + 25 - math.random() * 30,
  307. size = 10 + 8 * math.random(), life = 0.7 + 0.4 * math.random(), delay = math.random() * 1.2,
  308. vy = -35 - 25 * math.random(), tint = {0.4, 1, 0.5},
  309. })
  310. end
  311. end,
  312. -- Barrier: a sphere of light around the target.
  313. [15] = function(self, x, y, z) self:barrier_sphere("common_bari_m", x, y, z, 1.1, 1.3) end,
  314. -- MBarrier: the same with its own mesh.
  315. [16] = function(self, x, y, z) self:barrier_sphere("magic_mbaria_jo_mba", x, y, z, 1.1, 1.3) end,
  316. -- Reflect and Wall: both barriers at the same time.
  317. [17] = function(self, x, y, z)
  318. self:barrier_sphere("common_bari_m", x, y, z, 1.2, 1.5)
  319. self:ground_ring("magic_stop_stop_1", 9, x, y, z, 1.3, 1.2)
  320. end,
  321. [18] = function(self, x, y, z)
  322. self:barrier_sphere("common_bari_m", x, y, z, 1.1, 1.5)
  323. self:barrier_sphere("magic_mbaria_jo_mba", x, y, z, 1.3, 1.5)
  324. end,
  325. -- Slow: a pair of needles that fall on the target.
  326. [20] = function(self, x, y, z)
  327. for i = 1, 3 do
  328. self:spawn_piece({
  329. mesh = "magic_slou_hari", x = x, y = y, z = z + 1.4, life = 0.7, delay = 0.12 * (i - 1),
  330. update = function(piece, u, entity)
  331. local drop = 1 - math.min(1, u / 0.5)
  332. entity:set_position(x, y, z + 0.3 + 1.1 * drop)
  333. entity:set_scale(0.06, 0.06, 0.06)
  334. entity:set_alpha(self.fade(u, 0.1, 0.4))
  335. end,
  336. })
  337. end
  338. end,
  339. -- Stop: rings that close on the target.
  340. [21] = function(self, x, y, z)
  341. self:ground_ring("magic_stop_stop_1", 9, x, y, z, 1.4, 1.1)
  342. self:ground_ring("magic_stop_stop_2", 9, x, y, z, 1.0, 1.1, 0.2)
  343. end,
  344. -- DeBarrier: a big circle of light on the ground.
  345. [22] = function(self, x, y, z)
  346. self:ground_ring("magic_debaria_light_1", 39, x, y, z, 1.6, 1.2)
  347. end,
  348. -- Death: the same kind of circle.
  349. [24] = function(self, x, y, z)
  350. self:ground_ring("magic_dethper_light_1", 39, x, y, z, 1.6, 1.2)
  351. end,
  352. -- Shiva (Diamond Dust): ice spikes of the summon over the shards of the Ice spell.
  353. [57] = function(self, x, y, z, power, ux, uy)
  354. self.MODEL_EFFECTS[1](self, x, y, z, 2)
  355. local meshes = {"summon_siva_ice_1", "summon_siva_ice_2", "summon_siva_ice_3"}
  356. local count = 6
  357. for i = 1, count do
  358. local angle = (i / count) * 2 * math.pi + math.random() * 0.4
  359. local radius = (i == 1) and 0 or 0.35 + 0.35 * math.random()
  360. local size = 0.011 + 0.004 * math.random()
  361. self:spawn_piece({
  362. mesh = meshes[(i - 1) % 3 + 1],
  363. x = x + math.cos(angle) * radius, y = y + math.sin(angle) * radius, z = z,
  364. rx = 90, life = 1.4, delay = 0.08 * i,
  365. update = function(piece, u, entity)
  366. local grow = math.min(1, u / 0.3)
  367. entity:set_scale(size * (0.5 + 0.5 * grow), size * (0.2 + 0.8 * grow), size * (0.5 + 0.5 * grow))
  368. entity:set_alpha(self.fade(u, 0.15, 0.4))
  369. end,
  370. })
  371. end
  372. end,
  373. -- Ifrit (Hellfire): the flames of Fire with the explosion rings of the summon.
  374. [58] = function(self, x, y, z, power, ux, uy)
  375. self.MODEL_EFFECTS[0](self, x, y, z, 3)
  376. self:ground_ring("summon_ifu_ring_1", 7, x, y, z, 1.8, 1.3)
  377. self:ground_ring("summon_ifu_ring_1", 7, x, y, z, 1.2, 1.3, 0.25)
  378. end,
  379. -- Ramuh (Judgement Bolt): bolts that fall on the target.
  380. [59] = function(self, x, y, z, power, ux, uy)
  381. self.MODEL_EFFECTS[2](self, x, y, z, 3)
  382. end,
  383. },
  384. --- Summons, by attack id. The times are in ticks of the original effect (tps per second) and
  385. -- come from ff7.exe (see the notes on each one). The creature is the mesh (in
  386. -- game/battle/models/attacks) and its animation, which has one frame per tick, made by the
  387. -- installer at 30 frames per second.
  388. --
  389. -- appear, leave: ticks when the creature shows up and goes away.
  390. -- hit: tick when the damage lands, total: tick when the whole effect ends.
  391. -- animations: list of {tick, name}, the animation of the creature that starts at each tick.
  392. -- base: distance (original units) from the targets to the point the effect works from, camera:
  393. -- names of the two camera scripts (Game.EffectCameras, by address in ff7.exe). The game picks
  394. -- one at random: rand() & 0x100 chooses the first (found in the code of the effects).
  395. -- events: list of {tick, effect} (id of the ATTACK_EFFECTS to play on every target) or
  396. -- {tick, sound} (name of a sound). height: curve of the height of the creature over the
  397. -- ground, a list of {tick, height} in original units, with straight lines between the points
  398. -- (before the first and after the last one the height is the one of the point).
  399. -- scales: curve of the size of the creature, a list of {tick, scale} (1 is the size of the
  400. -- model in the files), used instead of scale when there is one.
  401. -- scale: size of the creature, place: where it stands between the caster (0) and the targets (1).
  402. SUMMONS = {
  403. -- Shiva. Main function 0x58f6fe of ff7.exe (1998 build): the creature (0x5924a8) is
  404. -- there from tick 34 to 198, the hit slots of the targets are created at tick 235 and the
  405. -- effect ends at tick 255. The ice (tick 200) is a guess of which callbacks are the ice.
  406. [57] = {
  407. mesh = "attacks/cyvadat", scale = 1.2, place = 0.55,
  408. animations = {{tick = 34, name = "cyvadat_00"}},
  409. appear = 34, leave = 198, hit = 235, total = 255,
  410. -- She comes down 2000 units (original units) in the 75 ticks after she appears, to 1000
  411. -- units over the ground (the y of the code goes from -3000 to -1000).
  412. height = {{34, 3000}, {109, 1000}},
  413. -- She stands 5000 units from the targets, toward the caster, and the camera of the
  414. -- effect (Game.EffectCameras, by address in ff7.exe) works from that point.
  415. base = 5000, camera = {"0x8ae268", "0x8ae340"},
  416. -- Sounds: the code plays three at tick 170 (612, 613, 614) and two at 200 (615, 616), but
  417. -- there is only one voice for sounds, so only the first of each group is played.
  418. -- The pieces of the callbacks: the spotlight (0x590781, tick 5), the snowflakes (every tick from
  419. -- tick 5), the glints (5 per tick from 105 to 164) and the ice (0x5912bf, tick 200). The storm
  420. -- models (0x5909c4, 0x590afe, tick 120), the particles and sparks (0x590cfa, tick 170) and the
  421. -- smoke (0x591fd3, tick 200) are not done.
  422. -- The ice (0x5912bf and 0x591745): four shapes in the data of the executable (the models of
  423. -- ICE_1 to ISE_4 are the same ones at a smaller size), that grow in 5 ticks to 1.125, stay
  424. -- until the tick 25, and then they break into their triangles, that go away along their
  425. -- normals while the whole shape turns, for 30 ticks. They are in the places of the table at
  426. -- 0x8ae508, turned 180 degrees around the first one, as seen in the original.
  427. meshes = (function()
  428. local spots = {{0, 3000}, {2500, 2500}, {-3000, 2000}, {4800, 1200}}
  429. local blocks = {"0x8aa590", "0x8ac6f8", "0x8ad278", "0x8ada58"}
  430. local list = {}
  431. for i = 1, 4 do
  432. local rates = {}
  433. for k = 1, 3 do rates[k] = (math.random(0, 255) - 128) * 360 / 4096 end
  434. local place = {-spots[i][1], 0, 8000 - spots[i][2]}
  435. list[i] = {
  436. name = blocks[i], texture = "battle_fx/common_water1.png", uv = {u = 0, v = 0, w = 256, h = 256},
  437. blend = 1, start = 200, length = 55,
  438. shatter = function(elapsed)
  439. local age = math.floor(elapsed)
  440. local size = age < 5 and age / 5 * 1.125 or 1.125
  441. if age < 25 then return size, nil, place, 1, nil end
  442. local ticks = age - 25 + 1
  443. return size, {rates[1] * ticks, rates[2] * ticks, rates[3] * ticks}, place, 1 - (age - 25) / 30, ticks
  444. end,
  445. }
  446. end
  447. return list
  448. end)(),
  449. emitters = {
  450. {kind = "siva_spot", start = 5, ticks = 1},
  451. {kind = "siva_snow", start = 5, ticks = 230},
  452. {kind = "siva_glints", start = 105, ticks = 60},
  453. {kind = "siva_storm", start = 120, ticks = 40},
  454. {kind = "siva_crystals", start = 170, ticks = 30},
  455. {kind = "siva_smoke", start = 200, ticks = 1},
  456. },
  457. events = {
  458. {tick = 170, sound = "612"},
  459. {tick = 200, sound = "615"},
  460. },
  461. },
  462. -- Ifrit. Main function 0x593a05: the creature is shown at tick 14 and released at 182 (the
  463. -- two animations, of 100 and 45 frames, follow each other: 14 + 100 = 114), the hit slots
  464. -- are created at 197 and it ends at 212. The fire (tick 162, where a callback and sounds
  465. -- start) is a guess. Sounds: three at 94 (428-430), three at 162 (431-433). The callback
  466. -- 0x594cf2 (registered at tick 14) moves him from 1200 to 3200 units over the ground in 30
  467. -- ticks, and grows his size from 0x1a00 to 0x1c00 (1.625 to 1.75, in 1/4096) in the next 50.
  468. -- The hole he comes out of is drawn by 0x594022 (not done).
  469. [58] = {
  470. mesh = "attacks/ifreetda", scale = 1.625, place = 0.55,
  471. animations = {{tick = 14, name = "ifreetda_00"}, {tick = 114, name = "ifreetda_01"}},
  472. height = {{14, 1200}, {44, 3200}},
  473. scales = {{14, 1.625}, {44, 1.625}, {94, 1.75}},
  474. -- The animations of Ifrit raise his root bone 1103 units (in every frame).
  475. root_y = 1103,
  476. -- The ground (0x594022, registered at tick 4): a mesh of the data of the executable that
  477. -- grows in 10 ticks, changes through nine shapes (every 4 ticks) and goes away at 40.
  478. meshes = {
  479. {name = "0x8af9b0", texture = "battle_fx/common_graund.png", uv = {u = 128, v = 128, w = 128, h = 128}, start = 4, length = 40, key_ticks = 4, ramp = 10, lift = 160},
  480. -- The flame halo around Ifrit (0x596853, tick 144, 18 ticks): the mesh of the executable at
  481. -- 0x8b4588 (a big flared cone with the texture of the flames) squashed to 0.25 x 0.25 x 0.0625
  482. -- and tilted 22.5 degrees, turned 180, over his head at (0, -2500, 1500) that moves
  483. -- (0, -10, -20) every tick, and goes away 1000 and 2000 higher in the last 3. It flickers.
  484. {name = "0x8b4588", texture = "battle_fx/summon_ifu_flame_1.png", uv = {u = 0, v = 0, w = 256, h = 128}, blend = 1,
  485. start = 144, length = 18,
  486. transform = function(n)
  487. local ty, tz = -2500 - 10 * math.min(n, 15), 1500 - 20 * math.min(n, 15)
  488. if n > 15 then ty, tz = ty + 1000 * (n - 15), tz + 2000 * (n - 15) end
  489. local flicker = (math.floor(n) % 2 == 0) and 0.5 or 0
  490. local bright = n < 15 and (1 + (flicker - 1) * n / 15) or 0
  491. return {0.25, 0.25, 0.0625}, {22.5, 180, 0}, {0, ty, tz}, bright
  492. end},
  493. -- The jet of fire (0x595002, tick 162, 20 ticks): the same mesh, flipped so the wide end
  494. -- trails (x -0.1875, y 0.1875, z from -0.1875 to -1.4), 1000 units over the ground, that
  495. -- goes 1500 units forward every tick from -5000 (it crosses the targets).
  496. {name = "0x8b4588", texture = "battle_fx/summon_ifu_flame_1.png", uv = {u = 0, v = 0, w = 256, h = 128}, blend = 1,
  497. start = 162, length = 20,
  498. transform = function(n)
  499. return {-0.1875, 0.1875, -(0.1875 + n * 0x1500 / 20 / 4096)}, nil, {0, -1000, n * 1500 - 5000}, 1
  500. end},
  501. },
  502. appear = 14, leave = 182, hit = 197, total = 212,
  503. base = 5000, camera = {"0x8b60c0", "0x8b61a8"},
  504. -- The sounds are the first of each group. The fire on the targets is a placeholder at the time
  505. -- the jet crosses them (162 + 6.6), until the callbacks of the hit (0x595975) are done.
  506. emitters = {
  507. -- The column of flames at the hole (0x594b77, tick 14, 15 ticks).
  508. {kind = "flame_column", start = 14, ticks = 1},
  509. {kind = "ifu_dome", start = 44, ticks = 5},
  510. {kind = "ifu_smoke", start = 115, ticks = 29},
  511. {kind = "ifu_flames", start = 182, ticks = 6},
  512. },
  513. events = {
  514. {tick = 94, sound = "428"},
  515. {tick = 169, effect = 58},
  516. {tick = 162, sound = "431"},
  517. -- The hit (0x595975, tick 197): the original draws 42 quads of the screen in a pink
  518. -- (0x2e8080ff), 5 ticks in and 5 out. This is a pink flash of 10 ticks: an approximation.
  519. {tick = 197, flash = {colour = {1, 0.5, 0.5}, strength = 0.35, life = 10 / 15}},
  520. },
  521. },
  522. -- Ramuh. Main function 0x598071: the creature is shown at tick 104 and released at 209
  523. -- (animations of 60 and 20 frames: 104 + 60 = 164, and 164 + 20 = 184, where a segment
  524. -- starts), the hit slots are created at 239 and it ends at 259. The bolts (tick 184) are a
  525. -- guess.
  526. [59] = {
  527. mesh = "attacks/lamdat", scale = 1.0, place = 0.55,
  528. animations = {{tick = 104, name = "lamdat_00"}, {tick = 164, name = "lamdat_01"}},
  529. appear = 104, leave = 209, hit = 239, total = 259,
  530. base = 5000, camera = {"0x8ba728", "0x8ba7f0"},
  531. -- The mountain (0x598f74, registered at tick 34): a mesh of the data of the executable that
  532. -- rises to the shapes 1, 3 and 4 (15 ticks each, with holds of 5, 5 and 119 ticks), from the
  533. -- durations that follow its table of shapes in the executable (schedule = "tail").
  534. -- The camera looks at his height: from 3000 to 5800 units in 80 ticks from tick 104.
  535. meshes = {{name = "0x8b7f88", texture = "battle_fx/common_rock.png", uv = {u = 0, v = 0, w = 256, h = 128}, start = 34, length = 174, lift = 20,
  536. schedule = "tail"}},
  537. height = {{104, 3000}, {184, 5800}},
  538. scales = {{104, 1.0}},
  539. -- Rocks that fly from random vertices of the mountain while it grows: 3 per tick for 55
  540. -- ticks (0x599bad, from the function of the mountain). They live 12 ticks. The mesh is
  541. -- STONE_1, tiny in the files (14 units): size is a factor to see it (tested by eye).
  542. debris = {{mesh = "summon_lamu_stone_1", from = 1, start = 34, ticks = 55, per_tick = 3, life = 12, size = 14}},
  543. -- Sprites (see EMITTERS): the first puff of smoke at tick 4 (0x598907, 15 ticks), more puffs from
  544. -- tick 24 to 181 (0x598d99), the ring of 8 lightnings at the top of the mountain at
  545. -- tick 194 (0x59a662, 43 ticks each) and the sparks around the targets, 2 per tick for 45
  546. -- ticks from tick 194 (0x59ac0d), and the yellow flash at tick 184 (0x59a408). The other flash
  547. -- of that tick (0x599feb, a sprite of 5 ticks) isn't done.
  548. emitters = {
  549. {kind = "smoke_first", start = 4, ticks = 1},
  550. {kind = "smoke_puffs", start = 24, ticks = 157},
  551. {kind = "flash_disc", start = 184, ticks = 1},
  552. {kind = "bolt_ring", start = 194, ticks = 1},
  553. {kind = "sparks", start = 194, ticks = 45},
  554. },
  555. -- The screen shakes (0x598763): the amplitude (pixels of a 320 x 240 screen) from each tick.
  556. shake = {{34, 50}, {44, 30}, {89, 10}, {104, 0}},
  557. -- His origin is at his base and the camera looks at the middle of his body: his mesh
  558. -- is about 850 units tall (-390 to 458), so he goes down half of that (tested by eye:
  559. -- he was about half his height too high).
  560. root_y = 424,
  561. -- (The placeholder bolts, ATTACK_EFFECTS[59], aren't used anymore: the sparks of the
  562. -- emitters are the original ones.)
  563. },
  564. },
  565. --- Ticks per second of the original effects. It's the one of the battle, 15 (from forum posts
  566. -- about the PC version, and it makes the creature animation, 165 frames, last as long as the
  567. -- creature is shown in the original code).
  568. SUMMON_TPS = 15,
  569. -- The bone of Shiva's hand where the storm gathers (a guess: change it if it's in the wrong place).
  570. SIVA_HAND_BONE = 12,
  571. --- Frames per second the animations of the files are made at by the installer.
  572. ANIMATION_FPS = 30,
  573. --- Summons being played.
  574. timelines = {},
  575. --- True if the attack is a summon with a sequence.
  576. has_summon = function(self, id)
  577. return id ~= nil and self.SUMMONS[id] ~= nil and self.models_available
  578. end,
  579. --- Plays the sequence of a summon: the creature appears, plays its animation, the effects
  580. -- happen on the targets and the hit lands, at the ticks of its definition.
  581. --
  582. -- @param attack Attack data.
  583. -- @param side Side of the caster.
  584. -- @param actor Index of the caster.
  585. -- @param target_group Side of the targets.
  586. -- @param targets List of indices of the targets.
  587. -- @param on_hit Function called when the hit lands.
  588. -- @param on_done Function called when the sequence ends.
  589. play_summon = function(self, attack, side, actor, target_group, targets, on_hit, on_done)
  590. local def = self.SUMMONS[attack.id]
  591. local timeline = {
  592. age = 0, def = def, on_hit = on_hit, on_done = on_done, hit_done = false,
  593. target_group = target_group, targets = targets, events_done = {},
  594. }
  595. -- Stands between the caster and the center of the targets.
  596. local caster = UiContainer.BattleUi:get_entity(side, actor)
  597. local cx, cy, cz = 0, 0, 0
  598. if caster ~= nil then cx, cy, cz = caster:get_position() end
  599. local tx, ty, count = 0, 0, 0
  600. for _, t in ipairs(targets) do
  601. local entity = UiContainer.BattleUi:get_entity(target_group, t)
  602. if entity ~= nil then
  603. local x, y = entity:get_position()
  604. tx, ty, count = tx + x, ty + y, count + 1
  605. end
  606. end
  607. if count > 0 then tx, ty = tx / count, ty / count else tx, ty = cx, cy end
  608. timeline.x = cx + (tx - cx) * def.place
  609. timeline.y = cy + (ty - cy) * def.place
  610. timeline.z = cz
  611. -- The space of the effect: forward goes from the caster to the targets, and the origin is
  612. -- `base` units from the targets, toward the caster.
  613. local fx, fy = tx - cx, ty - cy
  614. local l = math.sqrt(fx * fx + fy * fy)
  615. if l > 0.0001 then fx, fy = fx / l, fy / l else fx, fy = -1, 0 end
  616. timeline.f = {x = fx, y = fy}
  617. -- To the right of someone looking toward the targets (with the Z axis up).
  618. timeline.lateral = {x = fy, y = -fx}
  619. local base = (def.base or 0) * self.MODEL_UNIT
  620. timeline.origin = {x = tx - fx * base, y = ty - fy * base, z = cz}
  621. if def.base ~= nil then timeline.x, timeline.y = timeline.origin.x, timeline.origin.y end
  622. -- The camera of the effect.
  623. if def.camera ~= nil and EffectCamera ~= nil then
  624. local words = EffectCamera.get_script(def.camera[math.random(#def.camera)])
  625. if words ~= nil then
  626. timeline.camera = EffectCamera.new(words)
  627. timeline.camera_tick = 0
  628. end
  629. end
  630. -- Faces the targets, as the party members do.
  631. timeline.rotation = side == Battle.TargetGroup.PARTY and 0 or 180
  632. self.timelines[#self.timelines + 1] = timeline
  633. end,
  634. --- Creates the entity of the creature of a summon.
  635. start_creature = function(self, t)
  636. local name = battle_manager:add_effect_model(t.def.mesh, t.x, t.y, t.z, t.def.scale)
  637. if name == nil or name == "" then return end
  638. t.entity_name = name
  639. t.entity = entity_manager:get_entity(name)
  640. t.entity:set_rotation(t.rotation)
  641. t.entity:set_blend(Entity.BLEND_ALPHA)
  642. t.entity:set_alpha(0)
  643. -- The animation has one frame per tick of the effect.
  644. t.entity:set_animation_speed(self.SUMMON_TPS / self.ANIMATION_FPS)
  645. t.animation_index = 0
  646. end,
  647. --- Draws the meshes of the data of the executable that a summon has (def.meshes), from the
  648. -- tick they start to the end of their length.
  649. update_meshes = function(self, t, tick)
  650. if EffectMesh == nil then return end
  651. t.meshes = t.meshes or {}
  652. for i, def in ipairs(t.def.meshes or {}) do
  653. local elapsed = tick - def.start
  654. local mesh = t.meshes[i]
  655. if elapsed >= 0 and elapsed < def.length then
  656. if mesh == nil and not t.mesh_failed then
  657. local data = EffectMesh.get_data(def.name)
  658. if data ~= nil then mesh = EffectMesh.new(data, def) end
  659. if mesh == nil then t.mesh_failed = true else t.meshes[i] = mesh end
  660. end
  661. if mesh ~= nil then
  662. mesh:advance(math.floor(elapsed))
  663. local scale = def.ramp ~= nil and math.min(1, elapsed / def.ramp) or 1
  664. -- The lift moves the mesh up (original units): its lowest points are below the ground
  665. -- of the effect, and the floor of the battle would hide them (the original game
  666. -- doesn't test depth).
  667. local lift = def.lift or 0
  668. -- A transform gives the size of every axis, the rotation (degrees around X, Y and Z),
  669. -- the position (effect units) and the brightness for the time of the mesh.
  670. local sizes, rotation, offset, brightness = scale, nil, nil, 1
  671. if def.transform ~= nil then sizes, rotation, offset, brightness = def.transform(elapsed) end
  672. if def.shatter ~= nil then
  673. -- (the ice of Shiva, that breaks into its triangles)
  674. local ticks
  675. sizes, rotation, offset, brightness, ticks = def.shatter(elapsed)
  676. mesh:draw_shatter(function(x, y, z)
  677. return self:effect_to_scene(t, {x + offset[1], y + offset[2], z + offset[3]})
  678. end, sizes, brightness, rotation, ticks)
  679. else
  680. mesh:draw(function(x, y, z)
  681. if rotation ~= nil then x, y, z = EffectSprite.rotate(x, y, z, rotation[1], rotation[2], rotation[3]) end
  682. if offset ~= nil then x, y, z = x + offset[1], y + offset[2], z + offset[3] end
  683. return self:effect_to_scene(t, {x, y - lift, z})
  684. end, sizes, brightness)
  685. end
  686. end
  687. elseif mesh ~= nil then
  688. mesh:remove()
  689. t.meshes[i] = nil
  690. end
  691. end
  692. end,
  693. --- Removes the meshes of the executable of a summon.
  694. remove_meshes = function(self, t)
  695. for i, mesh in pairs(t.meshes or {}) do mesh:remove() end
  696. t.meshes = {}
  697. end,
  698. --- Value of a curve made of points with straight lines between them.
  699. --
  700. -- @param points List of {tick, value}, in order of tick.
  701. -- @param tick Where to get the value. Outside of the points, the value of the closest one.
  702. curve = function(points, tick)
  703. if tick <= points[1][1] then return points[1][2] end
  704. for i = 2, #points do
  705. if tick <= points[i][1] then
  706. local a, b = points[i - 1], points[i]
  707. return a[2] + (b[2] - a[2]) * (tick - a[1]) / (b[1] - a[1])
  708. end
  709. end
  710. return points[#points][2]
  711. end,
  712. --- Starts the animations of the creature whose tick has come.
  713. update_animations_of_creature = function(self, t, tick)
  714. local animations = t.def.animations or {}
  715. while t.animation_index < #animations and tick >= animations[t.animation_index + 1].tick do
  716. t.animation_index = t.animation_index + 1
  717. t.entity:play_animation_stop(animations[t.animation_index].name)
  718. end
  719. end,
  720. --- Plays the effect of an attack on every target of a summon.
  721. play_effect_on_targets = function(self, t, id)
  722. local effect = self.ATTACK_EFFECTS[id]
  723. if effect == nil then return end
  724. for _, index in ipairs(t.targets) do
  725. local entity = UiContainer.BattleUi:get_entity(t.target_group, index)
  726. if entity ~= nil then
  727. local x, y, z = entity:get_position()
  728. local ux, uy = self:get_position(t.target_group, index, self.BODY_HEIGHT)
  729. effect(self, x, y, z, 1, ux, uy)
  730. end
  731. end
  732. end,
  733. --- Calls a function of an effect and prints the first error it has (once per summon and name),
  734. -- so a problem isn't silent.
  735. safe = function(self, t, name, fn, ...)
  736. local ok, err = pcall(fn, ...)
  737. if not ok then
  738. t.reported = t.reported or {}
  739. if not t.reported[name] then
  740. t.reported[name] = true
  741. print("[BattleFx] error in " .. name .. ": " .. tostring(err))
  742. end
  743. end
  744. end,
  745. --- Factor of the size of the sparks around the targets, chosen by eye (3 was still small).
  746. SPARK_SIZE = 6,
  747. --- Sheets of sprites: the image, its size in the texture page of the original game and the sprite
  748. -- file (Game.EffectAnimations).
  749. SPRITE_SHEETS = {
  750. smoke = {texture = "battle_fx/common_lamu01.png", width = 256, height = 256, anim = "summon_lamu_smoke_1"},
  751. spark = {texture = "battle_fx/common_lamu00.png", width = 256, height = 256, anim = "summon_lamu_jo_c04"},
  752. lightning = {texture = "battle_fx/common_lamu00.png", width = 256, height = 256, anim = "common_houden_2"},
  753. -- Ifrit: the smoke (IFLEET03.TIM) and the flames (IFLEET00.TIM for FLAME_A to E and IFLEET01.TIM
  754. -- for FLAME_F to I; every file is a frame of the flame, see the emitter ifu_flames).
  755. ifu_smoke = {texture = "battle_fx/common_ifleet03.png", width = 256, height = 256, anim = "summon_ifu_smoke_1"},
  756. ifu_flame_00 = {texture = "battle_fx/common_ifleet00.png", width = 256, height = 256, anim = "common_flame_a"},
  757. ifu_flame_01 = {texture = "battle_fx/common_ifleet01.png", width = 256, height = 256, anim = "common_flame_f"},
  758. siva_smoke = {texture = "battle_fx/common_siva03.png", width = 256, height = 256, anim = "summon_siva_smoke_1"},
  759. siva_kira = {texture = "battle_fx/common_siva02.png", width = 256, height = 256, anim = "summon_siva_kira_1"},
  760. -- Shiva: the snowflake (SIVA02.TIM).
  761. siva_snow = {texture = "battle_fx/common_siva02.png", width = 256, height = 256, anim = "summon_siva_snow_1"},
  762. },
  763. --- A point of a mesh of the executable of a summon, in the space of the effect.
  764. mesh_point = function(self, t, mesh_index, vertex)
  765. local mesh = t.meshes ~= nil and t.meshes[mesh_index] or nil
  766. if mesh == nil then return {0, 0, 0} end
  767. return {mesh.current[vertex * 3 + 1], mesh.current[vertex * 3 + 2], mesh.current[vertex * 3 + 3]}
  768. end,
  769. --- Adds a sprite that plays the frames of a sprite file, one per tick.
  770. --
  771. -- @param p Table with sheet (name in SPRITE_SHEETS), frames (list of frames, from 0, one per
  772. -- tick), birth (tick), pos (a position of the effect, or a function that gets it), scale (a
  773. -- number, or a function of the age in ticks), angles (degrees, around X, Y and Z) and rate (degrees
  774. -- of change of the angles every tick), and brightness (function of the age, from 0 to 1).
  775. add_sprite = function(self, t, p)
  776. t.sprites = t.sprites or {}
  777. t.sprites[#t.sprites + 1] = p
  778. end,
  779. --- Draws the sprites of a summon.
  780. update_sprites3d = function(self, t, tick)
  781. if t.sprites == nil then return end
  782. t.batches = t.batches or {}
  783. local alive = {}
  784. local k = self.MODEL_UNIT
  785. for _, p in ipairs(t.sprites) do
  786. local age = tick - p.birth
  787. -- A sprite has one file and the frame to show in each tick (frames), or a file and a frame
  788. -- for each tick (sequence, for the effects that change file every tick).
  789. local count = p.sequence ~= nil and #p.sequence or #p.frames
  790. if age >= count then
  791. -- It's over.
  792. else
  793. alive[#alive + 1] = p
  794. local step = math.floor(age) + 1
  795. local sheet_name, frame_number, anim = p.sheet, nil, nil
  796. if p.sequence ~= nil then
  797. local entry = p.sequence[step]
  798. sheet_name, anim, frame_number = entry.sheet, entry.anim, entry.frame or 0
  799. else
  800. frame_number = p.frames[step]
  801. end
  802. local sheet = self.SPRITE_SHEETS[sheet_name]
  803. local frames = EffectSprite.get_frames(anim or sheet.anim)
  804. local frame = frames ~= nil and frames[frame_number + 1] or nil
  805. if frame ~= nil and age >= 0 then
  806. local batch = t.batches[sheet_name]
  807. if batch == nil then
  808. batch = EffectSpriteBatch.new(sheet.texture, sheet.width, sheet.height, sheet.blend or 1)
  809. t.batches[sheet_name] = batch
  810. end
  811. if batch ~= nil then
  812. local pos = type(p.pos) == "function" and p.pos(age) or p.pos
  813. local scale = type(p.scale) == "function" and p.scale(age) or p.scale
  814. local scale_y = p.scale_y or scale
  815. local a = p.angles or {0, 0, 0}
  816. local r = p.rate or {0, 0, 0}
  817. local ax, ay, az = a[1] + r[1] * age, a[2] + r[2] * age, a[3] + r[3] * age
  818. local bright = p.brightness ~= nil and p.brightness(age) or 1
  819. local colour = {bright, bright, bright, sheet.alpha or 1}
  820. local corner
  821. if p.billboard and t.view ~= nil then
  822. -- Faces the camera: the corners go along the right and up of the screen
  823. -- (the y of a sprite goes down).
  824. local cx, cy, cz = self:effect_to_scene(t, pos)
  825. local right, up = t.view.right, t.view.up
  826. corner = function(x, y)
  827. local dx, dy = x * 8 * scale * k, y * 8 * scale_y * k
  828. return {cx + right[1] * dx - up[1] * dy, cy + right[2] * dx - up[2] * dy, cz + right[3] * dx - up[3] * dy}
  829. end
  830. else
  831. corner = function(x, y)
  832. local rx, ry, rz = EffectSprite.rotate(x * 8 * scale, y * 8 * scale_y, 0, ax, ay, az)
  833. local sx, sy, sz = self:effect_to_scene(t, {pos[1] + rx, pos[2] + ry, pos[3] + rz})
  834. return {sx, sy, sz}
  835. end
  836. end
  837. for _, q in ipairs(frame) do
  838. local x, y, u, v, w, h = q[1], q[2], q[3], q[4], q[5], q[6]
  839. batch:add_quad(corner(x, y), corner(x + w, y), corner(x, y + h), corner(x + w, y + h), u, v, u + w, v + h, colour)
  840. end
  841. end
  842. end
  843. end
  844. end
  845. t.sprites = alive
  846. for _, batch in pairs(t.batches) do batch:flush() end
  847. end,
  848. --- Draws the flat flashes of a summon: discs of sixteen sides that face the camera, with a
  849. -- radius and a colour that change every tick (see 0x59a408 and 0x59a4e0 in the executable).
  850. update_flashes = function(self, t, tick)
  851. if t.flashes == nil then return end
  852. t.batches = t.batches or {}
  853. local alive = {}
  854. local batch = t.batches.flat
  855. if batch == nil and #t.flashes > 0 then
  856. batch = EffectSpriteBatch.new("", 1, 1, 1)
  857. t.batches.flat = batch
  858. end
  859. local k = self.MODEL_UNIT
  860. for _, f in ipairs(t.flashes) do
  861. local age = tick - f.birth
  862. if age < f.life then
  863. alive[#alive + 1] = f
  864. if age >= 0 and batch ~= nil and t.view ~= nil then
  865. local radius, colour = f.shape(age)
  866. local cx, cy, cz = self:effect_to_scene(t, f.pos)
  867. local right, up = t.view.right, t.view.up
  868. local function rim(step)
  869. local a = step * math.pi * 2 / 16
  870. local dx, dy = math.cos(a) * radius * k, math.sin(a) * radius * k
  871. return {cx + right[1] * dx + up[1] * dy, cy + right[2] * dx + up[2] * dy, cz + right[3] * dx + up[3] * dy}
  872. end
  873. for step = 0, 15 do
  874. batch:add_triangle({cx, cy, cz}, rim(step), rim(step + 1), colour)
  875. end
  876. end
  877. end
  878. end
  879. t.flashes = alive
  880. if batch ~= nil then batch:flush() end
  881. end,
  882. --- Removes the sprites and flashes of a summon.
  883. remove_sprites = function(self, t)
  884. for _, batch in pairs(t.batches or {}) do batch:remove() end
  885. t.batches, t.sprites, t.flashes = {}, {}, {}
  886. end,
  887. --- Size of the smoke of Ifrit relative to the sizes of the executable (0.125 to 0.25), chosen by eye
  888. -- (the animation is drawn by 0x5d1f6b and its unit isn't known).
  889. SMOKE_SIZE = 8,
  890. --- Converts a point of the scene to the space of an effect (the opposite of effect_to_scene).
  891. scene_to_effect = function(self, t, x, y, z)
  892. local k = self.MODEL_UNIT
  893. local dx, dy = x - t.origin.x, y - t.origin.y
  894. return {(dx * t.lateral.x + dy * t.lateral.y) / k, (t.origin.z - z) / k, (dx * t.f.x + dy * t.f.y) / k}
  895. end,
  896. --- What each kind of emitter of a summon creates, every tick it works (index is 0 for the first).
  897. EMITTERS = {
  898. -- The first puff of smoke, at the base of the mountain (0x598907 with 15 ticks).
  899. smoke_first = function(self, t, e, tick, index)
  900. local frames = {}
  901. for age = 0, 14 do frames[#frames + 1] = 3 - ((15 - age) % 4) end
  902. self:add_sprite(t, {sheet = "smoke", frames = frames, birth = tick, scale = 4.0, pos = function() return self:mesh_point(t, 1, 27) end})
  903. end,
  904. -- A puff every tick, tilted at random (0x598d99): 3 ticks each.
  905. smoke_puffs = function(self, t, e, tick, index)
  906. local tilt = function() return (math.random(0, 1023) - 512) * 360 / 4096 end
  907. self:add_sprite(t, {sheet = "smoke", frames = {0, 1, 2}, birth = tick, scale = 4.0,
  908. pos = function() return self:mesh_point(t, 1, 27) end, angles = {tilt(), tilt(), 0}})
  909. end,
  910. -- Eight lightnings that spin around the top of the mountain (0x59a662 and 0x59a740).
  911. bolt_ring = function(self, t, e, tick, index)
  912. local frames = {}
  913. for age = 0, 42 do
  914. local f = age % 8
  915. frames[#frames + 1] = f < 4 and f or f - 4
  916. end
  917. for i = 0, 7 do
  918. local tilt = (math.random(0, 431) - 1536) * 360 / 4096
  919. local turn = (i * 512 + math.random(0, 255)) * 360 / 4096
  920. self:add_sprite(t, {
  921. sheet = "lightning", frames = frames, birth = tick,
  922. pos = function() return self:mesh_point(t, 1, 27) end,
  923. scale = function(age) return age < 1 and 0 or (age < 2 and 3.5 or 7.0) end,
  924. brightness = function(age) return age < 38 and 1 or math.max(0, 1 - (age - 38) / 5) end,
  925. angles = {tilt, turn, 0}, rate = {0, 0x50 * 360 / 4096, 0},
  926. })
  927. end
  928. end,
  929. -- (No rotation: the meshes of the installer already have the 180 degrees around X of the P files
  930. -- applied, so the raw y of -24..0 is 0..24 up. With rx = 180 it was underground and not seen.)
  931. -- A column of flames at the origin (0x594b77, resource 1: FLAME_2, 1200 units tall in the game,
  932. -- 50 times its raw size): it gets 2.0 to 2.45 wide and 0 to 2.7 tall in 10 ticks, then 2.5 and
  933. -- 3.0 for 5 ticks while it fades.
  934. flame_column = function(self, t, e, tick, index)
  935. local x, y, z = self:effect_to_scene(t, {0, 0, 0})
  936. local unit = self.MODEL_UNIT * 50
  937. self:spawn_piece({
  938. mesh = "common_flame_2", x = x, y = y, z = z, rx = 0, blend = Entity.BLEND_ADDITIVE,
  939. texture = "battle_fx/summon_ifu_flame_1.png", -- FLAME_1.TIM, bound by the code of the effect
  940. life = 15 / self.SUMMON_TPS,
  941. update = function(piece, u, entity)
  942. local age = u * 15
  943. local wide, tall, alpha = 2.5, 3.0, 1
  944. if age < 10 then wide, tall = 2.0 + age * 0.05, age * 0.3
  945. else alpha = 1 - (age - 10) / 5
  946. end
  947. entity:set_scale(wide * unit, tall * unit, wide * unit)
  948. entity:set_alpha(alpha)
  949. end,
  950. })
  951. end,
  952. -- Smoke from bone 5 of the creature (0x593cb3..0x593d33 of the main callback and the particles of
  953. -- 0x596d15): two puffs per tick, each with the speed of a table (-10, 0, -10) or (20, 0, -30)
  954. -- and a bit of chance (-5 to +4), that lose 1/16 of the speed every tick (and the depth speed
  955. -- goes 5 up), for 16 ticks, one frame of SMOKE_1 per tick.
  956. ifu_smoke = function(self, t, e, tick, index)
  957. if t.entity == nil then return end
  958. local bx, by, bz = t.entity:get_bone_position(5)
  959. local start = self:scene_to_effect(t, bx, by, bz)
  960. local speeds = {{-10, 0, -10}, {20, 0, -30}}
  961. for i = 1, 2 do
  962. local v = {}
  963. for c = 1, 3 do v[c] = speeds[i][c] + math.random(0, 9) - 5 end
  964. local pos, done = {start[1], start[2], start[3]}, 0
  965. local frames = {}
  966. for age = 0, 15 do frames[#frames + 1] = age end
  967. self:add_sprite(t, {
  968. sheet = "ifu_smoke", frames = frames, birth = tick, billboard = true,
  969. scale = (512 + math.random(0, 511)) / 4096 * self.SMOKE_SIZE,
  970. pos = function(age)
  971. while done < math.floor(age) do
  972. done = done + 1
  973. for c = 1, 3 do pos[c] = pos[c] + v[c] end
  974. v[1], v[2], v[3] = v[1] * 0.9375, v[2] * 0.9375, v[3] * 0.9375 + 5
  975. end
  976. return pos
  977. end,
  978. })
  979. end
  980. end,
  981. -- Spheres of light around Ifrit (0x596aee): one per tick for 5 ticks, at a random height
  982. -- between 2500 and 3500, 20 ticks each. They grow wide (x and z) 1 to 4 times with the
  983. -- sine of the age and fade out and in again. The size of the model is 200 for 26 of raw size.
  984. ifu_dome = function(self, t, e, tick, index)
  985. local base = (384 + math.random(0, 127)) / 4096
  986. local x, y, z = self:effect_to_scene(t, {0, -3500 + math.random(0, 999), 0})
  987. local unit = self.MODEL_UNIT * 200 / 26
  988. self:spawn_piece({
  989. mesh = "summon_ifu_exp_2", x = x, y = y, z = z, rx = 0, blend = Entity.BLEND_ADDITIVE,
  990. life = 20 / self.SUMMON_TPS,
  991. update = function(piece, u, entity)
  992. local age = u * 20
  993. local wide = base * (1 + 3 * math.sin(math.rad(age * 4.5)))
  994. entity:set_scale(wide * unit, base * unit, wide * unit)
  995. entity:set_alpha(age < 10 and (1 - age / 10) or ((age - 10) / 10))
  996. end,
  997. })
  998. end,
  999. -- Fire on the ground after the jet (0x595701 and 0x5957fc): three flames per tick for 6 ticks,
  1000. -- at random places of a field of 6000 x 10000 units around the targets. Each one shows the nine
  1001. -- sprite files FLAME_A to FLAME_I, one per tick, scaled 1.5 to 2.5.
  1002. ifu_flames = function(self, t, e, tick, index)
  1003. local letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i"}
  1004. for _ = 1, 3 do
  1005. local sequence = {}
  1006. for i, letter in ipairs(letters) do
  1007. sequence[i] = {sheet = i <= 5 and "ifu_flame_00" or "ifu_flame_01", anim = "common_flame_" .. letter, frame = 0}
  1008. end
  1009. self:add_sprite(t, {
  1010. sequence = sequence, birth = tick, billboard = true,
  1011. pos = {math.random(-3000, 2999), 0, math.random(0, 9999)},
  1012. scale = (0x1800 + math.random(0, 0xfff)) / 4096, scale_y = (0x1800 + math.random(0, 0xfff)) / 4096,
  1013. })
  1014. end
  1015. end,
  1016. -- Shiva's snowflakes (0x58fb8b and 0x590585): one every tick, high in the air at a random place of
  1017. -- 6000 x 6000, that stays 100 ticks (the speed is 0 in the data) as the sprite of a snowflake.
  1018. siva_snow = function(self, t, e, tick, index)
  1019. self:add_sprite(t, {
  1020. sheet = "siva_snow", frames = (function() local f = {} for i = 1, 100 do f[i] = 0 end return f end)(),
  1021. birth = tick, billboard = true, scale = 1.0,
  1022. pos = (function()
  1023. -- The flakes fall from 5000 up to the ground in their 100 ticks.
  1024. local x, z = math.random(-3000, 2999), math.random(-3000, 2999)
  1025. return function(age) return {x, -5000 + age * 50, z} end
  1026. end)(),
  1027. })
  1028. end,
  1029. -- Shiva's glints (0x5906c7): five per tick between ticks 105 and 164, tiny points of light for 20
  1030. -- ticks in a volume of 10000 x 10000 x 10000, that get brighter from 20/255 to about 180/255.
  1031. siva_glints = function(self, t, e, tick, index)
  1032. t.flashes = t.flashes or {}
  1033. for _ = 1, 5 do
  1034. t.flashes[#t.flashes + 1] = {
  1035. birth = tick, life = 20,
  1036. pos = {math.random(-5000, 4999), math.random(-7000, 2999), math.random(-5000, 4999)},
  1037. shape = function(age)
  1038. local b = ((19 - age) * 0 + age * 8 + 20) / 255
  1039. return 40, {b, b, b, 1}
  1040. end,
  1041. }
  1042. end
  1043. end,
  1044. -- Shiva's ice (0x5912bf and 0x591745): four spikes at once at the places of a table, each with a
  1045. -- model of ICE_1 to ISE_4 (this one doesn't exist in the files, so it isn't shown): they grow to
  1046. -- 1.125 times the size of the table (800) in 5 ticks, stay until tick 25 and then spin and
  1047. -- break for 30 ticks (here they fade while they spin).
  1048. siva_spikes = function(self, t, e, tick, index)
  1049. local spots = {{0, 3000}, {2500, 2500}, {-3000, 2000}, {4800, 1200}}
  1050. local models = {"summon_siva_ice_1", "summon_siva_ice_2", "summon_siva_ice_3"}
  1051. -- The table size (800) over the biggest coordinate of each model (110, 3 and 50 in the files).
  1052. local reach = {110, 3, 50}
  1053. for i = 1, 3 do
  1054. -- The first place of the table (0, 3000) is taken as the place of the targets (5000 in
  1055. -- front of the origin), and the others keep their distance to it.
  1056. -- Turned 180 degrees around the first one (as seen in the original), so the sides and the
  1057. -- depth of the others are the opposite ones of the table.
  1058. local x, y, z = self:effect_to_scene(t, {-spots[i][1], 0, 8000 - spots[i][2]})
  1059. local unit = self.MODEL_UNIT * 800 / reach[i]
  1060. self:spawn_piece({
  1061. -- No turn: the model is drawn as it is (the installer already flipped it, so rx = 0).
  1062. mesh = models[i], x = x, y = y, z = z, rx = 0, blend = Entity.BLEND_ADDITIVE,
  1063. life = 55 / self.SUMMON_TPS,
  1064. update = function(p, u, entity)
  1065. local age = u * 55
  1066. local size, alpha = 1.125, 1
  1067. if age < 5 then size = age / 5 * 1.125
  1068. elseif age >= 25 then
  1069. -- The original breaks the polygons and throws them away for 30 ticks: here
  1070. -- the model swells and fades.
  1071. local f = (age - 25) / 30
  1072. size, alpha = 1.125 * (1 + f * 0.8), 1 - f * f
  1073. end
  1074. entity:set_scale(size * unit, size * unit, size * unit)
  1075. entity:set_alpha(alpha)
  1076. end,
  1077. })
  1078. end
  1079. end,
  1080. -- The hand of Shiva that the storm gathers around (the original takes the position from a
  1081. -- bone of the model; the bone number is a guess, see SIVA_HAND_BONE).
  1082. siva_hand = function(self, t)
  1083. if t.entity == nil then return {0, -1500, 1000} end
  1084. local bx, by, bz = t.entity:get_bone_position(self.SIVA_HAND_BONE)
  1085. return self:scene_to_effect(t, bx, by, bz)
  1086. end,
  1087. -- The storm that gathers at the hand (0x5908a1, from tick 120): a disc of STOME_1 that grows for
  1088. -- 50 ticks and then 15 more while it fades (0x5909c4), and every 4 ticks (10 times) a ring of
  1089. -- STOME_2 with a random turn that shrinks from 3.0 to 0 in 10 ticks (0x590afe). Both models are
  1090. -- flat (radius 10, 3204 in the game), so they are drawn facing the targets.
  1091. siva_storm = function(self, t, e, tick, index)
  1092. local hand = self.EMITTERS.siva_hand(self, t)
  1093. local x, y, z = self:effect_to_scene(t, {hand[1], hand[2], hand[3] - 500})
  1094. local unit = self.MODEL_UNIT * 3204 / 10
  1095. if index == 0 then
  1096. self:spawn_piece({
  1097. mesh = "common_stome_1", x = x, y = y, z = z, rx = 0, blend = Entity.BLEND_ADDITIVE,
  1098. life = 65 / self.SUMMON_TPS,
  1099. update = function(p, u, entity)
  1100. local age = u * 65
  1101. local size, alpha
  1102. if age < 50 then size, alpha = (128 + age / 50 * 2944) / 4096, 1
  1103. else
  1104. local f = (age - 50) / 15
  1105. size, alpha = (3072 + f * 13312) / 4096, 1 - f
  1106. end
  1107. entity:set_scale(size * unit, size * unit, size * unit)
  1108. entity:set_alpha(alpha)
  1109. end,
  1110. })
  1111. end
  1112. if index % 4 == 0 then
  1113. local turn = {math.random(0, 359), math.random(0, 359), math.random(0, 359)}
  1114. local sx, sy, sz = self:effect_to_scene(t, hand)
  1115. self:spawn_piece({
  1116. mesh = "common_stome_2", x = sx, y = sy, z = sz, rx = turn[1], ry = turn[2], rz = turn[3],
  1117. blend = Entity.BLEND_ADDITIVE, life = 10 / self.SUMMON_TPS,
  1118. update = function(p, u, entity)
  1119. local size = 3.0 * (1 - u)
  1120. entity:set_scale(size * unit, size * unit, size * unit)
  1121. entity:set_alpha(0.3 + 0.7 * u)
  1122. end,
  1123. })
  1124. end
  1125. end,
  1126. -- The crystals that Shiva throws (0x590cfa and 0x590f34, from tick 170 for 30 ticks): 4 per tick, from
  1127. -- (0, -3000, 1000), each one in a random direction of the plane x = 0 (the velocity is a
  1128. -- vector of 1500-1999 units that is turned by random angles) that moves for 15 ticks, with
  1129. -- sparkles (KIRA_1, 0x591204) on its way. The size of the model and its direction are guesses.
  1130. siva_crystals = function(self, t, e, tick, index)
  1131. for _ = 1, 4 do
  1132. -- The speed of the original (1500-1999 per tick) takes them out of the screen in a moment, so
  1133. -- it is divided, and they go in any direction instead of only the plane x = 0, that is seen
  1134. -- as a column.
  1135. local r = (1500 + math.random(0, 499)) / 8
  1136. local angle = math.random() * math.pi * 2
  1137. local spread = math.random() * 2 - 1
  1138. local flat = math.sqrt(1 - spread * spread)
  1139. local vx, vy, vz = r * spread, r * flat * math.sin(angle), -r * flat * math.cos(angle)
  1140. local start = {0, -3000, 1000}
  1141. local function position(age) return {start[1] - vx * age, start[2] - vy * age, start[3] - vz * age} end
  1142. local x, y, z = self:effect_to_scene(t, start)
  1143. local unit = self.MODEL_UNIT * 12
  1144. self:spawn_piece({
  1145. mesh = "common_partic_1", x = x, y = y, z = z, rx = 0, blend = Entity.BLEND_ADDITIVE,
  1146. life = 15 / self.SUMMON_TPS,
  1147. update = function(p, u, entity)
  1148. local age = u * 15
  1149. local sx, sy, sz = self:effect_to_scene(t, position(age))
  1150. entity:set_position(sx, sy, sz)
  1151. entity:set_scale(unit, unit, unit)
  1152. entity:set_alpha(1 - u * u)
  1153. end,
  1154. })
  1155. for age = 0, 14 do
  1156. if math.random() < 0.625 then
  1157. local p = position(age)
  1158. self:add_sprite(t, {
  1159. sheet = "siva_kira", frames = {0, 1, 2, 3, 4, 5, 6, 7}, birth = tick + age, billboard = true,
  1160. scale = 1.5, pos = {p[1] + math.random(-100, 99), p[2] + math.random(-100, 99), p[3] + math.random(-100, 99)},
  1161. })
  1162. end
  1163. end
  1164. end
  1165. end,
  1166. -- Shiva's smoke (0x591fd3 and 0x5921e1): the 16 frames of SMOKE_1, one per tick, where the ice
  1167. -- appears (the exact places are a guess).
  1168. siva_smoke = function(self, t, e, tick, index)
  1169. local spots = {{0, 3000}, {2500, 2500}, {-3000, 2000}, {4800, 1200}}
  1170. local frames = {}
  1171. for i = 0, 15 do frames[#frames + 1] = i end
  1172. for i = 1, 3 do
  1173. self:add_sprite(t, {
  1174. sheet = "siva_smoke", frames = frames, birth = tick, billboard = true, scale = 3.0,
  1175. pos = {-spots[i][1], -300, 8000 - spots[i][2]},
  1176. })
  1177. end
  1178. end,
  1179. -- Shiva's spotlight (0x590781, SPOT_1 of 200 for 20 of raw size, scaled 3.0): a thin beam at the
  1180. -- origin that fades in for 30 ticks, stays for 50 and fades out for 20.
  1181. siva_spot = function(self, t, e, tick, index)
  1182. local x, y, z = self:effect_to_scene(t, {0, 0, 0})
  1183. local unit = self.MODEL_UNIT * 10 * 3
  1184. self:spawn_piece({
  1185. mesh = "common_spot_1", x = x, y = y, z = z, rx = 0, blend = Entity.BLEND_ADDITIVE,
  1186. life = 100 / self.SUMMON_TPS,
  1187. update = function(p, u, entity)
  1188. local age = u * 100
  1189. local alpha = age < 30 and age / 30 or (age < 80 and 1 or 1 - (age - 80) / 20)
  1190. entity:set_scale(unit, unit, unit)
  1191. entity:set_alpha(alpha)
  1192. end,
  1193. })
  1194. end,
  1195. -- The yellow flash at the base (0x59a408): a disc of radius 1600 that grows for 10 ticks (the
  1196. -- size goes 0.1 more every tick) and then stays at 2.0 while its colour fades in 5 ticks.
  1197. flash_disc = function(self, t, e, tick, index)
  1198. t.flashes = t.flashes or {}
  1199. t.flashes[#t.flashes + 1] = {
  1200. birth = tick, life = 15, pos = {0, 0, 0},
  1201. shape = function(age)
  1202. if age < 10 then return 1600 * age * 2048 / 5 / 4096, {1, 1, 0, 1} end
  1203. local v = ((age - 10) * -255 / 5 + 255) / 255
  1204. return 1600 * 2.0, {v, v, 0, 1}
  1205. end,
  1206. }
  1207. end,
  1208. -- Sparks at random places in front of the mountain: two every tick (0x59ac0d and 0x59acef).
  1209. sparks = function(self, t, e, tick, index)
  1210. for _ = 1, 2 do
  1211. self:add_sprite(t, {
  1212. sheet = "spark", frames = {0, 1, 2, 3, 4, 5}, birth = tick,
  1213. -- The executable puts them all at a depth of 4096, which is a flat plane that is only
  1214. -- seen well from the front: they face the camera and have some depth too.
  1215. billboard = true,
  1216. pos = {math.random(-1280, 1279), math.random(-960, 959), 4096 + math.random(-1500, 1500)},
  1217. -- The sizes of the executable (0.25 to 1) looked too small: six times that.
  1218. scale = (1024 + math.random(0, 3071)) / 4096 * self.SPARK_SIZE,
  1219. })
  1220. end
  1221. end,
  1222. },
  1223. --- Creates what the emitters of a summon have to create by this tick.
  1224. update_emitters = function(self, t, tick)
  1225. t.emitted = t.emitted or {}
  1226. for i, e in ipairs(t.def.emitters or {}) do
  1227. local last = t.emitted[i] or -1
  1228. local now = math.min(math.floor(tick - e.start), e.ticks - 1)
  1229. while last < now do
  1230. last = last + 1
  1231. self.EMITTERS[e.kind](self, t, e, e.start + last, last)
  1232. end
  1233. t.emitted[i] = last
  1234. end
  1235. end,
  1236. --- Throws one rock of a mountain: it starts at a random vertex of the mesh, with a speed that
  1237. -- goes outward and up, and spins (0x599bad and the code that creates it, in 0x598f74).
  1238. spawn_debris = function(self, t, d, source)
  1239. local current = source.current
  1240. local v = math.random(0, #current / 3 - 1)
  1241. local x, y, z = current[v * 3 + 1], current[v * 3 + 2], current[v * 3 + 3]
  1242. local r = math.random(0, 255)
  1243. -- The speed is in units per tick, whole numbers like in the original.
  1244. local function whole(n) return n >= 0 and math.floor(n) or -math.floor(-n) end
  1245. local vx, vy, vz = whole(x * r / 4096), -(math.random(0, 99) + 200), whole(z * r / 4096)
  1246. local spin = {math.random(0, 15) - 8, math.random(0, 15) - 8, math.random(0, 15) - 8}
  1247. local size = (1 + math.random()) * d.size * self.MODEL_UNIT
  1248. local sx, sy, sz = self:effect_to_scene(t, {x, y, z})
  1249. self:spawn_piece({
  1250. mesh = d.mesh, x = sx, y = sy, z = sz, blend = Entity.BLEND_NORMAL, life = d.life / self.SUMMON_TPS,
  1251. update = function(piece, u, entity)
  1252. local age = u * d.life
  1253. local px, py, pz = self:effect_to_scene(t, {x + vx * age, y + vy * age, z + vz * age})
  1254. entity:set_position(px, py, pz)
  1255. -- The angles go 16 units of 4096 per turn for every unit of speed, each tick.
  1256. local to_degrees = 16 * 360 / 4096 * age
  1257. entity:set_model_rotation(spin[1] * to_degrees, spin[2] * to_degrees, spin[3] * to_degrees)
  1258. entity:set_scale(size, size, size)
  1259. end,
  1260. })
  1261. end,
  1262. --- Throws the rocks of a summon (def.debris) that have to be thrown by this tick.
  1263. update_debris = function(self, t, tick)
  1264. t.debris_done = t.debris_done or {}
  1265. for i, d in ipairs(t.def.debris or {}) do
  1266. local source = t.meshes ~= nil and t.meshes[d.from] or nil
  1267. local last = t.debris_done[i] or -1
  1268. local now = math.min(math.floor(tick - d.start), d.ticks - 1)
  1269. if source ~= nil then
  1270. while last < now do
  1271. last = last + 1
  1272. for _ = 1, d.per_tick do self:spawn_debris(t, d, source) end
  1273. end
  1274. t.debris_done[i] = last
  1275. end
  1276. end
  1277. end,
  1278. --- Strength of the shakes relative to the amplitudes of the original code. It was too strong at
  1279. -- 1 (the amplitudes are for a screen of 320 x 240 and the camera moves instead of the image), so
  1280. -- it's 10% of that, chosen by eye.
  1281. SHAKE_SCALE = 0.1,
  1282. --- Gets how far the screen is moved by a shake at this tick, in pixels of a screen of 320 x 240,
  1283. -- like the original code that adds and takes the amplitude every other tick (0x5987b6).
  1284. shake_offset = function(self, t, tick)
  1285. local shake = t.def.shake
  1286. if shake == nil or tick < shake[1][1] then return 0 end
  1287. local amplitude = 0
  1288. for _, point in ipairs(shake) do
  1289. if tick >= point[1] then amplitude = point[2] end
  1290. end
  1291. if amplitude == 0 then return 0 end
  1292. return ((math.floor(tick - shake[1][1]) % 2 == 0) and -amplitude or 0) * self.SHAKE_SCALE
  1293. end,
  1294. --- Converts a point of the space of an effect (original units, y down, z toward the targets) to
  1295. -- the scene. The script of Shiva looks at (0, 0, 5000) at the end, the place of the targets
  1296. -- when the origin is 5000 units from them.
  1297. effect_to_scene = function(self, t, p)
  1298. local k = self.MODEL_UNIT
  1299. return t.origin.x + (t.f.x * p[3] + t.lateral.x * p[1]) * k,
  1300. t.origin.y + (t.f.y * p[3] + t.lateral.y * p[1]) * k,
  1301. t.origin.z - p[2] * k
  1302. end,
  1303. --- Goes back to the camera of the battle that was in use.
  1304. restore_camera = function(self)
  1305. local index = UiContainer.BattleUi ~= nil and UiContainer.BattleUi.camera_index or 0
  1306. battle_manager:set_camera(index or 0)
  1307. end,
  1308. --- Moves the camera of an effect to where its script says at this moment.
  1309. update_camera = function(self, t, tick)
  1310. local target = math.floor(tick)
  1311. while t.camera_tick < target do
  1312. t.camera:tick()
  1313. t.camera_tick = t.camera_tick + 1
  1314. end
  1315. if not t.camera.started then return end
  1316. local cam, look = t.camera:get(tick - target)
  1317. local cx, cy, cz = self:effect_to_scene(t, cam)
  1318. local lx, ly, lz = self:effect_to_scene(t, look)
  1319. local offset = self:shake_offset(t, tick)
  1320. if offset ~= 0 then
  1321. -- The image moves by a part of the screen, so the camera moves the same part of what it
  1322. -- sees at the distance of the point it looks at.
  1323. local dx, dy, dz = lx - cx, ly - cy, lz - cz
  1324. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  1325. local rx, ry = dy, -dx
  1326. local rl = math.sqrt(rx * rx + ry * ry)
  1327. if rl > 0.0001 and distance > 0.0001 then
  1328. rx, ry = rx / rl, ry / rl
  1329. -- Up is the right vector crossed with the direction.
  1330. local ux, uy, uz = ry * dz, -rx * dz, rx * dy - ry * dx
  1331. local ul = math.sqrt(ux * ux + uy * uy + uz * uz)
  1332. ux, uy, uz = ux / ul, uy / ul, uz / ul
  1333. local height = 0.828 * distance
  1334. local shift_x, shift_y = offset / 320 * height * 16 / 9, offset / 240 * height
  1335. local mx, my, mz = -rx * shift_x + ux * shift_y, -ry * shift_x + uy * shift_y, uz * shift_y
  1336. cx, cy, cz, lx, ly, lz = cx + mx, cy + my, cz + mz, lx + mx, ly + my, lz + mz
  1337. end
  1338. end
  1339. -- The directions of the screen, for the sprites that face the camera.
  1340. local vx, vy, vz = lx - cx, ly - cy, lz - cz
  1341. local rx, ry = vy, -vx
  1342. local rl = math.sqrt(rx * rx + ry * ry)
  1343. local vl = math.sqrt(vx * vx + vy * vy + vz * vz)
  1344. if rl > 0.0001 and vl > 0.0001 then
  1345. rx, ry = rx / rl, ry / rl
  1346. local ux, uy, uz = ry * vz, -rx * vz, rx * vy - ry * vx
  1347. local ul = math.sqrt(ux * ux + uy * uy + uz * uz)
  1348. t.view = {right = {rx, ry, 0}, up = {ux / ul, uy / ul, uz / ul}}
  1349. end
  1350. battle_manager:set_camera_position(cx, cy, cz, lx, ly, lz)
  1351. t.camera_used = true
  1352. end,
  1353. --- Ticks the party takes to fade out when a summon starts, and to fade in when it ends.
  1354. PARTY_FADE_OUT = 10,
  1355. PARTY_FADE_IN = 12,
  1356. --- Sets how visible the party members (and the weapons they hold) are.
  1357. --
  1358. -- Dead members stay as they are. At full opacity the normal drawing is restored.
  1359. set_party_alpha = function(self, alpha)
  1360. local party = UiContainer.BattleUi.party
  1361. for i = 1, Party.MAX do
  1362. local entity = party[i] ~= nil and UiContainer.BattleUi:get_entity(Battle.TargetGroup.PARTY, i) or nil
  1363. if entity ~= nil then
  1364. if alpha >= 1 or party[i].dead then
  1365. -- Full opacity first (it only counts while the blend is not the normal one).
  1366. entity:set_alpha(1)
  1367. entity:set_blend(Entity.BLEND_NORMAL)
  1368. else
  1369. entity:set_blend(Entity.BLEND_ALPHA)
  1370. entity:set_alpha(alpha)
  1371. end
  1372. end
  1373. end
  1374. end,
  1375. --- Advances the summons. The hit and the end are always called, even if the creature couldn't
  1376. -- be shown, so the battle doesn't wait for them.
  1377. update_timelines = function(self, dt)
  1378. local alive = {}
  1379. for _, t in ipairs(self.timelines) do
  1380. t.age = t.age + dt
  1381. local tick = t.age * self.SUMMON_TPS
  1382. local def = t.def
  1383. -- The party fades out while the summon is on and comes back when it ends.
  1384. if t.party_faded ~= false then
  1385. local fade_in_start = def.total - self.PARTY_FADE_IN
  1386. local alpha
  1387. if tick < fade_in_start then alpha = math.max(0, 1 - tick / self.PARTY_FADE_OUT)
  1388. else alpha = math.min(1, (tick - fade_in_start) / self.PARTY_FADE_IN) end
  1389. local ok, err = pcall(self.set_party_alpha, self, alpha)
  1390. if not ok then print("Party fade failed: " .. tostring(err)) end
  1391. t.party_faded = ok
  1392. if ok then t.party_touched = true end
  1393. end
  1394. -- The creature is there from appear to leave, fading in and out.
  1395. if tick >= def.appear and tick < def.leave then
  1396. if t.entity == nil and not t.creature_failed then
  1397. pcall(self.start_creature, self, t)
  1398. if t.entity == nil then t.creature_failed = true end
  1399. end
  1400. if t.entity ~= nil then
  1401. local fade = math.min(1, (tick - def.appear) / 8, (def.leave - tick) / 8)
  1402. pcall(function() t.entity:set_alpha(fade) end)
  1403. pcall(self.update_animations_of_creature, self, t, tick)
  1404. if def.height ~= nil then
  1405. -- The animations lift the model by the height of its hips (root_y, in the
  1406. -- units of the model), and the heights of the executable don't count that.
  1407. local scale_now = def.scales ~= nil and self.curve(def.scales, tick) or def.scale
  1408. local height = self.curve(def.height, tick) - (def.root_y or 0) * scale_now
  1409. pcall(function() t.entity:set_position(t.x, t.y, t.z + height * self.MODEL_UNIT) end)
  1410. end
  1411. if def.scales ~= nil then
  1412. local s = self.curve(def.scales, tick) * self.MODEL_UNIT
  1413. pcall(function() t.entity:set_scale(s, s, s) end)
  1414. end
  1415. end
  1416. elseif t.entity ~= nil then
  1417. battle_manager:remove_effect_model(t.entity_name)
  1418. t.entity, t.entity_name = nil, nil
  1419. end
  1420. if t.camera ~= nil then pcall(self.update_camera, self, t, tick) end
  1421. if def.meshes ~= nil then self:safe(t, "meshes", self.update_meshes, self, t, tick) end
  1422. if def.debris ~= nil then self:safe(t, "debris", self.update_debris, self, t, tick) end
  1423. if def.emitters ~= nil then
  1424. self:safe(t, "emitters", self.update_emitters, self, t, tick)
  1425. self:safe(t, "sprites", self.update_sprites3d, self, t, tick)
  1426. self:safe(t, "flashes", self.update_flashes, self, t, tick)
  1427. end
  1428. for i, event in ipairs(def.events or {}) do
  1429. if tick >= event.tick and not t.events_done[i] then
  1430. t.events_done[i] = true
  1431. if event.effect ~= nil then pcall(self.play_effect_on_targets, self, t, event.effect) end
  1432. if event.sound ~= nil then pcall(function() audio_manager:play_sound(event.sound) end) end
  1433. if event.flash ~= nil then pcall(self.flash, self, event.flash.colour, event.flash.strength, event.flash.life) end
  1434. end
  1435. end
  1436. if not t.hit_done and tick >= def.hit then
  1437. t.hit_done = true
  1438. t.on_hit()
  1439. end
  1440. if tick >= def.total then
  1441. if t.party_touched then pcall(self.set_party_alpha, self, 1) end
  1442. if not t.hit_done then t.hit_done = true t.on_hit() end
  1443. if t.entity_name ~= nil then battle_manager:remove_effect_model(t.entity_name) end
  1444. if t.camera_used then self:restore_camera() end
  1445. pcall(self.remove_meshes, self, t)
  1446. pcall(self.remove_sprites, self, t)
  1447. t.on_done()
  1448. else
  1449. alive[#alive + 1] = t
  1450. end
  1451. end
  1452. self.timelines = alive
  1453. end,
  1454. --- Removes the summons in progress, without finishing them.
  1455. clear_timelines = function(self)
  1456. for _, t in ipairs(self.timelines) do
  1457. if t.party_touched then pcall(self.set_party_alpha, self, 1) end
  1458. if t.entity_name ~= nil then battle_manager:remove_effect_model(t.entity_name) end
  1459. if t.camera_used then pcall(self.restore_camera, self) end
  1460. pcall(self.remove_meshes, self, t)
  1461. pcall(self.remove_sprites, self, t)
  1462. end
  1463. self.timelines = {}
  1464. end,
  1465. --- Strength of a spell for the size of its effect: 1 for the basic one, 2 and 3 for the
  1466. -- upgrades (named like Fire2 and Fire3).
  1467. get_power = function(self, attack)
  1468. local level = tonumber(string.match(attack.name or "", "(%d)%s*$"))
  1469. if level == nil then return 1 end
  1470. return 1 + (level - 1) * 0.5
  1471. end,
  1472. --- Shows the effect of a spell on a target, made from the extracted assets: the one of the
  1473. -- attack if there is one, or the one of its element for the damage spells.
  1474. --
  1475. -- @return True if there is an effect for the attack.
  1476. play_model_effect = function(self, attack, side, index, heals)
  1477. if attack == nil or attack.id == nil or attack.id < 0 then do return false end end
  1478. if self.SUMMONS[attack.id] ~= nil then return false end
  1479. local effect = self.ATTACK_EFFECTS[attack.id]
  1480. if effect == nil and not heals and (attack.damage.formula or 0) == 2
  1481. and attack.elements ~= nil and #attack.elements > 0 then
  1482. effect = self.MODEL_EFFECTS[attack.elements[1]]
  1483. end
  1484. if effect == nil or not self.models_available then return false end
  1485. local entity = UiContainer.BattleUi:get_entity(side, index)
  1486. if entity == nil then return false end
  1487. local x, y, z = entity:get_position()
  1488. local ux, uy = self:get_position(side, index, self.BODY_HEIGHT)
  1489. effect(self, x, y, z, self:get_power(attack), ux, uy)
  1490. return true
  1491. end,
  1492. --- Models of the thrown things (the shuriken of the effects of magic.lgp).
  1493. THROW_MESH = "common_shuriken01",
  1494. THROW_TIME = 0.9,
  1495. THROW_SIZE = 14,
  1496. --- A thing flies from the thrower to the targets, spinning.
  1497. --
  1498. -- @param on_hit Called when it arrives to the first target.
  1499. -- @param on_end Called when it has finished.
  1500. play_throw = function(self, side, actor, target_group, targets, mesh, delay, on_hit, on_end)
  1501. local from = UiContainer.BattleUi:get_entity(side, actor)
  1502. local list = {}
  1503. for _, index in ipairs(targets or {}) do
  1504. local to = UiContainer.BattleUi:get_entity(target_group, index)
  1505. if to ~= nil then list[#list + 1] = to end
  1506. end
  1507. if from == nil or #list == 0 or not self.models_available then
  1508. on_hit()
  1509. on_end()
  1510. return
  1511. end
  1512. local x0, y0, z0 = from:get_position()
  1513. z0 = z0 + 1.2
  1514. local hit, arrived = false, 0
  1515. -- Weapons are models of the characters, bigger than the ones of the effects.
  1516. local is_weapon = mesh ~= nil and string.find(mesh, "characters/", 1, true) ~= nil
  1517. -- One thing flies to each target.
  1518. for _, to in ipairs(list) do
  1519. local x1, y1, z1 = to:get_position()
  1520. z1 = z1 + 1.0
  1521. local landed = false
  1522. self:spawn_piece({
  1523. mesh = mesh or self.THROW_MESH, x = x0, y = y0, z = z0, blend = Entity.BLEND_NORMAL, rx = 90,
  1524. life = self.THROW_TIME, delay = delay or 0,
  1525. update = function(piece, u, entity)
  1526. -- A small arc.
  1527. local arc = math.sin(u * math.pi) * 0.6
  1528. entity:set_position(x0 + (x1 - x0) * u, y0 + (y1 - y0) * u, z0 + (z1 - z0) * u + arc)
  1529. local size = is_weapon and 0.0039 or self.THROW_SIZE * self.MODEL_UNIT
  1530. entity:set_scale(size, size, size)
  1531. -- It flies straight, without spinning.
  1532. if is_weapon then entity:set_model_rotation(0, 0, 0)
  1533. else entity:set_model_rotation(90, 90, 0) end
  1534. entity:set_alpha(1)
  1535. if u >= 1 and not landed then
  1536. landed = true
  1537. -- The hit lands when the first one arrives, it ends with the last.
  1538. if not hit then hit = true on_hit() end
  1539. arrived = arrived + 1
  1540. if arrived == #list then on_end() end
  1541. end
  1542. end,
  1543. })
  1544. end
  1545. end,
  1546. --- Shows the effects screen and clears everything.
  1547. show = function(self)
  1548. self:clear()
  1549. self.last_time = timer:get_game_time_total()
  1550. ui_manager:get_widget("BattleFx"):set_visible(true)
  1551. end,
  1552. --- Hides the effects screen.
  1553. hide = function(self)
  1554. self:clear()
  1555. ui_manager:get_widget("BattleFx"):set_visible(false)
  1556. end,
  1557. --- Auras around the caster, the light tubes that appear while a character casts.
  1558. --
  1559. -- The original code (ff7.exe, 0x5c01a0) has four kinds, drawn by three functions of 15 ticks
  1560. -- that use the models JO_P01 to JO_P03: quarter cylinders (4 of them make a tube) of radius 10
  1561. -- and height 20 (11 and a flare for JO_P01). The colours are not in the code, they were told by
  1562. -- looking at the original game: spells green, enemy skills pink, limits yellow and orange,
  1563. -- summons yellow and red with sparkles.
  1564. AURA_TPS = 15,
  1565. AURA_TICKS = 15,
  1566. --- Size of a model unit of the aura models, in scene units (a guess).
  1567. AURA_UNIT = 0.06,
  1568. AURA_SEGMENTS = 16,
  1569. --- Extra factor for the radius only (the height keeps AURA_UNIT).
  1570. AURA_RADIUS = 2,
  1571. --- Layers that start one after another, and ticks between them.
  1572. AURA_LAYERS = 4,
  1573. AURA_LAYER_GAP = 3,
  1574. AURAS = {
  1575. -- Spells: JO_P02, grows and pulses.
  1576. [0] = {texture = "battle_fx/common_jo_b02.png", tint = {1, 1, 1}, radius = 10, base = 10, height = 20},
  1577. -- Limits: JO_P01, a flare that is narrow at the bottom.
  1578. [1] = {texture = "battle_fx/common_jo_b03.png", tint = {1, 1, 1}, radius = 10, base = 4.5, height = 28.5},
  1579. -- Enemy skills: JO_P03, shrinks toward the caster.
  1580. [2] = {texture = "battle_fx/common_jo_b04.png", tint = {1, 1, 1}, radius = 10, base = 10, height = 20},
  1581. -- Summons: JO_P02 with sparkles.
  1582. [3] = {texture = "battle_fx/common_jo_b03.png", tint = {1, 0.55, 0.45}, radius = 10, base = 10, height = 20},
  1583. },
  1584. --- Sound of each kind (ids of audio/sounds, from the opcodes of ff7.exe: E6 13, E0 36, A4 496,
  1585. -- A5 489, played by 0x430ca2).
  1586. AURA_SOUNDS = {[0] = "13", [1] = "36", [2] = "496", [3] = "489"},
  1587. auras = {},
  1588. --- Gets the aura kind of an action: 0 spells, 1 limits, 2 enemy skills, 3 summons.
  1589. aura_kind = function(self, action, summon)
  1590. if summon then return 3 end
  1591. local command = Game.Commands ~= nil and Game.Commands[action.command or -1] or nil
  1592. local name = command ~= nil and string.lower(command.name or "") or ""
  1593. if string.find(name, "e.skill", 1, true) then return 2 end
  1594. if string.find(name, "limit", 1, true) then return 1 end
  1595. return 0
  1596. end,
  1597. --- Starts the aura of a combatant that is casting.
  1598. start_aura = function(self, kind, side, index)
  1599. local def = self.AURAS[kind]
  1600. if def == nil then do return end end
  1601. pcall(function() audio_manager:play_sound(self.AURA_SOUNDS[kind]) end)
  1602. for layer = 0, self.AURA_LAYERS - 1 do
  1603. local name = battle_manager:create_dynamic_mesh(def.texture or "", 1)
  1604. if name ~= nil and name ~= "" then
  1605. self.auras[#self.auras + 1] = {
  1606. kind = kind, def = def, name = name, side = side, index = index, tick = -1,
  1607. age = -layer * self.AURA_LAYER_GAP / self.AURA_TPS,
  1608. }
  1609. end
  1610. end
  1611. end,
  1612. --- Builds the tube of an aura for a tick.
  1613. draw_aura = function(self, a, tick)
  1614. local def = a.def
  1615. local entity = UiContainer.BattleUi:get_entity(a.side, a.index)
  1616. if entity == nil then do return end end
  1617. local cx, cy, cz = entity:get_position()
  1618. local c = tick
  1619. local radius_scale, height_scale
  1620. if a.kind == 2 then
  1621. -- Closes in on the caster (the original counts down from 14).
  1622. radius_scale = 0.4 + (14 - c) / 14 * 1.6
  1623. height_scale = 1 + math.sin((14 - c) * 128 / 4096 * 2 * math.pi)
  1624. else
  1625. -- Comes out of the body and expands.
  1626. radius_scale = 0.4 + c / 14 * 1.6
  1627. height_scale = 1 + math.sin(c * 128 / 4096 * 2 * math.pi)
  1628. end
  1629. -- Fades in at the start and out at the end.
  1630. local alpha = math.min(1, (c + 1) / 4) * math.min(1, (self.AURA_TICKS - c) / 5)
  1631. local unit = self.AURA_UNIT
  1632. local r_top = def.radius * radius_scale * unit * self.AURA_RADIUS
  1633. local r_base = def.base * radius_scale * unit * self.AURA_RADIUS
  1634. local h = def.height * height_scale * unit
  1635. local n = self.AURA_SEGMENTS
  1636. local out, k = {}, 0
  1637. local tint = def.tint
  1638. -- The models map the whole texture over each quarter of the tube, bright part at the bottom.
  1639. local function vertex(angle, r, z, u, v)
  1640. out[k + 1], out[k + 2], out[k + 3] = cx + r * math.cos(angle), cy + r * math.sin(angle), cz + z
  1641. out[k + 4], out[k + 5] = u, v
  1642. out[k + 6], out[k + 7], out[k + 8], out[k + 9] = tint[1] * alpha, tint[2] * alpha, tint[3] * alpha, 1
  1643. k = k + 9
  1644. end
  1645. for i = 0, n - 1 do
  1646. local a0 = i / n * 2 * math.pi + c * 0.3
  1647. local a1 = (i + 1) / n * 2 * math.pi + c * 0.3
  1648. local u0, u1 = 0.016 + 0.953 * (i % 4) / 4, 0.016 + 0.953 * (i % 4 + 1) / 4
  1649. -- Both sides of every strip, the tube is seen from inside and outside.
  1650. for _, pass in ipairs({1, 2}) do
  1651. local p0, p1, q0, q1 = a0, a1, u0, u1
  1652. if pass == 2 then p0, p1, q0, q1 = a1, a0, u1, u0 end
  1653. vertex(p0, r_base, 0, q0, 0.992)
  1654. vertex(p1, r_base, 0, q1, 0.992)
  1655. vertex(p0, r_top, h, q0, 0.008)
  1656. vertex(p1, r_base, 0, q1, 0.992)
  1657. vertex(p1, r_top, h, q1, 0.008)
  1658. vertex(p0, r_top, h, q0, 0.008)
  1659. end
  1660. end
  1661. battle_manager:set_dynamic_mesh(a.name, out)
  1662. end,
  1663. --- Advances the auras.
  1664. update_auras = function(self, dt)
  1665. local alive = {}
  1666. for _, a in ipairs(self.auras) do
  1667. a.age = a.age + dt
  1668. local tick = math.floor(a.age * self.AURA_TPS)
  1669. if tick < 0 then
  1670. alive[#alive + 1] = a
  1671. elseif tick >= self.AURA_TICKS then
  1672. battle_manager:remove_dynamic_mesh(a.name)
  1673. else
  1674. if tick ~= a.tick then
  1675. a.tick = tick
  1676. pcall(self.draw_aura, self, a, tick)
  1677. -- Summons: sparkles around the caster (SUMMON.SPT in the original).
  1678. if a.kind == 3 and a.age < 0.3 then
  1679. local x, y = self:get_position(a.side, a.index, 0.6)
  1680. if x ~= nil then
  1681. for _ = 1, 2 do
  1682. self:spawn_sprite({
  1683. sheet = "sparkle", x = x + (math.random() - 0.5) * 60, y = y + (math.random() - 0.3) * 50,
  1684. size = 14 + 10 * math.random(), life = 0.5, vy = -30,
  1685. tint = math.random() < 0.5 and {1, 0.3, 0.15} or {1, 0.9, 0.3},
  1686. })
  1687. end
  1688. end
  1689. end
  1690. end
  1691. alive[#alive + 1] = a
  1692. end
  1693. end
  1694. self.auras = alive
  1695. end,
  1696. --- Removes the auras.
  1697. clear_auras = function(self)
  1698. for _, a in ipairs(self.auras) do battle_manager:remove_dynamic_mesh(a.name) end
  1699. self.auras = {}
  1700. end,
  1701. --- Removes every effect.
  1702. clear = function(self)
  1703. self:clear_auras()
  1704. self:clear_timelines()
  1705. self:clear_pieces()
  1706. self:clear_sprites()
  1707. for _, number in pairs(self.numbers) do ui_manager:get_widget("BattleFx.N" .. tostring(number.index)):set_visible(false) end
  1708. ui_manager:get_widget("BattleFx.Flash"):set_visible(false)
  1709. self.numbers = {}
  1710. self.flash_state = nil
  1711. end,
  1712. --- Gets the position of a combatant in the UI.
  1713. --
  1714. -- @param side Battle.TargetGroup.PARTY or ENEMIES.
  1715. -- @param index Index of the combatant.
  1716. -- @param height Height above the combatant, in scene units.
  1717. -- @return X and Y in UI units, nil if the combatant has no entity.
  1718. get_position = function(self, side, index, height)
  1719. local entity = UiContainer.BattleUi:get_entity(side, index)
  1720. if entity == nil then return nil end
  1721. local x, y, z = entity:get_position()
  1722. local ux, uy = camera_manager:project_to_ui(x, y, z + height)
  1723. -- What is made right after this follows the combatant when the camera moves.
  1724. self.spawn_anchor = {side = side, index = index, height = height}
  1725. return ux, uy
  1726. end,
  1727. --- Projects the point of an anchor ({side, index, height}) again, with the camera as it is now.
  1728. project = function(self, anchor)
  1729. local entity = UiContainer.BattleUi:get_entity(anchor.side, anchor.index)
  1730. if entity == nil then return nil end
  1731. local x, y, z = entity:get_position()
  1732. local ux, uy = camera_manager:project_to_ui(x, y, z + anchor.height)
  1733. return ux, uy
  1734. end,
  1735. --- Shows a screen flash that fades out.
  1736. --
  1737. -- @param colour Table with r, g, b.
  1738. -- @param strength Starting alpha.
  1739. -- @param life Seconds it lasts.
  1740. flash = function(self, colour, strength, life)
  1741. self.flash_state = {colour = colour, strength = strength, life = life or 0.35, age = 0}
  1742. local widget = ui_manager:get_widget("BattleFx.Flash")
  1743. widget:set_colour(colour[1], colour[2], colour[3])
  1744. widget:set_alpha(strength)
  1745. widget:set_visible(true)
  1746. end,
  1747. --- Effect of an attack that hits a target: the effects made from the original assets.
  1748. --
  1749. -- @param attack Attack data.
  1750. -- @param result The result of the attack, as in BattleUi.calculate_attack.
  1751. hit = function(self, attack, side, index, result)
  1752. local x, y = self:get_position(side, index, self.BODY_HEIGHT)
  1753. if x == nil then do return end end
  1754. local heals = result.amount < 0
  1755. self:play_model_effect(attack, side, index, heals)
  1756. if heals and attack ~= nil and attack.id ~= nil and attack.id >= 0 then
  1757. self:heal_sparkles(x, y, self:get_power(attack))
  1758. end
  1759. end,
  1760. --- Shows a floating text over a combatant.
  1761. --
  1762. -- @param text The text to show.
  1763. -- @param colour Table with r, g, b.
  1764. text = function(self, side, index, text, colour)
  1765. local x, y = self:get_position(side, index, self.BODY_HEIGHT + 0.6)
  1766. if x == nil then do return end end
  1767. local slot = nil
  1768. for i = 1, self.NUMBERS do
  1769. if self.numbers[i] == nil then slot = i break end
  1770. end
  1771. -- If all are used, take the one that has been there the longest.
  1772. if slot == nil then
  1773. local oldest = 1
  1774. for i = 2, self.NUMBERS do
  1775. if self.numbers[i].age > self.numbers[oldest].age then oldest = i end
  1776. end
  1777. slot = oldest
  1778. end
  1779. -- Spread the numbers of the same target a bit so they don't pile up.
  1780. self.numbers[slot] = {
  1781. index = slot, x = x + (math.random() - 0.5) * 40 - 7 * #text / 2, y = y - 10 * math.random(),
  1782. age = 0, life = 1.1, text = text, colour = colour,
  1783. anchor = self.spawn_anchor, ax = x, ay = y,
  1784. }
  1785. local number = self.numbers[slot]
  1786. local widget = ui_manager:get_widget("BattleFx.N" .. tostring(slot))
  1787. -- The colour is applied when the text is set.
  1788. widget:set_colour(colour[1], colour[2], colour[3])
  1789. widget:set_alpha(1)
  1790. widget:set_text(text)
  1791. widget:set_x(0, number.x)
  1792. widget:set_y(0, number.y)
  1793. widget:set_visible(true)
  1794. end,
  1795. --- Shows the HP or MP lost or recovered by a combatant.
  1796. --
  1797. -- @param amount Positive for damage, negative for recovery.
  1798. -- @param mp True if it's MP.
  1799. number = function(self, side, index, amount, mp)
  1800. local colour = self.COLOUR_DAMAGE
  1801. if amount < 0 then colour = self.COLOUR_HEAL end
  1802. if mp then colour = self.COLOUR_MP end
  1803. self:text(side, index, tostring(math.abs(amount)), colour)
  1804. end,
  1805. --- Advances the effects.
  1806. --
  1807. -- @param now Game time.
  1808. update = function(self, now)
  1809. local dt = math.max(0, math.min(0.1, now - self.last_time))
  1810. self.last_time = now
  1811. self:update_timelines(dt)
  1812. self:update_pieces(dt)
  1813. self:update_sprites(dt)
  1814. self:update_auras(dt)
  1815. for i, n in pairs(self.numbers) do
  1816. n.age = n.age + dt
  1817. local widget = ui_manager:get_widget("BattleFx.N" .. tostring(i))
  1818. if n.age >= n.life then
  1819. widget:set_visible(false)
  1820. self.numbers[i] = nil
  1821. else
  1822. -- Rise, and fade out in the last part. Text colour is baked when it's set, so the
  1823. -- text is set again with the new alpha.
  1824. local alpha = math.min(1, (n.life - n.age) / (n.life * 0.4))
  1825. widget:set_colour(n.colour[1], n.colour[2], n.colour[3])
  1826. widget:set_alpha(alpha)
  1827. widget:set_text(n.text)
  1828. local shift_x, shift_y = 0, 0
  1829. if n.anchor ~= nil then
  1830. local nx, ny = self:project(n.anchor)
  1831. if nx ~= nil then shift_x, shift_y = nx - n.ax, ny - n.ay end
  1832. end
  1833. widget:set_x(0, n.x + shift_x)
  1834. widget:set_y(0, n.y - 45 * (n.age / n.life) + shift_y)
  1835. end
  1836. end
  1837. local f = self.flash_state
  1838. if f ~= nil then
  1839. f.age = f.age + dt
  1840. local widget = ui_manager:get_widget("BattleFx.Flash")
  1841. if f.age >= f.life then
  1842. widget:set_visible(false)
  1843. self.flash_state = nil
  1844. else
  1845. widget:set_alpha(f.strength * (1 - f.age / f.life))
  1846. end
  1847. end
  1848. end,
  1849. }