battle.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. Battle = {}
  2. Battle.Action = {
  3. MONSTER_ACTION = 32,
  4. }
  5. Battle.Row = {
  6. BACK = 0,
  7. FRONT = 1,
  8. }
  9. Battle.Type = {
  10. ENEMY = 0,
  11. ALLY = 1,
  12. LOGIC = 2,
  13. }
  14. --- Menus that must be shown when selecting a command.
  15. Battle.Menu = {
  16. --- No menu, execute command directly, without target selection.
  17. NONE = 0,
  18. --- Show magic menu.
  19. MAGIC = 1,
  20. --- Show summon menu.
  21. SUMMON = 2,
  22. --- Show item menu.
  23. ITEM = 3,
  24. --- Show enemy skill menu.
  25. E_SKILL = 4,
  26. --- Show throw menu.
  27. THROW = 5,
  28. --- Show limit menu.
  29. LIMIT = 6,
  30. --- No menu, skip to target selection.
  31. TARGET = 7,
  32. --- Show W-Magic menu
  33. W_MAGIC = 8,
  34. --- Show W-Summon menu
  35. W_SUMMON = 9,
  36. --- Show W-Item menu
  37. W_ITEM = 10,
  38. --- Show Coin menu
  39. COIN = 11,
  40. }
  41. --- Target modes
  42. Battle.TargetType = {
  43. --- Single target
  44. SINGLE = 0,
  45. --- All targets in a battle side
  46. GROUP = 1
  47. }
  48. --- Target modes
  49. Battle.TargetGroup = {
  50. --- Single target
  51. PARTY = 0,
  52. --- All targets in a battle side
  53. ENEMIES = 1
  54. }
  55. --- Action types
  56. Battle.ActionType = {
  57. --- A command
  58. --
  59. -- An action of this type will be searched in the Commands array.
  60. COMMAND = 0,
  61. --- Anything that is not a command
  62. --
  63. -- -- An action of this type will be searched in the Attacks array.
  64. ATTACK = 1
  65. }
  66. Battle.ActionDefaults = {
  67. [ Battle.Action.MONSTER_ACTION ] = { animation_script = "0" }
  68. }
  69. Battle.AttackHitChance = {
  70. PhysicalCritical = 1, -- 0x01 upper 4 bits of formula
  71. }
  72. Battle.Damage = {
  73. Physical = 1, -- 0x01 lower 4 bits of formula
  74. }
  75. --- Is the ATB time stopped by the menus the player has open?
  76. --
  77. -- Depends on the ATB setting of the config menu (MenuSettings.config.atb):
  78. -- * 1, active: time never stops.
  79. -- * 2, recommended: time stops while a list (magic, summon, item) is open.
  80. -- * 3, wait: time stops while a list is open and while a target is being chosen.
  81. Battle.is_time_stopped = function()
  82. local mode = (MenuSettings.config ~= nil) and MenuSettings.config.atb or 2
  83. local ui = UiContainer.BattleUi
  84. if mode == 1 or ui == nil then return false end
  85. local state = ui.window_state
  86. if mode == 2 then return state == "list" end
  87. return state == "list" or state == "target"
  88. end
  89. --- Can the battle camera move by itself? (Camera angle: Auto in the config menu.)
  90. --
  91. -- TODO: The battle camera doesn't move during the battle yet. When it does, the movements of the
  92. -- camera (attacks, magic, summons) must only happen if this returns true.
  93. Battle.is_camera_auto = function()
  94. return MenuSettings.config == nil or MenuSettings.config.camera ~= 2
  95. end
  96. Battle.logic_script = {
  97. unit_type = Battle.Type.LOGIC,
  98. battle_speed = 0,
  99. game_timer = 0,
  100. enemy_number = 0,
  101. ally_number = 0,
  102. command_queue = {},
  103. temp_data = {
  104. attacker = nil, -- 0x00
  105. -- 0x04 attacker level. We don't this. Instead we use direct data from copied attacker (0x00)
  106. -- 0x10 attack index. We don't this. Instead we use direct data from copied attack (0x2c)
  107. target = nil, -- 0x18
  108. attack = nil, -- 0x2c
  109. -- 0x40 formula. We don't this. Instead we use direct data from copied attack (0x2c)
  110. -- 0x48 power modifier. We don't this. Instead we use direct data from copied attack (0x2c)
  111. base_damage = 0, -- 0x4c
  112. -- 0xa0 upper formula. We don't this. Instead we use direct data from copied attack (0x2c)
  113. -- 0xa4 lower formula. We don't this. Instead we use direct data from copied attack (0x2c)
  114. calculated_defense = 0, -- 0x210
  115. calculated_damage = 0, -- 0x214
  116. },
  117. on_update = function( self )
  118. local timer = timer:get_game_time_total()
  119. local delta = timer - self.game_timer
  120. -- The timers don't advance while the menus stop the ATB time.
  121. if Battle.is_time_stopped() then delta = 0 end
  122. -- update timers
  123. for key, value in pairs( EntityContainer ) do
  124. if value.unit_type ~= Battle.Type.LOGIC then
  125. if value.timer < 65535 then
  126. value.timer = value.timer + delta * 30 * value.battle_speed
  127. --print( tostring( key ) .. ".timer " .. tostring( value.timer ) )
  128. if value.timer > 65535 then
  129. print( tostring( key ) .. " performs script \"on_action\"" )
  130. script:request( Script.ENTITY, key, "on_action", 2 )
  131. end
  132. end
  133. end
  134. end
  135. self.game_timer = timer
  136. -- update command queue
  137. for key_p, value_p in pairs( self.command_queue ) do
  138. for key_c, value_c in pairs( value_p ) do
  139. Battle.run_command( self, value_c )
  140. value_c.self.timer = 0
  141. table.remove( value_p, key_c )
  142. end
  143. end
  144. return 0
  145. end,
  146. }
  147. --- Flags set by the field opcode BTLMD (battle_manager:set_mode).
  148. Battle.Mode = {
  149. NO_RESULTS = 0x80,
  150. ARENA = 0x40,
  151. NO_VICTORY_MUSIC = 0x20,
  152. NO_ESCAPE = 0x08,
  153. PREEMPTIVE = 0x04,
  154. TIMED = 0x02,
  155. NO_GAME_OVER = 0x100,
  156. }
  157. --- Checks if a BTLMD flag is set for the current battle.
  158. Battle.has_mode = function(flag)
  159. return math.floor(battle_manager:get_mode() / flag) % 2 == 1
  160. end
  161. --- Starts a battle.
  162. --
  163. -- @param id Battle ID.
  164. Battle.start = function(id)
  165. Battle.pending_id = id
  166. script:request_end_sync(Script.UI, "Fade", "battle_transition", 0)
  167. return 0
  168. end
  169. --- Starts a battle right away, with no transition. It can be called from the battle tick, so it
  170. -- doesn't yield. Used for battles that follow another battle.
  171. --
  172. -- @param id Battle ID.
  173. Battle.chain = function(id)
  174. battle_manager:start_battle(id)
  175. script:request(Script.UI, "BattleUi", "show", 0)
  176. end
  177. --- Random encounters.
  178. Encounter = {
  179. --- Indicates that the current battle is a random encounter.
  180. active = false,
  181. }
  182. --- Starts a random encounter.
  183. --
  184. -- Called by the engine when the player walks into one. It isn't a script, so it can't yield.
  185. -- @param id Battle ID.
  186. Encounter.start = function(id)
  187. -- The player can't walk during the battle.
  188. player_lock(true)
  189. Encounter.active = true
  190. Battle.pending_id = id
  191. script:request(Script.UI, "Fade", "battle_transition", 0)
  192. end
  193. --- Ends a battle.
  194. Battle.finish = function()
  195. battle_manager:end_battle()
  196. -- Random encounters give control back to the player.
  197. if Encounter.active then
  198. Encounter.active = false
  199. player_lock(false)
  200. elseif Battle.locked_by_transition then
  201. player_lock(false)
  202. end
  203. Battle.locked_by_transition = false
  204. -- Not "request_end_sync": this can be called from the battle tick, which can't yield.
  205. script:request(Script.UI, "BattleUi", "hide", 0)
  206. return 0
  207. end
  208. Battle.init = function()
  209. EntityContainer.BattleLogic = Battle.logic_script
  210. entity_manager:add_entity_script( "BattleLogic" )
  211. -- load players
  212. EntityContainer.Cloud = Characters.Cloud
  213. entity_manager:add_entity( "Cloud", "models/ffvii/battle/units/first_ray.mesh", 0, 2, 0, 0 -1)
  214. -- init timer with start value
  215. EntityContainer.BattleLogic.game_timer = timer:get_game_time_total()
  216. -- init battle speed calculated from menu settings
  217. EntityContainer.BattleLogic.battle_speed = 65536 / ( ( ( MenuSettings.battle_speed * 480 / 256 ) + 120 ) * 2 )
  218. local party_speed = 0
  219. for key, value in pairs( EntityContainer ) do
  220. if value.unit_type == Battle.Type.ALLY then
  221. EntityContainer.BattleLogic.ally_number = EntityContainer.BattleLogic.ally_number + 1
  222. party_speed = party_speed + value.dexterity
  223. value.speed = value.dexterity + 50
  224. elseif value.unit_type == Battle.Type.ENEMY then
  225. EntityContainer.BattleLogic.enemy_number = EntityContainer.BattleLogic.enemy_number + 1
  226. end
  227. end
  228. print( "ally_number = " .. tostring( EntityContainer.BattleLogic.ally_number ) )
  229. print( "enemy_number = " .. tostring( EntityContainer.BattleLogic.enemy_number ) )
  230. if EntityContainer.BattleLogic.ally_number > 0 then
  231. party_speed = ( party_speed - 1 + EntityContainer.BattleLogic.ally_number ) / EntityContainer.BattleLogic.ally_number + 50;
  232. for key, value in pairs( EntityContainer ) do
  233. if value.unit_type ~= Battle.Type.LOGIC then
  234. value.battle_speed = EntityContainer.BattleLogic.battle_speed * value.speed / party_speed
  235. value.battle_speed = value.battle_speed * 2 -- normal speed (initial value calculated for slow)
  236. end
  237. end
  238. end
  239. end
  240. Battle.run_command = function( self, command )
  241. print( "Command: attack = \"" .. command.attack.name .. "\", target = \"" .. tostring( command.target[ 1 ] ) .. "\"" )
  242. -- todo a lot of preparation
  243. EntityContainer.BattleLogic.temp_data.attacker = command.self
  244. EntityContainer.BattleLogic.temp_data.attack = command.attack
  245. EntityContainer.BattleLogic.temp_data.target = command.target
  246. -- todo a lot of preparation
  247. if command.action == Battle.Action.MONSTER_ACTION then
  248. -- todo action_type_07
  249. -- todo action_type_0C
  250. Battle.calculate_and_apply_damage() -- action_type_09
  251. end
  252. end
  253. -- action_type_09
  254. Battle.calculate_and_apply_damage = function()
  255. -- todo mp check
  256. -- todo a lot of fuctions
  257. Battle.set_base_damage()
  258. -- todo a lot of fuctions
  259. -- attack every unit in target
  260. for key, value in pairs( EntityContainer.BattleLogic.temp_data.target ) do
  261. Battle.main_damage_calculation( value )
  262. end
  263. -- todo a lot of functions
  264. end
  265. Battle.set_base_damage = function()
  266. -- todo force physical attack settings in physical formula
  267. -- todo use special settings for special attacks
  268. -- todo use another base damage for magic attack
  269. EntityContainer.BattleLogic.temp_data.base_damage = EntityContainer.BattleLogic.temp_data.attacker.physical_power
  270. -- todo apply damage modifier
  271. end
  272. Battle.main_damage_calculation = function( target )
  273. -- todo a lot of fuctions
  274. Battle.calculate_target_stats( target )
  275. -- todo a lot of fuctions
  276. Battle.damage_formula_run()
  277. -- todo a lot of fuctions
  278. end
  279. Battle.calculate_target_stats = function( target )
  280. -- todo a lot of initialization
  281. -- todo a lot of initialization
  282. -- todo defense check and modification
  283. EntityContainer.BattleLogic.temp_data.calculated_defense = target.physical_defense
  284. -- todo a lot of initialization
  285. if EntityContainer.BattleLogic.temp_data.calculated_defense > 512 then
  286. EntityContainer.BattleLogic.temp_data.calculated_defense = 512
  287. end
  288. -- todo sadness check
  289. end
  290. Battle.damage_formula_run = function()
  291. -- run high formula
  292. if EntityContainer.BattleLogic.temp_data.attack.hit_chance_formula == Battle.AttackHitChance.PhysicalCritical then
  293. Battle.HighFormula.Physical()
  294. Battle.HighFormula.Critical()
  295. end
  296. -- run low formula
  297. if EntityContainer.BattleLogic.temp_data.attack.power_modifier ~= 0 then
  298. -- todo add additional conditions for special attacks (A0 = 0;funca8e84;)
  299. if EntityContainer.BattleLogic.temp_data.attack.damage_formula == Battle.Damage.Physical then
  300. Battle.LowFormula.Physical()
  301. end
  302. -- todo add additional conditions for special attacks (A0 = 1;funca8e84;)
  303. end
  304. end
  305. Battle.HighFormula = {}
  306. Battle.HighFormula.Physical = function()
  307. -- todo add physical hit
  308. end
  309. Battle.HighFormula.Critical = function()
  310. -- todo add critical hit
  311. end
  312. Battle.LowFormula = {}
  313. Battle.LowFormula.Physical = function()
  314. local damage = EntityContainer.BattleLogic.temp_data.base_damage
  315. -- calculate base damage
  316. damage = damage + ( ( EntityContainer.BattleLogic.temp_data.attacker.level + damage ) / 32 ) * ( ( EntityContainer.BattleLogic.temp_data.attacker.level * damage ) / 32 )
  317. -- apply attack data power
  318. damage = damage * EntityContainer.BattleLogic.temp_data.attack.power
  319. -- apply defense
  320. damage = damage * ( 512 - EntityContainer.BattleLogic.temp_data.calculated_defense ) / 512
  321. -- todo add always critical check
  322. -- todo critical damage
  323. -- todo berserk modificator
  324. -- todo back row modificator
  325. -- todo target defend
  326. -- todo attack from back
  327. -- todo a lot of status modifiers
  328. damage = Battle.add_random_modifier_and_zero_check( damage )
  329. EntityContainer.BattleLogic.temp_data.calculated_damage = damage
  330. print( "Command: damage = \"" .. damage .. "\"." )
  331. end
  332. Battle.add_random_modifier_and_zero_check = function( damage )
  333. local random_damage = damage * ( 15 + math.random() ) / 16;
  334. if random_damage == 0 then
  335. random_damage = 1
  336. end
  337. return random_damage
  338. end
  339. --- Opcodes of the world map scripts that deal with battles.
  340. SetBattleLock = function(locked)
  341. world_map_module:set_encounters_locked(locked ~= 0)
  342. end
  343. TriggerBattle = function(id)
  344. Encounter.start(id)
  345. end