mob_movement_manager.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. #include "mob_movement_manager.h"
  2. #include "../Entity.h"
  3. #include "../zoneserver.h"
  4. #include "region_map.h"
  5. #include "map.h"
  6. #include "../../common/timer.h"
  7. #include "pathfinder_interface.h"
  8. #include "position.h"
  9. #include "../../common/Log.h"
  10. #include <vector>
  11. #include <deque>
  12. #include <map>
  13. #include <stdlib.h>
  14. #ifdef WIN32
  15. #include <windows.h>
  16. #endif
  17. extern double frame_time;
  18. class IMovementCommand {
  19. public:
  20. IMovementCommand() = default;
  21. virtual ~IMovementCommand() = default;
  22. virtual bool Process(MobMovementManager* mob_movement_manager, Entity* mob) = 0;
  23. virtual bool Started() const = 0;
  24. };
  25. class RotateToCommand : public IMovementCommand {
  26. public:
  27. RotateToCommand(double rotate_to, double dir, MobMovementMode mob_movement_mode)
  28. {
  29. m_rotate_to = rotate_to;
  30. m_rotate_to_dir = dir;
  31. m_rotate_to_mode = mob_movement_mode;
  32. m_started = false;
  33. }
  34. virtual ~RotateToCommand()
  35. {
  36. }
  37. virtual bool Process(MobMovementManager* mob_movement_manager, Entity* mob)
  38. {
  39. auto rotate_to_speed = m_rotate_to_mode == MovementRunning ? 200.0 : 16.0; //todo: get this from mob
  40. auto from = mob_movement_manager->FixHeading(mob->GetHeading());
  41. auto to = mob_movement_manager->FixHeading(m_rotate_to);
  42. auto diff = to - from;
  43. while (diff < -256.0) {
  44. diff += 512.0;
  45. }
  46. while (diff > 256) {
  47. diff -= 512.0;
  48. }
  49. auto dist = std::abs(diff);
  50. if (!m_started) {
  51. m_started = true;
  52. //mob->SetMoving(true);
  53. /*if (dist > 15.0f && rotate_to_speed > 0.0 && rotate_to_speed <= 25.0) { //send basic rotation
  54. mob_movement_manager->SendCommandToClients(
  55. mob,
  56. 0.0,
  57. 0.0,
  58. 0.0,
  59. m_rotate_to_dir * rotate_to_speed,
  60. 0,
  61. ClientRangeClose
  62. );
  63. }*/
  64. }
  65. auto td = rotate_to_speed * 19.0 * frame_time;
  66. if (td >= dist) {
  67. mob->SetHeading(to);
  68. //mob->SetMoving(false);
  69. //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, 0, ClientRangeCloseMedium);
  70. return true;
  71. }
  72. from += td * m_rotate_to_dir;
  73. mob->SetHeading(mob_movement_manager->FixHeading(from));
  74. return false;
  75. }
  76. virtual bool Started() const
  77. {
  78. return m_started;
  79. }
  80. private:
  81. double m_rotate_to;
  82. double m_rotate_to_dir;
  83. MobMovementMode m_rotate_to_mode;
  84. bool m_started;
  85. };
  86. class MoveToCommand : public IMovementCommand {
  87. public:
  88. MoveToCommand(float x, float y, float z, MobMovementMode mob_movement_mode)
  89. {
  90. m_distance_moved_since_correction = 0.0;
  91. m_move_to_x = x;
  92. m_move_to_y = y;
  93. m_move_to_z = z;
  94. m_move_to_mode = mob_movement_mode;
  95. m_last_sent_time = 0.0;
  96. m_last_sent_speed = 0;
  97. m_started = false;
  98. m_total_h_dist = 0.0;
  99. m_total_v_dist = 0.0;
  100. }
  101. virtual ~MoveToCommand()
  102. {
  103. }
  104. /**
  105. * @param mob_movement_manager
  106. * @param mob
  107. * @return
  108. */
  109. virtual bool Process(MobMovementManager* mob_movement_manager, Entity* mob)
  110. {
  111. //Send a movement packet when you start moving
  112. double current_time = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  113. int current_speed = 0;
  114. if (m_move_to_mode == MovementRunning) {
  115. current_speed = ((Spawn*)mob)->GetSpeed();
  116. }
  117. if (!m_started) {
  118. m_started = true;
  119. //rotate to the point
  120. //mob->SetMoving(true);
  121. mob->SetHeading(mob->GetFaceTarget(m_move_to_x, m_move_to_z));
  122. m_last_sent_speed = current_speed;
  123. m_last_sent_time = current_time;
  124. // Z/Y are flipped due to EverQuest 2 using Y as up/down
  125. 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));
  126. m_total_v_dist = m_move_to_y - mob->GetY();
  127. //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
  128. }
  129. //When speed changes
  130. if (current_speed != m_last_sent_speed) {
  131. //if (RuleB(Map, FixZWhenPathing)) {
  132. // mob->FixZ();
  133. //}
  134. m_distance_moved_since_correction = 0.0;
  135. m_last_sent_speed = current_speed;
  136. m_last_sent_time = current_time;
  137. //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
  138. }
  139. //If x seconds have passed without sending an update.
  140. if (current_time - m_last_sent_time >= 5.0) {
  141. //if (RuleB(Map, FixZWhenPathing)) {
  142. //mob->FixZ();
  143. //}
  144. m_distance_moved_since_correction = 0.0;
  145. m_last_sent_speed = current_speed;
  146. m_last_sent_time = current_time;
  147. //mob_movemesnt_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
  148. }
  149. glm::vec3 p = glm::vec3(mob->GetX(), mob->GetY(), mob->GetZ());
  150. // our X/Z versus the mobs X/Z
  151. glm::vec2 tar(m_move_to_x, m_move_to_z);
  152. glm::vec2 pos(p.x, p.z);
  153. double len = glm::distance(pos, tar);
  154. if (len < .01) {
  155. return true;
  156. }
  157. //mob->SetMoved(true);
  158. glm::vec2 dir = tar - pos;
  159. glm::vec2 ndir = glm::normalize(dir);
  160. double distance_moved = frame_time * current_speed * 0.4f * 1.45f;
  161. //mob->SetX(m_move_to_x);
  162. //mob->SetY(m_move_to_z);
  163. //mob->SetZ(m_move_to_y);
  164. mob->ClearRunningLocations();
  165. if (distance_moved > len) {
  166. //if (RuleB(Map, FixZWhenPathing)) {
  167. //mob->FixZ();
  168. //}
  169. // we use npos.y because higher up that is the equilvaent Z
  170. mob->AddRunningLocation(m_move_to_x, m_move_to_y, m_move_to_z, current_speed, distance_moved, true, true, "", true);
  171. return false;
  172. }
  173. else {
  174. glm::vec2 npos = pos + (ndir * static_cast<float>(distance_moved));
  175. len -= distance_moved;
  176. double total_distance_traveled = m_total_h_dist - len;
  177. double start_y = m_move_to_y - m_total_v_dist;
  178. double y_at_pos = start_y + (m_total_v_dist * (total_distance_traveled / m_total_h_dist));
  179. // we use npos.y because higher up that is the equilvaent Z
  180. mob->AddRunningLocation(m_move_to_x, m_move_to_y, m_move_to_z, current_speed, distance_moved, true, true, "", true);
  181. // mob->SetX(npos.x);
  182. // mob->SetY(z_at_pos);
  183. // mob->SetZ(npos.y);
  184. //if (RuleB(Map, FixZWhenPathing)) {
  185. // m_distance_moved_since_correction += distance_moved;
  186. // if (m_distance_moved_since_correction > 10.0f /*RuleR(Map, DistanceCanTravelBeforeAdjustment)*/) {
  187. // m_distance_moved_since_correction = 0.0;
  188. //mob->FixZ();
  189. //}
  190. // }
  191. }
  192. return false;
  193. }
  194. virtual bool Started() const
  195. {
  196. return m_started;
  197. }
  198. protected:
  199. double m_distance_moved_since_correction;
  200. double m_move_to_x;
  201. double m_move_to_y;
  202. double m_move_to_z;
  203. MobMovementMode m_move_to_mode;
  204. bool m_started;
  205. double m_last_sent_time;
  206. int m_last_sent_speed;
  207. double m_total_h_dist;
  208. double m_total_v_dist;
  209. };
  210. class SwimToCommand : public MoveToCommand {
  211. public:
  212. SwimToCommand(float x, float y, float z, MobMovementMode mob_movement_mode) : MoveToCommand(x, y, z, mob_movement_mode)
  213. {
  214. }
  215. virtual bool Process(MobMovementManager* mob_movement_manager, Entity* mob)
  216. {
  217. //Send a movement packet when you start moving
  218. double current_time = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  219. int current_speed = 0;
  220. if (m_move_to_mode == MovementRunning) {
  221. if (mob->IsFeared()) {
  222. current_speed = mob->GetBaseSpeed();
  223. }
  224. else {
  225. //runback overrides
  226. if (mob->GetSpeed() > mob->GetMaxSpeed())
  227. current_speed = mob->GetSpeed();
  228. else
  229. current_speed = mob->GetMaxSpeed();
  230. }
  231. }
  232. else {
  233. current_speed = mob->GetBaseSpeed();
  234. }
  235. if (!m_started) {
  236. m_started = true;
  237. //rotate to the point
  238. //mob->SetMoving(true);
  239. mob->SetHeading(mob->GetFaceTarget(m_move_to_x, m_move_to_z));
  240. m_last_sent_speed = current_speed;
  241. m_last_sent_time = current_time;
  242. 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));
  243. m_total_v_dist = m_move_to_y - mob->GetY();
  244. //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
  245. }
  246. //When speed changes
  247. if (current_speed != m_last_sent_speed) {
  248. m_last_sent_speed = current_speed;
  249. m_last_sent_time = current_time;
  250. //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
  251. }
  252. //If x seconds have passed without sending an update.
  253. if (current_time - m_last_sent_time >= 1.5) {
  254. m_last_sent_speed = current_speed;
  255. m_last_sent_time = current_time;
  256. //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, current_speed, ClientRangeCloseMedium);
  257. }
  258. glm::vec4 p = glm::vec4(mob->GetX(), mob->GetZ(), mob->GetY(), mob->GetHeading());
  259. glm::vec2 tar(m_move_to_x, m_move_to_y);
  260. glm::vec2 pos(p.x, p.y);
  261. double len = glm::distance(pos, tar);
  262. if (len == 0) {
  263. return true;
  264. }
  265. //mob->SetMoved(true);
  266. glm::vec2 dir = tar - pos;
  267. glm::vec2 ndir = glm::normalize(dir);
  268. double distance_moved = frame_time * current_speed * 0.4f * 1.45f;
  269. mob->SetX(m_move_to_x);
  270. mob->SetZ(m_move_to_z);
  271. mob->SetY(m_move_to_y);
  272. if (distance_moved > len) {
  273. return true;
  274. }
  275. else {
  276. glm::vec2 npos = pos + (ndir * static_cast<float>(distance_moved));
  277. len -= distance_moved;
  278. double total_distance_traveled = m_total_h_dist - len;
  279. double start_y = m_move_to_y - m_total_v_dist;
  280. double y_at_pos = start_y + (m_total_v_dist * (total_distance_traveled / m_total_h_dist));
  281. mob->SetX(npos.x);
  282. mob->SetZ(npos.y);
  283. mob->SetY(y_at_pos);
  284. }
  285. return false;
  286. }
  287. };
  288. class TeleportToCommand : public IMovementCommand {
  289. public:
  290. TeleportToCommand(float x, float y, float z, float heading)
  291. {
  292. m_teleport_to_x = x;
  293. m_teleport_to_y = y;
  294. m_teleport_to_z = z;
  295. m_teleport_to_heading = heading;
  296. }
  297. virtual ~TeleportToCommand()
  298. {
  299. }
  300. virtual bool Process(MobMovementManager* mob_movement_manager, Entity* mob)
  301. {
  302. mob->SetX(m_teleport_to_x);
  303. mob->SetZ(m_teleport_to_z);
  304. mob->SetY(m_teleport_to_y);
  305. mob->SetHeading(mob_movement_manager->FixHeading(m_teleport_to_heading));
  306. //mob_movement_manager->SendCommandToClients(mob, 0.0, 0.0, 0.0, 0.0, 0, ClientRangeAny);
  307. return true;
  308. }
  309. virtual bool Started() const
  310. {
  311. return false;
  312. }
  313. private:
  314. double m_teleport_to_x;
  315. double m_teleport_to_y;
  316. double m_teleport_to_z;
  317. double m_teleport_to_heading;
  318. };
  319. class StopMovingCommand : public IMovementCommand {
  320. public:
  321. StopMovingCommand()
  322. {
  323. }
  324. virtual ~StopMovingCommand()
  325. {
  326. }
  327. virtual bool Process(MobMovementManager* mob_movement_manager, Entity* mob)
  328. {
  329. mob->ClearRunningLocations();
  330. return true;
  331. }
  332. virtual bool Started() const
  333. {
  334. return false;
  335. }
  336. };
  337. class EvadeCombatCommand : public IMovementCommand {
  338. public:
  339. EvadeCombatCommand()
  340. {
  341. }
  342. virtual ~EvadeCombatCommand()
  343. {
  344. }
  345. virtual bool Process(MobMovementManager* mob_movement_manager, Entity* mob)
  346. {
  347. return true;
  348. }
  349. virtual bool Started() const
  350. {
  351. return false;
  352. }
  353. };
  354. struct MovementStats {
  355. MovementStats()
  356. {
  357. LastResetTime = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  358. TotalSent = 0ULL;
  359. TotalSentMovement = 0ULL;
  360. TotalSentPosition = 0ULL;
  361. TotalSentHeading = 0ULL;
  362. }
  363. double LastResetTime;
  364. uint64_t TotalSent;
  365. uint64_t TotalSentMovement;
  366. uint64_t TotalSentPosition;
  367. uint64_t TotalSentHeading;
  368. };
  369. struct NavigateTo {
  370. NavigateTo()
  371. {
  372. navigate_to_x = 0.0;
  373. navigate_to_y = 0.0;
  374. navigate_to_z = 0.0;
  375. navigate_to_heading = 0.0;
  376. last_set_time = 0.0;
  377. }
  378. double navigate_to_x;
  379. double navigate_to_y;
  380. double navigate_to_z;
  381. double navigate_to_heading;
  382. double last_set_time;
  383. };
  384. struct MobMovementEntry {
  385. std::deque<std::unique_ptr<IMovementCommand>> Commands;
  386. NavigateTo NavTo;
  387. };
  388. void AdjustRoute(std::list<IPathfinder::IPathNode> &nodes, Entity *who)
  389. {
  390. if (who->GetZone() == nullptr || !who->GetMap() /*|| !zone->HasWaterMap()*/) {
  391. return;
  392. }
  393. auto offset = who->GetYOffset();
  394. for (auto &node : nodes) {
  395. //if (!zone->watermap->InLiquid(node.pos)) {
  396. auto best_z = who->FindBestZ(node.pos, nullptr);
  397. if (best_z != BEST_Z_INVALID) {
  398. node.pos.z = best_z + offset;
  399. }
  400. //} // todo: floating logic?
  401. }
  402. }
  403. struct MobMovementManager::Implementation {
  404. std::map<Entity *, MobMovementEntry> Entries;
  405. std::vector<Client *> Clients;
  406. MovementStats Stats;
  407. };
  408. MobMovementManager::MobMovementManager()
  409. {
  410. MobListMutex.SetName("MobMovementManager");
  411. _impl.reset(new Implementation());
  412. }
  413. MobMovementManager::~MobMovementManager()
  414. {
  415. }
  416. void MobMovementManager::Process()
  417. {
  418. MobListMutex.readlock();
  419. for (auto &iter : _impl->Entries) {
  420. auto &ent = iter.second;
  421. auto &commands = ent.Commands;
  422. if (commands.size() < 1)
  423. continue;
  424. iter.first->MCommandMutex.writelock();
  425. while (true != commands.empty()) {
  426. auto &cmd = commands.front();
  427. auto r = cmd->Process(this, iter.first);
  428. if (true != r) {
  429. break;
  430. }
  431. commands.pop_front();
  432. }
  433. iter.first->MCommandMutex.releasewritelock();
  434. }
  435. MobListMutex.releasereadlock();
  436. }
  437. /**
  438. * @param mob
  439. */
  440. void MobMovementManager::AddMob(Entity *mob)
  441. {
  442. MobListMutex.writelock();
  443. _impl->Entries.insert(std::make_pair(mob, MobMovementEntry()));
  444. MobListMutex.releasewritelock();
  445. }
  446. /**
  447. * @param mob
  448. */
  449. void MobMovementManager::RemoveMob(Entity *mob)
  450. {
  451. MobListMutex.writelock();
  452. auto iter = _impl->Entries.find(mob);
  453. if(iter != _impl->Entries.end())
  454. _impl->Entries.erase(iter);
  455. MobListMutex.releasewritelock();
  456. }
  457. /**
  458. * @param client
  459. */
  460. void MobMovementManager::AddClient(Client *client)
  461. {
  462. _impl->Clients.push_back(client);
  463. }
  464. /**
  465. * @param client
  466. */
  467. void MobMovementManager::RemoveClient(Client *client)
  468. {
  469. auto iter = _impl->Clients.begin();
  470. while (iter != _impl->Clients.end()) {
  471. if (client == *iter) {
  472. _impl->Clients.erase(iter);
  473. return;
  474. }
  475. ++iter;
  476. }
  477. }
  478. /**
  479. * @param who
  480. * @param to
  481. * @param mob_movement_mode
  482. */
  483. void MobMovementManager::RotateTo(Entity *who, float to, MobMovementMode mob_movement_mode)
  484. {
  485. MobListMutex.readlock();
  486. auto iter = _impl->Entries.find(who);
  487. if (iter == _impl->Entries.end())
  488. {
  489. MobListMutex.releasereadlock();
  490. return; // does not exist in navigation
  491. }
  492. auto &ent = (*iter);
  493. if (true != ent.second.Commands.empty()) {
  494. MobListMutex.releasereadlock();
  495. return;
  496. }
  497. PushRotateTo(ent.second, who, to, mob_movement_mode);
  498. MobListMutex.releasereadlock();
  499. }
  500. /**
  501. * @param who
  502. * @param x
  503. * @param y
  504. * @param z
  505. * @param heading
  506. */
  507. void MobMovementManager::Teleport(Entity *who, float x, float y, float z, float heading)
  508. {
  509. MobListMutex.readlock();
  510. auto iter = _impl->Entries.find(who);
  511. if (iter == _impl->Entries.end())
  512. {
  513. MobListMutex.releasereadlock();
  514. return; // does not exist in navigation
  515. }
  516. auto &ent = (*iter);
  517. ent.second.Commands.clear();
  518. PushTeleportTo(ent.second, x, y, z, heading);
  519. MobListMutex.releasereadlock();
  520. }
  521. /**
  522. * @param who
  523. * @param x
  524. * @param y
  525. * @param z
  526. * @param mode
  527. */
  528. void MobMovementManager::NavigateTo(Entity *who, float x, float y, float z, MobMovementMode mode, bool overrideDistance)
  529. {
  530. glm::vec3 targPos(x, z, y);
  531. glm::vec3 origPos(who->GetX(), who->GetZ(), who->GetY());
  532. if (IsPositionEqualWithinCertainZ(targPos, origPos, 6.0f)) {
  533. return;
  534. }
  535. MobListMutex.readlock();
  536. auto iter = _impl->Entries.find(who);
  537. if (iter == _impl->Entries.end())
  538. {
  539. MobListMutex.releasereadlock();
  540. return; // does not exist in navigation
  541. }
  542. auto &ent = (*iter);
  543. auto &nav = ent.second.NavTo;
  544. double current_time = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  545. if ((current_time - nav.last_set_time) > 0.5) {
  546. //Can potentially recalc
  547. auto within = IsPositionWithinSimpleCylinder(
  548. glm::vec3(who->GetX(), who->GetZ(), who->GetY()),
  549. glm::vec3(nav.navigate_to_x, nav.navigate_to_z, nav.navigate_to_y),
  550. 1.5f,
  551. 6.0f
  552. );
  553. who->MCommandMutex.writelock();
  554. if (within && ent.second.Commands.size() > 0 && nav.last_set_time != 0)
  555. {
  556. //who->ClearRunningLocations();
  557. //StopNavigation((Entity*)who);
  558. who->MCommandMutex.releasewritelock();
  559. MobListMutex.releasereadlock();
  560. return;
  561. }
  562. else if (!within && ent.second.Commands.size() > 0 && nav.last_set_time != 0)
  563. {
  564. who->MCommandMutex.releasewritelock();
  565. MobListMutex.releasereadlock();
  566. return;
  567. }
  568. LogWrite(MAP__DEBUG, 0, "Map", "%s %f %f %f: within: %i, commands: %i, lastnav: %f %f %f", who->GetName(),
  569. who->GetX(),who->GetY(),who->GetZ(),within,
  570. ent.second.Commands.size(), nav.navigate_to_x, nav.navigate_to_y, nav.navigate_to_z);
  571. //auto heading_match = IsHeadingEqual(0.0, nav.navigate_to_heading);
  572. //if (/*false == within ||*/ false == heading_match || ent.second.Commands.size() == 0) {
  573. ent.second.Commands.clear();
  574. //Path is no longer valid, calculate a new path
  575. UpdatePath(who, x, y, z, mode);
  576. nav.navigate_to_x = x;
  577. nav.navigate_to_y = y;
  578. nav.navigate_to_z = z;
  579. nav.navigate_to_heading = 0.0;
  580. nav.last_set_time = current_time;
  581. who->MCommandMutex.releasewritelock();
  582. //}
  583. }
  584. MobListMutex.releasereadlock();
  585. }
  586. /**
  587. * @param who
  588. */
  589. void MobMovementManager::StopNavigation(Entity *who)
  590. {
  591. MobListMutex.readlock();
  592. auto iter = _impl->Entries.find(who);
  593. if (iter == _impl->Entries.end())
  594. {
  595. MobListMutex.releasereadlock();
  596. return; // does not exist in navigation
  597. }
  598. auto &ent = (*iter);
  599. auto &nav = ent.second.NavTo;
  600. nav.navigate_to_x = 0.0;
  601. nav.navigate_to_y = 0.0;
  602. nav.navigate_to_z = 0.0;
  603. nav.navigate_to_heading = 0.0;
  604. nav.last_set_time = 0.0;
  605. who->MCommandMutex.writelock();
  606. if (true == ent.second.Commands.empty()) {
  607. PushStopMoving(ent.second);
  608. who->MCommandMutex.releasewritelock();
  609. MobListMutex.releasereadlock();
  610. return;
  611. }
  612. if (!who->IsRunning()) {
  613. ent.second.Commands.clear();
  614. who->MCommandMutex.releasewritelock();
  615. MobListMutex.releasereadlock();
  616. return;
  617. }
  618. ent.second.Commands.clear();
  619. PushStopMoving(ent.second);
  620. who->MCommandMutex.releasewritelock();
  621. MobListMutex.releasereadlock();
  622. }
  623. void MobMovementManager::DisruptNavigation(Entity* who)
  624. {
  625. MobListMutex.readlock();
  626. auto iter = _impl->Entries.find(who);
  627. if (iter == _impl->Entries.end())
  628. {
  629. MobListMutex.releasereadlock();
  630. return; // does not exist in navigation
  631. }
  632. auto& ent = (*iter);
  633. auto& nav = ent.second.NavTo;
  634. nav.navigate_to_x = 0.0;
  635. nav.navigate_to_y = 0.0;
  636. nav.navigate_to_z = 0.0;
  637. nav.navigate_to_heading = 0.0;
  638. nav.last_set_time = 0.0;
  639. if (!who->IsRunning()) {
  640. who->MCommandMutex.writelock();
  641. ent.second.Commands.clear();
  642. who->MCommandMutex.releasewritelock();
  643. }
  644. MobListMutex.releasereadlock();
  645. }
  646. /**
  647. * @param in
  648. * @return
  649. */
  650. float MobMovementManager::FixHeading(float in)
  651. {
  652. auto h = in;
  653. while (h > 512.0) {
  654. h -= 512.0;
  655. }
  656. while (h < 0.0) {
  657. h += 512.0;
  658. }
  659. return h;
  660. }
  661. void MobMovementManager::ClearStats()
  662. {
  663. _impl->Stats.LastResetTime = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  664. _impl->Stats.TotalSent = 0;
  665. _impl->Stats.TotalSentHeading = 0;
  666. _impl->Stats.TotalSentMovement = 0;
  667. _impl->Stats.TotalSentPosition = 0;
  668. }
  669. /**
  670. * @param who
  671. * @param x
  672. * @param y
  673. * @param z
  674. * @param mob_movement_mode
  675. */
  676. void MobMovementManager::UpdatePath(Entity *who, float x, float y, float z, MobMovementMode mob_movement_mode)
  677. {
  678. if (!who->GetMap() /*|| !zone->HasWaterMap()*/) {
  679. MobListMutex.readlock();
  680. auto iter = _impl->Entries.find(who);
  681. if (iter == _impl->Entries.end())
  682. {
  683. MobListMutex.releasereadlock();
  684. return; // does not exist in navigation
  685. }
  686. auto &ent = (*iter);
  687. PushMoveTo(ent.second, x, y, z, mob_movement_mode);
  688. PushStopMoving(ent.second);
  689. MobListMutex.releasereadlock();
  690. return;
  691. }
  692. /*
  693. if (who-?()) {
  694. UpdatePathBoat(who, x, y, z, mob_movement_mode);
  695. }
  696. else if (who->IsUnderwaterOnly()) {
  697. UpdatePathUnderwater(who, x, y, z, mob_movement_mode);
  698. }*/
  699. //else {
  700. UpdatePathGround(who, x, y, z, mob_movement_mode);
  701. //}
  702. }
  703. /**
  704. * @param who
  705. * @param x
  706. * @param y
  707. * @param z
  708. * @param mode
  709. */
  710. void MobMovementManager::UpdatePathGround(Entity *who, float x, float y, float z, MobMovementMode mode)
  711. {
  712. PathfinderOptions opts;
  713. opts.smooth_path = true;
  714. opts.step_size = 100.0f;//RuleR(Pathing, NavmeshStepSize);
  715. opts.offset = who->GetYOffset()+1.0f;
  716. opts.flags = PathingNotDisabled ^ PathingZoneLine;
  717. //This is probably pointless since the nav mesh tool currently sets zonelines to disabled anyway
  718. auto partial = false;
  719. auto stuck = false;
  720. auto route = who->GetZone()->pathing->FindPath(
  721. glm::vec3(who->GetX(), who->GetZ(), who->GetY()),
  722. glm::vec3(x, z, y),
  723. partial,
  724. stuck,
  725. opts
  726. );
  727. MobListMutex.readlock();
  728. auto eiter = _impl->Entries.find(who);
  729. if (eiter == _impl->Entries.end())
  730. {
  731. MobListMutex.releasereadlock();
  732. return; // does not exist in navigation
  733. }
  734. auto &ent = (*eiter);
  735. if (route.size() == 0) {
  736. HandleStuckBehavior(who, x, y, z, mode);
  737. MobListMutex.releasereadlock();
  738. return;
  739. }
  740. AdjustRoute(route, who);
  741. //avoid doing any processing if the mob is stuck to allow normal stuck code to work.
  742. if (!stuck) {
  743. //there are times when the routes returned are no differen than where the mob is currently standing. What basically happens
  744. //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.
  745. //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
  746. //"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
  747. //have stuck mobs.
  748. auto routeNode = route.begin();
  749. bool noValidPath = true;
  750. while (routeNode != route.end() && noValidPath == true) {
  751. auto &currentNode = (*routeNode);
  752. if (routeNode == route.end()) {
  753. continue;
  754. }
  755. if (!(currentNode.pos.x == who->GetX() && currentNode.pos.y == who->GetZ())) {
  756. //if one of the nodes to move to, is not our current node, pass it.
  757. noValidPath = false;
  758. break;
  759. }
  760. //move to the next node
  761. routeNode++;
  762. }
  763. if (noValidPath) {
  764. //we are 'stuck' in a path, lets just get out of this by 'teleporting' to the next position.
  765. PushTeleportTo(
  766. ent.second,
  767. x,
  768. y,
  769. z,
  770. CalculateHeadingAngleBetweenPositions(who->GetX(), who->GetZ(), x, z)
  771. );
  772. MobListMutex.releasereadlock();
  773. return;
  774. }
  775. }
  776. auto iter = route.begin();
  777. glm::vec3 previous_pos(who->GetX(), who->GetZ(), who->GetY());
  778. bool first_node = true;
  779. while (iter != route.end()) {
  780. auto &current_node = (*iter);
  781. iter++;
  782. if (iter == route.end()) {
  783. continue;
  784. }
  785. previous_pos = current_node.pos;
  786. auto &next_node = (*iter);
  787. if (first_node) {
  788. if (mode == MovementWalking) {
  789. auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
  790. PushRotateTo(ent.second, who, h, mode);
  791. }
  792. first_node = false;
  793. }
  794. //move to / teleport to node + 1
  795. if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
  796. float calcedHeading =
  797. CalculateHeadingAngleBetweenPositions(
  798. current_node.pos.x,
  799. current_node.pos.y,
  800. next_node.pos.x,
  801. next_node.pos.y
  802. );
  803. PushTeleportTo(
  804. ent.second,
  805. next_node.pos.x,
  806. next_node.pos.z,
  807. next_node.pos.y,
  808. calcedHeading
  809. );
  810. }
  811. else {
  812. /* if (who->GetZone()->watermap->InLiquid(previous_pos)) {
  813. PushSwimTo(ent.second, next_node.pos.x, next_node.pos.y, next_node.pos.z, mode);
  814. }
  815. else {*/
  816. PushMoveTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, mode);
  817. // }
  818. }
  819. }
  820. if (stuck) {
  821. HandleStuckBehavior(who, x, y, z, mode);
  822. }
  823. else {
  824. PushStopMoving(ent.second);
  825. }
  826. MobListMutex.releasereadlock();
  827. }
  828. /**
  829. * @param who
  830. * @param x
  831. * @param y
  832. * @param z
  833. * @param movement_mode
  834. */
  835. void MobMovementManager::UpdatePathUnderwater(Entity *who, float x, float y, float z, MobMovementMode movement_mode)
  836. {
  837. MobListMutex.readlock();
  838. auto eiter = _impl->Entries.find(who);
  839. if (eiter == _impl->Entries.end())
  840. {
  841. MobListMutex.releasereadlock();
  842. return; // does not exist in navigation
  843. }
  844. auto &ent = (*eiter);
  845. if (/*zone->watermap->InLiquid(who->GetPosition()) && zone->watermap->InLiquid(glm::vec3(x, y, z)) &&*/
  846. who->CheckLoS(glm::vec3(who->GetX(),who->GetZ(),who->GetY()), glm::vec3(x, z, y))) {
  847. PushSwimTo(ent.second, x, y, z, movement_mode);
  848. PushStopMoving(ent.second);
  849. MobListMutex.releasereadlock();
  850. return;
  851. }
  852. PathfinderOptions opts;
  853. opts.smooth_path = true;
  854. opts.step_size = 100.0f;// RuleR(Pathing, NavmeshStepSize);
  855. opts.offset = who->GetYOffset();
  856. opts.flags = PathingNotDisabled ^ PathingZoneLine;
  857. auto partial = false;
  858. auto stuck = false;
  859. auto route = who->GetZone()->pathing->FindPath(
  860. glm::vec3(who->GetX(), who->GetY(), who->GetZ()),
  861. glm::vec3(x, y, z),
  862. partial,
  863. stuck,
  864. opts
  865. );
  866. if (route.size() == 0) {
  867. HandleStuckBehavior(who, x, z, y, movement_mode);
  868. MobListMutex.releasereadlock();
  869. return;
  870. }
  871. AdjustRoute(route, who);
  872. auto iter = route.begin();
  873. glm::vec3 previous_pos(who->GetX(), who->GetY(), who->GetZ());
  874. bool first_node = true;
  875. while (iter != route.end()) {
  876. auto &current_node = (*iter);
  877. /* if (!zone->watermap->InLiquid(current_node.pos)) {
  878. stuck = true;
  879. while (iter != route.end()) {
  880. iter = route.erase(iter);
  881. }
  882. break;
  883. }
  884. else {*/
  885. iter++;
  886. // }
  887. }
  888. if (route.size() == 0) {
  889. HandleStuckBehavior(who, x, y, z, movement_mode);
  890. MobListMutex.releasereadlock();
  891. return;
  892. }
  893. iter = route.begin();
  894. while (iter != route.end()) {
  895. auto &current_node = (*iter);
  896. iter++;
  897. if (iter == route.end()) {
  898. continue;
  899. }
  900. previous_pos = current_node.pos;
  901. auto &next_node = (*iter);
  902. if (first_node) {
  903. if (movement_mode == MovementWalking) {
  904. auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
  905. PushRotateTo(ent.second, who, h, movement_mode);
  906. }
  907. first_node = false;
  908. }
  909. //move to / teleport to node + 1
  910. if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
  911. float calcHeading = CalculateHeadingAngleBetweenPositions(
  912. current_node.pos.x,
  913. current_node.pos.y,
  914. next_node.pos.x,
  915. next_node.pos.y
  916. );
  917. PushTeleportTo(
  918. ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, calcHeading);
  919. }
  920. else {
  921. PushSwimTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, movement_mode);
  922. }
  923. }
  924. if (stuck) {
  925. HandleStuckBehavior(who, x, y, z, movement_mode);
  926. }
  927. else {
  928. PushStopMoving(ent.second);
  929. }
  930. MobListMutex.releasereadlock();
  931. }
  932. /**
  933. * @param who
  934. * @param x
  935. * @param y
  936. * @param z
  937. * @param mode
  938. */
  939. void MobMovementManager::UpdatePathBoat(Entity *who, float x, float y, float z, MobMovementMode mode)
  940. {
  941. MobListMutex.readlock();
  942. auto eiter = _impl->Entries.find(who);
  943. if (eiter == _impl->Entries.end())
  944. {
  945. MobListMutex.releasereadlock();
  946. return; // does not exist in navigation
  947. }
  948. auto &ent = (*eiter);
  949. PushSwimTo(ent.second, x, y, z, mode);
  950. PushStopMoving(ent.second);
  951. MobListMutex.releasereadlock();
  952. }
  953. /**
  954. * @param ent
  955. * @param x
  956. * @param y
  957. * @param z
  958. * @param heading
  959. */
  960. void MobMovementManager::PushTeleportTo(MobMovementEntry &ent, float x, float y, float z, float heading)
  961. {
  962. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new TeleportToCommand(x, y, z, heading)));
  963. }
  964. /**
  965. * @param ent
  966. * @param x
  967. * @param y
  968. * @param z
  969. * @param mob_movement_mode
  970. */
  971. void MobMovementManager::PushMoveTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
  972. {
  973. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new MoveToCommand(x, y, z, mob_movement_mode)));
  974. }
  975. /**
  976. * @param ent
  977. * @param x
  978. * @param y
  979. * @param z
  980. * @param mob_movement_mode
  981. */
  982. void MobMovementManager::PushSwimTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
  983. {
  984. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new SwimToCommand(x, y, z, mob_movement_mode)));
  985. }
  986. /**
  987. * @param ent
  988. * @param who
  989. * @param to
  990. * @param mob_movement_mode
  991. */
  992. void MobMovementManager::PushRotateTo(MobMovementEntry &ent, Entity *who, float to, MobMovementMode mob_movement_mode)
  993. {
  994. auto from = FixHeading(who->GetHeading());
  995. to = FixHeading(to);
  996. float diff = to - from;
  997. if (std::abs(diff) < 0.001f) {
  998. return;
  999. }
  1000. while (diff < -256.0) {
  1001. diff += 512.0;
  1002. }
  1003. while (diff > 256) {
  1004. diff -= 512.0;
  1005. }
  1006. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new RotateToCommand(to, diff > 0 ? 1.0 : -1.0, mob_movement_mode)));
  1007. }
  1008. /**
  1009. * @param mob_movement_entry
  1010. */
  1011. void MobMovementManager::PushStopMoving(MobMovementEntry &mob_movement_entry)
  1012. {
  1013. mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new StopMovingCommand()));
  1014. }
  1015. /**
  1016. * @param mob_movement_entry
  1017. */
  1018. void MobMovementManager::PushEvadeCombat(MobMovementEntry &mob_movement_entry)
  1019. {
  1020. mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new EvadeCombatCommand()));
  1021. }
  1022. /**
  1023. * @param who
  1024. * @param x
  1025. * @param y
  1026. * @param z
  1027. * @param mob_movement_mode
  1028. */
  1029. void MobMovementManager::HandleStuckBehavior(Entity *who, float x, float y, float z, MobMovementMode mob_movement_mode)
  1030. {
  1031. //LogDebug("Handle stuck behavior for {0} at ({1}, {2}, {3}) with movement_mode {4}", who->GetName(), x, y, z, mob_movement_mode);
  1032. MobListMutex.readlock();
  1033. auto sb = RunToTarget;//who->GetStuckBehavior();
  1034. MobStuckBehavior behavior = RunToTarget;
  1035. if (sb >= 0 && sb < MaxStuckBehavior) {
  1036. behavior = (MobStuckBehavior) sb;
  1037. }
  1038. auto eiter = _impl->Entries.find(who);
  1039. if (eiter == _impl->Entries.end())
  1040. {
  1041. MobListMutex.releasereadlock();
  1042. return; // does not exist in navigation
  1043. }
  1044. auto &ent = (*eiter);
  1045. switch (sb) {
  1046. case RunToTarget:
  1047. PushMoveTo(ent.second, x, y, z, mob_movement_mode);
  1048. PushStopMoving(ent.second);
  1049. break;
  1050. case WarpToTarget:
  1051. PushTeleportTo(ent.second, x, y, z, 0.0f);
  1052. PushStopMoving(ent.second);
  1053. break;
  1054. case TakeNoAction:
  1055. PushStopMoving(ent.second);
  1056. break;
  1057. case EvadeCombat:
  1058. PushEvadeCombat(ent.second);
  1059. break;
  1060. }
  1061. MobListMutex.releasereadlock();
  1062. }