Spawn.cpp 93 KB

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