Spawn.cpp 90 KB

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