BattleScene.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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. #include <memory>
  16. #include <boost/test/unit_test.hpp>
  17. #include "installer/formats/BattleSceneConverter.h"
  18. #include "installer/formats/BattleSceneReader.h"
  19. namespace{
  20. /**
  21. * A scene with no enemies and no attacks. It's too big for the stack.
  22. */
  23. std::unique_ptr<BattleSceneData> EmptyScene(){
  24. auto scene = std::make_unique<BattleSceneData>();
  25. for (u16& enemy : scene->enemy) enemy = 0xFFFF;
  26. for (u16& id : scene->attack_ids) id = 0xFFFF;
  27. return scene;
  28. }
  29. }
  30. BOOST_AUTO_TEST_CASE(TestBattleSceneValues){
  31. BOOST_CHECK_EQUAL(BattleSceneConverter::ElementFactor(0x02), 2.0f);
  32. BOOST_CHECK_EQUAL(BattleSceneConverter::ElementFactor(0x06), -1.0f);
  33. BOOST_CHECK_EQUAL(BattleSceneConverter::ElementFactor(0x55), 1.0f);
  34. BOOST_CHECK(BattleSceneConverter::Layout(5) == Formation::Layout::PINCER_ATTACK);
  35. BOOST_CHECK(BattleSceneConverter::Layout(8) == Formation::Layout::LOCKED);
  36. BOOST_CHECK(BattleSceneConverter::Layout(9) == Formation::Layout::NORMAL);
  37. BOOST_CHECK_EQUAL(BattleSceneConverter::LocationName(2), "grassland");
  38. BOOST_CHECK_EQUAL(BattleSceneConverter::LocationName(0x59), "ultimate_weapon_forest");
  39. BOOST_CHECK_EQUAL(BattleSceneConverter::LocationName(0x5A), "unknonw_location");
  40. }
  41. BOOST_AUTO_TEST_CASE(TestBattleSceneAttacks){
  42. auto scene = EmptyScene();
  43. // The same attack in two slots is listed once.
  44. scene->attack_ids[0] = scene->attack_ids[1] = 7;
  45. scene->attack_data[0].effect = 0xFF;
  46. scene->attack_data[0].impact_effect = 3;
  47. scene->attack_data[0].status = 0x00000005;
  48. scene->attack_data[0].status_change = 0x80 | 63;
  49. scene->attack_data[0].element = 0x0003;
  50. // The special properties are on when their bits are not set.
  51. scene->attack_data[0].special = 0xFFFE;
  52. const std::vector<Attack> attacks = BattleSceneConverter::Attacks(*scene);
  53. BOOST_REQUIRE_EQUAL(attacks.size(), 1u);
  54. BOOST_CHECK_EQUAL(attacks[0].animation.effect, -1);
  55. BOOST_CHECK_EQUAL(attacks[0].animation.impact_effect, 3);
  56. BOOST_REQUIRE_EQUAL(attacks[0].status_effects.size(), 2u);
  57. BOOST_CHECK_EQUAL(attacks[0].status_effects[1].id, 2);
  58. BOOST_CHECK_EQUAL(attacks[0].status_effects[1].chance, 1.0f);
  59. // A copy of the constant, which has no definition to take the address of.
  60. const int toggle = GameData::STATUS_EFFECT_MODE::TOGGLE;
  61. BOOST_CHECK_EQUAL(attacks[0].status_effects[1].type, toggle);
  62. BOOST_CHECK_EQUAL(attacks[0].elements.size(), 2u);
  63. BOOST_CHECK(attacks[0].damage.damage_mp);
  64. BOOST_CHECK(!attacks[0].reflectable);
  65. }
  66. BOOST_AUTO_TEST_CASE(TestBattleSceneEnemyItems){
  67. auto scene = EmptyScene();
  68. scene->enemy[0] = 0;
  69. BattleSceneData::EnemyData& data = scene->enemy_data[0];
  70. for (u16& attack : data.attacks) attack = 0xFFFF;
  71. // An armor to drop, nothing, a weapon to steal and nothing.
  72. const u16 items[4] = {297, 0xFFFF, 255, 0xFFFF};
  73. const u8 rates[4] = {63, 0, 0x80 + 63, 0};
  74. std::copy(items, items + 4, data.item);
  75. std::copy(rates, rates + 4, data.item_rate);
  76. const std::vector<Enemy> enemies = BattleSceneConverter::Enemies(*scene);
  77. BOOST_REQUIRE_EQUAL(enemies.size(), 1u);
  78. BOOST_REQUIRE_EQUAL(enemies[0].drop.size(), 1u);
  79. BOOST_CHECK_EQUAL(enemies[0].drop[0].id, 297u);
  80. BOOST_CHECK_EQUAL(enemies[0].drop[0].rate, 1.0f);
  81. BOOST_REQUIRE_EQUAL(enemies[0].steal.size(), 1u);
  82. BOOST_CHECK_EQUAL(enemies[0].steal[0].id, 255u);
  83. }
  84. BOOST_AUTO_TEST_CASE(TestBattleSceneFormations){
  85. auto scene = EmptyScene();
  86. for (auto& formation : scene->formation) for (auto& enemy : formation) enemy.id = 0xFFFF;
  87. for (auto& setup : scene->setup){
  88. for (u16& candidate : setup.next_arena_candidates) candidate = 999;
  89. }
  90. scene->formation[1][0].id = 5;
  91. scene->formation[1][0].cover_flags = 0x11;
  92. scene->setup[1].next_arena_candidates[2] = 30;
  93. const std::vector<Formation> formations = BattleSceneConverter::Formations(*scene);
  94. BOOST_REQUIRE_EQUAL(formations.size(), 4u);
  95. BOOST_CHECK(formations[0].enemies.empty());
  96. BOOST_CHECK(!formations[0].is_arena_battle);
  97. BOOST_CHECK(formations[1].is_arena_battle);
  98. BOOST_REQUIRE_EQUAL(formations[1].enemies.size(), 1u);
  99. BOOST_CHECK_EQUAL(std::string(formations[1].enemies[0].cover, 5), "10001");
  100. }
  101. BOOST_AUTO_TEST_CASE(TestBattleSceneFormationEnemyFlags){
  102. auto scene = EmptyScene();
  103. for (auto& formation : scene->formation) for (auto& enemy : formation) enemy.id = 0xFFFF;
  104. // An enemy with all the flags, and one with only the direction.
  105. scene->formation[0][0].id = 1;
  106. scene->formation[0][0].flags = 0xFFFFFFFF;
  107. scene->formation[0][1].id = 2;
  108. scene->formation[0][1].flags = 0xFFFFFFE6;
  109. const std::vector<Formation> formations = BattleSceneConverter::Formations(*scene);
  110. BOOST_REQUIRE_EQUAL(formations[0].enemies.size(), 2u);
  111. const Formation::Enemy& full = formations[0].enemies[0];
  112. BOOST_CHECK(full.visible && full.targeteable && full.main_script_active);
  113. BOOST_CHECK_EQUAL(full.direction, 1);
  114. const Formation::Enemy& waiting = formations[0].enemies[1];
  115. BOOST_CHECK(!waiting.visible && !waiting.targeteable && !waiting.main_script_active);
  116. BOOST_CHECK_EQUAL(waiting.direction, 1);
  117. }
  118. BOOST_AUTO_TEST_CASE(TestBattleSceneAiScripts){
  119. // The block of the second enemy at 6 bytes from the AI section: two scripts after 32 bytes.
  120. std::vector<u8> bytes(0x0E80 + 6 + 32 + 6, 0xFF);
  121. bytes[0x0E82] = 6;
  122. bytes[0x0E83] = 0;
  123. const size_t block = 0x0E80 + 6;
  124. bytes[block] = 32;
  125. bytes[block + 1] = 0;
  126. bytes[block + 4] = 35;
  127. bytes[block + 5] = 0;
  128. const u8 scripts[6] = {1, 0x73, 0xFF, 2, 0x73, 0xFF};
  129. std::copy(scripts, scripts + 6, bytes.begin() + block + 32);
  130. auto scene = EmptyScene();
  131. BattleSceneReader::ReadAiScripts(bytes, *scene);
  132. BOOST_REQUIRE_EQUAL(scene->ai_scripts[1].size(), 16u);
  133. // Without the padding.
  134. BOOST_CHECK(scene->ai_scripts[1][0] == std::vector<u8>({1, 0x73}));
  135. BOOST_CHECK(scene->ai_scripts[1][1].empty());
  136. BOOST_CHECK(scene->ai_scripts[1][2] == std::vector<u8>({2, 0x73}));
  137. BOOST_CHECK(scene->ai_scripts[0][0].empty());
  138. // A scene cut short has no scripts.
  139. BattleSceneReader::ReadAiScripts(std::vector<u8>(100), *scene);
  140. BOOST_CHECK(scene->ai_scripts[1][0].empty());
  141. }