Spawn.cpp 157 KB

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