1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213 |
- #include "mob_movement_manager.h"
- #include "../Entity.h"
- #include "../zoneserver.h"
- #include "water_map.h"
- #include "map.h"
- #include "../../common/timer.h"
- #include "pathfinder_interface.h"
- #include "position.h"
- #include "../../common/Log.h"
- #include <vector>
- #include <deque>
- #include <map>
- #include <stdlib.h>
- #ifdef WIN32
- #include <windows.h>
- #endif
- extern double frame_time;
- class IMovementCommand {
- public:
- IMovementCommand() = default;
- virtual ~IMovementCommand() = default;
- virtual bool Process(MobMovementManager* mob_movement_manager, Spawn* mob) = 0;
- virtual bool Started() const = 0;
- };
- class RotateToCommand : public IMovementCommand {
- public:
- RotateToCommand(double rotate_to, double dir, MobMovementMode mob_movement_mode)
- {
- m_rotate_to = rotate_to;
- m_rotate_to_dir = dir;
- m_rotate_to_mode = mob_movement_mode;
- m_started = false;
- }
- virtual ~RotateToCommand()
- {
- }
- virtual bool Process(MobMovementManager* mob_movement_manager, Spawn* mob)
- {
- auto rotate_to_speed = m_rotate_to_mode == MovementRunning ? 200.0 : 16.0; //todo: get this from mob
- auto from = mob_movement_manager->FixHeading(mob->GetHeading());
- auto to = mob_movement_manager->FixHeading(m_rotate_to);
- auto diff = to - from;
- while (diff < -256.0) {
- diff += 512.0;
- }
- while (diff > 256) {
- diff -= 512.0;
- }
- auto dist = std::abs(diff);
- if (!m_started) {
- m_started = true;
- //mob->SetMoving(true);
- /*if (dist > 15.0f && rotate_to_speed > 0.0 && rotate_to_speed <= 25.0) { //send basic rotation
- mob_movement_manager->SendCommandToClients(
- mob,
- 0.0,
- 0.0,
- 0.0,
- m_rotate_to_dir * rotate_to_speed,
- 0,
- ClientRangeClose
- );
- }*/
- }
- auto td = rotate_to_speed * 19.0 * frame_time;
- if (td >= dist) {
- mob->SetHeading(to);
- //mob->SetMoving(false);
- //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, 0, ClientRangeCloseMedium);
- return true;
- }
- from += td * m_rotate_to_dir;
- mob->SetHeading(mob_movement_manager->FixHeading(from));
- return false;
- }
- virtual bool Started() const
- {
- return m_started;
- }
- private:
- double m_rotate_to;
- double m_rotate_to_dir;
- MobMovementMode m_rotate_to_mode;
- bool m_started;
- };
- class MoveToCommand : public IMovementCommand {
- public:
- MoveToCommand(float x, float y, float z, MobMovementMode mob_movement_mode)
- {
- m_distance_moved_since_correction = 0.0;
- m_move_to_x = x;
- m_move_to_y = y;
- m_move_to_z = z;
- m_move_to_mode = mob_movement_mode;
- m_last_sent_time = 0.0;
- m_last_sent_speed = 0;
- m_started = false;
- m_total_h_dist = 0.0;
- m_total_v_dist = 0.0;
- }
- virtual ~MoveToCommand()
- {
- }
- /**
- * @param mob_movement_manager
- * @param mob
- * @return
- */
- virtual bool Process(MobMovementManager* mob_movement_manager, Spawn* mob)
- {
- //Send a movement packet when you start moving
- double current_time = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
- int current_speed = 0;
- if (m_move_to_mode == MovementRunning) {
- current_speed = ((Spawn*)mob)->GetSpeed();
- }
- if (!m_started) {
- m_started = true;
- //rotate to the point
- //mob->SetMoving(true);
- mob->SetHeading(mob->GetFaceTarget(m_move_to_x, m_move_to_z));
- m_last_sent_speed = current_speed;
- m_last_sent_time = current_time;
- // Z/Y are flipped due to EverQuest 2 using Y as up/down
- m_total_h_dist = DistanceNoZ(glm::vec4(mob->GetX(),mob->GetZ(),mob->GetY(),mob->GetHeading()), glm::vec4(m_move_to_x, m_move_to_z, 0.0f, 0.0f));
- m_total_v_dist = m_move_to_y - mob->GetY();
- //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
- }
- //When speed changes
- if (current_speed != m_last_sent_speed) {
- //if (RuleB(Map, FixZWhenPathing)) {
- // mob->FixZ();
- //}
- m_distance_moved_since_correction = 0.0;
- m_last_sent_speed = current_speed;
- m_last_sent_time = current_time;
- //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
- }
- //If x seconds have passed without sending an update.
- if (current_time - m_last_sent_time >= 5.0) {
- //if (RuleB(Map, FixZWhenPathing)) {
- //mob->FixZ();
- //}
- m_distance_moved_since_correction = 0.0;
- m_last_sent_speed = current_speed;
- m_last_sent_time = current_time;
- //mob_movemesnt_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
- }
- glm::vec3 p = glm::vec3(mob->GetX(), mob->GetY(), mob->GetZ());
- // our X/Z versus the mobs X/Z
- glm::vec2 tar(m_move_to_x, m_move_to_z);
- glm::vec2 pos(p.x, p.z);
- double len = glm::distance(pos, tar);
- if (len < .01) {
- return true;
- }
- //mob->SetMoved(true);
- glm::vec2 dir = tar - pos;
- glm::vec2 ndir = glm::normalize(dir);
- double distance_moved = frame_time * current_speed * 0.4f * 1.45f;
- //mob->SetX(m_move_to_x);
- //mob->SetY(m_move_to_z);
- //mob->SetZ(m_move_to_y);
- mob->ClearRunningLocations();
- if (distance_moved > len) {
- //if (RuleB(Map, FixZWhenPathing)) {
- //mob->FixZ();
- //}
- // we use npos.y because higher up that is the equilvaent Z
- mob->AddRunningLocation(m_move_to_x, m_move_to_y, m_move_to_z, current_speed, distance_moved, true, true, "", true);
- return false;
- }
- else {
- glm::vec2 npos = pos + (ndir * static_cast<float>(distance_moved));
- len -= distance_moved;
- double total_distance_traveled = m_total_h_dist - len;
- double start_y = m_move_to_y - m_total_v_dist;
- double y_at_pos = start_y + (m_total_v_dist * (total_distance_traveled / m_total_h_dist));
- // we use npos.y because higher up that is the equilvaent Z
- mob->AddRunningLocation(m_move_to_x, m_move_to_y, m_move_to_z, current_speed, distance_moved, true, true, "", true);
- // mob->SetX(npos.x);
- // mob->SetY(z_at_pos);
- // mob->SetZ(npos.y);
- //if (RuleB(Map, FixZWhenPathing)) {
- // m_distance_moved_since_correction += distance_moved;
- // if (m_distance_moved_since_correction > 10.0f /*RuleR(Map, DistanceCanTravelBeforeAdjustment)*/) {
- // m_distance_moved_since_correction = 0.0;
- //mob->FixZ();
- //}
- // }
- }
- return false;
- }
- virtual bool Started() const
- {
- return m_started;
- }
- protected:
- double m_distance_moved_since_correction;
- double m_move_to_x;
- double m_move_to_y;
- double m_move_to_z;
- MobMovementMode m_move_to_mode;
- bool m_started;
- double m_last_sent_time;
- int m_last_sent_speed;
- double m_total_h_dist;
- double m_total_v_dist;
- };
- class SwimToCommand : public MoveToCommand {
- public:
- SwimToCommand(float x, float y, float z, MobMovementMode mob_movement_mode) : MoveToCommand(x, y, z, mob_movement_mode)
- {
- }
- virtual bool Process(MobMovementManager* mob_movement_manager, Spawn* mob)
- {
- //Send a movement packet when you start moving
- double current_time = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
- int current_speed = 0;
- if (m_move_to_mode == MovementRunning && mob->IsEntity()) {
- if (((Entity*)mob)->IsFeared()) {
- current_speed = ((Entity*)mob)->GetBaseSpeed();
- }
- else {
- //runback overrides
- if (((Entity*)mob)->GetSpeed() > ((Entity*)mob)->GetMaxSpeed())
- current_speed = ((Entity*)mob)->GetSpeed();
- else
- current_speed = ((Entity*)mob)->GetMaxSpeed();
- }
- }
- else {
- current_speed = mob->GetBaseSpeed();
- }
- if (!m_started) {
- m_started = true;
- //rotate to the point
- //mob->SetMoving(true);
- mob->SetHeading(mob->GetFaceTarget(m_move_to_x, m_move_to_z));
- m_last_sent_speed = current_speed;
- m_last_sent_time = current_time;
- m_total_h_dist = DistanceNoZ(glm::vec4(mob->GetX(),mob->GetZ(),mob->GetY(),mob->GetHeading()), glm::vec4(m_move_to_x, m_move_to_z, 0.0f, 0.0f));
- m_total_v_dist = m_move_to_y - mob->GetY();
- //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
- }
- //When speed changes
- if (current_speed != m_last_sent_speed) {
- m_last_sent_speed = current_speed;
- m_last_sent_time = current_time;
- //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
- }
- //If x seconds have passed without sending an update.
- if (current_time - m_last_sent_time >= 1.5) {
- m_last_sent_speed = current_speed;
- m_last_sent_time = current_time;
- //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
- }
- glm::vec4 p = glm::vec4(mob->GetX(), mob->GetZ(), mob->GetY(), mob->GetHeading());
- glm::vec2 tar(m_move_to_x, m_move_to_y);
- glm::vec2 pos(p.x, p.y);
- double len = glm::distance(pos, tar);
- if (len == 0) {
- return true;
- }
- //mob->SetMoved(true);
- glm::vec2 dir = tar - pos;
- glm::vec2 ndir = glm::normalize(dir);
- double distance_moved = frame_time * current_speed * 0.4f * 1.45f;
- mob->SetX(m_move_to_x);
- mob->SetZ(m_move_to_z);
- mob->SetY(m_move_to_y);
- if (distance_moved > len) {
- return true;
- }
- else {
- glm::vec2 npos = pos + (ndir * static_cast<float>(distance_moved));
- len -= distance_moved;
- double total_distance_traveled = m_total_h_dist - len;
- double start_y = m_move_to_y - m_total_v_dist;
- double y_at_pos = start_y + (m_total_v_dist * (total_distance_traveled / m_total_h_dist));
- mob->SetX(npos.x);
- mob->SetZ(npos.y);
- mob->SetY(y_at_pos);
- }
- return false;
- }
- };
- class TeleportToCommand : public IMovementCommand {
- public:
- TeleportToCommand(float x, float y, float z, float heading)
- {
- m_teleport_to_x = x;
- m_teleport_to_y = y;
- m_teleport_to_z = z;
- m_teleport_to_heading = heading;
- }
- virtual ~TeleportToCommand()
- {
- }
- virtual bool Process(MobMovementManager* mob_movement_manager, Spawn* mob)
- {
- mob->SetX(m_teleport_to_x);
- mob->SetZ(m_teleport_to_z);
- mob->SetY(m_teleport_to_y);
- mob->SetHeading(mob_movement_manager->FixHeading(m_teleport_to_heading));
- //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, 0, ClientRangeAny);
- return true;
- }
- virtual bool Started() const
- {
- return false;
- }
- private:
- double m_teleport_to_x;
- double m_teleport_to_y;
- double m_teleport_to_z;
- double m_teleport_to_heading;
- };
- class StopMovingCommand : public IMovementCommand {
- public:
- StopMovingCommand()
- {
- }
- virtual ~StopMovingCommand()
- {
- }
- virtual bool Process(MobMovementManager* mob_movement_manager, Spawn* mob)
- {
- mob->ClearRunningLocations();
- return true;
- }
- virtual bool Started() const
- {
- return false;
- }
- };
- class EvadeCombatCommand : public IMovementCommand {
- public:
- EvadeCombatCommand()
- {
- }
- virtual ~EvadeCombatCommand()
- {
- }
- virtual bool Process(MobMovementManager* mob_movement_manager, Spawn* mob)
- {
- if (mob->IsRunning()) {
- mob->StopMoving();
- }
- return true;
- }
- virtual bool Started() const
- {
- return false;
- }
- };
- struct MovementStats {
- MovementStats()
- {
- LastResetTime = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
- TotalSent = 0ULL;
- TotalSentMovement = 0ULL;
- TotalSentPosition = 0ULL;
- TotalSentHeading = 0ULL;
- }
- double LastResetTime;
- uint64_t TotalSent;
- uint64_t TotalSentMovement;
- uint64_t TotalSentPosition;
- uint64_t TotalSentHeading;
- };
- struct NavigateTo {
- NavigateTo()
- {
- navigate_to_x = 0.0;
- navigate_to_y = 0.0;
- navigate_to_z = 0.0;
- navigate_to_heading = 0.0;
- last_set_time = 0.0;
- }
- double navigate_to_x;
- double navigate_to_y;
- double navigate_to_z;
- double navigate_to_heading;
- double last_set_time;
- };
- struct MobMovementEntry {
- std::deque<std::unique_ptr<IMovementCommand>> Commands;
- NavigateTo NavTo;
- };
- void AdjustRoute(std::list<IPathfinder::IPathNode> &nodes, Spawn *who)
- {
- if (who->GetZone() == nullptr || !who->GetZone()->zonemap /*|| !zone->HasWaterMap()*/) {
- return;
- }
- auto offset = who->GetYOffset();
- for (auto &node : nodes) {
- //if (!zone->watermap->InLiquid(node.pos)) {
- auto best_z = who->GetZone()->zonemap->FindBestZ(node.pos, nullptr);
- if (best_z != BEST_Z_INVALID) {
- node.pos.z = best_z + offset;
- }
- //} // todo: floating logic?
- }
- }
- struct MobMovementManager::Implementation {
- std::map<Spawn *, MobMovementEntry> Entries;
- std::vector<Client *> Clients;
- MovementStats Stats;
- };
- MobMovementManager::MobMovementManager()
- {
- MobListMutex.SetName("MobMovementManager");
- _impl.reset(new Implementation());
- }
- MobMovementManager::~MobMovementManager()
- {
- }
- void MobMovementManager::Process()
- {
- MobListMutex.readlock();
- for (auto &iter : _impl->Entries) {
- auto &ent = iter.second;
- auto &commands = ent.Commands;
- iter.first->MCommandMutex.writelock();
- while (true != commands.empty()) {
- auto &cmd = commands.front();
- auto r = cmd->Process(this, iter.first);
- if (true != r) {
- break;
- }
- commands.pop_front();
- }
- iter.first->MCommandMutex.releasewritelock();
- }
- MobListMutex.releasereadlock();
- }
- /**
- * @param mob
- */
- void MobMovementManager::AddMob(Spawn *mob)
- {
- MobListMutex.writelock();
- mob->MCommandMutex.writelock();
- _impl->Entries.insert(std::make_pair(mob, MobMovementEntry()));
- mob->MCommandMutex.releasewritelock();
- MobListMutex.releasewritelock();
- }
- /**
- * @param mob
- */
- void MobMovementManager::RemoveMob(Spawn *mob)
- {
- MobListMutex.writelock();
- mob->MCommandMutex.writelock();
- _impl->Entries.erase(mob);
- mob->MCommandMutex.releasewritelock();
- MobListMutex.releasewritelock();
- }
- /**
- * @param client
- */
- void MobMovementManager::AddClient(Client *client)
- {
- _impl->Clients.push_back(client);
- }
- /**
- * @param client
- */
- void MobMovementManager::RemoveClient(Client *client)
- {
- auto iter = _impl->Clients.begin();
- while (iter != _impl->Clients.end()) {
- if (client == *iter) {
- _impl->Clients.erase(iter);
- return;
- }
- ++iter;
- }
- }
- /**
- * @param who
- * @param to
- * @param mob_movement_mode
- */
- void MobMovementManager::RotateTo(Spawn *who, float to, MobMovementMode mob_movement_mode)
- {
- MobListMutex.readlock();
- auto iter = _impl->Entries.find(who);
- auto &ent = (*iter);
- if (true != ent.second.Commands.empty()) {
- return;
- }
- PushRotateTo(ent.second, who, to, mob_movement_mode);
- MobListMutex.releasereadlock();
- }
- /**
- * @param who
- * @param x
- * @param y
- * @param z
- * @param heading
- */
- void MobMovementManager::Teleport(Spawn *who, float x, float y, float z, float heading)
- {
- MobListMutex.readlock();
- auto iter = _impl->Entries.find(who);
- auto &ent = (*iter);
- ent.second.Commands.clear();
- PushTeleportTo(ent.second, x, y, z, heading);
- MobListMutex.releasereadlock();
- }
- /**
- * @param who
- * @param x
- * @param y
- * @param z
- * @param mode
- */
- void MobMovementManager::NavigateTo(Spawn *who, float x, float y, float z, MobMovementMode mode, bool overrideDistance)
- {
- if (who->IsRunning())
- return;
- glm::vec3 targPos(x, z, y);
- glm::vec3 origPos(who->GetX(), who->GetZ(), who->GetY());
- if (IsPositionEqualWithinCertainZ(targPos, origPos, 6.0f)) {
- return;
- }
- MobListMutex.readlock();
- auto iter = _impl->Entries.find(who);
- auto &ent = (*iter);
- auto &nav = ent.second.NavTo;
- double current_time = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
- if ((current_time - nav.last_set_time) > 0.5) {
- //Can potentially recalc
- auto within = IsPositionWithinSimpleCylinder(
- glm::vec3(who->GetX(), who->GetZ(), who->GetY()),
- glm::vec3(nav.navigate_to_x, nav.navigate_to_z, nav.navigate_to_y),
- 1.5f,
- 6.0f
- );
- who->MCommandMutex.writelock();
- if (within && ent.second.Commands.size() > 0 && nav.last_set_time != 0)
- {
- //who->ClearRunningLocations();
- //StopNavigation((Entity*)who);
- who->MCommandMutex.releasewritelock();
- MobListMutex.releasereadlock();
- return;
- }
- else if (!within && ent.second.Commands.size() > 0 && nav.last_set_time != 0)
- {
- who->MCommandMutex.releasewritelock();
- MobListMutex.releasereadlock();
- return;
- }
- LogWrite(MAP__DEBUG, 0, "Map", "%s %f %f %f: within: %i, commands: %i, lastnav: %f %f %f", who->GetName(),
- who->GetX(),who->GetY(),who->GetZ(),within,
- ent.second.Commands.size(), nav.navigate_to_x, nav.navigate_to_y, nav.navigate_to_z);
- //auto heading_match = IsHeadingEqual(0.0, nav.navigate_to_heading);
- //if (/*false == within ||*/ false == heading_match || ent.second.Commands.size() == 0) {
- ent.second.Commands.clear();
- //Path is no longer valid, calculate a new path
- UpdatePath(who, x, y, z, mode);
- nav.navigate_to_x = x;
- nav.navigate_to_y = y;
- nav.navigate_to_z = z;
- nav.navigate_to_heading = 0.0;
- nav.last_set_time = current_time;
- who->MCommandMutex.releasewritelock();
- //}
- }
- MobListMutex.releasereadlock();
- }
- /**
- * @param who
- */
- void MobMovementManager::StopNavigation(Spawn *who)
- {
- MobListMutex.readlock();
- auto iter = _impl->Entries.find(who);
- auto &ent = (*iter);
- auto &nav = ent.second.NavTo;
- nav.navigate_to_x = 0.0;
- nav.navigate_to_y = 0.0;
- nav.navigate_to_z = 0.0;
- nav.navigate_to_heading = 0.0;
- nav.last_set_time = 0.0;
- who->MCommandMutex.writelock();
- if (true == ent.second.Commands.empty()) {
- PushStopMoving(ent.second);
- who->MCommandMutex.releasewritelock();
- MobListMutex.releasereadlock();
- return;
- }
- if (!who->IsRunning()) {
- ent.second.Commands.clear();
- who->MCommandMutex.releasewritelock();
- MobListMutex.releasereadlock();
- return;
- }
- ent.second.Commands.clear();
- PushStopMoving(ent.second);
- who->MCommandMutex.releasewritelock();
- MobListMutex.releasereadlock();
- }
- void MobMovementManager::DisruptNavigation(Spawn* who)
- {
- MobListMutex.readlock();
- auto iter = _impl->Entries.find(who);
- auto& ent = (*iter);
- auto& nav = ent.second.NavTo;
- nav.navigate_to_x = 0.0;
- nav.navigate_to_y = 0.0;
- nav.navigate_to_z = 0.0;
- nav.navigate_to_heading = 0.0;
- nav.last_set_time = 0.0;
- if (!who->IsRunning()) {
- who->MCommandMutex.writelock();
- ent.second.Commands.clear();
- who->MCommandMutex.releasewritelock();
- MobListMutex.releasereadlock();
- return;
- }
- }
- /**
- * @param in
- * @return
- */
- float MobMovementManager::FixHeading(float in)
- {
- auto h = in;
- while (h > 512.0) {
- h -= 512.0;
- }
- while (h < 0.0) {
- h += 512.0;
- }
- return h;
- }
- void MobMovementManager::ClearStats()
- {
- _impl->Stats.LastResetTime = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
- _impl->Stats.TotalSent = 0;
- _impl->Stats.TotalSentHeading = 0;
- _impl->Stats.TotalSentMovement = 0;
- _impl->Stats.TotalSentPosition = 0;
- }
- /**
- * @param who
- * @param x
- * @param y
- * @param z
- * @param mob_movement_mode
- */
- void MobMovementManager::UpdatePath(Spawn *who, float x, float y, float z, MobMovementMode mob_movement_mode)
- {
- if (!who->GetZone()->zonemap /*|| !zone->HasWaterMap()*/) {
- MobListMutex.readlock();
- auto iter = _impl->Entries.find(who);
- auto &ent = (*iter);
- PushMoveTo(ent.second, x, y, z, mob_movement_mode);
- PushStopMoving(ent.second);
- MobListMutex.releasereadlock();
- return;
- }
- /*
- if (who-?()) {
- UpdatePathBoat(who, x, y, z, mob_movement_mode);
- }
- else if (who->IsUnderwaterOnly()) {
- UpdatePathUnderwater(who, x, y, z, mob_movement_mode);
- }*/
- //else {
- UpdatePathGround(who, x, y, z, mob_movement_mode);
- //}
- }
- /**
- * @param who
- * @param x
- * @param y
- * @param z
- * @param mode
- */
- void MobMovementManager::UpdatePathGround(Spawn *who, float x, float y, float z, MobMovementMode mode)
- {
- PathfinderOptions opts;
- opts.smooth_path = true;
- opts.step_size = 100.0f;//RuleR(Pathing, NavmeshStepSize);
- opts.offset = who->GetYOffset()+1.0f;
- opts.flags = PathingNotDisabled ^ PathingZoneLine;
- //This is probably pointless since the nav mesh tool currently sets zonelines to disabled anyway
- auto partial = false;
- auto stuck = false;
- auto route = who->GetZone()->pathing->FindPath(
- glm::vec3(who->GetX(), who->GetZ(), who->GetY()),
- glm::vec3(x, z, y),
- partial,
- stuck,
- opts
- );
- MobListMutex.readlock();
- auto eiter = _impl->Entries.find(who);
- auto &ent = (*eiter);
- if (route.size() == 0) {
- HandleStuckBehavior(who, x, y, z, mode);
- MobListMutex.releasereadlock();
- return;
- }
- AdjustRoute(route, who);
- //avoid doing any processing if the mob is stuck to allow normal stuck code to work.
- if (!stuck) {
- //there are times when the routes returned are no differen than where the mob is currently standing. What basically happens
- //is a mob will get 'stuck' in such a way that it should be moving but the 'moving' place is the exact same spot it is at.
- //this is a problem and creates an area of ground that if a mob gets to, will stay there forever. If socal this creates a
- //"Ball of Death" (tm). This code tries to prevent this by simply warping the mob to the requested x/y. Better to have a warp than
- //have stuck mobs.
- auto routeNode = route.begin();
- bool noValidPath = true;
- while (routeNode != route.end() && noValidPath == true) {
- auto ¤tNode = (*routeNode);
- if (routeNode == route.end()) {
- continue;
- }
- if (!(currentNode.pos.x == who->GetX() && currentNode.pos.y == who->GetZ())) {
- //if one of the nodes to move to, is not our current node, pass it.
- noValidPath = false;
- break;
- }
- //move to the next node
- routeNode++;
- }
- if (noValidPath) {
- //we are 'stuck' in a path, lets just get out of this by 'teleporting' to the next position.
- PushTeleportTo(
- ent.second,
- x,
- y,
- z,
- CalculateHeadingAngleBetweenPositions(who->GetX(), who->GetZ(), x, z)
- );
- MobListMutex.releasereadlock();
- return;
- }
- }
- auto iter = route.begin();
- glm::vec3 previous_pos(who->GetX(), who->GetZ(), who->GetY());
- bool first_node = true;
- while (iter != route.end()) {
- auto ¤t_node = (*iter);
- iter++;
- if (iter == route.end()) {
- continue;
- }
- previous_pos = current_node.pos;
- auto &next_node = (*iter);
- if (first_node) {
- if (mode == MovementWalking) {
- auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
- PushRotateTo(ent.second, who, h, mode);
- }
- first_node = false;
- }
- //move to / teleport to node + 1
- if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
- float calcedHeading =
- CalculateHeadingAngleBetweenPositions(
- current_node.pos.x,
- current_node.pos.y,
- next_node.pos.x,
- next_node.pos.y
- );
- PushTeleportTo(
- ent.second,
- next_node.pos.x,
- next_node.pos.z,
- next_node.pos.y,
- calcedHeading
- );
- }
- else {
- /* if (who->GetZone()->watermap->InLiquid(previous_pos)) {
- PushSwimTo(ent.second, next_node.pos.x, next_node.pos.y, next_node.pos.z, mode);
- }
- else {*/
- PushMoveTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, mode);
- // }
- }
- }
- if (stuck) {
- HandleStuckBehavior(who, x, y, z, mode);
- }
- else {
- PushStopMoving(ent.second);
- }
- MobListMutex.releasereadlock();
- }
- /**
- * @param who
- * @param x
- * @param y
- * @param z
- * @param movement_mode
- */
- void MobMovementManager::UpdatePathUnderwater(Spawn *who, float x, float y, float z, MobMovementMode movement_mode)
- {
- MobListMutex.readlock();
- auto eiter = _impl->Entries.find(who);
- auto &ent = (*eiter);
- if (/*zone->watermap->InLiquid(who->GetPosition()) && zone->watermap->InLiquid(glm::vec3(x, y, z)) &&*/
- who->GetZone()->zonemap->CheckLoS(glm::vec3(who->GetX(),who->GetZ(),who->GetY()), glm::vec3(x, y, z))) {
- PushSwimTo(ent.second, x, y, z, movement_mode);
- PushStopMoving(ent.second);
- MobListMutex.releasereadlock();
- return;
- }
- PathfinderOptions opts;
- opts.smooth_path = true;
- opts.step_size = 100.0f;// RuleR(Pathing, NavmeshStepSize);
- opts.offset = who->GetYOffset();
- opts.flags = PathingNotDisabled ^ PathingZoneLine;
- auto partial = false;
- auto stuck = false;
- auto route = who->GetZone()->pathing->FindPath(
- glm::vec3(who->GetX(), who->GetY(), who->GetZ()),
- glm::vec3(x, y, z),
- partial,
- stuck,
- opts
- );
- if (route.size() == 0) {
- HandleStuckBehavior(who, x, z, y, movement_mode);
- MobListMutex.releasereadlock();
- return;
- }
- AdjustRoute(route, who);
- auto iter = route.begin();
- glm::vec3 previous_pos(who->GetX(), who->GetY(), who->GetZ());
- bool first_node = true;
- while (iter != route.end()) {
- auto ¤t_node = (*iter);
- /* if (!zone->watermap->InLiquid(current_node.pos)) {
- stuck = true;
- while (iter != route.end()) {
- iter = route.erase(iter);
- }
- break;
- }
- else {*/
- iter++;
- // }
- }
- if (route.size() == 0) {
- HandleStuckBehavior(who, x, y, z, movement_mode);
- MobListMutex.releasereadlock();
- return;
- }
- iter = route.begin();
- while (iter != route.end()) {
- auto ¤t_node = (*iter);
- iter++;
- if (iter == route.end()) {
- continue;
- }
- previous_pos = current_node.pos;
- auto &next_node = (*iter);
- if (first_node) {
- if (movement_mode == MovementWalking) {
- auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
- PushRotateTo(ent.second, who, h, movement_mode);
- }
- first_node = false;
- }
- //move to / teleport to node + 1
- if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
- float calcHeading = CalculateHeadingAngleBetweenPositions(
- current_node.pos.x,
- current_node.pos.y,
- next_node.pos.x,
- next_node.pos.y
- );
- PushTeleportTo(
- ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, calcHeading);
- }
- else {
- PushSwimTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, movement_mode);
- }
- }
- if (stuck) {
- HandleStuckBehavior(who, x, y, z, movement_mode);
- }
- else {
- PushStopMoving(ent.second);
- }
- MobListMutex.releasereadlock();
- }
- /**
- * @param who
- * @param x
- * @param y
- * @param z
- * @param mode
- */
- void MobMovementManager::UpdatePathBoat(Spawn *who, float x, float y, float z, MobMovementMode mode)
- {
- MobListMutex.readlock();
- auto eiter = _impl->Entries.find(who);
- auto &ent = (*eiter);
- PushSwimTo(ent.second, x, y, z, mode);
- PushStopMoving(ent.second);
- MobListMutex.releasereadlock();
- }
- /**
- * @param ent
- * @param x
- * @param y
- * @param z
- * @param heading
- */
- void MobMovementManager::PushTeleportTo(MobMovementEntry &ent, float x, float y, float z, float heading)
- {
- ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new TeleportToCommand(x, y, z, heading)));
- }
- /**
- * @param ent
- * @param x
- * @param y
- * @param z
- * @param mob_movement_mode
- */
- void MobMovementManager::PushMoveTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
- {
- ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new MoveToCommand(x, y, z, mob_movement_mode)));
- }
- /**
- * @param ent
- * @param x
- * @param y
- * @param z
- * @param mob_movement_mode
- */
- void MobMovementManager::PushSwimTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
- {
- ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new SwimToCommand(x, y, z, mob_movement_mode)));
- }
- /**
- * @param ent
- * @param who
- * @param to
- * @param mob_movement_mode
- */
- void MobMovementManager::PushRotateTo(MobMovementEntry &ent, Spawn *who, float to, MobMovementMode mob_movement_mode)
- {
- auto from = FixHeading(who->GetHeading());
- to = FixHeading(to);
- float diff = to - from;
- if (std::abs(diff) < 0.001f) {
- return;
- }
- while (diff < -256.0) {
- diff += 512.0;
- }
- while (diff > 256) {
- diff -= 512.0;
- }
- ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new RotateToCommand(to, diff > 0 ? 1.0 : -1.0, mob_movement_mode)));
- }
- /**
- * @param mob_movement_entry
- */
- void MobMovementManager::PushStopMoving(MobMovementEntry &mob_movement_entry)
- {
- mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new StopMovingCommand()));
- }
- /**
- * @param mob_movement_entry
- */
- void MobMovementManager::PushEvadeCombat(MobMovementEntry &mob_movement_entry)
- {
- mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new EvadeCombatCommand()));
- }
- /**
- * @param who
- * @param x
- * @param y
- * @param z
- * @param mob_movement_mode
- */
- void MobMovementManager::HandleStuckBehavior(Spawn *who, float x, float y, float z, MobMovementMode mob_movement_mode)
- {
- //LogDebug("Handle stuck behavior for {0} at ({1}, {2}, {3}) with movement_mode {4}", who->GetName(), x, y, z, mob_movement_mode);
- MobListMutex.readlock();
- auto sb = RunToTarget;//who->GetStuckBehavior();
- MobStuckBehavior behavior = RunToTarget;
- if (sb >= 0 && sb < MaxStuckBehavior) {
- behavior = (MobStuckBehavior) sb;
- }
- auto eiter = _impl->Entries.find(who);
- auto &ent = (*eiter);
- switch (sb) {
- case RunToTarget:
- PushMoveTo(ent.second, x, y, z, mob_movement_mode);
- PushStopMoving(ent.second);
- break;
- case WarpToTarget:
- PushTeleportTo(ent.second, x, y, z, 0.0f);
- PushStopMoving(ent.second);
- break;
- case TakeNoAction:
- PushStopMoving(ent.second);
- break;
- case EvadeCombat:
- PushEvadeCombat(ent.second);
- break;
- }
- MobListMutex.releasereadlock();
- }
|