BattleSceneFile.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * V-Gears
  3. * Copyright (C) 2022 V-Gears Team
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <iostream>
  19. #include "BattleSceneFile.h"
  20. #include "VGearsUtility.h"
  21. #include "FF7Data.h"
  22. BattleSceneFile::BattleSceneFile(const unsigned int id, File file): id_(id){Read(file);}
  23. BattleSceneFile::~BattleSceneFile(){}
  24. std::vector<Attack> BattleSceneFile::GetAttacks(){return attacks_;}
  25. std::vector<Enemy> BattleSceneFile::GetEnemies(){return enemies_;}
  26. std::vector<Formation> BattleSceneFile::GetFormations(){return formations_;}
  27. void BattleSceneFile::Read(File file){
  28. file.SetOffset(0);
  29. scene_.enemy[0] = file.readU16LE();
  30. scene_.enemy[1] = file.readU16LE();
  31. scene_.enemy[2] = file.readU16LE();
  32. scene_.unused = file.readU16LE();
  33. for (int s = 0; s < 4; s ++){
  34. scene_.setup[s].location = file.readU16LE();
  35. scene_.setup[s].next_battle = file.readU16LE();
  36. scene_.setup[s].escape_counter = file.readU16LE();
  37. scene_.setup[s].unused = file.readU16LE();
  38. for (int a = 0; a < 4; a ++) scene_.setup[s].next_arena_candidates[a] = file.readU16LE();
  39. scene_.setup[s].flags = file.readU16LE();
  40. scene_.setup[s].layout = file.readU8();
  41. scene_.setup[s].camera = file.readU8();
  42. }
  43. for (int c = 0; c < 4; c ++){
  44. for (int p = 0; p < 3; p ++){
  45. // The position of the camera and the point it looks at, signed 16 bit values in the units of
  46. // the original game. They are kept as they are, and converted when the formations are built.
  47. scene_.camera[c].camera[p].x = static_cast<int16_t>(file.readU16LE());
  48. scene_.camera[c].camera[p].y = static_cast<int16_t>(file.readU16LE());
  49. scene_.camera[c].camera[p].z = static_cast<int16_t>(file.readU16LE());
  50. scene_.camera[c].camera[p].d_x = static_cast<int16_t>(file.readU16LE());
  51. scene_.camera[c].camera[p].d_y = static_cast<int16_t>(file.readU16LE());
  52. scene_.camera[c].camera[p].d_z = static_cast<int16_t>(file.readU16LE());
  53. }
  54. for (int p = 0; p < 12; p ++) scene_.camera[c].unused[p] = file.readU8();
  55. }
  56. for (int f = 0; f < 4; f ++){
  57. for (int e = 0; e < 6; e ++){
  58. scene_.formation[f][e].id = file.readU16LE();
  59. // Enemy positions are stored as 16 bit floats. Here, 16 bit are being read, then, the
  60. // sign is checked to fit it into -32766 and 32767 (0x7F9b). Then, the number is
  61. // divided by 255 (0xFF) to keep just the integer part.
  62. scene_.formation[f][e].x = file.readU16LE();
  63. if (scene_.formation[f][e].x > 0x7F9B) scene_.formation[f][e].x -= 0xFFFF;
  64. scene_.formation[f][e].x /= 0xFF;
  65. // Y and Z are inverted in original data.
  66. scene_.formation[f][e].z = file.readU16LE();
  67. if (scene_.formation[f][e].z > 0x7F9B) scene_.formation[f][e].z -= 0xFFFF;
  68. scene_.formation[f][e].z /= 0xFF;
  69. scene_.formation[f][e].y = file.readU16LE();
  70. if (scene_.formation[f][e].y > 0x7F9B) scene_.formation[f][e].y -= 0xFFFF;
  71. scene_.formation[f][e].y /= 0xFF;
  72. scene_.formation[f][e].row = file.readU16LE();
  73. scene_.formation[f][e].cover_flags = file.readU16LE();
  74. scene_.formation[f][e].flags = file.readU32LE();
  75. }
  76. }
  77. for (int e = 0; e < 3; e ++){
  78. for (int n = 0; n < 32; n ++) scene_.enemy_data[e].name[n] = file.readU8();
  79. scene_.enemy_data[e].level = file.readU8();
  80. scene_.enemy_data[e].spd = file.readU8();
  81. scene_.enemy_data[e].lck = file.readU8();
  82. scene_.enemy_data[e].eva = file.readU8();
  83. scene_.enemy_data[e].str = file.readU8();
  84. scene_.enemy_data[e].def = file.readU8();
  85. scene_.enemy_data[e].mag = file.readU8();
  86. scene_.enemy_data[e].mdef = file.readU8();
  87. for (int l = 0; l < 8; l ++) scene_.enemy_data[e].elements[l] = file.readU8();
  88. for (int l = 0; l < 8; l ++) scene_.enemy_data[e].element_rates[l] = file.readU8();
  89. for (int a = 0; a < 16; a ++) scene_.enemy_data[e].animation_index[a] = file.readU8();
  90. for (int a = 0; a < 16; a ++) scene_.enemy_data[e].attacks[a] = file.readU16LE();
  91. for (int a = 0; a < 16; a ++) scene_.enemy_data[e].attack_camera[a] = file.readU16LE();
  92. for (int i = 0; i < 4; i ++) scene_.enemy_data[e].item_rate[i] = file.readU8();
  93. for (int i = 0; i < 4; i ++) scene_.enemy_data[e].item[i] = file.readU16LE();
  94. for (int m = 0; m < 3; m ++) scene_.enemy_data[e].maniputale_attacks[m] = file.readU16LE();
  95. scene_.enemy_data[e].unknown_0 = file.readU16LE();
  96. scene_.enemy_data[e].mp = file.readU16LE();
  97. scene_.enemy_data[e].ap = file.readU16LE();
  98. scene_.enemy_data[e].morph = file.readU16LE();
  99. scene_.enemy_data[e].back_damage = file.readU8();
  100. scene_.enemy_data[e].unused = file.readU8();
  101. scene_.enemy_data[e].hp = file.readU32LE();
  102. scene_.enemy_data[e].exp = file.readU32LE();
  103. scene_.enemy_data[e].money = file.readU32LE();
  104. scene_.enemy_data[e].immunities = file.readU32LE();
  105. scene_.enemy_data[e].unknown_1 = file.readU32LE();
  106. }
  107. for (int a = 0; a < 32; a ++){
  108. scene_.attack_data[a].accuracy = file.readU8();
  109. scene_.attack_data[a].impact_effect = file.readU8();
  110. scene_.attack_data[a].hurt_anim = file.readU8();
  111. scene_.attack_data[a].unknown = file.readU8();
  112. scene_.attack_data[a].mp = file.readU16LE();
  113. scene_.attack_data[a].sound = file.readU16LE();
  114. scene_.attack_data[a].camera_single = file.readU16LE();
  115. scene_.attack_data[a].camera_multiple = file.readU16LE();
  116. scene_.attack_data[a].target = file.readU8();
  117. scene_.attack_data[a].effect = file.readU8();
  118. scene_.attack_data[a].damage = file.readU8();
  119. scene_.attack_data[a].power = file.readU8();
  120. scene_.attack_data[a].condition = file.readU8();
  121. scene_.attack_data[a].status_change = file.readU8();
  122. scene_.attack_data[a].additional_effects = file.readU8();
  123. scene_.attack_data[a].additional_effects_mod = file.readU8();
  124. scene_.attack_data[a].status = file.readU32LE();
  125. scene_.attack_data[a].element = file.readU16LE();
  126. scene_.attack_data[a].special = file.readU16LE();
  127. }
  128. for (int a = 0; a < 32; a ++) scene_.attack_ids[a] = file.readU16LE();
  129. for (int a = 0; a < 32; a ++) for (int b = 0; b < 32; b ++) scene_.attack_names[a][b] = file.readU8();
  130. // AI scripts. The AI block is at a fixed offset, and starts with an offset for each of the 3
  131. // enemies of the scene (0xFFFF if the enemy has none). Each enemy block has 16 offsets
  132. // (0xFFFF for slots without a script) followed by the scripts, which end with the opcode 0x73.
  133. for (int e = 0; e < 3; e ++) ai_scripts_[e].assign(16, std::vector<u8>());
  134. {
  135. const u32 ai_base = 0x0E80;
  136. const u32 file_size = file.GetFileSize();
  137. if (file_size > ai_base + 6){
  138. std::vector<u8> bytes(file_size);
  139. file.GetFileBuffer(bytes.data(), 0, file_size);
  140. u16 block_offsets[3];
  141. for (int e = 0; e < 3; e ++) block_offsets[e] = bytes[ai_base + 2 * e] | (bytes[ai_base + 2 * e + 1] << 8);
  142. for (int e = 0; e < 3; e ++){
  143. if (block_offsets[e] == 0xFFFF) continue;
  144. const u32 block_start = ai_base + block_offsets[e];
  145. // The block ends where the next enemy block starts, or at the end of the file.
  146. u32 block_end = file_size;
  147. for (int n = e + 1; n < 3; n ++){
  148. if (block_offsets[n] != 0xFFFF){
  149. block_end = ai_base + block_offsets[n];
  150. break;
  151. }
  152. }
  153. if (block_start + 32 > file_size || block_end > file_size) continue;
  154. u16 entries[16];
  155. for (int k = 0; k < 16; k ++) entries[k] = bytes[block_start + 2 * k] | (bytes[block_start + 2 * k + 1] << 8);
  156. for (int k = 0; k < 16; k ++){
  157. if (entries[k] == 0xFFFF) continue;
  158. // A script ends where the next one starts, or at the end of the block.
  159. u32 script_end = block_end - block_start;
  160. for (int n = 0; n < 16; n ++){
  161. if (entries[n] != 0xFFFF && entries[n] > entries[k] && entries[n] < script_end) script_end = entries[n];
  162. }
  163. if (entries[k] >= script_end || block_start + script_end > file_size) continue;
  164. std::vector<u8> script(
  165. bytes.begin() + block_start + entries[k], bytes.begin() + block_start + script_end
  166. );
  167. // Padding at the end.
  168. while (!script.empty() && script.back() == 0xFF) script.pop_back();
  169. ai_scripts_[e][k] = script;
  170. }
  171. }
  172. }
  173. }
  174. // All the data is read, now format it.
  175. // Populate enemy list.
  176. for (int e = 0; e < 3; e ++){
  177. if (scene_.enemy[e] == 0xFFFF) continue;
  178. // If the enemy is already on the list, skip it.
  179. bool already_added = false;
  180. for (int t = 0; t < enemies_.size(); t ++){
  181. if (enemies_[t].id == scene_.enemy[e]){
  182. already_added = true;
  183. break;
  184. }
  185. }
  186. if (already_added) continue;
  187. // Create a new enemy
  188. Enemy enemy;
  189. enemy.id = scene_.enemy[e];
  190. enemy.model = FF7Data::GetEnemyModelFromEnemyId(enemy.id);
  191. enemy.name = VGears::Utility::DecodeString(scene_.enemy_data[e].name, 32);
  192. enemy.level = scene_.enemy_data[e].level;
  193. enemy.str = scene_.enemy_data[e].str;
  194. enemy.mag = scene_.enemy_data[e].mag;
  195. enemy.def = scene_.enemy_data[e].def;
  196. enemy.mdef = scene_.enemy_data[e].mdef;
  197. enemy.spd = scene_.enemy_data[e].spd;
  198. enemy.lck = scene_.enemy_data[e].lck;
  199. enemy.eva = scene_.enemy_data[e].eva;
  200. enemy.hp = scene_.enemy_data[e].hp;
  201. enemy.mp = scene_.enemy_data[e].mp;
  202. enemy.exp = scene_.enemy_data[e].exp;
  203. enemy.ap = scene_.enemy_data[e].ap;
  204. enemy.money = scene_.enemy_data[e].money;
  205. enemy.back_damage = scene_.enemy_data[e].back_damage / 8.0f;
  206. for (int a = 0; a < 16; a ++){
  207. if (scene_.enemy_data[e].animation_index[a] != 0xFF)
  208. enemy.animations.push_back(scene_.enemy_data[e].animation_index[a]);
  209. }
  210. // Attacks. Each of the 16 slots has an attack ID, the animation script of the model to play
  211. // and the camera to use.
  212. // The AI scripts of the enemy.
  213. enemy.ai_scripts = ai_scripts_[e];
  214. for (int a = 0; a < 16; a ++){
  215. if (scene_.enemy_data[e].attacks[a] == 0xFFFF) continue;
  216. Enemy::Attack attack;
  217. attack.id = scene_.enemy_data[e].attacks[a];
  218. attack.animation
  219. = (scene_.enemy_data[e].animation_index[a] != 0xFF)
  220. ? scene_.enemy_data[e].animation_index[a] : -1;
  221. attack.camera
  222. = (scene_.enemy_data[e].attack_camera[a] != 0xFFFF)
  223. ? scene_.enemy_data[e].attack_camera[a] : -1;
  224. enemy.attacks.push_back(attack);
  225. }
  226. for (int l = 0; l < 8; l ++){
  227. if (scene_.enemy_data[e].elements[l] != 0xFF && scene_.enemy_data[e].element_rates[l] != 0xFF ){
  228. Enemy::Element element;
  229. element.id = scene_.enemy_data[e].elements[l];
  230. switch (scene_.enemy_data[e].element_rates[l]){
  231. case 0x00: element.factor = 11.0f; break; // Death.
  232. case 0x02: element.factor = 2.0f; break; // Double damage.
  233. case 0x04: element.factor = 0.5f; break; // Half damage.
  234. case 0x05: element.factor = 0.0f; break; // Nullify;
  235. case 0x06: element.factor = -1.0f; break; // Absorb 100%;
  236. case 0x07: element.factor = -11.0f; break; // Full cure;
  237. default: element.factor = 1.0f; // Normal damage.
  238. }
  239. enemy.elements.push_back(element);
  240. }
  241. }
  242. // Immunities are a bitmask: the bit position is the status ID, as in the original game
  243. // (0 death, 1 near death, 2 sleep, 3 poison, 4 sadness, 5 fury, 6 confusion, 7 silence...).
  244. if (scene_.enemy_data[e].immunities != 0xFFFFFFFF){
  245. for (int bit = 0; bit < 32; bit ++){
  246. if ((scene_.enemy_data[e].immunities & (1u << bit)) == 0) continue;
  247. Enemy::Immunity immunity;
  248. immunity.status = bit;
  249. immunity.rate = 1.0f;
  250. enemy.immunities.push_back(immunity);
  251. }
  252. }
  253. for (int a = 0; a < 16; a ++){
  254. if (scene_.enemy_data[e].attacks[a] == 0xFFFF) continue;
  255. Enemy::Attack attack;
  256. attack.id = scene_.enemy_data[e].attacks[a];
  257. if (scene_.enemy_data[e].attack_camera[a] == 0xFFFF) attack.camera = -1;
  258. else attack.camera = scene_.enemy_data[e].attack_camera[a];
  259. enemy.attacks.push_back(attack);
  260. }
  261. for (int m = 0; m < 3; m ++){
  262. if (scene_.enemy_data[e].maniputale_attacks[m] == 0xFFFF) continue;
  263. enemy.manipulate_attacks.push_back(scene_.enemy_data[e].maniputale_attacks[m]);
  264. }
  265. for (int i = 0; i < 4; i ++){
  266. if (scene_.enemy_data[e].item[i] == 0xFF) continue;
  267. Enemy::Item item;
  268. item.id = scene_.enemy_data[e].item[i];
  269. if (scene_.enemy_data[e].item_rate[i] < 0x80){
  270. item.rate = scene_.enemy_data[e].item_rate[i] / 63.0f;
  271. enemy.drop.push_back(item);
  272. }
  273. else{
  274. item.rate = (scene_.enemy_data[e].item_rate[i] - 0x80) / 63.0f;
  275. enemy.steal.push_back(item);
  276. }
  277. }
  278. if (scene_.enemy_data[e].morph != 0xFFFF) enemy.morph = scene_.enemy_data[e].morph;
  279. enemies_.push_back(enemy);
  280. }
  281. // Populate attack list.
  282. for (int a = 0; a < 32; a ++){
  283. if (scene_.attack_ids[a] == 0xFFFF) continue;
  284. // If the attack is already on the list, skip it.
  285. bool already_added = false;
  286. for (int t = 0; t < attacks_.size(); t ++){
  287. if (attacks_[t].id == scene_.attack_ids[a]){
  288. already_added = true;
  289. break;
  290. }
  291. }
  292. if (already_added) continue;
  293. // Create a new attack.
  294. Attack attack;
  295. attack.id = scene_.attack_ids[a];
  296. attack.name = VGears::Utility::DecodeString(scene_.attack_names[a], 32);
  297. attack.damage.accuracy = scene_.attack_data[a].accuracy / 255.0f;
  298. if (scene_.attack_data[a].impact_effect != 0xFF)
  299. attack.animation.impact_effect = scene_.attack_data[a].impact_effect;
  300. if (scene_.attack_data[a].hurt_anim != 0xFF) attack.animation.hurt_anim = scene_.attack_data[a].hurt_anim;
  301. attack.mp = scene_.attack_data[a].mp;
  302. if (scene_.attack_data[a].sound != 0xFFFF) attack.animation.sound = scene_.attack_data[a].sound;
  303. if (scene_.attack_data[a].camera_single != 0xFFFF)
  304. attack.animation.camera_single = scene_.attack_data[a].camera_single;
  305. if (scene_.attack_data[a].camera_multiple != 0xFFFF)
  306. attack.animation.camera_multiple = scene_.attack_data[a].camera_multiple;
  307. attack.target.selection_enabled = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x01));
  308. attack.target.default_enemy = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x02));
  309. attack.target.default_multiple = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x04));
  310. attack.target.toggle_multiple = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x08));
  311. attack.target.fixed_row = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x10));
  312. attack.target.short_range = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x20));
  313. attack.target.all_rows = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x40));
  314. attack.target.random = (scene_.attack_data[a].target == (scene_.attack_data[a].target | 0x80));
  315. if (scene_.attack_data[a].effect != 0xFFFF) attack.animation.effect = scene_.attack_data[a].effect;
  316. attack.damage.formula = scene_.attack_data[a].damage >> 4;
  317. attack.damage.modifier = scene_.attack_data[a].damage & 15;
  318. attack.damage.power = scene_.attack_data[a].power;
  319. switch(scene_.attack_data[a].condition){
  320. case 0: attack.damage.restore = GameData::RestoreType::HP; break;
  321. case 1: attack.damage.restore = GameData::RestoreType::MP; break;
  322. case 2: attack.damage.restore = GameData::RestoreType::STATUS; break;
  323. default: attack.damage.restore = GameData::RestoreType::NONE;
  324. }
  325. // Status attack byte: the lower 6 bits are the chance out of 63, and the upper 2 bits the
  326. // mode (0x80 toggles the statuses, 0x40 cures them, otherwise they are inflicted).
  327. // The status bitmask has the status IDs as bit positions, as in the original game.
  328. const int status_chance_raw = scene_.attack_data[a].status_change & 0x3F;
  329. int status_mode = GameData::STATUS_EFFECT_MODE::INFLICT;
  330. if ((scene_.attack_data[a].status_change & 0x80) != 0) status_mode = GameData::STATUS_EFFECT_MODE::TOGGLE;
  331. else if ((scene_.attack_data[a].status_change & 0x40) != 0) status_mode = GameData::STATUS_EFFECT_MODE::CURE;
  332. if (scene_.attack_data[a].status != 0xFFFFFFFF){
  333. const float status_chance = status_chance_raw / 63.0f;
  334. for (int bit = 0; bit < 32; bit ++){
  335. if ((scene_.attack_data[a].status & (1u << bit)) == 0) continue;
  336. Attack::StatusEffect effect;
  337. effect.id = bit;
  338. effect.chance = status_chance;
  339. effect.type = status_mode;
  340. attack.status_effects.push_back(effect);
  341. }
  342. }
  343. // Attack elements are a bitmask (0x0001 is fire, 0x0002 ice...), while enemies elements are
  344. // indexes (0 is fire, 1 is ice...). Elements are stored as indexes.
  345. if (scene_.attack_data[a].element != 0xFFFF){
  346. for (int bit = 0; bit < 16; bit ++){
  347. if ((scene_.attack_data[a].element & (1 << bit)) != 0) attack.elements.push_back(bit);
  348. }
  349. }
  350. attack.damage.damage_mp = (scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0001));
  351. attack.damage.affected_by_darkness = (scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0004));
  352. if ((scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0010))){
  353. if (attack.damage.damage_mp) attack.damage.drain_mp = true;
  354. else attack.damage.drain_hp = true;
  355. }
  356. if ((scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0020))){
  357. attack.damage.drain_hp = true;
  358. attack.damage.drain_mp = true;
  359. }
  360. attack.damage.ignore_status_defense
  361. = (scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0080));
  362. attack.target.only_for_dead = (scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0100));
  363. attack.reflectable = (scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0200));
  364. attack.target.no_re_target = (scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x0800));
  365. attack.damage.critical = (scene_.attack_data[a].special != (scene_.attack_data[a].special | 0x2000));
  366. // TODO: Parse scene_.attack_data[a].additional_effects with
  367. // scene_.attack_data[a].additional_effects_mod for special attack modifiers (23 total).
  368. // https://wiki.ffrtt.ru/index.php/FF7/Battle/Attack_Special_Effects
  369. //std::cout << "ATTACK " << ((int) attack.id) << ": " << attack.name << "\n";
  370. attacks_.push_back(attack);
  371. }
  372. // Formations
  373. for (int f = 0; f < 4; f ++){
  374. Formation formation;
  375. if (scene_.setup[f].next_battle != 0xFFFF) formation.next_formation = scene_.setup[f].next_battle;
  376. formation.escape_counter = (scene_.setup[f].escape_counter / 64.0f);
  377. // Observed in the original scene.bin (1024 setups): the upper byte is always 0xFF and
  378. // bit 0x02 is clear in most random battles, so it means "can't escape" (inverted).
  379. // TODO: What is flag 0x01?
  380. formation.escapable = ((scene_.setup[f].flags & 0x02) == 0);
  381. // TODO: Bits 0x04, 0x08 and 0x10 are set in almost every setup, so they can't be read as
  382. // positive "skip victory pose", "skip spoils" and "preemptive disabled" flags (the previous
  383. // code, due to operator precedence, always set all three). Use defaults until their
  384. // meaning is verified against known battles.
  385. formation.skip_victory_pose = false;
  386. formation.skip_spoils = false;
  387. formation.preemptive_disabled = false;
  388. // TODO: What is flag 0x20?
  389. // TODO: What is flag 0x40?
  390. // TODO: What is flag 0x80?
  391. switch(scene_.setup[f].layout){
  392. case 0x0: formation.layout = Formation::Layout::NORMAL; break;
  393. case 0x1: formation.layout = Formation::Layout::PREEMPTIVE; break;
  394. case 0x2: formation.layout = Formation::Layout::BACK_ATTACK; break;
  395. case 0x3: formation.layout = Formation::Layout::SIDE_ATTACK; break;
  396. case 0x4: formation.layout = Formation::Layout::PINCER_ATTACK; break;
  397. case 0x5: formation.layout = Formation::Layout::PINCER_ATTACK; break;
  398. case 0x6: formation.layout = Formation::Layout::SIDE_ATTACK; break;
  399. case 0x7: formation.layout = Formation::Layout::SIDE_ATTACK; break;
  400. case 0x8: formation.layout = Formation::Layout::LOCKED; break;
  401. default: formation.layout = Formation::Layout::NORMAL;
  402. }
  403. for (int e = 0; e < 6; e ++){
  404. if (scene_.formation[f][e].id != 0xFFFF){
  405. Formation::Enemy enemy;
  406. enemy.id = scene_.formation[f][e].id;
  407. enemy.x = scene_.formation[f][e].x;
  408. enemy.y = scene_.formation[f][e].y;
  409. enemy.z = scene_.formation[f][e].z;
  410. enemy.row = scene_.formation[f][e].row;
  411. enemy.cover[0] = (
  412. scene_.formation[f][e].cover_flags == (scene_.formation[f][e].cover_flags | 0x01) ? '1' : '0'
  413. );
  414. enemy.cover[1] = (
  415. scene_.formation[f][e].cover_flags == (scene_.formation[f][e].cover_flags | 0x02) ? '1' : '0'
  416. );
  417. enemy.cover[2] = (
  418. scene_.formation[f][e].cover_flags == (scene_.formation[f][e].cover_flags | 0x04) ? '1' : '0'
  419. );
  420. enemy.cover[3] = (
  421. scene_.formation[f][e].cover_flags == (scene_.formation[f][e].cover_flags | 0x08) ? '1' : '0'
  422. );
  423. enemy.cover[4] = (
  424. scene_.formation[f][e].cover_flags == (scene_.formation[f][e].cover_flags | 0x10) ? '1' : '0'
  425. );
  426. enemy.visible = (scene_.formation[f][e].flags == scene_.formation[f][e].flags | 0x01);
  427. enemy.direction = ((scene_.formation[f][e].flags == scene_.formation[f][e].flags | 0x02) ? 1 : 0);
  428. enemy.targeteable = (scene_.formation[f][e].flags == scene_.formation[f][e].flags | 0x08);
  429. enemy.main_script_active = (scene_.formation[f][e].flags == scene_.formation[f][e].flags | 0x10);
  430. formation.enemies.push_back(enemy);
  431. }
  432. }
  433. formation.location = scene_.setup[f].location;
  434. formation.location_name = LocationToString(scene_.setup[f].location);
  435. for (int c = 0; c < 3; c ++){
  436. Formation::Camera camera;
  437. // The original data has x to the side, y down and z in depth (the enemies are in the negative
  438. // side). The scene has the same x, the depth in y and z up, in units 255 times bigger, like the
  439. // positions of the enemies.
  440. const BattleSceneFile::Scene::Camera::Position& source = scene_.camera[f].camera[c];
  441. camera.x = source.x / 255.0f;
  442. camera.y = source.z / 255.0f;
  443. camera.z = -source.y / 255.0f;
  444. camera.d_x = source.d_x / 255.0f;
  445. camera.d_y = source.d_z / 255.0f;
  446. camera.d_z = -source.d_y / 255.0f;
  447. formation.camera_positions.push_back(camera);
  448. }
  449. // The camera byte of the formation is the script of the camera at the start of the battle, which
  450. // ends at the first camera of the formation unless it chooses another one.
  451. formation.intro_camera = scene_.setup[f].camera;
  452. for (int a = 0; a < 4; a ++){
  453. if (scene_.setup[f].next_arena_candidates[a] != 999){
  454. formation.next_arena_formation_candidates.push_back(scene_.setup[f].next_arena_candidates[a]);
  455. formation.is_arena_battle = true;
  456. }
  457. }
  458. formations_.push_back(formation);
  459. }
  460. }
  461. std::string BattleSceneFile::LocationToString(const int id){
  462. switch(id){
  463. case 0x0000: return "blank";
  464. case 0x0001: return "bizarro_battle_center";
  465. case 0x0002: return "grassland";
  466. case 0x0003: return "mt_nibel";
  467. case 0x0004: return "forest";
  468. case 0x0005: return "beach";
  469. case 0x0006: return "desert";
  470. case 0x0007: return "snow";
  471. case 0x0008: return "swamp";
  472. case 0x0009: return "sector_1_train_station";
  473. case 0x000a: return "reactor_1";
  474. case 0x000b: return "reactor_1_core";
  475. case 0x000c: return "reactor_1_entrance";
  476. case 0x000d: return "sector_4_subway";
  477. case 0x000e: return "nibel_caves";
  478. case 0x000f: return "shinra_hq";
  479. case 0x0010: return "midgar_raid_subway";
  480. case 0x0011: return "hojo_lab";
  481. case 0x0012: return "shinra_elevators";
  482. case 0x0013: return "shinra_roof";
  483. case 0x0014: return "midgar_highway";
  484. case 0x0015: return "wutai_pagoda";
  485. case 0x0016: return "church";
  486. case 0x0017: return "coral_valley";
  487. case 0x0018: return "midgar_slums";
  488. case 0x0019: return "sector_4_corridors";
  489. case 0x001a: return "sector_4_gantries";
  490. case 0x001b: return "sector_7_pillar_stairway";
  491. case 0x001c: return "sector_7pillar_top";
  492. case 0x001d: return "sector_8";
  493. case 0x001e: return "sewers";
  494. case 0x001f: return "mythril_mines";
  495. case 0x0020: return "crater_floating_platforms";
  496. case 0x0021: return "corel_mountain_path";
  497. case 0x0022: return "junon_beach";
  498. case 0x0023: return "junon_cargo_ship";
  499. case 0x0024: return "corel_prison";
  500. case 0x0025: return "battle_square";
  501. case 0x0026: return "da_chao_rapps_battle";
  502. case 0x0027: return "cids_backyard";
  503. case 0x0028: return "final_descent_to_sephiroth";
  504. case 0x0029: return "reactor_5_entrance";
  505. case 0x002a: return "ancients_escher_room";
  506. case 0x002b: return "shinra_mansion";
  507. case 0x002c: return "junon_airship_dock";
  508. case 0x002d: return "whirlwind_maze";
  509. case 0x002e: return "junon_underwater_reactor";
  510. case 0x002f: return "gongaga_reactor";
  511. case 0x0030: return "gelnika";
  512. case 0x0031: return "train_graveyard";
  513. case 0x0032: return "ice_caves";
  514. case 0x0033: return "sister_ray";
  515. case 0x0034: return "sister_ray_base";
  516. case 0x0035: return "forgotten_city_altar";
  517. case 0x0036: return "crater_descent";
  518. case 0x0037: return "crater_hatchery";
  519. case 0x0038: return "crater_water_area";
  520. case 0x0039: return "safer_battle";
  521. case 0x003a: return "kalm_flashback_dragon";
  522. case 0x003b: return "junon_pipe";
  523. case 0x003c: return "blank";
  524. case 0x003d: return "railway_canyon";
  525. case 0x003e: return "whirlwind_maze_crater";
  526. case 0x003f: return "railway_rollercoaster";
  527. case 0x0040: return "wooden_bridge";
  528. case 0x0041: return "da_chao";
  529. case 0x0042: return "fort_condor";
  530. case 0x0043: return "dirt_wasteland";
  531. case 0x0044: return "bizarro_battle_right";
  532. case 0x0045: return "bizarro_battle_left";
  533. case 0x0046: return "jenova_synthesis";
  534. case 0x0047: return "corel_train_battle";
  535. case 0x0048: return "cosmo_canyon";
  536. case 0x0049: return "caverns_of_the_gi";
  537. case 0x004a: return "nibelheim_mansion_basement";
  538. case 0x004b: return "ancients_demons_gate";
  539. case 0x004c: return "ancients_mural_room";
  540. case 0x004d: return "ancients_clock_passage";
  541. case 0x004e: return "final_battle";
  542. case 0x004f: return "jungle";
  543. case 0x0050: return "ultimate_weapon_highwind";
  544. case 0x0051: return "corel_reactor";
  545. case 0x0052: return "unused";
  546. case 0x0053: return "don_corneos_mansion";
  547. case 0x0054: return "emerald_weapon_battle";
  548. case 0x0055: return "reactor_5";
  549. case 0x0056: return "shinra_hq_-_escape";
  550. case 0x0057: return "ultimate_weapon_gongaga";
  551. case 0x0058: return "corel_prison_dyne";
  552. case 0x0059: return "ultimate_weapon_forest";
  553. default: return "unknonw_location";
  554. }
  555. }