Spawn.cpp 91 KB

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