mob_movement_manager.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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. if (mob->IsRunning()) {
  348. mob->StopMoving();
  349. }
  350. return true;
  351. }
  352. virtual bool Started() const
  353. {
  354. return false;
  355. }
  356. };
  357. struct MovementStats {
  358. MovementStats()
  359. {
  360. LastResetTime = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  361. TotalSent = 0ULL;
  362. TotalSentMovement = 0ULL;
  363. TotalSentPosition = 0ULL;
  364. TotalSentHeading = 0ULL;
  365. }
  366. double LastResetTime;
  367. uint64_t TotalSent;
  368. uint64_t TotalSentMovement;
  369. uint64_t TotalSentPosition;
  370. uint64_t TotalSentHeading;
  371. };
  372. struct NavigateTo {
  373. NavigateTo()
  374. {
  375. navigate_to_x = 0.0;
  376. navigate_to_y = 0.0;
  377. navigate_to_z = 0.0;
  378. navigate_to_heading = 0.0;
  379. last_set_time = 0.0;
  380. }
  381. double navigate_to_x;
  382. double navigate_to_y;
  383. double navigate_to_z;
  384. double navigate_to_heading;
  385. double last_set_time;
  386. };
  387. struct MobMovementEntry {
  388. std::deque<std::unique_ptr<IMovementCommand>> Commands;
  389. NavigateTo NavTo;
  390. };
  391. void AdjustRoute(std::list<IPathfinder::IPathNode> &nodes, Entity *who)
  392. {
  393. if (who->GetZone() == nullptr || !who->GetMap() /*|| !zone->HasWaterMap()*/) {
  394. return;
  395. }
  396. auto offset = who->GetYOffset();
  397. for (auto &node : nodes) {
  398. //if (!zone->watermap->InLiquid(node.pos)) {
  399. auto best_z = who->GetMap()->FindBestZ(node.pos, nullptr);
  400. if (best_z != BEST_Z_INVALID) {
  401. node.pos.z = best_z + offset;
  402. }
  403. //} // todo: floating logic?
  404. }
  405. }
  406. struct MobMovementManager::Implementation {
  407. std::map<Entity *, MobMovementEntry> Entries;
  408. std::vector<Client *> Clients;
  409. MovementStats Stats;
  410. };
  411. MobMovementManager::MobMovementManager()
  412. {
  413. MobListMutex.SetName("MobMovementManager");
  414. _impl.reset(new Implementation());
  415. }
  416. MobMovementManager::~MobMovementManager()
  417. {
  418. }
  419. void MobMovementManager::Process()
  420. {
  421. MobListMutex.readlock();
  422. for (auto &iter : _impl->Entries) {
  423. auto &ent = iter.second;
  424. auto &commands = ent.Commands;
  425. if (commands.size() < 1)
  426. continue;
  427. iter.first->MCommandMutex.writelock();
  428. while (true != commands.empty()) {
  429. auto &cmd = commands.front();
  430. auto r = cmd->Process(this, iter.first);
  431. if (true != r) {
  432. break;
  433. }
  434. commands.pop_front();
  435. }
  436. iter.first->MCommandMutex.releasewritelock();
  437. }
  438. MobListMutex.releasereadlock();
  439. }
  440. /**
  441. * @param mob
  442. */
  443. void MobMovementManager::AddMob(Entity *mob)
  444. {
  445. MobListMutex.writelock();
  446. _impl->Entries.insert(std::make_pair(mob, MobMovementEntry()));
  447. MobListMutex.releasewritelock();
  448. }
  449. /**
  450. * @param mob
  451. */
  452. void MobMovementManager::RemoveMob(Entity *mob)
  453. {
  454. MobListMutex.writelock();
  455. _impl->Entries.erase(mob);
  456. MobListMutex.releasewritelock();
  457. }
  458. /**
  459. * @param client
  460. */
  461. void MobMovementManager::AddClient(Client *client)
  462. {
  463. _impl->Clients.push_back(client);
  464. }
  465. /**
  466. * @param client
  467. */
  468. void MobMovementManager::RemoveClient(Client *client)
  469. {
  470. auto iter = _impl->Clients.begin();
  471. while (iter != _impl->Clients.end()) {
  472. if (client == *iter) {
  473. _impl->Clients.erase(iter);
  474. return;
  475. }
  476. ++iter;
  477. }
  478. }
  479. /**
  480. * @param who
  481. * @param to
  482. * @param mob_movement_mode
  483. */
  484. void MobMovementManager::RotateTo(Entity *who, float to, MobMovementMode mob_movement_mode)
  485. {
  486. MobListMutex.readlock();
  487. auto iter = _impl->Entries.find(who);
  488. if (iter == _impl->Entries.end())
  489. {
  490. MobListMutex.releasereadlock();
  491. return; // does not exist in navigation
  492. }
  493. auto &ent = (*iter);
  494. if (true != ent.second.Commands.empty()) {
  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. MobListMutex.releasereadlock();
  643. who->MCommandMutex.releasewritelock();
  644. return;
  645. }
  646. }
  647. /**
  648. * @param in
  649. * @return
  650. */
  651. float MobMovementManager::FixHeading(float in)
  652. {
  653. auto h = in;
  654. while (h > 512.0) {
  655. h -= 512.0;
  656. }
  657. while (h < 0.0) {
  658. h += 512.0;
  659. }
  660. return h;
  661. }
  662. void MobMovementManager::ClearStats()
  663. {
  664. _impl->Stats.LastResetTime = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  665. _impl->Stats.TotalSent = 0;
  666. _impl->Stats.TotalSentHeading = 0;
  667. _impl->Stats.TotalSentMovement = 0;
  668. _impl->Stats.TotalSentPosition = 0;
  669. }
  670. /**
  671. * @param who
  672. * @param x
  673. * @param y
  674. * @param z
  675. * @param mob_movement_mode
  676. */
  677. void MobMovementManager::UpdatePath(Entity *who, float x, float y, float z, MobMovementMode mob_movement_mode)
  678. {
  679. if (!who->GetMap() /*|| !zone->HasWaterMap()*/) {
  680. MobListMutex.readlock();
  681. auto iter = _impl->Entries.find(who);
  682. if (iter == _impl->Entries.end())
  683. {
  684. MobListMutex.releasereadlock();
  685. return; // does not exist in navigation
  686. }
  687. auto &ent = (*iter);
  688. PushMoveTo(ent.second, x, y, z, mob_movement_mode);
  689. PushStopMoving(ent.second);
  690. MobListMutex.releasereadlock();
  691. return;
  692. }
  693. /*
  694. if (who-?()) {
  695. UpdatePathBoat(who, x, y, z, mob_movement_mode);
  696. }
  697. else if (who->IsUnderwaterOnly()) {
  698. UpdatePathUnderwater(who, x, y, z, mob_movement_mode);
  699. }*/
  700. //else {
  701. UpdatePathGround(who, x, y, z, mob_movement_mode);
  702. //}
  703. }
  704. /**
  705. * @param who
  706. * @param x
  707. * @param y
  708. * @param z
  709. * @param mode
  710. */
  711. void MobMovementManager::UpdatePathGround(Entity *who, float x, float y, float z, MobMovementMode mode)
  712. {
  713. PathfinderOptions opts;
  714. opts.smooth_path = true;
  715. opts.step_size = 100.0f;//RuleR(Pathing, NavmeshStepSize);
  716. opts.offset = who->GetYOffset()+1.0f;
  717. opts.flags = PathingNotDisabled ^ PathingZoneLine;
  718. //This is probably pointless since the nav mesh tool currently sets zonelines to disabled anyway
  719. auto partial = false;
  720. auto stuck = false;
  721. auto route = who->GetZone()->pathing->FindPath(
  722. glm::vec3(who->GetX(), who->GetZ(), who->GetY()),
  723. glm::vec3(x, z, y),
  724. partial,
  725. stuck,
  726. opts
  727. );
  728. MobListMutex.readlock();
  729. auto eiter = _impl->Entries.find(who);
  730. if (eiter == _impl->Entries.end())
  731. {
  732. MobListMutex.releasereadlock();
  733. return; // does not exist in navigation
  734. }
  735. auto &ent = (*eiter);
  736. if (route.size() == 0) {
  737. HandleStuckBehavior(who, x, y, z, mode);
  738. MobListMutex.releasereadlock();
  739. return;
  740. }
  741. AdjustRoute(route, who);
  742. //avoid doing any processing if the mob is stuck to allow normal stuck code to work.
  743. if (!stuck) {
  744. //there are times when the routes returned are no differen than where the mob is currently standing. What basically happens
  745. //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.
  746. //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
  747. //"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
  748. //have stuck mobs.
  749. auto routeNode = route.begin();
  750. bool noValidPath = true;
  751. while (routeNode != route.end() && noValidPath == true) {
  752. auto &currentNode = (*routeNode);
  753. if (routeNode == route.end()) {
  754. continue;
  755. }
  756. if (!(currentNode.pos.x == who->GetX() && currentNode.pos.y == who->GetZ())) {
  757. //if one of the nodes to move to, is not our current node, pass it.
  758. noValidPath = false;
  759. break;
  760. }
  761. //move to the next node
  762. routeNode++;
  763. }
  764. if (noValidPath) {
  765. //we are 'stuck' in a path, lets just get out of this by 'teleporting' to the next position.
  766. PushTeleportTo(
  767. ent.second,
  768. x,
  769. y,
  770. z,
  771. CalculateHeadingAngleBetweenPositions(who->GetX(), who->GetZ(), x, z)
  772. );
  773. MobListMutex.releasereadlock();
  774. return;
  775. }
  776. }
  777. auto iter = route.begin();
  778. glm::vec3 previous_pos(who->GetX(), who->GetZ(), who->GetY());
  779. bool first_node = true;
  780. while (iter != route.end()) {
  781. auto &current_node = (*iter);
  782. iter++;
  783. if (iter == route.end()) {
  784. continue;
  785. }
  786. previous_pos = current_node.pos;
  787. auto &next_node = (*iter);
  788. if (first_node) {
  789. if (mode == MovementWalking) {
  790. auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
  791. PushRotateTo(ent.second, who, h, mode);
  792. }
  793. first_node = false;
  794. }
  795. //move to / teleport to node + 1
  796. if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
  797. float calcedHeading =
  798. CalculateHeadingAngleBetweenPositions(
  799. current_node.pos.x,
  800. current_node.pos.y,
  801. next_node.pos.x,
  802. next_node.pos.y
  803. );
  804. PushTeleportTo(
  805. ent.second,
  806. next_node.pos.x,
  807. next_node.pos.z,
  808. next_node.pos.y,
  809. calcedHeading
  810. );
  811. }
  812. else {
  813. /* if (who->GetZone()->watermap->InLiquid(previous_pos)) {
  814. PushSwimTo(ent.second, next_node.pos.x, next_node.pos.y, next_node.pos.z, mode);
  815. }
  816. else {*/
  817. PushMoveTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, mode);
  818. // }
  819. }
  820. }
  821. if (stuck) {
  822. HandleStuckBehavior(who, x, y, z, mode);
  823. }
  824. else {
  825. PushStopMoving(ent.second);
  826. }
  827. MobListMutex.releasereadlock();
  828. }
  829. /**
  830. * @param who
  831. * @param x
  832. * @param y
  833. * @param z
  834. * @param movement_mode
  835. */
  836. void MobMovementManager::UpdatePathUnderwater(Entity *who, float x, float y, float z, MobMovementMode movement_mode)
  837. {
  838. MobListMutex.readlock();
  839. auto eiter = _impl->Entries.find(who);
  840. if (eiter == _impl->Entries.end())
  841. {
  842. MobListMutex.releasereadlock();
  843. return; // does not exist in navigation
  844. }
  845. auto &ent = (*eiter);
  846. if (/*zone->watermap->InLiquid(who->GetPosition()) && zone->watermap->InLiquid(glm::vec3(x, y, z)) &&*/
  847. who->GetMap()->CheckLoS(glm::vec3(who->GetX(),who->GetZ(),who->GetY()), glm::vec3(x, y, z))) {
  848. PushSwimTo(ent.second, x, y, z, movement_mode);
  849. PushStopMoving(ent.second);
  850. MobListMutex.releasereadlock();
  851. return;
  852. }
  853. PathfinderOptions opts;
  854. opts.smooth_path = true;
  855. opts.step_size = 100.0f;// RuleR(Pathing, NavmeshStepSize);
  856. opts.offset = who->GetYOffset();
  857. opts.flags = PathingNotDisabled ^ PathingZoneLine;
  858. auto partial = false;
  859. auto stuck = false;
  860. auto route = who->GetZone()->pathing->FindPath(
  861. glm::vec3(who->GetX(), who->GetY(), who->GetZ()),
  862. glm::vec3(x, y, z),
  863. partial,
  864. stuck,
  865. opts
  866. );
  867. if (route.size() == 0) {
  868. HandleStuckBehavior(who, x, z, y, movement_mode);
  869. MobListMutex.releasereadlock();
  870. return;
  871. }
  872. AdjustRoute(route, who);
  873. auto iter = route.begin();
  874. glm::vec3 previous_pos(who->GetX(), who->GetY(), who->GetZ());
  875. bool first_node = true;
  876. while (iter != route.end()) {
  877. auto &current_node = (*iter);
  878. /* if (!zone->watermap->InLiquid(current_node.pos)) {
  879. stuck = true;
  880. while (iter != route.end()) {
  881. iter = route.erase(iter);
  882. }
  883. break;
  884. }
  885. else {*/
  886. iter++;
  887. // }
  888. }
  889. if (route.size() == 0) {
  890. HandleStuckBehavior(who, x, y, z, movement_mode);
  891. MobListMutex.releasereadlock();
  892. return;
  893. }
  894. iter = route.begin();
  895. while (iter != route.end()) {
  896. auto &current_node = (*iter);
  897. iter++;
  898. if (iter == route.end()) {
  899. continue;
  900. }
  901. previous_pos = current_node.pos;
  902. auto &next_node = (*iter);
  903. if (first_node) {
  904. if (movement_mode == MovementWalking) {
  905. auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
  906. PushRotateTo(ent.second, who, h, movement_mode);
  907. }
  908. first_node = false;
  909. }
  910. //move to / teleport to node + 1
  911. if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
  912. float calcHeading = CalculateHeadingAngleBetweenPositions(
  913. current_node.pos.x,
  914. current_node.pos.y,
  915. next_node.pos.x,
  916. next_node.pos.y
  917. );
  918. PushTeleportTo(
  919. ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, calcHeading);
  920. }
  921. else {
  922. PushSwimTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, movement_mode);
  923. }
  924. }
  925. if (stuck) {
  926. HandleStuckBehavior(who, x, y, z, movement_mode);
  927. }
  928. else {
  929. PushStopMoving(ent.second);
  930. }
  931. MobListMutex.releasereadlock();
  932. }
  933. /**
  934. * @param who
  935. * @param x
  936. * @param y
  937. * @param z
  938. * @param mode
  939. */
  940. void MobMovementManager::UpdatePathBoat(Entity *who, float x, float y, float z, MobMovementMode mode)
  941. {
  942. MobListMutex.readlock();
  943. auto eiter = _impl->Entries.find(who);
  944. if (eiter == _impl->Entries.end())
  945. {
  946. MobListMutex.releasereadlock();
  947. return; // does not exist in navigation
  948. }
  949. auto &ent = (*eiter);
  950. PushSwimTo(ent.second, x, y, z, mode);
  951. PushStopMoving(ent.second);
  952. MobListMutex.releasereadlock();
  953. }
  954. /**
  955. * @param ent
  956. * @param x
  957. * @param y
  958. * @param z
  959. * @param heading
  960. */
  961. void MobMovementManager::PushTeleportTo(MobMovementEntry &ent, float x, float y, float z, float heading)
  962. {
  963. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new TeleportToCommand(x, y, z, heading)));
  964. }
  965. /**
  966. * @param ent
  967. * @param x
  968. * @param y
  969. * @param z
  970. * @param mob_movement_mode
  971. */
  972. void MobMovementManager::PushMoveTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
  973. {
  974. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new MoveToCommand(x, y, z, mob_movement_mode)));
  975. }
  976. /**
  977. * @param ent
  978. * @param x
  979. * @param y
  980. * @param z
  981. * @param mob_movement_mode
  982. */
  983. void MobMovementManager::PushSwimTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
  984. {
  985. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new SwimToCommand(x, y, z, mob_movement_mode)));
  986. }
  987. /**
  988. * @param ent
  989. * @param who
  990. * @param to
  991. * @param mob_movement_mode
  992. */
  993. void MobMovementManager::PushRotateTo(MobMovementEntry &ent, Entity *who, float to, MobMovementMode mob_movement_mode)
  994. {
  995. auto from = FixHeading(who->GetHeading());
  996. to = FixHeading(to);
  997. float diff = to - from;
  998. if (std::abs(diff) < 0.001f) {
  999. return;
  1000. }
  1001. while (diff < -256.0) {
  1002. diff += 512.0;
  1003. }
  1004. while (diff > 256) {
  1005. diff -= 512.0;
  1006. }
  1007. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new RotateToCommand(to, diff > 0 ? 1.0 : -1.0, mob_movement_mode)));
  1008. }
  1009. /**
  1010. * @param mob_movement_entry
  1011. */
  1012. void MobMovementManager::PushStopMoving(MobMovementEntry &mob_movement_entry)
  1013. {
  1014. mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new StopMovingCommand()));
  1015. }
  1016. /**
  1017. * @param mob_movement_entry
  1018. */
  1019. void MobMovementManager::PushEvadeCombat(MobMovementEntry &mob_movement_entry)
  1020. {
  1021. mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new EvadeCombatCommand()));
  1022. }
  1023. /**
  1024. * @param who
  1025. * @param x
  1026. * @param y
  1027. * @param z
  1028. * @param mob_movement_mode
  1029. */
  1030. void MobMovementManager::HandleStuckBehavior(Entity *who, float x, float y, float z, MobMovementMode mob_movement_mode)
  1031. {
  1032. //LogDebug("Handle stuck behavior for {0} at ({1}, {2}, {3}) with movement_mode {4}", who->GetName(), x, y, z, mob_movement_mode);
  1033. MobListMutex.readlock();
  1034. auto sb = RunToTarget;//who->GetStuckBehavior();
  1035. MobStuckBehavior behavior = RunToTarget;
  1036. if (sb >= 0 && sb < MaxStuckBehavior) {
  1037. behavior = (MobStuckBehavior) sb;
  1038. }
  1039. auto eiter = _impl->Entries.find(who);
  1040. if (eiter == _impl->Entries.end())
  1041. {
  1042. MobListMutex.releasereadlock();
  1043. return; // does not exist in navigation
  1044. }
  1045. auto &ent = (*eiter);
  1046. switch (sb) {
  1047. case RunToTarget:
  1048. PushMoveTo(ent.second, x, y, z, mob_movement_mode);
  1049. PushStopMoving(ent.second);
  1050. break;
  1051. case WarpToTarget:
  1052. PushTeleportTo(ent.second, x, y, z, 0.0f);
  1053. PushStopMoving(ent.second);
  1054. break;
  1055. case TakeNoAction:
  1056. PushStopMoving(ent.second);
  1057. break;
  1058. case EvadeCombat:
  1059. PushEvadeCombat(ent.second);
  1060. break;
  1061. }
  1062. MobListMutex.releasereadlock();
  1063. }