Spawn.cpp 96 KB

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