Spawn.cpp 135 KB

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