mob_movement_manager.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. auto iter = _impl->Entries.find(mob);
  456. if(iter != _impl->Entries.end())
  457. _impl->Entries.erase(iter);
  458. MobListMutex.releasewritelock();
  459. }
  460. /**
  461. * @param client
  462. */
  463. void MobMovementManager::AddClient(Client *client)
  464. {
  465. _impl->Clients.push_back(client);
  466. }
  467. /**
  468. * @param client
  469. */
  470. void MobMovementManager::RemoveClient(Client *client)
  471. {
  472. auto iter = _impl->Clients.begin();
  473. while (iter != _impl->Clients.end()) {
  474. if (client == *iter) {
  475. _impl->Clients.erase(iter);
  476. return;
  477. }
  478. ++iter;
  479. }
  480. }
  481. /**
  482. * @param who
  483. * @param to
  484. * @param mob_movement_mode
  485. */
  486. void MobMovementManager::RotateTo(Entity *who, float to, MobMovementMode mob_movement_mode)
  487. {
  488. MobListMutex.readlock();
  489. auto iter = _impl->Entries.find(who);
  490. if (iter == _impl->Entries.end())
  491. {
  492. MobListMutex.releasereadlock();
  493. return; // does not exist in navigation
  494. }
  495. auto &ent = (*iter);
  496. if (true != ent.second.Commands.empty()) {
  497. MobListMutex.releasereadlock();
  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. if (iter == _impl->Entries.end())
  515. {
  516. MobListMutex.releasereadlock();
  517. return; // does not exist in navigation
  518. }
  519. auto &ent = (*iter);
  520. ent.second.Commands.clear();
  521. PushTeleportTo(ent.second, x, y, z, heading);
  522. MobListMutex.releasereadlock();
  523. }
  524. /**
  525. * @param who
  526. * @param x
  527. * @param y
  528. * @param z
  529. * @param mode
  530. */
  531. void MobMovementManager::NavigateTo(Entity *who, float x, float y, float z, MobMovementMode mode, bool overrideDistance)
  532. {
  533. glm::vec3 targPos(x, z, y);
  534. glm::vec3 origPos(who->GetX(), who->GetZ(), who->GetY());
  535. if (IsPositionEqualWithinCertainZ(targPos, origPos, 6.0f)) {
  536. return;
  537. }
  538. MobListMutex.readlock();
  539. auto iter = _impl->Entries.find(who);
  540. if (iter == _impl->Entries.end())
  541. {
  542. MobListMutex.releasereadlock();
  543. return; // does not exist in navigation
  544. }
  545. auto &ent = (*iter);
  546. auto &nav = ent.second.NavTo;
  547. double current_time = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  548. if ((current_time - nav.last_set_time) > 0.5) {
  549. //Can potentially recalc
  550. auto within = IsPositionWithinSimpleCylinder(
  551. glm::vec3(who->GetX(), who->GetZ(), who->GetY()),
  552. glm::vec3(nav.navigate_to_x, nav.navigate_to_z, nav.navigate_to_y),
  553. 1.5f,
  554. 6.0f
  555. );
  556. who->MCommandMutex.writelock();
  557. if (within && ent.second.Commands.size() > 0 && nav.last_set_time != 0)
  558. {
  559. //who->ClearRunningLocations();
  560. //StopNavigation((Entity*)who);
  561. who->MCommandMutex.releasewritelock();
  562. MobListMutex.releasereadlock();
  563. return;
  564. }
  565. else if (!within && ent.second.Commands.size() > 0 && nav.last_set_time != 0)
  566. {
  567. who->MCommandMutex.releasewritelock();
  568. MobListMutex.releasereadlock();
  569. return;
  570. }
  571. LogWrite(MAP__DEBUG, 0, "Map", "%s %f %f %f: within: %i, commands: %i, lastnav: %f %f %f", who->GetName(),
  572. who->GetX(),who->GetY(),who->GetZ(),within,
  573. ent.second.Commands.size(), nav.navigate_to_x, nav.navigate_to_y, nav.navigate_to_z);
  574. //auto heading_match = IsHeadingEqual(0.0, nav.navigate_to_heading);
  575. //if (/*false == within ||*/ false == heading_match || ent.second.Commands.size() == 0) {
  576. ent.second.Commands.clear();
  577. //Path is no longer valid, calculate a new path
  578. UpdatePath(who, x, y, z, mode);
  579. nav.navigate_to_x = x;
  580. nav.navigate_to_y = y;
  581. nav.navigate_to_z = z;
  582. nav.navigate_to_heading = 0.0;
  583. nav.last_set_time = current_time;
  584. who->MCommandMutex.releasewritelock();
  585. //}
  586. }
  587. MobListMutex.releasereadlock();
  588. }
  589. /**
  590. * @param who
  591. */
  592. void MobMovementManager::StopNavigation(Entity *who)
  593. {
  594. MobListMutex.readlock();
  595. auto iter = _impl->Entries.find(who);
  596. if (iter == _impl->Entries.end())
  597. {
  598. MobListMutex.releasereadlock();
  599. return; // does not exist in navigation
  600. }
  601. auto &ent = (*iter);
  602. auto &nav = ent.second.NavTo;
  603. nav.navigate_to_x = 0.0;
  604. nav.navigate_to_y = 0.0;
  605. nav.navigate_to_z = 0.0;
  606. nav.navigate_to_heading = 0.0;
  607. nav.last_set_time = 0.0;
  608. who->MCommandMutex.writelock();
  609. if (true == ent.second.Commands.empty()) {
  610. PushStopMoving(ent.second);
  611. who->MCommandMutex.releasewritelock();
  612. MobListMutex.releasereadlock();
  613. return;
  614. }
  615. if (!who->IsRunning()) {
  616. ent.second.Commands.clear();
  617. who->MCommandMutex.releasewritelock();
  618. MobListMutex.releasereadlock();
  619. return;
  620. }
  621. ent.second.Commands.clear();
  622. PushStopMoving(ent.second);
  623. who->MCommandMutex.releasewritelock();
  624. MobListMutex.releasereadlock();
  625. }
  626. void MobMovementManager::DisruptNavigation(Entity* who)
  627. {
  628. MobListMutex.readlock();
  629. auto iter = _impl->Entries.find(who);
  630. if (iter == _impl->Entries.end())
  631. {
  632. MobListMutex.releasereadlock();
  633. return; // does not exist in navigation
  634. }
  635. auto& ent = (*iter);
  636. auto& nav = ent.second.NavTo;
  637. nav.navigate_to_x = 0.0;
  638. nav.navigate_to_y = 0.0;
  639. nav.navigate_to_z = 0.0;
  640. nav.navigate_to_heading = 0.0;
  641. nav.last_set_time = 0.0;
  642. if (!who->IsRunning()) {
  643. who->MCommandMutex.writelock();
  644. ent.second.Commands.clear();
  645. who->MCommandMutex.releasewritelock();
  646. }
  647. MobListMutex.releasereadlock();
  648. }
  649. /**
  650. * @param in
  651. * @return
  652. */
  653. float MobMovementManager::FixHeading(float in)
  654. {
  655. auto h = in;
  656. while (h > 512.0) {
  657. h -= 512.0;
  658. }
  659. while (h < 0.0) {
  660. h += 512.0;
  661. }
  662. return h;
  663. }
  664. void MobMovementManager::ClearStats()
  665. {
  666. _impl->Stats.LastResetTime = static_cast<double>(Timer::GetCurrentTime2()) / 1000.0;
  667. _impl->Stats.TotalSent = 0;
  668. _impl->Stats.TotalSentHeading = 0;
  669. _impl->Stats.TotalSentMovement = 0;
  670. _impl->Stats.TotalSentPosition = 0;
  671. }
  672. /**
  673. * @param who
  674. * @param x
  675. * @param y
  676. * @param z
  677. * @param mob_movement_mode
  678. */
  679. void MobMovementManager::UpdatePath(Entity *who, float x, float y, float z, MobMovementMode mob_movement_mode)
  680. {
  681. if (!who->GetMap() /*|| !zone->HasWaterMap()*/) {
  682. MobListMutex.readlock();
  683. auto iter = _impl->Entries.find(who);
  684. if (iter == _impl->Entries.end())
  685. {
  686. MobListMutex.releasereadlock();
  687. return; // does not exist in navigation
  688. }
  689. auto &ent = (*iter);
  690. PushMoveTo(ent.second, x, y, z, mob_movement_mode);
  691. PushStopMoving(ent.second);
  692. MobListMutex.releasereadlock();
  693. return;
  694. }
  695. /*
  696. if (who-?()) {
  697. UpdatePathBoat(who, x, y, z, mob_movement_mode);
  698. }
  699. else if (who->IsUnderwaterOnly()) {
  700. UpdatePathUnderwater(who, x, y, z, mob_movement_mode);
  701. }*/
  702. //else {
  703. UpdatePathGround(who, x, y, z, mob_movement_mode);
  704. //}
  705. }
  706. /**
  707. * @param who
  708. * @param x
  709. * @param y
  710. * @param z
  711. * @param mode
  712. */
  713. void MobMovementManager::UpdatePathGround(Entity *who, float x, float y, float z, MobMovementMode mode)
  714. {
  715. PathfinderOptions opts;
  716. opts.smooth_path = true;
  717. opts.step_size = 100.0f;//RuleR(Pathing, NavmeshStepSize);
  718. opts.offset = who->GetYOffset()+1.0f;
  719. opts.flags = PathingNotDisabled ^ PathingZoneLine;
  720. //This is probably pointless since the nav mesh tool currently sets zonelines to disabled anyway
  721. auto partial = false;
  722. auto stuck = false;
  723. auto route = who->GetZone()->pathing->FindPath(
  724. glm::vec3(who->GetX(), who->GetZ(), who->GetY()),
  725. glm::vec3(x, z, y),
  726. partial,
  727. stuck,
  728. opts
  729. );
  730. MobListMutex.readlock();
  731. auto eiter = _impl->Entries.find(who);
  732. if (eiter == _impl->Entries.end())
  733. {
  734. MobListMutex.releasereadlock();
  735. return; // does not exist in navigation
  736. }
  737. auto &ent = (*eiter);
  738. if (route.size() == 0) {
  739. HandleStuckBehavior(who, x, y, z, mode);
  740. MobListMutex.releasereadlock();
  741. return;
  742. }
  743. AdjustRoute(route, who);
  744. //avoid doing any processing if the mob is stuck to allow normal stuck code to work.
  745. if (!stuck) {
  746. //there are times when the routes returned are no differen than where the mob is currently standing. What basically happens
  747. //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.
  748. //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
  749. //"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
  750. //have stuck mobs.
  751. auto routeNode = route.begin();
  752. bool noValidPath = true;
  753. while (routeNode != route.end() && noValidPath == true) {
  754. auto &currentNode = (*routeNode);
  755. if (routeNode == route.end()) {
  756. continue;
  757. }
  758. if (!(currentNode.pos.x == who->GetX() && currentNode.pos.y == who->GetZ())) {
  759. //if one of the nodes to move to, is not our current node, pass it.
  760. noValidPath = false;
  761. break;
  762. }
  763. //move to the next node
  764. routeNode++;
  765. }
  766. if (noValidPath) {
  767. //we are 'stuck' in a path, lets just get out of this by 'teleporting' to the next position.
  768. PushTeleportTo(
  769. ent.second,
  770. x,
  771. y,
  772. z,
  773. CalculateHeadingAngleBetweenPositions(who->GetX(), who->GetZ(), x, z)
  774. );
  775. MobListMutex.releasereadlock();
  776. return;
  777. }
  778. }
  779. auto iter = route.begin();
  780. glm::vec3 previous_pos(who->GetX(), who->GetZ(), who->GetY());
  781. bool first_node = true;
  782. while (iter != route.end()) {
  783. auto &current_node = (*iter);
  784. iter++;
  785. if (iter == route.end()) {
  786. continue;
  787. }
  788. previous_pos = current_node.pos;
  789. auto &next_node = (*iter);
  790. if (first_node) {
  791. if (mode == MovementWalking) {
  792. auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
  793. PushRotateTo(ent.second, who, h, mode);
  794. }
  795. first_node = false;
  796. }
  797. //move to / teleport to node + 1
  798. if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
  799. float calcedHeading =
  800. CalculateHeadingAngleBetweenPositions(
  801. current_node.pos.x,
  802. current_node.pos.y,
  803. next_node.pos.x,
  804. next_node.pos.y
  805. );
  806. PushTeleportTo(
  807. ent.second,
  808. next_node.pos.x,
  809. next_node.pos.z,
  810. next_node.pos.y,
  811. calcedHeading
  812. );
  813. }
  814. else {
  815. /* if (who->GetZone()->watermap->InLiquid(previous_pos)) {
  816. PushSwimTo(ent.second, next_node.pos.x, next_node.pos.y, next_node.pos.z, mode);
  817. }
  818. else {*/
  819. PushMoveTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, mode);
  820. // }
  821. }
  822. }
  823. if (stuck) {
  824. HandleStuckBehavior(who, x, y, z, mode);
  825. }
  826. else {
  827. PushStopMoving(ent.second);
  828. }
  829. MobListMutex.releasereadlock();
  830. }
  831. /**
  832. * @param who
  833. * @param x
  834. * @param y
  835. * @param z
  836. * @param movement_mode
  837. */
  838. void MobMovementManager::UpdatePathUnderwater(Entity *who, float x, float y, float z, MobMovementMode movement_mode)
  839. {
  840. MobListMutex.readlock();
  841. auto eiter = _impl->Entries.find(who);
  842. if (eiter == _impl->Entries.end())
  843. {
  844. MobListMutex.releasereadlock();
  845. return; // does not exist in navigation
  846. }
  847. auto &ent = (*eiter);
  848. if (/*zone->watermap->InLiquid(who->GetPosition()) && zone->watermap->InLiquid(glm::vec3(x, y, z)) &&*/
  849. who->GetMap()->CheckLoS(glm::vec3(who->GetX(),who->GetZ(),who->GetY()), glm::vec3(x, y, z))) {
  850. PushSwimTo(ent.second, x, y, z, movement_mode);
  851. PushStopMoving(ent.second);
  852. MobListMutex.releasereadlock();
  853. return;
  854. }
  855. PathfinderOptions opts;
  856. opts.smooth_path = true;
  857. opts.step_size = 100.0f;// RuleR(Pathing, NavmeshStepSize);
  858. opts.offset = who->GetYOffset();
  859. opts.flags = PathingNotDisabled ^ PathingZoneLine;
  860. auto partial = false;
  861. auto stuck = false;
  862. auto route = who->GetZone()->pathing->FindPath(
  863. glm::vec3(who->GetX(), who->GetY(), who->GetZ()),
  864. glm::vec3(x, y, z),
  865. partial,
  866. stuck,
  867. opts
  868. );
  869. if (route.size() == 0) {
  870. HandleStuckBehavior(who, x, z, y, movement_mode);
  871. MobListMutex.releasereadlock();
  872. return;
  873. }
  874. AdjustRoute(route, who);
  875. auto iter = route.begin();
  876. glm::vec3 previous_pos(who->GetX(), who->GetY(), who->GetZ());
  877. bool first_node = true;
  878. while (iter != route.end()) {
  879. auto &current_node = (*iter);
  880. /* if (!zone->watermap->InLiquid(current_node.pos)) {
  881. stuck = true;
  882. while (iter != route.end()) {
  883. iter = route.erase(iter);
  884. }
  885. break;
  886. }
  887. else {*/
  888. iter++;
  889. // }
  890. }
  891. if (route.size() == 0) {
  892. HandleStuckBehavior(who, x, y, z, movement_mode);
  893. MobListMutex.releasereadlock();
  894. return;
  895. }
  896. iter = route.begin();
  897. while (iter != route.end()) {
  898. auto &current_node = (*iter);
  899. iter++;
  900. if (iter == route.end()) {
  901. continue;
  902. }
  903. previous_pos = current_node.pos;
  904. auto &next_node = (*iter);
  905. if (first_node) {
  906. if (movement_mode == MovementWalking) {
  907. auto h = who->GetFaceTarget(next_node.pos.x, next_node.pos.y);
  908. PushRotateTo(ent.second, who, h, movement_mode);
  909. }
  910. first_node = false;
  911. }
  912. //move to / teleport to node + 1
  913. if (next_node.teleport && next_node.pos.x != 0.0f && next_node.pos.y != 0.0f) {
  914. float calcHeading = CalculateHeadingAngleBetweenPositions(
  915. current_node.pos.x,
  916. current_node.pos.y,
  917. next_node.pos.x,
  918. next_node.pos.y
  919. );
  920. PushTeleportTo(
  921. ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, calcHeading);
  922. }
  923. else {
  924. PushSwimTo(ent.second, next_node.pos.x, next_node.pos.z, next_node.pos.y, movement_mode);
  925. }
  926. }
  927. if (stuck) {
  928. HandleStuckBehavior(who, x, y, z, movement_mode);
  929. }
  930. else {
  931. PushStopMoving(ent.second);
  932. }
  933. MobListMutex.releasereadlock();
  934. }
  935. /**
  936. * @param who
  937. * @param x
  938. * @param y
  939. * @param z
  940. * @param mode
  941. */
  942. void MobMovementManager::UpdatePathBoat(Entity *who, float x, float y, float z, MobMovementMode mode)
  943. {
  944. MobListMutex.readlock();
  945. auto eiter = _impl->Entries.find(who);
  946. if (eiter == _impl->Entries.end())
  947. {
  948. MobListMutex.releasereadlock();
  949. return; // does not exist in navigation
  950. }
  951. auto &ent = (*eiter);
  952. PushSwimTo(ent.second, x, y, z, mode);
  953. PushStopMoving(ent.second);
  954. MobListMutex.releasereadlock();
  955. }
  956. /**
  957. * @param ent
  958. * @param x
  959. * @param y
  960. * @param z
  961. * @param heading
  962. */
  963. void MobMovementManager::PushTeleportTo(MobMovementEntry &ent, float x, float y, float z, float heading)
  964. {
  965. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new TeleportToCommand(x, y, z, heading)));
  966. }
  967. /**
  968. * @param ent
  969. * @param x
  970. * @param y
  971. * @param z
  972. * @param mob_movement_mode
  973. */
  974. void MobMovementManager::PushMoveTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
  975. {
  976. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new MoveToCommand(x, y, z, mob_movement_mode)));
  977. }
  978. /**
  979. * @param ent
  980. * @param x
  981. * @param y
  982. * @param z
  983. * @param mob_movement_mode
  984. */
  985. void MobMovementManager::PushSwimTo(MobMovementEntry &ent, float x, float y, float z, MobMovementMode mob_movement_mode)
  986. {
  987. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new SwimToCommand(x, y, z, mob_movement_mode)));
  988. }
  989. /**
  990. * @param ent
  991. * @param who
  992. * @param to
  993. * @param mob_movement_mode
  994. */
  995. void MobMovementManager::PushRotateTo(MobMovementEntry &ent, Entity *who, float to, MobMovementMode mob_movement_mode)
  996. {
  997. auto from = FixHeading(who->GetHeading());
  998. to = FixHeading(to);
  999. float diff = to - from;
  1000. if (std::abs(diff) < 0.001f) {
  1001. return;
  1002. }
  1003. while (diff < -256.0) {
  1004. diff += 512.0;
  1005. }
  1006. while (diff > 256) {
  1007. diff -= 512.0;
  1008. }
  1009. ent.Commands.push_back(std::unique_ptr<IMovementCommand>(new RotateToCommand(to, diff > 0 ? 1.0 : -1.0, mob_movement_mode)));
  1010. }
  1011. /**
  1012. * @param mob_movement_entry
  1013. */
  1014. void MobMovementManager::PushStopMoving(MobMovementEntry &mob_movement_entry)
  1015. {
  1016. mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new StopMovingCommand()));
  1017. }
  1018. /**
  1019. * @param mob_movement_entry
  1020. */
  1021. void MobMovementManager::PushEvadeCombat(MobMovementEntry &mob_movement_entry)
  1022. {
  1023. mob_movement_entry.Commands.push_back(std::unique_ptr<IMovementCommand>(new EvadeCombatCommand()));
  1024. }
  1025. /**
  1026. * @param who
  1027. * @param x
  1028. * @param y
  1029. * @param z
  1030. * @param mob_movement_mode
  1031. */
  1032. void MobMovementManager::HandleStuckBehavior(Entity *who, float x, float y, float z, MobMovementMode mob_movement_mode)
  1033. {
  1034. //LogDebug("Handle stuck behavior for {0} at ({1}, {2}, {3}) with movement_mode {4}", who->GetName(), x, y, z, mob_movement_mode);
  1035. MobListMutex.readlock();
  1036. auto sb = RunToTarget;//who->GetStuckBehavior();
  1037. MobStuckBehavior behavior = RunToTarget;
  1038. if (sb >= 0 && sb < MaxStuckBehavior) {
  1039. behavior = (MobStuckBehavior) sb;
  1040. }
  1041. auto eiter = _impl->Entries.find(who);
  1042. if (eiter == _impl->Entries.end())
  1043. {
  1044. MobListMutex.releasereadlock();
  1045. return; // does not exist in navigation
  1046. }
  1047. auto &ent = (*eiter);
  1048. switch (sb) {
  1049. case RunToTarget:
  1050. PushMoveTo(ent.second, x, y, z, mob_movement_mode);
  1051. PushStopMoving(ent.second);
  1052. break;
  1053. case WarpToTarget:
  1054. PushTeleportTo(ent.second, x, y, z, 0.0f);
  1055. PushStopMoving(ent.second);
  1056. break;
  1057. case TakeNoAction:
  1058. PushStopMoving(ent.second);
  1059. break;
  1060. case EvadeCombat:
  1061. PushEvadeCombat(ent.second);
  1062. break;
  1063. }
  1064. MobListMutex.releasereadlock();
  1065. }