effect_camera.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. --- Cameras of the battle effects (summons).
  2. --
  3. -- The original game has a small script for the camera of each effect, with the opcodes of a
  4. -- small interpreter inside ff7.exe (the Shiva one is at 0x58e57d). Two points move in the space
  5. -- of the effect: the camera (stream 0x40) and the point it looks at (stream 0x80). The script is
  6. -- a list of 16 bit words: the opcode in the low 6 bits and the stream in bit 0x40 or 0x80.
  7. --
  8. -- 0, 9 wait N ticks (N)
  9. -- 1 set the position (x, y, z)
  10. -- 2 move to a position in N ticks, in a straight line (N, x, y, z)
  11. -- 3 orbit: the camera goes to a position, but it stays in the line of the look point, and
  12. -- the distance to the look point changes smoothly (N, x, y, z)
  13. -- 4, 5 accelerated moves, played as the ones of opcode 2 and 3 (not used by Shiva)
  14. -- 6 two arguments, ignored. 8 one argument, ignored. 7 ends the script. A word of -1 ends
  15. -- the script too.
  16. --
  17. -- The interpreter takes a new opcode when one of the two streams has nothing to do, then moves
  18. -- both points one tick. Units are the ones of the original game (the scene ones are 0.0032 of them).
  19. EffectCamera = {}
  20. EffectCamera.__index = EffectCamera
  21. -- Arguments of each opcode.
  22. EffectCamera.ARGS = {[0] = 1, [1] = 3, [2] = 4, [3] = 4, [4] = 4, [5] = 5, [6] = 2, [7] = 0, [8] = 1, [9] = 1}
  23. local function copy(v) return {v[1], v[2], v[3]} end
  24. local function length(v) return math.sqrt(v[1] * v[1] + v[2] * v[2] + v[3] * v[3]) end
  25. local function new_stream()
  26. return {pos = {0, 0, 0}, dur = 0, mode = 0, delta = {0, 0, 0}, start = {0, 0, 0}, dist = 0, dist_step = 0}
  27. end
  28. --- Creates the camera of a script.
  29. --
  30. -- @param words List of the 16 bit words of the script.
  31. function EffectCamera.new(words)
  32. local self = setmetatable({}, EffectCamera)
  33. self.words = words
  34. self.index = 1
  35. self.finished = false
  36. self.cam = new_stream()
  37. self.look = new_stream()
  38. self.previous = {cam = copy(self.cam.pos), look = copy(self.look.pos)}
  39. return self
  40. end
  41. --- Runs the next opcode of the script.
  42. function EffectCamera:fetch()
  43. local w = self.words[self.index]
  44. if w == nil or w == -1 then self.finished = true return end
  45. local op = w % 64
  46. local count = EffectCamera.ARGS[op]
  47. if count == nil then self.finished = true return end
  48. local a, b, c, d, e = self.words[self.index + 1], self.words[self.index + 2], self.words[self.index + 3],
  49. self.words[self.index + 4], self.words[self.index + 5]
  50. self.index = self.index + 1 + count
  51. local s, other
  52. if (w % 128) >= 64 then s, other = self.cam, self.look else s, other = self.look, self.cam end
  53. s.mode = op
  54. if op == 0 or op == 9 then
  55. s.dur = math.max(0, a or 0)
  56. elseif op == 1 then
  57. s.pos = {a, b, c}
  58. -- Until the camera is placed for the first time, the camera of the battle is the one in use.
  59. if s == self.cam then self.started = true end
  60. elseif op == 2 or op == 4 then
  61. local n = math.max(1, a)
  62. s.dur = n
  63. s.delta = {(b - s.pos[1]) / n, (c - s.pos[2]) / n, (d - s.pos[3]) / n}
  64. s.mode = 2
  65. elseif op == 3 or op == 5 then
  66. local n = math.max(1, a)
  67. local target = op == 3 and {b, c, d} or {c, d, e}
  68. s.dur = n
  69. s.mode = 3
  70. s.start = copy(s.pos)
  71. s.delta = {(target[1] - s.pos[1]) / n, (target[2] - s.pos[2]) / n, (target[3] - s.pos[3]) / n}
  72. local d0 = length({other.pos[1] - s.pos[1], other.pos[2] - s.pos[2], other.pos[3] - s.pos[3]})
  73. -- Where the look point will be at the end, if it's moving in a straight line.
  74. local final = copy(other.pos)
  75. if other.mode == 2 then
  76. for i = 1, 3 do final[i] = other.pos[i] + other.delta[i] * other.dur end
  77. end
  78. local d1 = length({target[1] - final[1], target[2] - final[2], target[3] - final[3]})
  79. s.dist = d0
  80. s.dist_step = (d1 - d0) / n
  81. elseif op == 7 then
  82. self.finished = true
  83. end
  84. end
  85. --- Moves one of the points one tick.
  86. local function step_stream(s, other)
  87. if s.dur <= 0 then return end
  88. if s.mode == 2 then
  89. for i = 1, 3 do s.pos[i] = s.pos[i] + s.delta[i] end
  90. elseif s.mode == 3 then
  91. for i = 1, 3 do s.start[i] = s.start[i] + s.delta[i] end
  92. s.dist = s.dist + s.dist_step
  93. local dir = {other.pos[1] - s.start[1], other.pos[2] - s.start[2], other.pos[3] - s.start[3]}
  94. local l = length(dir)
  95. if l > 0 then
  96. for i = 1, 3 do s.pos[i] = other.pos[i] - dir[i] / l * s.dist end
  97. end
  98. end
  99. s.dur = s.dur - 1
  100. end
  101. --- Advances the camera one tick of the effect.
  102. function EffectCamera:tick()
  103. self.previous = {cam = copy(self.cam.pos), look = copy(self.look.pos)}
  104. local was_started = self.started
  105. while not self.finished and (self.cam.dur <= 0 or self.look.dur <= 0) do self:fetch() end
  106. -- The camera is moved first, with the look point of the previous tick.
  107. step_stream(self.cam, self.look)
  108. step_stream(self.look, self.cam)
  109. -- The first position doesn't move from nowhere.
  110. if self.started and not was_started then
  111. self.previous = {cam = copy(self.cam.pos), look = copy(self.look.pos)}
  112. end
  113. end
  114. --- Gets the position of the camera and the look point.
  115. --
  116. -- @param fraction How far (0 to 1) we are from the last tick to the next one, to move smoothly.
  117. -- @return Two tables with x, y, z in the units of the original game.
  118. function EffectCamera:get(fraction)
  119. local cam, look = {}, {}
  120. for i = 1, 3 do
  121. cam[i] = self.previous.cam[i] + (self.cam.pos[i] - self.previous.cam[i]) * fraction
  122. look[i] = self.previous.look[i] + (self.look.pos[i] - self.previous.look[i]) * fraction
  123. end
  124. return cam, look
  125. end
  126. --- Gets a camera script.
  127. --
  128. -- The scripts are read from ff7.exe by the installer (game/battle/effects/effect_cameras.lua), and they are
  129. -- named by their address in the executable, like "0x8ae268" (Shiva, one target). Without them
  130. -- (another version of the executable) the effects play with the camera of the battle.
  131. --
  132. -- @param name Name of the script.
  133. -- @return The list of words, nil if there is no such script.
  134. function EffectCamera.get_script(name)
  135. return Game ~= nil and Game.EffectCameras ~= nil and Game.EffectCameras[name] or nil
  136. end