Spawn.cpp 130 KB

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