9
3

Spawn.cpp 92 KB

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