Spawn.cpp 98 KB

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