#include "SpawnPetBaseServer.h" #include "GameMessages.h" #include "EntityManager.h" #include "PetComponent.h" #include "EntityInfo.h" #include "eTerminateType.h" void SpawnPetBaseServer::OnStartup(Entity* self) { SetVariables(self); self->SetVar(u"spawnedPets", ""); } void SpawnPetBaseServer::OnUse(Entity* self, Entity* user) { auto possibleSpawners = EntityManager::Instance()->GetEntitiesInGroup(self->GetVar(u"petType") + "Spawner"); if (possibleSpawners.empty()) return; if (!CheckNumberOfPets(self, user)) return; auto* spawner = possibleSpawners.at(0); auto petType = GeneralUtils::ASCIIToUTF16(self->GetVar(u"petType")); EntityInfo info{}; info.pos = spawner->GetPosition(); info.rot = spawner->GetRotation(); info.lot = self->GetVar(u"petLOT"); info.spawnerID = self->GetObjectID(); info.settings = { new LDFData(u"tamer", user->GetObjectID()), new LDFData(u"groupID", petType + (GeneralUtils::to_u16string(user->GetObjectID())) + u";" + petType + u"s"), new LDFData(u"spawnAnim", self->GetVar(u"spawnAnim")), new LDFData(u"spawnTimer", 1.0f) }; auto* pet = EntityManager::Instance()->CreateEntity(info); EntityManager::Instance()->ConstructEntity(pet); self->SetVar(u"spawnedPets", self->GetVar(u"spawnedPets") + "," + std::to_string(pet->GetObjectID())); auto spawnCinematic = self->GetVar(u"spawnCinematic"); if (!spawnCinematic.empty()) { GameMessages::SendPlayCinematic(user->GetObjectID(), spawnCinematic, UNASSIGNED_SYSTEM_ADDRESS); } GameMessages::SendTerminateInteraction(user->GetObjectID(), eTerminateType::FROM_INTERACTION, self->GetObjectID()); } bool SpawnPetBaseServer::CheckNumberOfPets(Entity* self, Entity* user) { auto petIDString = self->GetVar(u"spawnedPets"); auto petIDs = GeneralUtils::SplitString(petIDString, ','); // Check all the pets that were tamed in the process or were smashed std::vector petsToKeep{}; for (const auto& petID : petIDs) { if (petID.empty()) continue; const auto* spawnedPet = EntityManager::Instance()->GetEntity(std::stoull(petID)); if (spawnedPet == nullptr) continue; const auto petComponent = spawnedPet->GetComponent(); if (petComponent == nullptr || petComponent->GetOwner() != nullptr) continue; // Each user can only spawn one pet if (spawnedPet->GetVar(u"tamer") == user->GetObjectID()) return false; petsToKeep.push_back(spawnedPet->GetObjectID()); } self->SetNetworkVar(u"TooManyPets", petsToKeep.size() >= self->GetVar(u"maxPets")); std::string newPetIDs; for (const auto petID : petsToKeep) { newPetIDs += (std::to_string(petID) + ","); } self->SetVar(u"spawnedPets", newPetIDs); return petsToKeep.size() < self->GetVar(u"maxPets"); }