mob_movement_manager.cpp 28 KB

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