Spawn.cpp 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303
  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(!(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) {
  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()) || (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. // If this waypoint has a delay and we just arrived here (movement_start_time == 0)
  2750. if(data && data->delay > 0 && movement_start_time == 0){
  2751. // Set the current time
  2752. movement_start_time = Timer::GetCurrentTime2();
  2753. // If this waypoint had a lua function then call it
  2754. if(data->lua_function.length() > 0)
  2755. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2756. int16 nextMove;
  2757. if ((int16)(movement_index + 1) < movement_loop.size())
  2758. nextMove = movement_index + 1;
  2759. else
  2760. nextMove = 0;
  2761. // Get the next target location
  2762. data = movement_loop[nextMove];
  2763. //Go ahead and face the next location
  2764. if(data)
  2765. FaceTarget(data->x, data->z);
  2766. }
  2767. // If this waypoint has no delay or we have waited the required time (current time >= delay + movement_start_time)
  2768. else if(data && data->delay == 0 || (data && data->delay > 0 && Timer::GetCurrentTime2() >= (data->delay+movement_start_time))) {
  2769. // if no delay at this waypoint but a lua function for it then call the function
  2770. if(data->delay == 0 && data->lua_function.length() > 0)
  2771. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  2772. // since we ran a lua function make sure the movement loop is still alive and accurate
  2773. if(movement_loop.size() > 0)
  2774. {
  2775. // Advance the current movement loop index
  2776. if((int16)(movement_index+1) < movement_loop.size())
  2777. movement_index++;
  2778. else
  2779. movement_index = 0;
  2780. // Get the next target location
  2781. data = movement_loop[movement_index];
  2782. // set the speed for that location
  2783. SetSpeed(data->speed);
  2784. if(!IsWidget())
  2785. // turn towards the location
  2786. FaceTarget(data->x, data->z);
  2787. // If 0 delay at location get and set data for the point after it
  2788. if(data->delay == 0 && movement_loop.size() > 0){
  2789. MMovementLocations.lock();
  2790. if(movement_locations)
  2791. {
  2792. while (movement_locations->size()){
  2793. safe_delete(movement_locations->front());
  2794. movement_locations->pop_front();
  2795. }
  2796. // clear current target locations
  2797. movement_locations->clear();
  2798. }
  2799. MMovementLocations.unlock();
  2800. // get the data for the location after out new location
  2801. int16 tmp_index = movement_index+1;
  2802. MovementData* data2 = 0;
  2803. if(tmp_index < movement_loop.size())
  2804. data2 = movement_loop[tmp_index];
  2805. else
  2806. data2 = movement_loop[0];
  2807. // set the first location (adds it to movement_locations that we just cleared)
  2808. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, false, "", true);
  2809. // set the location after that
  2810. AddRunningLocation(data2->x, data2->y, data2->z, data2->speed, 0, true, true, "", true);
  2811. }
  2812. // there is a delay at the next location so we only need to set it
  2813. else
  2814. AddRunningLocation(data->x, data->y, data->z, data->speed, 0, true, true, "", true);
  2815. // reset this timer to 0 now that we are moving again
  2816. movement_start_time = 0;
  2817. }
  2818. }
  2819. }
  2820. // moving and not at target location yet
  2821. else if(GetBaseSpeed() > 0)
  2822. CalculateRunningLocation();
  2823. // not moving, have a target location but not at it yet
  2824. else if (data) {
  2825. SetSpeed(data->speed);
  2826. AddRunningLocation(data->x, data->y, data->z, data->speed);
  2827. }
  2828. }
  2829. MMovementLoop.releasewritelock();
  2830. }
  2831. if (!movementCase && IsRunning() && !IsPauseMovementTimerActive()) {
  2832. CalculateRunningLocation();
  2833. //last_movement_update = Timer::GetCurrentTime2();
  2834. }
  2835. else if(movementCase)
  2836. {
  2837. //last_movement_update = Timer::GetCurrentTime2();
  2838. }
  2839. /*else if (IsNPC() && !IsRunning() && !EngagedInCombat() && ((NPC*)this)->GetRunbackLocation()) {
  2840. // Is an npc that is not moving and not engaged in combat but has a run back location set then clear the runback location
  2841. LogWrite(NPC_AI__DEBUG, 7, "NPC_AI", "Clear runback location for %s", GetName());
  2842. ((NPC*)this)->ClearRunback();
  2843. resume_movement = true;
  2844. NeedsToResumeMovement(false);
  2845. }*/
  2846. }
  2847. void Spawn::ResetMovement(){
  2848. MMovementLoop.writelock();
  2849. vector<MovementData*>::iterator itr;
  2850. for(itr = movement_loop.begin(); itr != movement_loop.end(); itr++){
  2851. safe_delete(*itr);
  2852. }
  2853. movement_loop.clear();
  2854. movement_index = 0;
  2855. resume_movement = true;
  2856. ClearRunningLocations();
  2857. MMovementLoop.releasewritelock();
  2858. ValidateRunning(true, true);
  2859. }
  2860. void Spawn::AddMovementLocation(float x, float y, float z, float speed, int16 delay, const char* lua_function, float heading, bool include_heading){
  2861. LogWrite(LUA__DEBUG, 5, "LUA", "AddMovementLocation: x: %.2f, y: %.2f, z: %.2f, speed: %.2f, delay: %i, lua: %s",
  2862. x, y, z, speed, delay, string(lua_function).c_str());
  2863. MovementData* data = new MovementData;
  2864. data->x = x;
  2865. data->y = y;
  2866. data->z = z;
  2867. data->speed = speed;
  2868. data->delay = delay*1000;
  2869. if(lua_function)
  2870. data->lua_function = string(lua_function);
  2871. data->heading = heading;
  2872. data->use_movement_location_heading = include_heading;
  2873. MMovementLoop.lock();
  2874. movement_loop.push_back(data);
  2875. MMovementLoop.unlock();
  2876. }
  2877. bool Spawn::ValidateRunning(bool lockMovementLocation, bool lockMovementLoop) {
  2878. bool movement = false;
  2879. if(lockMovementLocation) {
  2880. MMovementLocations.lock_shared();
  2881. }
  2882. if(movement_locations) {
  2883. movement = movement_locations->size() > 0;
  2884. }
  2885. if(lockMovementLocation) {
  2886. MMovementLocations.unlock_shared();
  2887. }
  2888. if(IsPauseMovementTimerActive() || (IsEntity() && (((Entity*)this)->IsMezzedOrStunned() || ((Entity*)this)->IsRooted()))) {
  2889. is_running = false;
  2890. return false;
  2891. }
  2892. if(movement) {
  2893. is_running = true;
  2894. return true;
  2895. }
  2896. if(lockMovementLoop) {
  2897. MMovementLoop.lock();
  2898. }
  2899. movement = movement_loop.size() > 0;
  2900. if(movement) {
  2901. is_running = true;
  2902. }
  2903. else {
  2904. is_running = false;
  2905. }
  2906. if(lockMovementLoop) {
  2907. MMovementLoop.unlock();
  2908. }
  2909. return movement;
  2910. }
  2911. bool Spawn::IsRunning(){
  2912. return is_running;
  2913. }
  2914. void Spawn::RunToLocation(float x, float y, float z, float following_x, float following_y, float following_z){
  2915. if(IsPauseMovementTimerActive() || (IsEntity() && (((Entity*)this)->IsMezzedOrStunned() || ((Entity*)this)->IsRooted()))) {
  2916. is_running = false;
  2917. return;
  2918. }
  2919. if(!IsWidget())
  2920. FaceTarget(x, z);
  2921. SetPos(&appearance.pos.X2, x, false);
  2922. SetPos(&appearance.pos.Z2, z, false);
  2923. SetPos(&appearance.pos.Y2, y, false);
  2924. if(following_x == 0 && following_y == 0 && following_z == 0){
  2925. SetPos(&appearance.pos.X3, x, false);
  2926. SetPos(&appearance.pos.Z3, z, false);
  2927. SetPos(&appearance.pos.Y3, y, false);
  2928. }
  2929. else{
  2930. SetPos(&appearance.pos.X3, following_x, false);
  2931. SetPos(&appearance.pos.Y3, following_y, false);
  2932. SetPos(&appearance.pos.Z3, following_z, false);
  2933. }
  2934. is_running = true;
  2935. position_changed = true;
  2936. changed = true;
  2937. GetZone()->AddChangedSpawn(this);
  2938. }
  2939. MovementLocation* Spawn::GetCurrentRunningLocation(){
  2940. MovementLocation* ret = 0;
  2941. MMovementLocations.lock_shared();
  2942. if(movement_locations && movement_locations->size() > 0){
  2943. ret = movement_locations->front();
  2944. }
  2945. MMovementLocations.unlock_shared();
  2946. return ret;
  2947. }
  2948. MovementLocation* Spawn::GetLastRunningLocation(){
  2949. MovementLocation* ret = 0;
  2950. MMovementLocations.lock_shared();
  2951. if(movement_locations && movement_locations->size() > 0){
  2952. ret = movement_locations->back();
  2953. }
  2954. MMovementLocations.unlock_shared();
  2955. return ret;
  2956. }
  2957. 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){
  2958. if(speed == 0)
  2959. return;
  2960. if ( IsEntity() )
  2961. ((Entity*)this)->SetSpeed(speed);
  2962. else
  2963. this->SetSpeed(speed);
  2964. MovementLocation* current_location = 0;
  2965. float distance = GetDistance(x, y, z, distance_away != 0);
  2966. if(distance_away != 0){
  2967. distance -= distance_away;
  2968. x = x - (GetX() - x)*distance_away/distance;
  2969. z = z - (GetZ() - z)*distance_away/distance;
  2970. }
  2971. MMovementLocations.lock();
  2972. if(!movement_locations){
  2973. movement_locations = new deque<MovementLocation*>();
  2974. }
  2975. MMovementLocations.unlock();
  2976. MovementLocation* data = new MovementLocation;
  2977. data->mapped = isMapped;
  2978. data->x = x;
  2979. data->y = y;
  2980. data->z = z;
  2981. data->speed = speed;
  2982. data->attackable = attackable;
  2983. data->lua_function = lua_function;
  2984. data->gridid = 0; // used for runback defaults
  2985. data->reset_hp_on_runback = false;
  2986. MMovementLocations.lock_shared();
  2987. if(movement_locations->size() > 0)
  2988. current_location = movement_locations->back();
  2989. MMovementLocations.unlock_shared();
  2990. if(!current_location){
  2991. SetSpawnOrigX(GetX());
  2992. SetSpawnOrigY(GetY());
  2993. SetSpawnOrigZ(GetZ());
  2994. SetSpawnOrigHeading(GetHeading());
  2995. }
  2996. is_running = true;
  2997. MMovementLocations.lock();
  2998. movement_locations->push_back(data);
  2999. MMovementLocations.unlock();
  3000. if(!IsPauseMovementTimerActive() && finished_adding_locations){
  3001. MMovementLocations.lock();
  3002. current_location = movement_locations->front();
  3003. SetSpeed(current_location->speed);
  3004. if(movement_locations->size() > 1){
  3005. data = movement_locations->at(1);
  3006. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  3007. }
  3008. else
  3009. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  3010. MMovementLocations.unlock();
  3011. }
  3012. }
  3013. bool Spawn::RemoveRunningLocation(){
  3014. bool ret = false;
  3015. MMovementLocations.lock();
  3016. if(movement_locations && movement_locations->size() > 0){
  3017. delete movement_locations->front();
  3018. movement_locations->pop_front();
  3019. ret = true;
  3020. }
  3021. MMovementLocations.unlock();
  3022. ValidateRunning(true, false);
  3023. return ret;
  3024. }
  3025. void Spawn::ClearRunningLocations(){
  3026. while(RemoveRunningLocation()){}
  3027. }
  3028. void Spawn::NewWaypointChange(MovementLocation* data){
  3029. if(data){
  3030. if(NeedsToResumeMovement()){
  3031. resume_movement = true;
  3032. NeedsToResumeMovement(false);
  3033. }
  3034. if(!data->attackable)
  3035. SetHeading(GetSpawnOrigHeading());
  3036. }
  3037. if (data && data->lua_function.length() > 0)
  3038. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, 0, data->lua_function.c_str());
  3039. RemoveRunningLocation();
  3040. }
  3041. bool Spawn::CalculateChange(){
  3042. bool remove_needed = false;
  3043. MovementLocation* data = 0;
  3044. MovementLocation tmpLoc;
  3045. MMovementLocations.lock_shared();
  3046. if(movement_locations){
  3047. if(movement_locations->size() > 0){
  3048. // Target location
  3049. data = movement_locations->front();
  3050. if(data) {
  3051. tmpLoc.attackable = data->attackable;
  3052. tmpLoc.gridid = data->gridid;
  3053. tmpLoc.lua_function = string(data->lua_function);
  3054. tmpLoc.mapped = data->mapped;
  3055. tmpLoc.reset_hp_on_runback = data->reset_hp_on_runback;
  3056. tmpLoc.speed = data->speed;
  3057. tmpLoc.stage = data->stage;
  3058. tmpLoc.x = data->x;
  3059. tmpLoc.y = data->y;
  3060. tmpLoc.z = data->z;
  3061. data = &tmpLoc;
  3062. }
  3063. // If no target or we are at the target location need to remove this point
  3064. if(!data || (data->x == GetX() && data->y == GetY() && data->z == GetZ()))
  3065. remove_needed = true;
  3066. }
  3067. }
  3068. MMovementLocations.unlock_shared();
  3069. if(remove_needed){
  3070. NewWaypointChange(data);
  3071. }
  3072. else if(data){
  3073. // Speed is per second so we need a time_step (amount of time since the last update) to modify movement by
  3074. 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
  3075. // Get current location
  3076. float nx = GetX();
  3077. float ny = GetY();
  3078. float nz = GetZ();
  3079. // Get Forward vecotr
  3080. float tar_vx = data->x - nx;
  3081. float tar_vy = data->y - ny;
  3082. float tar_vz = data->z - nz;
  3083. // Multiply speed by the time_step to get how much should have changed over the last tick
  3084. float speed = GetSpeed() * time_step;
  3085. // Normalize the forward vector and multiply by speed, this gives us our change in coords, just need to add them to our current coords
  3086. float len = sqrtf(tar_vx * tar_vx + tar_vy * tar_vy + tar_vz * tar_vz);
  3087. tar_vx = (tar_vx / len) * speed;
  3088. tar_vy = (tar_vy / len) * speed;
  3089. tar_vz = (tar_vz / len) * speed;
  3090. // Distance less then 0.5 just set the npc to the target location
  3091. if (GetDistance(data->x, data->y, data->z, IsWidget() ? false : true) <= speed) {
  3092. SetX(data->x, false);
  3093. SetZ(data->z, false);
  3094. SetY(data->y, false, true);
  3095. remove_needed = true;
  3096. NewWaypointChange(data);
  3097. }
  3098. else {
  3099. SetX(nx + tar_vx, false);
  3100. SetZ(nz + tar_vz, false);
  3101. if ( IsWidget() )
  3102. SetY(ny + tar_vy, false, true);
  3103. else
  3104. SetY(ny + tar_vy, false);
  3105. }
  3106. int32 newGrid = GetLocation();
  3107. if (GetMap() != nullptr) {
  3108. m_GridMutex.writelock(__FUNCTION__, __LINE__);
  3109. std::map<int32,TimedGridData>::iterator itr = established_grid_id.begin();
  3110. if ( itr == established_grid_id.end() || itr->second.timestamp <= (Timer::GetCurrentTime2()))
  3111. {
  3112. if(itr != established_grid_id.end() && itr->second.x == GetX() && itr->second.z == GetZ()) {
  3113. itr->second.timestamp = Timer::GetCurrentTime2()+1000;
  3114. itr->second.npc_save = true;
  3115. newGrid = itr->second.grid_id;
  3116. }
  3117. else {
  3118. auto loc = glm::vec3(GetX(), GetZ(), GetY());
  3119. float new_z = FindBestZ(loc, nullptr, &newGrid);
  3120. TimedGridData data;
  3121. data.grid_id = newGrid;
  3122. data.x = GetX();
  3123. data.y = GetY();
  3124. data.z = GetZ();
  3125. data.npc_save = true;
  3126. data.timestamp = Timer::GetCurrentTime2()+1000;
  3127. established_grid_id.insert(make_pair(0, data));
  3128. }
  3129. }
  3130. else
  3131. newGrid = itr->second.grid_id;
  3132. m_GridMutex.releasewritelock(__FUNCTION__, __LINE__);
  3133. }
  3134. if ((!IsFlyingCreature() || IsTransportSpawn()) && newGrid != 0 && newGrid != GetLocation())
  3135. SetLocation(newGrid);
  3136. }
  3137. return remove_needed;
  3138. }
  3139. void Spawn::CalculateRunningLocation(bool stop){
  3140. bool pauseTimerEnabled = IsPauseMovementTimerActive();
  3141. if (!pauseTimerEnabled && !stop && (last_location_update + 100) > Timer::GetCurrentTime2())
  3142. return;
  3143. else if (!pauseTimerEnabled && !stop)
  3144. last_location_update = Timer::GetCurrentTime2();
  3145. bool continueElseIf = true;
  3146. bool removed = CalculateChange();
  3147. if (stop || pauseTimerEnabled) {
  3148. //following = false;
  3149. SetPos(&appearance.pos.X2, GetX(), false);
  3150. SetPos(&appearance.pos.Y2, GetY(), false);
  3151. SetPos(&appearance.pos.Z2, GetZ(), false);
  3152. SetPos(&appearance.pos.X3, GetX(), false);
  3153. SetPos(&appearance.pos.Y3, GetY(), false);
  3154. SetPos(&appearance.pos.Z3, GetZ(), false);
  3155. continueElseIf = false;
  3156. }
  3157. else if (removed) {
  3158. MMovementLocations.lock_shared();
  3159. if(movement_locations) {
  3160. if(movement_locations->size() > 0) {
  3161. MovementLocation* current_location = movement_locations->at(0);
  3162. if (movement_locations->size() > 1) {
  3163. MovementLocation* data = movement_locations->at(1);
  3164. RunToLocation(current_location->x, current_location->y, current_location->z, data->x, data->y, data->z);
  3165. }
  3166. else
  3167. RunToLocation(current_location->x, current_location->y, current_location->z, 0, 0, 0);
  3168. continueElseIf = false;
  3169. }
  3170. }
  3171. MMovementLocations.unlock_shared();
  3172. }
  3173. if (continueElseIf && GetZone() && GetTarget() != NULL && EngagedInCombat())
  3174. {
  3175. if (GetDistance(GetTarget()) > rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat())
  3176. {
  3177. if ((IsFlyingCreature() || IsWaterCreature() || InWater()) && CheckLoS(GetTarget()))
  3178. AddRunningLocation(GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ(), GetSpeed(), 0, false);
  3179. else
  3180. GetZone()->movementMgr->NavigateTo((Entity*)this, GetTarget()->GetX(), GetTarget()->GetY(), GetTarget()->GetZ());
  3181. }
  3182. else
  3183. ((Entity*)this)->HaltMovement();
  3184. }
  3185. else if (continueElseIf && !following)
  3186. {
  3187. position_changed = true;
  3188. changed = true;
  3189. GetZone()->AddChangedSpawn(this);
  3190. }
  3191. }
  3192. float Spawn::GetFaceTarget(float x, float z) {
  3193. float angle;
  3194. double diff_x = x - GetX();
  3195. double diff_z = z - GetZ();
  3196. //If we're very close to the same spot don't bother changing heading
  3197. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  3198. return GetHeading();
  3199. }
  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
  3211. angle = angle + 180;
  3212. if (diff_x < 0)
  3213. angle = angle + 180;
  3214. if (last_heading_angle == angle) return angle;
  3215. return angle;
  3216. }
  3217. void Spawn::FaceTarget(float x, float z){
  3218. float angle;
  3219. double diff_x = x - GetX();
  3220. double diff_z = z - GetZ();
  3221. //If we're very close to the same spot don't bother changing heading
  3222. if (sqrt(diff_x * diff_x * diff_z * diff_z) < .1) {
  3223. return;
  3224. }
  3225. if(diff_z==0){
  3226. if(diff_x > 0)
  3227. angle = 90;
  3228. else
  3229. angle = 270;
  3230. }
  3231. else
  3232. angle = ((atan(diff_x / diff_z)) * 180) / 3.14159265358979323846;
  3233. if(angle < 0)
  3234. angle = angle + 360;
  3235. else
  3236. angle = angle + 180;
  3237. if(diff_x < 0)
  3238. angle = angle + 180;
  3239. if (last_heading_angle == angle) return;
  3240. SetHeading(angle);
  3241. }
  3242. void Spawn::FaceTarget(Spawn* target, bool disable_action_state){
  3243. if(!target)
  3244. return;
  3245. if(GetHP() > 0 && target->IsPlayer() && !EngagedInCombat()){
  3246. if(!IsPet() && disable_action_state) {
  3247. if(IsNPC()) {
  3248. ((NPC*)this)->StartRunback();
  3249. ((NPC*)this)->PauseMovement(30000);
  3250. }
  3251. SetTempActionState(0);
  3252. }
  3253. }
  3254. FaceTarget(target->GetX(), target->GetZ());
  3255. }
  3256. bool Spawn::MeetsSpawnAccessRequirements(Player* player){
  3257. bool ret = false;
  3258. Quest* quest = 0;
  3259. //Check if we meet all quest requirements first..
  3260. m_requiredQuests.readlock(__FUNCTION__, __LINE__);
  3261. if (player && required_quests.size() > 0) {
  3262. map<int32, vector<int16>* >::iterator itr;
  3263. for (itr = required_quests.begin(); itr != required_quests.end(); itr++) {
  3264. player->AddQuestRequiredSpawn(this, itr->first);
  3265. vector<int16>* quest_steps = itr->second;
  3266. for (int32 i = 0; i < quest_steps->size(); i++) {
  3267. quest = player->GetQuest(itr->first);
  3268. if (req_quests_continued_access) {
  3269. if (quest) {
  3270. if (quest->GetQuestStepCompleted(quest_steps->at(i))) {
  3271. ret = true;
  3272. break;
  3273. }
  3274. }
  3275. else if (player->HasQuestBeenCompleted(itr->first)) {
  3276. ret = true;
  3277. break;
  3278. }
  3279. }
  3280. if (quest && quest->QuestStepIsActive(quest_steps->at(i))) {
  3281. ret = true;
  3282. break;
  3283. }
  3284. }
  3285. }
  3286. }
  3287. else
  3288. ret = true;
  3289. m_requiredQuests.releasereadlock(__FUNCTION__, __LINE__);
  3290. if (!ret)
  3291. return ret;
  3292. //Now check if the player meets all history requirements
  3293. m_requiredHistory.readlock(__FUNCTION__, __LINE__);
  3294. if (required_history.size() > 0){
  3295. map<int32, LUAHistory*>::iterator itr;
  3296. for (itr = required_history.begin(); itr != required_history.end(); itr++){
  3297. player->AddHistoryRequiredSpawn(this, itr->first);
  3298. LUAHistory* player_history = player->GetLUAHistory(itr->first);
  3299. if (player_history){
  3300. if (player_history->Value != itr->second->Value || player_history->Value2 != itr->second->Value2)
  3301. ret = false;
  3302. }
  3303. else
  3304. ret = false;
  3305. if (!ret)
  3306. break;
  3307. }
  3308. }
  3309. m_requiredHistory.releasereadlock(__FUNCTION__, __LINE__);
  3310. return ret;
  3311. }
  3312. vector<Spawn*>* Spawn::GetSpawnGroup(){
  3313. vector<Spawn*>* ret_list = 0;
  3314. if(spawn_group_list){
  3315. ret_list = new vector<Spawn*>();
  3316. if(MSpawnGroup)
  3317. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  3318. ret_list->insert(ret_list->begin(), spawn_group_list->begin(), spawn_group_list->end());
  3319. if(MSpawnGroup)
  3320. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  3321. }
  3322. return ret_list;
  3323. }
  3324. bool Spawn::HasSpawnGroup() {
  3325. return spawn_group_list && spawn_group_list->size() > 0;
  3326. }
  3327. bool Spawn::IsInSpawnGroup(Spawn* spawn) {
  3328. bool ret = false;
  3329. if (HasSpawnGroup() && spawn) {
  3330. vector<Spawn*>::iterator itr;
  3331. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  3332. if ((*itr) == spawn) {
  3333. ret = true;
  3334. break;
  3335. }
  3336. }
  3337. }
  3338. return ret;
  3339. }
  3340. Spawn* Spawn::IsSpawnGroupMembersAlive(Spawn* ignore_spawn, bool npc_only) {
  3341. Spawn* ret = nullptr;
  3342. if (MSpawnGroup && HasSpawnGroup()) {
  3343. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  3344. vector<Spawn*>::iterator itr;
  3345. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  3346. if ((*itr) != ignore_spawn && (*itr)->Alive() && (!npc_only || (npc_only && (*itr)->IsNPC()))) {
  3347. ret = (*itr);
  3348. break;
  3349. }
  3350. }
  3351. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  3352. }
  3353. return ret;
  3354. }
  3355. void Spawn::UpdateEncounterState(int8 new_state) {
  3356. if (MSpawnGroup && HasSpawnGroup()) {
  3357. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  3358. vector<Spawn*>::iterator itr;
  3359. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  3360. if ((*itr)->Alive() && (*itr)->IsNPC()) {
  3361. NPC* npc = (NPC*)(*itr);
  3362. (*itr)->SetLockedNoLoot(new_state);
  3363. if(new_state == ENCOUNTER_STATE_BROKEN && npc->Brain()) {
  3364. npc->Brain()->ClearEncounter();
  3365. }
  3366. }
  3367. }
  3368. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  3369. }
  3370. }
  3371. void Spawn::CheckEncounterState(Entity* victim, bool test_auto_lock) {
  3372. if (!IsEntity() || !victim->IsNPC())
  3373. return;
  3374. Entity* ent = ((Entity*)this);
  3375. if (victim->GetLockedNoLoot() == ENCOUNTER_STATE_AVAILABLE) {
  3376. Entity* attacker = nullptr;
  3377. if (ent->GetOwner())
  3378. attacker = ent->GetOwner();
  3379. else
  3380. attacker = ent;
  3381. bool matchedAutoLock = false;
  3382. if (attacker->IsEntity() && ((Entity*)attacker)->GetGroupMemberInfo()) {
  3383. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  3384. GroupMemberInfo* gmi = ((Entity*)attacker)->GetGroupMemberInfo();
  3385. if (gmi && gmi->group_id)
  3386. {
  3387. PlayerGroup* group = world.GetGroupManager()->GetGroup(gmi->group_id);
  3388. if (group && ((group->GetGroupOptions()->group_lock_method && group->GetGroupOptions()->group_autolock == 1) || attacker->GetGroupMemberInfo()->leader))
  3389. {
  3390. matchedAutoLock = true;
  3391. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  3392. deque<GroupMemberInfo*>* members = group->GetMembers();
  3393. for (int8 i = 0; i < members->size(); i++) {
  3394. Entity* member = members->at(i)->member;
  3395. if (!member || member->GetZone() != attacker->GetZone())
  3396. continue;
  3397. if (member->IsEntity()) {
  3398. if (!member->GetInfoStruct()->get_engaged_encounter()) {
  3399. member->GetInfoStruct()->set_engaged_encounter(1);
  3400. }
  3401. if (((NPC*)victim)->Brain()) {
  3402. ((NPC*)victim)->Brain()->AddHate(member, 0);
  3403. ((NPC*)victim)->Brain()->AddToEncounter(member);
  3404. victim->AddTargetToEncounter(member);
  3405. }
  3406. }
  3407. }
  3408. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  3409. }
  3410. }
  3411. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  3412. }
  3413. else if (attacker->GetInfoStruct()->get_group_solo_autolock()) {
  3414. matchedAutoLock = true;
  3415. if (((NPC*)victim)->Brain()) {
  3416. ((NPC*)victim)->Brain()->AddHate(attacker, 0);
  3417. ((NPC*)victim)->Brain()->AddToEncounter(attacker);
  3418. victim->AddTargetToEncounter(attacker);
  3419. }
  3420. }
  3421. if (test_auto_lock && !matchedAutoLock) {
  3422. return;
  3423. }
  3424. if (!ent->GetInfoStruct()->get_engaged_encounter()) {
  3425. ent->GetInfoStruct()->set_engaged_encounter(1);
  3426. }
  3427. if (!attacker->GetInfoStruct()->get_engaged_encounter()) {
  3428. attacker->GetInfoStruct()->set_engaged_encounter(1);
  3429. }
  3430. int8 skip_loot_gray_mob_flag = rule_manager.GetGlobalRule(R_Loot, SkipLootGrayMob)->GetInt8();
  3431. int8 difficulty = attacker->GetArrowColor(victim->GetLevel());
  3432. int8 new_enc_state = ENCOUNTER_STATE_AVAILABLE;
  3433. if (skip_loot_gray_mob_flag && difficulty == ARROW_COLOR_GRAY) {
  3434. if (!attacker->IsPlayer() && !attacker->IsBot()) {
  3435. new_enc_state = ENCOUNTER_STATE_BROKEN;
  3436. }
  3437. else {
  3438. new_enc_state = ENCOUNTER_STATE_OVERMATCHED;
  3439. }
  3440. }
  3441. else {
  3442. if (attacker->IsPlayer() || attacker->IsBot()) {
  3443. new_enc_state = ENCOUNTER_STATE_LOCKED;
  3444. }
  3445. else {
  3446. new_enc_state = ENCOUNTER_STATE_BROKEN;
  3447. }
  3448. }
  3449. victim->SetLockedNoLoot(new_enc_state);
  3450. victim->UpdateEncounterState(new_enc_state);
  3451. }
  3452. }
  3453. void Spawn::AddTargetToEncounter(Entity* entity) {
  3454. if (MSpawnGroup && HasSpawnGroup()) {
  3455. MSpawnGroup->readlock(__FUNCTION__, __LINE__);
  3456. vector<Spawn*>::iterator itr;
  3457. for (itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++) {
  3458. if ((*itr) != this && (*itr)->Alive() && (*itr)->IsNPC()) {
  3459. ((NPC*)(*itr))->Brain()->AddToEncounter(entity);
  3460. }
  3461. }
  3462. MSpawnGroup->releasereadlock(__FUNCTION__, __LINE__);
  3463. }
  3464. }
  3465. void Spawn::AddSpawnToGroup(Spawn* spawn){
  3466. if(!spawn)
  3467. return;
  3468. if(!spawn_group_list){
  3469. spawn_group_list = new vector<Spawn*>();
  3470. spawn_group_list->push_back(this);
  3471. safe_delete(MSpawnGroup);
  3472. MSpawnGroup = new Mutex();
  3473. MSpawnGroup->SetName("Spawn::MSpawnGroup");
  3474. }
  3475. vector<Spawn*>::iterator itr;
  3476. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  3477. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  3478. if((*itr) == spawn){
  3479. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3480. return;
  3481. }
  3482. }
  3483. spawn_group_list->push_back(spawn);
  3484. spawn->SetSpawnGroupList(spawn_group_list, MSpawnGroup);
  3485. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3486. }
  3487. void Spawn::SetSpawnGroupList(vector<Spawn*>* list, Mutex* mutex){
  3488. spawn_group_list = list;
  3489. MSpawnGroup = mutex;
  3490. }
  3491. void Spawn::RemoveSpawnFromGroup(bool erase_all){
  3492. SetSpawnGroupID(0);
  3493. bool del = false;
  3494. if(MSpawnGroup){
  3495. MSpawnGroup->writelock(__FUNCTION__, __LINE__);
  3496. if(spawn_group_list){
  3497. vector<Spawn*>::iterator itr;
  3498. Spawn* spawn = 0;
  3499. if(spawn_group_list->size() == 1)
  3500. erase_all = true;
  3501. for(itr = spawn_group_list->begin(); itr != spawn_group_list->end(); itr++){
  3502. spawn = *itr;
  3503. if (spawn) {
  3504. if(!erase_all){
  3505. if(spawn == this){
  3506. spawn_group_list->erase(itr);
  3507. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3508. spawn_group_list = 0;
  3509. MSpawnGroup = 0;
  3510. return;
  3511. }
  3512. }
  3513. else{
  3514. if (spawn != this)
  3515. spawn->SetSpawnGroupList(0, 0);
  3516. }
  3517. }
  3518. }
  3519. if (erase_all)
  3520. spawn_group_list->clear();
  3521. del = (spawn_group_list->size() == 0);
  3522. }
  3523. MSpawnGroup->releasewritelock(__FUNCTION__, __LINE__);
  3524. if (del){
  3525. safe_delete(MSpawnGroup);
  3526. safe_delete(spawn_group_list);
  3527. }
  3528. }
  3529. }
  3530. void Spawn::SetSpawnGroupID(int32 id){
  3531. m_SpawnMutex.writelock();
  3532. group_id = id;
  3533. m_SpawnMutex.releasewritelock();
  3534. }
  3535. int32 Spawn::GetSpawnGroupID(){
  3536. int32 groupid = 0;
  3537. m_SpawnMutex.readlock();
  3538. groupid = group_id;
  3539. m_SpawnMutex.releasereadlock();
  3540. return groupid;
  3541. }
  3542. void Spawn::AddChangedZoneSpawn(){
  3543. if(send_spawn_changes && GetZone())
  3544. GetZone()->AddChangedSpawn(this);
  3545. }
  3546. void Spawn::RemoveSpawnAccess(Spawn* spawn) {
  3547. if (allowed_access.count(spawn->GetID()) > 0) {
  3548. allowed_access.erase(spawn->GetID());
  3549. GetZone()->HidePrivateSpawn(this);
  3550. }
  3551. }
  3552. void Spawn::SetFollowTarget(Spawn* spawn, int32 follow_distance) {
  3553. if (spawn && spawn != this) {
  3554. m_followTarget = spawn->GetID();
  3555. m_followDistance = follow_distance;
  3556. }
  3557. else {
  3558. m_followTarget = 0;
  3559. if (following)
  3560. following = false;
  3561. m_followDistance = 0;
  3562. }
  3563. }
  3564. void Spawn::AddTempVariable(string var, string val) {
  3565. m_tempVariableTypes[var] = 5;
  3566. m_tempVariables[var] = val;
  3567. }
  3568. void Spawn::AddTempVariable(string var, Spawn* val) {
  3569. m_tempVariableTypes[var] = 1;
  3570. m_tempVariableSpawn[var] = val->GetID();
  3571. }
  3572. void Spawn::AddTempVariable(string var, ZoneServer* val) {
  3573. m_tempVariableTypes[var] = 2;
  3574. m_tempVariableZone[var] = val;
  3575. }
  3576. void Spawn::AddTempVariable(string var, Item* val) {
  3577. m_tempVariableTypes[var] = 3;
  3578. m_tempVariableItem[var] = val;
  3579. }
  3580. void Spawn::AddTempVariable(string var, Quest* val) {
  3581. m_tempVariableTypes[var] = 4;
  3582. m_tempVariableQuest[var] = val;
  3583. }
  3584. string Spawn::GetTempVariable(string var) {
  3585. string ret = "";
  3586. if (m_tempVariables.count(var) > 0)
  3587. ret = m_tempVariables[var];
  3588. return ret;
  3589. }
  3590. Spawn* Spawn::GetTempVariableSpawn(string var) {
  3591. Spawn* ret = 0;
  3592. if (m_tempVariableSpawn.count(var) > 0)
  3593. ret = GetZone()->GetSpawnByID(m_tempVariableSpawn[var]);
  3594. return ret;
  3595. }
  3596. ZoneServer* Spawn::GetTempVariableZone(string var) {
  3597. ZoneServer* ret = 0;
  3598. if (m_tempVariableZone.count(var) > 0)
  3599. ret = m_tempVariableZone[var];
  3600. return ret;
  3601. }
  3602. Item* Spawn::GetTempVariableItem(string var) {
  3603. Item* ret = 0;
  3604. if (m_tempVariableItem.count(var) > 0)
  3605. ret = m_tempVariableItem[var];
  3606. return ret;
  3607. }
  3608. Quest* Spawn::GetTempVariableQuest(string var) {
  3609. Quest* ret = 0;
  3610. if (m_tempVariableQuest.count(var) > 0)
  3611. ret = m_tempVariableQuest[var];
  3612. return ret;
  3613. }
  3614. int8 Spawn::GetTempVariableType(string var) {
  3615. int8 ret = 0;
  3616. if (m_tempVariableTypes.count(var) > 0)
  3617. ret = m_tempVariableTypes[var];
  3618. return ret;
  3619. }
  3620. void Spawn::DeleteTempVariable(string var) {
  3621. int8 type = GetTempVariableType(var);
  3622. switch (type) {
  3623. case 1:
  3624. m_tempVariableSpawn.erase(var);
  3625. break;
  3626. case 2:
  3627. m_tempVariableZone.erase(var);
  3628. break;
  3629. case 3:
  3630. m_tempVariableItem.erase(var);
  3631. break;
  3632. case 4:
  3633. m_tempVariableQuest.erase(var);
  3634. break;
  3635. case 5:
  3636. m_tempVariables.erase(var);
  3637. break;
  3638. }
  3639. m_tempVariableTypes.erase(var);
  3640. }
  3641. Spawn* Spawn::GetRunningTo() {
  3642. return GetZone()->GetSpawnByID(running_to);
  3643. }
  3644. Spawn* Spawn::GetFollowTarget() {
  3645. return GetZone()->GetSpawnByID(m_followTarget);
  3646. }
  3647. void Spawn::CopySpawnAppearance(Spawn* spawn){
  3648. if (!spawn)
  3649. return;
  3650. //This function copies the appearace of the provided spawn to this one
  3651. if (spawn->IsEntity() && IsEntity()){
  3652. memcpy(&((Entity*)this)->features, &((Entity*)spawn)->features, sizeof(CharFeatures));
  3653. memcpy(&((Entity*)this)->equipment, &((Entity*)spawn)->equipment, sizeof(EQ2_Equipment));
  3654. }
  3655. SetSize(spawn->GetSize());
  3656. SetModelType(spawn->GetModelType());
  3657. }
  3658. void Spawn::SetY(float y, bool updateFlags, bool disableYMapCheck)
  3659. {
  3660. SetPos(&appearance.pos.Y, y, updateFlags);
  3661. if (!disableYMapCheck)
  3662. FixZ();
  3663. }
  3664. float Spawn::FindDestGroundZ(glm::vec3 dest, float z_offset)
  3665. {
  3666. float best_z = BEST_Z_INVALID;
  3667. if (GetZone() != nullptr && GetMap() != nullptr)
  3668. {
  3669. dest.z += z_offset;
  3670. best_z = FindBestZ(dest, nullptr);
  3671. }
  3672. return best_z;
  3673. }
  3674. float Spawn::FindBestZ(glm::vec3 loc, glm::vec3* result, int32* new_grid_id, int32* new_widget_id) {
  3675. std::shared_lock lock(MIgnoredWidgets);
  3676. if(!GetMap())
  3677. return BEST_Z_INVALID;
  3678. float new_z = GetMap()->FindBestZ(loc, nullptr, &ignored_widgets, new_grid_id, new_widget_id);
  3679. return new_z;
  3680. }
  3681. float Spawn::GetFixedZ(const glm::vec3& destination, int32 z_find_offset) {
  3682. BenchTimer timer;
  3683. timer.reset();
  3684. float new_z = destination.z;
  3685. if (GetZone() != nullptr && GetMap() != nullptr) {
  3686. /* if (flymode == GravityBehavior::Flying)
  3687. return new_z;
  3688. */
  3689. /* if (zone->HasWaterMap() && zone->watermap->InLiquid(glm::vec3(m_Position)))
  3690. return new_z;
  3691. */
  3692. /*
  3693. * Any more than 5 in the offset makes NPC's hop/snap to ceiling in small corridors
  3694. */
  3695. new_z = this->FindDestGroundZ(destination, z_find_offset);
  3696. if (new_z != BEST_Z_INVALID) {
  3697. if (new_z < -2000) {
  3698. new_z = GetY();
  3699. }
  3700. }
  3701. auto duration = timer.elapsed();
  3702. LogWrite(MAP__DEBUG, 0, "Map", "Mob::GetFixedZ() ([{%s}]) returned [{%f}] at [{%f}], [{%f}], [{%f}] - Took [{%f}]",
  3703. this->GetName(),
  3704. new_z,
  3705. destination.x,
  3706. destination.y,
  3707. destination.z,
  3708. duration);
  3709. }
  3710. return new_z;
  3711. }
  3712. void Spawn::FixZ(bool forceUpdate) {
  3713. if (!GetZone()) {
  3714. return;
  3715. }
  3716. /*
  3717. if (flymode == GravityBehavior::Flying) {
  3718. return;
  3719. }*/
  3720. /*
  3721. if (zone->watermap && zone->watermap->InLiquid(m_Position)) {
  3722. return;
  3723. }*/
  3724. // we do the inwater check here manually to avoid double calling for a Z coordinate
  3725. glm::vec3 current_loc(GetX(), GetZ(), GetY());
  3726. uint32 GridID = 0;
  3727. uint32 WidgetID = 0;
  3728. float new_z = GetY();
  3729. if(GetMap() != nullptr) {
  3730. new_z = FindBestZ(current_loc, nullptr, &GridID, &WidgetID);
  3731. if ((IsTransportSpawn() || !IsFlyingCreature()) && GridID != 0 && GridID != GetLocation()) {
  3732. LogWrite(PLAYER__DEBUG, 0, "Player", "%s left grid %u and entered grid %u", appearance.name, GetLocation(), GridID);
  3733. const char* zone_script = world.GetZoneScript(GetZone()->GetZoneID());
  3734. if (zone_script && lua_interface) {
  3735. lua_interface->RunZoneScript(zone_script, "leave_location", GetZone(), this, GetLocation());
  3736. }
  3737. SetLocation(GridID);
  3738. if (zone_script && lua_interface) {
  3739. lua_interface->RunZoneScript(zone_script, "enter_location", GetZone(), this, GridID);
  3740. }
  3741. }
  3742. trigger_widget_id = WidgetID;
  3743. }
  3744. // no need to go any further for players, flying creatures or objects, just needed the grid id set
  3745. if (IsPlayer() || IsFlyingCreature() || IsObject()) {
  3746. return;
  3747. }
  3748. if ( region_map != nullptr )
  3749. {
  3750. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3751. if(region_map->InWater(targPos, GetLocation()))
  3752. return;
  3753. }
  3754. if (new_z == GetY())
  3755. return;
  3756. if ((new_z > -2000) && new_z != BEST_Z_INVALID) {
  3757. SetY(new_z, forceUpdate, true);
  3758. }
  3759. else {
  3760. LogWrite(MAP__DEBUG, 0, "Map", "[{%s}] is failing to find Z [{%f}]", this->GetName(), std::abs(GetY() - new_z));
  3761. }
  3762. }
  3763. bool Spawn::CheckLoS(Spawn* target)
  3764. {
  3765. float radiusSrc = 2.0f;
  3766. float radiusTarg = 2.0f;
  3767. glm::vec3 targpos(target->GetX(), target->GetZ(), target->GetY()+radiusTarg);
  3768. glm::vec3 pos(GetX(), GetZ(), GetY()+radiusSrc);
  3769. return CheckLoS(pos, targpos);
  3770. }
  3771. bool Spawn::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  3772. {
  3773. bool res = false;
  3774. ZoneServer* zone = GetZone();
  3775. if (zone == NULL || GetMap() == NULL || !GetMap()->IsMapLoaded())
  3776. return true;
  3777. else {
  3778. MIgnoredWidgets.lock_shared();
  3779. res = GetMap()->CheckLoS(myloc, oloc, &ignored_widgets);
  3780. MIgnoredWidgets.unlock_shared();
  3781. }
  3782. return res;
  3783. }
  3784. void Spawn::CalculateNewFearpoint()
  3785. {
  3786. if (GetZone() && GetZone()->pathing) {
  3787. auto Node = zone->pathing->GetRandomLocation(glm::vec3(GetX(), GetZ(), GetY()));
  3788. if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) {
  3789. AddRunningLocation(Node.x, Node.y, Node.z, GetSpeed(), 0, true, true, "", true);
  3790. }
  3791. }
  3792. }
  3793. Item* Spawn::LootItem(int32 id) {
  3794. Item* ret = 0;
  3795. vector<Item*>::iterator itr;
  3796. MLootItems.lock();
  3797. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3798. if ((*itr)->details.item_id == id) {
  3799. ret = *itr;
  3800. loot_items.erase(itr);
  3801. break;
  3802. }
  3803. }
  3804. MLootItems.unlock();
  3805. return ret;
  3806. }
  3807. void Spawn::TransferLoot(Spawn* spawn) {
  3808. if(spawn == this || spawn == nullptr)
  3809. return; // mmm no
  3810. vector<Item*>::iterator itr;
  3811. MLootItems.lock();
  3812. for (itr = loot_items.begin(); itr != loot_items.end();) {
  3813. if (!(*itr)->IsBodyDrop()) {
  3814. spawn->AddLootItem(*itr);
  3815. itr = loot_items.erase(itr);
  3816. }
  3817. else {
  3818. itr++;
  3819. }
  3820. }
  3821. MLootItems.unlock();
  3822. }
  3823. int32 Spawn::GetLootItemID() {
  3824. int32 ret = 0;
  3825. vector<Item*>::iterator itr;
  3826. MLootItems.lock();
  3827. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3828. ret = (*itr)->details.item_id;
  3829. break;
  3830. }
  3831. MLootItems.unlock();
  3832. return ret;
  3833. }
  3834. void Spawn::GetLootItemsList(std::vector<int32>* out_entries) {
  3835. if(!out_entries)
  3836. return;
  3837. vector<Item*>::iterator itr;
  3838. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3839. out_entries->push_back((*itr)->details.item_id);
  3840. }
  3841. }
  3842. bool Spawn::HasLootItemID(int32 id) {
  3843. bool ret = false;
  3844. vector<Item*>::iterator itr;
  3845. MLootItems.readlock(__FUNCTION__, __LINE__);
  3846. for (itr = loot_items.begin(); itr != loot_items.end(); itr++) {
  3847. if ((*itr)->details.item_id == id) {
  3848. ret = true;
  3849. break;
  3850. }
  3851. }
  3852. MLootItems.releasereadlock(__FUNCTION__, __LINE__);
  3853. return ret;
  3854. }
  3855. void Spawn::CheckProximities()
  3856. {
  3857. if (!has_spawn_proximities)
  3858. return;
  3859. if (spawn_proximities.size() > 0)
  3860. {
  3861. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3862. while (itr.Next()) {
  3863. SpawnProximity* prox = itr.value;
  3864. map<int32, bool>::iterator spawnsItr;
  3865. for (spawnsItr = prox->spawns_in_proximity.begin(); spawnsItr != prox->spawns_in_proximity.end(); spawnsItr++) {
  3866. Spawn* tmpSpawn = 0;
  3867. if (spawnsItr->first &&
  3868. ((prox->spawn_type == SPAWNPROXIMITY_DATABASE_ID && (tmpSpawn = GetZone()->GetSpawnByDatabaseID(spawnsItr->first)) != 0) ||
  3869. (prox->spawn_type == SPAWNPROXIMITY_LOCATION_ID && (tmpSpawn = GetZone()->GetSpawnByLocationID(spawnsItr->first)) != 0)))
  3870. {
  3871. if (!spawnsItr->second && tmpSpawn->GetDistance(this) <= prox->distance)
  3872. {
  3873. if (prox->in_range_lua_function.size() > 0)
  3874. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->in_range_lua_function.c_str());
  3875. spawnsItr->second = true;
  3876. }
  3877. else if (spawnsItr->second && tmpSpawn->GetDistance(this) > prox->distance)
  3878. {
  3879. if (prox->leaving_range_lua_function.size() > 0)
  3880. GetZone()->CallSpawnScript(this, SPAWN_SCRIPT_CUSTOM, tmpSpawn, prox->leaving_range_lua_function.c_str());
  3881. spawnsItr->second = false;
  3882. }
  3883. }
  3884. }
  3885. }
  3886. }
  3887. }
  3888. void Spawn::AddSpawnToProximity(int32 spawnValue, SpawnProximityType type)
  3889. {
  3890. if (!has_spawn_proximities)
  3891. return;
  3892. if (spawn_proximities.size() > 0)
  3893. {
  3894. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3895. while (itr.Next()) {
  3896. SpawnProximity* prox = itr->value;
  3897. if (prox->spawn_value == spawnValue && prox->spawn_type == type)
  3898. prox->spawns_in_proximity.insert(make_pair(spawnValue, false));
  3899. }
  3900. }
  3901. }
  3902. void Spawn::RemoveSpawnFromProximity(int32 spawnValue, SpawnProximityType type)
  3903. {
  3904. if (!has_spawn_proximities)
  3905. return;
  3906. if (spawn_proximities.size() > 0)
  3907. {
  3908. MutexList<SpawnProximity*>::iterator itr = spawn_proximities.begin();
  3909. while (itr.Next()) {
  3910. SpawnProximity* prox = itr->value;
  3911. if (prox->spawn_value == spawnValue && prox->spawn_type == type &&
  3912. prox->spawns_in_proximity.count(spawnValue) > 0)
  3913. prox->spawns_in_proximity.erase(spawnValue);
  3914. }
  3915. }
  3916. }
  3917. 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) {
  3918. EntityCommand* cmd = FindEntityCommand(string(command), true);
  3919. bool newCommand = false;
  3920. if (!cmd)
  3921. {
  3922. newCommand = true;
  3923. cmd = CreateEntityCommand(name, distance, command, error_text, cast_time, spell_visual, !defaultDenyList);
  3924. }
  3925. if (defaultDenyList)
  3926. SetPermissionToEntityCommand(cmd, player, true);
  3927. if (newCommand)
  3928. primary_command_list.push_back(cmd);
  3929. }
  3930. void Spawn::RemovePrimaryEntityCommand(const char* command) {
  3931. vector<EntityCommand*>::iterator itr;
  3932. string tmpStr(command);
  3933. for (itr = primary_command_list.begin(); itr != primary_command_list.end(); itr++) {
  3934. EntityCommand* cmd = *itr;
  3935. if (cmd->command.compare(tmpStr) == 0)
  3936. {
  3937. primary_command_list.erase(itr);
  3938. delete cmd;
  3939. break;
  3940. }
  3941. }
  3942. }
  3943. bool Spawn::SetPermissionToEntityCommand(EntityCommand* command, Player* player, bool permissionValue)
  3944. {
  3945. if(!player)
  3946. return false;
  3947. return SetPermissionToEntityCommandByCharID(command, player->GetCharacterID(), permissionValue);
  3948. }
  3949. bool Spawn::SetPermissionToEntityCommandByCharID(EntityCommand* command, int32 charID, bool permissionValue)
  3950. {
  3951. map<int32, bool>::iterator itr = command->allow_or_deny.find(charID);
  3952. if (itr == command->allow_or_deny.end())
  3953. command->allow_or_deny.insert(make_pair(charID, permissionValue));
  3954. else if (itr->second != permissionValue)
  3955. itr->second = permissionValue;
  3956. return true;
  3957. }
  3958. void Spawn::RemoveSpawnFromPlayer(Player* player)
  3959. {
  3960. m_Update.writelock(__FUNCTION__, __LINE__);
  3961. player->RemoveSpawn(this); // sets it as removed
  3962. m_Update.releasewritelock(__FUNCTION__, __LINE__);
  3963. }
  3964. bool Spawn::InWater()
  3965. {
  3966. bool inWater = false;
  3967. if ( region_map != nullptr )
  3968. {
  3969. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3970. if ( IsGroundSpawn() )
  3971. targPos.y -= .5f;
  3972. if(region_map->InWater(targPos, GetLocation()))
  3973. inWater = true;
  3974. }
  3975. return inWater;
  3976. }
  3977. bool Spawn::InLava()
  3978. {
  3979. bool inLava = false;
  3980. if ( region_map != nullptr )
  3981. {
  3982. glm::vec3 targPos(GetX(), GetY(), GetZ());
  3983. if ( IsGroundSpawn() )
  3984. targPos.y -= .5f;
  3985. if(region_map->InLava(targPos, GetLocation()))
  3986. inLava = true;
  3987. }
  3988. return inLava;
  3989. }
  3990. void Spawn::DeleteRegion(Region_Node* inNode, ZBSP_Node* rootNode)
  3991. {
  3992. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  3993. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  3994. {
  3995. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  3996. Region_Node* node = actualItr->first;
  3997. ZBSP_Node* BSP_Root = actualItr->second;
  3998. if(inNode == node && rootNode == BSP_Root )
  3999. {
  4000. testitr = Regions.erase(testitr);
  4001. break;
  4002. }
  4003. }
  4004. }
  4005. bool Spawn::InRegion(Region_Node* inNode, ZBSP_Node* rootNode)
  4006. {
  4007. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  4008. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  4009. {
  4010. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  4011. Region_Node* node = actualItr->first;
  4012. ZBSP_Node* BSP_Root = actualItr->second;
  4013. if(inNode == node && rootNode == BSP_Root )
  4014. {
  4015. return testitr->second.inRegion;
  4016. }
  4017. }
  4018. return false;
  4019. }
  4020. int32 Spawn::GetRegionType(Region_Node* inNode, ZBSP_Node* rootNode)
  4021. {
  4022. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  4023. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  4024. {
  4025. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  4026. Region_Node* node = actualItr->first;
  4027. ZBSP_Node* BSP_Root = actualItr->second;
  4028. if(inNode == node && rootNode == BSP_Root )
  4029. {
  4030. return testitr->second.regionType;
  4031. }
  4032. }
  4033. return false;
  4034. }
  4035. float Spawn::SpawnAngle(Spawn* target, float selfx, float selfz)
  4036. {
  4037. if (!target || target == this)
  4038. return 0.0f;
  4039. float angle, lengthb, vectorx, vectorz, dotp;
  4040. float spx = (target->GetX()); // mob xloc (inverse because eq)
  4041. float spz = -(target->GetZ()); // mob yloc
  4042. float heading = target->GetHeading(); // mob heading
  4043. if (heading < 270)
  4044. heading += 90;
  4045. else
  4046. heading -= 270;
  4047. heading = heading * 3.1415f / 180.0f; // convert to radians
  4048. vectorx = spx + (10.0f * std::cos(heading)); // create a vector based on heading
  4049. vectorz = spz + (10.0f * std::sin(heading)); // of spawn length 10
  4050. // length of spawn to player vector
  4051. lengthb = (float) std::sqrt(((selfx - spx) * (selfx - spx)) + ((-selfz - spz) * (-selfz - spz)));
  4052. // calculate dot product to get angle
  4053. // Handle acos domain errors due to floating point rounding errors
  4054. dotp = ((vectorx - spx) * (selfx - spx) +
  4055. (vectorz - spz) * (-selfz - spz)) / (10.0f * lengthb);
  4056. if (dotp > 1)
  4057. return 0.0f;
  4058. else if (dotp < -1)
  4059. return 180.0f;
  4060. angle = std::acos(dotp);
  4061. angle = angle * 180.0f / 3.1415f;
  4062. return angle;
  4063. }
  4064. void Spawn::StopMovement()
  4065. {
  4066. reset_movement = true;
  4067. }
  4068. bool Spawn::PauseMovement(int32 period_of_time_ms)
  4069. {
  4070. if(period_of_time_ms < 1)
  4071. period_of_time_ms = 1;
  4072. RunToLocation(GetX(),GetY(),GetZ());
  4073. pause_timer.Start(period_of_time_ms, true);
  4074. return true;
  4075. }
  4076. bool Spawn::IsPauseMovementTimerActive()
  4077. {
  4078. if(pause_timer.Check())
  4079. pause_timer.Disable();
  4080. return pause_timer.Enabled();
  4081. }
  4082. bool Spawn::IsFlyingCreature()
  4083. {
  4084. if(!IsEntity())
  4085. return false;
  4086. return ((Entity*)this)->GetInfoStruct()->get_flying_type();
  4087. }
  4088. bool Spawn::IsWaterCreature()
  4089. {
  4090. if(!IsEntity())
  4091. return false;
  4092. return ((Entity*)this)->GetInfoStruct()->get_water_type();
  4093. }
  4094. void Spawn::SetFlyingCreature() {
  4095. if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeFlyingModelType)->GetInt8())
  4096. return;
  4097. if(((Entity*)this)->GetInfoStruct()->get_flying_type() > 0) // DB spawn npc flag already set
  4098. return;
  4099. switch (GetModelType())
  4100. {
  4101. case 260:
  4102. case 295:
  4103. ((Entity*)this)->GetInfoStruct()->set_flying_type(1);
  4104. is_flying_creature = true;
  4105. break;
  4106. default:
  4107. ((Entity*)this)->GetInfoStruct()->set_flying_type(0);
  4108. break;
  4109. }
  4110. }
  4111. void Spawn::SetWaterCreature() {
  4112. if(!IsEntity() || !rule_manager.GetGlobalRule(R_Spawn, UseHardCodeWaterModelType)->GetInt8())
  4113. return;
  4114. if(((Entity*)this)->GetInfoStruct()->get_water_type() > 0) // DB spawn npc flag already set
  4115. return;
  4116. switch (GetModelType())
  4117. {
  4118. case 194:
  4119. case 204:
  4120. case 210:
  4121. case 241:
  4122. case 242:
  4123. case 254:
  4124. case 10668:
  4125. case 20828:
  4126. ((Entity*)this)->GetInfoStruct()->set_water_type(1);
  4127. break;
  4128. default:
  4129. ((Entity*)this)->GetInfoStruct()->set_water_type(0);
  4130. break;
  4131. }
  4132. }
  4133. void Spawn::AddRailPassenger(int32 char_id)
  4134. {
  4135. std::lock_guard<std::mutex> lk(m_RailMutex);
  4136. rail_passengers.insert(make_pair(char_id,true));
  4137. }
  4138. void Spawn::RemoveRailPassenger(int32 char_id)
  4139. {
  4140. std::lock_guard<std::mutex> lk(m_RailMutex);
  4141. std::map<int32, bool>::iterator itr = rail_passengers.find(char_id);
  4142. if(itr != rail_passengers.end())
  4143. rail_passengers.erase(itr);
  4144. }
  4145. vector<Spawn*> Spawn::GetPassengersOnRail() {
  4146. vector<Spawn*> tmp_list;
  4147. Spawn* spawn;
  4148. m_RailMutex.lock();
  4149. std::map<int32, bool>::iterator itr = rail_passengers.begin();
  4150. while(itr != rail_passengers.end()){
  4151. Client* client = zone_list.GetClientByCharID(itr->first);
  4152. if(!client || !client->GetPlayer())
  4153. continue;
  4154. tmp_list.push_back(client->GetPlayer());
  4155. itr++;
  4156. }
  4157. m_RailMutex.unlock();
  4158. return tmp_list;
  4159. }
  4160. void Spawn::SetAppearancePosition(float x, float y, float z) {
  4161. appearance.pos.X = x;
  4162. appearance.pos.Y = y;
  4163. appearance.pos.Z = z;
  4164. appearance.pos.X2 = appearance.pos.X;
  4165. appearance.pos.Y2 = appearance.pos.Y;
  4166. appearance.pos.Z2 = appearance.pos.Z;
  4167. appearance.pos.X3 = appearance.pos.X;
  4168. appearance.pos.Y3 = appearance.pos.Y;
  4169. appearance.pos.Z3 = appearance.pos.Z;
  4170. SetSpeedX(0);
  4171. SetSpeedY(0);
  4172. SetSpeedZ(0);
  4173. if(IsPlayer()) {
  4174. ((Player*)this)->SetSideSpeed(0);
  4175. ((Player*)this)->pos_packet_speed = 0;
  4176. }
  4177. }
  4178. int32 Spawn::InsertRegionToSpawn(Region_Node* node, ZBSP_Node* bsp_root, WaterRegionType regionType, bool in_region) {
  4179. std::map<Region_Node*, ZBSP_Node*> newMap;
  4180. newMap.insert(make_pair(node, bsp_root));
  4181. Region_Status status;
  4182. status.inRegion = in_region;
  4183. status.regionType = regionType;
  4184. int32 returnValue = 0;
  4185. if(in_region) {
  4186. lua_interface->RunRegionScript(node->regionScriptName, "EnterRegion", GetZone(), this, regionType, &returnValue);
  4187. }
  4188. status.timerTic = returnValue;
  4189. status.lastTimerTic = returnValue ? Timer::GetCurrentTime2() : 0;
  4190. Regions.insert(make_pair(newMap, status));
  4191. return returnValue;
  4192. }
  4193. bool Spawn::HasRegionTracked(Region_Node* node, ZBSP_Node* bsp_root, bool in_region) {
  4194. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  4195. for (testitr = Regions.begin(); testitr != Regions.end(); testitr++)
  4196. {
  4197. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  4198. Region_Node *node = actualItr->first;
  4199. ZBSP_Node *BSP_Root = actualItr->second;
  4200. if(node == actualItr->first && BSP_Root == actualItr->second) {
  4201. if(testitr->second.inRegion == in_region)
  4202. return true;
  4203. else
  4204. break;
  4205. }
  4206. }
  4207. return false;
  4208. }
  4209. void Spawn::SetLocation(int32 id, bool setUpdateFlags)
  4210. {
  4211. if(GetZone()) {
  4212. GetZone()->RemoveSpawnFromGrid(this, GetLocation());
  4213. SetPos(&appearance.pos.grid_id, id, setUpdateFlags);
  4214. GetZone()->AddSpawnToGrid(this, id);
  4215. }
  4216. else {
  4217. SetPos(&appearance.pos.grid_id, id, setUpdateFlags);
  4218. }
  4219. }
  4220. int8 Spawn::GetArrowColor(int8 spawn_level){
  4221. int8 color = 0;
  4222. sint16 diff = spawn_level - GetLevel();
  4223. if(GetLevel() < 10)
  4224. diff *= 3;
  4225. else if(GetLevel() <= 20)
  4226. diff *= 2;
  4227. if(diff >= 9)
  4228. color = ARROW_COLOR_RED;
  4229. else if(diff >= 5)
  4230. color = ARROW_COLOR_ORANGE;
  4231. else if(diff >= 1)
  4232. color = ARROW_COLOR_YELLOW;
  4233. else if(diff == 0)
  4234. color = ARROW_COLOR_WHITE;
  4235. else if(diff <= -11)
  4236. color = ARROW_COLOR_GRAY;
  4237. else if(diff <= -6)
  4238. color = ARROW_COLOR_GREEN;
  4239. else //if(diff < 0)
  4240. color = ARROW_COLOR_BLUE;
  4241. return color;
  4242. }
  4243. void Spawn::AddIgnoredWidget(int32 id) {
  4244. std::unique_lock lock(MIgnoredWidgets);
  4245. if(ignored_widgets.find(id) == ignored_widgets.end()) {
  4246. ignored_widgets.insert(make_pair(id,true));
  4247. }
  4248. }
  4249. void Spawn::SendGroupUpdate() {
  4250. if (IsEntity() && ((Entity*)this)->GetGroupMemberInfo()) {
  4251. ((Entity*)this)->UpdateGroupMemberInfo();
  4252. if (IsPlayer()) {
  4253. Client* client = ((Player*)this)->GetClient();
  4254. if(client) {
  4255. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id, client);
  4256. }
  4257. }
  4258. else
  4259. world.GetGroupManager()->SendGroupUpdate(((Entity*)this)->GetGroupMemberInfo()->group_id);
  4260. }
  4261. }
  4262. bool Spawn::AddNeedGreedItemRequest(int32 item_id, int32 spawn_id, bool need_item) {
  4263. LogWrite(LOOT__INFO, 0, "Loot", "%s: AddNeedGreedItemRequest Item ID: %u, Spawn ID: %u, Need Item: %u", GetName(), item_id, spawn_id, need_item);
  4264. if (HasSpawnNeedGreedEntry(item_id, spawn_id)) {
  4265. return false;
  4266. }
  4267. need_greed_items.insert(make_pair(item_id, std::make_pair(spawn_id, need_item)));
  4268. AddSpawnLootWindowCompleted(spawn_id, false);
  4269. return true;
  4270. }
  4271. bool Spawn::AddLottoItemRequest(int32 item_id, int32 spawn_id) {
  4272. LogWrite(LOOT__INFO, 0, "Loot", "%s: AddLottoItemRequest Item ID: %u, Spawn ID: %u", GetName(), item_id, spawn_id);
  4273. if (HasSpawnLottoEntry(item_id, spawn_id)) {
  4274. return false;
  4275. }
  4276. lotto_items.insert(make_pair(item_id, spawn_id));
  4277. AddSpawnLootWindowCompleted(spawn_id, false);
  4278. return true;
  4279. }
  4280. void Spawn::AddSpawnLootWindowCompleted(int32 spawn_id, bool status_) {
  4281. if (loot_complete.find(spawn_id) == loot_complete.end()) {
  4282. loot_complete.insert(make_pair(spawn_id, status_));
  4283. }
  4284. is_loot_complete = HasLootWindowCompleted();
  4285. }
  4286. bool Spawn::SetSpawnLootWindowCompleted(int32 spawn_id) {
  4287. std::map<int32, bool>::iterator itr = loot_complete.find(spawn_id);
  4288. if (itr != loot_complete.end()) {
  4289. itr->second = true;
  4290. is_loot_complete = HasLootWindowCompleted();
  4291. return true;
  4292. }
  4293. return false;
  4294. }
  4295. bool Spawn::HasSpawnLootWindowCompleted(int32 spawn_id) {
  4296. std::map<int32, bool>::iterator itr = loot_complete.find(spawn_id);
  4297. if (itr != loot_complete.end() && itr->second) {
  4298. return true;
  4299. }
  4300. return false;
  4301. }
  4302. bool Spawn::HasSpawnNeedGreedEntry(int32 item_id, int32 spawn_id) {
  4303. for (auto [itr, rangeEnd] = need_greed_items.equal_range(item_id); itr != rangeEnd; itr++) {
  4304. LogWrite(LOOT__DEBUG, 8, "Loot", "%s: HasSpawnNeedGreedEntry Item ID: %u, Spawn ID: %u", GetName(), itr->first, itr->second.first);
  4305. if (spawn_id == itr->second.first) {
  4306. return true;
  4307. }
  4308. }
  4309. return false;
  4310. }
  4311. bool Spawn::HasSpawnLottoEntry(int32 item_id, int32 spawn_id) {
  4312. for (auto [itr, rangeEnd] = lotto_items.equal_range(item_id); itr != rangeEnd; itr++) {
  4313. LogWrite(LOOT__DEBUG, 8, "Loot", "%s: HasSpawnLottoEntry Item ID: %u, Spawn ID: %u", GetName(), itr->first, itr->second);
  4314. if (spawn_id == itr->second) {
  4315. return true;
  4316. }
  4317. }
  4318. return false;
  4319. }
  4320. void Spawn::GetSpawnLottoEntries(int32 item_id, std::map<int32, int32>* out_entries) {
  4321. if (!out_entries)
  4322. return;
  4323. std::map<int32, bool> spawn_matches;
  4324. for (auto [itr, endrange] = lotto_items.equal_range(item_id); itr != endrange; itr++) {
  4325. out_entries->insert(std::make_pair(itr->second, (int32)MakeRandomInt(0, 100)));
  4326. spawn_matches[itr->second] = true;
  4327. }
  4328. // 0xFFFFFFFF represents selecting "All" on the lotto screen
  4329. for (auto [itr, endrange] = lotto_items.equal_range(0xFFFFFFFF); itr != endrange; itr++) {
  4330. if (spawn_matches.find(itr->second) == spawn_matches.end()) {
  4331. out_entries->insert(std::make_pair(itr->second, (int32)MakeRandomInt(0, 100)));
  4332. }
  4333. }
  4334. }
  4335. void Spawn::GetSpawnNeedGreedEntries(int32 item_id, bool need_item, std::map<int32, int32>* out_entries) {
  4336. if (!out_entries)
  4337. return;
  4338. for (auto [itr, rangeEnd] = need_greed_items.equal_range(item_id); itr != rangeEnd; itr++) {
  4339. out_entries->insert(std::make_pair(itr->second.first, (int32)MakeRandomInt(0, 100)));
  4340. }
  4341. }
  4342. bool Spawn::HasLootWindowCompleted() {
  4343. std::map<int32, bool>::iterator itr;
  4344. for (itr = loot_complete.begin(); itr != loot_complete.end(); itr++) {
  4345. if (!itr->second)
  4346. return false;
  4347. }
  4348. return true;
  4349. }
  4350. void Spawn::StartLootTimer(Spawn* looter) {
  4351. if (!IsLootTimerRunning()) {
  4352. int32 loot_timer_time = rule_manager.GetGlobalRule(R_Loot, LootDistributionTime)->GetInt32() * 1000;
  4353. if(rule_manager.GetGlobalRule(R_Loot, AllowChestUnlockByDropTime)->GetBool() && loot_timer_time > rule_manager.GetGlobalRule(R_Loot, ChestUnlockedTimeDrop)->GetInt32()*1000) {
  4354. loot_timer_time = (rule_manager.GetGlobalRule(R_Loot, ChestUnlockedTimeDrop)->GetInt32()*1000) / 2;
  4355. }
  4356. if(rule_manager.GetGlobalRule(R_Loot, AllowChestUnlockByTrapTime)->GetBool() && loot_timer_time > rule_manager.GetGlobalRule(R_Loot, ChestUnlockedTimeTrap)->GetInt32()*1000) {
  4357. loot_timer_time = (rule_manager.GetGlobalRule(R_Loot, ChestUnlockedTimeTrap)->GetInt32()*1000) / 2;
  4358. }
  4359. if(loot_timer_time < 1000) {
  4360. loot_timer_time = 60000; // hardcode assure they aren't setting some really ridiculous low number
  4361. }
  4362. loot_timer.Start(loot_timer_time, true);
  4363. }
  4364. if (looter) {
  4365. looter_spawn_id = looter->GetID();
  4366. }
  4367. }
  4368. void Spawn::CloseLoot(Spawn* sender) {
  4369. if (sender) {
  4370. SetSpawnLootWindowCompleted(sender->GetID());
  4371. }
  4372. if (sender && looter_spawn_id > 0 && sender->GetID() != looter_spawn_id) {
  4373. LogWrite(LOOT__ERROR, 0, "Loot", "%s: CloseLoot Looter Spawn ID: %u does not match sender %u.", GetName(), looter_spawn_id, sender->GetID());
  4374. return;
  4375. }
  4376. if (!IsLootTimerRunning() && GetLootMethod() != GroupLootMethod::METHOD_LOTTO && GetLootMethod() != GroupLootMethod::METHOD_NEED_BEFORE_GREED) {
  4377. loot_timer.Disable();
  4378. }
  4379. looter_spawn_id = 0;
  4380. }
  4381. void Spawn::SetLootMethod(GroupLootMethod method, int8 item_rarity, int32 group_id) {
  4382. LogWrite(LOOT__INFO, 0, "Loot", "%s: Set Loot Method : %u, group id : %u", GetName(), (int32)method, group_id);
  4383. loot_group_id = group_id;
  4384. loot_method = method;
  4385. loot_rarity = item_rarity;
  4386. if (loot_name.size() < 1) {
  4387. loot_name = std::string(GetName());
  4388. }
  4389. }
  4390. bool Spawn::IsItemInLootTier(Item* item) {
  4391. if (!item)
  4392. return true;
  4393. bool skipItem = true;
  4394. switch (GetLootRarity()) {
  4395. case LootTier::ITEMS_TREASURED_PLUS: {
  4396. if (item->details.tier >= ITEM_TAG_TREASURED) {
  4397. skipItem = false;
  4398. }
  4399. break;
  4400. }
  4401. case LootTier::ITEMS_LEGENDARY_PLUS: {
  4402. if (item->details.tier >= ITEM_TAG_LEGENDARY) {
  4403. skipItem = false;
  4404. }
  4405. break;
  4406. }
  4407. case LootTier::ITEMS_FABLED_PLUS: {
  4408. if (item->details.tier >= ITEM_TAG_FABLED) {
  4409. skipItem = false;
  4410. }
  4411. break;
  4412. }
  4413. default: {
  4414. skipItem = false;
  4415. break;
  4416. }
  4417. }
  4418. return skipItem;
  4419. }
  4420. void Spawn::DistributeGroupLoot_RoundRobin(std::vector<int32>* item_list, bool roundRobinTrashLoot) {
  4421. std::vector<int32>::iterator item_itr;
  4422. for (item_itr = item_list->begin(); item_itr != item_list->end(); item_itr++) {
  4423. int32 item_id = *item_itr;
  4424. Item* tmpItem = master_item_list.GetItem(item_id);
  4425. Spawn* looter = nullptr;
  4426. bool skipItem = IsItemInLootTier(tmpItem);
  4427. if ((skipItem && !roundRobinTrashLoot) || (!skipItem && roundRobinTrashLoot))
  4428. continue;
  4429. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  4430. PlayerGroup* group = world.GetGroupManager()->GetGroup(GetLootGroupID());
  4431. if (group) {
  4432. group->MGroupMembers.writelock(__FUNCTION__, __LINE__);
  4433. deque<GroupMemberInfo*>* members = group->GetMembers();
  4434. int8 index = group->GetLastLooterIndex();
  4435. if (index >= members->size()) {
  4436. index = 0;
  4437. }
  4438. GroupMemberInfo* gmi = members->at(index);
  4439. if (gmi) {
  4440. looter = gmi->member;
  4441. }
  4442. bool loopAttempted = false;
  4443. while (looter) {
  4444. if (!looter->IsPlayer()) {
  4445. index++;
  4446. if (index >= members->size()) {
  4447. if (loopAttempted) {
  4448. looter = nullptr;
  4449. break;
  4450. }
  4451. loopAttempted = true;
  4452. index = 0;
  4453. }
  4454. gmi = members->at(index);
  4455. if (gmi) {
  4456. looter = gmi->member;
  4457. }
  4458. continue;
  4459. }
  4460. else {
  4461. break;
  4462. }
  4463. }
  4464. index += 1;
  4465. group->SetNextLooterIndex(index);
  4466. group->MGroupMembers.releasewritelock(__FUNCTION__, __LINE__);
  4467. }
  4468. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  4469. if (looter) {
  4470. if (looter->IsPlayer()) {
  4471. Item* item = LootItem(item_id);
  4472. bool success = false;
  4473. success = ((Player*)looter)->GetClient()->HandleLootItem(this, item, ((Player*)looter), roundRobinTrashLoot);
  4474. if (!success)
  4475. AddLootItem(item);
  4476. }
  4477. else {
  4478. Item* item = LootItem(item_id);
  4479. safe_delete(item);
  4480. }
  4481. }
  4482. }
  4483. }
  4484. const double g = 9.81; // acceleration due to gravity (m/s^2)
  4485. void Spawn::CalculateInitialVelocity(float heading, float distanceHorizontal, float distanceVertical, float distanceDepth, float duration) {
  4486. float vx = distanceHorizontal / duration;
  4487. float vy = (distanceVertical + 0.5 * g * duration * duration) / duration;
  4488. float vz = distanceDepth / duration;
  4489. // Convert heading angle to radians
  4490. knocked_velocity.x = vx * cos(heading);
  4491. knocked_velocity.y = vy;
  4492. knocked_velocity.z = vz * sin(heading);
  4493. }
  4494. // Function to calculate the projectile position at a given time
  4495. glm::vec3 Spawn::CalculateProjectilePosition(glm::vec3 initialVelocity, float time) {
  4496. glm::vec3 position;
  4497. position.x = knocked_back_start_x + initialVelocity.x * time;
  4498. position.y = knocked_back_start_y + initialVelocity.y * time - 0.5 * g * time * time;
  4499. position.z = knocked_back_start_z + initialVelocity.z * time;
  4500. auto loc = glm::vec3(position.x, position.z, position.y);
  4501. float new_z = FindBestZ(loc, nullptr);
  4502. if(new_z > position.y)
  4503. position.y = new_z;
  4504. return position;
  4505. }
  4506. bool Spawn::CalculateSpawnProjectilePosition(float x, float y, float z) {
  4507. float currentTimeOffset = (Timer::GetCurrentTime2() - last_movement_update) * 0.001; // * 0.001 is the same as / 1000, float muliplications is suppose to be faster though
  4508. float stepAheadOne = currentTimeOffset+currentTimeOffset;
  4509. knocked_back_time_step += currentTimeOffset;
  4510. if(Timer::GetCurrentTime2() >= knocked_back_end_time) {
  4511. ResetKnockedBack();
  4512. FixZ(true);
  4513. return false;
  4514. }
  4515. glm::vec3 position = CalculateProjectilePosition(knocked_velocity, knocked_back_time_step);
  4516. glm::vec3 position_two = position;
  4517. if(Timer::GetCurrentTime2() <= knocked_back_end_time+stepAheadOne) {
  4518. position_two = CalculateProjectilePosition(knocked_velocity, knocked_back_time_step+stepAheadOne);
  4519. }
  4520. if(GetMap()) {
  4521. glm::vec3 loc(GetX(), GetZ(), GetY() + .5f);
  4522. glm::vec3 dest_loc(position_two.x, position_two.z, position_two.y);
  4523. MIgnoredWidgets.lock_shared();
  4524. glm::vec3 outNorm;
  4525. float dist = 0.0f;
  4526. bool collide_ = GetMap()->DoCollisionCheck(loc, dest_loc, &ignored_widgets, outNorm, dist);
  4527. if(collide_) {
  4528. 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);
  4529. MIgnoredWidgets.unlock_shared();
  4530. ResetKnockedBack();
  4531. FixZ(true);
  4532. return false;
  4533. }
  4534. MIgnoredWidgets.unlock_shared();
  4535. }
  4536. 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);
  4537. SetX(position.x, false);
  4538. SetZ(position.z, false);
  4539. SetY(position.y, false, true);
  4540. SetPos(&appearance.pos.X2, position_two.x, false);
  4541. SetPos(&appearance.pos.Z2, position_two.z, false);
  4542. SetPos(&appearance.pos.Y2, position_two.y, false);
  4543. SetPos(&appearance.pos.X3, position_two.x, false);
  4544. SetPos(&appearance.pos.Z3, position_two.z, false);
  4545. SetPos(&appearance.pos.Y3, position_two.y, false);
  4546. position_changed = true;
  4547. changed = true;
  4548. GetZone()->AddChangedSpawn(this);
  4549. return true;
  4550. }
  4551. void Spawn::SetKnockback(Spawn* target, int32 duration, float vertical, float horizontal) {
  4552. if(knocked_back) {
  4553. return; // already being knocked back
  4554. }
  4555. // Calculate the direction vector from source to destination
  4556. glm::vec3 direction = {GetX() - target->GetX(), GetZ() - target->GetZ(), GetY() - target->GetY()};
  4557. // Calculate the heading angle in radians
  4558. double headingRad = atan2(direction.y, sqrt(direction.x * direction.x + direction.z * direction.z));
  4559. knocked_angle = headingRad;
  4560. knocked_back_start_x = GetX();
  4561. knocked_back_start_y = GetY();
  4562. knocked_back_start_z = GetZ();
  4563. knocked_back_h_distance = horizontal / 10.0f;
  4564. knocked_back_v_distance = vertical / 10.0f;
  4565. knocked_back_duration = static_cast<float>(duration) / 1000.0f;
  4566. knocked_back_end_time = Timer::GetCurrentTime2() + duration;
  4567. CalculateInitialVelocity(knocked_angle, knocked_back_h_distance, knocked_back_h_distance, knocked_back_h_distance, knocked_back_duration);
  4568. knocked_back = true;
  4569. }
  4570. void Spawn::ResetKnockedBack() {
  4571. knocked_back = false;
  4572. knocked_back_time_step = 0.0f;
  4573. knocked_back_h_distance = 0.0f;
  4574. knocked_back_v_distance = 0.0f;
  4575. knocked_back_duration = 0.0f;
  4576. knocked_back_end_time = 0;
  4577. knocked_back_start_x = 0.0f;
  4578. knocked_back_start_y = 0.0f;
  4579. knocked_back_start_z = 0.0f;
  4580. knocked_angle = 0.0f;
  4581. knocked_velocity.x = 0.0f;
  4582. knocked_velocity.y = 0.0f;
  4583. knocked_velocity.z = 0.0f;
  4584. }