Spawn.cpp 110 KB

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