From 55e9f6531d49fd3662b0cf1025e75157235a4ec6 Mon Sep 17 00:00:00 2001 From: dongresource Date: Wed, 20 Oct 2021 00:30:53 +0200 Subject: [PATCH] [refactor] Remove redundant coord args from most entity constructors Mobs and CombatNPCs still need theirs in order to properly set their roaming and spawn coords. Assignment of the latter has been moved to the CombatNPC constructor, where it should have already been. --- src/Chunking.cpp | 3 +-- src/CustomCommands.cpp | 17 ++++++++++++++--- src/Entities.hpp | 23 +++++++++++------------ src/MobAI.hpp | 6 +++--- src/NPCManager.cpp | 5 ++--- src/TableData.cpp | 12 ++++++------ 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Chunking.cpp b/src/Chunking.cpp index 04b391f..cbb439b 100644 --- a/src/Chunking.cpp +++ b/src/Chunking.cpp @@ -310,8 +310,7 @@ void Chunking::createInstance(uint64_t instanceID) { NPCManager::updateNPCPosition(newMob->id, baseNPC->x, baseNPC->y, baseNPC->z, instanceID, baseNPC->angle); } else { - BaseNPC* newNPC = new BaseNPC(baseNPC->x, baseNPC->y, baseNPC->z, baseNPC->angle, - instanceID, baseNPC->type, NPCManager::nextId--); + BaseNPC* newNPC = new BaseNPC(baseNPC->angle, instanceID, baseNPC->type, NPCManager::nextId--); NPCManager::NPCs[newNPC->id] = newNPC; NPCManager::updateNPCPosition(newNPC->id, baseNPC->x, baseNPC->y, baseNPC->z, instanceID, baseNPC->angle); diff --git a/src/CustomCommands.cpp b/src/CustomCommands.cpp index d171bbb..2d92c21 100644 --- a/src/CustomCommands.cpp +++ b/src/CustomCommands.cpp @@ -532,7 +532,7 @@ static void eggCommand(std::string full, std::vector& args, CNSocke int addX = 0; //-500.0f * sin(plr->angle / 180.0f * M_PI); int addY = 0; //-500.0f * cos(plr->angle / 180.0f * M_PI); - Egg* egg = new Egg(plr->x + addX, plr->y + addY, plr->z, plr->instanceID, eggType, id, false); // change last arg to true after gruntwork + Egg* egg = new Egg(plr->instanceID, eggType, id, false); // change last arg to true after gruntwork NPCManager::NPCs[id] = egg; NPCManager::updateNPCPosition(id, plr->x + addX, plr->y + addY, plr->z, plr->instanceID, plr->angle); @@ -977,7 +977,13 @@ static void pathCommand(std::string full, std::vector& args, CNSock // add first point at NPC's current location std::vector pathPoints; - BaseNPC* marker = new BaseNPC(npc->x, npc->y, npc->z, 0, plr->instanceID, 1386, NPCManager::nextId--); + BaseNPC* marker = new BaseNPC(0, plr->instanceID, 1386, NPCManager::nextId--); + + // assign coords manually, since we aren't actually adding markers to the world + marker->x = npc->x; + marker->y = npc->y; + marker->z = npc->z; + pathPoints.push_back(marker); // map from player TableData::RunningNPCPaths[plr->iID] = std::make_pair(npc, pathPoints); @@ -998,7 +1004,12 @@ static void pathCommand(std::string full, std::vector& args, CNSock // /path kf if (args[1] == "kf") { - BaseNPC* marker = new BaseNPC(npc->x, npc->y, npc->z, 0, plr->instanceID, 1386, NPCManager::nextId--); + BaseNPC* marker = new BaseNPC(0, plr->instanceID, 1386, NPCManager::nextId--); + + marker->x = npc->x; + marker->y = npc->y; + marker->z = npc->z; + entry->second.push_back(marker); Chat::sendServerMessage(sock, "[PATH] Added keyframe"); updatePathMarkers(sock); diff --git a/src/Entities.hpp b/src/Entities.hpp index 9e56ea0..3e613ce 100644 --- a/src/Entities.hpp +++ b/src/Entities.hpp @@ -82,10 +82,7 @@ public: int barkerType; bool loopingPath = false; - BaseNPC(int _X, int _Y, int _Z, int _A, uint64_t iID, int t, int _id) { // XXX - x = _X; - y = _Y; - z = _Z; + BaseNPC(int _A, uint64_t iID, int t, int _id) { type = t; hp = 400; angle = _A; @@ -111,10 +108,12 @@ struct CombatNPC : public BaseNPC { void (*_stepAI)(CombatNPC*, time_t) = nullptr; - // XXX - CombatNPC(int x, int y, int z, int angle, uint64_t iID, int t, int id, int maxHP) : - BaseNPC(x, y, z, angle, iID, t, id), - maxHealth(maxHP) {} + CombatNPC(int x, int y, int z, int angle, uint64_t iID, int t, int id, int maxHP) + : BaseNPC(angle, iID, t, id), maxHealth(maxHP) { + spawnX = x; + spawnY = y; + spawnZ = z; + } virtual void stepAI(time_t currTime) { if (_stepAI != nullptr) @@ -132,8 +131,8 @@ struct Egg : public BaseNPC { bool dead = false; time_t deadUntil; - Egg(int x, int y, int z, uint64_t iID, int t, int32_t id, bool summon) - : BaseNPC(x, y, z, 0, iID, t, id) { + Egg(uint64_t iID, int t, int32_t id, bool summon) + : BaseNPC(0, iID, t, id) { summoned = summon; kind = EntityType::EGG; } @@ -146,8 +145,8 @@ struct Egg : public BaseNPC { // TODO: decouple from BaseNPC struct Bus : public BaseNPC { - Bus(int x, int y, int z, int angle, uint64_t iID, int t, int id) : - BaseNPC(x, y, z, angle, iID, t, id) { + Bus(int angle, uint64_t iID, int t, int id) : + BaseNPC(angle, iID, t, id) { kind = EntityType::BUS; loopingPath = true; } diff --git a/src/MobAI.hpp b/src/MobAI.hpp index dbf9c84..5eb3db8 100644 --- a/src/MobAI.hpp +++ b/src/MobAI.hpp @@ -65,9 +65,9 @@ struct Mob : public CombatNPC { idleRange = (int)data["m_iIdleRange"]; level = data["m_iNpcLevel"]; - roamX = spawnX = x; - roamY = spawnY = y; - roamZ = spawnZ = z; + roamX = x; + roamY = y; + roamZ = z; offsetX = 0; offsetY = 0; diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 50b3a0a..a0cb0c6 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -122,7 +122,6 @@ static void npcUnsummonHandler(CNSocket* sock, CNPacketData* data) { // type must already be checked and updateNPCPosition() must be called on the result BaseNPC *NPCManager::summonNPC(int x, int y, int z, uint64_t instance, int type, bool respawn, bool baseInstance) { uint64_t inst = baseInstance ? MAPNUM(instance) : instance; -#define EXTRA_HEIGHT 0 //assert(nextId < INT32_MAX); int id = nextId--; @@ -130,12 +129,12 @@ BaseNPC *NPCManager::summonNPC(int x, int y, int z, uint64_t instance, int type, BaseNPC *npc = nullptr; if (team == 2) { - npc = new Mob(x, y, z + EXTRA_HEIGHT, inst, type, NPCData[type], id); + npc = new Mob(x, y, z, inst, type, NPCData[type], id); // re-enable respawning, if desired ((Mob*)npc)->summoned = !respawn; } else - npc = new BaseNPC(x, y, z + EXTRA_HEIGHT, 0, inst, type, id); + npc = new BaseNPC(0, inst, type, id); NPCs[id] = npc; diff --git a/src/TableData.cpp b/src/TableData.cpp index 92a4aca..6b11c98 100644 --- a/src/TableData.cpp +++ b/src/TableData.cpp @@ -314,9 +314,9 @@ static void loadPaths(json& pathData, int32_t* nextId) { if (passedDistance >= SLIDER_GAP_SIZE) { // space them out uniformaly passedDistance -= SLIDER_GAP_SIZE; // step down // spawn a slider - Bus* slider = new Bus(point.x, point.y, point.z, 0, INSTANCE_OVERWORLD, 1, (*nextId)--); + Bus* slider = new Bus(0, INSTANCE_OVERWORLD, 1, (*nextId)--); NPCManager::NPCs[slider->id] = slider; - NPCManager::updateNPCPosition(slider->id, slider->x, slider->y, slider->z, INSTANCE_OVERWORLD, 0); + NPCManager::updateNPCPosition(slider->id, point.x, point.y, point.z, INSTANCE_OVERWORLD, 0); Transport::NPCQueues[slider->id] = route; } // rotate @@ -659,7 +659,7 @@ static void loadEggs(json& eggData, int32_t* nextId) { int id = (*nextId)--; uint64_t instanceID = egg.find("iMapNum") == egg.end() ? INSTANCE_OVERWORLD : (int)egg["iMapNum"]; - Egg* addEgg = new Egg((int)egg["iX"], (int)egg["iY"], (int)egg["iZ"], instanceID, (int)egg["iType"], id, false); + Egg* addEgg = new Egg(instanceID, (int)egg["iType"], id, false); NPCManager::NPCs[id] = addEgg; eggCount++; NPCManager::updateNPCPosition(id, (int)egg["iX"], (int)egg["iY"], (int)egg["iZ"], instanceID, 0); @@ -791,7 +791,7 @@ static void loadGruntworkPost(json& gruntwork, int32_t* nextId) { // re-enable respawning ((Mob*)npc)->summoned = false; } else { - npc = new BaseNPC(mob["iX"], mob["iY"], mob["iZ"], mob["iAngle"], instanceID, mob["iNPCType"], id); + npc = new BaseNPC(mob["iAngle"], instanceID, mob["iNPCType"], id); } NPCManager::NPCs[npc->id] = npc; @@ -859,7 +859,7 @@ static void loadGruntworkPost(json& gruntwork, int32_t* nextId) { int id = (*nextId)--; uint64_t instanceID = egg.find("iMapNum") == egg.end() ? INSTANCE_OVERWORLD : (int)egg["iMapNum"]; - Egg* addEgg = new Egg((int)egg["iX"], (int)egg["iY"], (int)egg["iZ"], instanceID, (int)egg["iType"], id, false); + Egg* addEgg = new Egg(instanceID, (int)egg["iType"], id, false); NPCManager::NPCs[id] = addEgg; NPCManager::updateNPCPosition(id, (int)egg["iX"], (int)egg["iY"], (int)egg["iZ"], instanceID, 0); RunningEggs[id] = addEgg; @@ -893,7 +893,7 @@ static void loadNPCs(json& npcData) { if (npc["iX"] > 512000 && npc["iY"] < 256000) continue; #endif - BaseNPC* tmp = new BaseNPC(npc["iX"], npc["iY"], npc["iZ"], npc["iAngle"], instanceID, type, npcID); + BaseNPC* tmp = new BaseNPC(npc["iAngle"], instanceID, type, npcID); NPCManager::NPCs[npcID] = tmp; NPCManager::updateNPCPosition(npcID, npc["iX"], npc["iY"], npc["iZ"], instanceID, npc["iAngle"]);