Spawn.cpp 134 KB

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