WorldData.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <sstream>
  16. #include <boost/test/unit_test.hpp>
  17. #include "installer/extractors/WorldEncounterInstaller.h"
  18. #include "installer/extractors/WorldScriptInstaller.h"
  19. BOOST_AUTO_TEST_CASE(TestWorldEventFileNames){
  20. BOOST_CHECK(WorldScriptInstaller::IsEventFile("wm0.ev"));
  21. BOOST_CHECK(WorldScriptInstaller::IsEventFile("wm12.ev"));
  22. BOOST_CHECK(!WorldScriptInstaller::IsEventFile("wm.ev"));
  23. BOOST_CHECK(!WorldScriptInstaller::IsEventFile("wmx.ev"));
  24. BOOST_CHECK(!WorldScriptInstaller::IsEventFile("wm0.tex"));
  25. BOOST_CHECK(!WorldScriptInstaller::IsEventFile("ab0.ev"));
  26. }
  27. BOOST_AUTO_TEST_CASE(TestWorldFunctionTables){
  28. // Function 0 is of an entity (type 1) and function 1 of a mesh (type 2).
  29. const unsigned int entity = (1 << 14) | (5 << 8) | 3;
  30. const unsigned int mesh = (2 << 14) | (100 << 4) | 2;
  31. const std::vector<u8> events{
  32. static_cast<u8>(entity & 0xFF), static_cast<u8>(entity >> 8), 0, 0,
  33. static_cast<u8>(mesh & 0xFF), static_cast<u8>(mesh >> 8), 0, 0
  34. };
  35. std::ostringstream meshes;
  36. WorldScriptInstaller::WriteMeshFunctions(meshes, events, {true, true});
  37. BOOST_CHECK(meshes.str().find("System.WorldMap.triangle_functions[100][2] = 1\n") != std::string::npos);
  38. BOOST_CHECK(meshes.str().find("= 0\n") == std::string::npos);
  39. std::ostringstream entities;
  40. WorldScriptInstaller::WriteEntityFunctions(entities, events, {true, true});
  41. BOOST_CHECK(entities.str().find("System.WorldMap.entity_functions[5][3] = 0\n") != std::string::npos);
  42. BOOST_CHECK(entities.str().find("= 1\n") == std::string::npos);
  43. // Functions that were not decompiled are not in the tables.
  44. std::ostringstream none;
  45. WorldScriptInstaller::WriteMeshFunctions(none, events, {false, false});
  46. BOOST_CHECK(none.str().empty());
  47. }
  48. BOOST_AUTO_TEST_CASE(TestWorldFieldTableSkipsFieldsNotInstalled){
  49. std::vector<u8> table(WorldScriptInstaller::FIELD_TABLE_SIZE, 0xFF);
  50. // First record, first scenario: field 1, at (-256, 128), triangle 7, direction 64.
  51. const u8 first[12] = {0x00, 0xFF, 0x80, 0x00, 7, 0, 1, 0, 64, 0, 0, 0};
  52. std::copy(first, first + 12, table.begin());
  53. // First record, second scenario: field 2, which is not installed.
  54. const u8 second[12] = {0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0};
  55. std::copy(second, second + 12, table.begin() + 12);
  56. std::ostringstream script;
  57. WorldScriptInstaller::WriteFieldTable(
  58. script, table, {"startmap", "md1stin", "md1_1"}, {{1, 1.0f}, {2, 1.0f}}, {"md1stin"}
  59. );
  60. BOOST_CHECK(script.str().find(
  61. "System.WorldMap.field_table[1][0] = {field = \"md1stin\", x = -2, y = 1, triangle = 7, direction = 90}"
  62. ) != std::string::npos);
  63. BOOST_CHECK(script.str().find("md1_1") == std::string::npos);
  64. }
  65. BOOST_AUTO_TEST_CASE(TestWorldEncounters){
  66. BOOST_CHECK(!WorldEncounterInstaller::CreateEncounters(std::vector<u8>(100)));
  67. std::vector<u8> data(0xA0 + 64 * 32, 0);
  68. // Region 1, terrain group 2: enabled, rate 40, first word 0x1234.
  69. const size_t record = 0xA0 + 6 * 32;
  70. data[record] = 1;
  71. data[record + 1] = 40;
  72. data[record + 2] = 0x34;
  73. data[record + 3] = 0x12;
  74. const auto encounters = WorldEncounterInstaller::CreateEncounters(data);
  75. BOOST_REQUIRE(encounters);
  76. const TiXmlElement* element = encounters->FirstChildElement("record");
  77. for (int i = 0; i < 6; ++ i) element = element->NextSiblingElement("record");
  78. BOOST_CHECK_EQUAL(std::string(element->Attribute("region")), "1");
  79. BOOST_CHECK_EQUAL(std::string(element->Attribute("enabled")), "1");
  80. BOOST_CHECK_EQUAL(std::string(element->Attribute("rate")), "40");
  81. BOOST_CHECK_EQUAL(std::string(element->Attribute("words")).substr(0, 7), "4660 0 ");
  82. }