Spawn.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Spawn.h"
  17. #include <stdio.h>
  18. #include "../common/timer.h"
  19. #include <time.h>
  20. #include <math.h>
  21. #include "Entity.h"
  22. #include "Widget.h"
  23. #include "Sign.h"
  24. #include "../common/MiscFunctions.h"
  25. #include "../common/Log.h"
  26. #include "Rules/Rules.h"
  27. #include "World.h"
  28. #include "LuaInterface.h"
  29. #include "Zone/SPGrid.h"
  30. #include "Bots/Bot.h"
  31. #include "Zone/raycast_mesh.h"
  32. extern ConfigReader configReader;
  33. extern RuleManager rule_manager;
  34. extern World world;
  35. Spawn::Spawn(){
  36. group_id = 0;
  37. size_offset = 0;
  38. merchant_id = 0;
  39. memset(&appearance, 0, sizeof(AppearanceData));
  40. memset(&basic_info, 0, sizeof(BasicInfoStruct));
  41. appearance.encounter_level =6;
  42. size = 32;
  43. appearance.pos.collision_radius = 32;
  44. id = Spawn::NextID();
  45. oversized_packet = 0xFF;
  46. zone = 0;
  47. spawn_location_id = 0;
  48. spawn_entry_id = 0;
  49. spawn_location_spawns_id = 0;
  50. respawn = 0;
  51. expire_time = 0;
  52. expire_offset = 0;
  53. x_offset = 0;
  54. y_offset = 0;
  55. z_offset = 0;
  56. database_id = 0;
  57. packet_num = 1;
  58. changed = false;
  59. vis_changed = false;
  60. position_changed = false;
  61. send_spawn_changes = true;
  62. info_changed = false;
  63. appearance.pos.Speed1 = 0;
  64. last_attacker = 0;
  65. faction_id = 0;
  66. running_to = 0;
  67. tmp_visual_state = -1;
  68. tmp_action_state = -1;
  69. transporter_id = 0;
  70. invulnerable = false;
  71. spawn_group_list = 0;
  72. MSpawnGroup = 0;
  73. movement_locations = 0;
  74. MMovementLocations = 0;
  75. target = 0;
  76. primary_command_list_id = 0;
  77. secondary_command_list_id = 0;
  78. is_pet = false;
  79. m_followTarget = 0;
  80. following = false;
  81. req_quests_continued_access = false;
  82. req_quests_override = 0;
  83. req_quests_private = false;
  84. m_illusionModel = 0;
  85. Cell_Info.CurrentCell = nullptr;
  86. Cell_Info.CellListIndex = -1;
  87. m_addedToWorldTimestamp = 0;
  88. m_spawnAnim = 0;
  89. m_spawnAnimLeeway = 0;
  90. m_Update.SetName("Spawn::m_Update");
  91. m_requiredHistory.SetName("Spawn::m_requiredHistory");
  92. m_requiredQuests.SetName("Spawn::m_requiredQuests");
  93. last_heading_angle = 0.0;
  94. last_grid_update = 0;
  95. last_location_update = 0.0;
  96. last_movement_update = Timer::GetCurrentTime2();
  97. }
  98. Spawn::~Spawn(){
  99. for(int32 i=0;i<primary_command_list.size();i++){
  100. safe_delete(primary_command_list[i]);
  101. }
  102. for(int32 i=0;i<secondary_command_list.size();i++){
  103. safe_delete(secondary_command_list[i]);
  104. }
  105. RemoveSpawnFromGroup();
  106. if (MMovementLocations)
  107. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  108. if(movement_locations){
  109. while(movement_locations->size()){
  110. safe_delete(movement_locations->front());
  111. movement_locations->pop_front();
  112. }
  113. safe_delete(movement_locations);
  114. }
  115. if (MMovementLocations)
  116. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  117. safe_delete(MMovementLocations);
  118. MMovementLoop.lock();
  119. for (int32 i = 0; i < movement_loop.size(); i++)
  120. safe_delete(movement_loop.at(i));
  121. MMovementLoop.unlock();
  122. map<int32, vector<int16>* >::iterator rq_itr;
  123. m_requiredQuests.writelock(__FUNCTION__, __LINE__);
  124. for (rq_itr = required_quests.begin(); rq_itr != required_quests.end(); rq_itr++){
  125. safe_delete(rq_itr->second);
  126. }
  127. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  128. }
  129. void Spawn::InitializeHeaderPacketData(Player* player, PacketStruct* header, int16 index) {
  130. header->setDataByName("index", index);
  131. if (GetSpawnAnim() > 0 && Timer::GetCurrentTime2() < (GetAddedToWorldTimestamp() + GetSpawnAnimLeeway())) {
  132. if (header->GetVersion() >= 57080)
  133. header->setDataByName("spawn_anim", GetSpawnAnim());
  134. else
  135. header->setDataByName("spawn_anim", (int16)GetSpawnAnim());
  136. }
  137. else {
  138. if (header->GetVersion() >= 57080)
  139. header->setDataByName("spawn_anim", 0xFFFFFFFF);
  140. else
  141. header->setDataByName("spawn_anim", 0xFFFF);
  142. }
  143. if (primary_command_list.size() > 0){
  144. if (primary_command_list.size() > 1) {
  145. header->setArrayLengthByName("command_list", primary_command_list.size());
  146. for (int32 i = 0; i < primary_command_list.size(); i++) {
  147. header->setArrayDataByName("command_list_name", primary_command_list[i]->name.c_str(), i);
  148. header->setArrayDataByName("command_list_max_distance", primary_command_list[i]->distance, i);
  149. header->setArrayDataByName("command_list_error", primary_command_list[i]->error_text.c_str(), i);
  150. header->setArrayDataByName("command_list_command", primary_command_list[i]->command.c_str(), i);
  151. }
  152. }
  153. header->setMediumStringByName("default_command", primary_command_list[0]->command.c_str());
  154. header->setDataByName("max_distance", primary_command_list[0]->distance);
  155. }
  156. if (spawn_group_list && MSpawnGroup){
  157. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  158. header->setArrayLengthByName("group_size", spawn_group_list->size());
  159. vector<Spawn*>::iterator itr;
  160. int i = 0;
  161. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++, i++){
  162. header->setArrayDataByName("group_spawn_id", player->GetIDWithPlayerSpawn((*itr)), i);
  163. }
  164. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  165. }
  166. header->setDataByName("spawn_id", player->GetIDWithPlayerSpawn(this));
  167. header->setDataByName("crc", id);
  168. header->setDataByName("time_stamp", Timer::GetCurrentTime2());
  169. }
  170. void Spawn::InitializeVisPacketData(Player* player, PacketStruct* vis_packet) {
  171. int16 version = vis_packet->GetVersion();
  172. if (IsPlayer())
  173. appearance.pos.grid_id = 0xFFFFFFFF;
  174. if (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1){
  175. if (!IsGroundSpawn()){
  176. int8 arrow_color = ARROW_COLOR_WHITE;
  177. sint8 npc_con = player->GetFactions()->GetCon(faction_id);
  178. if (appearance.attackable == 1)
  179. arrow_color = player->GetArrowColor(GetLevel());
  180. vis_packet->setDataByName("arrow_color", arrow_color);
  181. vis_packet->setDataByName("locked_no_loot", appearance.locked_no_loot);
  182. if (player->GetArrowColor(GetLevel()) == ARROW_COLOR_GRAY)
  183. if (npc_con == -4)
  184. npc_con = -3;
  185. vis_packet->setDataByName("npc_con", npc_con);
  186. if (appearance.attackable == 1 && IsNPC() && (player->GetFactions()->GetCon(faction_id) <= -4 || ((NPC*)this)->Brain()->GetHate(player) > 1))
  187. vis_packet->setDataByName("npc_hate", ((NPC*)this)->Brain()->GetHatePercentage(player));
  188. int8 quest_flag = player->CheckQuestFlag(this);
  189. if (version < 1188 && quest_flag >= 16)
  190. quest_flag = 1;
  191. vis_packet->setDataByName("quest_flag",quest_flag);
  192. if( player->CheckQuestsKillUpdate(this, false)){
  193. vis_packet->setDataByName("name_quest_icon", 1);
  194. }
  195. }
  196. }
  197. int8 vis_flags = 0;
  198. if (MeetsSpawnAccessRequirements(player)){
  199. if (appearance.attackable == 1)
  200. vis_flags += 64; //attackable icon
  201. if (appearance.show_level == 1)
  202. vis_flags += 32;
  203. if (appearance.display_name == 1)
  204. vis_flags += 16;
  205. if (IsPlayer() || appearance.targetable == 1)
  206. vis_flags += 4;
  207. if (appearance.show_command_icon == 1)
  208. vis_flags += 2;
  209. }
  210. else{
  211. //Check to see if there's an override value set
  212. if (req_quests_override > 0)
  213. vis_flags = req_quests_override & 0xFF;
  214. }
  215. vis_packet->setDataByName("vis_flags", vis_flags);
  216. if (MeetsSpawnAccessRequirements(player))
  217. vis_packet->setDataByName("hand_flag", appearance.display_hand_icon);
  218. else {
  219. if ((req_quests_override & 256) > 0)
  220. vis_packet->setDataByName("hand_flag", 1);
  221. }
  222. }
  223. void Spawn::InitializeFooterPacketData(Player* player, PacketStruct* footer) {
  224. if (IsWidget()){
  225. Widget* widget = (Widget*)this;
  226. if (widget->GetMultiFloorLift()) {
  227. footer->setDataByName("widget_x", widget->GetX());
  228. footer->setDataByName("widget_y", widget->GetY());
  229. footer->setDataByName("widget_z", widget->GetZ());
  230. }
  231. else {
  232. footer->setDataByName("widget_x", widget->GetWidgetX());
  233. footer->setDataByName("widget_y", widget->GetWidgetY());
  234. footer->setDataByName("widget_z", widget->GetWidgetZ());
  235. }
  236. footer->setDataByName("widget_id", widget->GetWidgetID());
  237. footer->setDataByName("unknown3c", 6);
  238. }
  239. else if (IsSign()){
  240. Sign* sign = (Sign*)this;
  241. footer->setDataByName("widget_id", sign->GetWidgetID());
  242. footer->setDataByName("widget_x", sign->GetWidgetX());
  243. footer->setDataByName("widget_y", sign->GetWidgetY());
  244. footer->setDataByName("widget_z", sign->GetWidgetZ());
  245. footer->setDataByName("unknown2b", 6);
  246. if (sign->GetSignTitle())
  247. footer->setMediumStringByName("title", sign->GetSignTitle());
  248. if (sign->GetSignDescription())
  249. footer->setMediumStringByName("description", sign->GetSignDescription());
  250. footer->setDataByName("sign_distance", sign->GetSignDistance());
  251. footer->setDataByName("show", 1);
  252. }
  253. footer->setMediumStringByName("name", appearance.name);
  254. footer->setMediumStringByName("guild", appearance.sub_title);
  255. footer->setMediumStringByName("prefix", appearance.prefix_title);
  256. footer->setMediumStringByName("suffix", appearance.suffix_title);
  257. footer->setMediumStringByName("last_name", appearance.last_name);
  258. if (appearance.attackable == 0 && GetLevel() > 0)
  259. footer->setDataByName("spawn_type", 1);
  260. else if (appearance.attackable == 0)
  261. footer->setDataByName("spawn_type", 6);
  262. else
  263. footer->setDataByName("spawn_type", 3);
  264. }
  265. EQ2Packet* Spawn::spawn_serialize(Player* player, int16 version){
  266. // If spawn is NPC AND is pet && owner is a player && owner is the player passed to this function && player's char sheet pet id is 0
  267. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && player == ((NPC*)this)->GetOwner() && player->GetInfoStruct()->pet_id == 0) {
  268. ((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->pet_id = player->spawn_id;
  269. player->SetCharSheetChanged(true);
  270. }
  271. int16 index;
  272. if (player->player_spawn_index_map.count(this) > 0) {
  273. index = player->player_spawn_index_map[this];
  274. player->player_spawn_map[index] = this;
  275. }
  276. else {
  277. player->spawn_index++;
  278. if(player->spawn_index == 255)
  279. player->spawn_index++; //just so we dont have to worry about overloading
  280. index = player->spawn_index;
  281. player->player_spawn_index_map[this] = index;
  282. player->player_spawn_map[index] = this;
  283. }
  284. // Jabantiz - [Bug] Client Crash on Revive
  285. if (player->player_spawn_reverse_id_map.count(this) == 0) {
  286. int32 spawn_id = ++player->spawn_id;
  287. player->player_spawn_id_map[spawn_id] = this;
  288. player->player_spawn_reverse_id_map[this] = spawn_id;
  289. }
  290. PacketStruct* header = player->GetSpawnHeaderStruct();
  291. header->ResetData();
  292. InitializeHeaderPacketData(player, header, index);
  293. PacketStruct* footer;
  294. if(IsWidget())
  295. footer = player->GetWidgetFooterStruct();
  296. else if(IsSign())
  297. footer = player->GetSignFooterStruct();
  298. else
  299. footer = player->GetSpawnFooterStruct();
  300. footer->ResetData();
  301. InitializeFooterPacketData(player, footer);
  302. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  303. PacketStruct* info_struct = player->GetSpawnInfoStruct();
  304. PacketStruct* pos_struct = player->GetSpawnPosStruct();
  305. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  306. vis_struct->ResetData();
  307. InitializeVisPacketData(player, vis_struct);
  308. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  309. info_struct->ResetData();
  310. InitializeInfoPacketData(player, info_struct);
  311. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  312. pos_struct->ResetData();
  313. InitializePosPacketData(player, pos_struct);
  314. string* vis_data= vis_struct->serializeString();
  315. string* pos_data = pos_struct->serializeString();
  316. string* info_data = info_struct->serializeString();
  317. int16 part2_size = pos_data->length() + vis_data->length() + info_data->length();
  318. uchar* part2 = new uchar[part2_size];
  319. player->AddSpawnPosPacketForXOR(id, (uchar*)pos_data->c_str(), pos_data->length());
  320. player->AddSpawnVisPacketForXOR(id, (uchar*)vis_data->c_str(), vis_data->length());
  321. player->AddSpawnInfoPacketForXOR(id, (uchar*)info_data->c_str(), info_data->length());
  322. uchar* ptr = part2;
  323. memcpy(ptr, pos_data->c_str(), pos_data->length());
  324. ptr += pos_data->length();
  325. memcpy(ptr, vis_data->c_str(), vis_data->length());
  326. ptr += vis_data->length();
  327. memcpy(ptr, info_data->c_str(), info_data->length());
  328. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  329. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  330. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  331. string* part1 = header->serializeString();
  332. string* part3 = footer->serializeString();
  333. uchar tmp[900];
  334. part2_size = Pack(tmp, part2, part2_size, 900, version);
  335. int32 total_size = part1->length() + part2_size + part3->length() + 3;
  336. uchar* final_packet = new uchar[total_size + 4];
  337. ptr = final_packet;
  338. memcpy(ptr, &total_size, sizeof(total_size));
  339. ptr += sizeof(total_size);
  340. memcpy(ptr, &oversized_packet, sizeof(oversized_packet));
  341. ptr += sizeof(oversized_packet);
  342. memcpy(ptr, &opcode, sizeof(opcode));
  343. ptr += sizeof(opcode);
  344. memcpy(ptr, part1->c_str(), part1->length());
  345. ptr += part1->length();
  346. memcpy(ptr, tmp, part2_size);
  347. ptr += part2_size;
  348. memcpy(ptr, part3->c_str(), part3->length());
  349. delete[] part2;
  350. EQ2Packet* ret = new EQ2Packet(OP_ClientCmdMsg, final_packet, total_size + 4);
  351. delete[] final_packet;
  352. return ret;
  353. }
  354. uchar* Spawn::spawn_info_changes(Player* player, int16 version){
  355. int16 index = player->player_spawn_index_map[this];
  356. PacketStruct* packet = player->GetSpawnInfoStruct();
  357. player->info_mutex.writelock(__FUNCTION__, __LINE__);
  358. packet->ResetData();
  359. InitializeInfoPacketData(player, packet);
  360. string* data = packet->serializeString();
  361. int32 size = data->length();
  362. uchar* xor_info_packet = player->GetTempInfoPacketForXOR();
  363. if (!xor_info_packet)
  364. xor_info_packet = player->SetTempInfoPacketForXOR(size);
  365. uchar* orig_packet = player->GetSpawnInfoPacketForXOR(id);
  366. if(orig_packet){
  367. memcpy(xor_info_packet, (uchar*)data->c_str(), size);
  368. Encode(xor_info_packet, orig_packet, size);
  369. }
  370. uchar* tmp = new uchar[size + 10];
  371. size = Pack(tmp, xor_info_packet, size, size, version);
  372. player->info_mutex.releasewritelock(__FUNCTION__, __LINE__);
  373. int32 orig_size = size;
  374. size-=sizeof(int32);
  375. size+=CheckOverLoadSize(index);
  376. info_packet_size = size + CheckOverLoadSize(size);
  377. uchar* tmp2 = new uchar[info_packet_size];
  378. uchar* ptr = tmp2;
  379. ptr += DoOverLoad(size, ptr);
  380. ptr += DoOverLoad(index, ptr);
  381. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  382. delete[] tmp;
  383. return tmp2;
  384. }
  385. uchar* Spawn::spawn_vis_changes(Player* player, int16 version){
  386. PacketStruct* vis_struct = player->GetSpawnVisStruct();
  387. int16 index = player->player_spawn_index_map[this];
  388. player->vis_mutex.writelock(__FUNCTION__, __LINE__);
  389. uchar* orig_packet = player->GetSpawnVisPacketForXOR(id);
  390. vis_struct->ResetData();
  391. InitializeVisPacketData(player, vis_struct);
  392. string* data = vis_struct->serializeString();
  393. int32 size = data->length();
  394. uchar* xor_vis_packet = player->GetTempVisPacketForXOR();
  395. if (!xor_vis_packet)
  396. xor_vis_packet = player->SetTempVisPacketForXOR(size);
  397. if(orig_packet){
  398. memcpy(xor_vis_packet, (uchar*)data->c_str(), size);
  399. Encode(xor_vis_packet, orig_packet, size);
  400. }
  401. uchar* tmp = new uchar[size + 10];
  402. size = Pack(tmp, xor_vis_packet, size, size, version);
  403. player->vis_mutex.releasewritelock(__FUNCTION__, __LINE__);
  404. int32 orig_size = size;
  405. size-=sizeof(int32);
  406. size+=CheckOverLoadSize(index);
  407. vis_packet_size = size + CheckOverLoadSize(size);
  408. uchar* tmp2 = new uchar[vis_packet_size];
  409. uchar* ptr = tmp2;
  410. ptr += DoOverLoad(size, ptr);
  411. ptr += DoOverLoad(index, ptr);
  412. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  413. delete[] tmp;
  414. return tmp2;
  415. }
  416. uchar* Spawn::spawn_pos_changes(Player* player, int16 version){
  417. int16 index = player->GetIndexForSpawn(this);
  418. PacketStruct* packet = player->GetSpawnPosStruct();
  419. player->pos_mutex.writelock(__FUNCTION__, __LINE__);
  420. uchar* orig_packet = player->GetSpawnPosPacketForXOR(id);
  421. packet->ResetData();
  422. InitializePosPacketData(player, packet, true);
  423. string* data = packet->serializeString();
  424. int32 size = data->length();
  425. uchar* xor_pos_packet = player->GetTempPosPacketForXOR();
  426. if (!xor_pos_packet)
  427. xor_pos_packet = player->SetTempPosPacketForXOR(size);
  428. if(orig_packet){
  429. memcpy(xor_pos_packet, (uchar*)data->c_str(), size);
  430. Encode(xor_pos_packet, orig_packet, size);
  431. }
  432. uchar* tmp;
  433. if (IsPlayer())
  434. tmp = new uchar[size + 14];
  435. else
  436. tmp = new uchar[size + 10];
  437. size = Pack(tmp, xor_pos_packet, size, size, version);
  438. player->pos_mutex.releasewritelock(__FUNCTION__, __LINE__);
  439. int32 orig_size = size;
  440. // Needed for CoE+ clients
  441. if (version >= 1188)
  442. size += 1;
  443. if(IsPlayer())
  444. size += 4;
  445. size-=sizeof(int32);
  446. size+=CheckOverLoadSize(index);
  447. pos_packet_size = size + CheckOverLoadSize(size);
  448. uchar* tmp2 = new uchar[pos_packet_size];
  449. uchar* ptr = tmp2;
  450. ptr += DoOverLoad(size, ptr);
  451. ptr += DoOverLoad(index, ptr);
  452. // extra byte in coe+ clients, 0 for NPC's 1 for Players
  453. int8 x = 0;
  454. if(IsPlayer()){
  455. if (version >= 1188) {
  456. // set x to 1 and add it to the packet
  457. x = 1;
  458. memcpy(ptr, &x, sizeof(int8));
  459. ptr += sizeof(int8);
  460. }
  461. int32 now = Timer::GetCurrentTime2();
  462. memcpy(ptr, &now, sizeof(int32));
  463. ptr += sizeof(int32);
  464. }
  465. else if (version >= 1188) {
  466. // add x to packet
  467. memcpy(ptr, &x, sizeof(int8));
  468. ptr += sizeof(int8);
  469. }
  470. memcpy(ptr, tmp+sizeof(int32), orig_size - sizeof(int32));
  471. delete[] tmp;
  472. return tmp2;
  473. }
  474. EQ2Packet* Spawn::player_position_update_packet(Player* player, int16 version){
  475. if(!player || player->IsPlayer() == false){
  476. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet without player!");
  477. return 0;
  478. }
  479. else if(IsPlayer() == false){
  480. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called player_position_update_packet from spawn!");
  481. return 0;
  482. }
  483. static const int8 info_size = 1;
  484. static const int8 vis_size = 1;
  485. m_Update.writelock(__FUNCTION__, __LINE__);
  486. uchar* pos_changes = spawn_pos_changes(player, version);
  487. int32 tmp_pos_packet_size = pos_packet_size;
  488. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  489. int32 size = info_size + tmp_pos_packet_size + vis_size + 11;
  490. static const int8 oversized = 255;
  491. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  492. uchar* tmp = new uchar[size];
  493. memset(tmp, 0, size);
  494. uchar* ptr = tmp;
  495. size -=4;
  496. memcpy(ptr, &size, sizeof(int32));
  497. size +=4;
  498. ptr += sizeof(int32);
  499. memcpy(ptr, &oversized, sizeof(int8));
  500. ptr += sizeof(int8);
  501. memcpy(ptr, &opcode_val, sizeof(int16));
  502. ptr += sizeof(int16);
  503. ptr += sizeof(int32);
  504. ptr += info_size;
  505. memcpy(ptr, pos_changes, tmp_pos_packet_size);
  506. EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  507. delete[] tmp;
  508. delete[] pos_changes;
  509. return ret_packet;
  510. }
  511. EQ2Packet* Spawn::spawn_update_packet(Player* player, int16 version, bool override_changes, bool override_vis_changes){
  512. if(!player || player->IsPlayer() == false){
  513. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error: Called spawn_update_packet without player!");
  514. return 0;
  515. }
  516. else if((IsPlayer() && info_changed == false && vis_changed == false) || (info_changed == false && vis_changed == false && position_changed == false)){
  517. if(!override_changes && !override_vis_changes)
  518. return 0;
  519. }
  520. static const uchar null_byte = 0;
  521. uchar* info_changes = 0;
  522. uchar* pos_changes = 0;
  523. uchar* vis_changes = 0;
  524. static const int8 oversized = 255;
  525. int16 opcode_val = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(OP_EqUpdateGhostCmd);
  526. int16 tmp_info_packet_size;
  527. int16 tmp_vis_packet_size;
  528. int16 tmp_pos_packet_size;
  529. //We need to lock these variables up to make this thread safe
  530. m_Update.writelock(__FUNCTION__, __LINE__);
  531. //These variables are set in the spawn_info_changes, pos and vis changes functions
  532. info_packet_size = 1;
  533. pos_packet_size = 1;
  534. vis_packet_size = 1;
  535. if (info_changed || override_changes)
  536. info_changes = spawn_info_changes(player, version);
  537. if ((position_changed || override_changes) && IsPlayer() == false)
  538. pos_changes = spawn_pos_changes(player, version);
  539. if (vis_changed || override_changes || override_vis_changes)
  540. vis_changes = spawn_vis_changes(player, version);
  541. tmp_info_packet_size = info_packet_size;
  542. tmp_pos_packet_size = pos_packet_size;
  543. tmp_vis_packet_size = vis_packet_size;
  544. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  545. int32 size = info_packet_size + pos_packet_size + vis_packet_size + 11;
  546. uchar* tmp = new uchar[size];
  547. memset(tmp, 0, size);
  548. uchar* ptr = tmp;
  549. size -=4;
  550. memcpy(ptr, &size, sizeof(int32));
  551. size +=4;
  552. ptr += sizeof(int32);
  553. memcpy(ptr, &oversized, sizeof(int8));
  554. ptr += sizeof(int8);
  555. memcpy(ptr, &opcode_val, sizeof(int16));
  556. ptr += sizeof(int16);
  557. if (IsPlayer() == false){ //this isnt sent for player updates, it is sent on position update
  558. //int32 time = Timer::GetCurrentTime2();
  559. packet_num = Timer::GetCurrentTime2();
  560. memcpy(ptr, &packet_num, sizeof(int32));
  561. }
  562. ptr += sizeof(int32);
  563. memcpy(ptr, info_changes ? info_changes : &null_byte, tmp_info_packet_size);
  564. ptr += info_packet_size;
  565. memcpy(ptr, pos_changes ? pos_changes : &null_byte, tmp_pos_packet_size);
  566. ptr += pos_packet_size;
  567. memcpy(ptr, vis_changes ? vis_changes : &null_byte, tmp_vis_packet_size);
  568. EQ2Packet* ret_packet = new EQ2Packet(OP_ClientCmdMsg, tmp, size);
  569. delete[] tmp;
  570. safe_delete_array(info_changes);
  571. safe_delete_array(vis_changes);
  572. safe_delete_array(pos_changes);
  573. return ret_packet;
  574. }
  575. EQ2Packet* Spawn::serialize(Player* player, int16 version){
  576. return 0;
  577. }
  578. Spawn* Spawn::GetTarget(){
  579. Spawn* ret = 0;
  580. // only attempt to get a spawn if we had a target stored
  581. if (target != 0)
  582. {
  583. ret = GetZone()->GetSpawnByID(target);
  584. if (!ret)
  585. target = 0;
  586. }
  587. return ret;
  588. }
  589. void Spawn::SetTarget(Spawn* spawn){
  590. SetInfo(&target, spawn ? spawn->GetID() : 0);
  591. }
  592. Spawn* Spawn::GetLastAttacker() {
  593. Spawn* ret = 0;
  594. ret = GetZone()->GetSpawnByID(last_attacker);
  595. if (!ret)
  596. last_attacker = 0;
  597. return ret;
  598. }
  599. void Spawn::SetLastAttacker(Spawn* spawn){
  600. last_attacker = spawn->GetID();
  601. }
  602. void Spawn::SetInvulnerable(bool val){
  603. invulnerable = val;
  604. }
  605. bool Spawn::GetInvulnerable(){
  606. return invulnerable;
  607. }
  608. bool Spawn::TakeDamage(int32 damage){
  609. if(invulnerable)
  610. return false;
  611. if (IsEntity()) {
  612. if (((Entity*)this)->IsMezzed())
  613. ((Entity*)this)->RemoveAllMezSpells();
  614. if (damage == 0)
  615. return true;
  616. }
  617. int32 hp = GetHP();
  618. if(damage >= hp) {
  619. SetHP(0);
  620. if (IsPlayer()) {
  621. ((Player*)this)->InCombat(false);
  622. ((Player*)this)->SetRangeAttack(false);
  623. GetZone()->TriggerCharSheetTimer(); // force char sheet updates now
  624. }
  625. }
  626. else {
  627. SetHP(hp - damage);
  628. // if player flag the char sheet as changed so the ui updates properly
  629. if (IsPlayer())
  630. ((Player*)this)->SetCharSheetChanged(true);
  631. }
  632. return true;
  633. }
  634. void Spawn::TakeDamage(Spawn* attacker, int32 damage){
  635. if (TakeDamage(damage))
  636. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_HEALTHCHANGED, attacker);
  637. SetLastAttacker(attacker);
  638. }
  639. ZoneServer* Spawn::GetZone(){
  640. return zone;
  641. }
  642. void Spawn::SetZone(ZoneServer* in_zone){
  643. zone = in_zone;
  644. }
  645. /*** HIT POINT ***/
  646. void Spawn::SetHP(sint32 new_val, bool setUpdateFlags){
  647. if(new_val == 0){
  648. ClearRunningLocations();
  649. CalculateRunningLocation(true);
  650. }
  651. if(new_val > basic_info.max_hp)
  652. SetInfo(&basic_info.max_hp, new_val, setUpdateFlags);
  653. SetInfo(&basic_info.cur_hp, new_val, setUpdateFlags);
  654. if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  655. GetZone()->AddDamagedSpawn(this);
  656. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  657. ((Entity*)this)->UpdateGroupMemberInfo();
  658. if (IsPlayer())
  659. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  660. else
  661. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  662. }
  663. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  664. Player* player = (Player*)((NPC*)this)->GetOwner();
  665. if (player->GetPet() && player->GetCharmedPet()) {
  666. if (this == player->GetPet()) {
  667. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  668. player->SetCharSheetChanged(true);
  669. }
  670. }
  671. else {
  672. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  673. player->SetCharSheetChanged(true);
  674. }
  675. }
  676. }
  677. void Spawn::SetTotalHP(sint32 new_val){
  678. if(basic_info.hp_base == 0)
  679. SetTotalHPBase(new_val);
  680. SetInfo(&basic_info.max_hp, new_val);
  681. if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  682. GetZone()->AddDamagedSpawn(this);
  683. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  684. ((Entity*)this)->UpdateGroupMemberInfo();
  685. if (IsPlayer())
  686. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  687. else
  688. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  689. }
  690. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  691. Player* player = (Player*)((NPC*)this)->GetOwner();
  692. if (player->GetPet() && player->GetCharmedPet()) {
  693. if (this == player->GetPet()) {
  694. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  695. player->SetCharSheetChanged(true);
  696. }
  697. }
  698. else {
  699. player->GetInfoStruct()->pet_health_pct = (float)basic_info.cur_hp / (float)basic_info.max_hp;
  700. player->SetCharSheetChanged(true);
  701. }
  702. }
  703. }
  704. void Spawn::SetTotalHPBase(sint32 new_val)
  705. {
  706. SetInfo(&basic_info.hp_base, new_val);
  707. if(GetZone() && basic_info.cur_hp > 0 && basic_info.cur_hp < basic_info.max_hp)
  708. GetZone()->AddDamagedSpawn(this);
  709. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  710. ((Entity*)this)->UpdateGroupMemberInfo();
  711. if (IsPlayer())
  712. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  713. else
  714. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  715. }
  716. }
  717. sint32 Spawn::GetHP()
  718. {
  719. return basic_info.cur_hp;
  720. }
  721. sint32 Spawn::GetTotalHP()
  722. {
  723. return basic_info.max_hp;
  724. }
  725. sint32 Spawn::GetTotalHPBase()
  726. {
  727. return basic_info.hp_base;
  728. }
  729. /*** POWER ***/
  730. void Spawn::SetPower(sint32 power, bool setUpdateFlags){
  731. if(power > basic_info.max_power)
  732. SetInfo(&basic_info.max_power, power, setUpdateFlags);
  733. SetInfo(&basic_info.cur_power, power, setUpdateFlags);
  734. if(/*IsPlayer() &&*/ GetZone() && basic_info.cur_power < basic_info.max_power)
  735. GetZone()->AddDamagedSpawn(this);
  736. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  737. ((Entity*)this)->UpdateGroupMemberInfo();
  738. if (IsPlayer())
  739. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  740. else
  741. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  742. }
  743. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  744. Player* player = (Player*)((NPC*)this)->GetOwner();
  745. if (player->GetPet() && player->GetCharmedPet()) {
  746. if (this == player->GetPet()) {
  747. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  748. player->SetCharSheetChanged(true);
  749. }
  750. }
  751. else {
  752. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  753. player->SetCharSheetChanged(true);
  754. }
  755. }
  756. }
  757. void Spawn::SetTotalPower(sint32 new_val)
  758. {
  759. if(basic_info.power_base == 0)
  760. SetTotalPowerBase(new_val);
  761. SetInfo(&basic_info.max_power, new_val);
  762. if(GetZone() && basic_info.cur_power < basic_info.max_power)
  763. GetZone()->AddDamagedSpawn(this);
  764. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  765. ((Entity*)this)->UpdateGroupMemberInfo();
  766. if (IsPlayer())
  767. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  768. else
  769. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  770. }
  771. if (IsNPC() && ((NPC*)this)->IsPet() && ((NPC*)this)->GetOwner()->IsPlayer()) {
  772. Player* player = (Player*)((NPC*)this)->GetOwner();
  773. if (player->GetPet() && player->GetCharmedPet()) {
  774. if (this == player->GetPet()) {
  775. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  776. player->SetCharSheetChanged(true);
  777. }
  778. }
  779. else {
  780. player->GetInfoStruct()->pet_power_pct = (float)basic_info.cur_power / (float)basic_info.max_power;
  781. player->SetCharSheetChanged(true);
  782. }
  783. }
  784. }
  785. void Spawn::SetTotalPowerBase(sint32 new_val)
  786. {
  787. SetInfo(&basic_info.power_base, new_val);
  788. if(GetZone() && basic_info.cur_power < basic_info.max_power)
  789. GetZone()->AddDamagedSpawn(this);
  790. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  791. ((Entity*)this)->UpdateGroupMemberInfo();
  792. if (IsPlayer())
  793. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  794. else
  795. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  796. }
  797. }
  798. sint32 Spawn::GetPower()
  799. {
  800. return basic_info.cur_power;
  801. }
  802. sint32 Spawn::GetTotalPower(){
  803. return basic_info.max_power;
  804. }
  805. sint32 Spawn::GetTotalPowerBase()
  806. {
  807. return basic_info.power_base;
  808. }
  809. /*** SAVAGERY ***/
  810. void Spawn::SetSavagery(sint32 savagery, bool setUpdateFlags)
  811. {
  812. /* JA: extremely limited functionality until we better understand Savagery */
  813. if(savagery > basic_info.max_savagery)
  814. SetInfo(&basic_info.max_savagery, savagery, setUpdateFlags);
  815. SetInfo(&basic_info.cur_savagery, savagery, setUpdateFlags);
  816. }
  817. void Spawn::SetTotalSavagery(sint32 new_val)
  818. {
  819. /* JA: extremely limited functionality until we better understand Savagery */
  820. if(basic_info.savagery_base == 0)
  821. SetTotalSavageryBase(new_val);
  822. SetInfo(&basic_info.max_savagery, new_val);
  823. }
  824. void Spawn::SetTotalSavageryBase(sint32 new_val){
  825. SetInfo(&basic_info.savagery_base, new_val);
  826. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  827. ((Entity*)this)->UpdateGroupMemberInfo();
  828. if (IsPlayer())
  829. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  830. else
  831. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  832. }
  833. }
  834. sint32 Spawn::GetTotalSavagery()
  835. {
  836. return basic_info.max_savagery;
  837. }
  838. sint32 Spawn::GetSavagery()
  839. {
  840. return basic_info.cur_savagery;
  841. }
  842. /*** DISSONANCE ***/
  843. void Spawn::SetDissonance(sint32 dissonance, bool setUpdateFlags)
  844. {
  845. /* JA: extremely limited functionality until we better understand Dissonance */
  846. if(dissonance > basic_info.max_dissonance)
  847. SetInfo(&basic_info.max_dissonance, dissonance, setUpdateFlags);
  848. SetInfo(&basic_info.cur_dissonance, dissonance, setUpdateFlags);
  849. }
  850. void Spawn::SetTotalDissonance(sint32 new_val)
  851. {
  852. /* JA: extremely limited functionality until we better understand Dissonance */
  853. if(basic_info.dissonance_base == 0)
  854. SetTotalDissonanceBase(new_val);
  855. SetInfo(&basic_info.max_dissonance, new_val);
  856. }
  857. void Spawn::SetTotalDissonanceBase(sint32 new_val)
  858. {
  859. SetInfo(&basic_info.dissonance_base, new_val);
  860. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  861. ((Entity*)this)->UpdateGroupMemberInfo();
  862. if (IsPlayer())
  863. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, GetZone()->GetClientBySpawn(this));
  864. else
  865. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  866. }
  867. }
  868. sint32 Spawn::GetTotalDissonance()
  869. {
  870. return basic_info.max_dissonance;
  871. }
  872. sint32 Spawn::GetDissonance()
  873. {
  874. return basic_info.cur_dissonance;
  875. }
  876. /* --< Alternate Advancement Points >-- */
  877. void Spawn::SetAssignedAA(sint16 new_val)
  878. {
  879. SetInfo(&basic_info.assigned_aa, new_val);
  880. }
  881. void Spawn::SetUnassignedAA(sint16 new_val)
  882. {
  883. SetInfo(&basic_info.unassigned_aa, new_val);
  884. }
  885. void Spawn::SetTradeskillAA(sint16 new_val)
  886. {
  887. SetInfo(&basic_info.tradeskill_aa, new_val);
  888. }
  889. void Spawn::SetUnassignedTradeskillAA(sint16 new_val)
  890. {
  891. SetInfo(&basic_info.unassigned_tradeskill_aa, new_val);
  892. }
  893. void Spawn::SetPrestigeAA(sint16 new_val)
  894. {
  895. SetInfo(&basic_info.prestige_aa, new_val);
  896. }
  897. void Spawn::SetUnassignedPrestigeAA(sint16 new_val)
  898. {
  899. SetInfo(&basic_info.unassigned_prestige_aa, new_val);
  900. }
  901. void Spawn::SetTradeskillPrestigeAA(sint16 new_val)
  902. {
  903. SetInfo(&basic_info.tradeskill_prestige_aa, new_val);
  904. }
  905. void Spawn::SetUnassignedTradeskillPrestigeAA(sint16 new_val)
  906. {
  907. SetInfo(&basic_info.unassigned_tradeskill_prestige_aa, new_val);
  908. }
  909. sint16 Spawn::GetAssignedAA()
  910. {
  911. return basic_info.assigned_aa;
  912. }
  913. sint16 Spawn::GetUnassignedAA()
  914. {
  915. return basic_info.unassigned_aa;
  916. }
  917. sint16 Spawn::GetTradeskillAA()
  918. {
  919. return basic_info.tradeskill_aa;
  920. }
  921. sint16 Spawn::GetUnassignedTradeskillAA()
  922. {
  923. return basic_info.unassigned_tradeskill_aa;
  924. }
  925. sint16 Spawn::GetPrestigeAA()
  926. {
  927. return basic_info.prestige_aa;
  928. }
  929. sint16 Spawn::GetUnassignedPretigeAA()
  930. {
  931. return basic_info.unassigned_prestige_aa;
  932. }
  933. sint16 Spawn::GetTradeskillPrestigeAA()
  934. {
  935. return basic_info.tradeskill_prestige_aa;
  936. }
  937. sint16 Spawn::GetUnassignedTradeskillPrestigeAA()
  938. {
  939. return basic_info.unassigned_tradeskill_prestige_aa;
  940. }
  941. float Spawn::GetDistance(float x1, float y1, float z1, float x2, float y2, float z2){
  942. x1 = x1 - x2;
  943. y1 = y1 - y2;
  944. z1 = z1 - z2;
  945. return sqrt(x1*x1 + y1*y1 + z1*z1);
  946. }
  947. float Spawn::GetDistance(float x, float y, float z, float radius, bool ignore_y) {
  948. if (ignore_y)
  949. return GetDistance(x, y, z, GetX(), y, GetZ()) - radius;
  950. else
  951. return GetDistance(x, y, z, GetX(), GetY(), GetZ()) - radius;
  952. }
  953. float Spawn::GetDistance(float x, float y, float z, bool ignore_y) {
  954. return GetDistance(x, y, z, 0.0f, ignore_y);
  955. }
  956. float Spawn::GetDistance(Spawn* spawn, bool ignore_y, bool includeRadius){
  957. float ret = 0;
  958. if (spawn)
  959. {
  960. float radius = 0.0f;
  961. if (includeRadius)
  962. radius = CalculateRadius(spawn);
  963. ret = GetDistance(spawn->GetX(), spawn->GetY(), spawn->GetZ(), radius, ignore_y);
  964. }
  965. // maybe distance against ourselves, in that case we want to nullify the radius check
  966. if (ret < 0)
  967. ret = 0.0f;
  968. return ret;
  969. }
  970. float Spawn::GetDistance(Spawn* spawn, float x1, float y1, float z1, bool includeRadius) {
  971. float ret = 0;
  972. if (spawn)
  973. {
  974. float radius = 0.0f;
  975. if (includeRadius)
  976. radius = CalculateRadius(spawn);
  977. ret = GetDistance(x1, y1, z1, spawn->GetX(), spawn->GetY(), spawn->GetZ()) - radius;
  978. }
  979. // maybe distance against ourselves, in that case we want to nullify the radius check
  980. if (ret < 0)
  981. ret = 0.0f;
  982. return ret;
  983. }
  984. float Spawn::CalculateRadius(Spawn* target)
  985. {
  986. float srcRadius = short_to_float(appearance.pos.collision_radius);
  987. if (target)
  988. {
  989. float targRadius = short_to_float(target->appearance.pos.collision_radius);
  990. return (targRadius / 32.0f) + (srcRadius / 32.0f);
  991. }
  992. else
  993. return (srcRadius / 32.0f);
  994. }
  995. int32 Spawn::GetRespawnTime(){
  996. return respawn;
  997. }
  998. void Spawn::SetRespawnTime(int32 time){
  999. respawn = time;
  1000. }
  1001. int32 Spawn::GetExpireOffsetTime(){
  1002. return expire_offset;
  1003. }
  1004. void Spawn::SetExpireOffsetTime(int32 time){
  1005. expire_offset = time;
  1006. }
  1007. int32 Spawn::GetSpawnLocationID(){
  1008. return spawn_location_id;
  1009. }
  1010. void Spawn::SetSpawnLocationID(int32 id){
  1011. spawn_location_id = id;
  1012. }
  1013. int32 Spawn::GetSpawnEntryID(){
  1014. return spawn_entry_id;
  1015. }
  1016. void Spawn::SetSpawnEntryID(int32 id){
  1017. spawn_entry_id = id;
  1018. }
  1019. int32 Spawn::GetSpawnLocationPlacementID(){
  1020. return spawn_location_spawns_id;
  1021. }
  1022. void Spawn::SetSpawnLocationPlacementID(int32 id){
  1023. spawn_location_spawns_id = id;
  1024. }
  1025. const char* Spawn::GetSpawnScript(){
  1026. if(spawn_script.length() > 0)
  1027. return spawn_script.c_str();
  1028. else
  1029. return 0;
  1030. }
  1031. void Spawn::SetSpawnScript(string name){
  1032. spawn_script = name;
  1033. }
  1034. void Spawn::SetPrimaryCommand(const char* name, const char* command, float distance){
  1035. EntityCommand* entity_command = CreateEntityCommand(name, distance, command, "", 0, 0);
  1036. if(primary_command_list.size() > 0 && primary_command_list[0]){
  1037. safe_delete(primary_command_list[0]);
  1038. primary_command_list[0] = entity_command;
  1039. }
  1040. else
  1041. primary_command_list.push_back(entity_command);
  1042. }
  1043. void Spawn::SetSecondaryCommands(vector<EntityCommand*>* commands){
  1044. if(commands && commands->size() > 0){
  1045. vector<EntityCommand*>::iterator itr;
  1046. if(secondary_command_list.size() > 0){
  1047. for(itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++){
  1048. safe_delete(*itr);
  1049. }
  1050. secondary_command_list.clear();
  1051. }
  1052. EntityCommand* command = 0;
  1053. for(itr = commands->begin(); itr != commands->end(); itr++){
  1054. command = CreateEntityCommand(*itr);
  1055. secondary_command_list.push_back(command);
  1056. }
  1057. }
  1058. }
  1059. void Spawn::SetPrimaryCommands(vector<EntityCommand*>* commands){
  1060. if(commands && commands->size() > 0){
  1061. vector<EntityCommand*>::iterator itr;
  1062. if(primary_command_list.size() > 0){
  1063. for(itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++){
  1064. safe_delete(*itr);
  1065. }
  1066. primary_command_list.clear();
  1067. }
  1068. EntityCommand* command = 0;
  1069. for(itr = commands->begin(); itr != commands->end(); itr++){
  1070. command = CreateEntityCommand(*itr);
  1071. primary_command_list.push_back(command);
  1072. }
  1073. }
  1074. }
  1075. EntityCommand* Spawn::FindEntityCommand(string command) {
  1076. EntityCommand* entity_command = 0;
  1077. if (primary_command_list.size() > 0) {
  1078. vector<EntityCommand*>::iterator itr;
  1079. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  1080. if ((*itr)->command == command) {
  1081. entity_command = *itr;
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. if (!entity_command && secondary_command_list.size() > 0) {
  1087. vector<EntityCommand*>::iterator itr;
  1088. for (itr = secondary_command_list.begin(); itr != secondary_command_list.end(); itr++) {
  1089. if ((*itr)->command == command) {
  1090. entity_command = *itr;
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. return entity_command;
  1096. }
  1097. void Spawn::SetSizeOffset(int8 offset){
  1098. size_offset = offset;
  1099. }
  1100. int8 Spawn::GetSizeOffset(){
  1101. return size_offset;
  1102. }
  1103. void Spawn::SetMerchantID(int32 val){
  1104. merchant_id = val;
  1105. }
  1106. int32 Spawn::GetMerchantID(){
  1107. return merchant_id;
  1108. }
  1109. void Spawn::SetMerchantType(int8 val){
  1110. merchant_type = val;
  1111. }
  1112. int8 Spawn::GetMerchantType(){
  1113. return merchant_type;
  1114. }
  1115. void Spawn::SetQuestsRequired(map<int32, vector<int16>* >* quests){
  1116. if(quests){
  1117. map<int32, vector<int16>* >::iterator itr;
  1118. for(itr = quests->begin(); itr != quests->end(); itr++){
  1119. vector<int16>* quest_steps = itr->second;
  1120. for (int32 i = 0; i < quest_steps->size(); i++)
  1121. SetQuestsRequired(itr->first, quest_steps->at(i));
  1122. }
  1123. }
  1124. }
  1125. void Spawn::SetQuestsRequired(int32 quest_id, int16 quest_step){
  1126. m_requiredQuests.writelock(__FUNCTION__, __LINE__);
  1127. if (required_quests.count(quest_id) == 0)
  1128. required_quests[quest_id] = new vector<int16>;
  1129. else{
  1130. for (int32 i = 0; i < required_quests[quest_id]->size(); i++){
  1131. if (required_quests[quest_id]->at(i) == quest_step){
  1132. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  1133. return;
  1134. }
  1135. }
  1136. }
  1137. required_quests[quest_id]->push_back(quest_step);
  1138. m_requiredQuests.releasewritelock(__FUNCTION__, __LINE__);
  1139. }
  1140. void Spawn::SetRequiredHistory(int32 event_id, int32 value1, int32 value2){
  1141. LUAHistory set_value;
  1142. set_value.Value = value1;
  1143. set_value.Value2 = value2;
  1144. set_value.SaveNeeded = false;
  1145. m_requiredHistory.writelock(__FUNCTION__, __LINE__);
  1146. required_history[event_id] = set_value;
  1147. m_requiredHistory.releasewritelock(__FUNCTION__, __LINE__);
  1148. }
  1149. map<int32, vector<int16>* >* Spawn::GetQuestsRequired(){
  1150. return &required_quests;
  1151. }
  1152. void Spawn::SetTransporterID(int32 id){
  1153. transporter_id = id;
  1154. }
  1155. int32 Spawn::GetTransporterID(){
  1156. return transporter_id;
  1157. }
  1158. void Spawn::InitializePosPacketData(Player* player, PacketStruct* packet, bool bSpawnUpdate) {
  1159. int16 version = packet->GetVersion();
  1160. packet->setDataByName("pos_grid_id", appearance.pos.grid_id);
  1161. bool include_heading = true;
  1162. if (IsWidget() && ((Widget*)this)->GetIncludeHeading() == false)
  1163. include_heading = false;
  1164. else if (IsSign() && ((Sign*)this)->GetIncludeHeading() == false)
  1165. include_heading = false;
  1166. else if (IsGroundSpawn())
  1167. include_heading = false;
  1168. if (include_heading){
  1169. packet->setDataByName("pos_heading1", appearance.pos.Dir1);
  1170. packet->setDataByName("pos_heading2", appearance.pos.Dir2);
  1171. }
  1172. packet->setDataByName("pos_collision_radius", appearance.pos.collision_radius > 0 ? appearance.pos.collision_radius : 32);
  1173. if (version <= 910) {
  1174. packet->setDataByName("pos_size", size > 0 ? size : 32);
  1175. packet->setDataByName("pos_size_multiplier", 32); //32 is normal
  1176. }
  1177. else {
  1178. if (IsPlayer()) {
  1179. if (this != player)
  1180. packet->setDataByName("pos_size", 49152);
  1181. packet->setDataByName("pos_size_ratio", 1);
  1182. packet->setDataByName("pos_size_multiplier_ratio", 1); // used for growth with players
  1183. }
  1184. else {
  1185. packet->setDataByName("pos_size_ratio", size > 0 ? (((float)size) / 32) : 1);
  1186. packet->setDataByName("pos_size_multiplier_ratio", 1);
  1187. }
  1188. }
  1189. packet->setDataByName("pos_state", appearance.pos.state);
  1190. bool include_location = true;
  1191. if (IsWidget() && ((Widget*)this)->GetIncludeLocation() == false)
  1192. include_location = false;
  1193. else if (IsSign() && ((Sign*)this)->GetIncludeLocation() == false)
  1194. include_location = false;
  1195. if (include_location){
  1196. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1197. Widget* widget = (Widget*)this;
  1198. float x = appearance.pos.X - widget->GetWidgetX();
  1199. float y = appearance.pos.Y - widget->GetWidgetY();
  1200. float z = appearance.pos.Z - widget->GetWidgetZ();
  1201. packet->setDataByName("pos_x", x);
  1202. packet->setDataByName("pos_y", y);
  1203. packet->setDataByName("pos_z", z);
  1204. }
  1205. else {
  1206. packet->setDataByName("pos_x", appearance.pos.X);
  1207. packet->setDataByName("pos_y", appearance.pos.Y);
  1208. packet->setDataByName("pos_z", appearance.pos.Z);
  1209. }
  1210. if (IsSign())
  1211. packet->setDataByName("pos_unknown6", 3, 2);
  1212. }
  1213. if (IsPlayer()) {
  1214. packet->setDataByName("pos_x_velocity", static_cast<sint16>(GetSpeedX() * 32));
  1215. packet->setDataByName("pos_y_velocity", static_cast<sint16>(GetSpeedY() * 32));
  1216. packet->setDataByName("pos_z_velocity", static_cast<sint16>(GetSpeedZ() * 32));
  1217. }
  1218. bool bSendSpeed = true;
  1219. if (IsWidget() && ((Widget*)this)->GetMultiFloorLift()) {
  1220. Widget* widget = (Widget*)this;
  1221. float x;
  1222. float y;
  1223. float z;
  1224. if (IsRunning()){
  1225. x = appearance.pos.X2 - widget->GetWidgetX();
  1226. y = appearance.pos.Y2 - widget->GetWidgetY();
  1227. z = appearance.pos.Z2- widget->GetWidgetZ();
  1228. }
  1229. else {
  1230. x = appearance.pos.X - widget->GetWidgetX();
  1231. y = appearance.pos.Y - widget->GetWidgetY();
  1232. z = appearance.pos.Z - widget->GetWidgetZ();
  1233. }
  1234. packet->setDataByName("pos_next_x", x);
  1235. packet->setDataByName("pos_next_y", y);
  1236. packet->setDataByName("pos_next_z", z);
  1237. packet->setDataByName("pos_x3", x);
  1238. packet->setDataByName("pos_y3", y);
  1239. packet->setDataByName("pos_z3", z);
  1240. }
  1241. //If this is a spawn update or this spawn is currently moving we can send these values, otherwise set speed and next_xyz to 0
  1242. //This fixes the bug where spawns with movement scripts face south when initially spawning if they are at their target location.
  1243. else if (bSpawnUpdate || memcmp(&appearance.pos.X, &appearance.pos.X2, sizeof(float) * 3) != 0) {
  1244. packet->setDataByName("pos_next_x", appearance.pos.X2);
  1245. packet->setDataByName("pos_next_y", appearance.pos.Y2);
  1246. packet->setDataByName("pos_next_z", appearance.pos.Z2);
  1247. packet->setDataByName("pos_x3", appearance.pos.X3);
  1248. packet->setDataByName("pos_y3", appearance.pos.Y3);
  1249. packet->setDataByName("pos_z3", appearance.pos.Z3);
  1250. }
  1251. else
  1252. {
  1253. bSendSpeed = false;
  1254. }
  1255. //packet->setDataByName("pos_unknown2", 4, 2);
  1256. int16 speed_multiplier = rule_manager.GetGlobalRule(R_Spawn, SpeedMultiplier)->GetInt16(); // was 1280, 600 and now 300... investigating why
  1257. if (IsPlayer()) {
  1258. Player* player = static_cast<Player*>(this);
  1259. packet->setDataByName("pos_speed", player->GetPosPacketSpeed() * speed_multiplier);
  1260. packet->setDataByName("pos_side_speed", player->GetSideSpeed() * speed_multiplier);
  1261. }
  1262. else if (bSendSpeed) {
  1263. packet->setDataByName("pos_speed", GetSpeed() * speed_multiplier);
  1264. }
  1265. if (IsNPC() || IsPlayer()) {
  1266. packet->setDataByName("pos_move_type", 25);
  1267. }
  1268. else if (IsWidget() || IsSign()) {
  1269. packet->setDataByName("pos_move_type", 11);
  1270. }
  1271. else if(IsGroundSpawn()) {
  1272. packet->setDataByName("pos_move_type", 16);
  1273. }
  1274. if (!IsPlayer())
  1275. packet->setDataByName("pos_movement_mode", 2);
  1276. if(version <= 910)
  1277. packet->setDataByName("pos_unknown10", 0xFFFF, 1);
  1278. else
  1279. packet->setDataByName("pos_unknown10", 0xFFFF);
  1280. if(version <= 910)
  1281. packet->setDataByName("pos_unknown10", 0xFFFF, 2);
  1282. else
  1283. packet->setDataByName("pos_unknown10", 0XFFFF, 1);
  1284. packet->setDataByName("pos_pitch1", appearance.pos.Pitch1);
  1285. packet->setDataByName("pos_pitch2", appearance.pos.Pitch2);
  1286. packet->setDataByName("pos_roll", appearance.pos.Roll);
  1287. }
  1288. void Spawn::InitializeInfoPacketData(Player* spawn, PacketStruct* packet){
  1289. int16 version = packet->GetVersion();
  1290. bool spawnHiddenFromClient = false;
  1291. // radius of 0 is always seen, -1 is never seen (unless items/spells override), larger than 0 is a defined radius to restrict visibility
  1292. sint32 radius = rule_manager.GetGlobalRule(R_PVP, InvisPlayerDiscoveryRange)->GetSInt32();
  1293. if (radius != 0 && (Spawn*)spawn != this && this->IsPlayer() && !spawn->CanSeeInvis((Entity*)this))
  1294. spawnHiddenFromClient = true;
  1295. if(!spawnHiddenFromClient && (appearance.targetable == 1 || appearance.show_level == 1 || appearance.display_name == 1)){
  1296. appearance.locked_no_loot = 1; //for now
  1297. if(!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign()){
  1298. int8 percent = 0;
  1299. if(GetHP() > 0)
  1300. percent = (int8)(((float)GetHP()/GetTotalHP()) * 100);
  1301. if(percent < 100){
  1302. packet->setDataByName("hp_remaining", 100 ^ percent);
  1303. }
  1304. else
  1305. packet->setDataByName("hp_remaining", 0);
  1306. if(GetTotalPower() > 0){
  1307. percent = (int8)(((float)GetPower()/GetTotalPower()) * 100);
  1308. if(percent > 0)
  1309. packet->setDataByName("power_percent", percent);
  1310. else
  1311. packet->setDataByName("power_percent", 0);
  1312. }
  1313. }
  1314. }
  1315. packet->setDataByName("level", (int8)GetLevel());
  1316. packet->setDataByName("unknown4", (int8)GetLevel());
  1317. packet->setDataByName("difficulty", appearance.encounter_level); //6);
  1318. packet->setDataByName("heroic_flag", appearance.heroic_flag);
  1319. if(!IsObject() && !IsGroundSpawn() && !IsWidget() && !IsSign())
  1320. packet->setDataByName("interaction_flag", 12); //this makes NPCs head turn to look at you
  1321. if (version >= 1188 && (IsPlayer() || IsBot()))
  1322. packet->setDataByName("spawn_type", 0);
  1323. else
  1324. packet->setDataByName("spawn_type", spawn_type);
  1325. packet->setDataByName("class", appearance.adventure_class);
  1326. int16 model_type = appearance.model_type;
  1327. if (GetIllusionModel() != 0) {
  1328. if (IsPlayer()) {
  1329. if (((Player*)this)->get_character_flag(CF_SHOW_ILLUSION)) {
  1330. model_type = GetIllusionModel();
  1331. }
  1332. }
  1333. else
  1334. model_type = GetIllusionModel();
  1335. }
  1336. int16 sogaModelType = appearance.soga_model_type;
  1337. if (spawnHiddenFromClient)
  1338. {
  1339. model_type = 0;
  1340. sogaModelType = 0;
  1341. }
  1342. packet->setDataByName("model_type", model_type);
  1343. if(appearance.soga_model_type == 0)
  1344. packet->setDataByName("soga_model_type", model_type);
  1345. else
  1346. packet->setDataByName("soga_model_type", sogaModelType);
  1347. if(GetTempActionState() >= 0)
  1348. packet->setDataByName("action_state", GetTempActionState());
  1349. else
  1350. packet->setDataByName("action_state", appearance.action_state);
  1351. if(GetTempVisualState() >= 0)
  1352. packet->setDataByName("visual_state", GetTempVisualState());
  1353. else
  1354. packet->setDataByName("visual_state", appearance.visual_state);
  1355. packet->setDataByName("emote_state", appearance.emote_state);
  1356. packet->setDataByName("mood_state", appearance.mood_state);
  1357. packet->setDataByName("gender", appearance.gender);
  1358. packet->setDataByName("race", appearance.race);
  1359. packet->setDataByName("gender", appearance.gender);
  1360. if(IsEntity()){
  1361. Entity* entity = ((Entity*)this);
  1362. packet->setDataByName("combat_voice", entity->GetCombatVoice());
  1363. packet->setDataByName("emote_voice", entity->GetEmoteVoice());
  1364. for(int i=0;i<25;i++){
  1365. if(i == 2){ //don't send helm if hidden flag
  1366. if(IsPlayer()){
  1367. if(((Player*)this)->get_character_flag(CF_HIDE_HELM)){
  1368. packet->setDataByName("equipment_types", 0, i);
  1369. packet->setColorByName("equipment_colors", 0, i);
  1370. packet->setColorByName("equipment_highlights", 0, i);
  1371. continue;
  1372. }
  1373. }
  1374. if (IsBot()) {
  1375. if (!((Bot*)this)->ShowHelm) {
  1376. packet->setDataByName("equipment_types", 0, i);
  1377. packet->setColorByName("equipment_colors", 0, i);
  1378. packet->setColorByName("equipment_highlights", 0, i);
  1379. continue;
  1380. }
  1381. }
  1382. }
  1383. else if(i == 19){ //don't send cloak if hidden
  1384. if(IsPlayer()){
  1385. if(!((Player*)this)->get_character_flag(CF_SHOW_CLOAK)){
  1386. packet->setDataByName("equipment_types", 0, i);
  1387. packet->setColorByName("equipment_colors", 0, i);
  1388. packet->setColorByName("equipment_highlights", 0, i);
  1389. continue;
  1390. }
  1391. }
  1392. if (IsBot()) {
  1393. if (!((Bot*)this)->ShowCloak) {
  1394. packet->setDataByName("equipment_types", 0, i);
  1395. packet->setColorByName("equipment_colors", 0, i);
  1396. packet->setColorByName("equipment_highlights", 0, i);
  1397. continue;
  1398. }
  1399. }
  1400. }
  1401. packet->setDataByName("equipment_types", entity->equipment.equip_id[i], i);
  1402. packet->setColorByName("equipment_colors", entity->equipment.color[i], i);
  1403. packet->setColorByName("equipment_highlights", entity->equipment.highlight[i], i);
  1404. }
  1405. packet->setDataByName("mount_type", entity->GetMount());
  1406. // find the visual flags
  1407. int8 vis_flag = 0;
  1408. //Invis + crouch flag check
  1409. if (entity->IsStealthed())
  1410. vis_flag += (INFO_VIS_FLAG_INVIS + INFO_VIS_FLAG_CROUCH);
  1411. //Invis flag check
  1412. else if (entity->IsInvis())
  1413. vis_flag += INFO_VIS_FLAG_INVIS;
  1414. //Mount flag check
  1415. if (entity->GetMount() > 0)
  1416. vis_flag += INFO_VIS_FLAG_MOUNTED;
  1417. //Hide hood check
  1418. if ((IsPlayer() && ((Player*)this)->get_character_flag(CF_HIDE_HOOD)) || appearance.hide_hood)
  1419. vis_flag += INFO_VIS_FLAG_HIDE_HOOD;
  1420. packet->setDataByName("visual_flag", vis_flag);
  1421. packet->setColorByName("mount_saddle_color", entity->GetMountSaddleColor());
  1422. packet->setColorByName("mount_color", entity->GetMountColor());
  1423. packet->setDataByName("hair_type_id", entity->features.hair_type);
  1424. packet->setDataByName("chest_type_id", entity->features.chest_type);
  1425. packet->setDataByName("wing_type_id", entity->features.wing_type);
  1426. packet->setDataByName("legs_type_id", entity->features.legs_type);
  1427. packet->setDataByName("soga_hair_type_id", entity->features.soga_hair_type);
  1428. packet->setDataByName("facial_hair_type_id", entity->features.hair_face_type);
  1429. packet->setDataByName("soga_facial_hair_type_id", entity->features.soga_hair_face_type);
  1430. for(int i=0;i<3;i++){
  1431. packet->setDataByName("eye_type", entity->features.eye_type[i], i);
  1432. packet->setDataByName("ear_type", entity->features.ear_type[i], i);
  1433. packet->setDataByName("eye_brow_type", entity->features.eye_brow_type[i], i);
  1434. packet->setDataByName("cheek_type", entity->features.cheek_type[i], i);
  1435. packet->setDataByName("lip_type", entity->features.lip_type[i], i);
  1436. packet->setDataByName("chin_type", entity->features.chin_type[i], i);
  1437. packet->setDataByName("nose_type", entity->features.nose_type[i], i);
  1438. packet->setDataByName("soga_eye_type", entity->features.soga_eye_type[i], i);
  1439. packet->setDataByName("soga_ear_type", entity->features.soga_ear_type[i], i);
  1440. packet->setDataByName("soga_eye_brow_type", entity->features.soga_eye_brow_type[i], i);
  1441. packet->setDataByName("soga_cheek_type", entity->features.soga_cheek_type[i], i);
  1442. packet->setDataByName("soga_lip_type", entity->features.soga_lip_type[i], i);
  1443. packet->setDataByName("soga_chin_type", entity->features.soga_chin_type[i], i);
  1444. packet->setDataByName("soga_nose_type", entity->features.soga_nose_type[i], i);
  1445. }
  1446. packet->setColorByName("skin_color", entity->features.skin_color);
  1447. packet->setColorByName("eye_color", entity->features.eye_color);
  1448. packet->setColorByName("hair_type_color", entity->features.hair_type_color);
  1449. packet->setColorByName("hair_type_highlight_color", entity->features.hair_type_highlight_color);
  1450. packet->setColorByName("hair_face_color", entity->features.hair_face_color);
  1451. packet->setColorByName("hair_face_highlight_color", entity->features.hair_face_highlight_color);
  1452. packet->setColorByName("hair_highlight", entity->features.hair_highlight_color);
  1453. packet->setColorByName("wing_color1", entity->features.wing_color1);
  1454. packet->setColorByName("wing_color2", entity->features.wing_color2);
  1455. packet->setColorByName("hair_color1", entity->features.hair_color1);
  1456. packet->setColorByName("hair_color2", entity->features.hair_color2);
  1457. packet->setColorByName("soga_skin_color", entity->features.soga_skin_color);
  1458. packet->setColorByName("soga_eye_color", entity->features.soga_eye_color);
  1459. packet->setColorByName("soga_hair_color1", entity->features.soga_hair_color1);
  1460. packet->setColorByName("soga_hair_color2", entity->features.soga_hair_color2);
  1461. packet->setColorByName("soga_hair_type_color", entity->features.soga_hair_type_color);
  1462. packet->setColorByName("soga_hair_type_highlight_color", entity->features.soga_hair_type_highlight_color);
  1463. packet->setColorByName("soga_hair_face_color", entity->features.soga_hair_face_color);
  1464. packet->setColorByName("soga_hair_face_highlight_color", entity->features.soga_hair_face_highlight_color);
  1465. packet->setColorByName("soga_hair_highlight", entity->features.soga_hair_highlight_color);
  1466. packet->setDataByName("body_age", entity->features.body_age);
  1467. }
  1468. else{
  1469. EQ2_Color empty;
  1470. empty.red = 255;
  1471. empty.blue = 255;
  1472. empty.green = 255;
  1473. packet->setColorByName("skin_color", empty);
  1474. packet->setColorByName("eye_color", empty);
  1475. packet->setColorByName("soga_skin_color", empty);
  1476. packet->setColorByName("soga_eye_color", empty);
  1477. }
  1478. if(appearance.icon == 0){
  1479. if(appearance.attackable == 1)
  1480. appearance.icon = 0;
  1481. else if(appearance.encounter_level > 0)
  1482. appearance.icon = 4;
  1483. else
  1484. appearance.icon = 6;
  1485. }
  1486. // If Coe+ clients modify the values before we send
  1487. // if not then just send the value we have.
  1488. int8 temp_icon = appearance.icon;
  1489. //Check if we need to add the hand icon..
  1490. if ((temp_icon & 6) != 6 && appearance.display_hand_icon) {
  1491. temp_icon |= 6;
  1492. }
  1493. //Icon value 28 for boats, set this without modifying the value
  1494. if (version >= 1188 && temp_icon != 28) {
  1495. if ((temp_icon & 64) > 0) {
  1496. temp_icon -= 64; // remove the DoV value;
  1497. temp_icon += 128; // add the CoE value
  1498. }
  1499. if ((temp_icon & 32) > 0) {
  1500. temp_icon -= 32; // remove the DoV value;
  1501. temp_icon += 64; // add the CoE value
  1502. }
  1503. if ((temp_icon & 4) > 0) {
  1504. temp_icon -= 4; // remove DoV value
  1505. temp_icon += 8; // add the CoE icon
  1506. }
  1507. if ((temp_icon & 6) > 0) {
  1508. temp_icon -= 10; // remove DoV value
  1509. temp_icon += 12; // add the CoE icon
  1510. }
  1511. }
  1512. packet->setDataByName("icon", temp_icon);//appearance.icon);
  1513. int16 temp_activity_status = 0;
  1514. if (version >= 1188) {
  1515. if ((appearance.activity_status & ACTIVITY_STATUS_ROLEPLAYING) > 0)
  1516. temp_activity_status += ACTIVITY_STATUS_ROLEPLAYING_1188;
  1517. if ((appearance.activity_status & ACTIVITY_STATUS_ANONYMOUS) > 0)
  1518. temp_activity_status += ACTIVITY_STATUS_ANONYMOUS_1188;
  1519. if ((appearance.activity_status & ACTIVITY_STATUS_LINKDEAD) > 0)
  1520. temp_activity_status += ACTIVITY_STATUS_LINKDEAD_1188;
  1521. if ((appearance.activity_status & ACTIVITY_STATUS_CAMPING) > 0)
  1522. temp_activity_status += ACTIVITY_STATUS_CAMPING_1188;
  1523. if ((appearance.activity_status & ACTIVITY_STATUS_LFG) > 0)
  1524. temp_activity_status += ACTIVITY_STATUS_LFG_1188;
  1525. if ((appearance.activity_status & ACTIVITY_STATUS_LFW) > 0)
  1526. temp_activity_status += ACTIVITY_STATUS_LFW_1188;
  1527. if ((appearance.activity_status & ACTIVITY_STATUS_SOLID) > 0)
  1528. temp_activity_status += ACTIVITY_STATUS_SOLID_1188;
  1529. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_GAINED) > 0)
  1530. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_GAINED_1188;
  1531. if ((appearance.activity_status & ACTIVITY_STATUS_IMMUNITY_REMAINING) > 0)
  1532. temp_activity_status += ACTIVITY_STATUS_IMMUNITY_REMAINING_1188;
  1533. }
  1534. else
  1535. temp_activity_status = appearance.activity_status;
  1536. packet->setDataByName("activity_status", temp_activity_status); //appearance.activity_status);
  1537. // If player and player has a follow target
  1538. if (IsPlayer()) {
  1539. if (((Player*)this)->GetFollowTarget())
  1540. packet->setDataByName("follow_target", ((((Player*)this)->GetIDWithPlayerSpawn(((Player*)this)->GetFollowTarget()) * -1) - 1));
  1541. else
  1542. packet->setDataByName("follow_target", 0);
  1543. }
  1544. if (GetTarget() && GetTarget()->GetTargetable())
  1545. packet->setDataByName("target_id", ((spawn->GetIDWithPlayerSpawn(GetTarget()) * -1) - 1));
  1546. else
  1547. packet->setDataByName("target_id", 0);
  1548. //Send spell effects for target window
  1549. if(IsEntity()){
  1550. InfoStruct* info = ((Entity*)this)->GetInfoStruct();
  1551. int8 i = 0;
  1552. int16 backdrop = 0;
  1553. int16 spell_icon = 0;
  1554. int32 spell_id = 0;
  1555. LuaSpell* spell = 0;
  1556. ((Entity*)this)->GetSpellEffectMutex()->readlock(__FUNCTION__, __LINE__);
  1557. while(i < 30){
  1558. //Change value of spell id for this packet if spell exists
  1559. spell_id = info->spell_effects[i].spell_id;
  1560. if(spell_id > 0)
  1561. spell_id = 0xFFFFFFFF - spell_id;
  1562. else
  1563. spell_id = 0;
  1564. packet->setSubstructDataByName("spell_effects", "spell_id", spell_id, i);
  1565. //Change value of spell icon for this packet if spell exists
  1566. spell_icon = info->spell_effects[i].icon;
  1567. if(spell_icon > 0){
  1568. if(!(spell_icon == 0xFFFF))
  1569. spell_icon = 0xFFFF - spell_icon;
  1570. }
  1571. else
  1572. spell_icon = 0;
  1573. packet->setSubstructDataByName("spell_effects", "spell_icon", spell_icon, i);
  1574. //Change backdrop values to match values in this packet
  1575. backdrop = info->spell_effects[i].icon_backdrop;
  1576. switch(backdrop){
  1577. case 312:
  1578. backdrop = 33080;
  1579. break;
  1580. case 313:
  1581. backdrop = 33081;
  1582. break;
  1583. case 314:
  1584. backdrop = 33082;
  1585. break;
  1586. case 315:
  1587. backdrop = 33083;
  1588. break;
  1589. case 316:
  1590. backdrop = 33084;
  1591. break;
  1592. case 317:
  1593. backdrop = 33085;
  1594. break;
  1595. case (318 || 319):
  1596. backdrop = 33086;
  1597. break;
  1598. default:
  1599. break;
  1600. }
  1601. packet->setSubstructDataByName("spell_effects", "spell_icon_backdrop", backdrop, i);
  1602. spell = info->spell_effects[i].spell;
  1603. if (spell)
  1604. packet->setSubstructDataByName("spell_effects", "spell_triggercount", spell->num_triggers, i);
  1605. i++;
  1606. }
  1607. ((Entity*)this)->GetSpellEffectMutex()->releasereadlock(__FUNCTION__, __LINE__);
  1608. }
  1609. }
  1610. void Spawn::MoveToLocation(Spawn* spawn, float distance, bool immediate, bool mapped){
  1611. if(!spawn)
  1612. return;
  1613. SetRunningTo(spawn);
  1614. FaceTarget(spawn);
  1615. if (!IsPlayer() && distance > 0.0f)
  1616. {
  1617. if (/*!mapped && */GetZone())
  1618. {
  1619. GetZone()->movementMgr->NavigateTo((Entity*)this, spawn->GetX(), spawn->GetY(), spawn->GetZ());
  1620. last_grid_update = Timer::GetCurrentTime2();
  1621. }
  1622. else
  1623. {
  1624. if (immediate)
  1625. ClearRunningLocations();
  1626. AddRunningLocation(spawn->GetX(), spawn->GetY(), spawn->GetZ(), GetSpeed(), distance, true, true, "", mapped);
  1627. }
  1628. }
  1629. }
  1630. void Spawn::ProcessMovement(bool isSpawnListLocked){
  1631. if(IsPlayer()){
  1632. //Check if player is riding a boat, if so update pos (boat's current location + XYZ offsets)
  1633. Player* player = ((Player*)this);
  1634. int32 boat_id = player->GetBoatSpawn();
  1635. Spawn* boat = 0;
  1636. if(boat_id > 0)
  1637. boat = GetZone()->GetSpawnByID(boat_id, isSpawnListLocked);
  1638. if(boat){
  1639. SetX(boat->GetX() + player->GetBoatX());
  1640. SetY(boat->GetY() + player->GetBoatY());
  1641. SetZ(boat->GetZ() + player->GetBoatZ());
  1642. }
  1643. return;
  1644. }
  1645. if (GetHP() <= 0 && !IsWidget())
  1646. return;
  1647. MMovementLoop.lock();
  1648. Spawn* followTarget = GetZone()->GetSpawnByID(m_followTarget, isSpawnListLocked);
  1649. if (!followTarget && m_followTarget > 0)
  1650. m_followTarget = 0;
  1651. if (following && followTarget) {
  1652. // Need to clear m_followTarget before the zoneserver deletes it
  1653. if (followTarget->GetHP() <= 0) {
  1654. followTarget = 0;
  1655. MMovementLoop.unlock();
  1656. return;
  1657. }
  1658. if (!IsEntity() || (!((Entity*)this)->IsCasting() && !((Entity*)this)->IsMezzedOrStunned() && !((Entity*)this)->IsRooted())) {
  1659. if (GetBaseSpeed() > 0) {
  1660. CalculateRunningLocation();
  1661. }
  1662. else {
  1663. float speed = 4.0f;
  1664. if (IsEntity())
  1665. speed = ((Entity*)this)->GetMaxSpeed();
  1666. SetSpeed(speed);
  1667. }
  1668. MovementLocation* loc = GetCurrentRunningLocation();
  1669. if ((GetDistance(followTarget, true) <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())) {
  1670. ClearRunningLocations();
  1671. CalculateRunningLocation(true);
  1672. }
  1673. else if (loc) {
  1674. float distance = GetDistance(followTarget, loc->x, loc->y, loc->z);
  1675. if (distance > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1676. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat(), true, loc->mapped);
  1677. CalculateRunningLocation();
  1678. }
  1679. }
  1680. else {
  1681. MoveToLocation(followTarget, rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat());
  1682. CalculateRunningLocation();
  1683. }
  1684. }
  1685. }
  1686. // Movement loop is only for scripted paths
  1687. else if(!EngagedInCombat() && !NeedsToResumeMovement() && movement_loop.size() > 0 && movement_index < movement_loop.size() && (!IsNPC() || !((NPC*)this)->m_runningBack)){
  1688. // Get the target location
  1689. MovementData* data = movement_loop[movement_index];
  1690. // need to resume our movement
  1691. if(resume_movement){
  1692. if (movement_locations){
  1693. while (movement_locations->size()){
  1694. safe_delete(movement_locations->front());
  1695. movement_locations->pop_front();
  1696. }
  1697. movement_locations->clear();
  1698. }
  1699. data = movement_loop[movement_index];
  1700. SetSpeed(data->speed);
  1701. if(!IsWidget())
  1702. FaceTarget(data->x, data->z);
  1703. // 0 delay at target location, need to set multiple locations
  1704. if(data->delay == 0 && movement_loop.size() > 0) {
  1705. int16 tmp_index = movement_index+1;
  1706. MovementData* data2 = 0;
  1707. if(tmp_index < movement_loop.size())
  1708. data2 = movement_loop[tmp_index];
  1709. else
  1710. data2 = movement_loop[0];
  1711. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  1712. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  1713. }
  1714. // delay at target location, only need to set 1 location
  1715. else
  1716. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1717. movement_start_time = 0;
  1718. resume_movement = false;
  1719. }
  1720. // If we are not moving or we have arrived at our destination
  1721. else if(!IsRunning() || (data && data->x == GetX() && data->y == GetY() && data->z == GetZ())){
  1722. // If we were moving remove the last running location (the point we just arrived at)
  1723. if(IsRunning())
  1724. RemoveRunningLocation();
  1725. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  1726. if(data->delay > 0 && movement_start_time == 0){
  1727. // Set the current time
  1728. movement_start_time = Timer::GetCurrentTime2();
  1729. // If this waypoint had a lua function then call it
  1730. if(data->lua_function.length() > 0)
  1731. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1732. }
  1733. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  1734. else if(data->delay == 0 || (data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  1735. // if no delay at this waypoint but a lua function for it then call the function
  1736. if(data->delay == 0 && data->lua_function.length() > 0)
  1737. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1738. // Advance the current movement loop index
  1739. if((int16)(movement_index+1) < movement_loop.size())
  1740. movement_index++;
  1741. else
  1742. movement_index = 0;
  1743. // Get the next target location
  1744. data = movement_loop[movement_index];
  1745. // set the speed for that location
  1746. SetSpeed(data->speed);
  1747. if(!IsWidget())
  1748. // turn towards the location
  1749. FaceTarget(data->x, data->z);
  1750. // If 0 delay at location get and set data for the point after it
  1751. if(data->delay == 0 && movement_loop.size() > 0){
  1752. while (movement_locations->size()){
  1753. safe_delete(movement_locations->front());
  1754. movement_locations->pop_front();
  1755. }
  1756. // clear current target locations
  1757. movement_locations->clear();
  1758. // get the data for the location after out new location
  1759. int16 tmp_index = movement_index+1;
  1760. MovementData* data2 = 0;
  1761. if(tmp_index < movement_loop.size())
  1762. data2 = movement_loop[tmp_index];
  1763. else
  1764. data2 = movement_loop[0];
  1765. // set the first location (adds it to movement_locations that we just cleared)
  1766. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  1767. // set the location after that
  1768. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  1769. }
  1770. // there is a delay at the next location so we only need to set it
  1771. else
  1772. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1773. // reset this timer to 0 now that we are moving again
  1774. movement_start_time = 0;
  1775. }
  1776. }
  1777. // moving and not at target location yet
  1778. else if(GetBaseSpeed() > 0)
  1779. CalculateRunningLocation();
  1780. // not moving, have a target location but not at it yet
  1781. else if (data) {
  1782. SetSpeed(data->speed);
  1783. AddRunningLocation(data->x, data->y, data->z, data->speed);
  1784. }
  1785. }
  1786. else if (IsRunning()) {
  1787. CalculateRunningLocation();
  1788. }
  1789. else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  1790. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  1791. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  1792. ((NPC*)this)->ClearRunback();
  1793. resume_movement = true;
  1794. NeedsToResumeMovement(false);
  1795. }
  1796. MMovementLoop.unlock();
  1797. }
  1798. void Spawn::ResetMovement(){
  1799. MMovementLoop.lock();
  1800. vector<MovementData*>::iterator itr;
  1801. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  1802. safe_delete(*itr);
  1803. }
  1804. MMovementLoop.unlock();
  1805. resume_movement = true;
  1806. movement_index = 0;
  1807. }
  1808. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function){
  1809. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  1810. x, y, z, speed, delay, string(lua_function).c_str());
  1811. MovementData* data = new MovementData;
  1812. data->x = x;
  1813. data->y = y;
  1814. data->z = z;
  1815. data->speed = speed;
  1816. data->delay = delay*1000;
  1817. if(lua_function)
  1818. data->lua_function = string(lua_function);
  1819. MMovementLoop.lock();
  1820. movement_loop.push_back(data);
  1821. MMovementLoop.unlock();
  1822. }
  1823. bool Spawn::IsRunning(){
  1824. if(movement_locations && movement_locations->size() > 0)
  1825. return true;
  1826. else
  1827. return false;
  1828. }
  1829. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  1830. if(!IsWidget())
  1831. FaceTarget(x, z);
  1832. SetPos(&appearance.pos.X2, x, false);
  1833. SetPos(&appearance.pos.Z2, z, false);
  1834. SetPos(&appearance.pos.Y2, y, false);
  1835. if(following_x == 0 && following_y == 0 && following_z == 0){
  1836. SetPos(&appearance.pos.X3, x, false);
  1837. SetPos(&appearance.pos.Z3, z, false);
  1838. SetPos(&appearance.pos.Y3, y, false);
  1839. }
  1840. else{
  1841. SetPos(&appearance.pos.X3, following_x, false);
  1842. SetPos(&appearance.pos.Y3, following_y, false);
  1843. SetPos(&appearance.pos.Z3, following_z, false);
  1844. }
  1845. position_changed = true;
  1846. changed = true;
  1847. GetZone()->AddChangedSpawn(this);
  1848. }
  1849. MovementLocation* Spawn::GetCurrentRunningLocation(){
  1850. MovementLocation* ret = 0;
  1851. if(movement_locations && movement_locations->size() > 0){
  1852. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  1853. ret = movement_locations->front();
  1854. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  1855. }
  1856. return ret;
  1857. }
  1858. MovementLocation* Spawn::GetLastRunningLocation(){
  1859. MovementLocation* ret = 0;
  1860. if(movement_locations && movement_locations->size() > 0){
  1861. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  1862. ret = movement_locations->back();
  1863. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  1864. }
  1865. return ret;
  1866. }
  1867. void Spawn::AddRunningLocation(float x, float y, float z, float speed, float distance_away, bool attackable, bool finished_adding_locations, string lua_function, bool isMapped){
  1868. if(speed == 0)
  1869. return;
  1870. MovementLocation* current_location = 0;
  1871. float distance = GetDistance(x, y, z, distance_away != 0);
  1872. if(distance_away != 0){
  1873. distance -= distance_away;
  1874. x = x - (GetX() - x)*distance_away/distance;
  1875. z = z - (GetZ() - z)*distance_away/distance;
  1876. }
  1877. if(!movement_locations){
  1878. movement_locations = new deque<MovementLocation*>();
  1879. MMovementLocations = new Mutex();
  1880. }
  1881. MovementLocation* data = new MovementLocation;
  1882. data->mapped = isMapped;
  1883. data->x = x;
  1884. data->y = y;
  1885. data->z = z;
  1886. data->speed = speed;
  1887. data->attackable = attackable;
  1888. data->lua_function = lua_function;
  1889. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  1890. if(movement_locations->size() > 0)
  1891. current_location = movement_locations->back();
  1892. if(!current_location){
  1893. SetSpawnOrigX(GetX());
  1894. SetSpawnOrigY(GetY());
  1895. SetSpawnOrigZ(GetZ());
  1896. SetSpawnOrigHeading(GetHeading());
  1897. }
  1898. movement_locations->push_back(data);
  1899. if(finished_adding_locations){
  1900. current_location = movement_locations->front();
  1901. SetSpeed(current_location->speed);
  1902. if(movement_locations->size() > 1){
  1903. data = movement_locations->at(1);
  1904. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  1905. }
  1906. else
  1907. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  1908. }
  1909. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  1910. }
  1911. bool Spawn::RemoveRunningLocation(){
  1912. bool ret = false;
  1913. if(movement_locations){
  1914. MMovementLocations->writelock(__FUNCTION__, __LINE__);
  1915. if(movement_locations->size() > 0){
  1916. delete movement_locations->front();
  1917. movement_locations->pop_front();
  1918. ret = true;
  1919. }
  1920. MMovementLocations->releasewritelock(__FUNCTION__, __LINE__);
  1921. }
  1922. return ret;
  1923. }
  1924. void Spawn::ClearRunningLocations(){
  1925. while(RemoveRunningLocation()){}
  1926. }
  1927. bool Spawn::CalculateChange(){
  1928. bool remove_needed = false;
  1929. if(movement_locations && MMovementLocations){
  1930. MovementLocation* data = 0;
  1931. MMovementLocations->readlock(__FUNCTION__, __LINE__);
  1932. if(movement_locations->size() > 0){
  1933. // Target location
  1934. data = movement_locations->front();
  1935. // If no target or we are at the target location need to remove this point
  1936. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  1937. remove_needed = true;
  1938. if(data){
  1939. if(NeedsToResumeMovement()){
  1940. resume_movement = true;
  1941. NeedsToResumeMovement(false);
  1942. }
  1943. if(!data->attackable)
  1944. SetHeading(GetSpawnOrigHeading());
  1945. }
  1946. }
  1947. MMovementLocations->releasereadlock(__FUNCTION__, __LINE__);
  1948. if(remove_needed) {
  1949. if (data && data->lua_function.length() > 0)
  1950. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  1951. RemoveRunningLocation();
  1952. //CalculateChange();
  1953. }
  1954. else if(data){
  1955. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  1956. float time_step = (Timer::GetCurrentTime2() - last_movement_update) * 0.001; // * 0.001 is the same as / 1000, float muliplications is suppose to be faster though
  1957. // Get current location
  1958. float nx = GetX();
  1959. float ny = GetY();
  1960. float nz = GetZ();
  1961. // Get Forward vecotr
  1962. float tar_vx = data->x - nx;
  1963. float tar_vy = data->y - ny;
  1964. float tar_vz = data->z - nz;
  1965. // Multiply speed by the time_step to get how much should have changed over the last tick
  1966. float speed = GetSpeed() * time_step;
  1967. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  1968. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  1969. tar_vx = (tar_vx / len) * speed;
  1970. if (GetZone() == NULL || GetZone()->Grid == NULL)
  1971. tar_vy = (tar_vy / len) * speed;
  1972. tar_vz = (tar_vz / len) * speed;
  1973. // Distance less then 0.5 just set the npc to the target location
  1974. if (GetDistance(data->x, data->y, data->z, ( IsWidget() && (!GetZone() || (GetZone() && GetZone()->Grid == NULL))) ? false : true) <= 0.5f) {
  1975. SetX(data->x, false);
  1976. SetZ(data->z, false);
  1977. SetY(data->y, false, true);
  1978. remove_needed = true;
  1979. }
  1980. else {
  1981. SetX(nx + tar_vx, false);
  1982. SetZ(nz + tar_vz, false);
  1983. SetY(ny, false, true);
  1984. remove_needed = true;
  1985. }
  1986. if (GetZone()->Grid != nullptr) {
  1987. Cell* newCell = GetZone()->Grid->GetCell(GetX(), GetZ());
  1988. if (newCell != Cell_Info.CurrentCell) {
  1989. GetZone()->Grid->RemoveSpawnFromCell(this);
  1990. GetZone()->Grid->AddSpawn(this, newCell);
  1991. }
  1992. int32 newGrid = GetZone()->Grid->GetGridID(this);
  1993. if (newGrid != appearance.pos.grid_id)
  1994. SetPos(&(appearance.pos.grid_id), newGrid);
  1995. }
  1996. }
  1997. }
  1998. return remove_needed;
  1999. }
  2000. void Spawn::CalculateRunningLocation(bool stop){
  2001. if (!stop && (last_location_update + 100) > Timer::GetCurrentTime2())
  2002. return;
  2003. else if (!stop)
  2004. last_location_update = Timer::GetCurrentTime2();
  2005. bool removed = CalculateChange();
  2006. if (stop) {
  2007. //following = false;
  2008. SetPos(&appearance.pos.X2, GetX());
  2009. SetPos(&appearance.pos.Y2, GetY());
  2010. SetPos(&appearance.pos.Z2, GetZ());
  2011. SetPos(&appearance.pos.X3, GetX());
  2012. SetPos(&appearance.pos.Y3, GetY());
  2013. SetPos(&appearance.pos.Z3, GetZ());
  2014. }
  2015. else if (removed && movement_locations->size() > 0) {
  2016. MovementLocation* current_location = movement_locations->at(0);
  2017. if (movement_locations->size() > 1) {
  2018. MovementLocation* data = movement_locations->at(1);
  2019. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  2020. }
  2021. else
  2022. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  2023. }
  2024. else if (GetZone() && GetTarget() != NULL && EngagedInCombat())
  2025. {
  2026. if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  2027. GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
  2028. else
  2029. GetZone()->movementMgr->StopNavigation((Entity*)this);
  2030. }
  2031. }
  2032. float Spawn::GetFaceTarget(float x, float z) {
  2033. float angle;
  2034. double diff_x = x - GetX();
  2035. double diff_z = z - GetZ();
  2036. //If we're very close to the same spot don't bother changing heading
  2037. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2038. return GetHeading();
  2039. }
  2040. if (diff_z == 0) {
  2041. if (diff_x > 0)
  2042. angle = 90;
  2043. else
  2044. angle = 270;
  2045. }
  2046. else
  2047. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2048. if (angle < 0)
  2049. angle = angle + 360;
  2050. else
  2051. angle = angle + 180;
  2052. if (diff_x < 0)
  2053. angle = angle + 180;
  2054. if (last_heading_angle == angle) return angle;
  2055. return angle;
  2056. }
  2057. void Spawn::FaceTarget(float x, float z){
  2058. float angle;
  2059. double diff_x = x - GetX();
  2060. double diff_z = z - GetZ();
  2061. //If we're very close to the same spot don't bother changing heading
  2062. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  2063. return;
  2064. }
  2065. if(diff_z==0){
  2066. if(diff_x > 0)
  2067. angle = 90;
  2068. else
  2069. angle = 270;
  2070. }
  2071. else
  2072. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  2073. if(angle < 0)
  2074. angle = angle + 360;
  2075. else
  2076. angle = angle + 180;
  2077. if(diff_x < 0)
  2078. angle = angle + 180;
  2079. if (last_heading_angle == angle) return;
  2080. SetHeading(angle);
  2081. }
  2082. void Spawn::FaceTarget(Spawn* target){
  2083. if(!target)
  2084. return;
  2085. FaceTarget(target->GetX(), target->GetZ());
  2086. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  2087. GetZone()->AddHeadingTimer(this);
  2088. SetTempActionState(0);
  2089. }
  2090. }
  2091. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  2092. bool ret = false;
  2093. Quest* quest = 0;
  2094. //Check if we meet all quest requirements first..
  2095. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  2096. if (player && required_quests.size() > 0) {
  2097. map<int32, vector<int16>* >::iterator itr;
  2098. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  2099. player->AddQuestRequiredSpawn(this, itr->first);
  2100. vector<int16>* quest_steps = itr->second;
  2101. for (int32 i = 0; i < quest_steps->size(); i++) {
  2102. quest = player->GetQuest(itr->first);
  2103. if (req_quests_continued_access) {
  2104. if (quest) {
  2105. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  2106. ret = true;
  2107. break;
  2108. }
  2109. }
  2110. else if (player->GetCompletedQuest(itr->first)) {
  2111. ret = true;
  2112. break;
  2113. }
  2114. }
  2115. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  2116. ret = true;
  2117. break;
  2118. }
  2119. }
  2120. }
  2121. }
  2122. else
  2123. ret = true;
  2124. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  2125. if (!ret)
  2126. return ret;
  2127. //Now check if the player meets all history requirements
  2128. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  2129. if (required_history.size() > 0){
  2130. map<int32, LUAHistory>::iterator itr;
  2131. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  2132. player->AddHistoryRequiredSpawn(this, itr->first);
  2133. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  2134. if (player_history){
  2135. if (player_history->Value != itr->second.Value || player_history->Value2 != itr->second.Value2)
  2136. ret = false;
  2137. }
  2138. else
  2139. ret = false;
  2140. if (!ret)
  2141. break;
  2142. }
  2143. }
  2144. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  2145. return ret;
  2146. }
  2147. vector<Spawn*>* Spawn::GetSpawnGroup(){
  2148. vector<Spawn*>* ret_list = 0;
  2149. if(spawn_group_list){
  2150. ret_list = new vector<Spawn*>();
  2151. if(MSpawnGroup)
  2152. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  2153. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  2154. if(MSpawnGroup)
  2155. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  2156. }
  2157. return ret_list;
  2158. }
  2159. bool Spawn::HasSpawnGroup() {
  2160. return spawn_group_list && spawn_group_list->size() > 0;
  2161. }
  2162. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  2163. bool ret = false;
  2164. if (HasSpawnGroup() && spawn) {
  2165. vector<Spawn*>::iterator itr;
  2166. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  2167. if ((*itr) == spawn) {
  2168. ret = true;
  2169. break;
  2170. }
  2171. }
  2172. }
  2173. return ret;
  2174. }
  2175. void Spawn::AddSpawnToGroup(Spawn* spawn){
  2176. if(!spawn)
  2177. return;
  2178. if(!spawn_group_list){
  2179. spawn_group_list = new vector<Spawn*>();
  2180. spawn_group_list->push_back(this);
  2181. safe_delete(MSpawnGroup);
  2182. MSpawnGroup = new Mutex();
  2183. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  2184. }
  2185. vector<Spawn*>::iterator itr;
  2186. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2187. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2188. if((*itr) == spawn){
  2189. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2190. return;
  2191. }
  2192. }
  2193. spawn_group_list->push_back(spawn);
  2194. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  2195. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2196. }
  2197. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  2198. spawn_group_list = list;
  2199. MSpawnGroup = mutex;
  2200. }
  2201. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  2202. SetSpawnGroupID(0);
  2203. bool del = false;
  2204. if(MSpawnGroup){
  2205. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  2206. if(spawn_group_list){
  2207. vector<Spawn*>::iterator itr;
  2208. Spawn* spawn = 0;
  2209. if(spawn_group_list->size() == 1)
  2210. erase_all = true;
  2211. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  2212. spawn = *itr;
  2213. if (spawn) {
  2214. if(!erase_all){
  2215. if(spawn == this){
  2216. spawn_group_list->erase(itr);
  2217. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2218. spawn_group_list = 0;
  2219. MSpawnGroup = 0;
  2220. return;
  2221. }
  2222. }
  2223. else{
  2224. if (spawn != this)
  2225. spawn->SetSpawnGroupList(0, 0);
  2226. }
  2227. }
  2228. }
  2229. if (erase_all)
  2230. spawn_group_list->clear();
  2231. del = (spawn_group_list->size() == 0);
  2232. }
  2233. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  2234. if (del){
  2235. safe_delete(MSpawnGroup);
  2236. safe_delete(spawn_group_list);
  2237. }
  2238. }
  2239. }
  2240. void Spawn::SetSpawnGroupID(int32 id){
  2241. group_id = id;
  2242. }
  2243. int32 Spawn::GetSpawnGroupID(){
  2244. return group_id;
  2245. }
  2246. void Spawn::AddChangedZoneSpawn(){
  2247. if(send_spawn_changes && GetZone())
  2248. GetZone()->AddChangedSpawn(this);
  2249. }
  2250. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  2251. if (allowed_access.count(spawn->GetID()) > 0) {
  2252. allowed_access.erase(spawn->GetID());
  2253. GetZone()->HidePrivateSpawn(this);
  2254. }
  2255. }
  2256. void Spawn::SetFollowTarget(Spawn* spawn) {
  2257. if (spawn && spawn != this) {
  2258. m_followTarget = spawn->GetID();
  2259. }
  2260. else {
  2261. m_followTarget = 0;
  2262. if (following)
  2263. following = false;
  2264. }
  2265. }
  2266. void Spawn::AddTempVariable(string var, string val) {
  2267. m_tempVariableTypes[var] = 5;
  2268. m_tempVariables[var] = val;
  2269. }
  2270. void Spawn::AddTempVariable(string var, Spawn* val) {
  2271. m_tempVariableTypes[var] = 1;
  2272. m_tempVariableSpawn[var] = val->GetID();
  2273. }
  2274. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  2275. m_tempVariableTypes[var] = 2;
  2276. m_tempVariableZone[var] = val;
  2277. }
  2278. void Spawn::AddTempVariable(string var, Item* val) {
  2279. m_tempVariableTypes[var] = 3;
  2280. m_tempVariableItem[var] = val;
  2281. }
  2282. void Spawn::AddTempVariable(string var, Quest* val) {
  2283. m_tempVariableTypes[var] = 4;
  2284. m_tempVariableQuest[var] = val;
  2285. }
  2286. string Spawn::GetTempVariable(string var) {
  2287. string ret = "";
  2288. if (m_tempVariables.count(var) > 0)
  2289. ret = m_tempVariables[var];
  2290. return ret;
  2291. }
  2292. Spawn* Spawn::GetTempVariableSpawn(string var) {
  2293. Spawn* ret = 0;
  2294. if (m_tempVariableSpawn.count(var) > 0)
  2295. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  2296. return ret;
  2297. }
  2298. ZoneServer* Spawn::GetTempVariableZone(string var) {
  2299. ZoneServer* ret = 0;
  2300. if (m_tempVariableZone.count(var) > 0)
  2301. ret = m_tempVariableZone[var];
  2302. return ret;
  2303. }
  2304. Item* Spawn::GetTempVariableItem(string var) {
  2305. Item* ret = 0;
  2306. if (m_tempVariableItem.count(var) > 0)
  2307. ret = m_tempVariableItem[var];
  2308. return ret;
  2309. }
  2310. Quest* Spawn::GetTempVariableQuest(string var) {
  2311. Quest* ret = 0;
  2312. if (m_tempVariableQuest.count(var) > 0)
  2313. ret = m_tempVariableQuest[var];
  2314. return ret;
  2315. }
  2316. int8 Spawn::GetTempVariableType(string var) {
  2317. int8 ret = 0;
  2318. if (m_tempVariableTypes.count(var) > 0)
  2319. ret = m_tempVariableTypes[var];
  2320. return ret;
  2321. }
  2322. void Spawn::DeleteTempVariable(string var) {
  2323. int8 type = GetTempVariableType(var);
  2324. switch (type) {
  2325. case 1:
  2326. m_tempVariableSpawn.erase(var);
  2327. break;
  2328. case 2:
  2329. m_tempVariableZone.erase(var);
  2330. break;
  2331. case 3:
  2332. m_tempVariableItem.erase(var);
  2333. break;
  2334. case 4:
  2335. m_tempVariableQuest.erase(var);
  2336. break;
  2337. case 5:
  2338. m_tempVariables.erase(var);
  2339. break;
  2340. }
  2341. m_tempVariableTypes.erase(var);
  2342. }
  2343. Spawn* Spawn::GetRunningTo() {
  2344. return GetZone()->GetSpawnByID(running_to);
  2345. }
  2346. Spawn* Spawn::GetFollowTarget() {
  2347. return GetZone()->GetSpawnByID(m_followTarget);
  2348. }
  2349. void Spawn::CopySpawnAppearance(Spawn* spawn){
  2350. if (!spawn)
  2351. return;
  2352. //This function copies the appearace of the provided spawn to this one
  2353. if (spawn->IsEntity() && IsEntity()){
  2354. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  2355. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  2356. }
  2357. SetSize(spawn->GetSize());
  2358. SetModelType(spawn->GetModelType());
  2359. }
  2360. void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
  2361. {
  2362. SetPos(&appearance.pos.Y, y, updateFlags);
  2363. if (!disableYMapCheck)
  2364. FixZ();
  2365. }
  2366. float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
  2367. {
  2368. float best_z = BEST_Z_INVALID;
  2369. if (GetZone() != nullptr && GetZone()->zonemap != nullptr)
  2370. {
  2371. dest.z += z_offset;
  2372. best_z = zone->zonemap->FindBestZ(dest, nullptr);
  2373. }
  2374. return best_z;
  2375. }
  2376. float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
  2377. BenchTimer timer;
  2378. timer.reset();
  2379. float new_z = destination.z;
  2380. if (GetZone() != nullptr && zone->zonemap != nullptr) {
  2381. /* if (flymode == GravityBehavior::Flying)
  2382. return new_z;
  2383. */
  2384. /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
  2385. return new_z;
  2386. */
  2387. /*
  2388. * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
  2389. */
  2390. new_z = this->FindDestGroundZ(destination, z_find_offset);
  2391. if (new_z != BEST_Z_INVALID) {
  2392. new_z += z_find_offset;
  2393. if (new_z < -2000) {
  2394. new_z = GetY();
  2395. }
  2396. }
  2397. auto duration = timer.elapsed();
  2398. LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
  2399. this->GetName(),
  2400. new_z,
  2401. destination.x,
  2402. destination.y,
  2403. destination.z,
  2404. duration);
  2405. }
  2406. return new_z;
  2407. }
  2408. void Spawn::FixZ(int32 z_find_offset /*= 1*/, bool fix_client_z /*= false*/) {
  2409. if (IsPlayer() && !fix_client_z) {
  2410. return;
  2411. }
  2412. /*
  2413. if (flymode == GravityBehavior::Flying) {
  2414. return;
  2415. }*/
  2416. /*
  2417. if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
  2418. return;
  2419. }*/
  2420. glm::vec3 current_loc(GetX(), GetZ(), GetY());
  2421. float new_z = GetFixedZ(current_loc, 1);
  2422. if (new_z == GetY())
  2423. return;
  2424. if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
  2425. SetY(new_z, fix_client_z,true);
  2426. }
  2427. else {
  2428. LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
  2429. }
  2430. }
  2431. bool Spawn::CheckLoS(Spawn* target)
  2432. {
  2433. glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+1.0f);
  2434. glm::vec3 pos(GetX(), GetZ(), GetY()+1.0f);
  2435. return CheckLoS(pos, targpos);
  2436. }
  2437. bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  2438. {
  2439. ZoneServer* zone = GetZone();
  2440. if (zone == NULL || zone->zonemap == NULL)
  2441. return true;
  2442. else
  2443. return zone->zonemap->CheckLoS(myloc, oloc);
  2444. return false;
  2445. }