From 2546c6c226272211e10999282a11f770fb1c30e7 Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Sun, 30 Nov 2014 18:10:30 -0800 Subject: [PATCH] Trap converted to use xyz_location as m_Position instead of x, y, z --- zone/client_packet.cpp | 22 ++++++------- zone/entity.cpp | 48 ++++++++++++--------------- zone/position.cpp | 10 ++++++ zone/position.h | 1 + zone/trap.cpp | 74 ++++++++++++++++++------------------------ zone/trap.h | 4 +-- 6 files changed, 74 insertions(+), 85 deletions(-) diff --git a/zone/client_packet.cpp b/zone/client_packet.cpp index 62f4f12dd..958834784 100644 --- a/zone/client_packet.cpp +++ b/zone/client_packet.cpp @@ -11699,29 +11699,29 @@ void Client::Handle_OP_SenseTraps(const EQApplicationPacket *app) int uskill = GetSkill(SkillSenseTraps); if ((MakeRandomInt(0, 99) + uskill) >= (MakeRandomInt(0, 99) + trap->skill*0.75)) { - float xdif = trap->x - GetX(); - float ydif = trap->y - GetY(); - if (xdif == 0 && ydif == 0) + auto diff = trap->m_Position - GetPosition(); + + if (diff.m_X == 0 && diff.m_Y == 0) Message(MT_Skills, "You sense a trap right under your feet!"); - else if (xdif > 10 && ydif > 10) + else if (diff.m_X > 10 && diff.m_Y > 10) Message(MT_Skills, "You sense a trap to the NorthWest."); - else if (xdif < -10 && ydif > 10) + else if (diff.m_X < -10 && diff.m_Y > 10) Message(MT_Skills, "You sense a trap to the NorthEast."); - else if (ydif > 10) + else if (diff.m_Y > 10) Message(MT_Skills, "You sense a trap to the North."); - else if (xdif > 10 && ydif < -10) + else if (diff.m_X > 10 && diff.m_Y < -10) Message(MT_Skills, "You sense a trap to the SouthWest."); - else if (xdif < -10 && ydif < -10) + else if (diff.m_X < -10 && diff.m_Y < -10) Message(MT_Skills, "You sense a trap to the SouthEast."); - else if (ydif < -10) + else if (diff.m_Y < -10) Message(MT_Skills, "You sense a trap to the South."); - else if (xdif > 10) + else if (diff.m_X > 10) Message(MT_Skills, "You sense a trap to the West."); else Message(MT_Skills, "You sense a trap to the East."); trap->detected = true; - float angle = CalculateHeadingToTarget(trap->x, trap->y); + float angle = CalculateHeadingToTarget(trap->m_Position.m_X, trap->m_Position.m_Y); if (angle < 0) angle = (256 + angle); diff --git a/zone/entity.cpp b/zone/entity.cpp index 122e24582..792deaad4 100644 --- a/zone/entity.cpp +++ b/zone/entity.cpp @@ -68,7 +68,7 @@ Entity::Entity() Entity::~Entity() { - + } Client *Entity::CastToClient() @@ -493,14 +493,14 @@ void EntityList::MobProcess() while (it != mob_list.end()) { uint16 id = it->first; Mob *mob = it->second; - + size_t sz = mob_list.size(); bool p_val = mob->Process(); size_t a_sz = mob_list.size(); - + if(a_sz > sz) { //increased size can potentially screw with iterators so reset it to current value - //if buckets are re-orderered we may skip a process here and there but since + //if buckets are re-orderered we may skip a process here and there but since //process happens so often it shouldn't matter much it = mob_list.find(id); ++it; @@ -3106,33 +3106,25 @@ void EntityList::OpenDoorsNear(NPC *who) void EntityList::SendAlarm(Trap *trap, Mob *currenttarget, uint8 kos) { - float val2 = trap->effectvalue * trap->effectvalue; + float preSquareDistance = trap->effectvalue * trap->effectvalue; - auto it = npc_list.begin(); - while (it != npc_list.end()) { + for (auto it = npc_list.begin();it != npc_list.end(); ++it) { NPC *cur = it->second; - float curdist = 0; - float tmp = cur->GetX() - trap->x; - curdist += tmp*tmp; - tmp = cur->GetY() - trap->y; - curdist += tmp*tmp; - tmp = cur->GetZ() - trap->z; - curdist += tmp*tmp; - if (!cur->GetOwner() && - /*!cur->CastToMob()->dead && */ - !cur->IsEngaged() && - curdist <= val2 ) - { - if (kos) { - uint8 factioncon = currenttarget->GetReverseFactionCon(cur); - if (factioncon == FACTION_THREATENLY || factioncon == FACTION_SCOWLS) { - cur->AddToHateList(currenttarget,1); - } - } else { + + auto diff = cur->GetPosition() - trap->m_Position; + float curdist = diff.m_X * diff.m_X + diff.m_Y * diff.m_Y + diff.m_Z * diff.m_Z; + + if (cur->GetOwner() || cur->IsEngaged() || curdist > preSquareDistance ) + continue; + + if (kos) { + uint8 factioncon = currenttarget->GetReverseFactionCon(cur); + if (factioncon == FACTION_THREATENLY || factioncon == FACTION_SCOWLS) { cur->AddToHateList(currenttarget,1); } - } - ++it; + } + else + cur->AddToHateList(currenttarget,1); } } @@ -3636,7 +3628,7 @@ int16 EntityList::CountTempPets(Mob *owner) } ++it; } - + owner->SetTempPetCount(count); return count; diff --git a/zone/position.cpp b/zone/position.cpp index 2d7d48cf8..887949f41 100644 --- a/zone/position.cpp +++ b/zone/position.cpp @@ -67,6 +67,16 @@ const xyz_heading xyz_heading::operator -(const xyz_location& rhs) const{ return xyz_heading(m_X - rhs.m_X, m_Y - rhs.m_Y, m_Z - rhs.m_Z, m_Heading); } +void xyz_heading::ABS_XYZ(void) { + if (m_X < 0) + m_X = -m_X; + + if (m_Y < 0) + m_Y = -m_Y; + + if (m_Z < 0) + m_Z = -m_Z; +} xyz_location::xyz_location(float x, float y, float z) : m_X(x), diff --git a/zone/position.h b/zone/position.h index 591c14b10..653cd4ba6 100644 --- a/zone/position.h +++ b/zone/position.h @@ -74,6 +74,7 @@ public: const xyz_heading operator -(const xyz_location& rhs) const; + void ABS_XYZ(); bool isOrigin() const { return m_X == 0.0f && m_Y == 0.0f && m_Z == 0.0f;} }; diff --git a/zone/trap.cpp b/zone/trap.cpp index b4a4a1f1f..7d53bcc70 100644 --- a/zone/trap.cpp +++ b/zone/trap.cpp @@ -50,12 +50,10 @@ CREATE TABLE traps ( Trap::Trap() : Entity(), respawn_timer(600000), - chkarea_timer(500) + chkarea_timer(500), + m_Position(xyz_location::Origin()) { trap_id = 0; - x = 0; - y = 0; - z = 0; maxzdiff = 0; radius = 0; effect = 0; @@ -145,8 +143,8 @@ void Trap::Trigger(Mob* trigger) if ((tmp = database.GetNPCType(effectvalue))) { auto randomOffset = xyz_heading(-5 + MakeRandomInt(0, 10),-5 + MakeRandomInt(0, 10),-5 + MakeRandomInt(0, 10), MakeRandomInt(0, 249)); - auto position = randomOffset + xyz_location(x, y, z); - NPC* new_npc = new NPC(tmp, nullptr, position, FlyMode3); + auto spawnPosition = randomOffset + m_Position; + NPC* new_npc = new NPC(tmp, nullptr, spawnPosition, FlyMode3); new_npc->AddLootTable(); entity_list.AddNPC(new_npc); new_npc->AddToHateList(trigger,1); @@ -168,8 +166,8 @@ void Trap::Trigger(Mob* trigger) if ((tmp = database.GetNPCType(effectvalue))) { auto randomOffset = xyz_heading(-2 + MakeRandomInt(0, 5), -2 + MakeRandomInt(0, 5), -2 + MakeRandomInt(0, 5), MakeRandomInt(0, 249)); - auto position = randomOffset + xyz_location(x, y, z); - NPC* new_npc = new NPC(tmp, nullptr, position, FlyMode3); + auto spawnPosition = randomOffset + m_Position; + NPC* new_npc = new NPC(tmp, nullptr, spawnPosition, FlyMode3); new_npc->AddLootTable(); entity_list.AddNPC(new_npc); new_npc->AddToHateList(trigger,1); @@ -212,55 +210,47 @@ Trap* EntityList::FindNearbyTrap(Mob* searcher, float max_dist) { float max_dist2 = max_dist*max_dist; Trap *cur; - auto it = trap_list.begin(); - while (it != trap_list.end()) { - cur = it->second; - if(!cur->disarmed) { - float curdist = 0; - float tmp = searcher->GetX() - cur->x; - curdist += tmp*tmp; - tmp = searcher->GetY() - cur->y; - curdist += tmp*tmp; - tmp = searcher->GetZ() - cur->z; - curdist += tmp*tmp; - if (curdist < max_dist2 && curdist < dist) - { - dist = curdist; - current_trap = cur; - } - } - ++it; + for (auto it = trap_list.begin(); it != trap_list.end(); ++it) { + cur = it->second; + if(cur->disarmed) + continue; + + auto diff = searcher->GetPosition() - cur->m_Position; + float curdist = diff.m_X * diff.m_X + diff.m_Y * diff.m_Y + diff.m_Z * diff.m_Z; + + if (curdist < max_dist2 && curdist < dist) + { + dist = curdist; + current_trap = cur; + } } + return current_trap; } Mob* EntityList::GetTrapTrigger(Trap* trap) { Mob* savemob = 0; - float xdiff, ydiff, zdiff; - float maxdist = trap->radius * trap->radius; - auto it = client_list.begin(); - while (it != client_list.end()) { + for (auto it = client_list.begin(); it != client_list.end(); ++it) { Client* cur = it->second; - zdiff = cur->GetZ() - trap->z; - if(zdiff < 0) - zdiff = 0 - zdiff; - xdiff = cur->GetX() - trap->x; - ydiff = cur->GetY() - trap->y; - if ((xdiff*xdiff + ydiff*ydiff) <= maxdist - && zdiff < trap->maxzdiff) + auto diff = cur->GetPosition() - trap->m_Position; + diff.ABS_XYZ(); + + if ((diff.m_X*diff.m_X + diff.m_Y*diff.m_Y) <= maxdist + && diff.m_Z < trap->maxzdiff) { if (MakeRandomInt(0,100) < trap->chance) - return(cur); + return cur; else savemob = cur; } - ++it; + } + return savemob; } @@ -279,9 +269,7 @@ bool ZoneDatabase::LoadTraps(const char* zonename, int16 version) { for (auto row = results.begin(); row != results.end(); ++row) { Trap* trap = new Trap(); trap->trap_id = atoi(row[0]); - trap->x = atof(row[1]); - trap->y = atof(row[2]); - trap->z = atof(row[3]); + trap->m_Position = xyz_location(atof(row[1]), atof(row[2]), atof(row[3])); trap->effect = atoi(row[4]); trap->effectvalue = atoi(row[5]); trap->effectvalue2 = atoi(row[6]); @@ -322,7 +310,7 @@ void Trap::CreateHiddenTrigger() make_npc->trackable = 0; make_npc->level = level; strcpy(make_npc->special_abilities, "19,1^20,1^24,1^25,1"); - NPC* npca = new NPC(make_npc, nullptr, xyz_heading(x, y, z, 0.0f), FlyMode3); + NPC* npca = new NPC(make_npc, nullptr, xyz_heading(m_Position, 0.0f), FlyMode3); npca->GiveNPCTypeData(make_npc); entity_list.AddNPC(npca); diff --git a/zone/trap.h b/zone/trap.h index ad2e2171a..ac1925e41 100644 --- a/zone/trap.h +++ b/zone/trap.h @@ -53,9 +53,7 @@ public: Timer respawn_timer; //Respawn Time when Trap's been disarmed Timer chkarea_timer; uint32 trap_id; //Database ID of trap - float x; //X position - float y; //Y position - float z; //Z position + xyz_location m_Position; float maxzdiff; //maximum z diff to be triggerable float radius; //radius around trap to be triggerable uint8 chance; //%chance that the trap is triggered each 'tick'