Spawn.cpp 131 KB

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