serialization_test.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. #include "core/component.h"
  2. #include "core/entity.h"
  3. #include "core/serialization.h"
  4. #include "core/world.h"
  5. #include "systems/nation_id.h"
  6. #include "systems/owner_registry.h"
  7. #include "units/spawn_type.h"
  8. #include "units/troop_type.h"
  9. #include <QJsonArray>
  10. #include <QJsonDocument>
  11. #include <QJsonObject>
  12. #include <QTemporaryFile>
  13. #include <gtest/gtest.h>
  14. using namespace Engine::Core;
  15. class SerializationTest : public ::testing::Test {
  16. protected:
  17. void SetUp() override { world = std::make_unique<World>(); }
  18. void TearDown() override { world.reset(); }
  19. std::unique_ptr<World> world;
  20. };
  21. TEST_F(SerializationTest, EntitySerializationBasic) {
  22. auto *entity = world->create_entity();
  23. ASSERT_NE(entity, nullptr);
  24. auto entity_id = entity->get_id();
  25. QJsonObject json = Serialization::serialize_entity(entity);
  26. EXPECT_TRUE(json.contains("id"));
  27. EXPECT_EQ(json["id"].toVariant().toULongLong(),
  28. static_cast<qulonglong>(entity_id));
  29. }
  30. TEST_F(SerializationTest, TransformComponentSerialization) {
  31. auto *entity = world->create_entity();
  32. auto *transform = entity->add_component<TransformComponent>();
  33. transform->position.x = 10.5F;
  34. transform->position.y = 20.3F;
  35. transform->position.z = 30.1F;
  36. transform->rotation.x = 0.5F;
  37. transform->rotation.y = 1.0F;
  38. transform->rotation.z = 1.5F;
  39. transform->scale.x = 2.0F;
  40. transform->scale.y = 2.5F;
  41. transform->scale.z = 3.0F;
  42. transform->has_desired_yaw = true;
  43. transform->desired_yaw = 45.0F;
  44. QJsonObject json = Serialization::serialize_entity(entity);
  45. ASSERT_TRUE(json.contains("transform"));
  46. QJsonObject transform_obj = json["transform"].toObject();
  47. EXPECT_FLOAT_EQ(transform_obj["pos_x"].toDouble(), 10.5);
  48. EXPECT_FLOAT_EQ(transform_obj["pos_y"].toDouble(), 20.3);
  49. EXPECT_FLOAT_EQ(transform_obj["pos_z"].toDouble(), 30.1);
  50. EXPECT_FLOAT_EQ(transform_obj["rot_x"].toDouble(), 0.5);
  51. EXPECT_FLOAT_EQ(transform_obj["rot_y"].toDouble(), 1.0);
  52. EXPECT_FLOAT_EQ(transform_obj["rot_z"].toDouble(), 1.5);
  53. EXPECT_FLOAT_EQ(transform_obj["scale_x"].toDouble(), 2.0);
  54. EXPECT_FLOAT_EQ(transform_obj["scale_y"].toDouble(), 2.5);
  55. EXPECT_FLOAT_EQ(transform_obj["scale_z"].toDouble(), 3.0);
  56. EXPECT_TRUE(transform_obj["has_desired_yaw"].toBool());
  57. EXPECT_FLOAT_EQ(transform_obj["desired_yaw"].toDouble(), 45.0);
  58. }
  59. TEST_F(SerializationTest, TransformComponentRoundTrip) {
  60. auto *original_entity = world->create_entity();
  61. auto *transform = original_entity->add_component<TransformComponent>();
  62. transform->position.x = 15.0F;
  63. transform->position.y = 25.0F;
  64. transform->position.z = 35.0F;
  65. transform->rotation.x = 1.0F;
  66. transform->rotation.y = 2.0F;
  67. transform->rotation.z = 3.0F;
  68. transform->scale.x = 1.5F;
  69. transform->scale.y = 2.5F;
  70. transform->scale.z = 3.5F;
  71. transform->has_desired_yaw = true;
  72. transform->desired_yaw = 90.0F;
  73. QJsonObject json = Serialization::serialize_entity(original_entity);
  74. auto *new_entity = world->create_entity();
  75. Serialization::deserialize_entity(new_entity, json);
  76. auto *deserialized = new_entity->get_component<TransformComponent>();
  77. ASSERT_NE(deserialized, nullptr);
  78. EXPECT_FLOAT_EQ(deserialized->position.x, 15.0F);
  79. EXPECT_FLOAT_EQ(deserialized->position.y, 25.0F);
  80. EXPECT_FLOAT_EQ(deserialized->position.z, 35.0F);
  81. EXPECT_FLOAT_EQ(deserialized->rotation.x, 1.0F);
  82. EXPECT_FLOAT_EQ(deserialized->rotation.y, 2.0F);
  83. EXPECT_FLOAT_EQ(deserialized->rotation.z, 3.0F);
  84. EXPECT_FLOAT_EQ(deserialized->scale.x, 1.5F);
  85. EXPECT_FLOAT_EQ(deserialized->scale.y, 2.5F);
  86. EXPECT_FLOAT_EQ(deserialized->scale.z, 3.5F);
  87. EXPECT_TRUE(deserialized->has_desired_yaw);
  88. EXPECT_FLOAT_EQ(deserialized->desired_yaw, 90.0F);
  89. }
  90. TEST_F(SerializationTest, UnitComponentSerialization) {
  91. auto *entity = world->create_entity();
  92. auto *unit = entity->add_component<UnitComponent>();
  93. unit->health = 80;
  94. unit->max_health = 100;
  95. unit->speed = 5.5F;
  96. unit->vision_range = 15.0F;
  97. unit->spawn_type = Game::Units::SpawnType::Archer;
  98. unit->owner_id = 1;
  99. unit->nation_id = Game::Systems::NationID::RomanRepublic;
  100. QJsonObject json = Serialization::serialize_entity(entity);
  101. ASSERT_TRUE(json.contains("unit"));
  102. QJsonObject unit_obj = json["unit"].toObject();
  103. EXPECT_EQ(unit_obj["health"].toInt(), 80);
  104. EXPECT_EQ(unit_obj["max_health"].toInt(), 100);
  105. EXPECT_FLOAT_EQ(unit_obj["speed"].toDouble(), 5.5);
  106. EXPECT_FLOAT_EQ(unit_obj["vision_range"].toDouble(), 15.0);
  107. EXPECT_EQ(unit_obj["unit_type"].toString(), QString("archer"));
  108. EXPECT_EQ(unit_obj["owner_id"].toInt(), 1);
  109. EXPECT_EQ(unit_obj["nation_id"].toString(), QString("roman_republic"));
  110. }
  111. TEST_F(SerializationTest, UnitComponentRoundTrip) {
  112. auto *original_entity = world->create_entity();
  113. auto *unit = original_entity->add_component<UnitComponent>();
  114. unit->health = 75;
  115. unit->max_health = 150;
  116. unit->speed = 7.5F;
  117. unit->vision_range = 20.0F;
  118. unit->spawn_type = Game::Units::SpawnType::Spearman;
  119. unit->owner_id = 2;
  120. unit->nation_id = Game::Systems::NationID::Carthage;
  121. QJsonObject json = Serialization::serialize_entity(original_entity);
  122. auto *new_entity = world->create_entity();
  123. Serialization::deserialize_entity(new_entity, json);
  124. auto *deserialized = new_entity->get_component<UnitComponent>();
  125. ASSERT_NE(deserialized, nullptr);
  126. EXPECT_EQ(deserialized->health, 75);
  127. EXPECT_EQ(deserialized->max_health, 150);
  128. EXPECT_FLOAT_EQ(deserialized->speed, 7.5F);
  129. EXPECT_FLOAT_EQ(deserialized->vision_range, 20.0F);
  130. EXPECT_EQ(deserialized->spawn_type, Game::Units::SpawnType::Spearman);
  131. EXPECT_EQ(deserialized->owner_id, 2);
  132. EXPECT_EQ(deserialized->nation_id, Game::Systems::NationID::Carthage);
  133. }
  134. TEST_F(SerializationTest, MovementComponentSerialization) {
  135. auto *entity = world->create_entity();
  136. auto *movement = entity->add_component<MovementComponent>();
  137. movement->has_target = true;
  138. movement->target_x = 50.0F;
  139. movement->target_y = 60.0F;
  140. movement->goal_x = 55.0F;
  141. movement->goal_y = 65.0F;
  142. movement->vx = 1.5F;
  143. movement->vz = 2.0F;
  144. movement->path_pending = false;
  145. movement->pending_request_id = 42;
  146. movement->repath_cooldown = 1.0F;
  147. movement->last_goal_x = 45.0F;
  148. movement->last_goal_y = 55.0F;
  149. movement->time_since_last_path_request = 0.5F;
  150. movement->path.emplace_back(10.0F, 20.0F);
  151. movement->path.emplace_back(30.0F, 40.0F);
  152. QJsonObject json = Serialization::serialize_entity(entity);
  153. ASSERT_TRUE(json.contains("movement"));
  154. QJsonObject movement_obj = json["movement"].toObject();
  155. EXPECT_TRUE(movement_obj["has_target"].toBool());
  156. EXPECT_FLOAT_EQ(movement_obj["target_x"].toDouble(), 50.0);
  157. EXPECT_FLOAT_EQ(movement_obj["target_y"].toDouble(), 60.0);
  158. EXPECT_FLOAT_EQ(movement_obj["goal_x"].toDouble(), 55.0);
  159. EXPECT_FLOAT_EQ(movement_obj["goal_y"].toDouble(), 65.0);
  160. EXPECT_FLOAT_EQ(movement_obj["vx"].toDouble(), 1.5);
  161. EXPECT_FLOAT_EQ(movement_obj["vz"].toDouble(), 2.0);
  162. EXPECT_FALSE(movement_obj["path_pending"].toBool());
  163. EXPECT_EQ(movement_obj["pending_request_id"].toVariant().toULongLong(),
  164. 42ULL);
  165. ASSERT_TRUE(movement_obj.contains("path"));
  166. QJsonArray path_array = movement_obj["path"].toArray();
  167. ASSERT_EQ(path_array.size(), 2);
  168. QJsonObject waypoint1 = path_array[0].toObject();
  169. EXPECT_FLOAT_EQ(waypoint1["x"].toDouble(), 10.0);
  170. EXPECT_FLOAT_EQ(waypoint1["y"].toDouble(), 20.0);
  171. QJsonObject waypoint2 = path_array[1].toObject();
  172. EXPECT_FLOAT_EQ(waypoint2["x"].toDouble(), 30.0);
  173. EXPECT_FLOAT_EQ(waypoint2["y"].toDouble(), 40.0);
  174. }
  175. TEST_F(SerializationTest, AttackComponentSerialization) {
  176. auto *entity = world->create_entity();
  177. auto *attack = entity->add_component<AttackComponent>();
  178. attack->range = 10.0F;
  179. attack->damage = 25;
  180. attack->cooldown = 2.0F;
  181. attack->time_since_last = 0.5F;
  182. attack->melee_range = 2.0F;
  183. attack->melee_damage = 15;
  184. attack->melee_cooldown = 1.5F;
  185. attack->preferred_mode = AttackComponent::CombatMode::Ranged;
  186. attack->current_mode = AttackComponent::CombatMode::Ranged;
  187. attack->can_melee = true;
  188. attack->can_ranged = true;
  189. attack->max_height_difference = 5.0F;
  190. attack->in_melee_lock = false;
  191. attack->melee_lock_target_id = 0;
  192. QJsonObject json = Serialization::serialize_entity(entity);
  193. ASSERT_TRUE(json.contains("attack"));
  194. QJsonObject attack_obj = json["attack"].toObject();
  195. EXPECT_FLOAT_EQ(attack_obj["range"].toDouble(), 10.0);
  196. EXPECT_EQ(attack_obj["damage"].toInt(), 25);
  197. EXPECT_FLOAT_EQ(attack_obj["cooldown"].toDouble(), 2.0);
  198. EXPECT_FLOAT_EQ(attack_obj["time_since_last"].toDouble(), 0.5);
  199. EXPECT_FLOAT_EQ(attack_obj["melee_range"].toDouble(), 2.0);
  200. EXPECT_EQ(attack_obj["melee_damage"].toInt(), 15);
  201. EXPECT_FLOAT_EQ(attack_obj["melee_cooldown"].toDouble(), 1.5);
  202. EXPECT_EQ(attack_obj["preferred_mode"].toString(), QString("ranged"));
  203. EXPECT_EQ(attack_obj["current_mode"].toString(), QString("ranged"));
  204. EXPECT_TRUE(attack_obj["can_melee"].toBool());
  205. EXPECT_TRUE(attack_obj["can_ranged"].toBool());
  206. EXPECT_FLOAT_EQ(attack_obj["max_height_difference"].toDouble(), 5.0);
  207. EXPECT_FALSE(attack_obj["in_melee_lock"].toBool());
  208. }
  209. TEST_F(SerializationTest, EntityDeserializationRoundTrip) {
  210. auto *original_entity = world->create_entity();
  211. auto *transform = original_entity->add_component<TransformComponent>();
  212. transform->position.x = 100.0F;
  213. transform->position.y = 200.0F;
  214. transform->position.z = 300.0F;
  215. auto *unit = original_entity->add_component<UnitComponent>();
  216. unit->health = 75;
  217. unit->max_health = 100;
  218. unit->speed = 6.0F;
  219. QJsonObject json = Serialization::serialize_entity(original_entity);
  220. auto *new_entity = world->create_entity();
  221. Serialization::deserialize_entity(new_entity, json);
  222. auto *deserialized_transform =
  223. new_entity->get_component<TransformComponent>();
  224. ASSERT_NE(deserialized_transform, nullptr);
  225. EXPECT_FLOAT_EQ(deserialized_transform->position.x, 100.0F);
  226. EXPECT_FLOAT_EQ(deserialized_transform->position.y, 200.0F);
  227. EXPECT_FLOAT_EQ(deserialized_transform->position.z, 300.0F);
  228. auto *deserialized_unit = new_entity->get_component<UnitComponent>();
  229. ASSERT_NE(deserialized_unit, nullptr);
  230. EXPECT_EQ(deserialized_unit->health, 75);
  231. EXPECT_EQ(deserialized_unit->max_health, 100);
  232. EXPECT_FLOAT_EQ(deserialized_unit->speed, 6.0F);
  233. }
  234. TEST_F(SerializationTest, DeserializationWithMissingFields) {
  235. QJsonObject json;
  236. json["id"] = 1;
  237. QJsonObject unit_obj;
  238. unit_obj["health"] = 50;
  239. json["unit"] = unit_obj;
  240. auto *entity = world->create_entity();
  241. Serialization::deserialize_entity(entity, json);
  242. auto *unit = entity->get_component<UnitComponent>();
  243. ASSERT_NE(unit, nullptr);
  244. EXPECT_EQ(unit->health, 50);
  245. EXPECT_EQ(unit->max_health, Defaults::kUnitDefaultHealth);
  246. }
  247. TEST_F(SerializationTest, DeserializationWithMalformedJSON) {
  248. QJsonObject json;
  249. json["id"] = 1;
  250. QJsonObject transform_obj;
  251. transform_obj["pos_x"] = "not_a_number";
  252. json["transform"] = transform_obj;
  253. auto *entity = world->create_entity();
  254. EXPECT_NO_THROW({ Serialization::deserialize_entity(entity, json); });
  255. auto *transform = entity->get_component<TransformComponent>();
  256. ASSERT_NE(transform, nullptr);
  257. EXPECT_FLOAT_EQ(transform->position.x, 0.0F);
  258. }
  259. TEST_F(SerializationTest, WorldSerializationRoundTrip) {
  260. auto *entity1 = world->create_entity();
  261. auto *transform1 = entity1->add_component<TransformComponent>();
  262. transform1->position.x = 10.0F;
  263. auto *entity2 = world->create_entity();
  264. auto *transform2 = entity2->add_component<TransformComponent>();
  265. transform2->position.x = 20.0F;
  266. QJsonDocument doc = Serialization::serialize_world(world.get());
  267. ASSERT_TRUE(doc.isObject());
  268. QJsonObject world_obj = doc.object();
  269. EXPECT_TRUE(world_obj.contains("entities"));
  270. EXPECT_TRUE(world_obj.contains("nextEntityId"));
  271. EXPECT_TRUE(world_obj.contains("schemaVersion"));
  272. auto new_world = std::make_unique<World>();
  273. Serialization::deserialize_world(new_world.get(), doc);
  274. const auto &entities = new_world->get_entities();
  275. EXPECT_EQ(entities.size(), 2UL);
  276. }
  277. TEST_F(SerializationTest, SaveAndLoadFromFile) {
  278. auto *entity = world->create_entity();
  279. auto *transform = entity->add_component<TransformComponent>();
  280. transform->position.x = 42.0F;
  281. transform->position.y = 43.0F;
  282. transform->position.z = 44.0F;
  283. QJsonDocument doc = Serialization::serialize_world(world.get());
  284. QTemporaryFile temp_file;
  285. ASSERT_TRUE(temp_file.open());
  286. QString filename = temp_file.fileName();
  287. temp_file.close();
  288. EXPECT_TRUE(Serialization::save_to_file(filename, doc));
  289. QJsonDocument loaded_doc = Serialization::load_from_file(filename);
  290. EXPECT_FALSE(loaded_doc.isNull());
  291. auto new_world = std::make_unique<World>();
  292. Serialization::deserialize_world(new_world.get(), loaded_doc);
  293. const auto &entities = new_world->get_entities();
  294. EXPECT_EQ(entities.size(), 1UL);
  295. if (!entities.empty()) {
  296. auto *loaded_entity = entities.begin()->second.get();
  297. auto *loaded_transform = loaded_entity->get_component<TransformComponent>();
  298. ASSERT_NE(loaded_transform, nullptr);
  299. EXPECT_FLOAT_EQ(loaded_transform->position.x, 42.0F);
  300. EXPECT_FLOAT_EQ(loaded_transform->position.y, 43.0F);
  301. EXPECT_FLOAT_EQ(loaded_transform->position.z, 44.0F);
  302. }
  303. }
  304. TEST_F(SerializationTest, ProductionComponentSerialization) {
  305. auto *entity = world->create_entity();
  306. auto *production = entity->add_component<ProductionComponent>();
  307. production->in_progress = true;
  308. production->build_time = 10.0F;
  309. production->time_remaining = 5.0F;
  310. production->produced_count = 3;
  311. production->max_units = 10;
  312. production->product_type = Game::Units::TroopType::Archer;
  313. production->rally_x = 100.0F;
  314. production->rally_z = 200.0F;
  315. production->rally_set = true;
  316. production->villager_cost = 2;
  317. production->production_queue.push_back(Game::Units::TroopType::Spearman);
  318. production->production_queue.push_back(Game::Units::TroopType::Archer);
  319. QJsonObject json = Serialization::serialize_entity(entity);
  320. ASSERT_TRUE(json.contains("production"));
  321. QJsonObject prod_obj = json["production"].toObject();
  322. EXPECT_TRUE(prod_obj["in_progress"].toBool());
  323. EXPECT_FLOAT_EQ(prod_obj["build_time"].toDouble(), 10.0);
  324. EXPECT_FLOAT_EQ(prod_obj["time_remaining"].toDouble(), 5.0);
  325. EXPECT_EQ(prod_obj["produced_count"].toInt(), 3);
  326. EXPECT_EQ(prod_obj["max_units"].toInt(), 10);
  327. EXPECT_EQ(prod_obj["product_type"].toString(), QString("archer"));
  328. EXPECT_FLOAT_EQ(prod_obj["rally_x"].toDouble(), 100.0);
  329. EXPECT_FLOAT_EQ(prod_obj["rally_z"].toDouble(), 200.0);
  330. EXPECT_TRUE(prod_obj["rally_set"].toBool());
  331. EXPECT_EQ(prod_obj["villager_cost"].toInt(), 2);
  332. ASSERT_TRUE(prod_obj.contains("queue"));
  333. QJsonArray queue = prod_obj["queue"].toArray();
  334. EXPECT_EQ(queue.size(), 2);
  335. EXPECT_EQ(queue[0].toString(), QString("spearman"));
  336. EXPECT_EQ(queue[1].toString(), QString("archer"));
  337. }
  338. TEST_F(SerializationTest, PatrolComponentSerialization) {
  339. auto *entity = world->create_entity();
  340. auto *patrol = entity->add_component<PatrolComponent>();
  341. patrol->current_waypoint = 1;
  342. patrol->patrolling = true;
  343. patrol->waypoints.emplace_back(10.0F, 20.0F);
  344. patrol->waypoints.emplace_back(30.0F, 40.0F);
  345. patrol->waypoints.emplace_back(50.0F, 60.0F);
  346. QJsonObject json = Serialization::serialize_entity(entity);
  347. ASSERT_TRUE(json.contains("patrol"));
  348. QJsonObject patrol_obj = json["patrol"].toObject();
  349. EXPECT_EQ(patrol_obj["current_waypoint"].toInt(), 1);
  350. EXPECT_TRUE(patrol_obj["patrolling"].toBool());
  351. ASSERT_TRUE(patrol_obj.contains("waypoints"));
  352. QJsonArray waypoints = patrol_obj["waypoints"].toArray();
  353. EXPECT_EQ(waypoints.size(), 3);
  354. QJsonObject wp0 = waypoints[0].toObject();
  355. EXPECT_FLOAT_EQ(wp0["x"].toDouble(), 10.0);
  356. EXPECT_FLOAT_EQ(wp0["y"].toDouble(), 20.0);
  357. }
  358. TEST_F(SerializationTest, PatrolComponentRoundTrip) {
  359. auto *original_entity = world->create_entity();
  360. auto *patrol = original_entity->add_component<PatrolComponent>();
  361. patrol->current_waypoint = 2;
  362. patrol->patrolling = true;
  363. patrol->waypoints.emplace_back(15.0F, 25.0F);
  364. patrol->waypoints.emplace_back(35.0F, 45.0F);
  365. QJsonObject json = Serialization::serialize_entity(original_entity);
  366. auto *new_entity = world->create_entity();
  367. Serialization::deserialize_entity(new_entity, json);
  368. auto *deserialized = new_entity->get_component<PatrolComponent>();
  369. ASSERT_NE(deserialized, nullptr);
  370. EXPECT_EQ(deserialized->current_waypoint, 2UL);
  371. EXPECT_TRUE(deserialized->patrolling);
  372. EXPECT_EQ(deserialized->waypoints.size(), 2UL);
  373. EXPECT_FLOAT_EQ(deserialized->waypoints[0].first, 15.0F);
  374. EXPECT_FLOAT_EQ(deserialized->waypoints[0].second, 25.0F);
  375. }
  376. TEST_F(SerializationTest, MovementComponentRoundTrip) {
  377. auto *original_entity = world->create_entity();
  378. auto *movement = original_entity->add_component<MovementComponent>();
  379. movement->has_target = true;
  380. movement->target_x = 100.0F;
  381. movement->target_y = 200.0F;
  382. movement->goal_x = 150.0F;
  383. movement->goal_y = 250.0F;
  384. movement->vx = 1.5F;
  385. movement->vz = 2.5F;
  386. movement->path.emplace_back(10.0F, 20.0F);
  387. movement->path.emplace_back(30.0F, 40.0F);
  388. QJsonObject json = Serialization::serialize_entity(original_entity);
  389. auto *new_entity = world->create_entity();
  390. Serialization::deserialize_entity(new_entity, json);
  391. auto *deserialized = new_entity->get_component<MovementComponent>();
  392. ASSERT_NE(deserialized, nullptr);
  393. EXPECT_TRUE(deserialized->has_target);
  394. EXPECT_FLOAT_EQ(deserialized->target_x, 100.0F);
  395. EXPECT_FLOAT_EQ(deserialized->target_y, 200.0F);
  396. EXPECT_FLOAT_EQ(deserialized->goal_x, 150.0F);
  397. EXPECT_FLOAT_EQ(deserialized->goal_y, 250.0F);
  398. EXPECT_FLOAT_EQ(deserialized->vx, 1.5F);
  399. EXPECT_FLOAT_EQ(deserialized->vz, 2.5F);
  400. EXPECT_EQ(deserialized->path.size(), 2UL);
  401. }
  402. TEST_F(SerializationTest, AttackComponentRoundTrip) {
  403. auto *original_entity = world->create_entity();
  404. auto *attack = original_entity->add_component<AttackComponent>();
  405. attack->range = 15.0F;
  406. attack->damage = 30;
  407. attack->cooldown = 2.5F;
  408. attack->melee_range = 3.0F;
  409. attack->melee_damage = 20;
  410. attack->preferred_mode = AttackComponent::CombatMode::Ranged;
  411. attack->current_mode = AttackComponent::CombatMode::Melee;
  412. attack->can_melee = true;
  413. attack->can_ranged = true;
  414. attack->in_melee_lock = true;
  415. attack->melee_lock_target_id = 42;
  416. QJsonObject json = Serialization::serialize_entity(original_entity);
  417. auto *new_entity = world->create_entity();
  418. Serialization::deserialize_entity(new_entity, json);
  419. auto *deserialized = new_entity->get_component<AttackComponent>();
  420. ASSERT_NE(deserialized, nullptr);
  421. EXPECT_FLOAT_EQ(deserialized->range, 15.0F);
  422. EXPECT_EQ(deserialized->damage, 30);
  423. EXPECT_FLOAT_EQ(deserialized->cooldown, 2.5F);
  424. EXPECT_FLOAT_EQ(deserialized->melee_range, 3.0F);
  425. EXPECT_EQ(deserialized->melee_damage, 20);
  426. EXPECT_EQ(deserialized->preferred_mode, AttackComponent::CombatMode::Ranged);
  427. EXPECT_EQ(deserialized->current_mode, AttackComponent::CombatMode::Melee);
  428. EXPECT_TRUE(deserialized->can_melee);
  429. EXPECT_TRUE(deserialized->can_ranged);
  430. EXPECT_TRUE(deserialized->in_melee_lock);
  431. EXPECT_EQ(deserialized->melee_lock_target_id, 42U);
  432. }
  433. TEST_F(SerializationTest, ProductionComponentRoundTrip) {
  434. auto *original_entity = world->create_entity();
  435. auto *production = original_entity->add_component<ProductionComponent>();
  436. production->in_progress = true;
  437. production->build_time = 15.0F;
  438. production->time_remaining = 7.5F;
  439. production->produced_count = 5;
  440. production->max_units = 20;
  441. production->product_type = Game::Units::TroopType::Spearman;
  442. production->rally_x = 150.0F;
  443. production->rally_z = 250.0F;
  444. production->rally_set = true;
  445. production->villager_cost = 3;
  446. production->production_queue.push_back(Game::Units::TroopType::Archer);
  447. QJsonObject json = Serialization::serialize_entity(original_entity);
  448. auto *new_entity = world->create_entity();
  449. Serialization::deserialize_entity(new_entity, json);
  450. auto *deserialized = new_entity->get_component<ProductionComponent>();
  451. ASSERT_NE(deserialized, nullptr);
  452. EXPECT_TRUE(deserialized->in_progress);
  453. EXPECT_FLOAT_EQ(deserialized->build_time, 15.0F);
  454. EXPECT_FLOAT_EQ(deserialized->time_remaining, 7.5F);
  455. EXPECT_EQ(deserialized->produced_count, 5);
  456. EXPECT_EQ(deserialized->max_units, 20);
  457. EXPECT_EQ(deserialized->product_type, Game::Units::TroopType::Spearman);
  458. EXPECT_FLOAT_EQ(deserialized->rally_x, 150.0F);
  459. EXPECT_FLOAT_EQ(deserialized->rally_z, 250.0F);
  460. EXPECT_TRUE(deserialized->rally_set);
  461. EXPECT_EQ(deserialized->villager_cost, 3);
  462. EXPECT_EQ(deserialized->production_queue.size(), 1UL);
  463. EXPECT_EQ(deserialized->production_queue[0], Game::Units::TroopType::Archer);
  464. }
  465. TEST_F(SerializationTest, RenderableComponentSerialization) {
  466. auto *entity = world->create_entity();
  467. auto *renderable =
  468. entity->add_component<RenderableComponent>("mesh.obj", "texture.png");
  469. renderable->mesh_path = "models/archer.obj";
  470. renderable->texture_path = "textures/archer_diffuse.png";
  471. renderable->renderer_id = "archer_renderer";
  472. renderable->visible = true;
  473. renderable->mesh = RenderableComponent::MeshKind::Capsule;
  474. renderable->color = {0.8F, 0.2F, 0.5F};
  475. QJsonObject json = Serialization::serialize_entity(entity);
  476. ASSERT_TRUE(json.contains("renderable"));
  477. QJsonObject renderable_obj = json["renderable"].toObject();
  478. EXPECT_EQ(renderable_obj["mesh_path"].toString(),
  479. QString("models/archer.obj"));
  480. EXPECT_EQ(renderable_obj["texture_path"].toString(),
  481. QString("textures/archer_diffuse.png"));
  482. EXPECT_EQ(renderable_obj["renderer_id"].toString(),
  483. QString("archer_renderer"));
  484. EXPECT_TRUE(renderable_obj["visible"].toBool());
  485. EXPECT_EQ(renderable_obj["mesh"].toInt(),
  486. static_cast<int>(RenderableComponent::MeshKind::Capsule));
  487. ASSERT_TRUE(renderable_obj.contains("color"));
  488. QJsonArray color = renderable_obj["color"].toArray();
  489. EXPECT_EQ(color.size(), 3);
  490. EXPECT_FLOAT_EQ(color[0].toDouble(), 0.8);
  491. EXPECT_FLOAT_EQ(color[1].toDouble(), 0.2);
  492. EXPECT_FLOAT_EQ(color[2].toDouble(), 0.5);
  493. }
  494. TEST_F(SerializationTest, RenderableComponentRoundTrip) {
  495. auto *original_entity = world->create_entity();
  496. auto *renderable = original_entity->add_component<RenderableComponent>(
  497. "test.obj", "test.png");
  498. renderable->mesh_path = "models/building.obj";
  499. renderable->texture_path = "textures/building.png";
  500. renderable->visible = false;
  501. renderable->mesh = RenderableComponent::MeshKind::Quad;
  502. renderable->color = {1.0F, 0.5F, 0.25F};
  503. QJsonObject json = Serialization::serialize_entity(original_entity);
  504. auto *new_entity = world->create_entity();
  505. Serialization::deserialize_entity(new_entity, json);
  506. auto *deserialized = new_entity->get_component<RenderableComponent>();
  507. ASSERT_NE(deserialized, nullptr);
  508. EXPECT_EQ(deserialized->mesh_path, "models/building.obj");
  509. EXPECT_EQ(deserialized->texture_path, "textures/building.png");
  510. EXPECT_FALSE(deserialized->visible);
  511. EXPECT_EQ(deserialized->mesh, RenderableComponent::MeshKind::Quad);
  512. EXPECT_FLOAT_EQ(deserialized->color[0], 1.0F);
  513. EXPECT_FLOAT_EQ(deserialized->color[1], 0.5F);
  514. EXPECT_FLOAT_EQ(deserialized->color[2], 0.25F);
  515. }
  516. TEST_F(SerializationTest, AttackTargetComponentSerialization) {
  517. auto *entity = world->create_entity();
  518. auto *attack_target = entity->add_component<AttackTargetComponent>();
  519. attack_target->target_id = 42;
  520. attack_target->should_chase = true;
  521. QJsonObject json = Serialization::serialize_entity(entity);
  522. ASSERT_TRUE(json.contains("attack_target"));
  523. QJsonObject attack_target_obj = json["attack_target"].toObject();
  524. EXPECT_EQ(attack_target_obj["target_id"].toVariant().toULongLong(), 42ULL);
  525. EXPECT_TRUE(attack_target_obj["should_chase"].toBool());
  526. }
  527. TEST_F(SerializationTest, AttackTargetComponentRoundTrip) {
  528. auto *original_entity = world->create_entity();
  529. auto *attack_target = original_entity->add_component<AttackTargetComponent>();
  530. attack_target->target_id = 123;
  531. attack_target->should_chase = false;
  532. QJsonObject json = Serialization::serialize_entity(original_entity);
  533. auto *new_entity = world->create_entity();
  534. Serialization::deserialize_entity(new_entity, json);
  535. auto *deserialized = new_entity->get_component<AttackTargetComponent>();
  536. ASSERT_NE(deserialized, nullptr);
  537. EXPECT_EQ(deserialized->target_id, 123U);
  538. EXPECT_FALSE(deserialized->should_chase);
  539. }
  540. TEST_F(SerializationTest, BuildingComponentSerialization) {
  541. auto *entity = world->create_entity();
  542. entity->add_component<BuildingComponent>();
  543. QJsonObject json = Serialization::serialize_entity(entity);
  544. ASSERT_TRUE(json.contains("building"));
  545. EXPECT_TRUE(json["building"].toBool());
  546. }
  547. TEST_F(SerializationTest, BuildingComponentRoundTrip) {
  548. auto *original_entity = world->create_entity();
  549. original_entity->add_component<BuildingComponent>();
  550. QJsonObject json = Serialization::serialize_entity(original_entity);
  551. auto *new_entity = world->create_entity();
  552. Serialization::deserialize_entity(new_entity, json);
  553. auto *deserialized = new_entity->get_component<BuildingComponent>();
  554. ASSERT_NE(deserialized, nullptr);
  555. }
  556. TEST_F(SerializationTest, AIControlledComponentSerialization) {
  557. auto *entity = world->create_entity();
  558. entity->add_component<AIControlledComponent>();
  559. QJsonObject json = Serialization::serialize_entity(entity);
  560. ASSERT_TRUE(json.contains("aiControlled"));
  561. EXPECT_TRUE(json["aiControlled"].toBool());
  562. }
  563. TEST_F(SerializationTest, AIControlledComponentRoundTrip) {
  564. auto *original_entity = world->create_entity();
  565. original_entity->add_component<AIControlledComponent>();
  566. QJsonObject json = Serialization::serialize_entity(original_entity);
  567. auto *new_entity = world->create_entity();
  568. Serialization::deserialize_entity(new_entity, json);
  569. auto *deserialized = new_entity->get_component<AIControlledComponent>();
  570. ASSERT_NE(deserialized, nullptr);
  571. }
  572. TEST_F(SerializationTest, CaptureComponentSerialization) {
  573. auto *entity = world->create_entity();
  574. auto *capture = entity->add_component<CaptureComponent>();
  575. capture->capturing_player_id = 2;
  576. capture->capture_progress = 7.5F;
  577. capture->required_time = 15.0F;
  578. capture->is_being_captured = true;
  579. QJsonObject json = Serialization::serialize_entity(entity);
  580. ASSERT_TRUE(json.contains("capture"));
  581. QJsonObject capture_obj = json["capture"].toObject();
  582. EXPECT_EQ(capture_obj["capturing_player_id"].toInt(), 2);
  583. EXPECT_FLOAT_EQ(capture_obj["capture_progress"].toDouble(), 7.5);
  584. EXPECT_FLOAT_EQ(capture_obj["required_time"].toDouble(), 15.0);
  585. EXPECT_TRUE(capture_obj["is_being_captured"].toBool());
  586. }
  587. TEST_F(SerializationTest, CaptureComponentRoundTrip) {
  588. auto *original_entity = world->create_entity();
  589. auto *capture = original_entity->add_component<CaptureComponent>();
  590. capture->capturing_player_id = 3;
  591. capture->capture_progress = 10.0F;
  592. capture->required_time = 20.0F;
  593. capture->is_being_captured = false;
  594. QJsonObject json = Serialization::serialize_entity(original_entity);
  595. auto *new_entity = world->create_entity();
  596. Serialization::deserialize_entity(new_entity, json);
  597. auto *deserialized = new_entity->get_component<CaptureComponent>();
  598. ASSERT_NE(deserialized, nullptr);
  599. EXPECT_EQ(deserialized->capturing_player_id, 3);
  600. EXPECT_FLOAT_EQ(deserialized->capture_progress, 10.0F);
  601. EXPECT_FLOAT_EQ(deserialized->required_time, 20.0F);
  602. EXPECT_FALSE(deserialized->is_being_captured);
  603. }
  604. TEST_F(SerializationTest, CompleteEntityWithAllComponents) {
  605. auto *entity = world->create_entity();
  606. auto *transform = entity->add_component<TransformComponent>();
  607. transform->position.x = 50.0F;
  608. transform->position.y = 10.0F;
  609. transform->position.z = 30.0F;
  610. auto *renderable =
  611. entity->add_component<RenderableComponent>("mesh.obj", "tex.png");
  612. renderable->visible = true;
  613. auto *unit = entity->add_component<UnitComponent>();
  614. unit->health = 100;
  615. unit->max_health = 100;
  616. auto *movement = entity->add_component<MovementComponent>();
  617. movement->has_target = true;
  618. movement->target_x = 100.0F;
  619. auto *attack = entity->add_component<AttackComponent>();
  620. attack->damage = 25;
  621. auto *attack_target = entity->add_component<AttackTargetComponent>();
  622. attack_target->target_id = 99;
  623. entity->add_component<BuildingComponent>();
  624. auto *production = entity->add_component<ProductionComponent>();
  625. production->in_progress = true;
  626. entity->add_component<AIControlledComponent>();
  627. auto *capture = entity->add_component<CaptureComponent>();
  628. capture->is_being_captured = true;
  629. auto *hold_mode = entity->add_component<HoldModeComponent>();
  630. hold_mode->active = true;
  631. auto *healer = entity->add_component<HealerComponent>();
  632. healer->healing_amount = 10;
  633. auto *catapult = entity->add_component<CatapultLoadingComponent>();
  634. catapult->state = CatapultLoadingComponent::LoadingState::Idle;
  635. QJsonObject json = Serialization::serialize_entity(entity);
  636. EXPECT_TRUE(json.contains("transform"));
  637. EXPECT_TRUE(json.contains("renderable"));
  638. EXPECT_TRUE(json.contains("unit"));
  639. EXPECT_TRUE(json.contains("movement"));
  640. EXPECT_TRUE(json.contains("attack"));
  641. EXPECT_TRUE(json.contains("attack_target"));
  642. EXPECT_TRUE(json.contains("building"));
  643. EXPECT_TRUE(json.contains("production"));
  644. EXPECT_TRUE(json.contains("aiControlled"));
  645. EXPECT_TRUE(json.contains("capture"));
  646. EXPECT_TRUE(json.contains("hold_mode"));
  647. EXPECT_TRUE(json.contains("healer"));
  648. EXPECT_TRUE(json.contains("catapult_loading"));
  649. auto *new_entity = world->create_entity();
  650. Serialization::deserialize_entity(new_entity, json);
  651. EXPECT_NE(new_entity->get_component<TransformComponent>(), nullptr);
  652. EXPECT_NE(new_entity->get_component<RenderableComponent>(), nullptr);
  653. EXPECT_NE(new_entity->get_component<UnitComponent>(), nullptr);
  654. EXPECT_NE(new_entity->get_component<MovementComponent>(), nullptr);
  655. EXPECT_NE(new_entity->get_component<AttackComponent>(), nullptr);
  656. EXPECT_NE(new_entity->get_component<AttackTargetComponent>(), nullptr);
  657. EXPECT_NE(new_entity->get_component<BuildingComponent>(), nullptr);
  658. EXPECT_NE(new_entity->get_component<ProductionComponent>(), nullptr);
  659. EXPECT_NE(new_entity->get_component<AIControlledComponent>(), nullptr);
  660. EXPECT_NE(new_entity->get_component<CaptureComponent>(), nullptr);
  661. EXPECT_NE(new_entity->get_component<HoldModeComponent>(), nullptr);
  662. EXPECT_NE(new_entity->get_component<HealerComponent>(), nullptr);
  663. EXPECT_NE(new_entity->get_component<CatapultLoadingComponent>(), nullptr);
  664. }
  665. TEST_F(SerializationTest, EmptyWorldSerialization) {
  666. QJsonDocument doc = Serialization::serialize_world(world.get());
  667. ASSERT_TRUE(doc.isObject());
  668. QJsonObject world_obj = doc.object();
  669. EXPECT_TRUE(world_obj.contains("entities"));
  670. QJsonArray entities = world_obj["entities"].toArray();
  671. EXPECT_EQ(entities.size(), 0);
  672. }
  673. TEST_F(SerializationTest, HoldModeComponentSerialization) {
  674. auto *entity = world->create_entity();
  675. auto *hold_mode = entity->add_component<HoldModeComponent>();
  676. hold_mode->active = false;
  677. hold_mode->exit_cooldown = 1.5F;
  678. hold_mode->stand_up_duration = 3.0F;
  679. QJsonObject json = Serialization::serialize_entity(entity);
  680. ASSERT_TRUE(json.contains("hold_mode"));
  681. QJsonObject hold_mode_obj = json["hold_mode"].toObject();
  682. EXPECT_FALSE(hold_mode_obj["active"].toBool());
  683. EXPECT_FLOAT_EQ(hold_mode_obj["exit_cooldown"].toDouble(), 1.5);
  684. EXPECT_FLOAT_EQ(hold_mode_obj["stand_up_duration"].toDouble(), 3.0);
  685. }
  686. TEST_F(SerializationTest, HoldModeComponentRoundTrip) {
  687. auto *original_entity = world->create_entity();
  688. auto *hold_mode = original_entity->add_component<HoldModeComponent>();
  689. hold_mode->active = true;
  690. hold_mode->exit_cooldown = 2.5F;
  691. hold_mode->stand_up_duration = 4.0F;
  692. QJsonObject json = Serialization::serialize_entity(original_entity);
  693. auto *new_entity = world->create_entity();
  694. Serialization::deserialize_entity(new_entity, json);
  695. auto *deserialized = new_entity->get_component<HoldModeComponent>();
  696. ASSERT_NE(deserialized, nullptr);
  697. EXPECT_TRUE(deserialized->active);
  698. EXPECT_FLOAT_EQ(deserialized->exit_cooldown, 2.5F);
  699. EXPECT_FLOAT_EQ(deserialized->stand_up_duration, 4.0F);
  700. }
  701. TEST_F(SerializationTest, HealerComponentSerialization) {
  702. auto *entity = world->create_entity();
  703. auto *healer = entity->add_component<HealerComponent>();
  704. healer->healing_range = 12.0F;
  705. healer->healing_amount = 10;
  706. healer->healing_cooldown = 3.0F;
  707. healer->time_since_last_heal = 1.0F;
  708. QJsonObject json = Serialization::serialize_entity(entity);
  709. ASSERT_TRUE(json.contains("healer"));
  710. QJsonObject healer_obj = json["healer"].toObject();
  711. EXPECT_FLOAT_EQ(healer_obj["healing_range"].toDouble(), 12.0);
  712. EXPECT_EQ(healer_obj["healing_amount"].toInt(), 10);
  713. EXPECT_FLOAT_EQ(healer_obj["healing_cooldown"].toDouble(), 3.0);
  714. EXPECT_FLOAT_EQ(healer_obj["time_since_last_heal"].toDouble(), 1.0);
  715. }
  716. TEST_F(SerializationTest, HealerComponentRoundTrip) {
  717. auto *original_entity = world->create_entity();
  718. auto *healer = original_entity->add_component<HealerComponent>();
  719. healer->healing_range = 15.0F;
  720. healer->healing_amount = 8;
  721. healer->healing_cooldown = 4.0F;
  722. healer->time_since_last_heal = 2.0F;
  723. QJsonObject json = Serialization::serialize_entity(original_entity);
  724. auto *new_entity = world->create_entity();
  725. Serialization::deserialize_entity(new_entity, json);
  726. auto *deserialized = new_entity->get_component<HealerComponent>();
  727. ASSERT_NE(deserialized, nullptr);
  728. EXPECT_FLOAT_EQ(deserialized->healing_range, 15.0F);
  729. EXPECT_EQ(deserialized->healing_amount, 8);
  730. EXPECT_FLOAT_EQ(deserialized->healing_cooldown, 4.0F);
  731. EXPECT_FLOAT_EQ(deserialized->time_since_last_heal, 2.0F);
  732. }
  733. TEST_F(SerializationTest, CatapultLoadingComponentSerialization) {
  734. auto *entity = world->create_entity();
  735. auto *catapult = entity->add_component<CatapultLoadingComponent>();
  736. catapult->state = CatapultLoadingComponent::LoadingState::Loading;
  737. catapult->loading_time = 1.0F;
  738. catapult->loading_duration = 3.0F;
  739. catapult->firing_time = 0.0F;
  740. catapult->firing_duration = 1.0F;
  741. catapult->target_id = 42;
  742. catapult->target_locked_x = 100.0F;
  743. catapult->target_locked_y = 50.0F;
  744. catapult->target_locked_z = 200.0F;
  745. catapult->target_position_locked = true;
  746. QJsonObject json = Serialization::serialize_entity(entity);
  747. ASSERT_TRUE(json.contains("catapult_loading"));
  748. QJsonObject catapult_obj = json["catapult_loading"].toObject();
  749. EXPECT_EQ(catapult_obj["state"].toInt(),
  750. static_cast<int>(CatapultLoadingComponent::LoadingState::Loading));
  751. EXPECT_FLOAT_EQ(catapult_obj["loading_time"].toDouble(), 1.0);
  752. EXPECT_FLOAT_EQ(catapult_obj["loading_duration"].toDouble(), 3.0);
  753. EXPECT_FLOAT_EQ(catapult_obj["firing_time"].toDouble(), 0.0);
  754. EXPECT_FLOAT_EQ(catapult_obj["firing_duration"].toDouble(), 1.0);
  755. EXPECT_EQ(catapult_obj["target_id"].toVariant().toULongLong(), 42ULL);
  756. EXPECT_FLOAT_EQ(catapult_obj["target_locked_x"].toDouble(), 100.0);
  757. EXPECT_FLOAT_EQ(catapult_obj["target_locked_y"].toDouble(), 50.0);
  758. EXPECT_FLOAT_EQ(catapult_obj["target_locked_z"].toDouble(), 200.0);
  759. EXPECT_TRUE(catapult_obj["target_position_locked"].toBool());
  760. }
  761. TEST_F(SerializationTest, CatapultLoadingComponentRoundTrip) {
  762. auto *original_entity = world->create_entity();
  763. auto *catapult = original_entity->add_component<CatapultLoadingComponent>();
  764. catapult->state = CatapultLoadingComponent::LoadingState::ReadyToFire;
  765. catapult->loading_time = 2.0F;
  766. catapult->loading_duration = 4.0F;
  767. catapult->firing_time = 0.25F;
  768. catapult->firing_duration = 0.75F;
  769. catapult->target_id = 99;
  770. catapult->target_locked_x = 150.0F;
  771. catapult->target_locked_y = 75.0F;
  772. catapult->target_locked_z = 250.0F;
  773. catapult->target_position_locked = false;
  774. QJsonObject json = Serialization::serialize_entity(original_entity);
  775. auto *new_entity = world->create_entity();
  776. Serialization::deserialize_entity(new_entity, json);
  777. auto *deserialized = new_entity->get_component<CatapultLoadingComponent>();
  778. ASSERT_NE(deserialized, nullptr);
  779. EXPECT_EQ(deserialized->state,
  780. CatapultLoadingComponent::LoadingState::ReadyToFire);
  781. EXPECT_FLOAT_EQ(deserialized->loading_time, 2.0F);
  782. EXPECT_FLOAT_EQ(deserialized->loading_duration, 4.0F);
  783. EXPECT_FLOAT_EQ(deserialized->firing_time, 0.25F);
  784. EXPECT_FLOAT_EQ(deserialized->firing_duration, 0.75F);
  785. EXPECT_EQ(deserialized->target_id, 99U);
  786. EXPECT_FLOAT_EQ(deserialized->target_locked_x, 150.0F);
  787. EXPECT_FLOAT_EQ(deserialized->target_locked_y, 75.0F);
  788. EXPECT_FLOAT_EQ(deserialized->target_locked_z, 250.0F);
  789. EXPECT_FALSE(deserialized->target_position_locked);
  790. }
  791. // ============================================================================
  792. // Integration Tests: Multi-Unit Battlefield State Preservation
  793. // ============================================================================
  794. TEST_F(SerializationTest, MultipleUnitsPositionsAndHealthPreserved) {
  795. // Create a battlefield with multiple units at different positions
  796. struct UnitData {
  797. float x, y, z;
  798. int health;
  799. int max_health;
  800. int owner_id;
  801. Game::Units::SpawnType spawn_type;
  802. };
  803. std::vector<UnitData> original_units = {
  804. {10.0F, 0.0F, 20.0F, 80, 100, 1, Game::Units::SpawnType::Archer},
  805. {15.5F, 1.0F, 25.5F, 45, 100, 1, Game::Units::SpawnType::Spearman},
  806. {30.0F, 0.0F, 40.0F, 100, 100, 2, Game::Units::SpawnType::Knight},
  807. {35.5F, 2.0F, 45.5F, 60, 150, 2, Game::Units::SpawnType::HorseArcher},
  808. {50.0F, 0.5F, 60.0F, 25, 80, 1, Game::Units::SpawnType::Catapult},
  809. };
  810. std::vector<EntityID> entity_ids;
  811. for (const auto &unit_data : original_units) {
  812. auto *entity = world->create_entity();
  813. entity_ids.push_back(entity->get_id());
  814. auto *transform = entity->add_component<TransformComponent>();
  815. transform->position.x = unit_data.x;
  816. transform->position.y = unit_data.y;
  817. transform->position.z = unit_data.z;
  818. auto *unit = entity->add_component<UnitComponent>();
  819. unit->health = unit_data.health;
  820. unit->max_health = unit_data.max_health;
  821. unit->owner_id = unit_data.owner_id;
  822. unit->spawn_type = unit_data.spawn_type;
  823. }
  824. // Serialize and deserialize the world
  825. QJsonDocument doc = Serialization::serialize_world(world.get());
  826. auto restored_world = std::make_unique<World>();
  827. Serialization::deserialize_world(restored_world.get(), doc);
  828. // Verify all units are restored with exact positions and health
  829. const auto &entities = restored_world->get_entities();
  830. EXPECT_EQ(entities.size(), original_units.size());
  831. for (size_t i = 0; i < entity_ids.size(); ++i) {
  832. auto *entity = restored_world->get_entity(entity_ids[i]);
  833. ASSERT_NE(entity, nullptr) << "Entity " << i << " not found";
  834. auto *transform = entity->get_component<TransformComponent>();
  835. ASSERT_NE(transform, nullptr);
  836. EXPECT_FLOAT_EQ(transform->position.x, original_units[i].x)
  837. << "Unit " << i << " X position mismatch";
  838. EXPECT_FLOAT_EQ(transform->position.y, original_units[i].y)
  839. << "Unit " << i << " Y position mismatch";
  840. EXPECT_FLOAT_EQ(transform->position.z, original_units[i].z)
  841. << "Unit " << i << " Z position mismatch";
  842. auto *unit = entity->get_component<UnitComponent>();
  843. ASSERT_NE(unit, nullptr);
  844. EXPECT_EQ(unit->health, original_units[i].health)
  845. << "Unit " << i << " health mismatch";
  846. EXPECT_EQ(unit->max_health, original_units[i].max_health)
  847. << "Unit " << i << " max_health mismatch";
  848. EXPECT_EQ(unit->owner_id, original_units[i].owner_id)
  849. << "Unit " << i << " owner_id mismatch";
  850. EXPECT_EQ(unit->spawn_type, original_units[i].spawn_type)
  851. << "Unit " << i << " spawn_type mismatch";
  852. }
  853. }
  854. TEST_F(SerializationTest, OwnerRegistryTeamsAndColorsPreserved) {
  855. // Setup owner registry with teams and custom colors
  856. auto &registry = Game::Systems::OwnerRegistry::instance();
  857. registry.clear();
  858. // Register players with specific teams and colors
  859. int player1 =
  860. registry.register_owner(Game::Systems::OwnerType::Player, "Blue Kingdom");
  861. int player2 =
  862. registry.register_owner(Game::Systems::OwnerType::AI, "Red Empire");
  863. int player3 = registry.register_owner(Game::Systems::OwnerType::Player,
  864. "Green Alliance");
  865. // Set teams (player1 and player3 are allies)
  866. registry.set_owner_team(player1, 1);
  867. registry.set_owner_team(player2, 2);
  868. registry.set_owner_team(player3, 1);
  869. // Set custom colors
  870. registry.set_owner_color(player1, 0.1F, 0.2F, 0.9F);
  871. registry.set_owner_color(player2, 0.9F, 0.1F, 0.1F);
  872. registry.set_owner_color(player3, 0.1F, 0.9F, 0.2F);
  873. registry.set_local_player_id(player1);
  874. // Create some entities owned by these players
  875. for (int i = 0; i < 3; ++i) {
  876. auto *entity = world->create_entity();
  877. auto *unit = entity->add_component<UnitComponent>();
  878. unit->owner_id = player1;
  879. }
  880. for (int i = 0; i < 2; ++i) {
  881. auto *entity = world->create_entity();
  882. auto *unit = entity->add_component<UnitComponent>();
  883. unit->owner_id = player2;
  884. }
  885. // Serialize world (includes owner_registry)
  886. QJsonDocument doc = Serialization::serialize_world(world.get());
  887. // Clear registry and restore
  888. registry.clear();
  889. auto restored_world = std::make_unique<World>();
  890. Serialization::deserialize_world(restored_world.get(), doc);
  891. // Verify owner registry state is preserved
  892. EXPECT_EQ(registry.get_local_player_id(), player1);
  893. // Verify teams are preserved
  894. EXPECT_EQ(registry.get_owner_team(player1), 1);
  895. EXPECT_EQ(registry.get_owner_team(player2), 2);
  896. EXPECT_EQ(registry.get_owner_team(player3), 1);
  897. // Verify alliances are preserved
  898. EXPECT_TRUE(registry.are_allies(player1, player3));
  899. EXPECT_TRUE(registry.are_enemies(player1, player2));
  900. EXPECT_TRUE(registry.are_enemies(player2, player3));
  901. // Verify colors are preserved
  902. auto color1 = registry.get_owner_color(player1);
  903. EXPECT_FLOAT_EQ(color1[0], 0.1F);
  904. EXPECT_FLOAT_EQ(color1[1], 0.2F);
  905. EXPECT_FLOAT_EQ(color1[2], 0.9F);
  906. auto color2 = registry.get_owner_color(player2);
  907. EXPECT_FLOAT_EQ(color2[0], 0.9F);
  908. EXPECT_FLOAT_EQ(color2[1], 0.1F);
  909. EXPECT_FLOAT_EQ(color2[2], 0.1F);
  910. auto color3 = registry.get_owner_color(player3);
  911. EXPECT_FLOAT_EQ(color3[0], 0.1F);
  912. EXPECT_FLOAT_EQ(color3[1], 0.9F);
  913. EXPECT_FLOAT_EQ(color3[2], 0.2F);
  914. // Verify owner names are preserved
  915. EXPECT_EQ(registry.get_owner_name(player1), "Blue Kingdom");
  916. EXPECT_EQ(registry.get_owner_name(player2), "Red Empire");
  917. EXPECT_EQ(registry.get_owner_name(player3), "Green Alliance");
  918. // Verify owner types are preserved
  919. EXPECT_TRUE(registry.is_player(player1));
  920. EXPECT_TRUE(registry.is_ai(player2));
  921. EXPECT_TRUE(registry.is_player(player3));
  922. // Clean up
  923. registry.clear();
  924. }
  925. TEST_F(SerializationTest, BuildingOwnershipAndCaptureStatePreserved) {
  926. // Create buildings (barracks/villages) with different ownership and capture
  927. // states
  928. struct BuildingData {
  929. float x, z;
  930. int owner_id;
  931. int capturing_player_id;
  932. float capture_progress;
  933. bool is_being_captured;
  934. };
  935. std::vector<BuildingData> buildings = {
  936. {100.0F, 100.0F, 1, -1, 0.0F,
  937. false}, // Owned by player 1, not being captured
  938. {200.0F, 200.0F, 2, 1, 7.5F,
  939. true}, // Owned by player 2, being captured by player 1
  940. {300.0F, 300.0F, 1, 2, 15.0F,
  941. true}, // Owned by player 1, being captured by player 2
  942. {400.0F, 400.0F, -1, -1, 0.0F, false}, // Neutral building
  943. };
  944. std::vector<EntityID> building_ids;
  945. for (const auto &bldg : buildings) {
  946. auto *entity = world->create_entity();
  947. building_ids.push_back(entity->get_id());
  948. auto *transform = entity->add_component<TransformComponent>();
  949. transform->position.x = bldg.x;
  950. transform->position.z = bldg.z;
  951. entity->add_component<BuildingComponent>();
  952. auto *unit = entity->add_component<UnitComponent>();
  953. unit->owner_id = bldg.owner_id;
  954. auto *capture = entity->add_component<CaptureComponent>();
  955. capture->capturing_player_id = bldg.capturing_player_id;
  956. capture->capture_progress = bldg.capture_progress;
  957. capture->is_being_captured = bldg.is_being_captured;
  958. }
  959. // Serialize and restore
  960. QJsonDocument doc = Serialization::serialize_world(world.get());
  961. auto restored_world = std::make_unique<World>();
  962. Serialization::deserialize_world(restored_world.get(), doc);
  963. // Verify all buildings are restored with correct ownership and capture state
  964. for (size_t i = 0; i < building_ids.size(); ++i) {
  965. auto *entity = restored_world->get_entity(building_ids[i]);
  966. ASSERT_NE(entity, nullptr) << "Building " << i << " not found";
  967. auto *transform = entity->get_component<TransformComponent>();
  968. ASSERT_NE(transform, nullptr);
  969. EXPECT_FLOAT_EQ(transform->position.x, buildings[i].x)
  970. << "Building " << i << " X position mismatch";
  971. EXPECT_FLOAT_EQ(transform->position.z, buildings[i].z)
  972. << "Building " << i << " Z position mismatch";
  973. EXPECT_NE(entity->get_component<BuildingComponent>(), nullptr);
  974. auto *unit = entity->get_component<UnitComponent>();
  975. ASSERT_NE(unit, nullptr);
  976. EXPECT_EQ(unit->owner_id, buildings[i].owner_id)
  977. << "Building " << i << " owner mismatch";
  978. auto *capture = entity->get_component<CaptureComponent>();
  979. ASSERT_NE(capture, nullptr);
  980. EXPECT_EQ(capture->capturing_player_id, buildings[i].capturing_player_id)
  981. << "Building " << i << " capturing_player_id mismatch";
  982. EXPECT_FLOAT_EQ(capture->capture_progress, buildings[i].capture_progress)
  983. << "Building " << i << " capture_progress mismatch";
  984. EXPECT_EQ(capture->is_being_captured, buildings[i].is_being_captured)
  985. << "Building " << i << " is_being_captured mismatch";
  986. }
  987. }
  988. TEST_F(SerializationTest, UnitMovementStatePreserved) {
  989. // Create units with active movement paths
  990. auto *entity = world->create_entity();
  991. auto entity_id = entity->get_id();
  992. auto *transform = entity->add_component<TransformComponent>();
  993. transform->position.x = 10.0F;
  994. transform->position.y = 0.0F;
  995. transform->position.z = 20.0F;
  996. auto *unit = entity->add_component<UnitComponent>();
  997. unit->owner_id = 1;
  998. unit->health = 85;
  999. auto *movement = entity->add_component<MovementComponent>();
  1000. movement->has_target = true;
  1001. movement->target_x = 50.0F;
  1002. movement->target_y = 60.0F;
  1003. movement->goal_x = 55.0F;
  1004. movement->goal_y = 65.0F;
  1005. movement->vx = 2.5F;
  1006. movement->vz = 3.0F;
  1007. // Add path waypoints
  1008. movement->path.emplace_back(20.0F, 30.0F);
  1009. movement->path.emplace_back(35.0F, 45.0F);
  1010. movement->path.emplace_back(50.0F, 60.0F);
  1011. const size_t expected_path_size = movement->path.size();
  1012. // Serialize and restore
  1013. QJsonDocument doc = Serialization::serialize_world(world.get());
  1014. auto restored_world = std::make_unique<World>();
  1015. Serialization::deserialize_world(restored_world.get(), doc);
  1016. // Verify movement state is preserved
  1017. auto *restored_entity = restored_world->get_entity(entity_id);
  1018. ASSERT_NE(restored_entity, nullptr);
  1019. auto *restored_movement = restored_entity->get_component<MovementComponent>();
  1020. ASSERT_NE(restored_movement, nullptr);
  1021. EXPECT_TRUE(restored_movement->has_target);
  1022. EXPECT_FLOAT_EQ(restored_movement->target_x, 50.0F);
  1023. EXPECT_FLOAT_EQ(restored_movement->target_y, 60.0F);
  1024. EXPECT_FLOAT_EQ(restored_movement->goal_x, 55.0F);
  1025. EXPECT_FLOAT_EQ(restored_movement->goal_y, 65.0F);
  1026. EXPECT_FLOAT_EQ(restored_movement->vx, 2.5F);
  1027. EXPECT_FLOAT_EQ(restored_movement->vz, 3.0F);
  1028. // Verify path is preserved
  1029. ASSERT_EQ(restored_movement->path.size(), expected_path_size);
  1030. EXPECT_FLOAT_EQ(restored_movement->path[0].first, 20.0F);
  1031. EXPECT_FLOAT_EQ(restored_movement->path[0].second, 30.0F);
  1032. EXPECT_FLOAT_EQ(restored_movement->path[1].first, 35.0F);
  1033. EXPECT_FLOAT_EQ(restored_movement->path[1].second, 45.0F);
  1034. EXPECT_FLOAT_EQ(restored_movement->path[2].first, 50.0F);
  1035. EXPECT_FLOAT_EQ(restored_movement->path[2].second, 60.0F);
  1036. }
  1037. TEST_F(SerializationTest, CombatStatePreserved) {
  1038. // Create units engaged in combat
  1039. auto *attacker = world->create_entity();
  1040. auto *defender = world->create_entity();
  1041. auto attacker_id = attacker->get_id();
  1042. auto defender_id = defender->get_id();
  1043. // Setup attacker
  1044. auto *attacker_transform = attacker->add_component<TransformComponent>();
  1045. attacker_transform->position.x = 10.0F;
  1046. attacker_transform->position.z = 10.0F;
  1047. auto *attacker_unit = attacker->add_component<UnitComponent>();
  1048. attacker_unit->owner_id = 1;
  1049. attacker_unit->health = 90;
  1050. auto *attacker_attack = attacker->add_component<AttackComponent>();
  1051. attacker_attack->damage = 25;
  1052. attacker_attack->range = 15.0F;
  1053. attacker_attack->current_mode = AttackComponent::CombatMode::Melee;
  1054. attacker_attack->in_melee_lock = true;
  1055. attacker_attack->melee_lock_target_id = defender_id;
  1056. auto *attacker_target = attacker->add_component<AttackTargetComponent>();
  1057. attacker_target->target_id = defender_id;
  1058. attacker_target->should_chase = true;
  1059. // Setup defender
  1060. auto *defender_transform = defender->add_component<TransformComponent>();
  1061. defender_transform->position.x = 12.0F;
  1062. defender_transform->position.z = 12.0F;
  1063. auto *defender_unit = defender->add_component<UnitComponent>();
  1064. defender_unit->owner_id = 2;
  1065. defender_unit->health = 60;
  1066. auto *defender_attack = defender->add_component<AttackComponent>();
  1067. defender_attack->damage = 20;
  1068. defender_attack->in_melee_lock = true;
  1069. defender_attack->melee_lock_target_id = attacker_id;
  1070. // Serialize and restore
  1071. QJsonDocument doc = Serialization::serialize_world(world.get());
  1072. auto restored_world = std::make_unique<World>();
  1073. Serialization::deserialize_world(restored_world.get(), doc);
  1074. // Verify combat state is preserved
  1075. auto *restored_attacker = restored_world->get_entity(attacker_id);
  1076. auto *restored_defender = restored_world->get_entity(defender_id);
  1077. ASSERT_NE(restored_attacker, nullptr);
  1078. ASSERT_NE(restored_defender, nullptr);
  1079. auto *restored_attacker_attack =
  1080. restored_attacker->get_component<AttackComponent>();
  1081. ASSERT_NE(restored_attacker_attack, nullptr);
  1082. EXPECT_TRUE(restored_attacker_attack->in_melee_lock);
  1083. EXPECT_EQ(restored_attacker_attack->melee_lock_target_id, defender_id);
  1084. EXPECT_EQ(restored_attacker_attack->current_mode,
  1085. AttackComponent::CombatMode::Melee);
  1086. auto *restored_attacker_target =
  1087. restored_attacker->get_component<AttackTargetComponent>();
  1088. ASSERT_NE(restored_attacker_target, nullptr);
  1089. EXPECT_EQ(restored_attacker_target->target_id, defender_id);
  1090. EXPECT_TRUE(restored_attacker_target->should_chase);
  1091. auto *restored_defender_attack =
  1092. restored_defender->get_component<AttackComponent>();
  1093. ASSERT_NE(restored_defender_attack, nullptr);
  1094. EXPECT_TRUE(restored_defender_attack->in_melee_lock);
  1095. EXPECT_EQ(restored_defender_attack->melee_lock_target_id, attacker_id);
  1096. // Verify health is preserved
  1097. auto *restored_attacker_unit =
  1098. restored_attacker->get_component<UnitComponent>();
  1099. auto *restored_defender_unit =
  1100. restored_defender->get_component<UnitComponent>();
  1101. EXPECT_EQ(restored_attacker_unit->health, 90);
  1102. EXPECT_EQ(restored_defender_unit->health, 60);
  1103. }
  1104. TEST_F(SerializationTest, NationIdentityPreserved) {
  1105. // Create units from different nations
  1106. auto *roman_unit = world->create_entity();
  1107. auto *carthage_unit = world->create_entity();
  1108. auto roman_id = roman_unit->get_id();
  1109. auto carthage_id = carthage_unit->get_id();
  1110. auto *roman_unit_comp = roman_unit->add_component<UnitComponent>();
  1111. roman_unit_comp->nation_id = Game::Systems::NationID::RomanRepublic;
  1112. roman_unit_comp->spawn_type = Game::Units::SpawnType::Spearman;
  1113. auto *carthage_unit_comp = carthage_unit->add_component<UnitComponent>();
  1114. carthage_unit_comp->nation_id = Game::Systems::NationID::Carthage;
  1115. carthage_unit_comp->spawn_type = Game::Units::SpawnType::Archer;
  1116. // Serialize and restore
  1117. QJsonDocument doc = Serialization::serialize_world(world.get());
  1118. auto restored_world = std::make_unique<World>();
  1119. Serialization::deserialize_world(restored_world.get(), doc);
  1120. // Verify nation IDs are preserved
  1121. auto *restored_roman = restored_world->get_entity(roman_id);
  1122. auto *restored_carthage = restored_world->get_entity(carthage_id);
  1123. auto *restored_roman_comp = restored_roman->get_component<UnitComponent>();
  1124. EXPECT_EQ(restored_roman_comp->nation_id,
  1125. Game::Systems::NationID::RomanRepublic);
  1126. EXPECT_EQ(restored_roman_comp->spawn_type, Game::Units::SpawnType::Spearman);
  1127. auto *restored_carthage_comp =
  1128. restored_carthage->get_component<UnitComponent>();
  1129. EXPECT_EQ(restored_carthage_comp->nation_id,
  1130. Game::Systems::NationID::Carthage);
  1131. EXPECT_EQ(restored_carthage_comp->spawn_type, Game::Units::SpawnType::Archer);
  1132. }