battle_camera.lua 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. --- Camera scripts of the battles (attacks, magic, enemy attacks).
  2. --
  3. -- The original game has a small script for the camera of each attack, in the files camdat0.bin,
  4. -- camdat1.bin and camdat2.bin (the installer writes them to game/battle/battle_cameras.lua). Two points
  5. -- move in a battle: the camera (position stream) and the point it looks at (look stream), each one
  6. -- with its own script of bytes, found with (camera id * 3 + variant). The ids are the camera_single
  7. -- and camera_multiple of the attacks. The interpreters are in ff7.exe (0x5c2341 and 0x5c3f45).
  8. --
  9. -- A script is a list of opcodes (0xd5-0xff, other values do nothing) with operands that are bytes or
  10. -- 16 bit values (little endian, with sign). Positions are in the units of the original game, with x to
  11. -- the side, y down and z in depth (the enemies are in the negative side), and the ones that follow an
  12. -- actor are relative to a point of it, the offsets turned with the way it faces if the stream has
  13. -- "rotate" on. The frame of the scene is converted like the positions of the formations:
  14. --
  15. -- scene = (x, z, -y) / 255
  16. --
  17. -- (the division is the one the installer uses for every coordinate of the battle). The facing angle of
  18. -- an actor in ff7.exe is the one of the direction to its target plus half a turn (0x420d5b), so the
  19. -- positive z of a turned offset is behind the actor. The streams run
  20. -- one opcode after the other until one of them waits or places the point (the ones that do are
  21. -- listed in YIELDS), once per tick, 15 ticks per second like the effects.
  22. --
  23. -- The state of the battle is read through an environment (see BattleCameraController in
  24. -- battle_camera_controller.lua): env:actor(ref) gives the position of an actor of the scene, the way it
  25. -- faces and its height, and env.formation the camera that was in use before the script.
  26. BattleCamera = {}
  27. BattleCamera.__index = BattleCamera
  28. --- Scene units for each original unit.
  29. BattleCamera.UNIT = 1 / 255
  30. --- Ticks of the scripts in a second.
  31. BattleCamera.TPS = 15
  32. --- Operands (the bytes after the opcode) of each opcode: the numbers are the ones of the installer
  33. -- (BattleCameraScripts.h).
  34. BattleCamera.LENGTH = {
  35. pos = {
  36. [0xd5] = 2, [0xd6] = 0, [0xd7] = 2, [0xd8] = 9, [0xd9] = 0, [0xda] = 0, [0xdb] = 0, [0xdc] = 0,
  37. [0xdd] = 1, [0xde] = 1, [0xdf] = 0, [0xe0] = 2, [0xe1] = 0, [0xe2] = 1, [0xe3] = 9, [0xe4] = 8,
  38. [0xe5] = 8, [0xe6] = 7, [0xe7] = 8, [0xe9] = 8, [0xeb] = 9, [0xef] = 8, [0xf0] = 7, [0xf1] = 0,
  39. [0xf2] = 5, [0xf3] = 5, [0xf4] = 0, [0xf5] = 1, [0xf7] = 7, [0xf8] = 12, [0xf9] = 6, [0xfe] = 0,
  40. [0xff] = 0,
  41. },
  42. look = {
  43. [0xd8] = 9, [0xd9] = 0, [0xdb] = 0, [0xdc] = 0, [0xdd] = 1, [0xde] = 1, [0xdf] = 0, [0xe0] = 2,
  44. [0xe1] = 0, [0xe2] = 1, [0xe3] = 9, [0xe4] = 8, [0xe5] = 8, [0xe6] = 7, [0xe8] = 8, [0xea] = 8,
  45. [0xec] = 9, [0xf0] = 8, [0xf4] = 0, [0xf5] = 1, [0xf8] = 7, [0xf9] = 7, [0xfa] = 6, [0xfe] = 0,
  46. [0xff] = 0,
  47. },
  48. }
  49. --- Opcodes that end the processing of a tick (the ones that wait or place the point).
  50. BattleCamera.YIELDS = {
  51. pos = {[0xef] = true, [0xf0] = true, [0xf4] = true, [0xf7] = true, [0xff] = true},
  52. look = {[0xf0] = true, [0xf4] = true, [0xf8] = true, [0xf9] = true, [0xff] = true},
  53. }
  54. --- Maximum opcodes in a tick, in case a script loops without waiting.
  55. BattleCamera.MAX_OPCODES = 64
  56. --- Gets the bytes of a script.
  57. --
  58. -- @param set Number of the camdat file (0 to 2).
  59. -- @param kind "pos" or "look".
  60. -- @param id Camera id.
  61. -- @param variant 0 to 2.
  62. -- @return The list of bytes, nil if there is none (ids of -1 or 0xffff do nothing).
  63. function BattleCamera.get_script(set, kind, id, variant)
  64. if id == nil or id < 0 or id >= 0xffff then return nil end
  65. local data = Game.BattleCameras ~= nil and Game.BattleCameras[set] or nil
  66. if data == nil then return nil end
  67. local key = data[kind == "pos" and "position" or "look"][id * 3 + (variant or 0) + 1]
  68. if key == nil then return nil end
  69. return data.scripts[key]
  70. end
  71. local function vec(x, y, z) return {x, y, z} end
  72. local function new_stream(kind, script)
  73. return {
  74. kind = kind, script = script, pc = 1, wait = 0, rotate = false, linear = false,
  75. pos = vec(0, 0, 0), tasks = {}, finished = script == nil, yielded = false,
  76. }
  77. end
  78. --- Creates the camera of a pair of scripts.
  79. --
  80. -- @param position Script of the position (list of bytes, can be nil).
  81. -- @param look Script of the look at point.
  82. -- @param env See above.
  83. function BattleCamera.new(position, look, env)
  84. local self = setmetatable({}, BattleCamera)
  85. self.env = env
  86. self.cam = new_stream("pos", position)
  87. self.look = new_stream("look", look)
  88. local f = env.formation
  89. self.cam.pos = {f.pos[1], f.pos[2], f.pos[3]}
  90. self.look.pos = {f.look[1], f.look[2], f.look[3]}
  91. -- The camera the script started on (opcode d9). The opcode dd changes the one of e1 and e2, not it.
  92. self.base = f
  93. return self
  94. end
  95. --- Is everything of the script done? (the scripts stay on their last opcode forever)
  96. function BattleCamera:is_finished()
  97. return self.cam.finished and self.look.finished and #self.cam.tasks == 0 and #self.look.tasks == 0
  98. end
  99. -- Operand readers. A "reader" has the script and the position of the next byte.
  100. local function u8(s)
  101. local v = s.script[s.pc]
  102. s.pc = s.pc + 1
  103. return v or 0
  104. end
  105. local function s16(s)
  106. local lo, hi = u8(s), u8(s)
  107. local v = hi * 256 + lo
  108. if v >= 32768 then v = v - 65536 end
  109. return v
  110. end
  111. --- Converts a vector of the original frame to the scene, as an offset.
  112. local function to_scene_offset(x, y, z)
  113. local k = BattleCamera.UNIT
  114. return x * k, z * k, -y * k
  115. end
  116. --- Converts a position of the original frame (absolute) to the scene.
  117. function BattleCamera:absolute(x, y, z)
  118. return vec(to_scene_offset(x, y, z))
  119. end
  120. --- Position of a point of an actor, plus an offset of the original frame.
  121. --
  122. -- As in ff7.exe, the offsets of the position of the camera grow with the size of the model (0x5c6290) and
  123. -- turn with the way it faces if the stream has "rotate" on, and the ones of the point it looks at do
  124. -- neither: they only move down by the height that the model has for it (0x5c5a78). None of that applies to
  125. -- the center of a group of targets.
  126. --
  127. -- @param kind Stream of the offset, "pos" or "look".
  128. -- @param rotate If the offset turns with the way the actor faces.
  129. function BattleCamera:anchor(ref, point, ox, oy, oz, rotate, kind)
  130. local actor = self.env:actor(ref)
  131. if actor == nil then return nil end
  132. -- The points are ends of bones of the model. Without that data (an old install), the middle of the actor.
  133. local px, py, pz
  134. if actor.point ~= nil then px, py, pz = actor.point(point) end
  135. if px == nil then px, py, pz = actor.pos[1], actor.pos[2], actor.pos[3] + actor.height * 0.5 end
  136. if actor.group then
  137. rotate = false
  138. end
  139. if kind == "look" then
  140. oy = oy + (actor.look_height or 0)
  141. rotate = false
  142. elseif actor.size ~= nil then
  143. local function scale(value, size)
  144. -- A Small actor halves the factors, but they don't go under 256.
  145. if actor.small then size = math.max(256, math.floor(size / 2)) end
  146. return math.floor(value * size / 256)
  147. end
  148. ox, oy, oz = scale(ox, actor.size[1]), scale(oy, actor.size[2]), scale(oz, actor.size[3])
  149. end
  150. local k = BattleCamera.UNIT
  151. local dx, dy, dz
  152. if rotate then
  153. -- x is to the left of the way it faces, z behind it, y down (the rotation of 0x5c6290).
  154. local fx, fy = actor.forward[1], actor.forward[2]
  155. dx = (-fy) * ox * k - fx * oz * k
  156. dy = fx * ox * k - fy * oz * k
  157. dz = -oy * k
  158. else
  159. dx, dy, dz = to_scene_offset(ox, oy, oz)
  160. end
  161. return vec(px + dx, py + dy, pz + dz)
  162. end
  163. --- The reference of an actor of a script (the byte of the opcodes that take it): 0-2 the party and
  164. -- 4-9 the enemies.
  165. local function actor_from_byte(b)
  166. if b >= 0 and b <= 2 then return {side = "party", index = b + 1} end
  167. if b >= 4 and b <= 9 then return {side = "enemies", index = b - 3} end
  168. return "targets"
  169. end
  170. --- Starts a move to a point in some ticks.
  171. local function start_move(stream, target, ticks)
  172. stream.tasks[#stream.tasks + 1] = {
  173. type = "move", start = {stream.pos[1], stream.pos[2], stream.pos[3]}, target = target,
  174. ticks = math.max(1, ticks), age = 0, linear = stream.linear,
  175. }
  176. end
  177. --- Starts the follow of an actor for some ticks.
  178. local function start_follow(stream, ref, point, ox, oy, oz, ticks, rotate)
  179. stream.tasks[#stream.tasks + 1] = {
  180. type = "follow", ref = ref, point = point, off = {ox, oy, oz}, ticks = math.max(1, ticks),
  181. age = 0, rotate = rotate, kind = stream.kind,
  182. }
  183. end
  184. --- Starts an orbit of the position around the point the camera looks at (opcode f8 of the position,
  185. -- task 0x5c3c7d of ff7.exe).
  186. --
  187. -- All the values are the ones of the script: angles in 4096ths of a turn, distances in the original frame.
  188. --
  189. -- @param speed Angle added each tick.
  190. -- @param radius Horizontal distance to the point, in eighths.
  191. -- @param angle Starting angle.
  192. -- @param radius_speed Added to the radius each tick.
  193. -- @param y_speed Added to the height (down) of the camera each tick.
  194. local function start_orbit(stream, speed, radius, angle, radius_speed, y_speed, ticks)
  195. stream.tasks[#stream.tasks + 1] = {
  196. type = "orbit", speed = speed, radius = radius, angle = angle, radius_speed = radius_speed,
  197. y_speed = y_speed, ticks = math.max(1, ticks), age = 0,
  198. }
  199. end
  200. --- The reference of the "current actor" of the opcodes (byte 0xbe40e4 of ff7.exe): the target, or
  201. -- the group of targets if there are several or after the opcode df.
  202. function BattleCamera:current_ref()
  203. return self.group and "targets" or "current"
  204. end
  205. --- Position of one of the two views a model has for the cameras (opcode e0): the position of the
  206. -- camera or the point it looks at, depending on the stream. It doesn't turn or grow with the model. The
  207. -- height of the first view is over the ground, and the one of the second over the actor.
  208. --
  209. -- @param view 0 or 1.
  210. -- @return Nil if there is no data, like for a group of targets.
  211. function BattleCamera:view_point(ref, view, kind)
  212. local actor = self.env:actor(ref)
  213. if actor == nil or actor.views == nil or actor.views[view + 1] == nil then return nil end
  214. local offset = actor.views[view + 1][kind]
  215. local x, y, z = to_scene_offset(offset[1], offset[2], offset[3])
  216. if view ~= 0 then z = z + actor.pos[3] end
  217. return vec(actor.pos[1] + x, actor.pos[2] + y, z)
  218. end
  219. --- Places a stream at a point of an actor, now.
  220. function BattleCamera:place(stream, ref, point, ox, oy, oz, rotate)
  221. local p = self:anchor(ref, point, ox, oy, oz, rotate, stream.kind)
  222. if p ~= nil then stream.pos = p end
  223. end
  224. --- Runs the opcodes of a stream until it waits.
  225. function BattleCamera:run(stream)
  226. local kind = stream.kind
  227. local lengths = BattleCamera.LENGTH[kind]
  228. local yields = BattleCamera.YIELDS[kind]
  229. local env = self.env
  230. for _ = 1, BattleCamera.MAX_OPCODES do
  231. if stream.finished then return end
  232. local op = stream.script[stream.pc]
  233. if op == nil then stream.finished = true return end
  234. stream.pc = stream.pc + 1
  235. local length = lengths[op]
  236. if length == nil then
  237. -- It isn't an opcode: nothing happens, but the tick is over.
  238. return
  239. end
  240. -- Opcodes that are the same in both streams (the ones with the same number and meaning).
  241. if op == 0xff then
  242. stream.pc = stream.pc - 1
  243. stream.finished = true
  244. return
  245. elseif op == 0xf5 then
  246. local n = u8(stream)
  247. if n >= 128 then n = n - 256 end
  248. stream.wait = n
  249. elseif op == 0xf4 then
  250. if stream.wait ~= 0 then
  251. stream.wait = stream.wait - 1
  252. stream.pc = stream.pc - 1
  253. return
  254. end
  255. elseif op == 0xfe then
  256. if stream.wait == 0 and stream.script[stream.pc] == 0xc0 then
  257. stream.pc = 1
  258. stream.wait = 0
  259. end
  260. elseif op == 0xdc then
  261. stream.rotate = true
  262. elseif op == 0xdf then
  263. -- From now on, the "current actor" of both streams is the group of targets.
  264. self.group = true
  265. elseif kind == "pos" and op == 0xf1 then
  266. stream.rotate = false
  267. elseif kind == "look" and op == 0xdb then
  268. stream.rotate = false
  269. elseif kind == "pos" and op == 0xda then
  270. stream.linear = false
  271. elseif kind == "pos" and op == 0xdb then
  272. stream.linear = true
  273. elseif op == 0xdd then
  274. -- Chooses the camera of the formation that e1 and e2 use, for both streams.
  275. local index = u8(stream)
  276. local chosen = env.formation_at ~= nil and env.formation_at(index) or nil
  277. if chosen ~= nil then env.formation, self.formation_index = chosen, index end
  278. elseif op == 0xe1 or op == 0xd9 then
  279. -- The camera of the formation (e1), or the one that was in use before the script (d9).
  280. local view = op == 0xd9 and self.base or env.formation
  281. local source = kind == "pos" and view.pos or view.look
  282. stream.pos = {source[1], source[2], source[3]}
  283. elseif op == 0xe2 then
  284. local source = kind == "pos" and env.formation.pos or env.formation.look
  285. start_move(stream, {source[1], source[2], source[3]}, u8(stream))
  286. elseif op == 0xe6 then
  287. local x, y, z = s16(stream), s16(stream), s16(stream)
  288. start_move(stream, self:absolute(x, y, z), u8(stream))
  289. elseif op == 0xe0 then
  290. -- Move to a view of the current actor: view, ticks.
  291. local view, ticks = u8(stream), u8(stream)
  292. local target = self:view_point(self:current_ref(), view, kind)
  293. if target ~= nil then start_move(stream, target, ticks) end
  294. elseif (kind == "pos" and op == 0xf9) or (kind == "look" and op == 0xfa) then
  295. local x, y, z = s16(stream), s16(stream), s16(stream)
  296. stream.pos = self:absolute(x, y, z)
  297. elseif op == 0xe3 or op == 0xd8 or op == 0xe4 or op == 0xe5 then
  298. -- Move to a point of an actor, with an offset: actor (e3 and d8), point, x, y, z, ticks.
  299. local ref
  300. if op == 0xe3 then ref = actor_from_byte(u8(stream))
  301. elseif op == 0xd8 then ref = {target = u8(stream) + 1}
  302. elseif op == 0xe4 then ref = "attacker"
  303. else ref = self:current_ref() end
  304. local point = u8(stream)
  305. local x, y, z = s16(stream), s16(stream), s16(stream)
  306. local ticks = u8(stream)
  307. local target = self:anchor(ref, point, x, y, z, stream.rotate, stream.kind)
  308. if target ~= nil then start_move(stream, target, ticks) end
  309. elseif (kind == "pos" and (op == 0xe7 or op == 0xe9 or op == 0xeb))
  310. or (kind == "look" and (op == 0xe8 or op == 0xea or op == 0xec)) then
  311. -- Follow a point of an actor with an offset, for some ticks.
  312. local ref
  313. if op == 0xeb or op == 0xec then ref = actor_from_byte(u8(stream))
  314. elseif op == 0xe7 or op == 0xe8 then ref = "attacker"
  315. else ref = self:current_ref() end
  316. local point = u8(stream)
  317. local x, y, z = s16(stream), s16(stream), s16(stream)
  318. local ticks = u8(stream)
  319. start_follow(stream, ref, point, x, y, z, ticks, stream.rotate)
  320. elseif (kind == "pos" and (op == 0xef or op == 0xf0 or op == 0xf7))
  321. or (kind == "look" and (op == 0xf0 or op == 0xf8 or op == 0xf9)) then
  322. -- Place at a point of an actor, with an offset.
  323. local ref
  324. if (kind == "pos" and op == 0xef) or (kind == "look" and op == 0xf0) then ref = actor_from_byte(u8(stream))
  325. elseif (kind == "pos" and op == 0xf0) or (kind == "look" and op == 0xf9) then ref = "attacker"
  326. else ref = self:current_ref() end
  327. local point = u8(stream)
  328. local x, y, z = s16(stream), s16(stream), s16(stream)
  329. self:place(stream, ref, point, x, y, z, stream.rotate)
  330. elseif kind == "pos" and op == 0xf8 then
  331. local speed, radius, angle = s16(stream), s16(stream), s16(stream)
  332. local radius_speed, y_speed, ticks = s16(stream), s16(stream), s16(stream)
  333. start_orbit(stream, speed, radius, angle, radius_speed, y_speed, ticks)
  334. else
  335. -- Opcodes that don't change the camera (events of the battle) and the ones no script of the
  336. -- game uses (like f2 and f3): skip their operands.
  337. stream.pc = stream.pc + length
  338. end
  339. if yields[op] and op ~= 0xff and op ~= 0xf4 then return end
  340. end
  341. end
  342. --- Advances the tasks of a stream one tick.
  343. function BattleCamera:run_tasks(stream)
  344. local alive = {}
  345. for _, task in ipairs(stream.tasks) do
  346. task.age = task.age + 1
  347. if task.type == "move" then
  348. if task.age >= task.ticks then
  349. stream.pos = {task.target[1], task.target[2], task.target[3]}
  350. else
  351. local f
  352. if task.linear then f = (task.age) / task.ticks
  353. else f = (1 - math.cos(math.pi * (task.age - 1) / task.ticks)) / 2 end
  354. stream.pos = {
  355. task.start[1] + (task.target[1] - task.start[1]) * f,
  356. task.start[2] + (task.target[2] - task.start[2]) * f,
  357. task.start[3] + (task.target[3] - task.start[3]) * f,
  358. }
  359. end
  360. elseif task.type == "orbit" then
  361. task.angle = task.angle + task.speed
  362. local a = task.angle * 2 * math.pi / 4096
  363. local r = task.radius * 8 * BattleCamera.UNIT
  364. local center = self.look.pos
  365. stream.pos = {
  366. center[1] + math.sin(a) * r,
  367. center[2] + math.cos(a) * r,
  368. stream.pos[3] - task.y_speed * BattleCamera.UNIT,
  369. }
  370. task.radius = task.radius + task.radius_speed
  371. else
  372. local p = self:anchor(task.ref, task.point, task.off[1], task.off[2], task.off[3], task.rotate, task.kind)
  373. if p ~= nil then stream.pos = p end
  374. end
  375. if task.age < task.ticks then alive[#alive + 1] = task end
  376. end
  377. stream.tasks = alive
  378. end
  379. --- Advances the camera one tick.
  380. function BattleCamera:tick()
  381. for _, stream in ipairs({self.cam, self.look}) do
  382. self:run(stream)
  383. self:run_tasks(stream)
  384. end
  385. end
  386. --- The position of the camera and the point it looks at, in the scene.
  387. function BattleCamera:get()
  388. return self.cam.pos, self.look.pos
  389. end
  390. --- Controller of the camera of the battle.
  391. --
  392. -- Starts the camera of an action when it begins, moves the camera while the action goes on and takes it
  393. -- back to the camera of the formation when it ends. Nothing moves if the camera is set to manual
  394. -- in the config menu (see {@see Battle.is_camera_auto}).
  395. BattleCameraController = {
  396. --- Number of the file camdatN.bin that the original uses for each battle layout (0 normal,
  397. -- 1 preemptive, 2 back attack, 3 side attack, ... 8 locked): the table at 0x7b8528 of ff7.exe,
  398. -- indexed with the type of the battle (byte 0x999e12), that has the same numbers as the layouts.
  399. FILE_BY_LAYOUT = {[0] = 0, 0, 1, 2, 2, 2, 2, 2, 0},
  400. --- Ticks in a second of the way back to the camera of the formation. The movement (half of the
  401. -- distance in every tick) is the one of the executable, but its rate isn't known: it was too
  402. -- fast with 15, the rate of the scripts, and with 10.
  403. RETURN_TPS = 5,
  404. --- The camera that is running, nil if none.
  405. camera = nil,
  406. --- Seconds that are left to the next tick, and the last two results (for the interpolation).
  407. accumulated = 0,
  408. previous = nil,
  409. current = nil,
  410. --- Ids that were started for debugging: set to a number to force an id.
  411. forced_id = nil,
  412. }
  413. --- Position of an entity and its height, in the scene.
  414. local function entity_info(entity)
  415. local x, y, z = entity:get_position()
  416. local info = BattleCameraController.heights[entity:get_name()]
  417. if info == nil then
  418. -- The height is the one of the highest bone.
  419. local top = z
  420. for bone = 0, 24 do
  421. local ok, bx, by, bz = pcall(entity.get_bone_position, entity, bone)
  422. if ok and bz ~= nil and bz > top then top = bz end
  423. end
  424. info = math.max(top - z, 1.5)
  425. BattleCameraController.heights[entity:get_name()] = info
  426. end
  427. return x, y, z, info
  428. end
  429. BattleCameraController.heights = {}
  430. --- Data of the battle models for the cameras, by model name. False for the models without it.
  431. BattleCameraController.models = {}
  432. --- Gets the camera data of a model, written by the installer. Nil if there is none.
  433. local function model_data(name)
  434. if name == nil then return nil end
  435. local data = BattleCameraController.models[name]
  436. if data == nil then
  437. local chunk = loadfile("./game/battle/scripts/" .. name .. "_camera.lua")
  438. if chunk ~= nil then chunk() end
  439. data = BattleModelCameras ~= nil and BattleModelCameras[name] or false
  440. BattleCameraController.models[name] = data
  441. end
  442. return data or nil
  443. end
  444. --- Describes an actor of the scene for the camera.
  445. --
  446. -- @param entity Its entity.
  447. -- @param side Its side, and index its place in it.
  448. -- @param forward The way it faces.
  449. local function actor_info(entity, side, index, forward)
  450. local x, y, z, height = entity_info(entity)
  451. local info = {pos = {x, y, z}, height = height, forward = forward({x, y, z})}
  452. local ui = UiContainer.BattleUi
  453. local data = model_data(ui:get_model_name(side, index))
  454. if data ~= nil then
  455. info.size, info.look_height, info.views = data.size, data.look_height, data.views
  456. info.small = ui:has_status(side, index, BattleStatus.SMALL)
  457. -- Position of a point of the model, nil if the model doesn't have it.
  458. info.point = function(point)
  459. local bone = data.points[point + 1]
  460. if bone == nil then return nil end
  461. local bx, by, bz
  462. if bone == 0 then bx, by, bz = entity:get_bone_position(0)
  463. elseif data.lengths[bone - 1] ~= nil then bx, by, bz = entity:get_bone_end(bone - 1, data.lengths[bone - 1])
  464. end
  465. if bx == nil or (bx == 0 and by == 0 and bz == 0) then return nil end
  466. return bx, by, bz
  467. end
  468. end
  469. return info
  470. end
  471. --- Builds the environment of the camera for an action.
  472. local function build_env(side, actor, target_side, targets, formation)
  473. local ui = UiContainer.BattleUi
  474. local function entity_of(s, index)
  475. local e = ui:get_entity(s, index)
  476. return e
  477. end
  478. -- Middle of the area the targets are in, on the ground.
  479. local function ground_center(s, list)
  480. local min_x, max_x, min_y, max_y
  481. for _, index in ipairs(list) do
  482. local e = entity_of(s, index)
  483. if e ~= nil then
  484. local x, y = entity_info(e)
  485. min_x, max_x = math.min(min_x or x, x), math.max(max_x or x, x)
  486. min_y, max_y = math.min(min_y or y, y), math.max(max_y or y, y)
  487. end
  488. end
  489. if min_x == nil then return nil end
  490. return {(min_x + max_x) / 2, (min_y + max_y) / 2, 0}
  491. end
  492. local attacker_entity = entity_of(side, actor)
  493. local attacker_pos
  494. if attacker_entity ~= nil then
  495. local x, y, z = entity_info(attacker_entity)
  496. attacker_pos = {x, y, z}
  497. end
  498. local target_pos = ground_center(target_side, targets)
  499. local function direction(from, to)
  500. if from == nil or to == nil then return {0, -1} end
  501. local dx, dy = to[1] - from[1], to[2] - from[2]
  502. local l = math.sqrt(dx * dx + dy * dy)
  503. if l < 0.0001 then return {0, -1} end
  504. return {dx / l, dy / l}
  505. end
  506. local function toward(to) return function(from) return direction(from, to) end end
  507. local env = {formation = formation}
  508. function env:actor(ref)
  509. if ref == "attacker" then
  510. if attacker_entity == nil then return nil end
  511. return actor_info(attacker_entity, side, actor, toward(target_pos))
  512. elseif ref == "current" and #targets == 1 then
  513. local e = entity_of(target_side, targets[1])
  514. if e ~= nil then return actor_info(e, target_side, targets[1], toward(attacker_pos)) end
  515. elseif ref == "current" or ref == "targets" then
  516. -- The center of the group of targets, on the ground (0x5bfe39).
  517. local center = ground_center(target_side, targets)
  518. if center == nil then return nil end
  519. return {
  520. group = true, pos = center, height = 0, forward = direction(center, attacker_pos),
  521. point = function() return center[1], center[2], center[3] end,
  522. }
  523. elseif type(ref) == "table" and ref.target ~= nil then
  524. -- One of the targets, by its place in the list (opcode d8).
  525. local index = targets[ref.target] or targets[1]
  526. local e = entity_of(target_side, index)
  527. if e ~= nil then return actor_info(e, target_side, index, toward(attacker_pos)) end
  528. elseif type(ref) == "table" then
  529. local ref_side = ref.side == "party" and Battle.TargetGroup.PARTY or Battle.TargetGroup.ENEMIES
  530. local e = entity_of(ref_side, ref.index)
  531. if e == nil then return nil end
  532. return actor_info(e, ref_side, ref.index, toward(ref.side == "party" and target_pos or attacker_pos))
  533. end
  534. return nil
  535. end
  536. return env
  537. end
  538. --- Starts the camera of an action.
  539. --
  540. -- @param side Side of the actor.
  541. -- @param actor Index of the actor.
  542. -- @param target_side Side of the targets.
  543. -- @param targets List of indexes of the targets.
  544. -- @param camera_single Id for a single target (nil or 0xffff for none).
  545. -- @param camera_multiple Id for several targets.
  546. -- @return True if there is a camera.
  547. function BattleCameraController:start(side, actor, target_side, targets, camera_single, camera_multiple)
  548. self:stop(false)
  549. self.from_script = false
  550. if Battle.is_camera_auto ~= nil and not Battle.is_camera_auto() then return false end
  551. if targets == nil or #targets == 0 then return false end
  552. local id = #targets > 1 and camera_multiple or camera_single
  553. if self.forced_id ~= nil then id = self.forced_id end
  554. local variant = math.random(0, 2)
  555. local layout = battle_manager:get_layout()
  556. local file = self.FILE_BY_LAYOUT[layout] or 0
  557. local position = BattleCamera.get_script(file, "pos", id, variant)
  558. local look = BattleCamera.get_script(file, "look", id, variant)
  559. if position == nil and look == nil then return false end
  560. -- A camera that was going back to the one of the formation is replaced by this one (if there is
  561. -- none, it goes on: an action with no camera must not leave it where it was).
  562. self.returning = nil
  563. self.last = nil
  564. local index = UiContainer.BattleUi.camera_index or 0
  565. local px, py, pz, lx, ly, lz = battle_manager:get_camera(index)
  566. local formation = {pos = {px, py, pz}, look = {lx, ly, lz}}
  567. local env = build_env(side, actor, target_side, targets, formation)
  568. self.camera = BattleCamera.new(position, look, env)
  569. self.camera_index = index
  570. self.accumulated = 0
  571. self.previous = nil
  572. self.current = nil
  573. print("Battle camera " .. tostring(id) .. " variant " .. tostring(variant) .. " file " .. tostring(file))
  574. return true
  575. end
  576. --- Starts the camera of the victory: shots of the party at fixed places, which depend on the camera
  577. -- file of the layout of the battle (camera id -3 of ff7.exe, set at 0x42dbd7).
  578. --
  579. -- @return True if there is a camera (an old install doesn't have its data).
  580. function BattleCameraController:start_victory()
  581. local variant = math.random(0, 2)
  582. local file = self.FILE_BY_LAYOUT[battle_manager:get_layout()] or 0
  583. local data = Game.BattleCameras ~= nil and Game.BattleCameras[file] or nil
  584. if data == nil or data.victory_position == nil then return false end
  585. local position = data.scripts[data.victory_position[variant + 1]]
  586. local look = data.scripts[data.victory_look[variant + 1]]
  587. if position == nil and look == nil then return false end
  588. self:reset()
  589. local index = UiContainer.BattleUi.camera_index or 0
  590. local px, py, pz, lx, ly, lz = battle_manager:get_camera(index)
  591. -- These scripts don't use any actor.
  592. local env = {formation = {pos = {px, py, pz}, look = {lx, ly, lz}}, actor = function() return nil end}
  593. self.camera = BattleCamera.new(position, look, env)
  594. self.camera_index = index
  595. print("Battle camera: victory, variant " .. tostring(variant) .. " file " .. tostring(file))
  596. return true
  597. end
  598. --- Starts one of the camera scripts that ff7.exe has (camera id -2): the ones of the start of the
  599. -- battles, and the ones that the animation scripts of some enemies use. They go to a camera of the
  600. -- formation, which they can change.
  601. --
  602. -- @param id Number of the script.
  603. -- @return True if there is a camera.
  604. function BattleCameraController:start_script(id)
  605. local data = Game.BattleCameras ~= nil and Game.BattleCameras.intro or nil
  606. if data == nil or id == nil or id < 0 then return false end
  607. local position = data.scripts[data.position[id + 1]]
  608. local look = data.scripts[data.look[id + 1]]
  609. if position == nil and look == nil then return false end
  610. local function formation_at(index)
  611. if index < 0 or index > 2 then return nil end
  612. local px, py, pz, lx, ly, lz = battle_manager:get_camera(index)
  613. return {pos = {px, py, pz}, look = {lx, ly, lz}}
  614. end
  615. local index = UiContainer.BattleUi.camera_index or 0
  616. local env = {formation = formation_at(index), formation_at = formation_at, actor = function() return nil end}
  617. self:reset()
  618. self.camera = BattleCamera.new(position, look, env)
  619. self.camera_index = index
  620. self.from_script = true
  621. return true
  622. end
  623. --- Starts the camera of the start of a battle: from some other place to a camera of the formation.
  624. -- The formation chooses the script.
  625. --
  626. -- @return True if there is a camera.
  627. function BattleCameraController:start_intro()
  628. self:reset()
  629. UiContainer.BattleUi.camera_index = 0
  630. local id = battle_manager:get_intro_camera()
  631. if not self:start_script(id) then return false end
  632. print("Battle camera: start of the battle, script " .. tostring(id))
  633. return true
  634. end
  635. --- Changes the camera of the formation the battle rests on, moving to it.
  636. --
  637. -- @param index 0 to 2.
  638. function BattleCameraController:set_view(index)
  639. if index < 0 or index > 2 then return end
  640. local previous = UiContainer.BattleUi.camera_index or 0
  641. UiContainer.BattleUi.camera_index = index
  642. self.camera_index = index
  643. if self.camera ~= nil or previous == index then return end
  644. local px, py, pz, lx, ly, lz = battle_manager:get_camera(previous)
  645. local from = self.returning ~= nil and self.returning.to or nil
  646. local cam, look = from ~= nil and from[1] or {px, py, pz}, from ~= nil and from[2] or {lx, ly, lz}
  647. px, py, pz, lx, ly, lz = battle_manager:get_camera(index)
  648. self.returning = {target = {{px, py, pz}, {lx, ly, lz}}, cam = cam, look = look, index = index}
  649. self.accumulated = 0
  650. end
  651. --- Is a script of an animation still moving the camera? It goes on even with no action.
  652. function BattleCameraController:is_script_running()
  653. return self.camera ~= nil and self.from_script == true and not self.camera:is_finished()
  654. end
  655. --- Moves the camera of the start of the battle, and leaves the one of the formation when it ends.
  656. --
  657. -- @param dt Seconds since the last call.
  658. -- @return True while it goes on.
  659. function BattleCameraController:update_intro(dt)
  660. if self.camera == nil then return false end
  661. self:update(dt)
  662. if not self.camera:is_finished() then return true end
  663. local index = self.camera_index or 0
  664. self:reset()
  665. battle_manager:set_camera(index)
  666. return false
  667. end
  668. --- Forgets the camera in use, without moving anything.
  669. function BattleCameraController:reset()
  670. self.camera = nil
  671. self.from_script = false
  672. self.returning = nil
  673. self.last = nil
  674. self.previous = nil
  675. self.current = nil
  676. self.accumulated = 0
  677. end
  678. --- Stops the camera and goes back to the one of the formation.
  679. --
  680. -- The original doesn't jump: every tick the camera and the point it looks at cover half of the
  681. -- distance that is left to the camera of the formation (function 0x5c64cc), until they get there.
  682. function BattleCameraController:stop(restore)
  683. if self.camera == nil then return end
  684. self.camera = nil
  685. if restore == false or self.last == nil then
  686. print("Battle camera: stopped without easing (" .. tostring(restore) .. ", " .. tostring(self.last ~= nil) .. ")")
  687. self.returning = nil
  688. if restore ~= false then battle_manager:set_camera(self.camera_index or 0) end
  689. return
  690. end
  691. local index = self.camera_index or 0
  692. local px, py, pz, lx, ly, lz = battle_manager:get_camera(index)
  693. self.returning = {target = {{px, py, pz}, {lx, ly, lz}}, cam = self.last.cam, look = self.last.look, index = index}
  694. self.accumulated = 0
  695. print("Battle camera: going back to the camera " .. tostring(index))
  696. end
  697. --- Moves the camera. Call it every frame.
  698. --
  699. -- @param dt Seconds since the last call.
  700. function BattleCameraController:update(dt)
  701. if self.returning ~= nil and self.camera == nil then
  702. local r = self.returning
  703. -- Every tick the points go half of the way (r.to is where they are after the last tick, and
  704. -- r.from where they were before), and between ticks they are drawn between the two.
  705. if r.from == nil then
  706. r.from = {{r.cam[1], r.cam[2], r.cam[3]}, {r.look[1], r.look[2], r.look[3]}}
  707. r.to = {{r.cam[1], r.cam[2], r.cam[3]}, {r.look[1], r.look[2], r.look[3]}}
  708. end
  709. self.accumulated = self.accumulated + dt * self.RETURN_TPS
  710. while self.accumulated >= 1 and self.returning ~= nil do
  711. self.accumulated = self.accumulated - 1
  712. local done = true
  713. for i = 1, 2 do
  714. for k = 1, 3 do
  715. r.from[i][k] = r.to[i][k]
  716. local diff = (r.target[i][k] - r.to[i][k]) / 2
  717. r.to[i][k] = r.to[i][k] + diff
  718. if math.abs(diff) > BattleCamera.UNIT then done = false end
  719. end
  720. end
  721. if done then
  722. self.returning = nil
  723. battle_manager:set_camera(r.index)
  724. print("Battle camera: back at the camera " .. tostring(r.index))
  725. end
  726. end
  727. if self.returning ~= nil then
  728. local f = self.accumulated
  729. local function mix(i, k) return r.from[i][k] + (r.to[i][k] - r.from[i][k]) * f end
  730. battle_manager:set_camera_position(mix(1, 1), mix(1, 2), mix(1, 3), mix(2, 1), mix(2, 2), mix(2, 3))
  731. end
  732. return
  733. end
  734. if self.camera == nil then return end
  735. self.accumulated = self.accumulated + dt * BattleCamera.TPS
  736. while self.accumulated >= 1 do
  737. self.accumulated = self.accumulated - 1
  738. self.camera:tick()
  739. -- A script chose another camera of the formation: the battle stays on it.
  740. local view = self.camera.formation_index
  741. if view ~= nil then
  742. self.camera_index = view
  743. UiContainer.BattleUi.camera_index = view
  744. end
  745. local cam, look = self.camera:get()
  746. self.previous = self.current
  747. self.current = {cam = {cam[1], cam[2], cam[3]}, look = {look[1], look[2], look[3]}}
  748. end
  749. if self.current == nil then return end
  750. local c, p, f = self.current, self.previous or self.current, self.accumulated
  751. local function mix(a, b) return {p[a][1] + (c[a][1] - p[a][1]) * f, p[a][2] + (c[a][2] - p[a][2]) * f, p[a][3] + (c[a][3] - p[a][3]) * f} end
  752. local cam, look = mix("cam"), mix("look")
  753. self.last = {cam = {cam[1], cam[2], cam[3]}, look = {look[1], look[2], look[3]}}
  754. battle_manager:set_camera_position(cam[1], cam[2], cam[3], look[1], look[2], look[3])
  755. end