zoneserver.cpp 317 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237
  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 "../common/debug.h"
  17. #include <iostream>
  18. using namespace std;
  19. #include <string.h>
  20. #include "../common/misc.h"
  21. #include <stdio.h>
  22. #include <time.h>
  23. #include <stdlib.h>
  24. #include <regex>
  25. #include <unordered_set>
  26. #include "Commands/Commands.h"
  27. #include "Zone/pathfinder_interface.h"
  28. #include "NPC_AI.h"
  29. #ifdef WIN32
  30. #include <WinSock2.h>
  31. #include <windows.h>
  32. #include <dbghelp.h>
  33. #pragma comment(lib,"imagehlp.lib")
  34. #else
  35. #include <sys/socket.h>
  36. #include <sys/stat.h>
  37. #ifdef FREEBSD //Timothy Whitman - January 7, 2003
  38. #include <sys/types.h>
  39. #endif
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <errno.h>
  43. #include <pthread.h>
  44. #include <stdarg.h>
  45. #include "../common/unix.h"
  46. #define SOCKET_ERROR -1
  47. #define INVALID_SOCKET -1
  48. extern int errno;
  49. #endif
  50. #include "../common/servertalk.h"
  51. #include "../common/packet_dump.h"
  52. #include "WorldDatabase.h"
  53. #include "races.h"
  54. #include "classes.h"
  55. #include "../common/seperator.h"
  56. #include "../common/EQStream.h"
  57. #include "../common/EQStreamFactory.h"
  58. #include "../common/opcodemgr.h"
  59. #include "zoneserver.h"
  60. #include "client.h"
  61. #include "LoginServer.h"
  62. #include "World.h"
  63. #include <string>
  64. #include <assert.h>
  65. #include "LuaInterface.h"
  66. #include "Factions.h"
  67. #include "VisualStates.h"
  68. #include "ClientPacketFunctions.h"
  69. #include "SpellProcess.h"
  70. #include "../common/Log.h"
  71. #include "Rules/Rules.h"
  72. #include "Chat/Chat.h"
  73. #include "Tradeskills/Tradeskills.h"
  74. #include "RaceTypes/RaceTypes.h"
  75. #include <algorithm>
  76. #include <random>
  77. #include "Bots/Bot.h"
  78. #ifdef WIN32
  79. #define strncasecmp _strnicmp
  80. #define strcasecmp _stricmp
  81. #endif
  82. // int32 numplayers = 0; // never used?
  83. // extern bool GetZoneLongName(char* short_name, char** long_name); // never used?
  84. // extern bool holdzones; // never used?
  85. // extern volatile bool RunLoops; // never used in the zone server?
  86. // extern Classes classes; // never used in the zone server?
  87. #define NO_CATCH 1
  88. extern WorldDatabase database;
  89. extern sint32 numzones;
  90. extern ClientList client_list;
  91. extern LoginServer loginserver;
  92. extern ZoneList zone_list;
  93. extern World world;
  94. extern ConfigReader configReader;
  95. extern Commands commands;
  96. extern LuaInterface* lua_interface;
  97. extern MasterFactionList master_faction_list;
  98. extern VisualStates visual_states;
  99. extern RuleManager rule_manager;
  100. extern Chat chat;
  101. extern MasterRaceTypeList race_types_list;
  102. extern MasterSpellList master_spell_list; // temp - remove later
  103. extern MasterSkillList master_skill_list;
  104. int32 MinInstanceID = 1000;
  105. // JA: Moved most default values to Rules and risky initializers to ZoneServer::Init() - 2012.12.07
  106. ZoneServer::ZoneServer(const char* name) {
  107. incoming_clients = 0;
  108. default_zone_map = nullptr;
  109. MIncomingClients.SetName("ZoneServer::MIncomingClients");
  110. depop_zone = false;
  111. repop_zone = false;
  112. respawns_allowed = true;
  113. instanceID = 0;
  114. strcpy(zone_name, name);
  115. zoneID = 0;
  116. rain = 0;
  117. cityzone = false;
  118. always_loaded = false;
  119. locked = false; // JA: implementing /zone lock|unlock commands
  120. pNumPlayers = 0;
  121. LoadingData = true;
  122. zoneShuttingDown = false;
  123. ++numzones;
  124. revive_points = 0;
  125. zone_motd = "";
  126. finished_depop = true;
  127. initial_spawn_threads_active = 0;
  128. minimumStatus = 0;
  129. minimumLevel = 0;
  130. maximumLevel = 0;
  131. minimumVersion = 0;
  132. weather_current_severity = 0;
  133. weather_signaled = false;
  134. xp_mod = 0;
  135. isDusk = false;
  136. dusk_hour = 0;
  137. dusk_minute = 0;
  138. dawn_hour = 0;
  139. dawn_minute = 0;
  140. reloading_spellprocess = false;
  141. expansion_flag = 0;
  142. holiday_flag = 0;
  143. can_bind = 1;
  144. can_gate = 1;
  145. MMasterZoneLock = new CriticalSection(MUTEX_ATTRIBUTE_RECURSIVE);
  146. pathing = nullptr;
  147. strcpy(zonesky_file,"");
  148. reloading = true;
  149. spawnthread_active = false;
  150. movementMgr = nullptr;
  151. spellProcess = nullptr;
  152. tradeskillMgr = nullptr;
  153. watchdogTimestamp = Timer::GetCurrentTime2();
  154. MPendingSpawnRemoval.SetName("ZoneServer::MPendingSpawnRemoval");
  155. lifetime_client_count = 0;
  156. is_initialized = false;
  157. }
  158. typedef map <int32, bool> ChangedSpawnMapType;
  159. ZoneServer::~ZoneServer() {
  160. zoneShuttingDown = true; //ensure other threads shut down too
  161. //allow other threads to properly shut down
  162. if(is_initialized) {
  163. LogWrite(ZONE__INFO, 0, "Zone", "Initiating zone shutdown of '%s'", zone_name);
  164. }
  165. int32 disp_count = 0;
  166. int32 next_disp_count = 100;
  167. while (spawnthread_active || initial_spawn_threads_active > 0){
  168. bool disp = false;
  169. if ( disp_count == 0 ) {
  170. disp = true;
  171. }
  172. else if(disp_count >= next_disp_count) {
  173. disp_count = 0;
  174. disp = true;
  175. }
  176. disp_count++;
  177. if (spawnthread_active && disp)
  178. LogWrite(ZONE__DEBUG, 7, "Zone", "Zone shutdown waiting on spawn thread");
  179. if (initial_spawn_threads_active > 0 && disp)
  180. LogWrite(ZONE__DEBUG, 7, "Zone", "Zone shutdown waiting on initial spawn thread");
  181. Sleep(10);
  182. }
  183. MChangedSpawns.lock();
  184. changed_spawns.clear();
  185. MChangedSpawns.unlock();
  186. transport_spawns.clear();
  187. safe_delete(tradeskillMgr);
  188. MMasterZoneLock->lock();
  189. MMasterSpawnLock.writelock(__FUNCTION__, __LINE__);
  190. DeleteData(true);
  191. RemoveLocationProximities();
  192. RemoveLocationGrids();
  193. DeleteSpawns(true);
  194. DeleteGlobalSpawns();
  195. DeleteFlightPaths();
  196. MMasterSpawnLock.releasewritelock(__FUNCTION__, __LINE__);
  197. MMasterZoneLock->unlock();
  198. world.UpdateServerStatistic(STAT_SERVER_NUM_ACTIVE_ZONES, -1);
  199. // If lockout, public, tradeskill, or quest instance delete from db when zone shuts down
  200. if (InstanceType == SOLO_LOCKOUT_INSTANCE || InstanceType == GROUP_LOCKOUT_INSTANCE || InstanceType == RAID_LOCKOUT_INSTANCE ||
  201. InstanceType == PUBLIC_INSTANCE || InstanceType == TRADESKILL_INSTANCE || InstanceType == QUEST_INSTANCE) {
  202. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Non persistent instance shutting down, deleting instance");
  203. database.DeleteInstance(instanceID);
  204. }
  205. if (pathing != nullptr)
  206. delete pathing;
  207. if (movementMgr != nullptr)
  208. delete movementMgr;
  209. // moved to the bottom as we want spawns deleted first, this used to be above Spawn deletion which is a big no no
  210. safe_delete(spellProcess);
  211. MGridMaps.lock();
  212. std::map<int32, GridMap*>::iterator grids;
  213. for(grids = grid_maps.begin(); grids != grid_maps.end(); grids++) {
  214. GridMap* gm = grids->second;
  215. safe_delete(gm);
  216. }
  217. grid_maps.clear();
  218. MGridMaps.unlock();
  219. if(is_initialized) {
  220. LogWrite(ZONE__INFO, 0, "Zone", "Completed zone shutdown of '%s'", zone_name);
  221. }
  222. --numzones;
  223. UpdateWindowTitle(0);
  224. zone_list.Remove(this);
  225. zone_list.RemoveClientZoneReference(this);
  226. safe_delete(MMasterZoneLock);
  227. }
  228. void ZoneServer::IncrementIncomingClients() {
  229. MIncomingClients.writelock(__FUNCTION__, __LINE__);
  230. incoming_clients++;
  231. LogWrite(ZONE__INFO, 0, "Zone", "Increment incoming clients of '%s' zoneid %u (instance id: %u). Current incoming client count: %u", zone_name, zoneID, instanceID, incoming_clients);
  232. MIncomingClients.releasewritelock(__FUNCTION__, __LINE__);
  233. }
  234. void ZoneServer::DecrementIncomingClients() {
  235. MIncomingClients.writelock(__FUNCTION__, __LINE__);
  236. bool zeroed = false;
  237. if(incoming_clients)
  238. incoming_clients--;
  239. else
  240. zeroed = true;
  241. LogWrite(ZONE__INFO, 0, "Zone", "Decrement incoming clients of '%s' zoneid %u (instance id: %u). Current incoming client count: %u (was client count previously zero: %u)", zone_name, zoneID, instanceID, incoming_clients, zeroed);
  242. MIncomingClients.releasewritelock(__FUNCTION__, __LINE__);
  243. }
  244. void ZoneServer::Init()
  245. {
  246. LogWrite(ZONE__INFO, 0, "Zone", "Loading new Zone '%s'", zone_name);
  247. zone_list.Add(this);
  248. is_initialized = true;
  249. spellProcess = new SpellProcess();
  250. tradeskillMgr = new TradeskillMgr();
  251. /* Dynamic Timers */
  252. regenTimer.Start(rule_manager.GetGlobalRule(R_Zone, RegenTimer)->GetInt32());
  253. client_save.Start(rule_manager.GetGlobalRule(R_Zone, ClientSaveTimer)->GetInt32());
  254. shutdownTimer.Disable();
  255. spawn_range.Start(rule_manager.GetGlobalRule(R_Zone, CheckAttackPlayer)->GetInt32());
  256. aggro_timer.Start(rule_manager.GetGlobalRule(R_Zone, CheckAttackNPC)->GetInt32());
  257. /* Weather stuff */
  258. InitWeather();
  259. /* Static Timers */
  260. // JA - haven't decided yet if these should remain hard-coded. Changing them could break EQ2Emu functionality
  261. spawn_check_add.Start(1000);
  262. spawn_check_remove.Start(30000);
  263. spawn_expire_timer.Start(10000);
  264. respawn_timer.Start(10000);
  265. // there was never a starter for these?
  266. widget_timer.Start(5000);
  267. tracking_timer.Start(5000);
  268. movement_timer.Start(100);
  269. location_prox_timer.Start(1000);
  270. location_grid_timer.Start(1000);
  271. charsheet_changes.Start(500);
  272. // Send game time packet every in game hour (180 sec)
  273. sync_game_time_timer.Start(180000);
  274. // Get the dusk and dawn time from the rule manager and store it in the correct variables
  275. sscanf (rule_manager.GetGlobalRule(R_World, DuskTime)->GetString(), "%d:%d", &dusk_hour, &dusk_minute);
  276. sscanf (rule_manager.GetGlobalRule(R_World, DawnTime)->GetString(), "%d:%d", &dawn_hour, &dawn_minute);
  277. spawn_update.Start(rule_manager.GetGlobalRule(R_Zone, SpawnUpdateTimer)->GetInt16());
  278. LogWrite(ZONE__DEBUG, 0, "Zone", "SpawnUpdateTimer: %ims", rule_manager.GetGlobalRule(R_Zone, SpawnUpdateTimer)->GetInt16());
  279. queue_updates.Start(rule_manager.GetGlobalRule(R_Zone, SpawnUpdateTimer)->GetInt16());
  280. LogWrite(ZONE__DEBUG, 0, "Zone", "QueueUpdateTimer(inherits SpawnUpdateTimer): %ims", rule_manager.GetGlobalRule(R_Zone, SpawnUpdateTimer)->GetInt16());
  281. spawn_delete_timer = rule_manager.GetGlobalRule(R_Zone, SpawnDeleteTimer)->GetInt32();
  282. LogWrite(ZONE__DEBUG, 0, "Zone", "SpawnDeleteTimer: %ums", spawn_delete_timer);
  283. LogWrite(ZONE__DEBUG, 0, "Zone", "Loading zone flight paths");
  284. database.LoadZoneFlightPaths(this);
  285. world.UpdateServerStatistic(STAT_SERVER_NUM_ACTIVE_ZONES, 1);
  286. UpdateWindowTitle(0);
  287. string zoneName(GetZoneFile());
  288. world.LoadRegionMaps(zoneName);
  289. world.LoadMaps(zoneName);
  290. pathing = IPathfinder::Load(zoneName);
  291. movementMgr = new MobMovementManager();
  292. if(GetInstanceID()) {
  293. PlayerHouse* ph = world.GetPlayerHouseByInstanceID(GetInstanceID());
  294. if(ph) {
  295. HouseZone* hz = world.GetHouseZone(ph->house_id);
  296. if(hz) {
  297. std::string desc = ph->player_name + "'s " + hz->name;
  298. SetZoneDescription((char*)desc.c_str());
  299. }
  300. }
  301. }
  302. MMasterSpawnLock.SetName("ZoneServer::MMasterSpawnLock");
  303. m_npc_faction_list.SetName("ZoneServer::npc_faction_list");
  304. m_enemy_faction_list.SetName("ZoneServer::enemy_faction_list");
  305. m_reverse_enemy_faction_list.SetName("ZoneServer::reverse_enemy_faction_list");
  306. MDeadSpawns.SetName("ZoneServer::dead_spawns");
  307. MTransportSpawns.SetName("ZoneServer::transport_spawns");
  308. MSpawnList.SetName("ZoneServer::spawn_list");
  309. MTransporters.SetName("ZoneServer::m_transportMaps");
  310. MSpawnGroupAssociation.SetName("ZoneServer::spawn_group_associations");
  311. MSpawnGroupLocations.SetName("ZoneServer::spawn_group_locations");
  312. MSpawnLocationGroups.SetName("ZoneServer::spawn_location_groups");
  313. MSpawnGroupChances.SetName("ZoneServer::spawn_group_chances");
  314. MTransportLocations.SetName("ZoneServer::transporter_locations");
  315. MSpawnLocationList.SetName("ZoneServer::spawn_location_list");
  316. MSpawnDeleteList.SetName("ZoneServer::spawn_delete_list");
  317. MSpawnScriptTimers.SetName("ZoneServer::spawn_script_timers");
  318. MRemoveSpawnScriptTimersList.SetName("ZoneServer::remove_spawn_script_timers_list");
  319. MClientList.SetName("ZoneServer::clients");
  320. MWidgetTimers.SetName("ZoneServer::widget_timers");
  321. #ifdef WIN32
  322. _beginthread(ZoneLoop, 0, this);
  323. _beginthread(SpawnLoop, 0, this);
  324. #else
  325. pthread_create(&ZoneThread, NULL, ZoneLoop, this);
  326. pthread_detach(ZoneThread);
  327. pthread_create(&SpawnThread, NULL, SpawnLoop, this);
  328. pthread_detach(SpawnThread);
  329. #endif
  330. }
  331. void ZoneServer::CancelThreads() {
  332. #ifdef WIN32
  333. LogWrite(WORLD__ERROR, 1, "World", "Zone %s is hung, however CancelThreads is unsupported for WIN32.", GetZoneName());
  334. #else
  335. pthread_cancel(ZoneThread);
  336. pthread_cancel(SpawnThread);
  337. #endif
  338. }
  339. void ZoneServer::InitWeather()
  340. {
  341. weather_enabled = rule_manager.GetZoneRule(GetZoneID(), R_Zone, WeatherEnabled)->GetBool();
  342. if( weather_enabled && isWeatherAllowed())
  343. {
  344. string tmp;
  345. // set up weather system when zone starts up
  346. weather_type = rule_manager.GetZoneRule(GetZoneID(), R_Zone, WeatherType)->GetInt8();
  347. switch(weather_type)
  348. {
  349. case 3: tmp = "Chaotic"; break;
  350. case 2: tmp = "Random"; break;
  351. case 1: tmp = "Dynamic"; break;
  352. default: tmp = "Normal"; break;
  353. }
  354. LogWrite(ZONE__DEBUG, 0, "Zone", "%s: Setting up '%s' weather", zone_name, tmp.c_str());
  355. weather_frequency = rule_manager.GetZoneRule(GetZoneID(), R_Zone, WeatherChangeFrequency)->GetInt32();
  356. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Change weather every %u seconds", zone_name, weather_frequency);
  357. weather_change_chance = rule_manager.GetZoneRule(GetZoneID(), R_Zone, WeatherChangeChance)->GetInt8();
  358. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Chance of weather change: %i%%", zone_name, weather_change_chance);
  359. weather_min_severity = rule_manager.GetZoneRule(GetZoneID(), R_Zone, MinWeatherSeverity)->GetFloat();
  360. weather_max_severity = rule_manager.GetZoneRule(GetZoneID(), R_Zone, MaxWeatherSeverity)->GetFloat();
  361. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather Severity min/max is %.2f - %.2f", zone_name, weather_min_severity, weather_max_severity);
  362. // Allow a random roll to determine if weather should start out severe or calm
  363. if( MakeRandomInt(1, 100) > 50)
  364. {
  365. weather_pattern = 1; // default weather to increase in severity initially
  366. weather_current_severity = weather_min_severity;
  367. }
  368. else
  369. {
  370. weather_pattern = 0; // default weather to decrease in severity initially
  371. weather_current_severity = weather_max_severity;
  372. }
  373. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather Severity set to %.2f, pattern: %i", zone_name, weather_current_severity, weather_pattern);
  374. weather_change_amount = rule_manager.GetZoneRule(GetZoneID(), R_Zone, WeatherChangePerInterval)->GetFloat();
  375. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather change by %.2f each interval", zone_name, weather_change_amount);
  376. if( weather_type > 0 )
  377. {
  378. weather_dynamic_offset = rule_manager.GetZoneRule(GetZoneID(), R_Zone, WeatherDynamicMaxOffset)->GetFloat();
  379. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather Max Offset changes no more than %.2f each interval", zone_name, weather_dynamic_offset);
  380. }
  381. else
  382. weather_dynamic_offset = 0;
  383. SetRain(weather_current_severity);
  384. weather_last_changed_time = Timer::GetUnixTimeStamp();
  385. weatherTimer.Start(rule_manager.GetZoneRule(GetZoneID(), R_Zone, WeatherTimer)->GetInt32());
  386. }
  387. }
  388. void ZoneServer::DeleteSpellProcess(){
  389. //Just get a lock to make sure we aren't already looping the spawnprocess or clientprocess if this is different than the calling thread
  390. MMasterSpawnLock.writelock(__FUNCTION__, __LINE__);
  391. MMasterZoneLock->lock();
  392. reloading_spellprocess = true;
  393. // Remove spells from NPC's
  394. Spawn* spawn = 0;
  395. map<int32, Spawn*>::iterator itr;
  396. MSpawnList.readlock(__FUNCTION__, __LINE__);
  397. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  398. spawn = itr->second;
  399. if(spawn && spawn->IsNPC())
  400. ((NPC*)spawn)->SetSpells(0);
  401. if(spawn->IsEntity())
  402. ((Entity*)spawn)->RemoveSpellBonus(nullptr, true);
  403. }
  404. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  405. MMasterZoneLock->unlock();
  406. MMasterSpawnLock.releasewritelock(__FUNCTION__, __LINE__);
  407. DismissAllPets();
  408. spellProcess->RemoveAllSpells(true);
  409. safe_delete(spellProcess);
  410. }
  411. void ZoneServer::LoadSpellProcess(){
  412. spellProcess = new SpellProcess();
  413. reloading_spellprocess = false;
  414. // Reload NPC's spells
  415. Spawn* spawn = 0;
  416. map<int32, Spawn*>::iterator itr;
  417. MSpawnList.readlock(__FUNCTION__, __LINE__);
  418. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  419. spawn = itr->second;
  420. if(spawn && spawn->IsNPC())
  421. ((NPC*)spawn)->SetSpells(world.GetNPCSpells(((NPC*)spawn)->GetPrimarySpellList(), ((NPC*)spawn)->GetSecondarySpellList()));
  422. }
  423. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  424. }
  425. void ZoneServer::LockAllSpells(Player* player) {
  426. if (player && spellProcess) {
  427. Client* client = ((Player*)player)->GetClient();
  428. if (client)
  429. spellProcess->LockAllSpells(client);
  430. }
  431. }
  432. void ZoneServer::UnlockAllSpells(Player* player) {
  433. if (player && spellProcess) {
  434. Client* client = ((Player*)player)->GetClient();
  435. if (client)
  436. spellProcess->UnlockAllSpells(client);
  437. }
  438. }
  439. void ZoneServer::DeleteFactionLists() {
  440. map<int32, vector<int32> *>::iterator faction_itr;
  441. map<int32, vector<int32> *>::iterator spawn_itr;
  442. m_enemy_faction_list.writelock(__FUNCTION__, __LINE__);
  443. for (faction_itr = enemy_faction_list.begin(); faction_itr != enemy_faction_list.end(); faction_itr++)
  444. safe_delete(faction_itr->second);
  445. enemy_faction_list.clear();
  446. m_enemy_faction_list.releasewritelock(__FUNCTION__, __LINE__);
  447. m_reverse_enemy_faction_list.writelock(__FUNCTION__, __LINE__);
  448. for (faction_itr = reverse_enemy_faction_list.begin(); faction_itr != reverse_enemy_faction_list.end(); faction_itr++)
  449. safe_delete(faction_itr->second);
  450. reverse_enemy_faction_list.clear();
  451. m_reverse_enemy_faction_list.releasewritelock(__FUNCTION__, __LINE__);
  452. m_npc_faction_list.writelock(__FUNCTION__, __LINE__);
  453. for (spawn_itr = npc_faction_list.begin(); spawn_itr != npc_faction_list.end(); spawn_itr++)
  454. safe_delete(spawn_itr->second);
  455. npc_faction_list.clear();
  456. m_npc_faction_list.releasewritelock(__FUNCTION__, __LINE__);
  457. }
  458. void ZoneServer::DeleteData(bool boot_clients){
  459. Spawn* spawn = 0;
  460. vector<Spawn*> tmp_player_list; // changed to a vector from a MutexList as this is a local variable and don't need mutex stuff for the list
  461. // Clear spawn groups
  462. spawn_group_map.clear();
  463. // Loop through the spawn list and set the spawn for deletion
  464. map<int32, Spawn*>::iterator itr;
  465. MSpawnList.readlock(__FUNCTION__, __LINE__);
  466. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  467. spawn = itr->second;
  468. if(spawn){
  469. if(!boot_clients && (spawn->IsPlayer() || spawn->IsBot()))
  470. tmp_player_list.push_back(spawn);
  471. else if(spawn->IsPlayer()){
  472. Client* client = ((Player*)spawn)->GetClient();
  473. if(client)
  474. client->Disconnect();
  475. }
  476. else{
  477. RemoveSpawnSupportFunctions(spawn, boot_clients, true);
  478. RemoveSpawnFromGrid(spawn, spawn->GetLocation());
  479. AddPendingDelete(spawn);
  480. }
  481. }
  482. }
  483. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  484. // Quick hack to prevent a deadlock, RemoveSpawnSupportFunctions() will cancel spells and result in zone->GetSpawnByID()
  485. // being called which read locks the spawn list and caused a dead lock as the above mutex's were write locked
  486. MSpawnList.writelock(__FUNCTION__, __LINE__);
  487. // Clear the spawn list, this was in the mutex above, moved it down so the above mutex could be a read lock
  488. spawn_list.clear();
  489. // Moved this up so we only read lock the list once in this list
  490. vector<Spawn*>::iterator spawn_iter2;
  491. for (spawn_iter2 = tmp_player_list.begin(); spawn_iter2 != tmp_player_list.end(); spawn_iter2++) {
  492. spawn_list[(*spawn_iter2)->GetID()] = (*spawn_iter2);
  493. }
  494. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  495. // Clear player proximities
  496. RemovePlayerProximity(0, true);
  497. spawn_range_map.clear(true);
  498. if(boot_clients) {
  499. // Refactor
  500. vector<Client*>::iterator itr;
  501. MClientList.writelock(__FUNCTION__, __LINE__);
  502. for (itr = clients.begin(); itr != clients.end(); itr++)
  503. safe_delete(*itr);
  504. clients.clear();
  505. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  506. }
  507. // Clear and delete spawn locations
  508. MSpawnLocationList.writelock(__FUNCTION__, __LINE__);
  509. map<int32, SpawnLocation*>::iterator spawn_location_iter;
  510. for (spawn_location_iter = spawn_location_list.begin(); spawn_location_iter != spawn_location_list.end(); spawn_location_iter++)
  511. safe_delete(spawn_location_iter->second);
  512. spawn_location_list.clear();
  513. MSpawnLocationList.releasewritelock(__FUNCTION__, __LINE__);
  514. // If we allow clients to stay in the zone we need to preserve the revive_points, otherwise if the player dies they will crash
  515. if(revive_points && boot_clients){
  516. vector<RevivePoint*>::iterator revive_iter;
  517. for(revive_iter=revive_points->begin(); revive_iter != revive_points->end(); revive_iter++){
  518. safe_delete(*revive_iter);
  519. }
  520. safe_delete(revive_points);
  521. }
  522. MSpawnGroupAssociation.writelock(__FUNCTION__, __LINE__);
  523. map<int32, set<int32>*>::iterator assoc_itr;
  524. for (assoc_itr = spawn_group_associations.begin(); assoc_itr != spawn_group_associations.end(); assoc_itr++)
  525. safe_delete(assoc_itr->second);
  526. spawn_group_associations.clear();
  527. MSpawnGroupAssociation.releasewritelock(__FUNCTION__, __LINE__);
  528. MSpawnGroupLocations.writelock(__FUNCTION__, __LINE__);
  529. map<int32, map<int32, int32>*>::iterator loc_itr;
  530. for (loc_itr = spawn_group_locations.begin(); loc_itr != spawn_group_locations.end(); loc_itr++)
  531. safe_delete(loc_itr->second);
  532. spawn_group_locations.clear();
  533. MSpawnGroupLocations.releasewritelock(__FUNCTION__, __LINE__);
  534. MSpawnLocationGroups.writelock(__FUNCTION__, __LINE__);
  535. map<int32, list<int32>*>::iterator group_itr;
  536. for (group_itr = spawn_location_groups.begin(); group_itr != spawn_location_groups.end(); group_itr++)
  537. safe_delete(group_itr->second);
  538. spawn_location_groups.clear();
  539. MSpawnLocationGroups.releasewritelock(__FUNCTION__, __LINE__);
  540. // Clear lists that need more then just a Clear()
  541. DeleteFactionLists();
  542. DeleteSpawnScriptTimers(0, true);
  543. DeleteSpawnScriptTimers();
  544. ClearDeadSpawns();
  545. // Clear lists
  546. movement_spawns.clear();
  547. respawn_timers.clear();
  548. transport_spawns.clear();
  549. quick_database_id_lookup.clear();
  550. MWidgetTimers.writelock(__FUNCTION__, __LINE__);
  551. widget_timers.clear();
  552. MWidgetTimers.releasewritelock(__FUNCTION__, __LINE__);
  553. map<int16, PacketStruct*>::iterator struct_itr;
  554. for (struct_itr = versioned_info_structs.begin(); struct_itr != versioned_info_structs.end(); struct_itr++)
  555. safe_delete(struct_itr->second);
  556. versioned_info_structs.clear();
  557. for (struct_itr = versioned_pos_structs.begin(); struct_itr != versioned_pos_structs.end(); struct_itr++)
  558. safe_delete(struct_itr->second);
  559. versioned_pos_structs.clear();
  560. for (struct_itr = versioned_vis_structs.begin(); struct_itr != versioned_vis_structs.end(); struct_itr++)
  561. safe_delete(struct_itr->second);
  562. versioned_vis_structs.clear();
  563. }
  564. void ZoneServer::RemoveLocationProximities() {
  565. MutexList<LocationProximity*>::iterator itr = location_proximities.begin();
  566. while(itr.Next()){
  567. safe_delete(itr->value);
  568. }
  569. location_proximities.clear();
  570. }
  571. RevivePoint* ZoneServer::GetRevivePoint(int32 id){
  572. vector<RevivePoint*>::iterator revive_iter;
  573. for(revive_iter=revive_points->begin(); revive_iter != revive_points->end(); revive_iter++){
  574. if((*revive_iter)->id == id)
  575. return *revive_iter;
  576. }
  577. return 0;
  578. }
  579. vector<RevivePoint*>* ZoneServer::GetRevivePoints(Client* client)
  580. {
  581. vector<RevivePoint*>* points = new vector<RevivePoint*>;
  582. RevivePoint* closest_point = 0;
  583. // we should not check for revive points if this is null
  584. if ( revive_points != NULL )
  585. {
  586. LogWrite(ZONE__DEBUG, 0, "Zone", "Got revive point in %s!", __FUNCTION__);
  587. float closest = 100000;
  588. float test_closest = 0;
  589. RevivePoint* test_point = 0;
  590. vector<RevivePoint*>::iterator revive_iter;
  591. for(revive_iter=revive_points->begin(); revive_iter != revive_points->end(); revive_iter++)
  592. {
  593. test_point = *revive_iter;
  594. if(test_point)
  595. {
  596. test_closest = client->GetPlayer()->GetDistance(test_point->x, test_point->y, test_point->z);
  597. // should this be changed to list all revive points within max distance or just the closest
  598. if(test_closest < closest)
  599. {
  600. LogWrite(ZONE__DEBUG, 0, "Zone", "test_closest: %.2f, closest: %.2f", test_closest, closest);
  601. closest = test_closest;
  602. closest_point = test_point;
  603. }
  604. if(test_point->always_included ) {
  605. points->push_back(test_point);
  606. if(closest_point == test_point) {
  607. closest_point = nullptr;
  608. closest = 100000;
  609. }
  610. }
  611. }
  612. }
  613. if(closest_point) {
  614. points->push_back(closest_point);
  615. }
  616. }
  617. if(closest_point && points->size() == 0 && closest_point->zone_id == GetZoneID())
  618. {
  619. LogWrite(ZONE__WARNING, 0, "Zone", "Nearest Revive Point too far away. Add another!");
  620. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "The closest revive point is quite far away, you might want to ask the server admin for a closer one.");
  621. points->push_back(closest_point);
  622. }
  623. else if(points->size() == 0)
  624. {
  625. LogWrite(ZONE__WARNING, 0, "Zone", "No Revive Points set for zoneID %u. Add some!", GetZoneID());
  626. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "There are no revive points for this zone, you might want to ask the server admin for one.");
  627. closest_point = new RevivePoint;
  628. closest_point->heading = GetSafeHeading();
  629. closest_point->id = 0xFFFFFFFF;
  630. closest_point->location_name = "Zone Safe Point";
  631. closest_point->zone_id = GetZoneID();
  632. closest_point->x = GetSafeX();
  633. closest_point->y = GetSafeY();
  634. closest_point->z = GetSafeZ();
  635. closest_point->always_included = true;
  636. points->push_back(closest_point);
  637. }
  638. return points;
  639. }
  640. void ZoneServer::TriggerCharSheetTimer(){
  641. charsheet_changes.Trigger();
  642. }
  643. void ZoneServer::RegenUpdate(){
  644. if(damaged_spawns.size(true) == 0)
  645. return;
  646. Spawn* spawn = 0;
  647. MutexList<int32>::iterator spawn_iter = damaged_spawns.begin();
  648. while(spawn_iter.Next()){
  649. spawn = GetSpawnByID(spawn_iter->value);
  650. if(spawn && (((spawn->GetHP() < spawn->GetTotalHP()) && spawn->GetHP()>0) || (spawn->GetPower() < spawn->GetTotalPower()))){
  651. if(spawn->IsEntity())
  652. ((Entity*)spawn)->DoRegenUpdate();
  653. if(spawn->IsPlayer()){
  654. Client* client = ((Player*)spawn)->GetClient();
  655. if(client && client->IsReadyForUpdates())
  656. client->QueuePacket(client->GetPlayer()->GetPlayerInfo()->serialize(client->GetVersion()));
  657. }
  658. }
  659. else
  660. RemoveDamagedSpawn(spawn);
  661. //Spawn no longer valid, remove it from the list
  662. if (!spawn)
  663. damaged_spawns.Remove(spawn_iter->value);
  664. }
  665. }
  666. void ZoneServer::ClearDeadSpawns(){
  667. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  668. dead_spawns.clear();
  669. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  670. }
  671. void ZoneServer::ProcessDepop(bool respawns_allowed, bool repop) {
  672. vector<Client*>::iterator client_itr;
  673. Client* client = 0;
  674. Spawn* spawn = 0;
  675. PacketStruct* packet = 0;
  676. int16 packet_version = 0;
  677. spawn_expire_timers.clear();
  678. MClientList.readlock(__FUNCTION__, __LINE__);
  679. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  680. client = *client_itr;
  681. if(!client)
  682. continue;
  683. client->GetPlayer()->SetTarget(0);
  684. if(repop)
  685. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Zone Repop in progress...");
  686. else{
  687. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Zone Depop in progress...");
  688. if(respawns_allowed)
  689. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Spawns will respawn according to their respawn timers.");
  690. }
  691. if(!packet || packet_version != client->GetVersion()){
  692. safe_delete(packet);
  693. packet_version = client->GetVersion();
  694. packet = configReader.getStruct("WS_DestroyGhostCmd", packet_version);
  695. }
  696. map<int32, Spawn*>::iterator itr;
  697. MSpawnList.readlock(__FUNCTION__, __LINE__);
  698. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  699. spawn = itr->second;
  700. if(spawn && !spawn->IsPlayer() && !spawn->IsBot()){
  701. bool dispatched = false;
  702. if(spawn->IsPet())
  703. {
  704. Entity* owner = ((Entity*)spawn)->GetOwner();
  705. if(owner)
  706. {
  707. owner->DismissPet((Entity*)spawn);
  708. dispatched = true;
  709. }
  710. }
  711. spawn->SetDeletedSpawn(true);
  712. if(!dispatched)
  713. SendRemoveSpawn(client, spawn, packet);
  714. }
  715. }
  716. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  717. }
  718. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  719. DeleteTransporters();
  720. safe_delete(packet);
  721. if(!repop && respawns_allowed){
  722. spawn_range_map.clear(true);
  723. MutexList<Spawn*> tmp_player_list; // Local variable, never be on another thread so probably don't need the extra mutex code that comes with a MutexList
  724. ClearDeadSpawns();
  725. map<int32, Spawn*>::iterator itr;
  726. MSpawnList.writelock(__FUNCTION__, __LINE__);
  727. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  728. spawn = itr->second;
  729. if (spawn) {
  730. if(spawn->GetRespawnTime() > 0 && spawn->GetSpawnLocationID() > 0)
  731. respawn_timers.Put(spawn->GetSpawnLocationID(), Timer::GetCurrentTime2() + spawn->GetRespawnTime()*1000);
  732. if(spawn->IsPlayer() || spawn->IsBot())
  733. tmp_player_list.Add(spawn);
  734. else {
  735. RemoveSpawnSupportFunctions(spawn, true);
  736. RemoveSpawnFromGrid(spawn, spawn->GetLocation());
  737. AddPendingDelete(spawn);
  738. }
  739. }
  740. }
  741. spawn_list.clear();
  742. //add back just the clients
  743. MutexList<Spawn*>::iterator spawn_iter2 = tmp_player_list.begin();
  744. while(spawn_iter2.Next()) {
  745. spawn_list[spawn_iter2->value->GetID()] = spawn_iter2->value;
  746. }
  747. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  748. }
  749. else {
  750. DeleteData(false);
  751. }
  752. if(repop)
  753. {
  754. // reload spirit shards for the current zone
  755. database.LoadSpiritShards(this);
  756. LoadingData = true;
  757. }
  758. }
  759. void ZoneServer::Depop(bool respawns, bool repop) {
  760. respawns_allowed = respawns;
  761. repop_zone = repop;
  762. finished_depop = false;
  763. depop_zone = true;
  764. }
  765. bool ZoneServer::AddCloseSpawnsToSpawnGroup(Spawn* spawn, float radius){
  766. if(!spawn)
  767. return false;
  768. Spawn* close_spawn = 0;
  769. bool ret = true;
  770. map<int32, Spawn*>::iterator itr;
  771. MSpawnList.readlock(__FUNCTION__, __LINE__);
  772. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  773. close_spawn = itr->second;
  774. if(close_spawn && close_spawn != spawn && !close_spawn->IsPlayer() && close_spawn->GetDistance(spawn) <= radius){
  775. if((spawn->IsNPC() && close_spawn->IsNPC()) || (spawn->IsGroundSpawn() && close_spawn->IsGroundSpawn()) || (spawn->IsObject() && close_spawn->IsObject()) || (spawn->IsWidget() && close_spawn->IsWidget()) || (spawn->IsSign() && close_spawn->IsSign())){
  776. if(close_spawn->GetSpawnGroupID() == 0){
  777. spawn->AddSpawnToGroup(close_spawn);
  778. close_spawn->AddSpawnToGroup(spawn);
  779. }
  780. else
  781. ret = false;
  782. }
  783. }
  784. }
  785. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  786. return ret;
  787. }
  788. void ZoneServer::RepopSpawns(Client* client, Spawn* in_spawn){
  789. vector<Spawn*>* spawns = in_spawn->GetSpawnGroup();
  790. PacketStruct* packet = configReader.getStruct("WS_DestroyGhostCmd", client->GetVersion());
  791. if(spawns){
  792. if(!packet)
  793. return;
  794. Spawn* spawn = 0;
  795. vector<Spawn*>::iterator itr;
  796. for(itr = spawns->begin(); itr != spawns->end(); itr++){
  797. spawn = *itr;
  798. spawn->SetDeletedSpawn(true);
  799. SendRemoveSpawn(client, spawn, packet);
  800. }
  801. }
  802. safe_delete(spawns);
  803. if(in_spawn)
  804. in_spawn->SetDeletedSpawn(true);
  805. SendRemoveSpawn(client, in_spawn, packet);
  806. spawn_check_add.Trigger();
  807. safe_delete(packet);
  808. }
  809. bool ZoneServer::AggroVictim(NPC* npc, Spawn* victim, Client* client)
  810. {
  811. bool isEntity = victim->IsEntity();
  812. if(isEntity && !npc->AttackAllowed((Entity*)victim))
  813. return false;
  814. if (npc->HasSpawnGroup()) {
  815. vector<Spawn*>* groupVec = npc->GetSpawnGroup();
  816. for (int32 i = 0; i < groupVec->size(); i++) {
  817. Spawn* group_member = groupVec->at(i);
  818. if (group_member && !group_member->EngagedInCombat() && group_member->Alive()) {
  819. CallSpawnScript(group_member, SPAWN_SCRIPT_AGGRO, victim);
  820. if (isEntity)
  821. ((NPC*)group_member)->AddHate((Entity*)victim, 50);
  822. else
  823. ((NPC*)group_member)->InCombat(true);
  824. }
  825. }
  826. safe_delete(groupVec);
  827. }
  828. else
  829. {
  830. if (isEntity)
  831. {
  832. CallSpawnScript(victim, SPAWN_SCRIPT_AGGRO, victim);
  833. npc->AddHate((Entity*)victim, 50);
  834. }
  835. else
  836. npc->InCombat(true);
  837. }
  838. victim->CheckEncounterState((Entity*)npc, true);
  839. return true;
  840. }
  841. bool ZoneServer::CheckNPCAttacks(NPC* npc, Spawn* victim, Client* client){
  842. if(!npc || !victim)
  843. return true;
  844. if (client) {
  845. int8 arrow = 0;
  846. if (client->IsReadyForUpdates() && npc->CanSeeInvis(client->GetPlayer()) && client->GetPlayer()->GetFactions()->ShouldAttack(npc->GetFactionID()) && npc->AttackAllowed((Entity*)victim, false)) {
  847. if (!npc->EngagedInCombat()) {
  848. if(client->GetPlayer()->GetArrowColor(npc->GetLevel()) != ARROW_COLOR_GRAY) {
  849. AggroVictim(npc, victim, client);
  850. }
  851. else if(npc->IsScaredByStrongPlayers() &&
  852. !client->GetPlayer()->IsSpawnInRangeList(npc->GetID())) {
  853. SendSpawnChanges(npc, client, true, true);
  854. client->GetPlayer()->SetSpawnInRangeList(npc->GetID(), true);
  855. }
  856. }
  857. }
  858. }
  859. else{
  860. AggroVictim(npc, victim, client);
  861. }
  862. return true;
  863. }
  864. bool ZoneServer::CheckEnemyList(NPC* npc) {
  865. vector<int32> *factions;
  866. vector<int32>::iterator faction_itr;
  867. vector<int32> *spawns;
  868. vector<int32>::iterator spawn_itr;
  869. map<float, Spawn*> attack_spawns;
  870. map<float, Spawn*> reverse_attack_spawns;
  871. map<float, Spawn*>::iterator itr;
  872. int32 faction_id = npc->GetFactionID();
  873. float distance;
  874. if (faction_id == 0)
  875. return true;
  876. m_enemy_faction_list.readlock(__FUNCTION__, __LINE__);
  877. if (enemy_faction_list.count(faction_id) > 0) {
  878. factions = enemy_faction_list[faction_id];
  879. for (faction_itr = factions->begin(); faction_itr != factions->end(); faction_itr++) {
  880. m_npc_faction_list.readlock(__FUNCTION__, __LINE__);
  881. if (npc_faction_list.count(*faction_itr) > 0) {
  882. spawns = npc_faction_list[*faction_itr];
  883. spawn_itr = spawns->begin();
  884. for (spawn_itr = spawns->begin(); spawn_itr != spawns->end(); spawn_itr++) {
  885. Spawn* spawn = GetSpawnByID(*spawn_itr);
  886. if (spawn) {
  887. if ((!npc->IsPrivateSpawn() || npc->AllowedAccess(spawn)) && (distance = spawn->GetDistance(npc)) <= npc->GetAggroRadius() && npc->CheckLoS(spawn))
  888. attack_spawns[distance] = spawn;
  889. }
  890. }
  891. }
  892. m_npc_faction_list.releasereadlock(__FUNCTION__, __LINE__);
  893. }
  894. }
  895. m_enemy_faction_list.releasereadlock(__FUNCTION__, __LINE__);
  896. m_reverse_enemy_faction_list.readlock(__FUNCTION__, __LINE__);
  897. if (reverse_enemy_faction_list.count(faction_id) > 0) {
  898. factions = reverse_enemy_faction_list[faction_id];
  899. for (faction_itr = factions->begin(); faction_itr != factions->end(); faction_itr++) {
  900. m_npc_faction_list.readlock(__FUNCTION__, __LINE__);
  901. if (npc_faction_list.count(*faction_itr) > 0) {
  902. spawns = npc_faction_list[*faction_itr];
  903. spawn_itr = spawns->begin();
  904. for (spawn_itr = spawns->begin(); spawn_itr != spawns->end(); spawn_itr++) {
  905. Spawn* spawn = GetSpawnByID(*spawn_itr);
  906. if (spawn) {
  907. if ((!npc->IsPrivateSpawn() || npc->AllowedAccess(spawn)) && (distance = spawn->GetDistance(npc)) <= npc->GetAggroRadius() && npc->CheckLoS(spawn))
  908. reverse_attack_spawns[distance] = spawn;
  909. }
  910. }
  911. }
  912. m_npc_faction_list.releasereadlock(__FUNCTION__, __LINE__);
  913. }
  914. }
  915. m_reverse_enemy_faction_list.releasereadlock(__FUNCTION__, __LINE__);
  916. if (attack_spawns.size() > 0) {
  917. for (itr = attack_spawns.begin(); itr != attack_spawns.end(); itr++)
  918. CheckNPCAttacks(npc, itr->second);
  919. }
  920. if (reverse_attack_spawns.size() > 0) {
  921. for (itr = reverse_attack_spawns.begin(); itr != reverse_attack_spawns.end(); itr++)
  922. CheckNPCAttacks((NPC*)itr->second, npc);
  923. }
  924. return attack_spawns.size() == 0;
  925. }
  926. void ZoneServer::RemoveDeadEnemyList(Spawn *spawn)
  927. {
  928. int32 faction_id = spawn->GetFactionID();
  929. vector<int32> *spawns;
  930. vector<int32>::iterator itr;
  931. m_npc_faction_list.writelock(__FUNCTION__, __LINE__);
  932. if (npc_faction_list.count(faction_id) > 0) {
  933. spawns = npc_faction_list[faction_id];
  934. for (itr = spawns->begin(); itr != spawns->end(); itr++) {
  935. if (*itr == spawn->GetID()) {
  936. spawns->erase(itr);
  937. break;
  938. }
  939. }
  940. }
  941. m_npc_faction_list.releasewritelock(__FUNCTION__, __LINE__);
  942. }
  943. void ZoneServer::AddEnemyList(NPC* npc){
  944. int32 faction_id = npc->GetFactionID();
  945. vector<int32> *hostile_factions;
  946. vector<int32>::iterator itr;
  947. if(faction_id <= 9)
  948. return;
  949. if(!rule_manager.GetZoneRule(GetZoneID(), R_Faction, AllowFactionBasedCombat)->GetBool()) {
  950. LogWrite(FACTION__WARNING, 0, "Faction", "Faction Combat is DISABLED via R_Faction::AllowFactionBasedCombat rule!");
  951. return;
  952. }
  953. m_npc_faction_list.readlock(__FUNCTION__, __LINE__);
  954. if (npc_faction_list.count(faction_id) == 0) {
  955. if(faction_id > 10) {
  956. if ((hostile_factions = master_faction_list.GetHostileFactions(faction_id)) != NULL) {
  957. itr = hostile_factions->begin();
  958. for (itr = hostile_factions->begin(); itr != hostile_factions->end(); itr++) {
  959. m_enemy_faction_list.writelock(__FUNCTION__, __LINE__);
  960. if (enemy_faction_list.count(faction_id) == 0)
  961. enemy_faction_list[faction_id] = new vector<int32>;
  962. enemy_faction_list[faction_id]->push_back(*itr);
  963. m_enemy_faction_list.releasewritelock(__FUNCTION__, __LINE__);
  964. m_reverse_enemy_faction_list.writelock(__FUNCTION__, __LINE__);
  965. if(reverse_enemy_faction_list.count(*itr) == 0)
  966. reverse_enemy_faction_list[*itr] = new vector<int32>;
  967. reverse_enemy_faction_list[*itr]->push_back(faction_id);
  968. m_reverse_enemy_faction_list.releasewritelock(__FUNCTION__, __LINE__);
  969. }
  970. }
  971. }
  972. /*m_enemy_faction_list.writelock(__FUNCTION__, __LINE__);
  973. if(enemy_faction_list.count(1) == 0)
  974. enemy_faction_list[1] = new vector<int32>;
  975. enemy_faction_list[1]->push_back(faction_id);
  976. m_enemy_faction_list.releasewritelock(__FUNCTION__, __LINE__);*/
  977. }
  978. m_npc_faction_list.releasereadlock(__FUNCTION__, __LINE__);
  979. m_npc_faction_list.writelock(__FUNCTION__, __LINE__);
  980. if(npc_faction_list.count(faction_id) == 0)
  981. npc_faction_list[faction_id] = new vector<int32>;
  982. npc_faction_list[faction_id]->push_back(npc->GetID());
  983. m_npc_faction_list.releasewritelock(__FUNCTION__, __LINE__);
  984. }
  985. void ZoneServer::CheckSpawnRange(Client* client, Spawn* spawn, bool initial_login){
  986. if(client && spawn && (initial_login || client->IsConnected())) {
  987. if(spawn != client->GetPlayer()) {
  988. if(spawn_range_map.count(client) == 0)
  989. spawn_range_map.Put(client, new MutexMap<int32, float >());
  990. float curDist = spawn->GetDistance(client->GetPlayer());
  991. int32 ghost_spawn_id = client->GetPlayerPOVGhostSpawnID();
  992. Spawn* otherSpawn = GetSpawnByID(ghost_spawn_id);
  993. if (!client->GetPlayer()->WasSentSpawn(spawn->GetID())
  994. && (!otherSpawn || otherSpawn->GetDistance(spawn) > SEND_SPAWN_DISTANCE) && curDist > SEND_SPAWN_DISTANCE)
  995. {
  996. return;
  997. }
  998. spawn_range_map.Get(client)->Put(spawn->GetID(), curDist);
  999. if(!initial_login && client && spawn->IsNPC() && (!spawn->IsPrivateSpawn() || spawn->AllowedAccess(client->GetPlayer()))
  1000. && curDist <= ((NPC*)spawn)->GetAggroRadius() && !client->GetPlayer()->GetInvulnerable())
  1001. CheckNPCAttacks((NPC*)spawn, client->GetPlayer(), client);
  1002. }
  1003. if(!initial_login)
  1004. CheckPlayerProximity(spawn, client);
  1005. }
  1006. }
  1007. void ZoneServer::CheckSpawnRange(Spawn* spawn){
  1008. vector<Client*>::iterator client_itr;
  1009. Client* client = 0;
  1010. MClientList.readlock(__FUNCTION__, __LINE__);
  1011. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  1012. client = *client_itr;
  1013. if(client && client->IsReadyForSpawns())
  1014. CheckSpawnRange(client, spawn);
  1015. }
  1016. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1017. }
  1018. bool ZoneServer::PrepareSpawnID(Player* player, Spawn* spawn){
  1019. return player->SetSpawnMap(spawn);
  1020. }
  1021. void ZoneServer::CheckSendSpawnToClient(Client* client, bool initial_login) {
  1022. if (!client) {
  1023. LogWrite(ZONE__ERROR, 0, "Zone", "CheckSendSpawnToClient called with an invalid client");
  1024. return;
  1025. }
  1026. if (!initial_login && !client->GetInitialSpawnsSent() || (!initial_login && !client->IsReadyForSpawns()))
  1027. return;
  1028. Spawn* spawn = 0;
  1029. map<float, vector<Spawn*>* > closest_spawns;
  1030. if (spawn_range_map.count(client) > 0) {
  1031. if (initial_login || client->IsConnected()) {
  1032. MutexMap<int32, float >::iterator spawn_iter = spawn_range_map.Get(client)->begin();
  1033. while (spawn_iter.Next()) {
  1034. spawn = GetSpawnByID(spawn_iter->first, true);
  1035. if (spawn && spawn->GetPrivateQuestSpawn()) {
  1036. if (!spawn->IsPrivateSpawn())
  1037. spawn->AddAllowAccessSpawn(spawn);
  1038. if (spawn->MeetsSpawnAccessRequirements(client->GetPlayer())) {
  1039. if (spawn->IsPrivateSpawn() && !spawn->AllowedAccess(client->GetPlayer()))
  1040. spawn->AddAllowAccessSpawn(client->GetPlayer());
  1041. }
  1042. else if (spawn->AllowedAccess(client->GetPlayer()))
  1043. spawn->RemoveSpawnAccess(client->GetPlayer());
  1044. }
  1045. if (spawn && spawn != client->GetPlayer() && client->GetPlayer()->ShouldSendSpawn(spawn)) {
  1046. if ((!initial_login && spawn_iter->second <= SEND_SPAWN_DISTANCE) || (initial_login && (spawn_iter->second <= (SEND_SPAWN_DISTANCE / 2) || spawn->IsWidget()))) {
  1047. if(PrepareSpawnID(client->GetPlayer(), spawn)) {
  1048. if (closest_spawns.count(spawn_iter->second) == 0)
  1049. closest_spawns[spawn_iter->second] = new vector<Spawn*>();
  1050. closest_spawns[spawn_iter->second]->push_back(spawn);
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. vector<Spawn*>::iterator spawn_iter2;
  1057. map<float, vector<Spawn*>* >::iterator itr;
  1058. for (itr = closest_spawns.begin(); itr != closest_spawns.end(); ) {
  1059. for (spawn_iter2 = itr->second->begin(); spawn_iter2 != itr->second->end(); spawn_iter2++) {
  1060. spawn = *spawn_iter2;
  1061. if(!client->IsReloadingZone() || (client->IsReloadingZone() && spawn != client->GetPlayer()))
  1062. SendSpawn(spawn, client);
  1063. if (client->ShouldTarget() && client->GetCombineSpawn() == spawn)
  1064. client->TargetSpawn(spawn);
  1065. }
  1066. vector<Spawn*>* vect = itr->second;
  1067. map<float, vector<Spawn*>* >::iterator tmpitr = itr;
  1068. itr++;
  1069. closest_spawns.erase(tmpitr);
  1070. safe_delete(vect);
  1071. }
  1072. }
  1073. if (initial_login)
  1074. client->SetInitialSpawnsSent(true);
  1075. }
  1076. void ZoneServer::CheckSendSpawnToClient(){
  1077. vector<Client*>::iterator itr;
  1078. Client* client = 0;
  1079. MClientList.readlock(__FUNCTION__, __LINE__);
  1080. MSpawnList.readlock(__FUNCTION__, __LINE__);
  1081. for (itr = clients.begin(); itr != clients.end(); itr++) {
  1082. client = *itr;
  1083. if(client->IsReadyForSpawns())
  1084. CheckSendSpawnToClient(client);
  1085. }
  1086. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  1087. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1088. }
  1089. void ZoneServer::CheckRemoveSpawnFromClient(Spawn* spawn) {
  1090. vector<Client*>::iterator itr;
  1091. Client* client = 0;
  1092. PacketStruct* packet = 0;
  1093. int16 packet_version = 0;
  1094. MClientList.readlock(__FUNCTION__, __LINE__);
  1095. for (itr = clients.begin(); itr != clients.end(); itr++) {
  1096. client = *itr;
  1097. if(client){
  1098. int32 ghost_spawn_id = client->GetPlayerPOVGhostSpawnID();
  1099. Spawn* otherSpawn = GetSpawnByID(ghost_spawn_id);
  1100. if(!packet || packet_version != client->GetVersion()){
  1101. safe_delete(packet);
  1102. packet_version = client->GetVersion();
  1103. packet = configReader.getStruct("WS_DestroyGhostCmd", packet_version);
  1104. }
  1105. if(spawn && spawn != client->GetPlayer() &&
  1106. client->GetPlayer()->WasSentSpawn(spawn->GetID()) &&
  1107. !client->GetPlayer()->IsRemovingSpawn(spawn->GetID()) &&
  1108. client->GetPlayer()->WasSpawnRemoved(spawn) == false &&
  1109. (ghost_spawn_id == 0 || (ghost_spawn_id != spawn->GetID() && otherSpawn && otherSpawn->GetDistance(spawn) > REMOVE_SPAWN_DISTANCE)) &&
  1110. (spawn_range_map.Get(client)->Get(spawn->GetID()) > REMOVE_SPAWN_DISTANCE &&
  1111. !spawn->IsSign() && !spawn->IsObject() && !spawn->IsWidget() && !spawn->IsTransportSpawn())){
  1112. SendRemoveSpawn(client, spawn, packet);
  1113. spawn_range_map.Get(client)->erase(spawn->GetID());
  1114. }
  1115. }
  1116. }
  1117. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1118. safe_delete(packet);
  1119. }
  1120. bool ZoneServer::CombatProcess(Spawn* spawn) {
  1121. bool ret = true;
  1122. if (spawn && spawn->IsEntity())
  1123. ((Entity*)spawn)->ProcessCombat();
  1124. if (spawn && !spawn->Alive() && !spawn->IsLootDispensed()) {
  1125. LootProcess(spawn);
  1126. }
  1127. return ret;
  1128. }
  1129. void ZoneServer::LootProcess(Spawn* spawn) {
  1130. if (spawn->GetLootMethod() == GroupLootMethod::METHOD_ROUND_ROBIN) {
  1131. spawn->LockLoot();
  1132. if (spawn->CheckLootTimer() || spawn->IsLootWindowComplete()) {
  1133. LogWrite(LOOT__INFO, 0, "Loot", "%s: Dispensing loot, loot window was completed? %s.", spawn->GetName(), spawn->IsLootWindowComplete() ? "YES" : "NO");
  1134. spawn->DisableLootTimer();
  1135. spawn->SetLootDispensed();
  1136. Spawn* looter = nullptr;
  1137. if (spawn->GetLootGroupID() < 1 && spawn->GetLootWindowList()->size() > 0) {
  1138. std::map<int32, bool>::iterator itr;
  1139. for (itr = spawn->GetLootWindowList()->begin(); itr != spawn->GetLootWindowList()->end(); itr++) {
  1140. Spawn* entry = GetSpawnByID(itr->first, true);
  1141. if (entry->IsPlayer()) {
  1142. looter = entry;
  1143. break;
  1144. }
  1145. }
  1146. int32 item_id = 0;
  1147. std::vector<int32> item_list;
  1148. spawn->GetLootItemsList(&item_list);
  1149. spawn->UnlockLoot();
  1150. std::vector<int32>::iterator item_itr;
  1151. for (item_itr = item_list.begin(); item_itr != item_list.end(); item_itr++) {
  1152. int32 item_id = *item_itr;
  1153. Item* tmpItem = master_item_list.GetItem(item_id);
  1154. bool skipItem = spawn->IsItemInLootTier(tmpItem);
  1155. if (skipItem)
  1156. continue;
  1157. if (looter) {
  1158. if (looter->IsPlayer()) {
  1159. Item* item = spawn->LootItem(item_id);
  1160. bool success = false;
  1161. success = ((Player*)looter)->GetClient()->HandleLootItem(spawn, item, ((Player*)looter));
  1162. if (!success)
  1163. spawn->AddLootItem(item);
  1164. }
  1165. else {
  1166. Item* item = spawn->LootItem(item_id);
  1167. safe_delete(item);
  1168. }
  1169. }
  1170. }
  1171. }
  1172. else if (spawn->GetLootGroupID() > 0) {
  1173. int32 item_id = 0;
  1174. std::vector<int32> item_list;
  1175. spawn->GetLootItemsList(&item_list);
  1176. spawn->UnlockLoot();
  1177. spawn->DistributeGroupLoot_RoundRobin(&item_list);
  1178. }
  1179. if (!spawn->HasLoot()) {
  1180. if (spawn->IsNPC())
  1181. RemoveDeadSpawn(spawn);
  1182. }
  1183. else {
  1184. spawn->LockLoot();
  1185. spawn->SetLootMethod(GroupLootMethod::METHOD_FFA, 0, 0);
  1186. spawn->SetLooterSpawnID(0);
  1187. spawn->UnlockLoot();
  1188. }
  1189. }
  1190. else {
  1191. spawn->UnlockLoot();
  1192. }
  1193. }
  1194. else if ((spawn->GetLootMethod() == GroupLootMethod::METHOD_LOTTO || spawn->GetLootMethod() == GroupLootMethod::METHOD_NEED_BEFORE_GREED) && spawn->IsLootTimerRunning()) {
  1195. spawn->LockLoot();
  1196. if (spawn->CheckLootTimer() || spawn->IsLootWindowComplete()) {
  1197. LogWrite(LOOT__INFO, 0, "Loot", "%s: Dispensing loot, loot window was completed? %s.", spawn->GetName(), spawn->IsLootWindowComplete() ? "YES" : "NO");
  1198. spawn->DisableLootTimer();
  1199. spawn->SetLootDispensed();
  1200. // identify any clients that still have the loot window open, close it out
  1201. CloseSpawnLootWindow(spawn);
  1202. // lotto items while we have loot items in the list
  1203. int32 item_id = 0;
  1204. std::vector<int32> item_list;
  1205. spawn->GetLootItemsList(&item_list);
  1206. spawn->UnlockLoot();
  1207. std::vector<int32>::iterator item_itr;
  1208. for (item_itr = item_list.begin(); item_itr != item_list.end(); item_itr++) {
  1209. int32 item_id = *item_itr;
  1210. Item* tmpItem = master_item_list.GetItem(item_id);
  1211. bool skipItem = spawn->IsItemInLootTier(tmpItem);
  1212. if (skipItem)
  1213. continue;
  1214. std::map<int32, int32> out_entries;
  1215. std::map<int32, int32>::iterator out_itr;
  1216. bool itemNeed = true;
  1217. switch (spawn->GetLootMethod()) {
  1218. case GroupLootMethod::METHOD_LOTTO: {
  1219. spawn->GetSpawnLottoEntries(item_id, &out_entries);
  1220. break;
  1221. }
  1222. case GroupLootMethod::METHOD_NEED_BEFORE_GREED: {
  1223. spawn->GetSpawnNeedGreedEntries(item_id, true, &out_entries);
  1224. if (out_entries.size() < 1) {
  1225. spawn->GetSpawnNeedGreedEntries(item_id, false, &out_entries);
  1226. itemNeed = false;
  1227. }
  1228. break;
  1229. }
  1230. }
  1231. if (out_entries.size() < 1) {
  1232. LogWrite(LOOT__INFO, 0, "Loot", "%s: No spawns matched for loot attempt of %s (%u), skip item.", spawn->GetName(), tmpItem ? tmpItem->name.c_str() : "Unknown", item_id);
  1233. continue;
  1234. }
  1235. Spawn* looter = nullptr;
  1236. int32 curWinValue = 0;
  1237. for (out_itr = out_entries.begin(); out_itr != out_entries.end(); out_itr++) {
  1238. Spawn* entry = GetSpawnByID(out_itr->first, true);
  1239. if ((out_itr->second > curWinValue) || looter == nullptr) {
  1240. curWinValue = out_itr->second;
  1241. looter = entry;
  1242. }
  1243. if (spawn->GetLootMethod() == GroupLootMethod::METHOD_LOTTO) {
  1244. world.GetGroupManager()->SendGroupChatMessage(spawn->GetLootGroupID(), CHANNEL_LOOT_ROLLS, "%s rolled %u on %s.", entry->GetName(), out_itr->second, tmpItem ? tmpItem->name.c_str() : "Unknown");
  1245. }
  1246. else {
  1247. world.GetGroupManager()->SendGroupChatMessage(spawn->GetLootGroupID(), CHANNEL_LOOT_ROLLS, "%s rolled %s (%u) on %s.", entry->GetName(), itemNeed ? "NEED" : "GREED", out_itr->second, tmpItem ? tmpItem->name.c_str() : "Unknown");
  1248. }
  1249. }
  1250. if (looter) {
  1251. if (looter->IsPlayer()) {
  1252. Item* item = spawn->LootItem(item_id);
  1253. bool success = false;
  1254. success = ((Player*)looter)->GetClient()->HandleLootItem(spawn, item, ((Player*)looter));
  1255. if (!success)
  1256. spawn->AddLootItem(item);
  1257. }
  1258. else {
  1259. Item* item = spawn->LootItem(item_id);
  1260. safe_delete(item);
  1261. }
  1262. }
  1263. }
  1264. if (!spawn->HasLoot()) {
  1265. if (spawn->IsNPC())
  1266. RemoveDeadSpawn(spawn);
  1267. }
  1268. else {
  1269. spawn->LockLoot();
  1270. spawn->SetLootMethod(GroupLootMethod::METHOD_FFA, 0, 0);
  1271. spawn->SetLooterSpawnID(0);
  1272. spawn->UnlockLoot();
  1273. }
  1274. }
  1275. else {
  1276. spawn->UnlockLoot();
  1277. }
  1278. }
  1279. }
  1280. void ZoneServer::CloseSpawnLootWindow(Spawn* spawn) {
  1281. if (spawn->GetLootWindowList()->size() > 0) {
  1282. std::map<int32, bool>::iterator itr;
  1283. for (itr = spawn->GetLootWindowList()->begin(); itr != spawn->GetLootWindowList()->end(); itr++) {
  1284. if (itr->second)
  1285. continue;
  1286. itr->second = true;
  1287. Spawn* looter = GetSpawnByID(itr->first, true);
  1288. if (looter && looter->IsPlayer() && ((Player*)looter)->GetClient()) {
  1289. LogWrite(LOOT__DEBUG, 0, "Loot", "%s: Close loot for player %s.", spawn->GetName(), looter->GetName());
  1290. ((Player*)looter)->GetClient()->CloseLoot(spawn->GetID());
  1291. }
  1292. }
  1293. }
  1294. }
  1295. void ZoneServer::AddPendingDelete(Spawn* spawn) {
  1296. MSpawnDeleteList.writelock(__FUNCTION__, __LINE__);
  1297. spawn->SetDeletedSpawn(true);
  1298. if (spawn_delete_list.count(spawn) == 0)
  1299. spawn_delete_list.insert(make_pair(spawn, Timer::GetCurrentTime2() + spawn_delete_timer)); //give other threads up to 30 seconds to stop using this spawn reference
  1300. MSpawnDeleteList.releasewritelock(__FUNCTION__, __LINE__);
  1301. }
  1302. void ZoneServer::DeleteSpawns(bool delete_all) {
  1303. MSpawnDeleteList.writelock(__FUNCTION__, __LINE__);
  1304. MPendingSpawnRemoval.readlock(__FUNCTION__, __LINE__);
  1305. if(spawn_delete_list.size() > 0){
  1306. map<Spawn*, int32>::iterator itr;
  1307. map<Spawn*, int32>::iterator erase_itr;
  1308. int32 current_time = Timer::GetCurrentTime2();
  1309. Spawn* spawn = 0;
  1310. for (itr = spawn_delete_list.begin(); itr != spawn_delete_list.end(); ) {
  1311. if (delete_all || current_time >= itr->second){
  1312. // we haven't removed it from the spawn list yet..
  1313. if(!delete_all && m_pendingSpawnRemove.count(itr->first->GetID()))
  1314. continue;
  1315. spawn = itr->first;
  1316. lua_interface->SetLuaUserDataStale(spawn);
  1317. if(movementMgr != nullptr) {
  1318. movementMgr->RemoveMob((Entity*)spawn);
  1319. }
  1320. // delete brain if it has one
  1321. if(spawn->IsNPC()) {
  1322. NPC* tmpNPC = (NPC*)spawn;
  1323. if(tmpNPC->Brain())
  1324. tmpNPC->SetBrain(nullptr);
  1325. }
  1326. erase_itr = itr++;
  1327. spawn_delete_list.erase(erase_itr);
  1328. MSpawnList.writelock(__FUNCTION__, __LINE__);
  1329. std::map<int32, Spawn*>::iterator sitr = spawn_list.find(spawn->GetID());
  1330. if(sitr != spawn_list.end()) {
  1331. spawn_list.erase(sitr);
  1332. }
  1333. if(spawn->IsCollector()) {
  1334. std::map<int32, Spawn*>::iterator subitr = subspawn_list[SUBSPAWN_TYPES::COLLECTOR].find(spawn->GetID());
  1335. if(subitr != subspawn_list[SUBSPAWN_TYPES::COLLECTOR].end()) {
  1336. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].erase(subitr);
  1337. }
  1338. }
  1339. if(spawn->GetPickupItemID()) {
  1340. std::map<int32, Spawn*>::iterator subitr = subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].find(spawn->GetPickupItemID());
  1341. if(subitr != subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].end() && subitr->second == spawn) {
  1342. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].erase(subitr);
  1343. }
  1344. housing_spawn_map.erase(spawn->GetID());
  1345. }
  1346. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  1347. safe_delete(spawn);
  1348. }
  1349. else
  1350. itr++;
  1351. }
  1352. }
  1353. MPendingSpawnRemoval.releasereadlock(__FUNCTION__, __LINE__);
  1354. MSpawnDeleteList.releasewritelock(__FUNCTION__, __LINE__);
  1355. }
  1356. void ZoneServer::AddDamagedSpawn(Spawn* spawn){
  1357. if (spawn)
  1358. damaged_spawns.Add(spawn->GetID());
  1359. }
  1360. void ZoneServer::RemoveDamagedSpawn(Spawn* spawn){
  1361. if (spawn)
  1362. damaged_spawns.Remove(spawn->GetID());
  1363. }
  1364. bool ZoneServer::Process()
  1365. {
  1366. MMasterZoneLock->lock(); //Changing this back to a recursive lock to fix a possible /reload spells crash with multiple zones running - Foof
  1367. SetWatchdogTime(Timer::GetCurrentTime2());
  1368. #ifndef NO_CATCH
  1369. try
  1370. {
  1371. #endif
  1372. while (zoneID == 0) { //this is loaded by world
  1373. SetWatchdogTime(Timer::GetCurrentTime2());
  1374. Sleep(10);
  1375. }
  1376. if (LoadingData) {
  1377. if (lua_interface) {
  1378. string tmpScript("ZoneScripts/");
  1379. tmpScript.append(GetZoneName());
  1380. tmpScript.append(".lua");
  1381. struct stat buffer;
  1382. bool fileExists = (stat(tmpScript.c_str(), &buffer) == 0);
  1383. if (fileExists)
  1384. lua_interface->RunZoneScript(tmpScript.c_str(), "preinit_zone_script", this);
  1385. }
  1386. if (reloading) {
  1387. LogWrite(COMMAND__DEBUG, 0, "Command", "-Loading Entity Commands...");
  1388. database.LoadEntityCommands(this);
  1389. LogWrite(NPC__INFO, 0, "NPC", "-Loading Spirit Shard data...");
  1390. database.LoadSpiritShards(this);
  1391. LogWrite(NPC__INFO, 0, "NPC", "-Load Spirit Shard data complete!");
  1392. LogWrite(NPC__INFO, 0, "NPC", "-Loading NPC data...");
  1393. database.LoadNPCs(this);
  1394. LogWrite(NPC__INFO, 0, "NPC", "-Load NPC data complete!");
  1395. LogWrite(OBJECT__INFO, 0, "Object", "-Loading Object data...");
  1396. database.LoadObjects(this);
  1397. LogWrite(OBJECT__INFO, 0, "Object", "-Load Object data complete!");
  1398. LogWrite(SIGN__INFO, 0, "Sign", "-Loading Sign data...");
  1399. database.LoadSigns(this);
  1400. LogWrite(SIGN__INFO, 0, "Sign", "-Load Sign data complete!");
  1401. LogWrite(WIDGET__INFO, 0, "Widget", "-Loading Widget data...");
  1402. database.LoadWidgets(this);
  1403. LogWrite(WIDGET__INFO, 0, "Widget", "-Load Widget data complete!");
  1404. LogWrite(GROUNDSPAWN__INFO, 0, "GSpawn", "-Loading Groundspawn data...");
  1405. database.LoadGroundSpawns(this);
  1406. database.LoadGroundSpawnEntries(this);
  1407. LogWrite(GROUNDSPAWN__INFO, 0, "GSpawn", "-Load Groundspawn data complete!");
  1408. LogWrite(PET__INFO, 0, "Pet", "-Loading Pet data...");
  1409. database.GetPetNames(this);
  1410. LogWrite(PET__INFO, 0, "Pet", "-Load Pet data complete!");
  1411. LogWrite(LOOT__INFO, 0, "Loot", "-Loading Spawn loot data...");
  1412. database.LoadLoot(this);
  1413. LogWrite(LOOT__INFO, 0, "Loot", "-Loading Spawn loot data complete!");
  1414. LogWrite(TRANSPORT__INFO, 0, "Transport", "-Loading Transporters...");
  1415. database.LoadTransporters(this);
  1416. LogWrite(TRANSPORT__INFO, 0, "Transport", "-Loading Transporters complete!");
  1417. reloading = false;
  1418. world.RemoveReloadingSubSystem("Spawns");
  1419. }
  1420. MSpawnGroupAssociation.writelock(__FUNCTION__, __LINE__);
  1421. spawn_group_associations.clear();
  1422. MSpawnGroupAssociation.releasewritelock(__FUNCTION__, __LINE__);
  1423. MSpawnGroupLocations.writelock(__FUNCTION__, __LINE__);
  1424. spawn_group_locations.clear();
  1425. MSpawnGroupLocations.releasewritelock(__FUNCTION__, __LINE__);
  1426. MSpawnLocationGroups.writelock(__FUNCTION__, __LINE__);
  1427. spawn_location_groups.clear();
  1428. MSpawnLocationGroups.releasewritelock(__FUNCTION__, __LINE__);
  1429. MSpawnGroupChances.writelock(__FUNCTION__, __LINE__);
  1430. spawn_group_chances.clear();
  1431. MSpawnGroupChances.releasewritelock(__FUNCTION__, __LINE__);
  1432. Map* zonemap = world.GetMap(std::string(GetZoneFile()),0);
  1433. while (zonemap != nullptr && zonemap->IsMapLoading())
  1434. {
  1435. SetWatchdogTime(Timer::GetCurrentTime2());
  1436. // Client loop
  1437. ClientProcess(true);
  1438. Sleep(10);
  1439. }
  1440. default_zone_map = world.GetMap(std::string(GetZoneFile()),0);
  1441. DeleteTransporters();
  1442. ReloadTransporters();
  1443. database.LoadSpawns(this);
  1444. ProcessSpawnLocations();
  1445. if (!revive_points)
  1446. revive_points = new vector<RevivePoint*>;
  1447. else {
  1448. while (!revive_points->empty()) {
  1449. safe_delete(revive_points->back());
  1450. revive_points->pop_back();
  1451. }
  1452. }
  1453. database.LoadRevivePoints(revive_points, GetZoneID());
  1454. RemoveLocationGrids();
  1455. database.LoadLocationGrids(this);
  1456. MMasterZoneLock->unlock();
  1457. while(true) {
  1458. ProcessPendingSpawns();
  1459. Sleep(20);
  1460. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  1461. int32 count = pending_spawn_list_add.size();
  1462. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  1463. if(count < 1)
  1464. break;
  1465. }
  1466. startupDelayTimer.Start(60000); // this is hard coded for 60 seconds after the zone is loaded to allow a client to at least add itself to the list before we start zone shutdown timer
  1467. MMasterZoneLock->lock();
  1468. LoadingData = false;
  1469. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  1470. if (lua_interface && zone_script) {
  1471. RemoveLocationProximities();
  1472. lua_interface->RunZoneScript(zone_script, "init_zone_script", this);
  1473. }
  1474. spawn_range.Trigger();
  1475. spawn_check_add.Trigger();
  1476. }
  1477. if (reloading_spellprocess){
  1478. MMasterZoneLock->unlock();
  1479. return !zoneShuttingDown;
  1480. }
  1481. if(shutdownTimer.Enabled() && shutdownTimer.Check() && connected_clients.size(true) == 0) {
  1482. //if(lifetime_client_count)
  1483. zoneShuttingDown = true;
  1484. /*else { // allow up to 120 seconds then timeout
  1485. LogWrite(ZONE__WARNING, 0, "Zone", "No clients have connected to zone '%s' and the shutdown timer has counted down -- will delay shutdown for 120 seconds.", GetZoneName());
  1486. shutdownTimer.Start(120000, true);
  1487. lifetime_client_count = 1;
  1488. }*/
  1489. MMasterZoneLock->unlock();
  1490. return false;
  1491. }
  1492. // client loop
  1493. if(charsheet_changes.Check()) {
  1494. SendCharSheetChanges();
  1495. }
  1496. else {
  1497. SendRaidSheetChanges();
  1498. }
  1499. // Client loop
  1500. ClientProcess(startupDelayTimer.Enabled());
  1501. if(startupDelayTimer.Check())
  1502. startupDelayTimer.Disable();
  1503. if(!reloading && spellProcess)
  1504. spellProcess->Process();
  1505. if (tradeskillMgr)
  1506. tradeskillMgr->Process();
  1507. // Client loop
  1508. if(client_save.Check())
  1509. SaveClients();
  1510. // Possibility to do a client loop
  1511. if(weather_enabled && weatherTimer.Check())
  1512. ProcessWeather();
  1513. // client related loop, move to main thread?
  1514. if(!zoneShuttingDown)
  1515. ProcessDrowning();
  1516. // client than location_proximities loop, move to main thread
  1517. if (location_prox_timer.Check() && !zoneShuttingDown)
  1518. CheckLocationProximity();
  1519. // client than location_grid loop, move to main thread
  1520. if (location_grid_timer.Check() && !zoneShuttingDown)
  1521. CheckLocationGrids();
  1522. if (sync_game_time_timer.Check() && !zoneShuttingDown)
  1523. SendTimeUpdateToAllClients();
  1524. world.MWorldTime.readlock(__FUNCTION__, __LINE__);
  1525. int hour = world.GetWorldTimeStruct()->hour;
  1526. int minute = world.GetWorldTimeStruct()->minute;
  1527. world.MWorldTime.releasereadlock(__FUNCTION__, __LINE__);
  1528. if (!isDusk && (hour >= 19 || hour < 8)) {//((hour > dusk_hour || hour < dawn_hour) || ((dusk_hour == hour && minute >= dusk_minute) || (hour == dawn_hour && minute < dawn_minute)))) {
  1529. isDusk = true;
  1530. const char* zone_script = world.GetZoneScript(GetZoneID());
  1531. if (lua_interface && zone_script)
  1532. lua_interface->RunZoneScript(zone_script, "dusk", this);
  1533. ProcessSpawnConditional(SPAWN_CONDITIONAL_NIGHT);
  1534. }
  1535. else if (isDusk && hour >= 8 && hour < 19) {//((hour > dawn_hour && hour < dusk_hour) || ((hour == dawn_hour && minute >= dawn_minute) || (hour == dusk_hour && minute < dusk_minute)))) {
  1536. isDusk = false;
  1537. const char* zone_script = world.GetZoneScript(GetZoneID());
  1538. if (lua_interface && zone_script)
  1539. lua_interface->RunZoneScript(zone_script, "dawn", this);
  1540. ProcessSpawnConditional(SPAWN_CONDITIONAL_DAY);
  1541. }
  1542. // damaged spawns loop, spawn related, move to spawn thread?
  1543. if(regenTimer.Check())
  1544. RegenUpdate();
  1545. // respawn_timers loop
  1546. if(respawn_timer.Check() && !zoneShuttingDown)
  1547. CheckRespawns();
  1548. // spawn_expire_timers loop
  1549. if (spawn_expire_timer.Check() && !zoneShuttingDown)
  1550. CheckSpawnExpireTimers();
  1551. // widget_timers loop
  1552. if(widget_timer.Check() && !zoneShuttingDown)
  1553. CheckWidgetTimers();
  1554. // spawn_script_timers loop
  1555. if(!reloading && !zoneShuttingDown)
  1556. CheckSpawnScriptTimers();
  1557. // Check to see if a dead spawn needs to be removed
  1558. CheckDeadSpawnRemoval();
  1559. #ifndef NO_CATCH
  1560. }
  1561. catch(...)
  1562. {
  1563. LogWrite(ZONE__ERROR, 0, "Zone", "Exception while running '%s'", GetZoneName());
  1564. zoneShuttingDown = true;
  1565. MMasterZoneLock->unlock();
  1566. return false;
  1567. }
  1568. #endif
  1569. MMasterZoneLock->unlock();
  1570. return (zoneShuttingDown == false);
  1571. }
  1572. bool ZoneServer::SpawnProcess(){
  1573. if(depop_zone) {
  1574. depop_zone = false;
  1575. ProcessDepop(respawns_allowed, repop_zone);
  1576. finished_depop = true;
  1577. }
  1578. MMasterSpawnLock.writelock(__FUNCTION__, __LINE__);
  1579. // If the zone is loading data or shutting down don't do anything
  1580. if(!LoadingData && !zoneShuttingDown && !reloading_spellprocess) {
  1581. // Set some bool's for timers
  1582. bool movement = movement_timer.Check();
  1583. bool spawnRange = spawn_range.Check();
  1584. bool checkRemove = spawn_check_remove.Check();
  1585. bool aggroCheck = aggro_timer.Check();
  1586. vector<int32> pending_spawn_list_remove;
  1587. // Check to see if there are any spawn id's that need to be removed from the spawn list, if so remove them all
  1588. ProcessSpawnRemovals();
  1589. map<int32, Spawn*>::iterator itr;
  1590. if (spawnRange || checkRemove)
  1591. {
  1592. // Loop through the spawn list
  1593. MSpawnList.readlock(__FUNCTION__, __LINE__);
  1594. // Loop throught the list to set up spawns to be sent to clients
  1595. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  1596. // if zone is shutting down kill the loop
  1597. if (zoneShuttingDown)
  1598. break;
  1599. Spawn* spawn = itr->second;
  1600. if (spawn) {
  1601. // Checks the range to all clients in the zone
  1602. if (spawnRange)
  1603. CheckSpawnRange(spawn);
  1604. // Checks to see if the spawn needs to be removed from a client
  1605. if (checkRemove)
  1606. CheckRemoveSpawnFromClient(spawn);
  1607. }
  1608. }
  1609. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  1610. }
  1611. // Broke the spawn loop into 2 so spawns are sent to the client faster, send the spawns to clients now then resume the spawn loop
  1612. // client loop, move to main thread?
  1613. // moved this back to the spawn thread as on the main thread during a depop, done on the spawn thread, spawns would start to pop again
  1614. // might be an issue with other functions moved from the spawn thread to the main thread?
  1615. if(spawn_check_add.Check() && !zoneShuttingDown)
  1616. CheckSendSpawnToClient();
  1617. // send spawn changes, changed_spawns loop
  1618. if (spawn_update.Check() && !zoneShuttingDown) { //check for changed spawns every {Rule:SpawnUpdateTimer} milliseconds (default: 200ms)
  1619. SendSpawnChanges();
  1620. }
  1621. if (movement || aggroCheck)
  1622. {
  1623. MSpawnList.readlock(__FUNCTION__, __LINE__);
  1624. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  1625. // Break the loop if the zone is shutting down
  1626. if (zoneShuttingDown)
  1627. break;
  1628. Spawn* spawn = itr->second;
  1629. if (spawn) {
  1630. // Process spawn movement
  1631. if (movement) {
  1632. spawn->ProcessMovement(true);
  1633. // update last_movement_update for all spawns (used for time_step)
  1634. spawn->last_movement_update = Timer::GetCurrentTime2();
  1635. if (!aggroCheck)
  1636. CombatProcess(spawn);
  1637. }
  1638. // Makes NPC's KOS to other NPC's or players
  1639. if (aggroCheck)
  1640. {
  1641. ProcessAggroChecks(spawn);
  1642. CombatProcess(spawn);
  1643. }
  1644. }
  1645. else {
  1646. // unable to get a valid spawn, lets add the id to another list to remove from the spawn list after this loop is finished
  1647. pending_spawn_list_remove.push_back(itr->first);
  1648. }
  1649. }
  1650. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  1651. }
  1652. // Check to see if there are any spawn id's that need to be removed from the spawn list, if so remove them all
  1653. if (pending_spawn_list_remove.size() > 0) {
  1654. MSpawnList.writelock(__FUNCTION__, __LINE__);
  1655. vector<int32>::iterator itr2;
  1656. for (itr2 = pending_spawn_list_remove.begin(); itr2 != pending_spawn_list_remove.end(); itr2++) {
  1657. spawn_list.erase(*itr2);
  1658. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].erase(*itr2);
  1659. std::map<int32,int32>::iterator hsmitr = housing_spawn_map.find(*itr2);
  1660. if(hsmitr != housing_spawn_map.end()) {
  1661. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].erase(hsmitr->second);
  1662. housing_spawn_map.erase(hsmitr);
  1663. }
  1664. }
  1665. pending_spawn_list_remove.clear();
  1666. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  1667. }
  1668. // Double Check to see if there are any spawn id's that need to be removed from the spawn list, if so remove them before we replace with pending spawns
  1669. // and also potentially further down when we delete the Spawn* in DeleteSpawns(false)
  1670. ProcessSpawnRemovals();
  1671. // Check to see if there are spawns waiting to be added to the spawn list, if so add them all
  1672. if (pending_spawn_list_add.size() > 0) {
  1673. ProcessPendingSpawns();
  1674. }
  1675. MSpawnList.readlock(__FUNCTION__, __LINE__);
  1676. if (movementMgr != nullptr)
  1677. movementMgr->Process();
  1678. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  1679. if(queue_updates.Check())
  1680. ProcessQueuedStateCommands();
  1681. // Do other loops for spawns
  1682. // tracking, client loop with spawn loop for each client that is tracking, change to a spawn_range_map loop instead of using the main spawn list?
  1683. //if (tracking_timer.Check())
  1684. //ProcessTracking(); // probably doesn't work as spawn loop iterator is never set
  1685. // Delete unused spawns, do this last
  1686. if(!zoneShuttingDown)
  1687. DeleteSpawns(false);
  1688. // Nothing should come after this
  1689. //LogWrite(PLAYER__ERROR, 0, "Debug", "Spawn loop time %u", Timer::GetCurrentTime2() - time);
  1690. }
  1691. MMasterSpawnLock.releasewritelock(__FUNCTION__, __LINE__);
  1692. return (zoneShuttingDown == false);
  1693. }
  1694. void ZoneServer::CheckDeadSpawnRemoval() {
  1695. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  1696. if(dead_spawns.size() > 0){
  1697. vector<Spawn*> tmp_dead_list;
  1698. int32 current_time = Timer::GetCurrentTime2();
  1699. Spawn* spawn = 0;
  1700. map<int32, int32>::iterator itr = dead_spawns.begin();
  1701. map<int32, int32>::iterator itr_delete;
  1702. while (itr != dead_spawns.end()) {
  1703. spawn = GetSpawnByID(itr->first);
  1704. if (spawn) {
  1705. if(current_time >= itr->second)
  1706. tmp_dead_list.push_back(spawn);
  1707. itr++;
  1708. }
  1709. else {
  1710. itr_delete = itr++;
  1711. dead_spawns.erase(itr_delete);
  1712. }
  1713. }
  1714. for(int i=tmp_dead_list.size()-1;i>=0;i--){
  1715. spawn = tmp_dead_list[i];
  1716. if (!spawn->IsPlayer())
  1717. {
  1718. dead_spawns.erase(spawn->GetID());
  1719. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  1720. RemoveSpawn(spawn, true, true, true, true, true);
  1721. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  1722. }
  1723. }
  1724. }
  1725. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  1726. }
  1727. void ZoneServer::CheckRespawns(){
  1728. vector<int32> tmp_respawn_list;
  1729. MutexMap<int32, int32>::iterator itr = respawn_timers.begin();
  1730. while(itr.Next()){
  1731. if(Timer::GetCurrentTime2() >= itr->second)
  1732. tmp_respawn_list.push_back(itr->first);
  1733. }
  1734. for(int i=tmp_respawn_list.size()-1;i>=0;i--){
  1735. if ( IsInstanceZone() )
  1736. {
  1737. database.DeleteInstanceSpawnRemoved(GetInstanceID(),tmp_respawn_list[i]);
  1738. }
  1739. else {
  1740. database.DeletePersistedRespawn(GetZoneID(),tmp_respawn_list[i]);
  1741. }
  1742. ProcessSpawnLocation(tmp_respawn_list[i], nullptr, nullptr, nullptr, nullptr, nullptr, true);
  1743. respawn_timers.erase(tmp_respawn_list[i]);
  1744. }
  1745. }
  1746. void ZoneServer::CheckSpawnExpireTimers() {
  1747. MutexMap<int32, int32>::iterator itr = spawn_expire_timers.begin();
  1748. while (itr.Next()) {
  1749. Spawn* spawn = GetSpawnByID(itr->first);
  1750. if (spawn) {
  1751. if (Timer::GetCurrentTime2() >= itr.second) {
  1752. spawn_expire_timers.erase(itr.first);
  1753. Despawn(spawn, spawn->GetRespawnTime());
  1754. }
  1755. }
  1756. else
  1757. spawn_expire_timers.erase(itr->first);
  1758. }
  1759. }
  1760. void ZoneServer::AddSpawnExpireTimer(Spawn* spawn, int32 expire_time, int32 expire_offset) {
  1761. if (spawn) {
  1762. int32 actual_expire_time = expire_time;
  1763. if (expire_offset > 0) {
  1764. int32 low = expire_time;
  1765. int32 high = expire_time + expire_offset;
  1766. if (expire_offset < expire_time)
  1767. low = expire_time - expire_offset;
  1768. int32 range = (high - low) + 1;
  1769. actual_expire_time = (low + (int32)((range * rand()) / (RAND_MAX + 1.0)));
  1770. }
  1771. actual_expire_time *= 1000;
  1772. spawn_expire_timers.Put(spawn->GetID(), Timer::GetCurrentTime2() + actual_expire_time);
  1773. }
  1774. }
  1775. void ZoneServer::SaveClient(Client* client){
  1776. client->Save();
  1777. }
  1778. void ZoneServer::SaveClients(){
  1779. vector<Client*>::iterator itr;
  1780. Client* client = 0;
  1781. MClientList.readlock(__FUNCTION__, __LINE__);
  1782. for (itr = clients.begin(); itr != clients.end(); itr++) {
  1783. client = *itr;
  1784. if(client->IsConnected() && client->IsReadyForUpdates()){
  1785. SaveClient(client);
  1786. }
  1787. }
  1788. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1789. }
  1790. void ZoneServer::SendSpawnVisualState(Spawn* spawn, int16 type){
  1791. if(!spawn)
  1792. return;
  1793. vector<Client*>::iterator itr;
  1794. spawn->SetTempVisualState(type);
  1795. Client* client = 0;
  1796. MClientList.readlock(__FUNCTION__, __LINE__);
  1797. for (itr = clients.begin(); itr != clients.end(); itr++) {
  1798. client = *itr;
  1799. if(client && client->GetPlayer() != spawn)
  1800. AddChangedSpawn(spawn);
  1801. }
  1802. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1803. }
  1804. void ZoneServer::SendSpawnChangesByDBID(int32 db_id, Client* client, bool override_changes, bool override_vis_changes){
  1805. Spawn* spawn = GetSpawnByDatabaseID(db_id);
  1806. if(spawn && (spawn->changed || override_changes || override_vis_changes))
  1807. SendSpawnChanges(spawn, client, override_changes, override_vis_changes);
  1808. }
  1809. void ZoneServer::SendSpawnChanges(Spawn* spawn, Client* client, bool override_changes, bool override_vis_changes){
  1810. if(client && client->IsConnected() && client->IsReadyForUpdates() && client->GetPlayer()->WasSentSpawn(spawn->GetID()) && (spawn->IsTransportSpawn() || client->GetPlayer()->GetDistance(spawn) < SEND_SPAWN_DISTANCE)){
  1811. EQ2Packet* outapp = spawn->spawn_update_packet(client->GetPlayer(), client->GetVersion(), override_changes, override_vis_changes);
  1812. if(outapp)
  1813. client->QueuePacket(outapp);
  1814. }
  1815. }
  1816. void ZoneServer::SendSpawnChanges(Spawn* spawn){
  1817. MClientList.readlock(__FUNCTION__, __LINE__);
  1818. MSpawnList.readlock();
  1819. if(spawn && spawn->changed){
  1820. if(!spawn->IsPlayer() || (spawn->IsPlayer() && (spawn->info_changed || spawn->vis_changed))){
  1821. vector<Client*>::iterator itr;
  1822. Client* client = 0;
  1823. // MClientList locked at a higher level
  1824. for (itr = clients.begin(); itr != clients.end(); itr++) {
  1825. client = *itr;
  1826. SendSpawnChanges(spawn, client);
  1827. }
  1828. }
  1829. spawn->changed = false;
  1830. spawn->info_changed = false;
  1831. if(spawn->IsPlayer() == false)
  1832. spawn->position_changed = false;
  1833. spawn->vis_changed = false;
  1834. }
  1835. MSpawnList.releasereadlock();
  1836. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1837. }
  1838. Spawn* ZoneServer::FindSpawn(Player* searcher, const char* name){
  1839. if(!searcher || !name)
  1840. return 0;
  1841. Spawn* spawn = 0;
  1842. vector<Spawn*> find_spawn_list;
  1843. vector<Spawn*>::iterator fspawn_iter;
  1844. int8 name_size = strlen(name);
  1845. map<int32, Spawn*>::iterator itr;
  1846. MSpawnList.readlock(__FUNCTION__, __LINE__);
  1847. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  1848. spawn = itr->second;
  1849. if(spawn && !strncasecmp(spawn->GetName(), name, name_size))
  1850. find_spawn_list.push_back(spawn);
  1851. }
  1852. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  1853. Spawn* closest = 0;
  1854. float distance = 0;
  1855. float test_distance = 0;
  1856. for(fspawn_iter=find_spawn_list.begin(); fspawn_iter!=find_spawn_list.end(); fspawn_iter++){
  1857. spawn = *fspawn_iter;
  1858. if(spawn && ((((test_distance = searcher->GetDistance(spawn)) < distance)) || !closest)){
  1859. distance = test_distance;
  1860. closest = spawn;
  1861. }
  1862. }
  1863. return closest;
  1864. }
  1865. void ZoneServer::AddChangedSpawn(Spawn* spawn) {
  1866. if (!spawn || (spawn->IsPlayer() && !spawn->info_changed && !spawn->vis_changed) || (spawn->IsPlayer() && ((Player*)spawn)->GetClient() && ((Player*)spawn)->GetClient()->IsReadyForUpdates() == false))
  1867. return;
  1868. MChangedSpawns.lock_shared();
  1869. ChangedSpawnMapType::iterator it = changed_spawns.find(spawn->GetID());
  1870. if (it != changed_spawns.end()) {
  1871. it->second = true;
  1872. MChangedSpawns.unlock_shared();
  1873. }
  1874. else {
  1875. MChangedSpawns.unlock_shared();
  1876. MChangedSpawns.lock();
  1877. changed_spawns.insert(make_pair(spawn->GetID(),true));
  1878. MChangedSpawns.unlock();
  1879. }
  1880. }
  1881. void ZoneServer::RemoveChangedSpawn(Spawn* spawn){
  1882. if(!spawn)
  1883. return;
  1884. MChangedSpawns.lock();
  1885. ChangedSpawnMapType::iterator it = changed_spawns.find(spawn->GetID());
  1886. if (it != changed_spawns.end()) {
  1887. it->second = false;
  1888. }
  1889. MChangedSpawns.unlock();
  1890. }
  1891. void ZoneServer::AddDrowningVictim(Player* player){
  1892. Client* client = ((Player*)player)->GetClient();
  1893. if(client && drowning_victims.count(client) == 0)
  1894. drowning_victims.Put(client, Timer::GetCurrentTime2());
  1895. }
  1896. void ZoneServer::RemoveDrowningVictim(Player* player){
  1897. Client* client = ((Player*)player)->GetClient();
  1898. if(client)
  1899. drowning_victims.erase(client);
  1900. }
  1901. Client* ZoneServer::GetDrowningVictim(Player* player){
  1902. Client* client = ((Player*)player)->GetClient();
  1903. if(client && drowning_victims.count(client) > 0)
  1904. return(client);
  1905. return 0;
  1906. }
  1907. void ZoneServer::ProcessDrowning(){
  1908. vector<Client*> dead_list;
  1909. if(drowning_victims.size(true) > 0){
  1910. sint32 damage = 0;
  1911. int32 current_time = Timer::GetCurrentTime2();
  1912. MutexMap<Client*, int32>::iterator itr = drowning_victims.begin();
  1913. while(itr.Next()){
  1914. if(current_time >= itr->second) {
  1915. Client* client = itr->first;
  1916. Player* player = client->GetPlayer();
  1917. drowning_victims.Get(client) = Timer::GetCurrentTime2() + 2000;
  1918. damage = player->GetTotalHP()/20 + player->GetInfoStruct()->get_hp_regen();
  1919. player->TakeDamage(damage);
  1920. if(!player->Alive())
  1921. dead_list.push_back(client);
  1922. player->SetCharSheetChanged(true);
  1923. SendCharSheetChanges(client);
  1924. SendDamagePacket(0, player, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_SUCCESSFUL, DAMAGE_PACKET_DAMAGE_TYPE_DROWN, damage, 0);
  1925. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You are drowning!");
  1926. }
  1927. }
  1928. }
  1929. if(dead_list.size() > 0){
  1930. vector<Client*>::iterator itr;
  1931. for(itr = dead_list.begin(); itr != dead_list.end(); itr++){
  1932. RemoveDrowningVictim((*itr)->GetPlayer());
  1933. KillSpawn(false, (*itr)->GetPlayer(), nullptr, true, 0, 0, 10); // kill blow type 10 means death by WATER! (glug glug!)
  1934. }
  1935. }
  1936. }
  1937. void ZoneServer::SendSpawnChanges(){
  1938. std::shared_lock lock(MChangedSpawns);
  1939. if (changed_spawns.size() < 1)
  1940. return;
  1941. set<Spawn*> spawns_to_send;
  1942. Spawn* spawn = 0;
  1943. int count = 0;
  1944. MSpawnList.readlock(__FUNCTION__, __LINE__);
  1945. for( ChangedSpawnMapType::iterator it = changed_spawns.begin(); it != changed_spawns.end(); ++it ) {
  1946. if(!it->second)
  1947. continue;
  1948. spawn = GetSpawnByID(it->first);
  1949. if(spawn){
  1950. spawns_to_send.insert(spawn);
  1951. count++;
  1952. }
  1953. }
  1954. vector<Client*>::iterator client_itr;
  1955. Client* client = 0;
  1956. MClientList.readlock(__FUNCTION__, __LINE__);
  1957. if(clients.size())
  1958. {
  1959. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  1960. client = *client_itr;
  1961. if(client)
  1962. client->SendSpawnChanges(spawns_to_send);
  1963. }
  1964. }
  1965. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1966. for (const auto& spawn : spawns_to_send) {
  1967. spawn->changed = false;
  1968. spawn->position_changed = false;
  1969. spawn->vis_changed = false;
  1970. spawn->info_changed = false;
  1971. }
  1972. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  1973. }
  1974. void ZoneServer::SendPlayerPositionChanges(Player* player){
  1975. if(player){
  1976. player->position_changed = false;
  1977. Client* client = 0;
  1978. vector<Client*>::iterator client_itr;
  1979. MClientList.readlock(__FUNCTION__, __LINE__);
  1980. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  1981. client = *client_itr;
  1982. if(player != client->GetPlayer() && client->GetPlayer()->WasSentSpawn(player->GetID())){
  1983. if(client->GetVersion() > 373) {
  1984. EQ2Packet* outapp = player->player_position_update_packet(client->GetPlayer(), client->GetVersion(), true);
  1985. if(outapp)
  1986. client->QueuePacket(outapp);
  1987. }
  1988. }
  1989. }
  1990. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1991. }
  1992. }
  1993. void ZoneServer::SendRaidSheetChanges(){
  1994. vector<Client*>::iterator client_itr;
  1995. MClientList.readlock(__FUNCTION__, __LINE__);
  1996. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  1997. Client* client = (Client*)(*client_itr);
  1998. if(client && client->IsConnected() && client->GetPlayer()->GetRaidSheetChanged()) {
  1999. client->GetPlayer()->SetRaidSheetChanged(false);
  2000. EQ2Packet* packet = client->GetPlayer()->GetRaidUpdatePacket(client->GetVersion());
  2001. if(packet) {
  2002. client->QueuePacket(packet);
  2003. }
  2004. }
  2005. }
  2006. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  2007. }
  2008. void ZoneServer::SendCharSheetChanges(){
  2009. vector<Client*>::iterator client_itr;
  2010. MClientList.readlock(__FUNCTION__, __LINE__);
  2011. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++)
  2012. SendCharSheetChanges(*client_itr);
  2013. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  2014. }
  2015. void ZoneServer::SendCharSheetChanges(Client* client){
  2016. if(client && client->IsConnected()){
  2017. if(client->GetPlayer()->GetCharSheetChanged()) {
  2018. client->GetPlayer()->SetCharSheetChanged(false);
  2019. ClientPacketFunctions::SendCharacterSheet(client);
  2020. }
  2021. }
  2022. }
  2023. int32 ZoneServer::CalculateSpawnGroup(SpawnLocation* spawnlocation, bool respawn)
  2024. {
  2025. int32 group = 0;
  2026. list<int32>* groups_at_location = GetSpawnGroupsByLocation(spawnlocation->placement_id);
  2027. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2028. if(groups_at_location){
  2029. list<int32>::iterator group_location_itr;
  2030. float chance = 0;
  2031. float total_chance = 0;
  2032. map<int32, float> tmp_chances;
  2033. set<int32>* associated_groups = 0;
  2034. for (group_location_itr = groups_at_location->begin(); group_location_itr != groups_at_location->end(); group_location_itr++) {
  2035. if(tmp_chances.count(*group_location_itr) > 0)
  2036. continue;
  2037. associated_groups = GetAssociatedGroups(*group_location_itr);
  2038. if(associated_groups){
  2039. set<int32>::iterator group_itr;
  2040. for (group_itr = associated_groups->begin(); group_itr != associated_groups->end(); group_itr++) {
  2041. chance = GetSpawnGroupChance(*group_itr);
  2042. if(chance > 0){
  2043. total_chance += chance;
  2044. tmp_chances[*group_itr] = chance;
  2045. }
  2046. else
  2047. tmp_chances[*group_itr] = 0;
  2048. }
  2049. }
  2050. else{ //single group, no associations
  2051. chance = GetSpawnGroupChance(*group_location_itr);
  2052. total_chance += chance;
  2053. tmp_chances[*group_location_itr] = chance;
  2054. }
  2055. }
  2056. if(tmp_chances.size() > 1){
  2057. //set the default for any chances not set
  2058. map<int32, float>::iterator itr2;
  2059. for(itr2 = tmp_chances.begin(); itr2 != tmp_chances.end(); itr2++){
  2060. if(itr2->second == 0){
  2061. total_chance += 100/tmp_chances.size();
  2062. tmp_chances[itr2->first] = (float)(100 / tmp_chances.size());
  2063. }
  2064. }
  2065. }
  2066. if(tmp_chances.size() > 1){
  2067. float roll = (float)(rand()%((int32)total_chance));
  2068. map<int32, float>::iterator itr3;
  2069. for (itr3 = tmp_chances.begin(); itr3 != tmp_chances.end(); itr3++){
  2070. if(itr3->second >= roll){
  2071. group = itr3->first;
  2072. break;
  2073. }
  2074. else
  2075. roll -= itr3->second;
  2076. }
  2077. }
  2078. else if(tmp_chances.size() == 1)
  2079. group = tmp_chances.begin()->first;
  2080. }
  2081. if(group > 0){
  2082. map<int32, int32>* locations = GetSpawnLocationsByGroup(group);
  2083. if(locations){
  2084. map<int32, int32>::iterator itr;
  2085. Spawn* spawn = 0;
  2086. Spawn* leader = 0;
  2087. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  2088. for (itr = locations->begin(); itr != locations->end(); itr++) {
  2089. if(spawn_location_list.count(itr->second) > 0){
  2090. spawn = ProcessSpawnLocation(spawn_location_list[itr->second], nullptr, nullptr, nullptr, nullptr, nullptr, respawn);
  2091. if(!leader && spawn)
  2092. leader = spawn;
  2093. if(leader)
  2094. leader->AddSpawnToGroup(spawn);
  2095. if(spawn){
  2096. //if(spawn_group_map.count(group) == 0)
  2097. // spawn_group_map.Put(group, new MutexList<Spawn*>());
  2098. MutexList<int32>* groupList = &spawn_group_map.Get(group);
  2099. groupList->Add(spawn->GetID());
  2100. spawn->SetSpawnGroupID(group);
  2101. }
  2102. }
  2103. }
  2104. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  2105. }
  2106. }
  2107. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2108. return group;
  2109. }
  2110. void ZoneServer::ProcessSpawnLocation(int32 location_id, map<int32,int32>* instNPCs, map<int32,int32>* instGroundSpawns, map<int32,int32>* instObjSpawns, map<int32,int32>* instWidgetSpawns, map<int32,int32>* instSignSpawns, bool respawn)
  2111. {
  2112. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2113. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  2114. if(spawn_location_list.count(location_id) > 0)
  2115. {
  2116. if(respawn) //see if there are any spawns still in game associated with this spawn's group, if so, dont spawn this
  2117. {
  2118. list<int32>* groups = GetSpawnGroupsByLocation(spawn_location_list[location_id]->placement_id);
  2119. if(groups)
  2120. {
  2121. set<int32>* associated_groups = 0;
  2122. bool should_spawn = true;
  2123. list<int32>::iterator itr;
  2124. for (itr = groups->begin(); itr != groups->end(); itr++) {
  2125. associated_groups = GetAssociatedGroups(*itr);
  2126. if(associated_groups)
  2127. {
  2128. set<int32>::iterator assoc_itr;
  2129. for (assoc_itr = associated_groups->begin(); assoc_itr != associated_groups->end(); assoc_itr++) {
  2130. if(spawn_group_map.count(*assoc_itr) > 0 && spawn_group_map.Get(*assoc_itr).size() > 0)
  2131. should_spawn = false;
  2132. }
  2133. }
  2134. }
  2135. if(should_spawn)
  2136. CalculateSpawnGroup(spawn_location_list[location_id]);
  2137. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2138. // need to unlock the list before we exit the function
  2139. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  2140. return;
  2141. }
  2142. }
  2143. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2144. ProcessSpawnLocation(spawn_location_list[location_id], instNPCs, instGroundSpawns, instObjSpawns, instWidgetSpawns, instSignSpawns, respawn);
  2145. }
  2146. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  2147. }
  2148. Spawn* ZoneServer::ProcessSpawnLocation(SpawnLocation* spawnlocation, map<int32,int32>* instNPCs, map<int32,int32>* instGroundSpawns, map<int32,int32>* instObjSpawns, map<int32,int32>* instWidgetSpawns, map<int32,int32>* instSignSpawns, bool respawn)
  2149. {
  2150. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2151. if(!spawnlocation)
  2152. return 0;
  2153. Spawn* spawn = 0;
  2154. float rand_number = MakeRandomFloat(0, spawnlocation->total_percentage);
  2155. for(int32 i=0;i<spawnlocation->entities.size();i++)
  2156. {
  2157. if(spawnlocation->entities[i]->spawn_percentage == 0)
  2158. continue;
  2159. int32 spawnTime = 0;
  2160. if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  2161. spawnTime = database.CheckSpawnRemoveInfo(instNPCs,spawnlocation->entities[i]->spawn_location_id);
  2162. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  2163. spawnTime = database.CheckSpawnRemoveInfo(instObjSpawns,spawnlocation->entities[i]->spawn_location_id);
  2164. if(spawnTime == 0) { // don't respawn
  2165. return nullptr;
  2166. }
  2167. else if(spawnTime > 1) { // if not 1, respawn after time
  2168. AddRespawn(spawnlocation->entities[i]->spawn_location_id, spawnTime);
  2169. return nullptr;
  2170. }
  2171. if (spawnlocation->conditional > 0) {
  2172. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_DAY) == SPAWN_CONDITIONAL_DAY && isDusk)
  2173. continue;
  2174. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_NIGHT) == SPAWN_CONDITIONAL_NIGHT && !isDusk)
  2175. continue;
  2176. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_DAY) == SPAWN_CONDITIONAL_NOT_RAINING && rain >= 0.75f)
  2177. continue;
  2178. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_DAY) == SPAWN_CONDITIONAL_RAINING && rain < 0.75f)
  2179. continue;
  2180. }
  2181. if (spawnlocation->entities[i]->spawn_percentage >= rand_number) {
  2182. if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  2183. spawn = AddNPCSpawn(spawnlocation, spawnlocation->entities[i]);
  2184. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  2185. spawn = AddGroundSpawn(spawnlocation, spawnlocation->entities[i]);
  2186. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  2187. spawn = AddObjectSpawn(spawnlocation, spawnlocation->entities[i]);
  2188. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  2189. spawn = AddWidgetSpawn(spawnlocation, spawnlocation->entities[i]);
  2190. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  2191. spawn = AddSignSpawn(spawnlocation, spawnlocation->entities[i]);
  2192. if (GetInstanceType() == PERSONAL_HOUSE_INSTANCE)
  2193. database.GetHouseSpawnInstanceData(this, spawn);
  2194. if(spawn && spawn->IsOmittedByDBFlag())
  2195. {
  2196. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) in spawn location id %u was skipped due to a missing expansion / holiday flag being met (ZoneServer::ProcessSpawnLocation)", spawnlocation->entities[i]->spawn_id, spawnlocation->entities[i]->spawn_location_id);
  2197. safe_delete(spawn);
  2198. spawn = 0;
  2199. continue;
  2200. }
  2201. else if (!spawn)
  2202. {
  2203. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn by spawn location to zone %s with location id %u, spawn id %u, spawn type %u.", GetZoneName(), spawnlocation->entities[i]->spawn_location_id, spawnlocation->entities[i]->spawn_id, spawnlocation->entities[i]->spawn_type);
  2204. continue;
  2205. }
  2206. if (spawn)
  2207. {
  2208. if(respawn)
  2209. CallSpawnScript(spawn, SPAWN_SCRIPT_RESPAWN);
  2210. else
  2211. CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  2212. }
  2213. break;
  2214. }
  2215. else
  2216. rand_number -= spawnlocation->entities[i]->spawn_percentage;
  2217. }
  2218. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2219. return spawn;
  2220. }
  2221. Spawn* ZoneServer::ProcessInstanceSpawnLocation(SpawnLocation* spawnlocation, map<int32,int32>* instNPCs, map<int32,int32>* instGroundSpawns, map<int32,int32>* instObjSpawns, map<int32,int32>* instWidgetSpawns, map<int32,int32>* instSignSpawns, bool respawn)
  2222. {
  2223. if(!spawnlocation)
  2224. return 0;
  2225. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2226. Spawn* spawn = 0;
  2227. float rand_number = MakeRandomFloat(0, spawnlocation->total_percentage);
  2228. for(int32 i=0;i<spawnlocation->entities.size();i++)
  2229. {
  2230. if(spawnlocation->entities[i]->spawn_percentage == 0)
  2231. continue;
  2232. int32 spawnTime = 0;
  2233. if(spawnlocation->entities[i]->spawn_percentage >= rand_number)
  2234. {
  2235. if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_NPC &&
  2236. (spawnTime = database.CheckSpawnRemoveInfo(instNPCs,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2237. spawn = AddNPCSpawn(spawnlocation, spawnlocation->entities[i]);
  2238. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN &&
  2239. (spawnTime = database.CheckSpawnRemoveInfo(instGroundSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2240. spawn = AddGroundSpawn(spawnlocation, spawnlocation->entities[i]);
  2241. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT &&
  2242. (spawnTime = database.CheckSpawnRemoveInfo(instObjSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2243. spawn = AddObjectSpawn(spawnlocation, spawnlocation->entities[i]);
  2244. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET &&
  2245. (spawnTime = database.CheckSpawnRemoveInfo(instWidgetSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2246. spawn = AddWidgetSpawn(spawnlocation, spawnlocation->entities[i]);
  2247. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_SIGN &&
  2248. (spawnTime = database.CheckSpawnRemoveInfo(instSignSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2249. spawn = AddSignSpawn(spawnlocation, spawnlocation->entities[i]);
  2250. if(spawn && spawn->IsOmittedByDBFlag())
  2251. {
  2252. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) in spawn location id %u was skipped due to a missing expansion / holiday flag being met (ZoneServer::ProcessInstanceSpawnLocation)", spawnlocation->entities[i]->spawn_id, spawnlocation->entities[i]->spawn_location_id);
  2253. safe_delete(spawn);
  2254. spawn = 0;
  2255. continue;
  2256. }
  2257. if (GetInstanceType() == PERSONAL_HOUSE_INSTANCE)
  2258. database.GetHouseSpawnInstanceData(this, spawn);
  2259. const char* script = 0;
  2260. for(int x=0;x<3;x++)
  2261. {
  2262. switch(x)
  2263. {
  2264. case 0:
  2265. script = world.GetSpawnEntryScript(spawnlocation->entities[i]->spawn_entry_id);
  2266. break;
  2267. case 1:
  2268. script = world.GetSpawnLocationScript(spawnlocation->entities[i]->spawn_location_id);
  2269. break;
  2270. case 2:
  2271. script = world.GetSpawnScript(spawnlocation->entities[i]->spawn_id);
  2272. break;
  2273. }
  2274. if(spawn && script && lua_interface->GetSpawnScript(script) != 0)
  2275. {
  2276. spawn->SetSpawnScript(string(script));
  2277. break;
  2278. }
  2279. }
  2280. if(spawn)
  2281. {
  2282. if (respawn)
  2283. CallSpawnScript(spawn, SPAWN_SCRIPT_RESPAWN);
  2284. else
  2285. CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  2286. if ( spawnTime > 1 )
  2287. {
  2288. spawn->SetRespawnTime(spawnTime);
  2289. }
  2290. }
  2291. break;
  2292. }
  2293. else
  2294. rand_number -= spawnlocation->entities[i]->spawn_percentage;
  2295. }
  2296. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2297. return spawn;
  2298. }
  2299. void ZoneServer::ProcessSpawnLocations()
  2300. {
  2301. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2302. map<int32,int32>* instNPCs = NULL;
  2303. map<int32,int32>* instGroundSpawns = NULL;
  2304. map<int32,int32>* instObjSpawns = NULL;
  2305. map<int32,int32>* instWidgetSpawns = NULL;
  2306. map<int32,int32>* instSignSpawns = NULL;
  2307. if ( this->IsInstanceZone() )
  2308. {
  2309. LogWrite(SPAWN__DEBUG, 0, "Spawn", "Processing Instance Removed Spawns...");
  2310. instNPCs = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_NPC );
  2311. instGroundSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_GROUNDSPAWN );
  2312. instObjSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_OBJECT );
  2313. instWidgetSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_WIDGET );
  2314. instSignSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_SIGN );
  2315. }
  2316. else {
  2317. instNPCs = database.GetPersistedSpawns(this->GetZoneID() , SPAWN_ENTRY_TYPE_NPC );
  2318. instGroundSpawns = database.GetPersistedSpawns(this->GetZoneID() , SPAWN_ENTRY_TYPE_GROUNDSPAWN );
  2319. instObjSpawns = database.GetPersistedSpawns(this->GetZoneID() , SPAWN_ENTRY_TYPE_OBJECT );
  2320. instWidgetSpawns = database.GetPersistedSpawns(this->GetZoneID() , SPAWN_ENTRY_TYPE_WIDGET );
  2321. instSignSpawns = database.GetPersistedSpawns(this->GetZoneID() , SPAWN_ENTRY_TYPE_SIGN );
  2322. }
  2323. map<int32, bool> processed_spawn_locations;
  2324. map<int32, SpawnLocation*>::iterator itr;
  2325. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  2326. for (itr = spawn_location_list.begin(); itr != spawn_location_list.end(); itr++) {
  2327. LogWrite(SPAWN__TRACE, 0, "Spawn", "while spawn_location_list itr (#%u)", spawn_location_list.size());
  2328. if(itr->second && processed_spawn_locations.count(itr->second->placement_id) > 0) //if we processed one spawn in a spawn group, we processed them all for that group
  2329. continue;
  2330. if(itr->second && spawn_location_groups.count(itr->second->placement_id) > 0)
  2331. {
  2332. int32 group_id = CalculateSpawnGroup(itr->second);
  2333. if(group_id)
  2334. {
  2335. LogWrite(SPAWN__TRACE, 0, "Spawn", "is group_id");
  2336. set<int32>* associated_groups = GetAssociatedGroups(group_id);
  2337. if(associated_groups)
  2338. {
  2339. LogWrite(SPAWN__TRACE, 0, "Spawn", "is associated_groups");
  2340. vector<int32>* associated_locations = GetAssociatedLocations(associated_groups);
  2341. if(associated_locations)
  2342. {
  2343. LogWrite(SPAWN__TRACE, 0, "Spawn", "is associated_locations");
  2344. for(int32 i=0;i<associated_locations->size();i++)
  2345. {
  2346. LogWrite(SPAWN__DEBUG, 5, "Spawn", "Loading processed_spawn_locations...");
  2347. processed_spawn_locations[associated_locations->at(i)] = true;
  2348. }
  2349. safe_delete(associated_locations);
  2350. }
  2351. }
  2352. }
  2353. }
  2354. else
  2355. {
  2356. if ( this->IsInstanceZone() )
  2357. {
  2358. //LogWrite(SPAWN__DEBUG, 5, "Spawn", "ProcessInstanceSpawnLocation (%u)...", itr->second->placement_id);
  2359. ProcessInstanceSpawnLocation(itr->second,instNPCs,instGroundSpawns,instObjSpawns,instWidgetSpawns,instSignSpawns);
  2360. }
  2361. else
  2362. {
  2363. //LogWrite(SPAWN__DEBUG, 5, "Spawn", "ProcessSpawnLocation (%u)...", itr->second->placement_id);
  2364. ProcessSpawnLocation(itr->second,instNPCs,instGroundSpawns,instObjSpawns,instWidgetSpawns,instSignSpawns);
  2365. }
  2366. }
  2367. }
  2368. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  2369. safe_delete(instNPCs);
  2370. safe_delete(instGroundSpawns);
  2371. safe_delete(instObjSpawns);
  2372. safe_delete(instWidgetSpawns);
  2373. safe_delete(instSignSpawns);
  2374. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2375. }
  2376. void ZoneServer::AddLoot(NPC* npc, Spawn* killer, GroupLootMethod loot_method, int8 item_rarity, int32 group_id){
  2377. // this function is ran twice, first on spawn of mob, then at death of mob (gray mob check and no_drop_quest_completed_id check)
  2378. // first we see if the skipping of gray mobs loot is enabled, then we move all non body drops
  2379. if(killer)
  2380. {
  2381. npc->SetLootMethod(loot_method, item_rarity, group_id);
  2382. int8 skip_loot_gray_mob_flag = rule_manager.GetZoneRule(GetZoneID(), R_Loot, SkipLootGrayMob)->GetInt8();
  2383. if(skip_loot_gray_mob_flag) {
  2384. Player* player = 0;
  2385. if(killer->IsPlayer())
  2386. player = (Player*)killer;
  2387. else if(killer->IsPet()) {
  2388. Spawn* owner = ((Entity*)killer)->GetOwner();
  2389. if(owner->IsPlayer())
  2390. player = (Player*)owner;
  2391. }
  2392. if(player) {
  2393. int8 difficulty = player->GetArrowColor(npc->GetLevel());
  2394. if(difficulty == ARROW_COLOR_GRAY) {
  2395. npc->ClearNonBodyLoot();
  2396. }
  2397. }
  2398. }
  2399. }
  2400. // check for starting loot of Spawn and death of Spawn loot (no_drop_quest_completed_id)
  2401. vector<int32> loot_tables = GetSpawnLootList(npc->GetDatabaseID(), GetZoneID(), npc->GetLevel(), race_types_list.GetRaceType(npc->GetModelType()), npc);
  2402. if(loot_tables.size() > 0){
  2403. vector<LootDrop*>* loot_drops = 0;
  2404. vector<LootDrop*>::iterator loot_drop_itr;
  2405. LootTable* table = 0;
  2406. vector<int32>::iterator loot_list_itr;
  2407. float chancecoin = 0;
  2408. float chancetable = 0;
  2409. float chancedrop = 0;
  2410. float chancetally = 0;
  2411. float droptally = 0;
  2412. // the following loop,loops through each table
  2413. for(loot_list_itr = loot_tables.begin(); loot_list_itr != loot_tables.end(); loot_list_itr++){
  2414. table = GetLootTable(*loot_list_itr);
  2415. // if killer is assigned this is on-death, we already assigned coin
  2416. if(!killer && table && table->maxcoin > 0){
  2417. chancecoin = rand()%100;
  2418. if(table->coin_probability >= chancecoin){
  2419. if(table->maxcoin > table->mincoin)
  2420. npc->AddLootCoins(table->mincoin + rand()%(table->maxcoin - table->mincoin));
  2421. }
  2422. }
  2423. int numberchances = 1;
  2424. //if (table->lootdrop_probability == 100){ }
  2425. //else
  2426. //chancetally += table->lootdrop_probability;
  2427. int maxchance = 0;
  2428. if (table) {
  2429. maxchance = table->maxlootitems;
  2430. for (numberchances; numberchances <= maxchance; numberchances++) {
  2431. chancetable = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / 100));
  2432. //LogWrite(PLAYER__DEBUG, 0, "Player", "Table Chance: '%f'", chancetable);
  2433. float droppercenttotal = 0;
  2434. //--------------------------------------------------------------------------------------------------------------------------------------------------------------
  2435. if (table->lootdrop_probability == 100 || table->lootdrop_probability >= chancetable) {
  2436. //LogWrite(PLAYER__DEBUG, 0, "Player", "Probability:%f Table Chance: '%f'", table->lootdrop_probability, chancetable);
  2437. loot_drops = GetLootDrops(*loot_list_itr);
  2438. if (loot_drops && loot_drops->size() > 0) {
  2439. LootDrop* drop = 0;
  2440. int16 count = 0;
  2441. std::shuffle(loot_drops->begin(), loot_drops->end(), std::default_random_engine(Timer::GetCurrentTime2()));
  2442. int16 IC = 0;
  2443. for (loot_drop_itr = loot_drops->begin(); loot_drop_itr != loot_drops->end(); loot_drop_itr++) {
  2444. drop = *loot_drop_itr;
  2445. droppercenttotal += drop->probability;
  2446. }
  2447. int droplistsize = loot_drops->size();
  2448. float chancedroptally = 0;
  2449. bool breakIterMaxLoot = false;
  2450. chancedrop = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / 100));
  2451. for (loot_drop_itr = loot_drops->begin(); loot_drop_itr != loot_drops->end(); loot_drop_itr++) {
  2452. drop = *loot_drop_itr;
  2453. // if no killer is provided, then we are instantiating the spawn loot, quest related loot should be added on death of the spawn to check against spawn/group members
  2454. if(drop->no_drop_quest_completed_id && killer == nullptr)
  2455. continue;
  2456. else if(!drop->no_drop_quest_completed_id && killer) // skip since this doesn't have a quest id attached and we are doing after-math of death loot additions
  2457. continue;
  2458. else if(killer && drop->no_drop_quest_completed_id) // check if the player already completed quest related to item
  2459. {
  2460. Player* player = nullptr;
  2461. if(killer->IsPlayer())
  2462. {
  2463. player = (Player*)killer;
  2464. // player has already completed the quest
  2465. if(player->HasQuestBeenCompleted(drop->no_drop_quest_completed_id) && !player->GetGroupMemberInfo())
  2466. {
  2467. LogWrite(PLAYER__DEBUG, 0, "Player", "%s: Player has completed quest %u, skipping loot item %u", npc->GetName(), drop->no_drop_quest_completed_id, drop->item_id);
  2468. continue;
  2469. }
  2470. else if(player->GetGroupMemberInfo() && world.GetGroupManager()->HasGroupCompletedQuest(player->GetGroupMemberInfo()->group_id, drop->no_drop_quest_completed_id))
  2471. {
  2472. LogWrite(PLAYER__DEBUG, 0, "Player", "%s: Group %u has completed quest %u, skipping loot item %u", npc->GetName(), player->GetGroupMemberInfo()->group_id, drop->no_drop_quest_completed_id, drop->item_id);
  2473. continue;
  2474. }
  2475. }
  2476. else
  2477. {
  2478. LogWrite(PLAYER__DEBUG, 0, "Player", "%s: Killer is not a player, skipping loot item %u", npc->GetName(), drop->item_id);
  2479. continue;
  2480. }
  2481. }
  2482. if (npc->HasLootItemID(drop->item_id))
  2483. continue;
  2484. if (droppercenttotal >= 100)
  2485. droppercenttotal = 100;
  2486. chancedroptally += 100 / droppercenttotal * drop->probability;
  2487. //chancedrop = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / 100));
  2488. //LogWrite(PLAYER__DEBUG, 0, "Player", "Loot drop: '%i' Chance: %f Prob tally: %f min: %f", drop, chancedrop, chancedroptally, chancedroptally - drop->probability);
  2489. if ((chancedroptally == 100) || ((chancedroptally >= chancedrop) && (chancedroptally - (100 / droppercenttotal * drop->probability)) <= chancedrop)) {
  2490. //LogWrite(PLAYER__DEBUG, 0, "Player", "Loot drop: '%i' Chance: %f Prob: %f We have a loot drop winner", drop, chancedrop, chancedroptally);
  2491. count++;
  2492. npc->AddLootItem(drop->item_id, drop->item_charges);
  2493. //LogWrite(PLAYER__DEBUG, 0, "Player", "loot Count: '%i'",count);
  2494. //LogWrite(MISC__TODO, 1, "TODO", "Auto-Equip new looted items\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  2495. //if(drop->equip_item)
  2496. }
  2497. // so many items on this table break out and cap it!
  2498. if (table->maxlootitems > 0 && count >= table->maxlootitems) {
  2499. breakIterMaxLoot = true;
  2500. break;
  2501. }
  2502. }
  2503. // hit our max item drop for this table already, break out of numberchances
  2504. if(breakIterMaxLoot) {
  2505. break;
  2506. }
  2507. }
  2508. }
  2509. }
  2510. }
  2511. }
  2512. }
  2513. }
  2514. void ZoneServer::DeterminePosition(SpawnLocation* spawnlocation, Spawn* spawn){
  2515. if(!spawn || !spawnlocation)
  2516. return;
  2517. int offset = 0;
  2518. if(spawnlocation->x_offset > 0){
  2519. //since rand() deals with integers only, we are going to divide by 1000 later so that we can use fractions of integers
  2520. offset = (int)((spawnlocation->x_offset*1000)+1);
  2521. spawn->SetX(spawnlocation->x + ((float)(rand()%offset - rand()%offset))/1000);
  2522. }
  2523. else
  2524. spawn->SetX(spawnlocation->x);
  2525. if(spawnlocation->y_offset > 0){
  2526. //since rand() deals with integers only, we are going to divide by 1000 later so that we can use fractions of integers
  2527. offset = (int)((spawnlocation->y_offset*1000)+1);
  2528. spawn->SetY(spawnlocation->y + ((float)(rand()%offset - rand()%offset))/1000);
  2529. }
  2530. else
  2531. spawn->SetY(spawnlocation->y, true, true);
  2532. if(spawnlocation->z_offset > 0){
  2533. //since rand() deals with integers only, we are going to divide by 1000 later so that we can use fractions of integers
  2534. offset = (int)((spawnlocation->z_offset*1000)+1);
  2535. spawn->SetZ(spawnlocation->z + ((float)(rand()%offset - rand()%offset))/1000);
  2536. }
  2537. else
  2538. spawn->SetZ(spawnlocation->z);
  2539. spawn->SetHeading(spawnlocation->heading);
  2540. spawn->SetPitch(spawnlocation->pitch);
  2541. spawn->SetRoll(spawnlocation->roll);
  2542. spawn->SetSpawnOrigX(spawn->GetX());
  2543. spawn->SetSpawnOrigY(spawn->GetY());
  2544. spawn->SetSpawnOrigZ(spawn->GetZ());
  2545. spawn->SetSpawnOrigHeading(spawn->GetHeading());
  2546. spawn->SetSpawnOrigPitch(spawnlocation->pitch);
  2547. spawn->SetSpawnOrigRoll(spawnlocation->roll);
  2548. spawn->SetLocation(spawnlocation->grid_id);
  2549. spawn->SetSpawnLocationPlacementID(spawnlocation->placement_id);
  2550. }
  2551. NPC* ZoneServer::AddNPCSpawn(SpawnLocation* spawnlocation, SpawnEntry* spawnentry){
  2552. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  2553. NPC* npc = GetNewNPC(spawnentry->spawn_id);
  2554. if(npc && !npc->IsOmittedByDBFlag()){
  2555. InfoStruct* info = npc->GetInfoStruct();
  2556. DeterminePosition(spawnlocation, npc);
  2557. npc->SetDatabaseID(spawnentry->spawn_id);
  2558. npc->SetSpawnLocationID(spawnentry->spawn_location_id);
  2559. npc->SetSpawnEntryID(spawnentry->spawn_entry_id);
  2560. npc->SetRespawnTime(spawnentry->respawn);
  2561. npc->SetExpireTime(spawnentry->expire_time);
  2562. //devn00b add overrides for some spawns
  2563. if(spawnentry->hp_override > 0){
  2564. npc->SetHP(spawnentry->hp_override);
  2565. }
  2566. if(spawnentry->lvl_override > 0){
  2567. npc->SetLevel(spawnentry->lvl_override);
  2568. }
  2569. if(spawnentry->mp_override > 0){
  2570. npc->SetPower(spawnentry->mp_override);
  2571. }
  2572. if(spawnentry->str_override > 0){
  2573. info->set_str_base(spawnentry->str_override);
  2574. info->set_str(spawnentry->str_override);
  2575. }
  2576. if(spawnentry->sta_override > 0){
  2577. info->set_sta_base(spawnentry->sta_override);
  2578. info->set_sta(spawnentry->sta_override);
  2579. }
  2580. if(spawnentry->wis_override > 0){
  2581. info->set_wis_base(spawnentry->wis_override);
  2582. info->set_wis(spawnentry->wis_override);
  2583. }
  2584. if(spawnentry->int_override > 0){
  2585. info->set_intel_base(spawnentry->int_override);
  2586. info->set_intel(spawnentry->int_override);
  2587. }
  2588. if(spawnentry->agi_override > 0){
  2589. info->set_agi_base(spawnentry->agi_override);
  2590. info->set_agi(spawnentry->agi_override);
  2591. }
  2592. if(spawnentry->heat_override > 0){
  2593. info->set_heat_base(spawnentry->heat_override);
  2594. info->set_heat(spawnentry->heat_override);
  2595. }
  2596. if(spawnentry->cold_override > 0){
  2597. info->set_cold_base(spawnentry->cold_override);
  2598. info->set_cold(spawnentry->cold_override);
  2599. }
  2600. if(spawnentry->magic_override > 0){
  2601. info->set_magic_base(spawnentry->magic_override);
  2602. info->set_magic(spawnentry->magic_override);
  2603. }
  2604. if(spawnentry->mental_override > 0){
  2605. info->set_mental_base(spawnentry->mental_override);
  2606. info->set_mental(spawnentry->mental_override);
  2607. }
  2608. if(spawnentry->divine_override > 0){
  2609. info->set_divine_base(spawnentry->divine_override);
  2610. info->set_divine(spawnentry->divine_override);
  2611. }
  2612. if(spawnentry->disease_override > 0){
  2613. info->set_disease_base(spawnentry->disease_override);
  2614. info->set_disease(spawnentry->disease_override);
  2615. }
  2616. if(spawnentry->poison_override > 0){
  2617. info->set_poison_base(spawnentry->poison_override);
  2618. info->set_poison(spawnentry->poison_override);
  2619. }
  2620. if(spawnentry->difficulty_override > 0){
  2621. npc->SetDifficulty(spawnentry->difficulty_override, 1);
  2622. }
  2623. if (spawnentry->expire_time > 0)
  2624. AddSpawnExpireTimer(npc, spawnentry->expire_time, spawnentry->expire_offset);
  2625. AddLoot(npc);
  2626. SetSpawnScript(spawnentry, npc);
  2627. CallSpawnScript(npc, SPAWN_SCRIPT_PRESPAWN);
  2628. AddSpawn(npc);
  2629. }
  2630. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  2631. return npc;
  2632. }
  2633. vector<int32>* ZoneServer::GetAssociatedLocations(set<int32>* groups){
  2634. vector<int32>* ret = 0;
  2635. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  2636. if(groups){
  2637. int32 group_id = 0;
  2638. set<int32>::iterator group_itr;
  2639. for (group_itr = groups->begin(); group_itr != groups->end(); group_itr++) {
  2640. if(!ret)
  2641. ret = new vector<int32>();
  2642. group_id = *group_itr;
  2643. MSpawnGroupLocations.readlock(__FUNCTION__, __LINE__);
  2644. if(spawn_group_locations.count(group_id) > 0){
  2645. map<int32, int32>::iterator itr;
  2646. for (itr = spawn_group_locations[group_id]->begin(); itr != spawn_group_locations[group_id]->end(); itr++) {
  2647. ret->push_back(itr->first);
  2648. }
  2649. }
  2650. MSpawnGroupLocations.releasereadlock(__FUNCTION__, __LINE__);
  2651. }
  2652. }
  2653. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  2654. return ret;
  2655. }
  2656. set<int32>* ZoneServer::GetAssociatedGroups(int32 group_id) {
  2657. set<int32>* ret = 0;
  2658. MSpawnGroupAssociation.readlock(__FUNCTION__, __LINE__);
  2659. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  2660. if(spawn_group_associations.count(group_id) > 0)
  2661. ret = spawn_group_associations[group_id];
  2662. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  2663. MSpawnGroupAssociation.releasereadlock(__FUNCTION__, __LINE__);
  2664. return ret;
  2665. }
  2666. map<int32, int32>* ZoneServer::GetSpawnLocationsByGroup(int32 group_id) {
  2667. map<int32, int32>* ret = 0;
  2668. MSpawnGroupLocations.readlock(__FUNCTION__, __LINE__);
  2669. if(spawn_group_locations.count(group_id) > 0)
  2670. ret = spawn_group_locations[group_id];
  2671. MSpawnGroupLocations.releasereadlock(__FUNCTION__, __LINE__);
  2672. return ret;
  2673. }
  2674. list<int32>* ZoneServer::GetSpawnGroupsByLocation(int32 location_id){
  2675. list<int32>* ret = 0;
  2676. MSpawnLocationGroups.readlock(__FUNCTION__, __LINE__);
  2677. if(spawn_location_groups.count(location_id) > 0)
  2678. ret = spawn_location_groups[location_id];
  2679. MSpawnLocationGroups.releasereadlock(__FUNCTION__, __LINE__);
  2680. return ret;
  2681. }
  2682. float ZoneServer::GetSpawnGroupChance(int32 group_id){
  2683. float ret = -1;
  2684. MSpawnGroupChances.readlock(__FUNCTION__, __LINE__);
  2685. if(spawn_group_chances.count(group_id) > 0)
  2686. ret = spawn_group_chances[group_id];
  2687. MSpawnGroupChances.releasereadlock(__FUNCTION__, __LINE__);
  2688. return ret;
  2689. }
  2690. void ZoneServer::AddSpawnGroupChance(int32 group_id, float percent){
  2691. MSpawnGroupChances.writelock(__FUNCTION__, __LINE__);
  2692. spawn_group_chances[group_id] = percent;
  2693. MSpawnGroupChances.releasewritelock(__FUNCTION__, __LINE__);
  2694. }
  2695. void ZoneServer::AddSpawnGroupAssociation(int32 group_id1, int32 group_id2) {
  2696. MSpawnGroupAssociation.writelock(__FUNCTION__, __LINE__);
  2697. //Check if we already have containers for these group ids, if not create them
  2698. if (spawn_group_associations.count(group_id1) == 0)
  2699. spawn_group_associations[group_id1] = new set<int32>;
  2700. if (spawn_group_associations.count(group_id2) == 0)
  2701. spawn_group_associations[group_id2] = new set<int32>;
  2702. //Associate groups 1 and 2 now
  2703. set<int32>* group_1 = spawn_group_associations.find(group_id1)->second;
  2704. set<int32>* group_2 = spawn_group_associations.find(group_id2)->second;
  2705. group_1->insert(group_id2);
  2706. group_2->insert(group_id1);
  2707. //Associate the remaining groups together
  2708. set<int32>::iterator itr;
  2709. for (itr = group_1->begin(); itr != group_1->end(); itr++){
  2710. group_2->insert(*itr);
  2711. map<int32, set<int32>*>::iterator assoc_itr = spawn_group_associations.find(*itr);
  2712. if (assoc_itr != spawn_group_associations.end())
  2713. assoc_itr->second->insert(group_id2);
  2714. else {
  2715. set<int32>* new_set = new set<int32>;
  2716. spawn_group_associations[*itr] = new_set;
  2717. new_set->insert(group_id2);
  2718. }
  2719. }
  2720. for (itr = group_2->begin(); itr != group_2->end(); itr++){
  2721. group_1->insert(*itr);
  2722. map<int32, set<int32>*>::iterator assoc_itr = spawn_group_associations.find(*itr);
  2723. if (assoc_itr != spawn_group_associations.end())
  2724. assoc_itr->second->insert(group_id1);
  2725. else {
  2726. set<int32>* new_set = new set<int32>;
  2727. spawn_group_associations[*itr] = new_set;
  2728. new_set->insert(group_id1);
  2729. }
  2730. }
  2731. MSpawnGroupAssociation.releasewritelock(__FUNCTION__, __LINE__);
  2732. }
  2733. void ZoneServer::AddSpawnGroupLocation(int32 group_id, int32 location_id, int32 spawn_location_id) {
  2734. MSpawnGroupLocations.writelock(__FUNCTION__, __LINE__);
  2735. if(spawn_group_locations.count(group_id) == 0)
  2736. spawn_group_locations[group_id] = new map<int32, int32>();
  2737. (*spawn_group_locations[group_id])[location_id] = spawn_location_id;
  2738. MSpawnGroupLocations.releasewritelock(__FUNCTION__, __LINE__);
  2739. MSpawnLocationGroups.writelock(__FUNCTION__, __LINE__);
  2740. if(spawn_location_groups.count(location_id) == 0)
  2741. spawn_location_groups[location_id] = new list<int32>();
  2742. spawn_location_groups[location_id]->push_back(group_id);
  2743. MSpawnLocationGroups.releasewritelock(__FUNCTION__, __LINE__);
  2744. MSpawnGroupAssociation.writelock(__FUNCTION__, __LINE__);
  2745. if(spawn_group_associations.count(group_id) == 0)
  2746. spawn_group_associations[group_id] = new set<int32>();
  2747. spawn_group_associations[group_id]->insert(group_id);
  2748. MSpawnGroupAssociation.releasewritelock(__FUNCTION__, __LINE__);
  2749. }
  2750. bool ZoneServer::CallSpawnScript(Spawn* npc, int8 type, Spawn* spawn, const char* message, bool is_door_open, sint32 input_value, sint32* return_value){
  2751. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2752. if(!npc)
  2753. return false;
  2754. const char* script = npc->GetSpawnScript();
  2755. if ( script == nullptr || strlen(script) < 1 )
  2756. {
  2757. if (!npc->IsPet() && npc->GetZone() != nullptr)
  2758. {
  2759. string tmpScript;
  2760. tmpScript.append("SpawnScripts/");
  2761. tmpScript.append(npc->GetZone()->GetZoneName());
  2762. tmpScript.append("/");
  2763. int count = 0;
  2764. for (int s = 0; s < strlen(npc->GetName()); s++)
  2765. {
  2766. if (isalnum((unsigned char)npc->GetName()[s]))
  2767. {
  2768. tmpScript += npc->GetName()[s];
  2769. count++;
  2770. }
  2771. }
  2772. tmpScript.append(".lua");
  2773. if (count < 1)
  2774. {
  2775. LogWrite(SPAWN__TRACE, 0, "Spawn", "Could not form script name %s..", __FUNCTION__);
  2776. }
  2777. else
  2778. {
  2779. struct stat buffer;
  2780. bool fileExists = (stat(tmpScript.c_str(), &buffer) == 0);
  2781. if (fileExists)
  2782. {
  2783. LogWrite(SPAWN__WARNING, 0, "Spawn", "No script file described in the database, overriding with SpawnScript at %s", (char*)tmpScript.c_str());
  2784. npc->SetSpawnScript(tmpScript);
  2785. script = npc->GetSpawnScript();
  2786. }
  2787. }
  2788. }
  2789. }
  2790. bool result = false;
  2791. if(lua_interface && script){
  2792. result = true; // default to true, if we don't match a switch case, return false in default case
  2793. switch(type){
  2794. case SPAWN_SCRIPT_SPAWN:{
  2795. lua_interface->RunSpawnScript(script, "spawn", npc);
  2796. break;
  2797. }
  2798. case SPAWN_SCRIPT_RESPAWN:{
  2799. lua_interface->RunSpawnScript(script, "respawn", npc);
  2800. break;
  2801. }
  2802. case SPAWN_SCRIPT_ATTACKED:{
  2803. lua_interface->RunSpawnScript(script, "attacked", npc, spawn);
  2804. break;
  2805. }
  2806. case SPAWN_SCRIPT_TARGETED:{
  2807. lua_interface->RunSpawnScript(script, "targeted", npc, spawn);
  2808. break;
  2809. }
  2810. case SPAWN_SCRIPT_HAILED:{
  2811. result = lua_interface->RunSpawnScript(script, "hailed", npc, spawn);
  2812. break;
  2813. }
  2814. case SPAWN_SCRIPT_HAILED_BUSY:{
  2815. lua_interface->RunSpawnScript(script, "hailed_busy", npc, spawn);
  2816. break;
  2817. }
  2818. case SPAWN_SCRIPT_DEATH:{
  2819. lua_interface->RunSpawnScript(script, "death", npc, spawn);
  2820. break;
  2821. }
  2822. case SPAWN_SCRIPT_KILLED:{
  2823. lua_interface->RunSpawnScript(script, "killed", npc, spawn);
  2824. break;
  2825. }
  2826. case SPAWN_SCRIPT_AGGRO:{
  2827. lua_interface->RunSpawnScript(script, "aggro", npc, spawn);
  2828. break;
  2829. }
  2830. case SPAWN_SCRIPT_HEALTHCHANGED:{
  2831. result = lua_interface->RunSpawnScript(script, "healthchanged", npc, spawn, 0, false, input_value, return_value);
  2832. break;
  2833. }
  2834. case SPAWN_SCRIPT_RANDOMCHAT:{
  2835. lua_interface->RunSpawnScript(script, "randomchat", npc, 0, message);
  2836. break;
  2837. }
  2838. case SPAWN_SCRIPT_CUSTOM:
  2839. case SPAWN_SCRIPT_TIMER:
  2840. case SPAWN_SCRIPT_CONVERSATION:{
  2841. lua_interface->RunSpawnScript(script, message, npc, spawn);
  2842. break;
  2843. }
  2844. case SPAWN_SCRIPT_CASTED_ON: {
  2845. lua_interface->RunSpawnScript(script, "casted_on", npc, spawn, message);
  2846. break;
  2847. }
  2848. case SPAWN_SCRIPT_AUTO_ATTACK_TICK: {
  2849. lua_interface->RunSpawnScript(script, "auto_attack_tick", npc, spawn);
  2850. break;
  2851. }
  2852. case SPAWN_SCRIPT_COMBAT_RESET: {
  2853. lua_interface->RunSpawnScript(script, "CombatReset", npc);
  2854. break;
  2855. }
  2856. case SPAWN_SCRIPT_GROUP_DEAD: {
  2857. lua_interface->RunSpawnScript(script, "group_dead", npc, spawn);
  2858. break;
  2859. }
  2860. case SPAWN_SCRIPT_HEAR_SAY: {
  2861. lua_interface->RunSpawnScript(script, "hear_say", npc, spawn, message);
  2862. break;
  2863. }
  2864. case SPAWN_SCRIPT_PRESPAWN: {
  2865. lua_interface->RunSpawnScript(script, "prespawn", npc);
  2866. break;
  2867. }
  2868. case SPAWN_SCRIPT_USEDOOR: {
  2869. result = lua_interface->RunSpawnScript(script, "usedoor", npc, spawn, "", is_door_open);
  2870. break;
  2871. }
  2872. case SPAWN_SCRIPT_BOARD: {
  2873. result = lua_interface->RunSpawnScript(script, "board", npc, spawn);
  2874. break;
  2875. }
  2876. case SPAWN_SCRIPT_DEBOARD: {
  2877. result = lua_interface->RunSpawnScript(script, "deboard", npc, spawn);
  2878. break;
  2879. }
  2880. default:
  2881. {
  2882. result = false;
  2883. break;
  2884. }
  2885. }
  2886. }
  2887. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2888. return result;
  2889. }
  2890. void ZoneServer::DeleteTransporters() {
  2891. MTransportLocations.writelock(__FUNCTION__, __LINE__);
  2892. transporter_locations.clear(); //world takes care of actually deleting the data
  2893. MTransportLocations.releasewritelock(__FUNCTION__, __LINE__);
  2894. }
  2895. void ZoneServer::ReloadTransporters(){
  2896. MutexList<LocationTransportDestination*>* locations = GetLocationTransporters(GetZoneID());
  2897. if(locations){
  2898. MutexList<LocationTransportDestination*>::iterator itr = locations->begin();
  2899. while(itr.Next())
  2900. AddTransporter(itr->value);
  2901. }
  2902. }
  2903. void ZoneServer::CheckTransporters(Client* client) {
  2904. MTransportLocations.readlock(__FUNCTION__, __LINE__);
  2905. if(transporter_locations.size() > 0){
  2906. LocationTransportDestination* loc = 0;
  2907. list<LocationTransportDestination*>::iterator itr;
  2908. for (itr = transporter_locations.begin(); itr != transporter_locations.end(); itr++) {
  2909. loc = *itr;
  2910. if(client->GetPlayer()->GetDistance(loc->trigger_x, loc->trigger_y, loc->trigger_z) <= loc->trigger_radius){
  2911. if(loc->destination_zone_id == 0 || loc->destination_zone_id == GetZoneID()){
  2912. EQ2Packet* packet = client->GetPlayer()->Move(loc->destination_x, loc->destination_y, loc->destination_z, client->GetVersion());
  2913. if(packet)
  2914. client->QueuePacket(packet);
  2915. }
  2916. else{
  2917. ZoneChangeDetails zone_details;
  2918. bool foundZone = zone_list.GetZone(&zone_details, loc->destination_zone_id);
  2919. if(foundZone){
  2920. client->GetPlayer()->SetX(loc->destination_x);
  2921. client->GetPlayer()->SetY(loc->destination_y);
  2922. client->GetPlayer()->SetZ(loc->destination_z);
  2923. client->GetPlayer()->SetHeading(loc->destination_heading);
  2924. client->Zone(&zone_details, (ZoneServer*)zone_details.zonePtr);
  2925. }
  2926. }
  2927. break;
  2928. }
  2929. }
  2930. }
  2931. MTransportLocations.releasereadlock(__FUNCTION__, __LINE__);
  2932. }
  2933. void ZoneServer::AddTransporter(LocationTransportDestination* loc) {
  2934. MTransportLocations.writelock(__FUNCTION__, __LINE__);
  2935. transporter_locations.push_back(loc);
  2936. MTransportLocations.releasewritelock(__FUNCTION__, __LINE__);
  2937. }
  2938. Sign* ZoneServer::AddSignSpawn(SpawnLocation* spawnlocation, SpawnEntry* spawnentry){
  2939. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2940. Sign* sign = GetNewSign(spawnentry->spawn_id);
  2941. if(sign && !sign->IsOmittedByDBFlag()){
  2942. DeterminePosition(spawnlocation, sign);
  2943. sign->SetDatabaseID(spawnentry->spawn_id);
  2944. sign->SetSpawnLocationID(spawnentry->spawn_location_id);
  2945. sign->SetSpawnEntryID(spawnentry->spawn_entry_id);
  2946. sign->SetRespawnTime(spawnentry->respawn);
  2947. sign->SetExpireTime(spawnentry->expire_time);
  2948. if (spawnentry->expire_time > 0)
  2949. AddSpawnExpireTimer(sign, spawnentry->expire_time, spawnentry->expire_offset);
  2950. SetSpawnScript(spawnentry, sign);
  2951. CallSpawnScript(sign, SPAWN_SCRIPT_PRESPAWN);
  2952. AddSpawn(sign);
  2953. }
  2954. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2955. return sign;
  2956. }
  2957. Widget* ZoneServer::AddWidgetSpawn(SpawnLocation* spawnlocation, SpawnEntry* spawnentry){
  2958. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2959. Widget* widget = GetNewWidget(spawnentry->spawn_id);
  2960. if(widget && !widget->IsOmittedByDBFlag()){
  2961. DeterminePosition(spawnlocation, widget);
  2962. widget->SetDatabaseID(spawnentry->spawn_id);
  2963. widget->SetSpawnLocationID(spawnentry->spawn_location_id);
  2964. widget->SetSpawnEntryID(spawnentry->spawn_entry_id);
  2965. if(!widget->GetIncludeLocation()){
  2966. widget->SetX(widget->GetWidgetX());
  2967. if(widget->GetCloseY() != 0)
  2968. widget->SetY(widget->GetCloseY());
  2969. widget->SetZ(widget->GetWidgetZ());
  2970. }
  2971. widget->SetRespawnTime(spawnentry->respawn);
  2972. widget->SetExpireTime(spawnentry->expire_time);
  2973. widget->SetSpawnOrigHeading(widget->GetHeading());
  2974. if (spawnentry->expire_time > 0)
  2975. AddSpawnExpireTimer(widget, spawnentry->expire_time, spawnentry->expire_offset);
  2976. SetSpawnScript(spawnentry, widget);
  2977. CallSpawnScript(widget, SPAWN_SCRIPT_PRESPAWN);
  2978. AddSpawn(widget);
  2979. }
  2980. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2981. return widget;
  2982. }
  2983. Object* ZoneServer::AddObjectSpawn(SpawnLocation* spawnlocation, SpawnEntry* spawnentry){
  2984. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2985. Object* object = GetNewObject(spawnentry->spawn_id);
  2986. if(object && !object->IsOmittedByDBFlag()){
  2987. DeterminePosition(spawnlocation, object);
  2988. object->SetDatabaseID(spawnentry->spawn_id);
  2989. object->SetSpawnLocationID(spawnentry->spawn_location_id);
  2990. object->SetSpawnEntryID(spawnentry->spawn_entry_id);
  2991. object->SetRespawnTime(spawnentry->respawn);
  2992. object->SetExpireTime(spawnentry->expire_time);
  2993. if (spawnentry->expire_time > 0)
  2994. AddSpawnExpireTimer(object, spawnentry->expire_time, spawnentry->expire_offset);
  2995. SetSpawnScript(spawnentry, object);
  2996. CallSpawnScript(object, SPAWN_SCRIPT_PRESPAWN);
  2997. AddSpawn(object);
  2998. }
  2999. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  3000. return object;
  3001. }
  3002. GroundSpawn* ZoneServer::AddGroundSpawn(SpawnLocation* spawnlocation, SpawnEntry* spawnentry){
  3003. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  3004. GroundSpawn* spawn = GetNewGroundSpawn(spawnentry->spawn_id);
  3005. if(spawn && !spawn->IsOmittedByDBFlag()){
  3006. DeterminePosition(spawnlocation, spawn);
  3007. spawn->SetDatabaseID(spawnentry->spawn_id);
  3008. spawn->SetSpawnLocationID(spawnentry->spawn_location_id);
  3009. spawn->SetSpawnEntryID(spawnentry->spawn_entry_id);
  3010. spawn->SetRespawnTime(spawnentry->respawn);
  3011. spawn->SetExpireTime(spawnentry->expire_time);
  3012. if(spawn->GetRandomizeHeading()) {
  3013. float rand_heading = MakeRandomFloat(0.0f, 360.0f);
  3014. spawn->SetHeading(rand_heading);
  3015. }
  3016. else {
  3017. spawn->SetHeading(spawnlocation->heading);
  3018. }
  3019. if (spawnentry->expire_time > 0)
  3020. AddSpawnExpireTimer(spawn, spawnentry->expire_time, spawnentry->expire_offset);
  3021. SetSpawnScript(spawnentry, spawn);
  3022. CallSpawnScript(spawn, SPAWN_SCRIPT_PRESPAWN);
  3023. AddSpawn(spawn);
  3024. }
  3025. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  3026. return spawn;
  3027. }
  3028. void ZoneServer::AddSpawn(Spawn* spawn) {
  3029. if(!spawn->IsPlayer()) // we already set it on loadCharacter
  3030. spawn->SetZone(this);
  3031. MIgnoredWidgets.lock_shared();
  3032. std::map<int32, bool>::iterator itr;
  3033. for(itr = ignored_widgets.begin(); itr != ignored_widgets.end(); itr++) {
  3034. spawn->AddIgnoredWidget(itr->first);
  3035. }
  3036. MIgnoredWidgets.unlock_shared();
  3037. spawn->position_changed = false;
  3038. spawn->info_changed = false;
  3039. spawn->vis_changed = false;
  3040. spawn->changed = false;
  3041. // Write locking the spawn list here will cause deadlocks, so instead add it to a temp list that the
  3042. // main spawn thread will put into the spawn_list when ever it has a chance.
  3043. MPendingSpawnListAdd.writelock(__FUNCTION__, __LINE__);
  3044. pending_spawn_list_add.push_back(spawn);
  3045. MPendingSpawnListAdd.releasewritelock(__FUNCTION__, __LINE__);
  3046. spawn_range.Trigger();
  3047. if (GetInstanceType() == PERSONAL_HOUSE_INSTANCE && spawn->IsObject())
  3048. {
  3049. spawn->AddSecondaryEntityCommand("Examine", 20, "house_spawn_examine", "", 0, 0);
  3050. spawn->AddSecondaryEntityCommand("Move", 20, "move_item", "", 0, 0);
  3051. spawn->AddSecondaryEntityCommand("Pack in Moving Crate", 20, "house_spawn_pack_in_moving_crate", "", 0, 0);
  3052. spawn->AddSecondaryEntityCommand("Pick Up", 20, "pickup", "", 0, 0);
  3053. spawn->SetShowCommandIcon(1);
  3054. }
  3055. if(spawn->IsNPC())
  3056. AddEnemyList((NPC*)spawn);
  3057. if(spawn->IsPlayer() && ((Player*)spawn)->GetGroupMemberInfo())
  3058. spawn->SendGroupUpdate();
  3059. if (spawn->IsPlayer()) {
  3060. ((Player*)spawn)->GetInfoStruct()->set_rain(rain);
  3061. ((Player*)spawn)->SetCharSheetChanged(true);
  3062. }
  3063. if (movementMgr != nullptr && spawn->IsEntity()) {
  3064. movementMgr->AddMob((Entity*)spawn);
  3065. }
  3066. AddSpawnProximities(spawn);
  3067. AddSpawnToGrid(spawn, spawn->GetLocation());
  3068. spawn->SetAddedToWorldTimestamp(Timer::GetCurrentTime2());
  3069. }
  3070. void ZoneServer::AddClient(Client* client){
  3071. MClientList.writelock(__FUNCTION__, __LINE__);
  3072. lifetime_client_count++;
  3073. DecrementIncomingClients();
  3074. clients.push_back(client);
  3075. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  3076. connected_clients.Add(client);
  3077. }
  3078. void ZoneServer::RemoveClient(Client* client)
  3079. {
  3080. Guild *guild;
  3081. bool dismissPets = false;
  3082. if(client)
  3083. {
  3084. if (client->GetPlayer())
  3085. client_list.RemovePlayerFromInvisHistory(client->GetPlayer()->GetID());
  3086. LogWrite(ZONE__DEBUG, 0, "Zone", "Sending login equipment appearance updates...");
  3087. loginserver.SendImmediateEquipmentUpdatesForChar(client->GetPlayer()->GetCharacterID());
  3088. if (!client->IsZoning())
  3089. {
  3090. client->SaveSpells();
  3091. client->GetPlayer()->DeleteSpellEffects(true);
  3092. if ((guild = client->GetPlayer()->GetGuild()) != NULL)
  3093. guild->GuildMemberLogoff(client->GetPlayer());
  3094. chat.LeaveAllChannels(client);
  3095. }
  3096. if(!zoneShuttingDown && !client->IsZoning())
  3097. {
  3098. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  3099. GroupMemberInfo* gmi = client->GetPlayer()->GetGroupMemberInfo();
  3100. int32 group_id = 0;
  3101. if (gmi) {
  3102. group_id = gmi->group_id;
  3103. }
  3104. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  3105. if (group_id) {
  3106. int32 size = world.GetGroupManager()->GetGroupSize(group_id);
  3107. if (size > 1) {
  3108. bool send_left_message = size > 2;
  3109. // removegroupmember can delete the gmi, so make sure we still have a group_id after
  3110. world.GetGroupManager()->RemoveGroupMember(group_id, client->GetPlayer());
  3111. if (send_left_message)
  3112. world.GetGroupManager()->GroupMessage(group_id, "%s has left the group.", client->GetPlayer()->GetName());
  3113. }
  3114. }
  3115. if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0)
  3116. {
  3117. LogWrite(ZONE__DEBUG, 0, "Zone", "Removing client '%s' (%u) due to LD/Exit...", client->GetPlayer()->GetName(), client->GetPlayer()->GetCharacterID());
  3118. }
  3119. else
  3120. {
  3121. LogWrite(ZONE__DEBUG, 0, "Zone", "Removing client '%s' (%u) due to Camp/Quit...", client->GetPlayer()->GetName(), client->GetPlayer()->GetCharacterID());
  3122. }
  3123. dismissPets = true;
  3124. //}
  3125. }
  3126. else
  3127. {
  3128. LogWrite(ZONE__DEBUG, 0, "Zone", "Removing client '%s' (%u) due to some client zoning...", client->GetPlayer()->GetName(), client->GetPlayer()->GetCharacterID());
  3129. }
  3130. map<int32, int32>::iterator itr;
  3131. for (itr = client->GetPlayer()->SpawnedBots.begin(); itr != client->GetPlayer()->SpawnedBots.end(); itr++) {
  3132. Spawn* spawn = GetSpawnByID(itr->second);
  3133. if (spawn && spawn->IsBot()) {
  3134. ((Entity*)spawn)->SetOwner(nullptr);
  3135. ((Bot*)spawn)->Camp();
  3136. }
  3137. }
  3138. if(dismissPets) {
  3139. ((Entity*)client->GetPlayer())->DismissAllPets();
  3140. }
  3141. MClientList.writelock(__FUNCTION__, __LINE__);
  3142. LogWrite(ZONE__DEBUG, 0, "Zone", "Calling clients.Remove(client)...");
  3143. std::vector<Client*>::iterator itr2 = find(clients.begin(), clients.end(), client);
  3144. if (itr2 != clients.end())
  3145. clients.erase(itr2);
  3146. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  3147. LogWrite(ZONE__INFO, 0, "Zone", "Scheduling client '%s' for removal.", client->GetPlayer()->GetName());
  3148. database.ToggleCharacterOnline(client, 0);
  3149. RemoveSpawn(client->GetPlayer(), false, true, true, true, true);
  3150. int32 DisconnectClientTimer = rule_manager.GetGlobalRule(R_World, RemoveDisconnectedClientsTimer)->GetInt32();
  3151. connected_clients.Remove(client, true, DisconnectClientTimer); // changed from a hardcoded 30000 (30 sec) to the DisconnectClientTimer rule
  3152. }
  3153. }
  3154. void ZoneServer::RemoveClientImmediately(Client* client) {
  3155. Guild *guild;
  3156. if(client)
  3157. {
  3158. if(client->GetPlayer()) {
  3159. if((client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0) {
  3160. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() - ACTIVITY_STATUS_LINKDEAD);
  3161. }
  3162. if ((client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0) {
  3163. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() + ACTIVITY_STATUS_CAMPING);
  3164. }
  3165. client->Disconnect();
  3166. }
  3167. MClientList.writelock(__FUNCTION__, __LINE__);
  3168. std::vector<Client*>::iterator itr = find(clients.begin(), clients.end(), client);
  3169. if (itr != clients.end())
  3170. clients.erase(itr);
  3171. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  3172. //clients.Remove(client, true);
  3173. }
  3174. }
  3175. void ZoneServer::ClientProcess(bool ignore_shutdown_timer)
  3176. {
  3177. if(!ignore_shutdown_timer && connected_clients.size(true) == 0)
  3178. {
  3179. MIncomingClients.readlock(__FUNCTION__, __LINE__);
  3180. bool shutdownDelayCheck = shutdownDelayTimer.Check();
  3181. if((!AlwaysLoaded() && !shutdownTimer.Enabled()) || (!AlwaysLoaded() && shutdownDelayCheck))
  3182. {
  3183. if(incoming_clients && !shutdownDelayTimer.Enabled()) {
  3184. LogWrite(ZONE__INFO, 0, "Zone", "Incoming clients (%u) expected for %s, delaying shutdown timer...", incoming_clients, GetZoneName());
  3185. int32 timerDelay = rule_manager.GetGlobalRule(R_Zone, ShutdownDelayTimer)->GetInt32();
  3186. if(timerDelay < 10) {
  3187. LogWrite(ZONE__INFO, 0, "Zone", "Overriding %s shutdown delay timer as other clients are incoming, value %u too short, setting to 10...", GetZoneName(), timerDelay);
  3188. timerDelay = 10;
  3189. }
  3190. shutdownDelayTimer.Start(timerDelay, true);
  3191. }
  3192. else if(!incoming_clients || shutdownDelayCheck) {
  3193. if(!shutdownTimer.Enabled()) {
  3194. LogWrite(ZONE__INFO, 0, "Zone", "Starting zone shutdown timer for %s...", GetZoneName());
  3195. shutdownTimer.Start();
  3196. }
  3197. else {
  3198. LogWrite(ZONE__INFO, 0, "Zone", "zone shutdown timer for %s has %u remaining...", GetZoneName(), shutdownTimer.GetRemainingTime());
  3199. }
  3200. }
  3201. }
  3202. else if(AlwaysLoaded() && shutdownTimer.Enabled()) {
  3203. shutdownTimer.Disable();
  3204. }
  3205. MIncomingClients.releasereadlock(__FUNCTION__, __LINE__);
  3206. return;
  3207. }
  3208. shutdownTimer.Disable();
  3209. shutdownDelayTimer.Disable();
  3210. Client* client = 0;
  3211. MutexList<Client*>::iterator iterator = connected_clients.begin();
  3212. while(iterator.Next())
  3213. {
  3214. client = iterator->value;
  3215. #ifndef NO_CATCH
  3216. try
  3217. {
  3218. #endif
  3219. if(zoneShuttingDown || !client->Process(true))
  3220. {
  3221. if(!zoneShuttingDown && !client->IsZoning())
  3222. {
  3223. // avoid spam of messages while we await linkdead to complete
  3224. if(!client->IsLinkdeadTimerEnabled()) {
  3225. bool camping = (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING);
  3226. LogWrite(ZONE__DEBUG, 0, "Zone", "Client is disconnecting in %s (camping = %s)", __FUNCTION__, camping ? "true" : "false");
  3227. if(!camping) {
  3228. client->setConnection(nullptr);
  3229. }
  3230. }
  3231. if((client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0) {
  3232. client->StartLinkdeadTimer();
  3233. }
  3234. else if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 )
  3235. {
  3236. //only set LD flag if we're disconnecting but not camping/quitting
  3237. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() + ACTIVITY_STATUS_LINKDEAD);
  3238. client->StartLinkdeadTimer();
  3239. }
  3240. else {
  3241. // camp timer completed, remove client
  3242. RemoveClient(client);
  3243. client->Disconnect();
  3244. }
  3245. }
  3246. else {
  3247. // force boot all players or clients zoning
  3248. RemoveClient(client);
  3249. client->Disconnect();
  3250. }
  3251. }
  3252. #ifndef NO_CATCH
  3253. }
  3254. catch(...)
  3255. {
  3256. LogWrite(ZONE__ERROR, 0, "Zone", "Exception caught when in ZoneServer::ClientProcess() for zone '%s'!\n%s, %i", GetZoneName(), __FUNCTION__, __LINE__);
  3257. try{
  3258. bool isLinkdead = false;
  3259. if(!client->IsZoning())
  3260. {
  3261. if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 )
  3262. {
  3263. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() + ACTIVITY_STATUS_LINKDEAD);
  3264. client->StartLinkdeadTimer();
  3265. isLinkdead = true;
  3266. if(client->GetPlayer()->GetGroupMemberInfo())
  3267. world.GetGroupManager()->GroupMessage(client->GetPlayer()->GetGroupMemberInfo()->group_id, "%s has gone Linkdead.", client->GetPlayer()->GetName());
  3268. }
  3269. }
  3270. if(!isLinkdead) {
  3271. RemoveClient(client);
  3272. client->Disconnect();
  3273. }
  3274. }
  3275. catch(...){
  3276. LogWrite(ZONE__ERROR, 0, "Zone", "Exception caught when in ZoneServer::ClientProcess(), second try\n%s, %i", __FUNCTION__, __LINE__);
  3277. }
  3278. }
  3279. #endif
  3280. }
  3281. }
  3282. void ZoneServer::SimpleMessage(int8 type, const char* message, Spawn* from, float distance, bool send_to_sender){
  3283. Client* client = 0;
  3284. vector<Client*>::iterator client_itr;
  3285. MClientList.readlock(__FUNCTION__, __LINE__);
  3286. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3287. client = *client_itr;
  3288. if(from && client && client->IsConnected() && (send_to_sender || from != client->GetPlayer()) && from->GetDistance(client->GetPlayer()) <= distance){
  3289. client->SimpleMessage(type, message);
  3290. }
  3291. }
  3292. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3293. }
  3294. void ZoneServer::HandleChatMessage(Client* client, Spawn* from, const char* to, int16 channel, const char* message, float distance, const char* channel_name, bool show_bubble, int32 language) {
  3295. if ((!distance || from->GetDistance(client->GetPlayer()) <= distance) && (!from || !client->GetPlayer()->IsIgnored(from->GetName()))) {
  3296. PacketStruct* packet = configReader.getStruct("WS_HearChat", client->GetVersion());
  3297. if (packet) {
  3298. if (from)
  3299. packet->setMediumStringByName("from", from->GetName());
  3300. if (client->GetPlayer() != from)
  3301. packet->setMediumStringByName("to", client->GetPlayer()->GetName());
  3302. int8 clientchannel = client->GetMessageChannelColor(channel);
  3303. packet->setDataByName("channel", client->GetMessageChannelColor(channel));
  3304. if (from && ((from == client->GetPlayer()) || (client->GetPlayer()->WasSentSpawn(from->GetID()))))
  3305. packet->setDataByName("from_spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(from));
  3306. else
  3307. packet->setDataByName("from_spawn_id", 0xFFFFFFFF);
  3308. packet->setDataByName("to_spawn_id", 0xFFFFFFFF);
  3309. packet->setMediumStringByName("message", message);
  3310. packet->setDataByName("language", language);
  3311. bool hasLanguage = client->GetPlayer()->HasLanguage(language);
  3312. if (language > 0 && !hasLanguage)
  3313. packet->setDataByName("understood", 0);
  3314. else
  3315. packet->setDataByName("understood", 1);
  3316. show_bubble == true ? packet->setDataByName("show_bubble", 1) : packet->setDataByName("show_bubble", 0);
  3317. if (channel_name)
  3318. packet->setMediumStringByName("channel_name", channel_name);
  3319. EQ2Packet* outapp = packet->serialize();
  3320. //DumpPacket(outapp);
  3321. client->QueuePacket(outapp);
  3322. safe_delete(packet);
  3323. }
  3324. }
  3325. }
  3326. void ZoneServer::HandleChatMessage(Client* client, std::string fromName, const char* to, int16 channel, const char* message, float distance, const char* channel_name, int32 language) {
  3327. if (!client->GetPlayer()->IsIgnored(fromName.c_str())) {
  3328. PacketStruct* packet = configReader.getStruct("WS_HearChat", client->GetVersion());
  3329. if (packet) {
  3330. packet->setMediumStringByName("from", fromName.c_str());
  3331. int8 clientchannel = client->GetMessageChannelColor(channel);
  3332. packet->setDataByName("channel", client->GetMessageChannelColor(channel));
  3333. packet->setDataByName("from_spawn_id", 0xFFFFFFFF);
  3334. packet->setDataByName("to_spawn_id", 0xFFFFFFFF);
  3335. packet->setMediumStringByName("message", message);
  3336. packet->setDataByName("language", language);
  3337. bool hasLanguage = client->GetPlayer()->HasLanguage(language);
  3338. if (language > 0 && !hasLanguage)
  3339. packet->setDataByName("understood", 0);
  3340. else
  3341. packet->setDataByName("understood", 1);
  3342. packet->setDataByName("show_bubble", 0);
  3343. if (channel_name)
  3344. packet->setMediumStringByName("channel_name", channel_name);
  3345. EQ2Packet* outapp = packet->serialize();
  3346. //DumpPacket(outapp);
  3347. client->QueuePacket(outapp);
  3348. safe_delete(packet);
  3349. }
  3350. }
  3351. }
  3352. void ZoneServer::HandleChatMessage(Spawn* from, const char* to, int16 channel, const char* message, float distance, const char* channel_name, bool show_bubble, int32 language){
  3353. vector<Client*>::iterator client_itr;
  3354. Client* client = 0;
  3355. MClientList.readlock(__FUNCTION__, __LINE__);
  3356. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3357. client = *client_itr;
  3358. if(client && client->IsConnected())
  3359. HandleChatMessage(client, from, to, channel, message, distance, channel_name, show_bubble, language);
  3360. }
  3361. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3362. }
  3363. void ZoneServer::HandleChatMessage(std::string fromName, const char* to, int16 channel, const char* message, float distance, const char* channel_name, int32 language){
  3364. vector<Client*>::iterator client_itr;
  3365. Client* client = 0;
  3366. MClientList.readlock(__FUNCTION__, __LINE__);
  3367. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3368. client = *client_itr;
  3369. if(client && client->IsConnected())
  3370. HandleChatMessage(client, fromName, to, channel, message, distance, channel_name, language);
  3371. }
  3372. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3373. }
  3374. void ZoneServer::HandleBroadcast(const char* message) {
  3375. vector<Client*>::iterator client_itr;
  3376. Client* client = 0;
  3377. MClientList.readlock(__FUNCTION__, __LINE__);
  3378. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3379. client = *client_itr;
  3380. if(client && client->IsConnected())
  3381. client->SimpleMessage(CHANNEL_BROADCAST, message);
  3382. }
  3383. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3384. }
  3385. void ZoneServer::HandleAnnouncement(const char* message) {
  3386. vector<Client*>::iterator client_itr;
  3387. Client* client = 0;
  3388. int32 words = ::CountWordsInString(message);
  3389. if (words < 5)
  3390. words = 5;
  3391. MClientList.readlock(__FUNCTION__, __LINE__);
  3392. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3393. client = *client_itr;
  3394. if(client && client->IsConnected()) {
  3395. client->SimpleMessage(CHANNEL_BROADCAST, message);
  3396. client->SendPopupMessage(10, message, "ui_harvest_normal", words, 0xFF, 0xFF, 0x00);
  3397. }
  3398. }
  3399. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3400. }
  3401. void ZoneServer::SendTimeUpdate(Client* client){
  3402. if(client){
  3403. PacketStruct* packet = world.GetWorldTime(client->GetVersion());
  3404. if(packet){
  3405. client->QueuePacket(packet->serialize());
  3406. safe_delete(packet);
  3407. }
  3408. }
  3409. }
  3410. void ZoneServer::SendTimeUpdateToAllClients(){
  3411. Client* client = 0;
  3412. vector<Client*>::iterator client_itr;
  3413. MClientList.readlock(__FUNCTION__, __LINE__);
  3414. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3415. client = *client_itr;
  3416. if(client && client->IsConnected())
  3417. SendTimeUpdate(client);
  3418. }
  3419. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3420. }
  3421. void ZoneServer::UpdateVitality(float amount){
  3422. Client* client = 0;
  3423. vector<Client*>::iterator client_itr;
  3424. MClientList.readlock(__FUNCTION__, __LINE__);
  3425. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3426. client = *client_itr;
  3427. if(client && client->GetPlayer()->GetInfoStruct()->get_xp_vitality() < 100){
  3428. if((client->GetPlayer()->GetInfoStruct()->get_xp_vitality() + amount) > 100)
  3429. client->GetPlayer()->GetInfoStruct()->set_xp_vitality(100);
  3430. else
  3431. client->GetPlayer()->GetInfoStruct()->add_xp_vitality(amount);
  3432. client->GetPlayer()->SetCharSheetChanged(true);
  3433. }
  3434. }
  3435. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3436. }
  3437. void ZoneServer::SendSpawn(Spawn* spawn, Client* client){
  3438. EQ2Packet* outapp = spawn->serialize(client->GetPlayer(), client->GetVersion());
  3439. if(!client->GetPlayer()->IsSendingSpawn(spawn->GetID())) {
  3440. safe_delete(outapp);
  3441. }
  3442. else {
  3443. LogWrite(ZONE__DEBUG, 7, "Zone", "%s: Processing SendSpawn for spawn index %u (%s)...", client->GetPlayer()->GetName(), client->GetPlayer()->GetIndexForSpawn(spawn), spawn->GetName());
  3444. if(outapp)
  3445. client->QueuePacket(outapp, true);
  3446. client->GetPlayer()->SetSpawnSentState(spawn, SpawnState::SPAWN_STATE_SENT_WAIT);
  3447. }
  3448. /*
  3449. vis flags:
  3450. 2 = show icon
  3451. 4 = targetable
  3452. 16 = show name
  3453. 32 = show level/border
  3454. activity_status:
  3455. 4 - linkdead
  3456. 8 - camping
  3457. 16 - LFG
  3458. 32 - LFW
  3459. 2048 - mentoring
  3460. 4096 - displays shield
  3461. 8192 - immunity gained
  3462. 16384 - immunity remaining
  3463. attackable_status
  3464. 1 - no_hp_bar
  3465. 4 - not attackable
  3466. npc_con
  3467. -4 = scowls
  3468. -3 = threatening
  3469. -2 = dubiously
  3470. -1 = apprehensively
  3471. 0 = indifferent
  3472. 1 = amiably
  3473. 2 = kindly
  3474. 3 = warmly
  3475. 4 = ally
  3476. quest_flag
  3477. 1 = new quest
  3478. 2 = update and new quest
  3479. 3 = update
  3480. */
  3481. if(spawn->IsEntity() && spawn->HasTrapTriggered())
  3482. client->QueueStateCommand(client->GetPlayer()->GetIDWithPlayerSpawn(spawn), spawn->GetTrapState());
  3483. }
  3484. Client* ZoneServer::GetClientByName(char* name) {
  3485. Client* ret = 0;
  3486. vector<Client*>::iterator itr;
  3487. MClientList.readlock(__FUNCTION__, __LINE__);
  3488. for (itr = clients.begin(); itr != clients.end(); itr++) {
  3489. if ((*itr)->GetPlayer()) {
  3490. if (strncmp((*itr)->GetPlayer()->GetName(), name, strlen(name)) == 0) {
  3491. ret = *itr;
  3492. break;
  3493. }
  3494. }
  3495. }
  3496. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3497. return ret;
  3498. }
  3499. Client* ZoneServer::GetClientByCharID(int32 charid) {
  3500. Client* ret = 0;
  3501. vector<Client*>::iterator itr;
  3502. MClientList.readlock(__FUNCTION__, __LINE__);
  3503. for (itr = clients.begin(); itr != clients.end(); itr++) {
  3504. if ((*itr)->GetCharacterID() == charid) {
  3505. ret = *itr;
  3506. break;
  3507. }
  3508. }
  3509. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3510. return ret;
  3511. }
  3512. void ZoneServer::AddMovementNPC(Spawn* spawn){
  3513. if (spawn)
  3514. movement_spawns.Put(spawn->GetID(), 1);
  3515. }
  3516. void ZoneServer::RemoveMovementNPC(Spawn* spawn){
  3517. if (spawn)
  3518. remove_movement_spawns.Add(spawn->GetID());
  3519. }
  3520. void ZoneServer::PlayFlavor(Client* client, Spawn* spawn, const char* mp3, const char* text, const char* emote, int32 key1, int32 key2, int8 language){
  3521. if(!client || !spawn)
  3522. return;
  3523. PacketStruct* packet = configReader.getStruct("WS_PlayFlavor", client->GetVersion());
  3524. if(packet){
  3525. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  3526. packet->setDataByName("unknown1", 0xFFFFFFFF);
  3527. packet->setDataByName("unknown5", 1, 1);
  3528. packet->setDataByName("unknown5", 1, 6);
  3529. if(mp3){
  3530. packet->setMediumStringByName("mp3", mp3);
  3531. packet->setDataByName("key", key1);
  3532. packet->setDataByName("key", key2, 1);
  3533. }
  3534. packet->setMediumStringByName("name", spawn->GetName());
  3535. if(text)
  3536. packet->setMediumStringByName("text", text);
  3537. if(emote) {
  3538. if(client->GetVersion() > 561) {
  3539. packet->setMediumStringByName("emote", emote);
  3540. }
  3541. else {
  3542. HandleEmote(spawn, std::string(emote));
  3543. }
  3544. }
  3545. if (language != 0)
  3546. packet->setDataByName("language", language);
  3547. //We should probably add Common = language id 0 or 0xFF so admins can customize more..
  3548. if (language == 0 || client->GetPlayer()->HasLanguage(language))
  3549. packet->setDataByName("understood", 1);
  3550. EQ2Packet* app = packet->serialize();
  3551. //DumpPacket(app);
  3552. client->QueuePacket(app);
  3553. safe_delete(packet);
  3554. }
  3555. }
  3556. void ZoneServer::PlayVoice(Client* client, Spawn* spawn, const char* mp3, int32 key1, int32 key2){
  3557. if(!client || !spawn)
  3558. return;
  3559. PacketStruct* packet = configReader.getStruct("WS_PlayVoice", client->GetVersion());
  3560. if(packet){
  3561. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  3562. packet->setMediumStringByName("mp3", mp3);
  3563. packet->setDataByName("key", key1);
  3564. packet->setDataByName("key", key2, 1);
  3565. client->QueuePacket(packet->serialize());
  3566. safe_delete(packet);
  3567. }
  3568. }
  3569. void ZoneServer::PlayFlavor(Spawn* spawn, const char* mp3, const char* text, const char* emote, int32 key1, int32 key2, int8 language){
  3570. if(!spawn)
  3571. return;
  3572. Client* client = 0;
  3573. vector<Client*>::iterator client_itr;
  3574. MClientList.readlock(__FUNCTION__, __LINE__);
  3575. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3576. client = *client_itr;
  3577. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->GetDistance(spawn) > 30)
  3578. continue;
  3579. PlayFlavor(client, spawn, mp3, text, emote, key1, key2, language);
  3580. }
  3581. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3582. }
  3583. void ZoneServer::PlayFlavorID(Spawn* spawn, int8 type, int32 id, int16 index, int8 language){
  3584. if(!spawn)
  3585. return;
  3586. Client* client = 0;
  3587. vector<Client*>::iterator client_itr;
  3588. VoiceOverStruct non_garble, garble;
  3589. bool garble_success = false;
  3590. bool success = world.FindVoiceOver(type, id, index, &non_garble, &garble_success, &garble);
  3591. VoiceOverStruct* resStruct = nullptr;
  3592. MClientList.readlock(__FUNCTION__, __LINE__);
  3593. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3594. client = *client_itr;
  3595. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->GetDistance(spawn) > 30)
  3596. continue;
  3597. client->SendPlayFlavor(spawn, language, &non_garble, &garble, success, garble_success);
  3598. }
  3599. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3600. }
  3601. void ZoneServer::PlayVoice(Spawn* spawn, const char* mp3, int32 key1, int32 key2){
  3602. if(!spawn || !mp3)
  3603. return;
  3604. Client* client = 0;
  3605. vector<Client*>::iterator client_itr;
  3606. MClientList.readlock(__FUNCTION__, __LINE__);
  3607. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3608. client = *client_itr;
  3609. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  3610. continue;
  3611. PlayVoice(client, spawn, mp3, key1, key2);
  3612. }
  3613. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3614. }
  3615. void ZoneServer::PlaySoundFile(Client* client, const char* name, float origin_x, float origin_y, float origin_z){
  3616. if(!name)
  3617. return;
  3618. PacketStruct* packet = 0;
  3619. if(client){
  3620. packet = configReader.getStruct("WS_Play3DSound", client->GetVersion());
  3621. if(packet){
  3622. packet->setMediumStringByName("name", name);
  3623. packet->setDataByName("x", origin_x);
  3624. packet->setDataByName("y", origin_y);
  3625. packet->setDataByName("z", origin_z);
  3626. packet->setDataByName("unknown1", 1);
  3627. packet->setDataByName("unknown2", 2.5);
  3628. packet->setDataByName("unknown3", 15);
  3629. client->QueuePacket(packet->serialize());
  3630. safe_delete(packet);
  3631. }
  3632. }
  3633. else{
  3634. EQ2Packet* outapp = 0;
  3635. int16 packet_version = 0;
  3636. vector<Client*>::iterator client_itr;
  3637. MClientList.readlock(__FUNCTION__, __LINE__);
  3638. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3639. client = *client_itr;
  3640. if(client && (!packet || packet_version != client->GetVersion())){
  3641. safe_delete(packet);
  3642. safe_delete(outapp);
  3643. packet_version = client->GetVersion();
  3644. packet = configReader.getStruct("WS_Play3DSound", packet_version);
  3645. if(packet){
  3646. packet->setMediumStringByName("name", name);
  3647. packet->setDataByName("x", origin_x);
  3648. packet->setDataByName("y", origin_y);
  3649. packet->setDataByName("z", origin_z);
  3650. packet->setDataByName("unknown1", 1);
  3651. packet->setDataByName("unknown2", 2.5);
  3652. packet->setDataByName("unknown3", 15);
  3653. outapp = packet->serialize();
  3654. }
  3655. }
  3656. if(outapp && client && client->IsReadyForUpdates())
  3657. client->QueuePacket(outapp->Copy());
  3658. }
  3659. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3660. safe_delete(packet);
  3661. safe_delete(outapp);
  3662. }
  3663. }
  3664. bool ZoneServer::HasWidgetTimer(Spawn* widget){
  3665. bool ret = false;
  3666. if (widget) {
  3667. int32 id = widget->GetID();
  3668. map<int32, int32>::iterator itr;
  3669. MWidgetTimers.readlock(__FUNCTION__, __LINE__);
  3670. for (itr = widget_timers.begin(); itr != widget_timers.end(); itr++) {
  3671. if(itr->first == id){
  3672. ret = true;
  3673. break;
  3674. }
  3675. }
  3676. MWidgetTimers.releasereadlock(__FUNCTION__, __LINE__);
  3677. }
  3678. return ret;
  3679. }
  3680. void ZoneServer::CheckWidgetTimers(){
  3681. vector<int32> remove_list;
  3682. map<int32, int32>::iterator itr;
  3683. MWidgetTimers.readlock(__FUNCTION__, __LINE__);
  3684. for (itr = widget_timers.begin(); itr != widget_timers.end(); itr++) {
  3685. if(Timer::GetCurrentTime2() >= itr->second){
  3686. /*Spawn* widget = GetSpawnByID(itr->first);
  3687. if (widget && widget->IsWidget())
  3688. ((Widget*)widget)->HandleTimerUpdate();*/
  3689. remove_list.push_back(itr->first);
  3690. }
  3691. }
  3692. MWidgetTimers.releasereadlock(__FUNCTION__, __LINE__);
  3693. for (int32 i = 0; i < remove_list.size(); i++) {
  3694. Spawn* widget = GetSpawnByID(remove_list[i]);
  3695. if (widget && widget->IsWidget())
  3696. ((Widget*)widget)->HandleTimerUpdate();
  3697. }
  3698. MWidgetTimers.writelock(__FUNCTION__, __LINE__);
  3699. for(int32 i=0;i<remove_list.size(); i++)
  3700. widget_timers.erase(remove_list[i]);
  3701. MWidgetTimers.releasewritelock(__FUNCTION__, __LINE__);
  3702. }
  3703. void ZoneServer::AddWidgetTimer(Spawn* widget, float time) {
  3704. if (widget && widget->IsWidget()) {
  3705. MWidgetTimers.writelock(__FUNCTION__, __LINE__);
  3706. widget_timers[widget->GetID()] = ((int32)(time * 1000)) + Timer::GetCurrentTime2();
  3707. MWidgetTimers.releasewritelock(__FUNCTION__, __LINE__);
  3708. }
  3709. }
  3710. Spawn* ZoneServer::GetSpawnGroup(int32 id){
  3711. Spawn* ret = 0;
  3712. Spawn* spawn = 0;
  3713. if(id < 1)
  3714. return 0;
  3715. bool lookup = false;
  3716. if(quick_group_id_lookup.count(id) > 0) {
  3717. ret = GetSpawnByID(quick_group_id_lookup.Get(id));
  3718. lookup = true;
  3719. }
  3720. if(ret == NULL) {
  3721. if(lookup)
  3722. quick_group_id_lookup.erase(id);
  3723. map<int32, Spawn*>::iterator itr;
  3724. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3725. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3726. spawn = itr->second;
  3727. if(spawn){
  3728. if(spawn->GetSpawnGroupID() == id){
  3729. ret = spawn;
  3730. quick_group_id_lookup.Put(id, spawn->GetID());
  3731. break;
  3732. }
  3733. }
  3734. }
  3735. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3736. }
  3737. return ret;
  3738. }
  3739. bool ZoneServer::IsSpawnGroupAlive(int32 id){
  3740. bool ret = false;
  3741. if(id < 1)
  3742. return ret;
  3743. Spawn* spawn = GetSpawnGroup(id);
  3744. if(spawn) {
  3745. vector<Spawn*> groupMembers;
  3746. if (!spawn->IsPlayer() && spawn->HasSpawnGroup()) {
  3747. groupMembers = *spawn->GetSpawnGroup();
  3748. }
  3749. Spawn* group_spawn = 0;
  3750. vector<Spawn*>::iterator itr;
  3751. for(itr = groupMembers.begin(); itr != groupMembers.end(); itr++){
  3752. group_spawn = *itr;
  3753. if(group_spawn->Alive()) {
  3754. ret = true;
  3755. break;
  3756. }
  3757. }
  3758. }
  3759. return ret;
  3760. }
  3761. Spawn* ZoneServer::GetSpawnByLocationID(int32 location_id) {
  3762. Spawn* ret = 0;
  3763. Spawn* current_spawn = 0;
  3764. if(location_id < 1)
  3765. return 0;
  3766. bool lookup = false;
  3767. if(quick_location_id_lookup.count(location_id) > 0) {
  3768. ret = GetSpawnByID(quick_location_id_lookup.Get(location_id));
  3769. lookup = true;
  3770. }
  3771. if(ret == NULL) {
  3772. if(lookup)
  3773. quick_location_id_lookup.erase(location_id);
  3774. map<int32, Spawn*>::iterator itr;
  3775. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3776. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3777. current_spawn = itr->second;
  3778. if (current_spawn && current_spawn->GetSpawnLocationID() == location_id) {
  3779. ret = current_spawn;
  3780. quick_location_id_lookup.Put(location_id, ret->GetID());
  3781. break;
  3782. }
  3783. }
  3784. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3785. }
  3786. return ret;
  3787. }
  3788. Spawn* ZoneServer::GetSpawnByDatabaseID(int32 id){
  3789. Spawn* ret = 0;
  3790. if(id < 1)
  3791. return 0;
  3792. bool lookup = false;
  3793. if(quick_database_id_lookup.count(id) > 0) {
  3794. ret = GetSpawnByID(quick_database_id_lookup.Get(id));
  3795. lookup = true;
  3796. }
  3797. if(ret == NULL){
  3798. if(lookup)
  3799. quick_database_id_lookup.erase(id);
  3800. Spawn* spawn = 0;
  3801. map<int32, Spawn*>::iterator itr;
  3802. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3803. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++){
  3804. spawn = itr->second;
  3805. if(spawn){
  3806. if(spawn->GetDatabaseID() == id){
  3807. quick_database_id_lookup.Put(id, spawn->GetID());
  3808. ret = spawn;
  3809. break;
  3810. }
  3811. }
  3812. }
  3813. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3814. }
  3815. return ret;
  3816. }
  3817. Spawn* ZoneServer::GetSpawnByID(int32 id, bool spawnListLocked) {
  3818. Spawn* ret = 0;
  3819. if (!spawnListLocked )
  3820. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3821. if (spawn_list.count(id) > 0)
  3822. ret = spawn_list[id];
  3823. if (!spawnListLocked)
  3824. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3825. return ret;
  3826. }
  3827. bool ZoneServer::SendRemoveSpawn(Client* client, Spawn* spawn, PacketStruct* packet, bool delete_spawn)
  3828. {
  3829. if(!client || !spawn || (client && client->GetPlayer() == spawn))
  3830. return false;
  3831. if(client->GetPlayerPOVGhostSpawnID() == spawn->GetID()) {
  3832. client->SetPlayerPOVGhost(nullptr);
  3833. }
  3834. spawn->RemoveSpawnFromPlayer(client->GetPlayer());
  3835. return true;
  3836. }
  3837. void ZoneServer::SetSpawnCommand(Spawn* spawn, int8 type, char* value, Client* client){
  3838. //commands
  3839. LogWrite(MISC__TODO, 1, "TODO", "%s does nothing!\n%s, %i", __FUNCTION__, __FILE__, __LINE__);
  3840. }
  3841. void ZoneServer::SetSpawnCommand(int32 spawn_id, int8 type, char* value, Client* client){
  3842. LogWrite(MISC__TODO, 1, "TODO", "%s does nothing!\n%s, %i", __FUNCTION__, __FILE__, __LINE__);
  3843. }
  3844. void ZoneServer::ApplySetSpawnCommand(Client* client, Spawn* target, int8 type, const char* value){
  3845. // This will apply the /spawn set command to all the spawns in the zone with the same DB ID, we do not want to set
  3846. // location values (x, y, z, heading, grid) for all spawns in the zone with the same DB ID, only the targeted spawn
  3847. if(type == SPAWN_SET_VALUE_SPAWNENTRY_SCRIPT || type == SPAWN_SET_VALUE_SPAWNLOCATION_SCRIPT || (type >= SPAWN_SET_VALUE_X && type <= SPAWN_SET_VALUE_LOCATION) ||
  3848. type == SPAWN_SET_VALUE_PITCH || type == SPAWN_SET_VALUE_ROLL)
  3849. return;
  3850. Spawn* tmp = 0;
  3851. if(target->IsNPC())
  3852. tmp = GetNPC(target->GetDatabaseID());
  3853. else if(target->IsObject())
  3854. tmp = GetObject(target->GetDatabaseID());
  3855. else if(target->IsGroundSpawn())
  3856. tmp = GetGroundSpawn(target->GetDatabaseID());
  3857. else if(target->IsSign())
  3858. tmp = GetSign(target->GetDatabaseID());
  3859. else if(target->IsWidget())
  3860. tmp = GetWidget(target->GetDatabaseID());
  3861. if(tmp && type == SPAWN_SET_VALUE_SPAWN_SCRIPT)
  3862. tmp->SetSpawnScript(value);
  3863. else if(tmp)
  3864. commands.SetSpawnCommand(client, tmp, type, value); // set the master spawn
  3865. Spawn* spawn = 0;
  3866. // this check needs to be here otherwise every spawn with 0 will be set
  3867. if ( target->GetDatabaseID ( ) > 0 )
  3868. {
  3869. map<int32, Spawn*>::iterator itr;
  3870. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3871. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3872. spawn = itr->second;
  3873. if(spawn && spawn->GetDatabaseID() == target->GetDatabaseID()){
  3874. if(type == SPAWN_SET_VALUE_SPAWN_SCRIPT)
  3875. spawn->SetSpawnScript(value);
  3876. else
  3877. commands.SetSpawnCommand(client, spawn, type, value);
  3878. }
  3879. }
  3880. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3881. }
  3882. }
  3883. void ZoneServer::StopSpawnScriptTimer(Spawn* spawn, std::string functionName){
  3884. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3885. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3886. if(spawn_script_timers.size() > 0){
  3887. set<SpawnScriptTimer*>::iterator itr;
  3888. SpawnScriptTimer* timer = 0;
  3889. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); ) {
  3890. timer = *itr;
  3891. if(timer->spawn == spawn->GetID() && (functionName == "" || timer->function == functionName) && remove_spawn_script_timers_list.count(timer) == 0) {
  3892. itr = spawn_script_timers.erase(itr);
  3893. safe_delete(timer);
  3894. }
  3895. else
  3896. itr++;
  3897. }
  3898. }
  3899. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3900. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3901. }
  3902. void ZoneServer::DeleteSpawnScriptTimers(Spawn* spawn, bool all){
  3903. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3904. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3905. if(spawn_script_timers.size() > 0){
  3906. set<SpawnScriptTimer*>::iterator itr;
  3907. SpawnScriptTimer* timer = 0;
  3908. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3909. timer = *itr;
  3910. if((all || timer->spawn == spawn->GetID()) && remove_spawn_script_timers_list.count(timer) == 0)
  3911. remove_spawn_script_timers_list.insert(timer);
  3912. }
  3913. if(all)
  3914. spawn_script_timers.clear();
  3915. }
  3916. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3917. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3918. }
  3919. void ZoneServer::DeleteSpawnScriptTimers() {
  3920. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3921. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3922. if(remove_spawn_script_timers_list.size() > 0){
  3923. set<SpawnScriptTimer*>::iterator itr;
  3924. SpawnScriptTimer* timer = 0;
  3925. for (itr = remove_spawn_script_timers_list.begin(); itr != remove_spawn_script_timers_list.end(); itr++) {
  3926. timer = *itr;
  3927. set<SpawnScriptTimer*>::iterator itr2;
  3928. itr2 = spawn_script_timers.find(timer);
  3929. if(itr2 != spawn_script_timers.end())
  3930. spawn_script_timers.erase(itr2);
  3931. safe_delete(timer);
  3932. }
  3933. remove_spawn_script_timers_list.clear();
  3934. }
  3935. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3936. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3937. }
  3938. void ZoneServer::CheckSpawnScriptTimers(){
  3939. DeleteSpawnScriptTimers();
  3940. SpawnScriptTimer* timer = 0;
  3941. vector<SpawnScriptTimer> call_timers;
  3942. MSpawnScriptTimers.readlock(__FUNCTION__, __LINE__);
  3943. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3944. if(spawn_script_timers.size() > 0){
  3945. int32 current_time = Timer::GetCurrentTime2();
  3946. set<SpawnScriptTimer*>::iterator itr;
  3947. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3948. timer = *itr;
  3949. if(remove_spawn_script_timers_list.count(timer) == 0 &&
  3950. timer->current_count < timer->max_count && current_time >= timer->timer){
  3951. timer->current_count++;
  3952. SpawnScriptTimer tmpTimer;
  3953. tmpTimer.current_count = timer->current_count;
  3954. tmpTimer.function = timer->function;
  3955. tmpTimer.player = timer->player;
  3956. tmpTimer.spawn = timer->spawn;
  3957. tmpTimer.max_count = timer->max_count;
  3958. call_timers.push_back(tmpTimer);
  3959. }
  3960. if(timer->current_count >= timer->max_count && remove_spawn_script_timers_list.count(timer) == 0)
  3961. remove_spawn_script_timers_list.insert(timer);
  3962. }
  3963. }
  3964. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3965. MSpawnScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  3966. if(call_timers.size() > 0){
  3967. vector<SpawnScriptTimer>::iterator itr;
  3968. for(itr = call_timers.begin(); itr != call_timers.end(); itr++){
  3969. SpawnScriptTimer tmpTimer = (SpawnScriptTimer)*itr;
  3970. CallSpawnScript(GetSpawnByID(tmpTimer.spawn), SPAWN_SCRIPT_TIMER, tmpTimer.player > 0 ? GetSpawnByID(tmpTimer.player) : 0, tmpTimer.function.c_str());
  3971. }
  3972. }
  3973. }
  3974. void ZoneServer::KillSpawnByDistance(Spawn* spawn, float max_distance, bool include_players, bool send_packet){
  3975. if(!spawn)
  3976. return;
  3977. auto loc = glm::vec3(spawn->GetX(), spawn->GetZ(), spawn->GetY());
  3978. std::vector<int32> grids_by_radius;
  3979. if(spawn->GetMap()) {
  3980. grids_by_radius = GetGridsByLocation(spawn, loc, max_distance);
  3981. }
  3982. else {
  3983. grids_by_radius.push_back(spawn->GetLocation());
  3984. }
  3985. Spawn* test_spawn = 0;
  3986. MGridMaps.lock_shared();
  3987. std::vector<int32>::iterator grid_radius_itr;
  3988. for(grid_radius_itr = grids_by_radius.begin(); grid_radius_itr != grids_by_radius.end(); grid_radius_itr++) {
  3989. std::map<int32, GridMap*>::iterator grids = grid_maps.find((*grid_radius_itr));
  3990. if(grids != grid_maps.end()) {
  3991. grids->second->MSpawns.lock_shared();
  3992. typedef map <int32, Spawn*> SpawnMapType;
  3993. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  3994. test_spawn = it->second;
  3995. if(test_spawn && test_spawn->Alive() && test_spawn->GetID() > 0 && test_spawn->GetID() != spawn->GetID() && test_spawn->IsEntity() &&
  3996. (!test_spawn->IsPlayer() || include_players)){
  3997. if(test_spawn->GetDistance(spawn) < max_distance)
  3998. KillSpawn(true, test_spawn, spawn, send_packet);
  3999. }
  4000. }
  4001. grids->second->MSpawns.unlock_shared();
  4002. }
  4003. }
  4004. MGridMaps.unlock_shared();
  4005. }
  4006. void ZoneServer::SpawnSetByDistance(Spawn* spawn, float max_distance, string field, string value){
  4007. if(!spawn)
  4008. return;
  4009. Spawn* test_spawn = 0;
  4010. int32 type = commands.GetSpawnSetType(field);
  4011. if(type == 0xFFFFFFFF)
  4012. return;
  4013. auto loc = glm::vec3(spawn->GetX(), spawn->GetZ(), spawn->GetY());
  4014. std::vector<int32> grids_by_radius;
  4015. if(spawn->GetMap()) {
  4016. grids_by_radius = GetGridsByLocation(spawn, loc, max_distance);
  4017. }
  4018. else {
  4019. grids_by_radius.push_back(spawn->GetLocation());
  4020. }
  4021. MGridMaps.lock_shared();
  4022. std::vector<int32>::iterator grid_radius_itr;
  4023. for(grid_radius_itr = grids_by_radius.begin(); grid_radius_itr != grids_by_radius.end(); grid_radius_itr++) {
  4024. std::map<int32, GridMap*>::iterator grids = grid_maps.find((*grid_radius_itr));
  4025. grids->second->MSpawns.lock_shared();
  4026. typedef map <int32, Spawn*> SpawnMapType;
  4027. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  4028. test_spawn = it->second;
  4029. if(test_spawn && test_spawn->GetID() > 0 && test_spawn->GetID() != spawn->GetID() && !test_spawn->IsPlayer()){
  4030. if(test_spawn->GetDistance(spawn) < max_distance){
  4031. commands.SetSpawnCommand(0, test_spawn, type, value.c_str());
  4032. }
  4033. }
  4034. }
  4035. grids->second->MSpawns.unlock_shared();
  4036. }
  4037. MGridMaps.unlock_shared();
  4038. }
  4039. void ZoneServer::AddSpawnScriptTimer(SpawnScriptTimer* timer){
  4040. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  4041. spawn_script_timers.insert(timer);
  4042. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  4043. }
  4044. /*
  4045. void ZoneServer::RemoveFromRangeMap(Client* client){
  4046. spawn_range_map.erase(client);
  4047. }
  4048. */
  4049. void ZoneServer::RemoveSpawn(Spawn* spawn, bool delete_spawn, bool respawn, bool lock, bool erase_from_spawn_list, bool lock_spell_process)
  4050. {
  4051. LogWrite(ZONE__DEBUG, 3, "Zone", "Processing RemoveSpawn function for %s (%i)...", spawn->GetName(),spawn->GetID());
  4052. PacketStruct* packet = 0;
  4053. int16 packet_version = 0;
  4054. Client* client = 0;
  4055. vector<Client*>::iterator client_itr;
  4056. MClientList.readlock(__FUNCTION__, __LINE__);
  4057. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4058. client = *client_itr;
  4059. if (client && (client->GetVersion() > 373 || !client->IsZoning() || client->GetPlayer() != spawn)) { //don't send destroy ghost of 283 client when zoning
  4060. if (client->GetPlayer()->HasTarget() && client->GetPlayer()->GetTarget() == spawn)
  4061. client->GetPlayer()->SetTarget(0);
  4062. if(client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->IsSendingSpawn(spawn->GetID()))
  4063. SendRemoveSpawn(client, spawn, packet, delete_spawn);
  4064. if (spawn_range_map.count(client) > 0)
  4065. spawn_range_map.Get(client)->erase(spawn->GetID());
  4066. }
  4067. }
  4068. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4069. safe_delete(packet);
  4070. spawn->RemoveSpawnProximities();
  4071. RemoveSpawnProximities(spawn);
  4072. if (movementMgr != nullptr && spawn->IsEntity()) {
  4073. movementMgr->RemoveMob((Entity*)spawn);
  4074. }
  4075. RemoveSpawnSupportFunctions(spawn, lock_spell_process);
  4076. if (reloading)
  4077. RemoveDeadEnemyList(spawn);
  4078. if (lock)
  4079. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  4080. if (dead_spawns.count(spawn->GetID()) > 0)
  4081. dead_spawns.erase(spawn->GetID());
  4082. if (lock)
  4083. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  4084. if (spawn_expire_timers.count(spawn->GetID()) > 0)
  4085. spawn_expire_timers.erase(spawn->GetID());
  4086. spawn->SetDeletedSpawn(true);
  4087. // we will remove the spawn ptr and entry in the spawn_list later.. it is not safe right now (lua? client process? spawn process? etc? too many factors)
  4088. if(erase_from_spawn_list)
  4089. AddPendingSpawnRemove(spawn->GetID());
  4090. if(respawn && !spawn->IsPlayer() && spawn->GetSpawnLocationID() > 0) {
  4091. LogWrite(ZONE__DEBUG, 3, "Zone", "Handle NPC Respawn for '%s'.", spawn->GetName());
  4092. AddRespawn(spawn);
  4093. }
  4094. RemoveSpawnFromGrid(spawn, spawn->GetLocation());
  4095. // Do we really need the mutex locks and check to dead_spawns as we remove it from dead spawns at the start of this function
  4096. if (lock && !respawn)
  4097. MDeadSpawns.readlock(__FUNCTION__, __LINE__);
  4098. if(delete_spawn && dead_spawns.count(spawn->GetID()) == 0)
  4099. AddPendingDelete(spawn);
  4100. if (lock && !respawn)
  4101. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  4102. LogWrite(ZONE__DEBUG, 3, "Zone", "Done processing RemoveSpawn function...");
  4103. }
  4104. Spawn* ZoneServer::GetClosestSpawn(Spawn* spawn, int32 spawn_id){
  4105. Spawn* closest_spawn = 0;
  4106. Spawn* test_spawn = 0;
  4107. float closest_distance = 1000000;
  4108. float test_distance = 0;
  4109. map<int32, Spawn*>::iterator itr;
  4110. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4111. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4112. test_spawn = itr->second;
  4113. if(test_spawn && test_spawn != spawn && test_spawn->GetDatabaseID() == spawn_id){
  4114. test_distance = test_spawn->GetDistance(spawn);
  4115. if(test_distance < closest_distance){
  4116. closest_distance = test_distance;
  4117. closest_spawn = test_spawn;
  4118. }
  4119. }
  4120. }
  4121. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4122. return closest_spawn;
  4123. }
  4124. int32 ZoneServer::GetClosestLocation(Spawn* spawn){
  4125. Spawn* closest_spawn = 0;
  4126. Spawn* test_spawn = 0;
  4127. float closest_distance = 1000000;
  4128. float test_distance = 0;
  4129. map<int32, Spawn*>::iterator itr;
  4130. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4131. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4132. test_spawn = itr->second;
  4133. if(test_spawn){
  4134. test_distance = test_spawn->GetDistance(spawn);
  4135. if(test_distance < closest_distance){
  4136. closest_distance = test_distance;
  4137. closest_spawn = test_spawn;
  4138. if(closest_distance < 10)
  4139. break;
  4140. }
  4141. }
  4142. }
  4143. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4144. if(closest_spawn)
  4145. return closest_spawn->GetLocation();
  4146. return 0;
  4147. }
  4148. void ZoneServer::SendQuestUpdates(Client* client, Spawn* spawn){
  4149. if(!client)
  4150. return;
  4151. if(spawn){
  4152. if(client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  4153. SendSpawnChanges(spawn, client, false, true);
  4154. }
  4155. else{
  4156. client->GetCurrentZone()->SendAllSpawnsForVisChange(client);
  4157. }
  4158. }
  4159. void ZoneServer::SendAllSpawnsForLevelChange(Client* client) {
  4160. Spawn* spawn = 0;
  4161. if (spawn_range_map.count(client) > 0) {
  4162. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  4163. while (itr.Next()) {
  4164. spawn = GetSpawnByID(itr->first);
  4165. if (spawn && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  4166. SendSpawnChanges(spawn, client, false, true);
  4167. // Attempt to slow down the packet spam sent to the client
  4168. // who the bloody fuck put a Sleep here
  4169. //Sleep(5);
  4170. }
  4171. }
  4172. }
  4173. }
  4174. void ZoneServer::SendAllSpawnsForSeeInvisChange(Client* client) {
  4175. Spawn* spawn = 0;
  4176. if (spawn_range_map.count(client) > 0) {
  4177. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  4178. while (itr.Next()) {
  4179. spawn = GetSpawnByID(itr->first);
  4180. if (spawn && spawn->IsEntity() && (((Entity*)spawn)->IsInvis() || ((Entity*)spawn)->IsStealthed()) && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  4181. SendSpawnChanges(spawn, client, true, true);
  4182. }
  4183. }
  4184. }
  4185. }
  4186. void ZoneServer::SendAllSpawnsForVisChange(Client* client, bool limitToEntities) {
  4187. Spawn* spawn = 0;
  4188. if (spawn_range_map.count(client) > 0) {
  4189. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  4190. while (itr.Next()) {
  4191. spawn = GetSpawnByID(itr->first);
  4192. if (spawn && (!limitToEntities || (limitToEntities && spawn->IsEntity())) && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  4193. SendSpawnChanges(spawn, client, false, true);
  4194. }
  4195. }
  4196. }
  4197. }
  4198. void ZoneServer::StartZoneSpawnsForLevelThread(Client* client){
  4199. if(zoneShuttingDown)
  4200. return;
  4201. #ifdef WIN32
  4202. _beginthread(SendLevelChangedSpawns, 0, client);
  4203. #else
  4204. pthread_t thread;
  4205. pthread_create(&thread, NULL, SendLevelChangedSpawns, client);
  4206. pthread_detach(thread);
  4207. #endif
  4208. }
  4209. void ZoneServer::ReloadClientQuests(){
  4210. Client* client = 0;
  4211. vector<Client*>::iterator client_itr;
  4212. MClientList.readlock(__FUNCTION__, __LINE__);
  4213. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4214. client = *client_itr;
  4215. if(client)
  4216. client->ReloadQuests();
  4217. }
  4218. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4219. }
  4220. void ZoneServer::SendCalculatedXP(Player* player, Spawn* victim){
  4221. if (player && victim) {
  4222. if (player->GetGroupMemberInfo()) {
  4223. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  4224. PlayerGroup* group = world.GetGroupManager()->GetGroup(player->GetGroupMemberInfo()->group_id);
  4225. if (group)
  4226. {
  4227. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  4228. deque<GroupMemberInfo*>* members = group->GetMembers();
  4229. deque<GroupMemberInfo*>::iterator itr;
  4230. bool skipGrayMob = false;
  4231. for (itr = members->begin(); itr != members->end(); itr++) {
  4232. GroupMemberInfo* gmi = *itr;
  4233. if (gmi->client) {
  4234. Player* group_member = gmi->client->GetPlayer();
  4235. if(group_member && group_member->GetArrowColor(victim->GetLevel()) == ARROW_COLOR_GRAY) {
  4236. skipGrayMob = true;
  4237. break;
  4238. }
  4239. }
  4240. }
  4241. for (itr = members->begin(); !skipGrayMob && itr != members->end(); itr++) {
  4242. GroupMemberInfo* gmi = *itr;
  4243. if (gmi->client) {
  4244. Player* group_member = gmi->client->GetPlayer();
  4245. if(group_member) {
  4246. float xp = group_member->CalculateXP(victim) / members->size();
  4247. if (xp > 0) {
  4248. group_member->AddXP((int32)xp);
  4249. }
  4250. }
  4251. }
  4252. }
  4253. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  4254. }
  4255. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  4256. }
  4257. else {
  4258. float xp = player->CalculateXP(victim);
  4259. if (xp > 0) {
  4260. Client* client = ((Player*)player)->GetClient();
  4261. if(!client)
  4262. return;
  4263. player->AddXP((int32)xp);
  4264. }
  4265. }
  4266. }
  4267. }
  4268. void ZoneServer::ProcessFaction(Spawn* spawn, Client* client)
  4269. {
  4270. if(client && !spawn->IsPlayer() && spawn->GetFactionID() > 10)
  4271. {
  4272. bool update_result = false;
  4273. Faction* faction = 0;
  4274. vector<int32>* factions = 0;
  4275. Player* player = client->GetPlayer();
  4276. bool hasfaction = database.VerifyFactionID(player->GetCharacterID(), spawn->GetFactionID());
  4277. //if 0 they dont have an entry in the db for this faction. if one they do and we can skip it.
  4278. if(hasfaction == 0) {
  4279. //Find out the default for this faction
  4280. sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
  4281. //add the default faction for the player.
  4282. player->SetFactionValue(spawn->GetFactionID(), defaultfaction);
  4283. //save the character so the new default gets written to the db.
  4284. client->Save();
  4285. }
  4286. if(player->GetFactions()->ShouldDecrease(spawn->GetFactionID()))
  4287. {
  4288. update_result = player->GetFactions()->DecreaseFaction(spawn->GetFactionID());
  4289. faction = master_faction_list.GetFaction(spawn->GetFactionID());
  4290. if(faction && update_result)
  4291. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  4292. else if(faction)
  4293. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  4294. factions = master_faction_list.GetHostileFactions(spawn->GetFactionID());
  4295. if(factions)
  4296. {
  4297. vector<int32>::iterator itr;
  4298. for(itr = factions->begin(); itr != factions->end(); itr++)
  4299. {
  4300. if(player->GetFactions()->ShouldIncrease(*itr))
  4301. {
  4302. update_result = player->GetFactions()->IncreaseFaction(*itr);
  4303. faction = master_faction_list.GetFaction(*itr);
  4304. if(faction && update_result)
  4305. client->Message(CHANNEL_FACTION, "Your faction standing with %s got better.", faction->name.c_str());
  4306. else if(faction)
  4307. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any better.", faction->name.c_str());
  4308. }
  4309. }
  4310. }
  4311. }
  4312. factions = master_faction_list.GetFriendlyFactions(spawn->GetFactionID());
  4313. if(factions)
  4314. {
  4315. vector<int32>::iterator itr;
  4316. for(itr = factions->begin(); itr != factions->end(); itr++)
  4317. {
  4318. if(player->GetFactions()->ShouldDecrease(*itr))
  4319. {
  4320. bool hasfaction = database.VerifyFactionID(player->GetCharacterID(),spawn->GetFactionID());
  4321. if(hasfaction == 0) {
  4322. //they do not have the faction. Lets get the default value and feed it in.
  4323. sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
  4324. //add the default faction for the player.
  4325. player->SetFactionValue(spawn->GetFactionID(), defaultfaction);
  4326. }
  4327. update_result = player->GetFactions()->DecreaseFaction(*itr);
  4328. faction = master_faction_list.GetFaction(*itr);
  4329. if(faction && update_result)
  4330. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  4331. else if(faction)
  4332. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  4333. }
  4334. }
  4335. }
  4336. EQ2Packet* outapp = client->GetPlayer()->GetFactions()->FactionUpdate(client->GetVersion());
  4337. if(outapp)
  4338. client->QueuePacket(outapp);
  4339. }
  4340. }
  4341. void ZoneServer::Despawn(Spawn* spawn, int32 timer){
  4342. if (spawn && movementMgr != nullptr) {
  4343. movementMgr->RemoveMob((Entity*)spawn);
  4344. }
  4345. if(!spawn || spawn->IsPlayer())
  4346. return;
  4347. if(spawn->IsEntity())
  4348. ((Entity*)spawn)->InCombat(false);
  4349. if(timer == 0)
  4350. timer = 1;
  4351. AddDeadSpawn(spawn, timer);
  4352. }
  4353. void ZoneServer::KillSpawn(bool spawnListLocked, Spawn* dead, Spawn* killer, bool send_packet, int8 type, int8 damage_type, int16 kill_blow_type)
  4354. {
  4355. bool isSpell = (type == DAMAGE_PACKET_TYPE_SIPHON_SPELL || type == DAMAGE_PACKET_TYPE_SIPHON_SPELL2 ||
  4356. type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE || type == DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG ||
  4357. type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE2 || type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE3);
  4358. MDeadSpawns.readlock(__FUNCTION__, __LINE__);
  4359. if(!dead || this->dead_spawns.count(dead->GetID()) > 0) {
  4360. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  4361. return;
  4362. }
  4363. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  4364. PacketStruct* packet = 0;
  4365. Client* client = 0;
  4366. vector<int32>* encounter = 0;
  4367. int32 encounter_player_bot_count = 1;
  4368. bool killer_in_encounter = false;
  4369. int8 loot_state = dead->GetLockedNoLoot();
  4370. if(dead->IsEntity())
  4371. {
  4372. // add any special quest related loot (no_drop_quest_completed)
  4373. if(dead->IsNPC() && ((NPC*)dead)->Brain()) {
  4374. if(!((NPC*)dead)->Brain()->PlayerInEncounter() || (loot_state != ENCOUNTER_STATE_LOCKED && loot_state != ENCOUNTER_STATE_OVERMATCHED)) {
  4375. LogWrite(LOOT__DEBUG, 0, "Loot", "NPC %s bypassed loot drop due to no player in encounter, or encounter state not locked.", ((NPC*)dead)->GetName());
  4376. }
  4377. else {
  4378. Entity* hated = ((NPC*)dead)->Brain()->GetMostHated();
  4379. if(hated) {
  4380. GroupLootMethod loot_method = GroupLootMethod::METHOD_FFA;
  4381. int8 item_rarity = 0;
  4382. if(hated->GetGroupMemberInfo()) {
  4383. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  4384. PlayerGroup* group = world.GetGroupManager()->GetGroup(hated->GetGroupMemberInfo()->group_id);
  4385. if (group) {
  4386. loot_method = (GroupLootMethod)group->GetGroupOptions()->loot_method;
  4387. item_rarity = group->GetGroupOptions()->loot_items_rarity;
  4388. LogWrite(LOOT__DEBUG, 0, "Loot", "%s: Loot method set to %u.", dead->GetName(), loot_method);
  4389. }
  4390. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  4391. }
  4392. AddLoot((NPC*)dead, hated, loot_method, item_rarity, hated->GetGroupMemberInfo() ? hated->GetGroupMemberInfo()->group_id : 0);
  4393. }
  4394. }
  4395. }
  4396. ((Entity*)dead)->InCombat(false);
  4397. dead->SetInitialState(16512, false); // This will make aerial npc's fall after death
  4398. dead->SetHP(0);
  4399. dead->SetSpawnType(3);
  4400. dead->appearance.attackable = 0;
  4401. // Remove hate towards dead from all npc's in the zone
  4402. ClearHate((Entity*)dead);
  4403. // Check kill and death procs
  4404. if (killer && dead != killer){
  4405. if (dead->IsEntity())
  4406. ((Entity*)dead)->CheckProcs(PROC_TYPE_DEATH, killer);
  4407. if (killer->IsEntity())
  4408. ((Entity*)killer)->CheckProcs(PROC_TYPE_KILL, dead);
  4409. }
  4410. //Check if caster is alive after death proc called, incase of deathsave
  4411. if (dead->Alive())
  4412. return;
  4413. RemoveSpellTimersFromSpawn(dead, true, !dead->IsPlayer(), true, !isSpell);
  4414. ((Entity*)dead)->IsCasting(false);
  4415. if(dead->IsPlayer())
  4416. {
  4417. ((Player*)dead)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_DEATHS, 1);
  4418. client = ((Player*)dead)->GetClient();
  4419. ((Entity*)dead)->HandleDeathExperienceDebt(killer);
  4420. if(client) {
  4421. if(client->GetPlayer()->DamageEquippedItems(10, client))
  4422. client->QueuePacket(client->GetPlayer()->GetEquipmentList()->serialize(client->GetVersion(), client->GetPlayer()));
  4423. client->DisplayDeadWindow();
  4424. }
  4425. }
  4426. else if (dead->IsNPC()) {
  4427. encounter = ((NPC*)dead)->Brain()->GetEncounter();
  4428. encounter_player_bot_count = ((NPC*)dead)->Brain()->CountPlayerBotInEncounter();
  4429. if(encounter_player_bot_count < 1)
  4430. encounter_player_bot_count = 1;
  4431. }
  4432. }
  4433. dead->SetActionState(0);
  4434. dead->SetTempActionState(0);
  4435. // Needs npc to have access to the encounter list for who is allowed to loot
  4436. NPC* chest = 0;
  4437. if (dead->IsNPC() && !((NPC*)dead)->Brain()->PlayerInEncounter()) {
  4438. dead->SetLootCoins(0);
  4439. dead->ClearLoot();
  4440. }
  4441. Spawn* groupMemberAlive = nullptr;
  4442. // If dead has loot attempt to drop a chest
  4443. if (dead->HasLoot()) {
  4444. if(!(groupMemberAlive = dead->IsSpawnGroupMembersAlive(dead))) {
  4445. chest = ((Entity*)dead)->DropChest();
  4446. }
  4447. else {
  4448. switch(dead->GetLootDropType()) {
  4449. case 0:
  4450. // default drop all chest type as a group
  4451. dead->TransferLoot(groupMemberAlive);
  4452. break;
  4453. case 1:
  4454. // this is a primary mob it drops its own loot
  4455. chest = ((Entity*)dead)->DropChest();
  4456. break;
  4457. }
  4458. }
  4459. }
  4460. // If dead is an npc get the encounter and loop through it giving out the rewards, no rewards for pets
  4461. if (dead->IsNPC() && !dead->IsPet() && !dead->IsBot()) {
  4462. Spawn* spawn = 0;
  4463. int8 size = encounter->size();
  4464. for (int8 i = 0; i < encounter->size(); i++) {
  4465. spawn = GetSpawnByID(encounter->at(i), spawnListLocked);
  4466. // set a flag to let us know if the killer is in the encounter
  4467. if (!killer_in_encounter && spawn == killer)
  4468. killer_in_encounter = true;
  4469. if (spawn && spawn->IsPlayer()) {
  4470. // Update players total kill count
  4471. ((Player*)spawn)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_NPC_KILLS, 1);
  4472. // If this was an epic mob kill send the announcement for this player
  4473. if (dead->GetDifficulty() >= 10)
  4474. SendEpicMobDeathToGuild((Player*)spawn, dead);
  4475. // Clear hostile spells from the players spell queue
  4476. spellProcess->RemoveSpellFromQueue((Player*)spawn, true);
  4477. // Get the client of the player
  4478. client = ((Player*)spawn)->GetClient();
  4479. // valid client?
  4480. if (client) {
  4481. // Check for quest kill updates
  4482. if(!dead->IsNPC() || loot_state != ENCOUNTER_STATE_BROKEN) {
  4483. client->CheckPlayerQuestsKillUpdate(dead);
  4484. }
  4485. // If the dead mob is not a player and if it had a faction with an ID greater or equal to 10 the send faction changes
  4486. if (!dead->IsPlayer() && dead->GetFactionID() > 10)
  4487. ProcessFaction(dead, client);
  4488. // Send xp...this is currently wrong fix it
  4489. if (spawn != dead && ((Player*)spawn)->GetArrowColor(dead->GetLevel()) >= ARROW_COLOR_GREEN) {
  4490. //SendCalculatedXP((Player*)spawn, dead);
  4491. float xp = ((Player*)spawn)->CalculateXP(dead) / encounter_player_bot_count;
  4492. if (xp > 0) {
  4493. ((Player*)spawn)->AddXP((int32)xp);
  4494. }
  4495. }
  4496. }
  4497. }
  4498. // If a chest is being dropped add this spawn to the chest's encounter so they can loot it
  4499. if (chest && spawn && spawn->IsEntity())
  4500. chest->Brain()->AddToEncounter((Entity*)spawn);
  4501. }
  4502. }
  4503. // If a chest is being dropped add it to the world and set the timer to remove it.
  4504. if (chest) {
  4505. AddSpawn(chest);
  4506. AddDeadSpawn(chest, 0xFFFFFFFF);
  4507. LogWrite(LOOT__DEBUG, 0, "Loot", "Adding a chest to the world...");
  4508. }
  4509. // Reset client pointer
  4510. client = 0;
  4511. // Killer was not in the encounter, give them the faction hit but no xp
  4512. if (!killer_in_encounter) {
  4513. // make sure the killer is a player and the dead spawn had a faction and wasn't a player
  4514. if (killer && killer->IsPlayer()) {
  4515. if (!dead->IsPlayer() && dead->GetFactionID() > 10) {
  4516. client = ((Player*)killer)->GetClient();
  4517. if (client)
  4518. ProcessFaction(dead, client);
  4519. }
  4520. // Clear hostile spells from the killers spell queue
  4521. spellProcess->RemoveSpellFromQueue((Player*)killer, true);
  4522. }
  4523. }
  4524. // Reset client pointer
  4525. client = 0;
  4526. vector<Spawn*>* group = dead->GetSpawnGroup();
  4527. if (group && group->size() == 1)
  4528. CallSpawnScript(dead, SPAWN_SCRIPT_GROUP_DEAD, killer);
  4529. safe_delete(group);
  4530. // Remove the support functions for the dead spawn
  4531. RemoveSpawnSupportFunctions(dead, !isSpell);
  4532. // Erase the expire timer if it has one
  4533. if (spawn_expire_timers.count(dead->GetID()) > 0)
  4534. spawn_expire_timers.erase(dead->GetID());
  4535. // If dead is an npc or object call the spawn scrip and handle instance stuff
  4536. if(dead->IsNPC() || dead->IsObject())
  4537. {
  4538. // handle instance spawn db info
  4539. // we don't care if a NPC or a client kills the spawn, we could have events that cause NPCs to kill NPCs.
  4540. if(dead->GetZone()->GetInstanceID() > 0 && dead->GetSpawnLocationID() > 0)
  4541. {
  4542. // use respawn time to either insert/update entry (likely insert in this situation)
  4543. if(dead->IsNPC())
  4544. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4545. else if ( dead->IsObject ( ) )
  4546. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4547. }
  4548. else if(!groupMemberAlive && dead->GetSpawnLocationID() > 0) {
  4549. if(dead->IsNPC())
  4550. database.CreatePersistedRespawn(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC,dead->GetRespawnTime(),GetZoneID());
  4551. else if(dead->IsObject())
  4552. database.CreatePersistedRespawn(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT,dead->GetRespawnTime(),GetZoneID());
  4553. }
  4554. // Call the spawn scripts death() function
  4555. CallSpawnScript(dead, SPAWN_SCRIPT_DEATH, killer);
  4556. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  4557. if (zone_script && lua_interface)
  4558. lua_interface->RunZoneScript(zone_script, "spawn_killed", this, dead, 0, 0, killer);
  4559. }
  4560. int32 victim_id = dead->GetID();
  4561. int32 attacker_id = 0xFFFFFFFF;
  4562. if(killer)
  4563. attacker_id = killer->GetID();
  4564. if(send_packet)
  4565. {
  4566. vector<Client*>::iterator client_itr;
  4567. MClientList.readlock(__FUNCTION__, __LINE__);
  4568. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4569. client = *client_itr;
  4570. if(!client->GetPlayer()->WasSentSpawn(victim_id) || (attacker_id != 0xFFFFFFFF && !client->GetPlayer()->WasSentSpawn(attacker_id)) )
  4571. continue;
  4572. else if(killer && killer->GetDistance(client->GetPlayer()) > HEAR_SPAWN_DISTANCE)
  4573. continue;
  4574. packet = configReader.getStruct("WS_HearDeath", client->GetVersion());
  4575. if(packet)
  4576. {
  4577. if(killer)
  4578. packet->setDataByName("attacker", client->GetPlayer()->GetIDWithPlayerSpawn(killer));
  4579. else
  4580. packet->setDataByName("attacker", 0xFFFFFFFF);
  4581. packet->setDataByName("defender", client->GetPlayer()->GetIDWithPlayerSpawn(dead));
  4582. packet->setDataByName("damage_type", damage_type);
  4583. packet->setDataByName("blow_type", kill_blow_type);
  4584. client->QueuePacket(packet->serialize());
  4585. LogWrite(COMBAT__DEBUG, 0, "Combat", "Zone Killing of '%s' by '%s' damage type %u, blow type %u", dead->GetName(), killer ? killer->GetName() : "", damage_type, kill_blow_type);
  4586. safe_delete(packet);
  4587. }
  4588. }
  4589. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4590. }
  4591. int32 pop_timer = 0xFFFFFFFF;
  4592. if(killer && killer->IsNPC())
  4593. {
  4594. // Call the spawn scripts killed() function
  4595. CallSpawnScript(killer, SPAWN_SCRIPT_KILLED, dead);
  4596. if(!dead->IsPlayer())
  4597. {
  4598. LogWrite(MISC__TODO, 1, "TODO", "Whenever pets are added, check for pet kills\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  4599. // Set the time for the corpse to linger to 5 sec
  4600. //pop_timer = 5000;
  4601. // commented out the timer so in the event the killer is not a player (pet, guard, something else i haven't thought of)
  4602. // the corpse doesn't vanish sooner then it should if it had loot for the players. AddDeadSpawn() will set timers based on if
  4603. // the corpse has loot or not if the timer value (pop_timer) is 0xFFFFFFFF
  4604. }
  4605. }
  4606. // If the dead spawns was not a player add it to the dead spawn list
  4607. if (!dead->IsPlayer() && !dead->IsBot())
  4608. AddDeadSpawn(dead, pop_timer);
  4609. // if dead was a player clear hostile spells from its spell queue
  4610. if (dead->IsPlayer())
  4611. spellProcess->RemoveSpellFromQueue((Player*)dead, true);
  4612. if (dead->IsNPC())
  4613. ((NPC*)dead)->Brain()->ClearHate();
  4614. safe_delete(encounter);
  4615. }
  4616. void ZoneServer::SendDamagePacket(Spawn* attacker, Spawn* victim, int8 type1, int8 type2, int8 damage_type, int16 damage, const char* spell_name) {
  4617. //Scat: was set but never being used anywhere. i see references to 0xFFFFFFFF below so could be old code not used anymore
  4618. //int32 attacker_id = 0xFFFFFFFF;
  4619. //if(attacker)
  4620. // attacker_id = attacker->GetID();
  4621. PacketStruct* packet = 0;
  4622. Client* client = 0;
  4623. if (attacker && victim && victim->IsPlayer() && victim->GetTarget() == 0) {
  4624. client = ((Player*)victim)->GetClient();
  4625. if (client)
  4626. client->TargetSpawn(attacker);
  4627. }
  4628. if(damage_type == DAMAGE_PACKET_DAMAGE_TYPE_FOCUS) {
  4629. damage_type = 0;
  4630. type2 = DAMAGE_PACKET_RESULT_FOCUS;
  4631. }
  4632. vector<Client*>::iterator client_itr;
  4633. MClientList.readlock(__FUNCTION__, __LINE__);
  4634. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4635. client = *client_itr;
  4636. if (!client || (client->GetPlayer() != attacker && client->GetPlayer() != victim && ((attacker && client->GetPlayer()->WasSentSpawn(attacker->GetID()) == false) || (victim && client->GetPlayer()->WasSentSpawn(victim->GetID()) == false))))
  4637. continue;
  4638. if (attacker && attacker->GetDistance(client->GetPlayer()) > 50)
  4639. continue;
  4640. if (victim && victim->GetDistance(client->GetPlayer()) > 50)
  4641. continue;
  4642. int8 mod_type1 = type1;
  4643. if(client->GetVersion() <= 561) {
  4644. mod_type1 = DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE;
  4645. }
  4646. switch (mod_type1) {
  4647. case DAMAGE_PACKET_TYPE_SIPHON_SPELL:
  4648. case DAMAGE_PACKET_TYPE_SIPHON_SPELL2:
  4649. packet = configReader.getStruct("WS_HearSiphonSpellDamage", client->GetVersion());
  4650. break;
  4651. case DAMAGE_PACKET_TYPE_MULTIPLE_DAMAGE:
  4652. if (client->GetVersion() > 561)
  4653. packet = configReader.getStruct("WS_HearMultipleDamage", client->GetVersion());
  4654. else
  4655. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4656. break;
  4657. case DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG:
  4658. case DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE:
  4659. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4660. break;
  4661. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE2:
  4662. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE3:
  4663. case DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG:
  4664. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE:
  4665. if (client->GetVersion() > 561)
  4666. packet = configReader.getStruct("WS_HearSpellDamage", client->GetVersion());
  4667. else
  4668. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4669. if (packet)
  4670. packet->setSubstructDataByName("header", "unknown", 5);
  4671. break;
  4672. case DAMAGE_PACKET_TYPE_RANGE_DAMAGE:
  4673. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4674. break;
  4675. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG:
  4676. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG2:
  4677. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4678. break;
  4679. default:
  4680. LogWrite(ZONE__ERROR, 0, "Zone", "Unknown Damage Packet type: %i in ZoneServer::SendDamagePacket.", type1);
  4681. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4682. return;
  4683. }
  4684. if (packet) {
  4685. if (client->GetVersion() > 561) {
  4686. packet->setSubstructDataByName("header", "packet_type", type1);
  4687. packet->setSubstructDataByName("header", "result_type", type2);
  4688. packet->setDataByName("damage_type", damage_type);
  4689. packet->setDataByName("damage", damage);
  4690. }
  4691. else {
  4692. switch (type2) {
  4693. case DAMAGE_PACKET_RESULT_MISS:
  4694. packet->setSubstructDataByName("header", "result_type", 1);
  4695. break;
  4696. case DAMAGE_PACKET_RESULT_DODGE:
  4697. packet->setSubstructDataByName("header", "result_type", 2);
  4698. break;
  4699. case DAMAGE_PACKET_RESULT_PARRY:
  4700. packet->setSubstructDataByName("header", "result_type", 3);
  4701. break;
  4702. case DAMAGE_PACKET_RESULT_RIPOSTE:
  4703. packet->setSubstructDataByName("header", "result_type", 4);
  4704. break;
  4705. case DAMAGE_PACKET_RESULT_BLOCK:
  4706. packet->setSubstructDataByName("header", "result_type", 5);
  4707. break;
  4708. case DAMAGE_PACKET_RESULT_INVULNERABLE:
  4709. packet->setSubstructDataByName("header", "result_type", 7);
  4710. break;
  4711. case DAMAGE_PACKET_RESULT_RESIST:
  4712. packet->setSubstructDataByName("header", "result_type", 9);
  4713. break;
  4714. case DAMAGE_PACKET_RESULT_REFLECT:
  4715. packet->setSubstructDataByName("header", "result_type", 10);
  4716. break;
  4717. case DAMAGE_PACKET_RESULT_IMMUNE:
  4718. packet->setSubstructDataByName("header", "result_type", 11);
  4719. break;
  4720. }
  4721. packet->setArrayLengthByName("num_dmg", 1);
  4722. packet->setSubstructDataByName("header", "defender_proxy", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4723. packet->setArrayDataByName("damage_type", damage_type);
  4724. packet->setArrayDataByName("damage", damage);
  4725. }
  4726. if (!attacker)
  4727. packet->setSubstructDataByName("header", "attacker", 0xFFFFFFFF);
  4728. else
  4729. packet->setSubstructDataByName("header", "attacker", client->GetPlayer()->GetIDWithPlayerSpawn(attacker));
  4730. packet->setSubstructDataByName("header", "defender", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4731. if (spell_name) {
  4732. packet->setDataByName("spell", 1);
  4733. packet->setDataByName("spell_name", spell_name);
  4734. }
  4735. EQ2Packet* app = packet->serialize();
  4736. //DumpPacket(app);
  4737. client->QueuePacket(app);
  4738. safe_delete(packet);
  4739. packet = 0;
  4740. }
  4741. }
  4742. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4743. }
  4744. void ZoneServer::SendHealPacket(Spawn* caster, Spawn* target, int16 heal_type, int32 heal_amt, const char* spell_name){
  4745. Client* client = 0;
  4746. vector<Client*>::iterator client_itr;
  4747. MClientList.readlock(__FUNCTION__, __LINE__);
  4748. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4749. client = *client_itr;
  4750. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4751. continue;
  4752. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4753. continue;
  4754. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4755. continue;
  4756. PacketStruct* packet = configReader.getStruct("WS_HearHeal", client->GetVersion());
  4757. if (packet) {
  4758. packet->setDataByName("caster", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4759. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4760. packet->setDataByName("heal_amt", heal_amt);
  4761. packet->setDataByName("spellname", spell_name);
  4762. packet->setDataByName("type", heal_type);
  4763. packet->setDataByName("unknown2", 1);
  4764. EQ2Packet* app = packet->serialize();
  4765. client->QueuePacket(app);
  4766. safe_delete(packet);
  4767. }
  4768. }
  4769. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4770. }
  4771. void ZoneServer::SendThreatPacket(Spawn* caster, Spawn* target, int32 threat_amt, const char* spell_name) {
  4772. Client* client = 0;
  4773. vector<Client*>::iterator client_itr;
  4774. MClientList.readlock(__FUNCTION__, __LINE__);
  4775. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4776. client = *client_itr;
  4777. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4778. continue;
  4779. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4780. continue;
  4781. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4782. continue;
  4783. if(client->GetVersion() <= 561) {
  4784. int8 channel = 46;
  4785. if(client->GetPlayer() == caster || client->GetPlayer() == target)
  4786. channel = 42;
  4787. client->Message(channel, "%s increases %s hate with %s for %u threat.", spell_name, (client->GetPlayer() == caster) ? "YOUR" : caster->GetName(), (client->GetPlayer() == target) ? "YOU" : target->GetName(), threat_amt);
  4788. }
  4789. else {
  4790. PacketStruct* packet = configReader.getStruct("WS_HearThreatCmd", client->GetVersion());
  4791. if (packet) {
  4792. packet->setDataByName("spell_name", spell_name);
  4793. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4794. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4795. packet->setDataByName("threat_amount", threat_amt);
  4796. client->QueuePacket(packet->serialize());
  4797. }
  4798. safe_delete(packet);
  4799. }
  4800. }
  4801. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4802. }
  4803. void ZoneServer::SendYellPacket(Spawn* yeller, float max_distance) {
  4804. Client* client = 0;
  4805. string yellMsg = std::string(yeller->GetName()) + " yelled for help!";
  4806. vector<Client*>::iterator client_itr;
  4807. PacketStruct* packet = nullptr;
  4808. MClientList.readlock(__FUNCTION__, __LINE__);
  4809. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4810. client = *client_itr;
  4811. if(!client || !client->GetPlayer() || client->GetPlayer()->WasSentSpawn(yeller->GetID()) == false)
  4812. continue;
  4813. if(client->GetPlayer()->GetDistance(yeller) > max_distance)
  4814. continue;
  4815. if(packet && packet->GetVersion() == client->GetVersion()) {
  4816. client->QueuePacket(packet->serialize());
  4817. }
  4818. else {
  4819. safe_delete(packet);
  4820. packet = configReader.getStruct("WS_EncounterBroken", client->GetVersion());
  4821. if (packet) {
  4822. packet->setDataByName("message", yellMsg.c_str());
  4823. /* none of the other data seems necessary, keeping for reference for future disassembly
  4824. packet2->setDataByName("unknown2", 0x40);
  4825. packet2->setDataByName("unknown3", 0x40);
  4826. packet2->setDataByName("unknown4", 0xFF);
  4827. packet2->setDataByName("unknown5", 0xFF);
  4828. packet2->setDataByName("unknown6", 0xFF);*/
  4829. client->QueuePacket(packet->serialize());
  4830. }
  4831. }
  4832. }
  4833. safe_delete(packet);
  4834. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4835. }
  4836. void ZoneServer::SendSpellFailedPacket(Client* client, int16 error){
  4837. if(!client)
  4838. return;
  4839. PacketStruct* packet = configReader.getStruct("WS_DisplaySpellFailed", client->GetVersion());
  4840. if(packet){
  4841. /* Temp solution, need to modify the error code before this function and while we still have access to the spell/combat art */
  4842. error = master_spell_list.GetSpellErrorValue(client->GetVersion(), error);
  4843. if(client->GetVersion() > 373 && client->GetVersion() <= 561 && error) {
  4844. error += 1;
  4845. }
  4846. packet->setDataByName("error_code", error);
  4847. //packet->PrintPacket();
  4848. client->QueuePacket(packet->serialize());
  4849. safe_delete(packet);
  4850. }
  4851. }
  4852. void ZoneServer::SendInterruptPacket(Spawn* interrupted, LuaSpell* spell, bool fizzle){
  4853. if(!interrupted || !spell)
  4854. return;
  4855. EQ2Packet* outapp = 0;
  4856. PacketStruct* packet = 0;
  4857. Client* client = 0;
  4858. vector<Client*>::iterator client_itr;
  4859. MClientList.readlock(__FUNCTION__, __LINE__);
  4860. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4861. client = *client_itr;
  4862. if(!client || !client->GetPlayer()->WasSentSpawn(interrupted->GetID()))
  4863. continue;
  4864. packet = configReader.getStruct(fizzle ? "WS_SpellFizzle" : "WS_Interrupt", client->GetVersion());
  4865. if(packet){
  4866. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(interrupted));
  4867. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4868. for (int32 i = 0; i < spell->targets.size(); i++)
  4869. packet->setArrayDataByName("target_id", client->GetPlayer()->GetIDWithPlayerSpawn(client->GetPlayer()->GetZone()->GetSpawnByID(spell->targets[i])), i);
  4870. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4871. outapp = packet->serialize();
  4872. client->QueuePacket(outapp);
  4873. safe_delete(packet);
  4874. }
  4875. }
  4876. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4877. safe_delete(packet);
  4878. }
  4879. void ZoneServer::SendCastSpellPacket(LuaSpell* spell, Entity* caster, int32 spell_visual_override, int16 casttime_override){
  4880. EQ2Packet* outapp = 0;
  4881. PacketStruct* packet = 0;
  4882. Client* client = 0;
  4883. if(!caster || !spell || !spell->spell || spell->interrupted)
  4884. return;
  4885. if(spell->is_damage_spell && (!spell->has_damaged || spell->resisted)) {
  4886. // we did not successfully hit target, so we should not send the visual
  4887. return;
  4888. }
  4889. vector<Client*>::iterator client_itr;
  4890. MClientList.readlock(__FUNCTION__, __LINE__);
  4891. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4892. client = *client_itr;
  4893. if(!client)
  4894. continue;
  4895. packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4896. if(packet){
  4897. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4898. if(!caster_id) {
  4899. safe_delete(packet);
  4900. continue;
  4901. }
  4902. packet->setDataByName("spawn_id", caster_id);
  4903. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4904. for (int32 i = 0; i < spell->targets.size(); i++) {
  4905. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(spell->caster->GetZone()->GetSpawnByID(spell->targets[i]));
  4906. if(target_id) {
  4907. packet->setArrayDataByName("target", target_id, i);
  4908. }
  4909. else {
  4910. packet->setArrayDataByName("target", 0xFFFFFFFF, i);
  4911. }
  4912. }
  4913. int32 visual_to_use = spell_visual_override > 0 ? spell_visual_override : spell->spell->GetSpellData()->spell_visual;
  4914. int32 visual = client->GetSpellVisualOverride(visual_to_use);
  4915. packet->setDataByName("spell_visual", visual); //result
  4916. if(casttime_override != 0xFFFF) {
  4917. packet->setDataByName("cast_time", casttime_override*.01f); //delay
  4918. }
  4919. else {
  4920. packet->setDataByName("cast_time", spell->spell->GetSpellData()->cast_time*.01f); //delay
  4921. }
  4922. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4923. packet->setDataByName("spell_level", 1);
  4924. packet->setDataByName("spell_tier", spell->spell->GetSpellData()->tier);
  4925. outapp = packet->serialize();
  4926. client->QueuePacket(outapp);
  4927. safe_delete(packet);
  4928. }
  4929. }
  4930. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4931. safe_delete(packet);
  4932. }
  4933. void ZoneServer::SendCastSpellPacket(int32 spell_visual, Spawn* target, Spawn* caster) {
  4934. if (target) {
  4935. vector<Client*>::iterator client_itr;
  4936. MClientList.readlock(__FUNCTION__, __LINE__);
  4937. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4938. Client* client = *client_itr;
  4939. if (!client)
  4940. continue;
  4941. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4942. if (packet) {
  4943. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4944. if(!target_id) { // client is not aware of spawn
  4945. safe_delete(packet);
  4946. continue;
  4947. }
  4948. if (!caster) {
  4949. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  4950. }
  4951. else {
  4952. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4953. if(!caster_id) { // client is not aware of spawn
  4954. safe_delete(packet);
  4955. continue;
  4956. }
  4957. packet->setDataByName("spawn_id", caster_id);
  4958. }
  4959. packet->setArrayLengthByName("num_targets", 1);
  4960. packet->setArrayDataByName("target", target_id);
  4961. int32 visual = client->GetSpellVisualOverride(spell_visual);
  4962. packet->setDataByName("spell_visual", visual);
  4963. packet->setDataByName("cast_time", 0);
  4964. packet->setDataByName("spell_id", 0);
  4965. packet->setDataByName("spell_level", 0);
  4966. packet->setDataByName("spell_tier", 1);
  4967. client->QueuePacket(packet->serialize());
  4968. safe_delete(packet);
  4969. }
  4970. }
  4971. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4972. }
  4973. }
  4974. void ZoneServer::SendCastEntityCommandPacket(EntityCommand* entity_command, int32 spawn_id, int32 target_id) {
  4975. if (entity_command) {
  4976. Spawn* spawn = GetSpawnByID(spawn_id);
  4977. Spawn* target = GetSpawnByID(target_id);
  4978. if (!spawn || !target)
  4979. return;
  4980. Client* client = 0;
  4981. vector<Client*>::iterator client_itr;
  4982. MClientList.readlock(__FUNCTION__, __LINE__);
  4983. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4984. client = *client_itr;
  4985. if (!client || !client->GetPlayer()->WasSentSpawn(spawn_id) || !client->GetPlayer()->WasSentSpawn(target_id))
  4986. continue;
  4987. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4988. if (packet) {
  4989. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  4990. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4991. if(!caster_id || !target_id)
  4992. continue;
  4993. packet->setDataByName("spawn_id", caster_id);
  4994. packet->setArrayLengthByName("num_targets", 1);
  4995. packet->setArrayDataByName("target", target_id);
  4996. packet->setDataByName("num_targets", 1);
  4997. packet->setDataByName("spell_visual", entity_command->spell_visual); //result
  4998. packet->setDataByName("cast_time", entity_command->cast_time * 0.01); //delay
  4999. packet->setDataByName("spell_id", 1);
  5000. packet->setDataByName("spell_level", 1);
  5001. packet->setDataByName("spell_tier", 1);
  5002. EQ2Packet* outapp = packet->serialize();
  5003. client->QueuePacket(outapp);
  5004. safe_delete(packet);
  5005. }
  5006. }
  5007. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5008. }
  5009. }
  5010. void ZoneServer::StartZoneInitialSpawnThread(Client* client){
  5011. if(zoneShuttingDown)
  5012. return;
  5013. #ifdef WIN32
  5014. _beginthread(SendInitialSpawns, 0, client);
  5015. #else
  5016. pthread_t thread;
  5017. pthread_create(&thread, NULL, SendInitialSpawns, client);
  5018. pthread_detach(thread);
  5019. #endif
  5020. }
  5021. void ZoneServer::SendZoneSpawns(Client* client){
  5022. int8 count = 0;
  5023. while (LoadingData && count <= 6000) { //sleep for max of 60 seconds (60000ms) while the maps are loading
  5024. count++;
  5025. Sleep(10);
  5026. }
  5027. count = 0;
  5028. int16 size = 0;
  5029. //give the spawn thread a tad bit of time to add the pending_spawns to spawn_list (up to 10 seconds)
  5030. while (count < 1000) {
  5031. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  5032. size = pending_spawn_list_add.size();
  5033. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  5034. if (size == 0)
  5035. break;
  5036. Sleep(10);
  5037. count++;
  5038. }
  5039. initial_spawn_threads_active++;
  5040. map<int32, Spawn*>::iterator itr;
  5041. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5042. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5043. Spawn* spawn = itr->second;
  5044. if (spawn) {
  5045. if(spawn == client->GetPlayer() && (client->IsReloadingZone() || client->GetPlayer()->IsReturningFromLD()))
  5046. {
  5047. if(!client->GetPlayer()->SetSpawnMap(spawn))
  5048. continue;
  5049. }
  5050. CheckSpawnRange(client, spawn, true);
  5051. }
  5052. }
  5053. CheckSendSpawnToClient(client, true);
  5054. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5055. client->SetConnected(true);
  5056. ClientPacketFunctions::SendFinishedEntitiesList(client);
  5057. initial_spawn_threads_active--;
  5058. }
  5059. vector<Entity*> ZoneServer::GetPlayers(){
  5060. vector<Entity*> ret;
  5061. Client* client = 0;
  5062. vector<Client*>::iterator client_itr;
  5063. MClientList.readlock(__FUNCTION__, __LINE__);
  5064. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  5065. client = *client_itr;
  5066. ret.push_back(client->GetPlayer());
  5067. }
  5068. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5069. return ret;
  5070. }
  5071. int16 ZoneServer::SetSpawnTargetable(Spawn* spawn, float distance){
  5072. Spawn* test_spawn = 0;
  5073. int16 ret_val = 0;
  5074. map<int32, Spawn*>::iterator itr;
  5075. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5076. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5077. test_spawn = itr->second;
  5078. if(test_spawn){
  5079. if(test_spawn->GetDistance(spawn) <= distance){
  5080. test_spawn->SetTargetable(1);
  5081. ret_val++;
  5082. }
  5083. }
  5084. }
  5085. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5086. return ret_val;
  5087. }
  5088. int16 ZoneServer::SetSpawnTargetable(int32 spawn_id){
  5089. Spawn* spawn = 0;
  5090. int16 ret_val = 0;
  5091. map<int32, Spawn*>::iterator itr;
  5092. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5093. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5094. spawn = itr->second;
  5095. if(spawn){
  5096. if(spawn->GetDatabaseID() == spawn_id){
  5097. spawn->SetTargetable(1);
  5098. ret_val++;
  5099. }
  5100. }
  5101. }
  5102. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5103. return ret_val;
  5104. }
  5105. ZoneInfoSlideStruct* ZoneServer::GenerateSlideStruct(float unknown1a, float unknown1b, int32 unknown2a, int32 unknown2b, int32 unknown3, int32 unknown4, const char* slide, const char* voiceover, int32 key1, int32 key2) {
  5106. ZoneInfoSlideStructInfo* info = new ZoneInfoSlideStructInfo();
  5107. memset(info, 0, sizeof(ZoneInfoSlideStructInfo));
  5108. info->unknown1[0] = unknown1a;
  5109. info->unknown1[1] = unknown1b;
  5110. info->unknown2[0] = unknown2a;
  5111. info->unknown2[1] = unknown2b;
  5112. info->unknown3 = unknown3;
  5113. info->unknown4 = unknown4;
  5114. int8 length = strlen(slide);
  5115. if (length >= 128)
  5116. length = 127;
  5117. strncpy(info->slide, slide, length);
  5118. length = strlen(voiceover);
  5119. if (length >= 128)
  5120. length = 127;
  5121. strncpy(info->voiceover, voiceover, length);
  5122. info->key1 = key1;
  5123. info->key2 = key2;
  5124. ZoneInfoSlideStruct* ret = new ZoneInfoSlideStruct();
  5125. ret->info = info;
  5126. return ret;
  5127. }
  5128. void ZoneServer::AddZoneInfoSlideStructTransitionInfo(ZoneInfoSlideStruct* info, int32 x, int32 y, float zoom, float transition_time) {
  5129. ZoneInfoSlideStructTransitionInfo* transition_info = new ZoneInfoSlideStructTransitionInfo();
  5130. transition_info->transition_x = x;
  5131. transition_info->transition_y = y;
  5132. transition_info->transition_zoom = zoom;
  5133. transition_info->transition_time = transition_time;
  5134. info->slide_transition_info.push_back(transition_info);
  5135. }
  5136. vector<ZoneInfoSlideStruct*>* ZoneServer::GenerateTutorialSlides() {
  5137. vector<ZoneInfoSlideStruct*>* slides = new vector<ZoneInfoSlideStruct*>();
  5138. ZoneInfoSlideStruct* slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt01_final001.dds", "voiceover/english/antonia_intro/antonia_intro_001_64.mp3", 2519553957, 1010319376);
  5139. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  5140. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 16);
  5141. slides->push_back(slide);
  5142. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt02_final001.dds", "voiceover/english/antonia_intro/antonia_intro_002_64.mp3", 567178266, 3055063399);
  5143. AddZoneInfoSlideStructTransitionInfo(slide, 600, 365, 1.60000002384186, 0);
  5144. AddZoneInfoSlideStructTransitionInfo(slide, 800, 370, 1.39999997615814, 9);
  5145. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 8);
  5146. slides->push_back(slide);
  5147. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt03_final001.dds", "voiceover/english/antonia_intro/antonia_intro_003_64.mp3", 3171561318, 593374281);
  5148. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 0);
  5149. AddZoneInfoSlideStructTransitionInfo(slide, 750, 320, 1.60000002384186, 10);
  5150. AddZoneInfoSlideStructTransitionInfo(slide, 575, 265, 2.29999995231628, 10);
  5151. slides->push_back(slide);
  5152. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt04_final001.dds", "voiceover/english/antonia_intro/antonia_intro_004_64.mp3", 1959944485, 4285605574);
  5153. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 2.5, 0);
  5154. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.70000004768372, 8);
  5155. AddZoneInfoSlideStructTransitionInfo(slide, 675, 390, 2.20000004768372, 11);
  5156. slides->push_back(slide);
  5157. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt05_final001.dds", "voiceover/english/antonia_intro/antonia_intro_005_64.mp3", 609693392, 260295215);
  5158. AddZoneInfoSlideStructTransitionInfo(slide, 750, 500, 2.79999995231628, 0);
  5159. AddZoneInfoSlideStructTransitionInfo(slide, 720, 300, 2.5, 9);
  5160. AddZoneInfoSlideStructTransitionInfo(slide, 975, 270, 2.20000004768372, 9);
  5161. slides->push_back(slide);
  5162. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt06_final001.dds", "voiceover/english/antonia_intro/antonia_intro_006_64.mp3", 3056613203, 775201556);
  5163. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.89999997615814, 0);
  5164. AddZoneInfoSlideStructTransitionInfo(slide, 920, 475, 1, 24);
  5165. slides->push_back(slide);
  5166. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt07_final001.dds", "voiceover/english/antonia_intro/antonia_intro_007_64.mp3", 3113327662, 1299367895);
  5167. AddZoneInfoSlideStructTransitionInfo(slide, 1400, 420, 2.40000009536743, 0);
  5168. AddZoneInfoSlideStructTransitionInfo(slide, 1200, 375, 1.70000004768372, 7);
  5169. AddZoneInfoSlideStructTransitionInfo(slide, 800, 225, 2.29999995231628, 7);
  5170. slides->push_back(slide);
  5171. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt08_final001.dds", "voiceover/english/antonia_intro/antonia_intro_008_64.mp3", 2558791235, 2674773065);
  5172. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  5173. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 27);
  5174. slides->push_back(slide);
  5175. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt09_final001.dds", "voiceover/english/antonia_intro/antonia_intro_009_64.mp3", 4029296401, 1369011033);
  5176. AddZoneInfoSlideStructTransitionInfo(slide, 715, 305, 2.40000009536743, 0);
  5177. AddZoneInfoSlideStructTransitionInfo(slide, 730, 325, 1.79999995231628, 6);
  5178. AddZoneInfoSlideStructTransitionInfo(slide, 920, 395, 1.5, 5);
  5179. AddZoneInfoSlideStructTransitionInfo(slide, 1360, 330, 1.79999995231628, 9);
  5180. slides->push_back(slide);
  5181. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt10_final001.dds", "voiceover/english/antonia_intro/antonia_intro_010_64.mp3", 3055524517, 3787058332);
  5182. AddZoneInfoSlideStructTransitionInfo(slide, 670, 675, 2.20000004768372, 0);
  5183. AddZoneInfoSlideStructTransitionInfo(slide, 710, 390, 1.79999995231628, 7);
  5184. AddZoneInfoSlideStructTransitionInfo(slide, 920, 415, 1.60000002384186, 5.5);
  5185. AddZoneInfoSlideStructTransitionInfo(slide, 1250, 675, 1.79999995231628, 8);
  5186. slides->push_back(slide);
  5187. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt11_final001.dds", "voiceover/english/antonia_intro/antonia_intro_011_64.mp3", 3525586740, 812068950);
  5188. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  5189. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 19);
  5190. slides->push_back(slide);
  5191. slide = GenerateSlideStruct(0.5, 0.5, 15, 15, 1842, 1012, "images/slideshows/boat_06p_tutorial02/lore_chapt12_final001.dds", "voiceover/english/antonia_intro/antonia_intro_012_64.mp3", 3493874350, 2037661816);
  5192. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 0);
  5193. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 43);
  5194. slides->push_back(slide);
  5195. return slides;
  5196. }
  5197. EQ2Packet* ZoneServer::GetZoneInfoPacket(Client* client){
  5198. PacketStruct* packet = configReader.getStruct("WS_ZoneInfo", client->GetVersion());
  5199. packet->setSmallStringByName("server1",net.GetWorldName());
  5200. packet->setSmallStringByName("server2",net.GetWorldName());
  5201. packet->setDataByName("unknown1", 1, 1);//1, 1
  5202. int32 expansions = EXPANSION_UNKNOWN + EXPANSION_DOF + EXPANSION_KOS + EXPANSION_EOF + EXPANSION_ROK + EXPANSION_TSO + EXPANSION_DOV;
  5203. //packet->setDataByName("expansions_enabled", 82313211);//expansions 63181
  5204. //packet->setDataByName("expansions_enabled", 552075103);//expansions 63182
  5205. packet->setDataByName("expansions_enabled", 4294967295);//expansions 1096 //612499455 works
  5206. if (client->GetVersion() >= 1193) {
  5207. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  5208. packet->setDataByName("unknown3", 4294967295, 1); //COE and up
  5209. packet->setDataByName("unknown3", 4294967295, 2);
  5210. }
  5211. else
  5212. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  5213. packet->setSmallStringByName("auction_website", "eq2emulator.net");
  5214. packet->setDataByName("auction_port", 80);
  5215. packet->setSmallStringByName("upload_page", "test_upload.m");
  5216. packet->setSmallStringByName("upload_key", "dsya987yda9");
  5217. packet->setSmallStringByName("zone", GetZoneFile());
  5218. //packet->setSmallStringByName("zone2", GetZoneName());
  5219. //if ( strlen(GetZoneSkyFile()) > 0 )
  5220. // packet->setSmallStringByName("zone_unknown2", GetZoneSkyFile()); // used for the sky map
  5221. packet->setSmallStringByName("zone_desc", GetZoneDescription());
  5222. packet->setSmallStringByName("char_name", client->GetPlayer()->GetName());
  5223. packet->setDataByName("x", client->GetPlayer()->GetX());
  5224. packet->setDataByName("y", client->GetPlayer()->GetY());
  5225. packet->setDataByName("z", client->GetPlayer()->GetZ());
  5226. if ((GetZoneFile() && strcmp("boat_06p_tutorial02", GetZoneFile()) == 0) && client->GetPlayer()->GetX() == this->GetSafeX() && client->GetPlayer()->GetY() == this->GetSafeY() && client->GetPlayer()->GetZ() == this->GetSafeZ()) { //basically the only time the player will see this is if their zone in coords are the exact same as the safe coords (they haven't moved)
  5227. vector<ZoneInfoSlideStruct*>* slides = GenerateTutorialSlides();
  5228. if (slides) {
  5229. packet->setArrayLengthByName("num_slides", slides->size());
  5230. ZoneInfoSlideStruct* slide = 0;
  5231. for (int8 i = 0; i < slides->size(); i++) {
  5232. slide = slides->at(i);
  5233. packet->setArrayDataByName("unknown1", slide->info->unknown1[0], i, 0);
  5234. packet->setArrayDataByName("unknown1", slide->info->unknown1[1], i, 1);
  5235. packet->setArrayDataByName("unknown2", slide->info->unknown2[0], i, 0);
  5236. packet->setArrayDataByName("unknown2", slide->info->unknown2[1], i, 1);
  5237. packet->setArrayDataByName("unknown3", slide->info->unknown3, i);
  5238. packet->setArrayDataByName("unknown4", slide->info->unknown4, i);
  5239. packet->setArrayDataByName("slide", slide->info->slide, i);
  5240. packet->setArrayDataByName("voiceover", slide->info->voiceover, i);
  5241. packet->setArrayDataByName("key1", slide->info->key1, i);
  5242. packet->setArrayDataByName("key2", slide->info->key2, i);
  5243. packet->setSubArrayLengthByName("num_transitions", slide->slide_transition_info.size(), i);
  5244. for (int8 x = 0; x < slide->slide_transition_info.size(); x++) {
  5245. packet->setSubArrayDataByName("transition_x", slide->slide_transition_info[x]->transition_x, i, x);
  5246. packet->setSubArrayDataByName("transition_y", slide->slide_transition_info[x]->transition_y, i, x);
  5247. packet->setSubArrayDataByName("transition_zoom", slide->slide_transition_info[x]->transition_zoom, i, x);
  5248. packet->setSubArrayDataByName("transition_time", slide->slide_transition_info[x]->transition_time, i, x);
  5249. safe_delete(slide->slide_transition_info[x]);
  5250. }
  5251. safe_delete(slide->info);
  5252. safe_delete(slide);
  5253. }
  5254. }
  5255. safe_delete(slides);
  5256. }
  5257. if(rule_manager.GetZoneRule(GetZoneID(), R_Zone, UseMapUnderworldCoords)->GetBool() && client->GetPlayer()->GetMap()) {
  5258. packet->setDataByName("underworld", client->GetPlayer()->GetMap()->GetMinY() + rule_manager.GetZoneRule(GetZoneID(), R_Zone, MapUnderworldCoordOffset)->GetFloat());
  5259. }
  5260. else {
  5261. packet->setDataByName("underworld", underworld);
  5262. }
  5263. // unknown3 can prevent screen shots from being taken if
  5264. //packet->setDataByName("unknown3", 2094661567, 1); // Screenshots allowed with this value
  5265. //packet->setDataByName("unknown3", 3815767999, 1); // Screenshots disabled with this value
  5266. //packet->setDataByName("unknown3", 1, 2);
  5267. /*if (client->GetVersion() >= 63587) {
  5268. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  5269. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  5270. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  5271. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  5272. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  5273. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  5274. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  5275. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  5276. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  5277. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  5278. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  5279. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  5280. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  5281. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  5282. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  5283. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  5284. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  5285. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  5286. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  5287. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  5288. }
  5289. else if (client->GetVersion() >= 63214) {
  5290. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  5291. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  5292. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  5293. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  5294. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  5295. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  5296. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  5297. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  5298. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  5299. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  5300. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  5301. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  5302. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  5303. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  5304. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  5305. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  5306. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  5307. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  5308. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  5309. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  5310. }*/
  5311. if (client->GetVersion() >= 64644) {
  5312. packet->setDataByName("unknown3a", 12598924);
  5313. packet->setDataByName("unknown3b", 3992452959);
  5314. packet->setDataByName("unknown3c", 4294967183);
  5315. packet->setDataByName("unknown2a", 9);
  5316. packet->setDataByName("unknown2b", 9);
  5317. }
  5318. else if (client->GetVersion() >= 63181) {
  5319. packet->setDataByName("unknown3a", 750796556);//63182 73821356
  5320. packet->setDataByName("unknown3b", 3991404383);// 63182 3991404383
  5321. packet->setDataByName("unknown3c", 4278189967);// 63182 4278189967
  5322. packet->setDataByName("unknown2a", 8);// 63182
  5323. packet->setDataByName("unknown2b", 8);// 63182
  5324. }
  5325. else{
  5326. //packet->setDataByName("unknown3", 872447025,0);//63181
  5327. //packet->setDataByName("unknown3", 3085434875,1);// 63181
  5328. //packet->setDataByName("unknown3", 2147483633,2);// 63181
  5329. }
  5330. packet->setDataByName("year", world.GetWorldTimeStruct()->year);
  5331. packet->setDataByName("month", world.GetWorldTimeStruct()->month);
  5332. packet->setDataByName("day", world.GetWorldTimeStruct()->day);
  5333. packet->setDataByName("hour", world.GetWorldTimeStruct()->hour);
  5334. packet->setDataByName("minute", world.GetWorldTimeStruct()->minute);
  5335. packet->setDataByName("unknown", 0);
  5336. packet->setDataByName("unknown7", 1);
  5337. packet->setDataByName("unknown7", 1, 1);
  5338. packet->setDataByName("unknown9", 13);
  5339. //packet->setDataByName("unknown10", 25188959);4294967295
  5340. //packet->setDataByName("unknown10", 25190239);
  5341. packet->setDataByName("unknown10", 25191524);//25191524
  5342. packet->setDataByName("unknown10b", 1);
  5343. packet->setDataByName("permission_level",3);// added on 63182 for now till we figur it out 0=none,1=visitor,2=friend,3=trustee,4=owner
  5344. packet->setDataByName("num_adv", 9);
  5345. packet->setArrayDataByName("adv_name", "adv02_dun_drowned_caverns", 0);
  5346. packet->setArrayDataByName("adv_id", 6, 0);
  5347. packet->setArrayDataByName("adv_name", "adv02_dun_sundered_splitpaw_hub", 1);
  5348. packet->setArrayDataByName("adv_id", 5, 1);
  5349. packet->setArrayDataByName("adv_name", "exp03_rgn_butcherblock", 2);
  5350. packet->setArrayDataByName("adv_id", 8, 2);
  5351. packet->setArrayDataByName("adv_name", "exp03_rgn_greater_faydark", 3);
  5352. packet->setArrayDataByName("adv_id", 7, 3);
  5353. packet->setArrayDataByName("adv_name", "mod01_dun_crypt_of_thaen", 4);
  5354. packet->setArrayDataByName("adv_id", 3, 4);
  5355. packet->setArrayDataByName("adv_name", "mod01_dun_tombs_of_night", 5);
  5356. packet->setArrayDataByName("adv_id", 4, 5);
  5357. packet->setArrayDataByName("adv_name", "nektulos_mini01", 6);
  5358. packet->setArrayDataByName("adv_id", 0, 6);
  5359. packet->setArrayDataByName("adv_name", "nektulos_mini02", 7);
  5360. packet->setArrayDataByName("adv_id", 1, 7);
  5361. packet->setArrayDataByName("adv_name", "nektulos_mini03", 8);
  5362. packet->setArrayDataByName("adv_id", 2, 8);
  5363. LogWrite(MISC__TODO, 0, "TODO", "Put cl_ client commands back in variables (not Rules) so they can be dynamically maintained");
  5364. vector<Variable*>* variables = world.GetClientVariables();
  5365. packet->setArrayLengthByName("num_client_setup", variables->size());
  5366. for(int i=variables->size()-1;i>=0;i--)
  5367. packet->setArrayDataByName("client_cmds", variables->at(i)->GetNameValuePair().c_str(), i);
  5368. // For AoM clients so item link work
  5369. if (client->GetVersion() >= 60114)
  5370. packet->setArrayDataByName("client_cmds", "chat_linkbrackets_item 1", variables->size());
  5371. safe_delete(variables);
  5372. //packet->setDataByName("unknown8", ); story?
  5373. // AA Tabs for 1193+ clients
  5374. if (client->GetVersion() >= 1193) {
  5375. packet->setArrayLengthByName("tab_count", 48);
  5376. int8 i = 0;
  5377. packet->setArrayDataByName("tab_index", i, i);
  5378. packet->setArrayDataByName("tab_name", ":es24a58bd8fcaac8c2:All", i);
  5379. i++;
  5380. packet->setArrayDataByName("tab_index", i, i);
  5381. packet->setArrayDataByName("tab_name", ":410385c727bd47a6:Racial Innate", i);
  5382. i++;
  5383. packet->setArrayDataByName("tab_index", i, i);
  5384. packet->setArrayDataByName("tab_name", ":410385c75a96e23c:Tradeskill Advancement", i);
  5385. i++;
  5386. packet->setArrayDataByName("tab_index", i, i);
  5387. packet->setArrayDataByName("tab_name", ":410385c744f1fd99:Focus Effects", i);
  5388. i++;
  5389. packet->setArrayDataByName("tab_index", i, i);
  5390. packet->setArrayDataByName("tab_name", ":410385c71edd2a66:Heroic", i);
  5391. i++;
  5392. packet->setArrayDataByName("tab_index", i, i);
  5393. packet->setArrayDataByName("tab_name", ":410385c76ee6239f:Shadows", i);
  5394. i++;
  5395. packet->setArrayDataByName("tab_index", i, i);
  5396. packet->setArrayDataByName("tab_name", ":410385c7e678b977:Prestige", i);
  5397. i++;
  5398. packet->setArrayDataByName("tab_index", i, i);
  5399. packet->setArrayDataByName("tab_name", ":410385c77ee422d7:Animalist", i);
  5400. i++;
  5401. packet->setArrayDataByName("tab_index", i, i);
  5402. packet->setArrayDataByName("tab_name", ":410385c7f165af77:Bard", i);
  5403. i++;
  5404. packet->setArrayDataByName("tab_index", i, i);
  5405. packet->setArrayDataByName("tab_name", ":410385c7421b9375:Brawler", i);
  5406. i++;
  5407. packet->setArrayDataByName("tab_index", i, i);
  5408. packet->setArrayDataByName("tab_name", ":410385c7a03ae7d1:Cleric", i);
  5409. i++;
  5410. packet->setArrayDataByName("tab_index", i, i);
  5411. packet->setArrayDataByName("tab_name", ":410385c7c9605e9f:Crusader", i);
  5412. i++;
  5413. packet->setArrayDataByName("tab_index", i, i);
  5414. packet->setArrayDataByName("tab_name", ":410385c7f9424168:Druid", i);
  5415. i++;
  5416. packet->setArrayDataByName("tab_index", i, i);
  5417. packet->setArrayDataByName("tab_name", ":410385c79cb9556c:Enchanter", i);
  5418. i++;
  5419. packet->setArrayDataByName("tab_index", i, i);
  5420. packet->setArrayDataByName("tab_name", ":410385c70c8b6aa4:Predator", i);
  5421. i++;
  5422. packet->setArrayDataByName("tab_index", i, i);
  5423. packet->setArrayDataByName("tab_name", ":410385c73a43b6dd:Rogue", i);
  5424. i++;
  5425. packet->setArrayDataByName("tab_index", i, i);
  5426. packet->setArrayDataByName("tab_name", ":410385c759fe7d15:Sorcerer", i);
  5427. i++;
  5428. packet->setArrayDataByName("tab_index", i, i);
  5429. packet->setArrayDataByName("tab_name", ":410385c7ad610aca:Summoner", i);
  5430. i++;
  5431. packet->setArrayDataByName("tab_index", i, i);
  5432. packet->setArrayDataByName("tab_name", ":410385c71e056728:Warrior", i);
  5433. i++;
  5434. packet->setArrayDataByName("tab_index", i, i);
  5435. packet->setArrayDataByName("tab_name", ":410385c7ba864c0b:Assassin", i);
  5436. i++;
  5437. packet->setArrayDataByName("tab_index", i, i);
  5438. packet->setArrayDataByName("tab_name", ":410385c7b8116aad:Beastlord", i);
  5439. i++;
  5440. packet->setArrayDataByName("tab_index", i, i);
  5441. packet->setArrayDataByName("tab_name", ":410385c7f53feb7b:Berserker", i);
  5442. i++;
  5443. packet->setArrayDataByName("tab_index", i, i);
  5444. packet->setArrayDataByName("tab_name", ":410385c73d8a70e2:Brigand", i);
  5445. i++;
  5446. packet->setArrayDataByName("tab_index", i, i);
  5447. packet->setArrayDataByName("tab_name", ":410385c770c766d6:Bruiser", i);
  5448. i++;
  5449. packet->setArrayDataByName("tab_index", i, i);
  5450. packet->setArrayDataByName("tab_name", ":410385c79226984b:Coercer", i);
  5451. i++;
  5452. packet->setArrayDataByName("tab_index", i, i);
  5453. packet->setArrayDataByName("tab_name", ":410385c70c58bb30:Conjurer", i);
  5454. i++;
  5455. packet->setArrayDataByName("tab_index", i, i);
  5456. packet->setArrayDataByName("tab_name", ":410385c73dfe68d0:Defiler", i);
  5457. i++;
  5458. packet->setArrayDataByName("tab_index", i, i);
  5459. packet->setArrayDataByName("tab_name", ":410385c792919a6b:Dirge", i);
  5460. i++;
  5461. packet->setArrayDataByName("tab_index", i, i);
  5462. packet->setArrayDataByName("tab_name", ":410385c7062e5f55:Fury", i);
  5463. i++;
  5464. packet->setArrayDataByName("tab_index", i, i);
  5465. packet->setArrayDataByName("tab_name", ":410385c762c1fdfc:Guardian", i);
  5466. i++;
  5467. packet->setArrayDataByName("tab_index", i, i);
  5468. packet->setArrayDataByName("tab_name", ":410385c78addfbf4:Illusionist", i);
  5469. i++;
  5470. packet->setArrayDataByName("tab_index", i, i);
  5471. packet->setArrayDataByName("tab_name", ":410385c7ece054a7:Inquisitor", i);
  5472. i++;
  5473. packet->setArrayDataByName("tab_index", i, i);
  5474. packet->setArrayDataByName("tab_name", ":410385c7d550d2e7:Monk", i);
  5475. i++;
  5476. packet->setArrayDataByName("tab_index", i, i);
  5477. packet->setArrayDataByName("tab_name", ":410385c743cfeaa2:Mystic", i);
  5478. i++;
  5479. packet->setArrayDataByName("tab_index", i, i);
  5480. packet->setArrayDataByName("tab_name", ":410385c7f63c9c8c:Necromancer", i);
  5481. i++;
  5482. packet->setArrayDataByName("tab_index", i, i);
  5483. packet->setArrayDataByName("tab_name", ":410385c70c5de0ae:Paladin", i);
  5484. i++;
  5485. packet->setArrayDataByName("tab_index", i, i);
  5486. packet->setArrayDataByName("tab_name", ":410385c79bc97b3a:Ranger", i);
  5487. i++;
  5488. packet->setArrayDataByName("tab_index", i, i);
  5489. packet->setArrayDataByName("tab_name", ":410385c78fbd2256:Shadowknight", i);
  5490. i++;
  5491. packet->setArrayDataByName("tab_index", i, i);
  5492. packet->setArrayDataByName("tab_name", ":410385c7781cc625:Shaman", i);
  5493. i++;
  5494. packet->setArrayDataByName("tab_index", i, i);
  5495. packet->setArrayDataByName("tab_name", ":410385c77eecdcdb:Swashbuckler", i);
  5496. i++;
  5497. packet->setArrayDataByName("tab_index", i, i);
  5498. packet->setArrayDataByName("tab_name", ":410385c7648d181e:Templar", i);
  5499. i++;
  5500. packet->setArrayDataByName("tab_index", i, i);
  5501. packet->setArrayDataByName("tab_name", ":410385c78df47d77:Troubador", i);
  5502. i++;
  5503. packet->setArrayDataByName("tab_index", i, i);
  5504. packet->setArrayDataByName("tab_name", ":410385c7c78ce0b8:Warden", i);
  5505. i++;
  5506. packet->setArrayDataByName("tab_index", i, i);
  5507. packet->setArrayDataByName("tab_name", ":410385c76290dcfa:Warlock", i);
  5508. i++;
  5509. packet->setArrayDataByName("tab_index", i, i);
  5510. packet->setArrayDataByName("tab_name", ":410385c7d1d52cf5:Wizard", i);
  5511. i++;
  5512. packet->setArrayDataByName("tab_index", i, i);
  5513. packet->setArrayDataByName("tab_name", ":410385c71c8f6f4d:Shaper", i);
  5514. i++;
  5515. packet->setArrayDataByName("tab_index", i, i);
  5516. packet->setArrayDataByName("tab_name", ":410385c72f6e354d:Channeler", i);
  5517. i++;
  5518. packet->setArrayDataByName("tab_index", i, i);
  5519. packet->setArrayDataByName("tab_name", ":410385c7df8bd37d:Dragon", i);
  5520. }
  5521. packet->setDataByName("unknown_mj", 1);//int8
  5522. packet->setDataByName("unknown_mj1", 335544320);//int32
  5523. packet->setDataByName("unknown_mj2", 4);//int32
  5524. packet->setDataByName("unknown_mj3", 3962504088);//int32
  5525. packet->setDataByName("unknown_mj4", 3985947216);//int32
  5526. packet->setDataByName("unknown_mj5", 1);//int32
  5527. packet->setDataByName("unknown_mj6", 386);//int32
  5528. packet->setDataByName("unknown_mj7", 4294967295);//int32
  5529. packet->setDataByName("unknown_mj8", 2716312211);//int32
  5530. packet->setDataByName("unknown_mj9", 1774338333);//int32
  5531. packet->setDataByName("unknown_mj10", 1);//int32
  5532. packet->setDataByName("unknown_mj11", 391);//int32
  5533. packet->setDataByName("unknown_mj12", 4294967295);//int32
  5534. packet->setDataByName("unknown_mj13", 3168965163);//int32
  5535. packet->setDataByName("unknown_mj14", 4117025286);//int32
  5536. packet->setDataByName("unknown_mj15", 1);//int32
  5537. packet->setDataByName("unknown_mj16", 394);//int32
  5538. packet->setDataByName("unknown_mj17", 4294967295);//int32
  5539. packet->setDataByName("unknown_mj18", 1790669110);//int32
  5540. packet->setDataByName("unknown_mj19", 107158108);//int32
  5541. packet->setDataByName("unknown_mj20", 1);//int32
  5542. packet->setDataByName("unknown_mj21", 393);//int32
  5543. packet->setDataByName("unknown_mj22", 4294967295);//int32
  5544. EQ2Packet* outapp = packet->serialize();
  5545. //packet->PrintPacket();
  5546. //DumpPacket(outapp);
  5547. safe_delete(packet);
  5548. return outapp;
  5549. }
  5550. void ZoneServer::SendUpdateDefaultCommand(Spawn* spawn, const char* command, float distance, Spawn* toPlayer){
  5551. if (spawn == nullptr || command == nullptr)
  5552. return;
  5553. if (toPlayer)
  5554. {
  5555. if (!toPlayer->IsPlayer())
  5556. return;
  5557. Client* client = ((Player*)toPlayer)->GetClient();
  5558. if (client)
  5559. {
  5560. client->SendDefaultCommand(spawn, command, distance);
  5561. }
  5562. // we don't override the primary command cause that would change ALL clients
  5563. return;
  5564. }
  5565. QueueDefaultCommand(spawn->GetID(), std::string(command), distance);
  5566. if (strlen(command)>0)
  5567. spawn->SetPrimaryCommand(command, command, distance);
  5568. }
  5569. void ZoneServer::CheckPlayerProximity(Spawn* spawn, Client* client){
  5570. if (player_proximities.size() < 1)
  5571. return;
  5572. if(player_proximities.count(spawn->GetID()) > 0){
  5573. PlayerProximity* prox = player_proximities.Get(spawn->GetID());
  5574. if(prox->clients_in_proximity.count(client) == 0 && spawn_range_map.count(client) > 0 && spawn_range_map.Get(client)->count(spawn->GetID()) > 0 && spawn_range_map.Get(client)->Get(spawn->GetID()) < prox->distance){
  5575. prox->clients_in_proximity[client] = true;
  5576. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->in_range_lua_function.c_str());
  5577. }
  5578. else if(prox->clients_in_proximity.count(client) > 0 && spawn_range_map.count(client) > 0 && spawn_range_map.Get(client)->count(spawn->GetID()) > 0 && spawn_range_map.Get(client)->Get(spawn->GetID()) > prox->distance){
  5579. if(prox->leaving_range_lua_function.length() > 0)
  5580. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->leaving_range_lua_function.c_str());
  5581. prox->clients_in_proximity.erase(client);
  5582. }
  5583. }
  5584. }
  5585. void ZoneServer::AddPlayerProximity(Spawn* spawn, float distance, string in_range_function, string leaving_range_function){
  5586. RemovePlayerProximity(spawn);
  5587. PlayerProximity* prox = new PlayerProximity;
  5588. prox->distance = distance;
  5589. prox->in_range_lua_function = in_range_function;
  5590. prox->leaving_range_lua_function = leaving_range_function;
  5591. player_proximities.Put(spawn->GetID(), prox);
  5592. }
  5593. void ZoneServer::RemovePlayerProximity(Client* client){
  5594. PlayerProximity* prox = 0;
  5595. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5596. while(itr.Next()){
  5597. prox = itr->second;
  5598. if(prox->clients_in_proximity.count(client) > 0)
  5599. prox->clients_in_proximity.erase(client);
  5600. }
  5601. }
  5602. void ZoneServer::RemovePlayerProximity(Spawn* spawn, bool all){
  5603. if(all){
  5604. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5605. while(itr.Next()){
  5606. player_proximities.erase(itr->first, false, true, 10000);
  5607. }
  5608. }
  5609. else if(player_proximities.count(spawn->GetID()) > 0){
  5610. player_proximities.erase(spawn->GetID(), false, true, 10000);
  5611. }
  5612. }
  5613. void ZoneServer::AddLocationProximity(float x, float y, float z, float max_variation, string in_range_function, string leaving_range_function) {
  5614. LocationProximity* prox = new LocationProximity;
  5615. prox->x = x;
  5616. prox->y = y;
  5617. prox->z = z;
  5618. prox->max_variation = max_variation;
  5619. prox->in_range_lua_function = in_range_function;
  5620. prox->leaving_range_lua_function = leaving_range_function;
  5621. location_proximities.Add(prox);
  5622. }
  5623. void ZoneServer::CheckLocationProximity() {
  5624. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  5625. if (!zone_script)
  5626. return;
  5627. if (location_proximities.size() > 0 && connected_clients.size() > 0) {
  5628. Client* client = 0;
  5629. MutexList<Client*>::iterator iterator = connected_clients.begin();
  5630. while(iterator.Next()){
  5631. client = iterator->value;
  5632. if (client->IsConnected() && client->IsReadyForUpdates() && !client->IsZoning()) {
  5633. try {
  5634. MutexList<LocationProximity*>::iterator itr = location_proximities.begin();
  5635. LocationProximity* prox = 0;
  5636. while(itr.Next()){
  5637. prox = itr->value;
  5638. bool in_range = false;
  5639. float char_x = client->GetPlayer()->GetX();
  5640. float char_y = client->GetPlayer()->GetY();
  5641. float char_z = client->GetPlayer()->GetZ();
  5642. float x = prox->x;
  5643. float y = prox->y;
  5644. float z = prox->z;
  5645. float max_variation = prox->max_variation;
  5646. float total_diff = 0;
  5647. float diff = x - char_x; //Check X
  5648. if(diff < 0)
  5649. diff *= -1;
  5650. if(diff <= max_variation) {
  5651. total_diff += diff;
  5652. diff = z - char_z; //Check Z (we check Z first because it is far more likely to be a much greater variation than y)
  5653. if(diff < 0)
  5654. diff *= -1;
  5655. if(diff <= max_variation) {
  5656. total_diff += diff;
  5657. if(total_diff <= max_variation) { //Check Total
  5658. diff = y - char_y; //Check Y
  5659. if(diff < 0)
  5660. diff *= -1;
  5661. if(diff <= max_variation) {
  5662. total_diff += diff;
  5663. if(total_diff <= max_variation) {
  5664. in_range = true;
  5665. if(lua_interface && prox->in_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) == 0) { //Check Total
  5666. prox->clients_in_proximity[client] = true;
  5667. lua_interface->RunZoneScript(zone_script, prox->in_range_lua_function.c_str(), this, client->GetPlayer());
  5668. }
  5669. }
  5670. }
  5671. }
  5672. }
  5673. }
  5674. if (!in_range) {
  5675. if(lua_interface && prox->leaving_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) > 0) {
  5676. lua_interface->RunZoneScript(zone_script, prox->leaving_range_lua_function.c_str(), this, client->GetPlayer());
  5677. prox->clients_in_proximity.erase(client);
  5678. }
  5679. }
  5680. }
  5681. }
  5682. catch (...) {
  5683. LogWrite(ZONE__ERROR, 0, "Zone", "Except caught in ZoneServer::CheckLocationProximity");
  5684. return;
  5685. }
  5686. }
  5687. }
  5688. }
  5689. }
  5690. void ZoneServer::CheckLocationGrids() {
  5691. if (connected_clients.size() > 0 && location_grids.size() > 0) {
  5692. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5693. while (client_itr.Next()) {
  5694. Client* client = client_itr.value;
  5695. if (!client)
  5696. continue;
  5697. Player* player = client->GetPlayer();
  5698. float x = player->GetX();
  5699. float y = player->GetY();
  5700. float z = player->GetZ();
  5701. int32 grid_id = player->GetLocation();
  5702. MutexList<LocationGrid*>::iterator location_grid_itr = location_grids.begin();
  5703. while (location_grid_itr.Next()) {
  5704. LocationGrid* grid = location_grid_itr.value;
  5705. bool playerInGrid = false;
  5706. if (grid->locations.size() > 0 || (playerInGrid = (grid->grid_id > 0 && grid->grid_id == grid_id)) || grid->players.count(player) > 0) {
  5707. float x_small = 0;
  5708. float x_large = 0;
  5709. float y_small = 0;
  5710. float y_large = 0;
  5711. float z_small = 0;
  5712. float z_large = 0;
  5713. bool first = true;
  5714. bool in_grid = false;
  5715. if(grid->locations.size() == 0 && playerInGrid) { // no locations, we presume player is in grid
  5716. in_grid = true;
  5717. }
  5718. else {
  5719. MutexList<Location*>::iterator location_itr = grid->locations.begin();
  5720. while (location_itr.Next()) {
  5721. Location* location = location_itr.value;
  5722. if (first) {
  5723. x_small = location->x;
  5724. x_large = location->x;
  5725. if (grid->include_y) {
  5726. y_small = location->y;
  5727. y_large = location->y;
  5728. }
  5729. z_small = location->z;
  5730. z_large = location->z;
  5731. first = false;
  5732. }
  5733. else {
  5734. if (location->x < x_small)
  5735. x_small = location->x;
  5736. else if (location->x > x_large)
  5737. x_large = location->x;
  5738. if (grid->include_y) {
  5739. if (location->y < y_small)
  5740. y_small = location->y;
  5741. else if (location->y > y_large)
  5742. y_large = location->y;
  5743. }
  5744. if (location->z < z_small)
  5745. z_small = location->z;
  5746. else if (location->z > z_large)
  5747. z_large = location->z;
  5748. }
  5749. }
  5750. if (grid->include_y && (x >= x_small && x <= x_large && y >= y_small && y <= y_large && z >= z_small && z <= z_large))
  5751. in_grid = true;
  5752. else if (x >= x_small && x <= x_large && z >= z_small && z <= z_large)
  5753. in_grid = true;
  5754. }
  5755. if (in_grid && grid->players.count(player) == 0) {
  5756. grid->players.Put(player, true);
  5757. bool show_enter_location_popup = true;
  5758. bool discovery_enabled = rule_manager.GetZoneRule(GetZoneID(), R_World, EnablePOIDiscovery)->GetBool();
  5759. if( grid->discovery && discovery_enabled && !player->DiscoveredLocation(grid->id) )
  5760. {
  5761. // check if player has already discovered this location
  5762. // if not, process new discovery
  5763. char tmp[200] = {0};
  5764. sprintf(tmp, "\\#FFE400You have discovered\12\\#FFF283%s", grid->name.c_str());
  5765. client->SendPopupMessage(11, tmp, "ui_discovery", 2.25, 0xFF, 0xFF, 0xFF);
  5766. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' discovered location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5767. player->UpdatePlayerHistory(HISTORY_TYPE_DISCOVERY, HISTORY_SUBTYPE_LOCATION, grid->id);
  5768. show_enter_location_popup = false;
  5769. // else, print standard location entry
  5770. }
  5771. if( show_enter_location_popup )
  5772. {
  5773. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' entering location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5774. client->SendPopupMessage(10, grid->name.c_str(), 0, 2.5, 255, 255, 0);
  5775. }
  5776. }
  5777. else if (!in_grid && grid->players.count(player) > 0) {
  5778. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' leaving location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5779. grid->players.erase(player);
  5780. }
  5781. }
  5782. }
  5783. }
  5784. }
  5785. }
  5786. // Called from a command (client, main zone thread) and the main zone thread
  5787. // so no need for a mutex container
  5788. void ZoneServer::AddLocationGrid(LocationGrid* grid) {
  5789. if (grid)
  5790. location_grids.Add(grid);
  5791. }
  5792. void ZoneServer::RemoveLocationGrids() {
  5793. MutexList<LocationGrid*>::iterator itr = location_grids.begin();
  5794. while (itr.Next())
  5795. itr.value->locations.clear(true);
  5796. location_grids.clear(true);
  5797. }
  5798. void ZoneServer::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast, bool call_expire_function, bool lock_spell_process){
  5799. if(spellProcess)
  5800. spellProcess->RemoveSpellTimersFromSpawn(spawn, remove_all, delete_recast, call_expire_function, lock_spell_process);
  5801. }
  5802. void ZoneServer::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement){
  5803. if(spellProcess)
  5804. spellProcess->Interrupted(caster, interruptor, error_code, cancel, from_movement);
  5805. }
  5806. Spell* ZoneServer::GetSpell(Entity* caster){
  5807. Spell* spell = 0;
  5808. if(spellProcess)
  5809. spell = spellProcess->GetSpell(caster);
  5810. return spell;
  5811. }
  5812. void ZoneServer::ProcessSpell(Spell* spell, Entity* caster, Spawn* target, bool lock, bool harvest_spell, LuaSpell* customSpell, int16 custom_cast_time, bool in_heroic_opp){
  5813. if(spellProcess)
  5814. spellProcess->ProcessSpell(this, spell, caster, target, lock, harvest_spell, customSpell, custom_cast_time, in_heroic_opp);
  5815. }
  5816. void ZoneServer::ProcessEntityCommand(EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock) {
  5817. if (target && target->GetSpawnScript()) {
  5818. Player* player = 0;
  5819. if (caster && caster->IsPlayer())
  5820. player = (Player*)caster;
  5821. CallSpawnScript(target, SPAWN_SCRIPT_CUSTOM, player, entity_command->command.c_str());
  5822. }
  5823. if (spellProcess)
  5824. spellProcess->ProcessEntityCommand(this, entity_command, caster, target, lock);
  5825. }
  5826. void ZoneServer::RemoveSpawnSupportFunctions(Spawn* spawn, bool lock_spell_process, bool shutdown) {
  5827. if(!spawn)
  5828. return;
  5829. // remove entity* in trade class
  5830. if(spawn->IsEntity()) {
  5831. ((Entity*)spawn)->TerminateTrade();
  5832. }
  5833. if(spawn->IsPlayer() && spawn->GetZone()) {
  5834. spawn->GetZone()->RemovePlayerPassenger(((Player*)spawn)->GetCharacterID());
  5835. if(((Player*)spawn)->GetClient()) {
  5836. GetTradeskillMgr()->StopCrafting(((Player*)spawn)->GetClient());
  5837. }
  5838. }
  5839. if(spawn->IsEntity())
  5840. RemoveSpellTimersFromSpawn((Entity*)spawn, true, true, true, lock_spell_process);
  5841. if(!shutdown) { // in case of shutdown, DeleteData(true) handles the cleanup later via DeleteSpawnScriptTimers
  5842. StopSpawnScriptTimer(spawn, "");
  5843. }
  5844. if(spawn->IsEntity()) {
  5845. ClearHate((Entity*)spawn);
  5846. }
  5847. RemoveDamagedSpawn(spawn);
  5848. spawn->SendSpawnChanges(false);
  5849. RemoveChangedSpawn(spawn);
  5850. // Everything inside this if will be nuked during a reload in other spots, no need to do it twice
  5851. if (!reloading) {
  5852. RemoveDeadEnemyList(spawn);
  5853. spawn->changed = true;
  5854. spawn->info_changed = true;
  5855. spawn->vis_changed = true;
  5856. spawn->position_changed = true;
  5857. SendSpawnChanges(spawn);
  5858. if (spawn->GetSpawnGroupID() > 0) {
  5859. int32 group_id = spawn->GetSpawnGroupID();
  5860. spawn->RemoveSpawnFromGroup();
  5861. if (spawn_group_map.count(group_id) > 0)
  5862. spawn_group_map.Get(group_id).Remove(spawn->GetID());
  5863. }
  5864. if (!spawn->IsPlayer()) {
  5865. if(quick_database_id_lookup.count(spawn->GetDatabaseID()) > 0)
  5866. quick_database_id_lookup.erase(spawn->GetDatabaseID());
  5867. if(spawn->GetSpawnLocationID() > 0 && quick_location_id_lookup.count(spawn->GetSpawnLocationID()) > 0 && quick_location_id_lookup.Get(spawn->GetSpawnLocationID()) == spawn->GetID())
  5868. quick_location_id_lookup.erase(spawn->GetSpawnLocationID());
  5869. if(spawn->GetSpawnGroupID() > 0 && quick_group_id_lookup.count(spawn->GetSpawnGroupID()) > 0 && quick_group_id_lookup.Get(spawn->GetSpawnGroupID()) == spawn->GetID())
  5870. quick_group_id_lookup.erase(spawn->GetSpawnGroupID());
  5871. }
  5872. DeleteSpawnScriptTimers(spawn);
  5873. RemovePlayerProximity(spawn);
  5874. }
  5875. // We don't use RemoveMovementNPC() here as it caused a hell of a delay during reloads
  5876. // instead we remove it from the list directly
  5877. if (spawn->IsNPC())
  5878. movement_spawns.erase(spawn->GetID());
  5879. }
  5880. void ZoneServer::HandleEmote(Spawn* originator, string name) {
  5881. if (!originator) {
  5882. LogWrite(ZONE__ERROR, 0, "Zone", "HandleEmote called with an invalid client");
  5883. return;
  5884. }
  5885. Client* orig_client = (originator->IsPlayer() && ((Player*)originator)->GetClient()) ? ((Player*)originator)->GetClient() : nullptr;
  5886. Client* client = 0;
  5887. int32 cur_client_version = orig_client ? orig_client->GetVersion() : 546;
  5888. Emote* origEmote = visual_states.FindEmote(name, cur_client_version);
  5889. if(!origEmote){
  5890. if(orig_client) {
  5891. orig_client->Message(CHANNEL_COLOR_YELLOW, "Unable to find emote '%s'. If this should be a valid emote be sure to submit a /bug report.", name.c_str());
  5892. }
  5893. return;
  5894. }
  5895. Emote* emote = origEmote;
  5896. PacketStruct* packet = 0;
  5897. char* emoteResponse = 0;
  5898. vector<Client*>::iterator client_itr;
  5899. map<int32, Emote*> emote_version_range;
  5900. MClientList.readlock(__FUNCTION__, __LINE__);
  5901. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  5902. client = *client_itr;
  5903. if(!client || (client && originator->IsPlayer() && client->GetPlayer()->IsIgnored(originator->GetName())))
  5904. continue;
  5905. // establish appropriate emote for the version used by the client
  5906. if (client->GetVersion() != cur_client_version)
  5907. {
  5908. map<int32, Emote*>::iterator rangeitr = emote_version_range.find(client->GetVersion());
  5909. if (rangeitr == emote_version_range.end())
  5910. {
  5911. Emote* tmp_new_emote = visual_states.FindEmote(name, client->GetVersion());
  5912. if (tmp_new_emote)
  5913. {
  5914. emote_version_range.insert(make_pair(client->GetVersion(), tmp_new_emote));
  5915. emote = tmp_new_emote;
  5916. } // else its missing just use the current clients default
  5917. }
  5918. else // we have an existing emote already cached
  5919. emote = rangeitr->second;
  5920. }
  5921. else // since the client and originator client match use the original emote
  5922. emote = origEmote;
  5923. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  5924. if(packet){
  5925. packet->setDataByName("spawn_id" , client->GetPlayer()->GetIDWithPlayerSpawn(originator));
  5926. if(!emoteResponse){
  5927. string message;
  5928. if(originator->GetTarget() && originator->GetTarget()->GetID() != originator->GetID()){
  5929. message = emote->GetTargetedMessageString();
  5930. if(message.find("%t") < 0xFFFFFFFF)
  5931. message.replace(message.find("%t"), 2, originator->GetTarget()->GetName());
  5932. }
  5933. if(message.length() == 0)
  5934. message = emote->GetMessageString();
  5935. if(message.find("%g1") < 0xFFFFFFFF){
  5936. if(originator->GetGender() == 1)
  5937. message.replace(message.find("%g1"), 3, "his");
  5938. else
  5939. message.replace(message.find("%g1"), 3, "her");
  5940. }
  5941. if(message.find("%g2") < 0xFFFFFFFF){
  5942. if(originator->GetGender() == 1)
  5943. message.replace(message.find("%g2"), 3, "him");
  5944. else
  5945. message.replace(message.find("%g2"), 3, "her");
  5946. }
  5947. if(message.find("%g3") < 0xFFFFFFFF){
  5948. if(originator->GetGender() == 1)
  5949. message.replace(message.find("%g3"), 3, "he");
  5950. else
  5951. message.replace(message.find("%g3"), 3, "she");
  5952. }
  5953. if(message.length() > 0){
  5954. emoteResponse = new char[message.length() + strlen(originator->GetName()) + 10];
  5955. sprintf(emoteResponse,"%s %s", originator->GetName(), message.c_str());
  5956. }
  5957. }
  5958. if(originator->IsPlayer()) {
  5959. packet->setMediumStringByName("emote_msg", emoteResponse ? emoteResponse : "");
  5960. }
  5961. packet->setDataByName("anim_type", emote->GetVisualState());
  5962. client->QueuePacket(packet->serialize());
  5963. safe_delete(packet);
  5964. safe_delete_array(emoteResponse);
  5965. }
  5966. }
  5967. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5968. }
  5969. void ZoneServer::SetupInstance(int32 createdInstanceID) {
  5970. if ( createdInstanceID == 0 ) // if this happens that isn't good!
  5971. instanceID = ++MinInstanceID;
  5972. else // db should pass the good ID
  5973. instanceID = createdInstanceID;
  5974. }
  5975. void ZoneServer::RemoveDeadSpawn(Spawn* spawn){
  5976. AddDeadSpawn(spawn, 0);
  5977. }
  5978. void ZoneServer::AddDeadSpawn(Spawn* spawn, int32 timer){
  5979. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  5980. if (dead_spawns.count(spawn->GetID()) > 0)
  5981. dead_spawns[spawn->GetID()] = Timer::GetCurrentTime2() + timer;
  5982. else if(timer != 0xFFFFFFFF)
  5983. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + timer));
  5984. else{
  5985. if(spawn->IsEntity() && spawn->HasLoot()){
  5986. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + (15000 * spawn->GetLevel() + 240000)));
  5987. SendUpdateDefaultCommand(spawn, "loot", 10);
  5988. }
  5989. else
  5990. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + 10000));
  5991. }
  5992. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5993. }
  5994. void ZoneServer::WritePlayerStatistics() {
  5995. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5996. while(client_itr.Next())
  5997. client_itr->value->GetPlayer()->WritePlayerStatistics();
  5998. }
  5999. bool ZoneServer::SendRadiusSpawnInfo(Client* client, float radius) {
  6000. if (!client)
  6001. return false;
  6002. Spawn* spawn = 0;
  6003. bool ret = false;
  6004. map<int32, Spawn*>::iterator itr;
  6005. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6006. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6007. spawn = itr->second;
  6008. if (spawn && spawn != client->GetPlayer() && !spawn->IsPlayer() && spawn->GetDistance(client->GetPlayer()) <= radius) {
  6009. const char* type = "NPC";
  6010. const char* specialTypeID = "N/A";
  6011. int32 specialID = 0, spawnEntryID = spawn->GetSpawnEntryID();
  6012. if (spawn->IsObject())
  6013. {
  6014. Object* obj = (Object*)spawn;
  6015. specialID = obj->GetID();
  6016. specialTypeID = "GetID";
  6017. type = "Object";
  6018. }
  6019. else if (spawn->IsSign())
  6020. {
  6021. Sign* sign = (Sign*)spawn;
  6022. specialID = sign->GetWidgetID();
  6023. specialTypeID = "WidgetID";
  6024. type = "Sign";
  6025. }
  6026. else if (spawn->IsWidget())
  6027. {
  6028. Widget* widget = (Widget*)spawn;
  6029. specialID = widget->GetWidgetID();
  6030. specialTypeID = "WidgetID";
  6031. if ( specialID == 0xFFFFFFFF )
  6032. specialTypeID = "WidgetID(spawn_widgets entry missing)";
  6033. type = "Widget";
  6034. }
  6035. else if (spawn->IsGroundSpawn())
  6036. {
  6037. GroundSpawn* gs = (GroundSpawn*)spawn;
  6038. specialID = gs->GetGroundSpawnEntryID();
  6039. specialTypeID = "GroundSpawnEntryID";
  6040. type = "GroundSpawn";
  6041. }
  6042. client->Message(CHANNEL_COLOR_RED, "Name: %s (%s), Spawn Table ID: %u, %s: %u", spawn->GetName(), type, spawn->GetDatabaseID(), specialTypeID, specialID);
  6043. client->Message(CHANNEL_COLOR_RED, "Spawn Location ID: %u, Spawn Group ID: %u, SpawnEntryID: %u, Grid ID: %u", spawn->GetSpawnLocationID(), spawn->GetSpawnGroupID(), spawnEntryID, spawn->GetLocation());
  6044. client->Message(CHANNEL_COLOR_RED, "Respawn Time: %u (sec), X: %f, Y: %f, Z: %f Heading: %f", spawn->GetRespawnTime(), spawn->GetX(), spawn->GetY(), spawn->GetZ(), spawn->GetHeading());
  6045. client->Message(CHANNEL_COLOR_YELLOW, "=============================");
  6046. ret = true;
  6047. }
  6048. }
  6049. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6050. return ret;
  6051. }
  6052. void ZoneServer::FindSpawn(Client* client, char* regSearchStr)
  6053. {
  6054. if (!regSearchStr || strlen(regSearchStr) < 1)
  6055. {
  6056. client->SimpleMessage(CHANNEL_COLOR_RED, "Bad ZoneServer::FindSpawn(Client*, char* regSearchStr) attempt, regSearchStr is NULL or empty.");
  6057. return;
  6058. }
  6059. string resString = string(regSearchStr);
  6060. try
  6061. {
  6062. std::regex pre_re_check("^[a-zA-Z0-9_ ]+$");
  6063. bool output = std::regex_match(resString, pre_re_check);
  6064. if (output)
  6065. {
  6066. string newStr(".*");
  6067. newStr.append(regSearchStr);
  6068. newStr.append(".*");
  6069. resString = newStr;
  6070. }
  6071. }
  6072. catch (...)
  6073. {
  6074. client->SimpleMessage(CHANNEL_COLOR_RED, "Try/Catch ZoneServer::FindSpawn(Client*, char* regSearchStr) failure.");
  6075. return;
  6076. }
  6077. std::regex re;
  6078. try {
  6079. re = std::regex(resString, std::regex_constants::icase);
  6080. }
  6081. catch(...) {
  6082. client->SimpleMessage(CHANNEL_COLOR_RED, "Invalid regex for FindSpawn.");
  6083. return;
  6084. }
  6085. client->Message(CHANNEL_NARRATIVE, "RegEx Search Spawn List: %s", regSearchStr);
  6086. client->Message(CHANNEL_NARRATIVE, "Database ID | Spawn Name | X , Y , Z");
  6087. client->Message(CHANNEL_NARRATIVE, "========================");
  6088. map<int32, Spawn*>::iterator itr;
  6089. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6090. int32 spawnsFound = 0;
  6091. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6092. Spawn* spawn = itr->second;
  6093. if (!spawn || !spawn->GetName())
  6094. continue;
  6095. bool output = false;
  6096. try {
  6097. output = std::regex_match(string(spawn->GetName()), re);
  6098. }
  6099. catch (...)
  6100. {
  6101. continue;
  6102. }
  6103. if (output)
  6104. {
  6105. client->Message(CHANNEL_NARRATIVE, "%i | %s | %f , %f , %f", spawn->GetDatabaseID(), spawn->GetName(), spawn->GetX(), spawn->GetY(), spawn->GetZ());
  6106. spawnsFound++;
  6107. }
  6108. }
  6109. client->Message(CHANNEL_NARRATIVE, "========================", spawnsFound);
  6110. client->Message(CHANNEL_NARRATIVE, "%u Results Found.", spawnsFound);
  6111. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6112. }
  6113. void ZoneServer::AddPlayerTracking(Player* player) {
  6114. if (player && !player->GetIsTracking() && players_tracking.count(player->GetDatabaseID()) == 0) {
  6115. Client* client = ((Player*)player)->GetClient();
  6116. if (client) {
  6117. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  6118. if (packet) {
  6119. player->SetIsTracking(true);
  6120. players_tracking.Put(client->GetCharacterID(), player);
  6121. packet->setDataByName("mode", TRACKING_START);
  6122. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  6123. client->QueuePacket(packet->serialize());
  6124. safe_delete(packet);
  6125. }
  6126. }
  6127. }
  6128. }
  6129. void ZoneServer::RemovePlayerTracking(Player* player, int8 mode) {
  6130. if (player && player->GetIsTracking()) {
  6131. Client* client = ((Player*)player)->GetClient();
  6132. if (client) {
  6133. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  6134. if (packet) {
  6135. player->SetIsTracking(false);
  6136. players_tracking.erase(client->GetCharacterID());
  6137. packet->setDataByName("mode", mode);
  6138. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  6139. client->QueuePacket(packet->serialize());
  6140. safe_delete(packet);
  6141. }
  6142. }
  6143. }
  6144. }
  6145. void ZoneServer::ProcessTracking() {
  6146. MutexMap<int32, Player*>::iterator itr = players_tracking.begin();
  6147. while (itr.Next()) {
  6148. Player* player = itr->second;
  6149. if(player->GetClient()) {
  6150. ProcessTracking(player->GetClient());
  6151. }
  6152. }
  6153. }
  6154. void ZoneServer::ProcessTracking(Client* client) {
  6155. if (!client)
  6156. return;
  6157. Player* player = client->GetPlayer();
  6158. if (player && player->GetIsTracking()) {
  6159. MutexMap<int32, Spawn*>::iterator spawn_itr;
  6160. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  6161. if (packet) {
  6162. packet->setDataByName("mode", TRACKING_UPDATE);
  6163. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  6164. vector<TrackedSpawn*> spawns_tracked;
  6165. while (spawn_itr.Next()) {
  6166. Spawn* spawn = spawn_itr->second;
  6167. float distance = player->GetDistance(spawn);
  6168. if (spawn->IsEntity() && distance <= 80 && spawn != player) {
  6169. TrackedSpawn* ts = new TrackedSpawn;
  6170. ts->spawn = spawn;
  6171. ts->distance = distance;
  6172. /* Add spawns in ascending order from closest to furthest */
  6173. if (spawns_tracked.empty())
  6174. spawns_tracked.push_back(ts);
  6175. else {
  6176. vector<TrackedSpawn*>::iterator tracked_itr;
  6177. bool added = false;
  6178. for (tracked_itr = spawns_tracked.begin(); tracked_itr != spawns_tracked.end(); tracked_itr++) {
  6179. TrackedSpawn* cur_ts = *tracked_itr;
  6180. if (ts->distance <= cur_ts->distance) {
  6181. spawns_tracked.insert(tracked_itr, ts);
  6182. added = true;
  6183. break;
  6184. }
  6185. }
  6186. if (!added)
  6187. spawns_tracked.push_back(ts);
  6188. }
  6189. }
  6190. }
  6191. packet->setArrayLengthByName("num_spawns", spawns_tracked.size());
  6192. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  6193. TrackedSpawn* ts = spawns_tracked[i];
  6194. LogWrite(ZONE__DEBUG, 0, "Zone", "%s (%f)", ts->spawn->GetName(), ts->distance);
  6195. packet->setArrayDataByName("spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  6196. packet->setArrayDataByName("spawn_name", ts->spawn->GetName(), i);
  6197. if (ts->spawn->IsPlayer())
  6198. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_PC, i);
  6199. else
  6200. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_NPC, i);
  6201. packet->setArrayDataByName("spawn_con_color", player->GetArrowColor(ts->spawn->GetLevel()), i);
  6202. }
  6203. packet->setArrayLengthByName("num_array1", 0);
  6204. //for (int32 i = 0; i < spawns_tracked.size(); i++) {
  6205. //}
  6206. packet->setArrayLengthByName("num_spawns2", spawns_tracked.size());
  6207. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  6208. TrackedSpawn* ts = spawns_tracked[i];
  6209. packet->setArrayDataByName("list_spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  6210. packet->setArrayDataByName("list_number", i, i);
  6211. }
  6212. client->QueuePacket(packet->serialize());
  6213. safe_delete(packet);
  6214. for (int32 i = 0; i < spawns_tracked.size(); i++)
  6215. safe_delete(spawns_tracked[i]);
  6216. }
  6217. }
  6218. }
  6219. void ZoneServer::SendEpicMobDeathToGuild(Player* killer, Spawn* victim) {
  6220. if (killer && victim) {
  6221. LogWrite(MISC__TODO, 1, "TODO" , "Check if player is in raid\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  6222. if (killer->GetGroupMemberInfo()) {
  6223. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  6224. deque<GroupMemberInfo*>::iterator itr;
  6225. PlayerGroup* group = world.GetGroupManager()->GetGroup(killer->GetGroupMemberInfo()->group_id);
  6226. if (group)
  6227. {
  6228. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  6229. deque<GroupMemberInfo*>* members = group->GetMembers();
  6230. for (itr = members->begin(); itr != members->end(); itr++) {
  6231. GroupMemberInfo* gmi = *itr;
  6232. if (gmi->client) {
  6233. Player* group_member = gmi->client->GetPlayer();
  6234. if (group_member && group_member->GetGuild()) {
  6235. Guild* guild = group_member->GetGuild();
  6236. string message = Guild::GetEpicMobDeathMessage(group_member->GetName(), victim->GetName());
  6237. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  6238. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  6239. }
  6240. }
  6241. }
  6242. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  6243. }
  6244. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  6245. }
  6246. else if (killer->GetGuild()) {
  6247. Guild* guild = killer->GetGuild();
  6248. string message = Guild::GetEpicMobDeathMessage(killer->GetName(), victim->GetName());
  6249. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  6250. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  6251. }
  6252. }
  6253. }
  6254. void ZoneServer::ProcessAggroChecks(Spawn* spawn) {
  6255. if (spawn->GetFactionID() < 1 || spawn->EngagedInCombat())
  6256. return;
  6257. // If faction based combat is not allowed then no need to run the loops so just return out
  6258. if(!rule_manager.GetZoneRule(GetZoneID(), R_Faction, AllowFactionBasedCombat)->GetBool())
  6259. return;
  6260. if (spawn && spawn->IsNPC() && spawn->Alive())
  6261. CheckEnemyList((NPC*)spawn);
  6262. }
  6263. void ZoneServer::SendUpdateTitles(Client* client, Title* suffix, Title* prefix) {
  6264. assert(client);
  6265. if (client->GetVersion() > 561)
  6266. SendUpdateTitles(client->GetPlayer(), suffix, prefix);
  6267. }
  6268. void ZoneServer::SendUpdateTitles(Spawn *spawn, Title *suffix, Title *prefix) {
  6269. if (!spawn)
  6270. return;
  6271. vector<Client*>::iterator itr;
  6272. PacketStruct *packet;
  6273. Client* current_client;
  6274. MClientList.readlock(__FUNCTION__, __LINE__);
  6275. for (itr = clients.begin(); itr != clients.end(); itr++) {
  6276. current_client = *itr;
  6277. if (current_client->GetVersion() <= 561)
  6278. continue;
  6279. if (!(packet = configReader.getStruct("WS_UpdateTitle", current_client->GetVersion())))
  6280. continue;
  6281. packet->setDataByName("player_id", current_client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  6282. packet->setDataByName("player_name", spawn->GetName());
  6283. packet->setDataByName("unknown1", 1, 1);
  6284. if(suffix)
  6285. packet->setDataByName("suffix_title", suffix->GetName());
  6286. else
  6287. packet->setDataByName("suffix_title", spawn->GetSuffixTitle());
  6288. if(prefix)
  6289. packet->setDataByName("prefix_title", prefix->GetName());
  6290. else
  6291. packet->setDataByName("prefix_title", spawn->GetPrefixTitle());
  6292. packet->setDataByName("last_name", spawn->GetLastName());
  6293. packet->setDataByName("sub_title", spawn->GetSubTitle());
  6294. current_client->QueuePacket(packet->serialize());
  6295. safe_delete(packet);
  6296. }
  6297. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6298. }
  6299. void ZoneServer::AddTransportSpawn(Spawn* spawn){
  6300. if(!spawn)
  6301. return;
  6302. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  6303. transport_spawns.push_back(spawn->GetID());
  6304. spawn->SetTransportSpawn(true);
  6305. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  6306. }
  6307. Spawn* ZoneServer::GetClosestTransportSpawn(float x, float y, float z){
  6308. Spawn* spawn = 0;
  6309. Spawn* closest_spawn = 0;
  6310. float closest_distance = 0.0;
  6311. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  6312. vector<int32>::iterator itr = transport_spawns.begin();
  6313. while(itr != transport_spawns.end()){
  6314. spawn = GetSpawnByID(*itr);
  6315. if(spawn){
  6316. if(closest_distance == 0.0){
  6317. closest_spawn = spawn;
  6318. closest_distance = spawn->GetDistance(x, y, z);
  6319. }
  6320. else if(spawn->GetDistance(x, y, z) < closest_distance){
  6321. closest_spawn = spawn;
  6322. closest_distance = spawn->GetDistance(x, y, z);
  6323. }
  6324. itr++;
  6325. }
  6326. else
  6327. itr = transport_spawns.erase(itr);
  6328. }
  6329. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  6330. return closest_spawn;
  6331. }
  6332. Spawn* ZoneServer::GetTransportByRailID(sint64 rail_id){
  6333. Spawn* spawn = 0;
  6334. Spawn* closest_spawn = 0;
  6335. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6336. vector<int32>::iterator itr = transport_spawns.begin();
  6337. while(itr != transport_spawns.end()){
  6338. spawn = GetSpawnByID(*itr);
  6339. //printf("Rail id: %i vs %i\n", spawn ? spawn->GetRailID() : 0, rail_id);
  6340. if(spawn && spawn->GetRailID() == rail_id){
  6341. closest_spawn = spawn;
  6342. break;
  6343. }
  6344. itr++;
  6345. }
  6346. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6347. return closest_spawn;
  6348. }
  6349. void ZoneServer::SetRain(float val) {
  6350. rain = val;
  6351. vector<Client*>::iterator itr;
  6352. MClientList.readlock(__FUNCTION__, __LINE__);
  6353. for (itr = clients.begin(); itr != clients.end(); itr++) {
  6354. Client* client = *itr;
  6355. client->GetPlayer()->GetInfoStruct()->set_rain(val);
  6356. client->GetPlayer()->SetCharSheetChanged(true);
  6357. if( val >= 0.75 && !weather_signaled )
  6358. {
  6359. client->SimpleMessage(CHANNEL_NARRATIVE, "It starts to rain.");
  6360. }
  6361. else if( val < 0.75 && weather_signaled )
  6362. {
  6363. client->SimpleMessage(CHANNEL_NARRATIVE, "It stops raining.");
  6364. }
  6365. }
  6366. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6367. if (val >= 0.75 && !weather_signaled) {
  6368. weather_signaled = true;
  6369. ProcessSpawnConditional(SPAWN_CONDITIONAL_RAINING);
  6370. }
  6371. else if (val < 0.75 && weather_signaled) {
  6372. weather_signaled = false;
  6373. ProcessSpawnConditional(SPAWN_CONDITIONAL_NOT_RAINING);
  6374. }
  6375. }
  6376. void ZoneServer::SetWind(float val) {
  6377. vector<Client*>::iterator itr;
  6378. MClientList.readlock(__FUNCTION__, __LINE__);
  6379. for (itr = clients.begin(); itr != clients.end(); itr++) {
  6380. Client* client = *itr;
  6381. client->GetPlayer()->GetInfoStruct()->set_wind(val);
  6382. client->GetPlayer()->SetCharSheetChanged(true);
  6383. }
  6384. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6385. }
  6386. void ZoneServer::ProcessWeather()
  6387. {
  6388. // if the global rule to disable weather is set, or if the `weather_allowed` field in the zone record == 0, do not process weather
  6389. if( !weather_enabled || !isWeatherAllowed() )
  6390. return;
  6391. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Processing weather changes", zone_name);
  6392. float new_weather = 0;
  6393. float weather_offset = 0;
  6394. bool change_weather = false;
  6395. // check to see if it is time to change the weather according to weather_frequency (time between changes)
  6396. if( weather_last_changed_time <= (Timer::GetUnixTimeStamp() - weather_frequency) )
  6397. {
  6398. LogWrite(ZONE__DEBUG, 2, "Zone", "%s: Checking for weather changes", zone_name);
  6399. // reset last changed time (frequency check)
  6400. weather_last_changed_time = Timer::GetUnixTimeStamp();
  6401. // this is the chance a weather change occurs at all at the expired interval
  6402. int8 weather_random = MakeRandomInt(1, 100);
  6403. LogWrite(ZONE__DEBUG, 2, "Zone", "%s: Chance to change weather: %i%%, rolled: %i%% - Change weather: %s", zone_name, weather_change_chance, weather_random, weather_random <= weather_change_chance ? "True" : "False");
  6404. if( weather_random <= weather_change_chance )
  6405. {
  6406. change_weather = true;
  6407. weather_offset = weather_change_amount;
  6408. if( weather_type == 3 ) // chaotic weather patterns, random weather between min/max
  6409. {
  6410. new_weather = MakeRandomFloat(weather_min_severity, weather_max_severity);
  6411. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Chaotic weather severity changed to %.2f", zone_name, new_weather);
  6412. weather_pattern = 2;
  6413. }
  6414. else if( weather_type == 2 ) // random weather patterns, combination of normal + dynamic + max_offset
  6415. {
  6416. weather_offset = MakeRandomFloat(weather_change_amount, weather_dynamic_offset);
  6417. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Random weather severity changed by %.2f", zone_name, weather_offset);
  6418. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  6419. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  6420. if( weather_random <= weather_alter )
  6421. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  6422. }
  6423. else if( weather_type == 1 ) // dynamic weather patterns, weather may not reach min/max
  6424. {
  6425. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  6426. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  6427. if( weather_random <= weather_alter )
  6428. {
  6429. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  6430. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Dynamic weather pattern changed to %i", zone_name, weather_pattern);
  6431. }
  6432. }
  6433. else // normal weather patterns, weather starts at min, goes to max, then back down again
  6434. {
  6435. // do nothing (processed below)
  6436. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Normal weather severity changed by %.2f", zone_name, weather_offset);
  6437. }
  6438. // when all done, change the weather
  6439. if( change_weather )
  6440. {
  6441. if( weather_pattern == 1 )
  6442. {
  6443. // weather is getting worse, til it reaches weather_max_severity
  6444. new_weather = ( weather_current_severity <= weather_max_severity ) ? weather_current_severity + weather_offset : weather_max_severity;
  6445. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Increased weather severity by %.2f", zone_name, weather_offset);
  6446. if(new_weather > weather_max_severity)
  6447. {
  6448. new_weather = weather_max_severity - weather_offset;
  6449. weather_pattern = 0;
  6450. }
  6451. }
  6452. else if( weather_pattern == 0 )
  6453. {
  6454. // weather is clearing up, til it reaches weather_min_severity
  6455. new_weather = ( weather_current_severity >= weather_min_severity ) ? weather_current_severity - weather_offset : weather_min_severity;
  6456. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Decreased weather severity by %.2f", zone_name, weather_offset);
  6457. if(new_weather < weather_min_severity)
  6458. {
  6459. new_weather = weather_min_severity + weather_offset;
  6460. weather_pattern = 1;
  6461. }
  6462. }
  6463. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather change triggered from %.2f to %.2f", zone_name, weather_current_severity, new_weather);
  6464. this->SetRain(new_weather);
  6465. weather_current_severity = new_weather;
  6466. }
  6467. }
  6468. }
  6469. else
  6470. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Not time to change weather yet", zone_name);
  6471. }
  6472. void ZoneServer::HidePrivateSpawn(Spawn* spawn) {
  6473. if (!spawn->IsPrivateSpawn())
  6474. return;
  6475. Client* client = 0;
  6476. Player* player = 0;
  6477. PacketStruct* packet = 0;
  6478. int32 packet_version = 0;
  6479. MutexList<Client*>::iterator itr = connected_clients.begin();
  6480. while (itr->Next()) {
  6481. client = itr->value;
  6482. player = client->GetPlayer();
  6483. if (player->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->IsSendingSpawn(spawn->GetID())) {
  6484. if (!packet || packet_version != client->GetVersion()) {
  6485. safe_delete(packet);
  6486. packet_version = client->GetVersion();
  6487. packet = configReader.getStruct("WS_DestroyGhostCmd", packet_version);
  6488. }
  6489. SendRemoveSpawn(client, spawn, packet);
  6490. if(spawn_range_map.count(client) > 0)
  6491. spawn_range_map.Get(client)->erase(spawn->GetID());
  6492. if(player->GetTarget() == spawn)
  6493. player->SetTarget(0);
  6494. }
  6495. }
  6496. safe_delete(packet);
  6497. }
  6498. SpawnLocation* ZoneServer::GetSpawnLocation(int32 id) {
  6499. SpawnLocation* ret = 0;
  6500. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  6501. if (spawn_location_list.count(id) > 0)
  6502. ret = spawn_location_list[id];
  6503. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  6504. return ret;
  6505. }
  6506. void ZoneServer::PlayAnimation(Spawn* spawn, int32 visual_state, Spawn* spawn2, int8 hide_type){
  6507. Client* client = 0;
  6508. PacketStruct* packet = 0;
  6509. Spawn* exclude_spawn = 0;
  6510. if (!spawn)
  6511. return;
  6512. if (spawn2){
  6513. if(hide_type == 1){
  6514. if(spawn2->IsPlayer()) {
  6515. client = ((Player*)spawn2)->GetClient();
  6516. if(client){
  6517. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6518. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  6519. packet->setDataByName("anim_type", visual_state);
  6520. client->QueuePacket(packet->serialize());
  6521. }
  6522. safe_delete(packet);
  6523. return;
  6524. }
  6525. }
  6526. if(hide_type == 2)
  6527. exclude_spawn = spawn2;
  6528. }
  6529. vector<Client*>::iterator client_itr;
  6530. MClientList.readlock(__FUNCTION__, __LINE__);
  6531. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  6532. client = *client_itr;
  6533. if(spawn->GetDistance(client->GetPlayer()) > 50)
  6534. continue;
  6535. if(exclude_spawn == client->GetPlayer())
  6536. continue;
  6537. if(!client->IsReadyForUpdates()) // client is not in world yet so we shouldn't be sending animations of spawns yet
  6538. continue;
  6539. if(!packet || packet->GetVersion() != client->GetVersion()) {
  6540. safe_delete(packet);
  6541. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6542. }
  6543. if (packet) {
  6544. int32 spawn_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  6545. if(spawn_id) {
  6546. packet->setDataByName("spawn_id", spawn_id);
  6547. packet->setDataByName("anim_type", visual_state);
  6548. client->QueuePacket(packet->serialize());
  6549. }
  6550. }
  6551. }
  6552. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6553. safe_delete(packet);
  6554. }
  6555. vector<Spawn*> ZoneServer::GetSpawnsByID(int32 id) {
  6556. vector<Spawn*> tmp_list;
  6557. Spawn* spawn;
  6558. map<int32, Spawn*>::iterator itr;
  6559. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6560. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6561. spawn = itr->second;
  6562. if (spawn && (spawn->GetDatabaseID() == id))
  6563. tmp_list.push_back(spawn);
  6564. }
  6565. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6566. return tmp_list;
  6567. }
  6568. vector<Spawn*> ZoneServer::GetSpawnsByRailID(sint64 rail_id) {
  6569. vector<Spawn*> tmp_list;
  6570. Spawn* spawn;
  6571. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6572. vector<int32>::iterator itr = transport_spawns.begin();
  6573. while(itr != transport_spawns.end()){
  6574. spawn = GetSpawnByID(*itr);
  6575. if(spawn && spawn->GetRailID() == rail_id){
  6576. tmp_list.push_back(spawn);
  6577. }
  6578. itr++;
  6579. }
  6580. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6581. return tmp_list;
  6582. }
  6583. void ZoneServer::RemovePlayerPassenger(int32 char_id) {
  6584. vector<Spawn*> tmp_list;
  6585. Spawn* spawn;
  6586. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6587. vector<int32>::iterator itr = transport_spawns.begin();
  6588. while(itr != transport_spawns.end()){
  6589. spawn = GetSpawnByID(*itr);
  6590. if(spawn) {
  6591. spawn->RemoveRailPassenger(char_id);
  6592. }
  6593. itr++;
  6594. }
  6595. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6596. }
  6597. bool ZoneServer::SetPlayerTargetByName(Client* originator, char* targetName, float distance) {
  6598. if(!targetName || !originator->GetPlayer()) {
  6599. return false;
  6600. }
  6601. int32 name_size = strlen(targetName);
  6602. if(name_size < 1 || name_size > 127) {
  6603. return false;
  6604. }
  6605. auto loc = glm::vec3(originator->GetPlayer()->GetX(), originator->GetPlayer()->GetZ(), originator->GetPlayer()->GetY());
  6606. std::vector<int32> grids_by_radius;
  6607. if(originator->GetPlayer()->GetMap()) {
  6608. grids_by_radius = GetGridsByLocation(originator->GetPlayer(), loc, distance);
  6609. }
  6610. else {
  6611. grids_by_radius.push_back(originator->GetPlayer()->GetLocation());
  6612. }
  6613. bool found_target = false;
  6614. float spawn_dist = 999999.0f;
  6615. Spawn* target_spawn = nullptr;
  6616. float tmp_dist = 0.0f;
  6617. MGridMaps.lock_shared();
  6618. std::vector<int32>::iterator grid_radius_itr;
  6619. for(grid_radius_itr = grids_by_radius.begin(); grid_radius_itr != grids_by_radius.end(); grid_radius_itr++) {
  6620. std::map<int32, GridMap*>::iterator grids = grid_maps.find((*grid_radius_itr));
  6621. if(grids != grid_maps.end()) {
  6622. grids->second->MSpawns.lock_shared();
  6623. typedef map <int32, Spawn*> SpawnMapType;
  6624. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  6625. Spawn* spawn = it->second;
  6626. bool inSameGroup = false;
  6627. if(spawn->IsEntity()) {
  6628. GroupMemberInfo* gmi = originator->GetPlayer()->GetGroupMemberInfo();
  6629. inSameGroup = (((Entity*)spawn)->GetGroupMemberInfo() && originator->GetPlayer()->GetGroupMemberInfo() && ((Entity*)spawn)->GetGroupMemberInfo()->group_id == originator->GetPlayer()->GetGroupMemberInfo()->group_id);
  6630. }
  6631. if (spawn && spawn->appearance.targetable > 0 && !strncasecmp(spawn->GetName(), targetName, name_size) && (inSameGroup || (((tmp_dist = spawn->GetDistance(originator->GetPlayer(), true)) <= distance) && tmp_dist < spawn_dist && originator->GetPlayer()->CheckLoS(spawn)))) {
  6632. spawn_dist = tmp_dist;
  6633. target_spawn = spawn;
  6634. }
  6635. }
  6636. grids->second->MSpawns.unlock_shared();
  6637. }
  6638. }
  6639. MGridMaps.unlock_shared();
  6640. if(target_spawn != nullptr) {
  6641. originator->TargetSpawn(target_spawn);
  6642. }
  6643. return (target_spawn != nullptr);
  6644. }
  6645. std::vector<int32> ZoneServer::GetGridsByLocation(Spawn* originator, glm::vec3 loc, float distance) {
  6646. std::vector<int32> grids_by_radius;
  6647. if(originator == nullptr)
  6648. return grids_by_radius;
  6649. if(originator->GetMap()) {
  6650. grids_by_radius = originator->GetMap()->GetGridsByPoint(loc, distance);
  6651. if(default_zone_map && default_zone_map != originator->GetMap()) {
  6652. std::vector<int32> default_grids = default_zone_map->GetGridsByPoint(loc, distance);
  6653. std::unordered_set<int32> elements(grids_by_radius.begin(), grids_by_radius.end());
  6654. for (const auto& elem : default_grids) {
  6655. if (elements.find(elem) == elements.end()) {
  6656. grids_by_radius.push_back(elem);
  6657. elements.insert(elem);
  6658. }
  6659. }
  6660. }
  6661. }
  6662. return grids_by_radius;
  6663. }
  6664. std::vector<std::pair<int32, float>> ZoneServer::GetAttackableSpawnsByDistance(Spawn* caster, float distance) {
  6665. std::vector<std::pair<int32, float>> spawns_by_distance;
  6666. Spawn* spawn = 0;
  6667. auto loc = glm::vec3(caster->GetX(), caster->GetZ(), caster->GetY());
  6668. std::vector<int32> grids_by_radius;
  6669. if(caster->GetMap()) {
  6670. grids_by_radius = GetGridsByLocation(caster, loc, distance);
  6671. }
  6672. else {
  6673. grids_by_radius.push_back(caster->GetLocation());
  6674. }
  6675. float tmp_dist = 0.0f;
  6676. MGridMaps.lock_shared();
  6677. std::vector<int32>::iterator grid_radius_itr;
  6678. for(grid_radius_itr = grids_by_radius.begin(); grid_radius_itr != grids_by_radius.end(); grid_radius_itr++) {
  6679. std::map<int32, GridMap*>::iterator grids = grid_maps.find((*grid_radius_itr));
  6680. if(grids != grid_maps.end()) {
  6681. grids->second->MSpawns.lock_shared();
  6682. typedef map <int32, Spawn*> SpawnMapType;
  6683. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  6684. Spawn* spawn = it->second;
  6685. if (spawn && spawn->IsNPC() && spawn->appearance.attackable > 0 && spawn->GetID() > 0 && spawn->GetID() != caster->GetID() &&
  6686. spawn->Alive() && ((tmp_dist = spawn->GetDistance(caster, true)) <= distance)) {
  6687. spawns_by_distance.push_back({spawn->GetID(), tmp_dist});
  6688. }
  6689. }
  6690. grids->second->MSpawns.unlock_shared();
  6691. }
  6692. }
  6693. MGridMaps.unlock_shared();
  6694. std::sort(spawns_by_distance.begin(), spawns_by_distance.end(), compareByValue);
  6695. return spawns_by_distance;
  6696. }
  6697. void ZoneServer::ResurrectSpawn(Spawn* spawn, Client* client) {
  6698. if(!client || !spawn)
  6699. return;
  6700. PendingResurrection* rez = client->GetCurrentRez();
  6701. if(!rez || !rez->caster)
  6702. return;
  6703. PacketStruct* packet = 0;
  6704. float power_perc = rez->mp_perc;
  6705. float health_perc = rez->hp_perc;
  6706. Spawn* caster_spawn = rez->caster;
  6707. sint32 heal_amt = 0;
  6708. sint32 power_amt = 0;
  6709. bool no_calcs = rez->no_calcs;
  6710. int8 crit_mod = rez->crit_mod;
  6711. Entity* caster = 0;
  6712. InfoStruct* info = 0;
  6713. bool crit = false;
  6714. string heal_spell = rez->heal_name;
  6715. int16 heal_packet_type = 0;
  6716. int16 power_packet_type = 0;
  6717. //Calculations for how much to heal the spawn
  6718. if(health_perc > 0)
  6719. heal_amt = (spawn->GetTotalHP() * (health_perc / 100));
  6720. if(power_perc > 0)
  6721. power_amt = (spawn->GetTotalPower() * (power_perc / 100));
  6722. if(caster_spawn->IsEntity()){
  6723. caster = ((Entity*)caster_spawn);
  6724. info = caster->GetInfoStruct();
  6725. }
  6726. if(!no_calcs && caster){
  6727. heal_amt = caster->CalculateHealAmount(spawn, heal_amt, crit_mod, &crit);
  6728. power_amt = caster->CalculateHealAmount(spawn, power_amt, crit_mod, &crit);
  6729. }
  6730. //Set this rez as a crit to be passed to subspell (not yet used)
  6731. rez->crit = true;
  6732. //Set Heal amt to 1 if 0 now so the player has health
  6733. if(heal_amt == 0)
  6734. heal_amt = 1;
  6735. if(heal_amt > spawn->GetTotalHP())
  6736. heal_amt = spawn->GetTotalHP();
  6737. if(power_amt > spawn->GetTotalPower())
  6738. power_amt = spawn->GetTotalPower();
  6739. spawn->SetAlive(true);
  6740. spawn->SetHP(heal_amt);
  6741. if(power_amt > 0)
  6742. spawn->SetPower(power_amt);
  6743. if(client && caster){
  6744. EQ2Packet* move = ((Player*)spawn)->Move(caster->GetX(), caster->GetY(), caster->GetZ(), client->GetVersion());
  6745. if(move)
  6746. client->QueuePacket(move);
  6747. }
  6748. if(crit){
  6749. power_packet_type = HEAL_PACKET_TYPE_CRIT_MANA;
  6750. heal_packet_type = HEAL_PACKET_TYPE_CRIT_HEAL;
  6751. }
  6752. else {
  6753. power_packet_type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  6754. heal_packet_type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  6755. }
  6756. SendHealPacket(caster, spawn, heal_packet_type, heal_amt, heal_spell.c_str());
  6757. if(power_amt > 0)
  6758. SendHealPacket(caster, spawn, power_packet_type, power_amt, heal_spell.c_str());
  6759. //The following code sets the spawn as alive
  6760. if(dead_spawns.count(spawn->GetID()) > 0)
  6761. dead_spawns.erase(spawn->GetID());
  6762. if(spawn->IsPlayer()){
  6763. spawn->SetSpawnType(4);
  6764. client = ((Player*)spawn)->GetClient();
  6765. if(client){
  6766. packet = configReader.getStruct("WS_Resurrected", client->GetVersion());
  6767. if(packet){
  6768. client->QueuePacket(packet->serialize());
  6769. }
  6770. safe_delete(packet);
  6771. if(client->GetVersion() <= 561) {
  6772. ClientPacketFunctions::SendServerControlFlagsClassic(client, 8, 0);
  6773. ClientPacketFunctions::SendServerControlFlagsClassic(client, 16, 0);
  6774. }
  6775. else {
  6776. ClientPacketFunctions::SendServerControlFlags(client, 1, 8, 0);
  6777. ClientPacketFunctions::SendServerControlFlags(client, 1, 16, 0);
  6778. }
  6779. client->SimpleMessage(CHANNEL_NARRATIVE, "You regain consciousness!");
  6780. }
  6781. }
  6782. spawn->SendSpawnChanges(true);
  6783. spawn->SetTempActionState(-1);
  6784. spawn->appearance.attackable = 1;
  6785. }
  6786. void ZoneServer::SendDispellPacket(Entity* caster, Spawn* target, string dispell_name, string spell_name, int8 dispell_type){
  6787. if(!caster || !target)
  6788. return;
  6789. Client* client = 0;
  6790. Player* player = 0;
  6791. PacketStruct* packet = 0;
  6792. vector<Client*>::iterator client_itr;
  6793. MClientList.readlock(__FUNCTION__, __LINE__);
  6794. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++){
  6795. client = *client_itr;
  6796. if(!client || !(player = client->GetPlayer()) || (player != caster && ((caster && player->WasSentSpawn(caster->GetID()) == false) || (target && player->WasSentSpawn(target->GetID()) == false))))
  6797. continue;
  6798. if(caster && caster->GetDistance(player) > 50)
  6799. continue;
  6800. if(target && target->GetDistance(player) > 50)
  6801. continue;
  6802. packet = configReader.getStruct("WS_HearDispell", client->GetVersion());
  6803. if(packet){
  6804. packet->setDataByName("spell_name", spell_name.c_str());
  6805. packet->setDataByName("dispell_name", dispell_name.c_str());
  6806. packet->setDataByName("caster", player->GetIDWithPlayerSpawn(caster));
  6807. packet->setDataByName("target", player->GetIDWithPlayerSpawn(target));
  6808. packet->setDataByName("type", dispell_type);
  6809. client->QueuePacket(packet->serialize());
  6810. }
  6811. safe_delete(packet);
  6812. }
  6813. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6814. }
  6815. void ZoneServer::DismissAllPets() {
  6816. Spawn* spawn = 0;
  6817. map<int32, Spawn*>::iterator itr;
  6818. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6819. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6820. spawn = itr->second;
  6821. if (spawn && spawn->IsEntity())
  6822. ((Entity*)spawn)->DismissAllPets();
  6823. }
  6824. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6825. }
  6826. void ZoneServer::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target, bool remove_caster){
  6827. if (spellProcess)
  6828. spellProcess->RemoveTargetFromSpell(spell, target, remove_caster);
  6829. }
  6830. void ZoneServer::ClearHate(Entity* entity) {
  6831. Spawn* spawn = 0;
  6832. map<int32, Spawn*>::iterator itr;
  6833. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6834. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6835. spawn = itr->second;
  6836. if (spawn && spawn->IsNPC() && ((NPC*)spawn)->Brain())
  6837. ((NPC*)spawn)->Brain()->ClearHate(entity);
  6838. }
  6839. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6840. }
  6841. ThreadReturnType ZoneLoop(void* tmp) {
  6842. #ifdef WIN32
  6843. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6844. #endif
  6845. if (tmp == 0) {
  6846. ThrowError("ZoneLoop(): tmp = 0!");
  6847. THREAD_RETURN(NULL);
  6848. }
  6849. ZoneServer* zs = (ZoneServer*) tmp;
  6850. while (zs->Process()) {
  6851. if(zs->GetClientCount() == 0)
  6852. Sleep(1000);
  6853. else
  6854. Sleep(10);
  6855. }
  6856. // we failed, time to disappear, no more processing period
  6857. safe_delete(zs);
  6858. THREAD_RETURN(NULL);
  6859. }
  6860. ThreadReturnType SpawnLoop(void* tmp) {
  6861. #ifdef WIN32
  6862. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6863. #endif
  6864. if (tmp == 0) {
  6865. ThrowError("SpawnLoop(): tmp = 0!");
  6866. THREAD_RETURN(NULL);
  6867. }
  6868. ZoneServer* zs = (ZoneServer*) tmp;
  6869. #ifndef NO_CATCH
  6870. try {
  6871. #endif
  6872. zs->spawnthread_active = true;
  6873. while (zs->SpawnProcess()) {
  6874. if(zs->GetClientCount() == 0)
  6875. Sleep(1000);
  6876. else
  6877. Sleep(20);
  6878. }
  6879. zs->spawnthread_active = false;
  6880. #ifndef NO_CATCH
  6881. }
  6882. catch(...) {
  6883. zs->spawnthread_active = false;
  6884. zs->initial_spawn_threads_active = 0;
  6885. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop, shutting down zone '%s'...", zs->GetZoneName());
  6886. try{
  6887. zs->Shutdown();
  6888. }
  6889. catch(...){
  6890. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop while shutting down zone '%s'...", zs->GetZoneName());
  6891. throw;
  6892. }
  6893. throw;
  6894. }
  6895. #endif
  6896. THREAD_RETURN(NULL);
  6897. }
  6898. ThreadReturnType SendInitialSpawns(void* tmp) {
  6899. #ifdef WIN32
  6900. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  6901. #endif
  6902. if (tmp == 0) {
  6903. ThrowError("SendInitialSpawns(): tmp = 0!");
  6904. THREAD_RETURN(NULL);
  6905. }
  6906. Client* client = (Client*) tmp;
  6907. client->GetCurrentZone()->SendZoneSpawns(client);
  6908. THREAD_RETURN(NULL);
  6909. }
  6910. ThreadReturnType SendLevelChangedSpawns(void* tmp) {
  6911. #ifdef WIN32
  6912. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6913. #endif
  6914. if (tmp == 0) {
  6915. ThrowError("SendLevelChangedSpawns(): tmp = 0!");
  6916. THREAD_RETURN(NULL);
  6917. }
  6918. Client* client = (Client*)tmp;
  6919. client->GetCurrentZone()->SendAllSpawnsForLevelChange(client);
  6920. THREAD_RETURN(NULL);
  6921. }
  6922. void ZoneServer::SetSpawnStructs(Client* client) {
  6923. int16 client_ver = client->GetVersion();
  6924. Player* player = client->GetPlayer();
  6925. //Save a copy of the correct spawn substructs for the client's player, save here a copy if we don't have one
  6926. PacketStruct* pos = configReader.getStruct("Substruct_SpawnPositionStruct", client_ver);
  6927. player->SetSpawnPosStruct(pos);
  6928. if (versioned_pos_structs.count(pos->GetVersion()) == 0)
  6929. versioned_pos_structs[pos->GetVersion()] = new PacketStruct(pos, true);
  6930. PacketStruct* vis = configReader.getStruct("Substruct_SpawnVisualizationInfoStruct", client_ver);
  6931. player->SetSpawnVisStruct(vis);
  6932. if (versioned_vis_structs.count(vis->GetVersion()) == 0)
  6933. versioned_vis_structs[vis->GetVersion()] = new PacketStruct(vis, true);
  6934. PacketStruct* info = configReader.getStruct("Substruct_SpawnInfoStruct", client_ver);
  6935. player->SetSpawnInfoStruct(info);
  6936. if (versioned_info_structs.count(info->GetVersion()) == 0)
  6937. versioned_info_structs[info->GetVersion()] = new PacketStruct(info, true);
  6938. PacketStruct* header = configReader.getStruct("WS_SpawnStruct_Header", client_ver);
  6939. player->SetSpawnHeaderStruct(header);
  6940. PacketStruct* footer = configReader.getStruct("WS_SpawnStruct_Footer", client_ver);
  6941. player->SetSpawnFooterStruct(footer);
  6942. PacketStruct* sfooter = configReader.getStruct("WS_SignWidgetSpawnStruct_Footer", client_ver);
  6943. player->SetSignFooterStruct(sfooter);
  6944. PacketStruct* wfooter = configReader.getStruct("WS_WidgetSpawnStruct_Footer", client_ver);
  6945. player->SetWidgetFooterStruct(wfooter);
  6946. }
  6947. Spawn* ZoneServer::GetSpawn(int32 id){
  6948. Spawn* ret = 0;
  6949. if(GetNPC(id))
  6950. ret = GetNewNPC(id);
  6951. else if(this->GetObject(id))
  6952. ret = GetNewObject(id);
  6953. else if(GetWidget(id))
  6954. ret = GetNewWidget(id);
  6955. else if(GetSign(id))
  6956. ret = GetNewSign(id);
  6957. else if(GetGroundSpawn(id))
  6958. ret = GetNewGroundSpawn(id);
  6959. // Unable to find the spawn in the list lets attempt to add it if we are not currently reloading
  6960. else if (!reloading && database.LoadNPC(this, id)) {
  6961. if (GetNPC(id))
  6962. ret = GetNewNPC(id);
  6963. else
  6964. LogWrite(NPC__ERROR, 0, "NPC", "Database inserted npc (%u) but was still unable to retrieve it!", id);
  6965. }
  6966. else if (!reloading && database.LoadObject(this, id)) {
  6967. if (this->GetObject(id))
  6968. ret = GetNewObject(id);
  6969. else
  6970. LogWrite(OBJECT__ERROR, 0, "Object", "Database inserted object (%u) but was still unable to retrieve it!", id);
  6971. }
  6972. else if (!reloading && database.LoadWidget(this, id)) {
  6973. if (GetWidget(id))
  6974. ret = GetNewWidget(id);
  6975. else
  6976. LogWrite(WIDGET__ERROR, 0, "Widget", "Database inserted widget (%u) but was still unable to retrieve it!", id);
  6977. }
  6978. else if (!reloading && database.LoadSign(this, id)) {
  6979. if (GetSign(id))
  6980. ret = GetNewSign(id);
  6981. else
  6982. LogWrite(SIGN__ERROR, 0, "Sign", "Database inserted sign (%u) but was still unable to retrieve it!", id);
  6983. }
  6984. else if (!reloading && database.LoadGroundSpawn(this, id)) {
  6985. if (GetGroundSpawn(id))
  6986. ret = GetNewGroundSpawn(id);
  6987. else
  6988. LogWrite(GROUNDSPAWN__ERROR, 0, "GSpawn", "Database inserted ground spawn (%u) but was still unable to retrieve it!", id);
  6989. }
  6990. if(ret && ret->IsOmittedByDBFlag())
  6991. {
  6992. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) was skipped due to a missing expansion / holiday flag being met.", id);
  6993. safe_delete(ret);
  6994. ret = 0;
  6995. }
  6996. if(ret)
  6997. ret->SetID(Spawn::NextID());
  6998. return ret;
  6999. }
  7000. vector<EntityCommand*>* ZoneServer::GetEntityCommandList(int32 id){
  7001. if(entity_command_list.count(id) > 0)
  7002. return entity_command_list[id];
  7003. else
  7004. return 0;
  7005. }
  7006. void ZoneServer::SetEntityCommandList(int32 id, EntityCommand* command) {
  7007. if (entity_command_list.count(id) == 0)
  7008. entity_command_list[id] = new vector<EntityCommand*>;
  7009. entity_command_list[id]->push_back(command);
  7010. }
  7011. EntityCommand* ZoneServer::GetEntityCommand(int32 id, string name) {
  7012. EntityCommand* ret = 0;
  7013. if (entity_command_list.count(id) == 0)
  7014. return ret;
  7015. vector<EntityCommand*>::iterator itr;
  7016. for (itr = entity_command_list[id]->begin(); itr != entity_command_list[id]->end(); itr++) {
  7017. if ((*itr)->name == name) {
  7018. ret = (*itr);
  7019. break;
  7020. }
  7021. }
  7022. return ret;
  7023. }
  7024. void ZoneServer::ClearEntityCommands() {
  7025. if (entity_command_list.size() > 0) {
  7026. map<int32, vector<EntityCommand*>* >::iterator itr;
  7027. for (itr = entity_command_list.begin(); itr != entity_command_list.end(); itr++) {
  7028. vector<EntityCommand*>* entity_commands = itr->second;
  7029. if (entity_commands && entity_commands->size() > 0) {
  7030. vector<EntityCommand*>::iterator v_itr;
  7031. for (v_itr = entity_commands->begin(); v_itr != entity_commands->end(); v_itr++)
  7032. safe_delete(*v_itr);
  7033. entity_commands->clear();
  7034. }
  7035. safe_delete(entity_commands);
  7036. }
  7037. entity_command_list.clear();
  7038. }
  7039. }
  7040. void ZoneServer::AddNPCSkill(int32 list_id, int32 skill_id, int16 value){
  7041. npc_skill_list[list_id][skill_id] = value;
  7042. }
  7043. map<string, Skill*>* ZoneServer::GetNPCSkills(int32 primary_list, int32 secondary_list){
  7044. map<string, Skill*>* ret = 0;
  7045. if(npc_skill_list.count(primary_list) > 0){
  7046. ret = new map<string, Skill*>();
  7047. map<int32, int16>::iterator itr;
  7048. Skill* tmpSkill = 0;
  7049. for(itr = npc_skill_list[primary_list].begin(); itr != npc_skill_list[primary_list].end(); itr++){
  7050. tmpSkill = master_skill_list.GetSkill(itr->first);
  7051. if(tmpSkill){
  7052. tmpSkill = new Skill(tmpSkill);
  7053. tmpSkill->current_val = itr->second;
  7054. tmpSkill->max_val = tmpSkill->current_val+5;
  7055. (*ret)[tmpSkill->name.data] = tmpSkill;
  7056. }
  7057. }
  7058. }
  7059. if(npc_skill_list.count(secondary_list) > 0){
  7060. if(!ret)
  7061. ret = new map<string, Skill*>();
  7062. map<int32, int16>::iterator itr;
  7063. Skill* tmpSkill = 0;
  7064. for(itr = npc_skill_list[secondary_list].begin(); itr != npc_skill_list[secondary_list].end(); itr++){
  7065. tmpSkill = master_skill_list.GetSkill(itr->first);
  7066. if(tmpSkill){
  7067. tmpSkill = new Skill(tmpSkill);
  7068. tmpSkill->current_val = itr->second;
  7069. tmpSkill->max_val = tmpSkill->current_val+5;
  7070. (*ret)[tmpSkill->name.data] = tmpSkill;
  7071. }
  7072. }
  7073. }
  7074. if(ret && ret->size() == 0){
  7075. safe_delete(ret);
  7076. ret = 0;
  7077. }
  7078. return ret;
  7079. }
  7080. void ZoneServer::AddNPCEquipment(int32 list_id, int32 item_id){
  7081. npc_equipment_list[list_id].push_back(item_id);
  7082. }
  7083. void ZoneServer::SetNPCEquipment(NPC* npc) {
  7084. if(npc_equipment_list.count(npc->GetEquipmentListID()) > 0){
  7085. Item* tmpItem = 0;
  7086. int8 slot = 0;
  7087. vector<int32>::iterator itr;
  7088. for(itr = npc_equipment_list[npc->GetEquipmentListID()].begin(); itr != npc_equipment_list[npc->GetEquipmentListID()].end(); itr++){
  7089. tmpItem = master_item_list.GetItem(*itr);
  7090. if(tmpItem){
  7091. slot = npc->GetEquipmentList()->GetFreeSlot(tmpItem);
  7092. if(slot < 255){
  7093. tmpItem = new Item(tmpItem);
  7094. npc->GetEquipmentList()->SetItem(slot, tmpItem);
  7095. }
  7096. }
  7097. }
  7098. }
  7099. }
  7100. void ZoneServer::AddNPC(int32 id, NPC* npc) {
  7101. npc_list[id] = npc;
  7102. }
  7103. void ZoneServer::AddWidget(int32 id, Widget* widget) {
  7104. widget_list[id] = widget;
  7105. }
  7106. Widget* ZoneServer::GetWidget(int32 id, bool override_loading) {
  7107. if((!reloading || override_loading) && widget_list.count(id) > 0)
  7108. return widget_list[id];
  7109. else
  7110. return 0;
  7111. }
  7112. Widget* ZoneServer::GetNewWidget(int32 id) {
  7113. if(!reloading && widget_list.count(id) > 0)
  7114. return widget_list[id]->Copy();
  7115. else
  7116. return 0;
  7117. }
  7118. void ZoneServer::LoadGroundSpawnEntries(){
  7119. MGroundSpawnItems.lock();
  7120. database.LoadGroundSpawnEntries(this);
  7121. MGroundSpawnItems.unlock();
  7122. }
  7123. void ZoneServer::LoadGroundSpawnItems() {
  7124. }
  7125. void ZoneServer::AddGroundSpawnEntry(int32 groundspawn_id, int16 min_skill_level, int16 min_adventure_level, int8 bonus_table, float harvest1, float harvest3, float harvest5, float harvest_imbue, float harvest_rare, float harvest10, int32 harvest_coin) {
  7126. GroundSpawnEntry* entry = new GroundSpawnEntry;
  7127. entry->min_skill_level = min_skill_level;
  7128. entry->min_adventure_level = min_adventure_level;
  7129. entry->bonus_table = bonus_table;
  7130. entry->harvest1 = harvest1;
  7131. entry->harvest3 = harvest3;
  7132. entry->harvest5 = harvest5;
  7133. entry->harvest_imbue = harvest_imbue;
  7134. entry->harvest_rare = harvest_rare;
  7135. entry->harvest10 = harvest10;
  7136. entry->harvest_coin = harvest_coin;
  7137. groundspawn_entries[groundspawn_id].push_back(entry);
  7138. }
  7139. void ZoneServer::AddGroundSpawnItem(int32 groundspawn_id, int32 item_id, int8 is_rare, int32 grid_id) {
  7140. GroundSpawnEntryItem* entry = new GroundSpawnEntryItem;
  7141. entry->item_id = item_id;
  7142. entry->is_rare = is_rare;
  7143. entry->grid_id = grid_id;
  7144. groundspawn_items[groundspawn_id].push_back(entry);
  7145. }
  7146. vector<GroundSpawnEntry*>* ZoneServer::GetGroundSpawnEntries(int32 id){
  7147. vector<GroundSpawnEntry*>* ret = 0;
  7148. MGroundSpawnItems.lock();
  7149. if(groundspawn_entries.count(id) > 0)
  7150. ret = &groundspawn_entries[id];
  7151. MGroundSpawnItems.unlock();
  7152. return ret;
  7153. }
  7154. vector<GroundSpawnEntryItem*>* ZoneServer::GetGroundSpawnEntryItems(int32 id){
  7155. vector<GroundSpawnEntryItem*>* ret = 0;
  7156. if(groundspawn_items.count(id) > 0)
  7157. ret = &groundspawn_items[id];
  7158. return ret;
  7159. }
  7160. // TODO - mis-named, should be DeleteGroundSpawnEntries() but this is ok for now :)
  7161. void ZoneServer::DeleteGroundSpawnItems()
  7162. {
  7163. MGroundSpawnItems.lock();
  7164. map<int32, vector<GroundSpawnEntry*> >::iterator groundspawnentry_map_itr;
  7165. vector<GroundSpawnEntry*>::iterator groundspawnentry_itr;
  7166. for(groundspawnentry_map_itr = groundspawn_entries.begin(); groundspawnentry_map_itr != groundspawn_entries.end(); groundspawnentry_map_itr++)
  7167. {
  7168. for(groundspawnentry_itr = groundspawnentry_map_itr->second.begin(); groundspawnentry_itr != groundspawnentry_map_itr->second.end(); groundspawnentry_itr++)
  7169. {
  7170. safe_delete(*groundspawnentry_itr);
  7171. }
  7172. }
  7173. groundspawn_entries.clear();
  7174. map<int32, vector<GroundSpawnEntryItem*> >::iterator groundspawnitem_map_itr;
  7175. vector<GroundSpawnEntryItem*>::iterator groundspawnitem_itr;
  7176. for(groundspawnitem_map_itr = groundspawn_items.begin(); groundspawnitem_map_itr != groundspawn_items.end(); groundspawnitem_map_itr++)
  7177. {
  7178. for(groundspawnitem_itr = groundspawnitem_map_itr->second.begin(); groundspawnitem_itr != groundspawnitem_map_itr->second.end(); groundspawnitem_itr++)
  7179. {
  7180. safe_delete(*groundspawnitem_itr);
  7181. }
  7182. }
  7183. groundspawn_items.clear();
  7184. MGroundSpawnItems.unlock();
  7185. }
  7186. void ZoneServer::AddGroundSpawn(int32 id, GroundSpawn* spawn) {
  7187. groundspawn_list[id] = spawn;
  7188. }
  7189. GroundSpawn* ZoneServer::GetGroundSpawn(int32 id, bool override_loading) {
  7190. if((!reloading || override_loading) && groundspawn_list.count(id) > 0)
  7191. return groundspawn_list[id];
  7192. else
  7193. return 0;
  7194. }
  7195. GroundSpawn* ZoneServer::GetNewGroundSpawn(int32 id) {
  7196. if(!reloading && groundspawn_list.count(id) > 0)
  7197. return groundspawn_list[id]->Copy();
  7198. else
  7199. return 0;
  7200. }
  7201. void ZoneServer::AddLootTable(int32 id, LootTable* table){
  7202. loot_tables[id] = table;
  7203. }
  7204. void ZoneServer::AddLootDrop(int32 id, LootDrop* drop){
  7205. loot_drops[id].push_back(drop);
  7206. }
  7207. void ZoneServer::AddSpawnLootList(int32 spawn_id, int32 id) {
  7208. spawn_loot_list[spawn_id].push_back(id);
  7209. }
  7210. void ZoneServer::ClearSpawnLootList(int32 spawn_id) {
  7211. spawn_loot_list[spawn_id].clear();
  7212. }
  7213. void ZoneServer::AddLevelLootList(GlobalLoot* loot) {
  7214. level_loot_list.push_back(loot);
  7215. }
  7216. void ZoneServer::AddRacialLootList(int16 racial_id, GlobalLoot* loot) {
  7217. racial_loot_list[racial_id].push_back(loot);
  7218. }
  7219. void ZoneServer::AddZoneLootList(int32 zone, GlobalLoot* loot) {
  7220. zone_loot_list[zone].push_back(loot);
  7221. }
  7222. void ZoneServer::ClearLootTables(){
  7223. map<int32,LootTable*>::iterator table_itr;
  7224. for(table_itr = loot_tables.begin(); table_itr != loot_tables.end(); table_itr++){
  7225. safe_delete(table_itr->second);
  7226. }
  7227. map<int32, vector<LootDrop*> >::iterator drop_itr;
  7228. vector<LootDrop*>::iterator drop_itr2;
  7229. for(drop_itr = loot_drops.begin(); drop_itr != loot_drops.end(); drop_itr++){
  7230. for(drop_itr2 = drop_itr->second.begin(); drop_itr2 != drop_itr->second.end(); drop_itr2++){
  7231. safe_delete(*drop_itr2);
  7232. }
  7233. }
  7234. vector<GlobalLoot*>::iterator level_itr;
  7235. for (level_itr = level_loot_list.begin(); level_itr != level_loot_list.end(); level_itr++) {
  7236. safe_delete(*level_itr);
  7237. }
  7238. map<int16, vector<GlobalLoot*> >::iterator race_itr;
  7239. vector<GlobalLoot*>::iterator race_itr2;
  7240. for (race_itr = racial_loot_list.begin(); race_itr != racial_loot_list.end(); race_itr++) {
  7241. for (race_itr2 = race_itr->second.begin(); race_itr2 != race_itr->second.end(); race_itr2++) {
  7242. safe_delete(*race_itr2);
  7243. }
  7244. }
  7245. map<int32, vector<GlobalLoot*> >::iterator zone_itr;
  7246. vector<GlobalLoot*>::iterator zone_itr2;
  7247. for(zone_itr = zone_loot_list.begin(); zone_itr != zone_loot_list.end(); zone_itr++) {
  7248. for (zone_itr2 = zone_itr->second.begin(); zone_itr2 != zone_itr->second.end(); zone_itr2++) {
  7249. safe_delete(*zone_itr2);
  7250. }
  7251. }
  7252. loot_tables.clear();
  7253. loot_drops.clear();
  7254. spawn_loot_list.clear();
  7255. level_loot_list.clear();
  7256. racial_loot_list.clear();
  7257. zone_loot_list.clear();
  7258. }
  7259. vector<int32> ZoneServer::GetSpawnLootList(int32 spawn_id, int32 zone_id, int8 spawn_level, int16 racial_id, Spawn* spawn) {
  7260. vector<int32> ret;
  7261. int32 returnValue = 0;
  7262. if(reloading)
  7263. return ret;
  7264. if (spawn_loot_list.count(spawn_id) > 0)
  7265. ret.insert(ret.end(), spawn_loot_list[spawn_id].begin(), spawn_loot_list[spawn_id].end());
  7266. if (level_loot_list.size() > 0) {
  7267. vector<GlobalLoot*>::iterator itr;
  7268. for (itr = level_loot_list.begin(); itr != level_loot_list.end(); itr++) {
  7269. GlobalLoot* loot = *itr;
  7270. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  7271. returnValue = 0; // reset since this can override the database setting
  7272. if(zone_script)
  7273. {
  7274. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_level", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  7275. continue;
  7276. }
  7277. bool entryAdded = false;
  7278. if (loot->minLevel == 0 && loot->maxLevel == 0 && (!loot->loot_tier || spawn->GetLootTier() >= loot->loot_tier) && (entryAdded = true)) // successful plan to add set entryAdded to true
  7279. ret.push_back(loot->table_id);
  7280. else {
  7281. if (spawn_level >= loot->minLevel && spawn_level <= loot->maxLevel && (!loot->loot_tier || spawn->GetLootTier() >= loot->loot_tier) && (entryAdded = true)) // successful plan to add set entryAdded to true
  7282. ret.push_back(loot->table_id);
  7283. }
  7284. if(!entryAdded && returnValue) // DB override via LUA scripting
  7285. ret.push_back(loot->table_id);
  7286. }
  7287. }
  7288. if (racial_loot_list.count(racial_id) > 0) {
  7289. vector<GlobalLoot*>::iterator itr;
  7290. for (itr = racial_loot_list[racial_id].begin(); itr != racial_loot_list[racial_id].end(); itr++) {
  7291. GlobalLoot* loot = *itr;
  7292. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  7293. returnValue = 0; // reset since this can override the database setting
  7294. if(zone_script)
  7295. {
  7296. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_racial", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  7297. continue;
  7298. }
  7299. bool entryAdded = false;
  7300. if (loot->minLevel == 0 && loot->maxLevel == 0 && (!loot->loot_tier || spawn->GetLootTier() >= loot->loot_tier) && (entryAdded = true)) // successful plan to add set entryAdded to true
  7301. ret.push_back(loot->table_id);
  7302. else {
  7303. if (spawn_level >= loot->minLevel && spawn_level <= loot->maxLevel && (!loot->loot_tier || spawn->GetLootTier() >= loot->loot_tier) && (entryAdded = true)) // successful plan to add set entryAdded to true
  7304. ret.push_back(loot->table_id);
  7305. }
  7306. if(!entryAdded && returnValue) // DB override via LUA scripting
  7307. ret.push_back(loot->table_id);
  7308. }
  7309. }
  7310. if (zone_loot_list.count(zone_id) > 0) {
  7311. vector<GlobalLoot*>::iterator itr;
  7312. for (itr = zone_loot_list[zone_id].begin(); itr != zone_loot_list[zone_id].end(); itr++) {
  7313. GlobalLoot* loot = *itr;
  7314. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  7315. returnValue = 0; // reset since this can override the database setting
  7316. if(zone_script)
  7317. {
  7318. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_zone", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  7319. continue;
  7320. }
  7321. bool entryAdded = false;
  7322. if (loot->minLevel == 0 && loot->maxLevel == 0 && (!loot->loot_tier || spawn->GetLootTier() >= loot->loot_tier) && (entryAdded = true)) // successful plan to add set entryAdded to true
  7323. ret.push_back(loot->table_id);
  7324. else {
  7325. if (spawn_level >= loot->minLevel && spawn_level <= loot->maxLevel && (!loot->loot_tier || spawn->GetLootTier() >= loot->loot_tier) && (entryAdded = true)) // successful plan to add set entryAdded to true
  7326. ret.push_back(loot->table_id);
  7327. }
  7328. if(!entryAdded && returnValue) // DB override via LUA scripting
  7329. ret.push_back(loot->table_id);
  7330. }
  7331. }
  7332. return ret;
  7333. }
  7334. vector<LootDrop*>* ZoneServer::GetLootDrops(int32 table_id){
  7335. if(!reloading && loot_drops.count(table_id) > 0)
  7336. return &(loot_drops[table_id]);
  7337. else
  7338. return 0;
  7339. }
  7340. LootTable* ZoneServer::GetLootTable(int32 table_id){
  7341. return loot_tables[table_id];
  7342. }
  7343. void ZoneServer::AddLocationTransporter(int32 zone_id, string message, float trigger_x, float trigger_y, float trigger_z, float trigger_radius, int32 destination_zone_id, float destination_x, float destination_y, float destination_z, float destination_heading, int32 cost, int32 unique_id){
  7344. LocationTransportDestination* loc = new LocationTransportDestination;
  7345. loc->message = message;
  7346. loc->trigger_x = trigger_x;
  7347. loc->trigger_y = trigger_y;
  7348. loc->trigger_z = trigger_z;
  7349. loc->trigger_radius = trigger_radius;
  7350. loc->destination_zone_id = destination_zone_id;
  7351. loc->destination_x = destination_x;
  7352. loc->destination_y = destination_y;
  7353. loc->destination_z = destination_z;
  7354. loc->destination_heading = destination_heading;
  7355. loc->cost = cost;
  7356. loc->unique_id = unique_id;
  7357. MTransporters.lock();
  7358. if(location_transporters.count(zone_id) == 0)
  7359. location_transporters[zone_id] = new MutexList<LocationTransportDestination*>();
  7360. location_transporters[zone_id]->Add(loc);
  7361. MTransporters.unlock();
  7362. }
  7363. void ZoneServer::AddTransporter(int32 transport_id, int8 type, string name, string message, int32 destination_zone_id, float destination_x, float destination_y, float destination_z, float destination_heading,
  7364. int32 cost, int32 unique_id, int8 min_level, int8 max_level, int32 quest_req, int16 quest_step_req, int32 quest_complete, int32 map_x, int32 map_y, int32 expansion_flag, int32 holiday_flag, int32 min_client_version,
  7365. int32 max_client_version, int32 flight_path_id, int16 mount_id, int8 mount_red_color, int8 mount_green_color, int8 mount_blue_color){
  7366. TransportDestination* transport = new TransportDestination;
  7367. transport->type = type;
  7368. transport->display_name = name;
  7369. transport->message = message;
  7370. transport->destination_zone_id = destination_zone_id;
  7371. transport->destination_x = destination_x;
  7372. transport->destination_y = destination_y;
  7373. transport->destination_z = destination_z;
  7374. transport->destination_heading = destination_heading;
  7375. transport->cost = cost;
  7376. transport->unique_id = unique_id;
  7377. transport->min_level = min_level;
  7378. transport->max_level = max_level;
  7379. transport->req_quest = quest_req;
  7380. transport->req_quest_step = quest_step_req;
  7381. transport->req_quest_complete = quest_complete;
  7382. transport->map_x = map_x;
  7383. transport->map_y = map_y;
  7384. transport->expansion_flag = expansion_flag;
  7385. transport->holiday_flag = holiday_flag;
  7386. transport->min_client_version = min_client_version;
  7387. transport->max_client_version = max_client_version;
  7388. transport->flight_path_id = flight_path_id;
  7389. transport->mount_id = mount_id;
  7390. transport->mount_red_color = mount_red_color;
  7391. transport->mount_green_color = mount_green_color;
  7392. transport->mount_blue_color = mount_blue_color;
  7393. MTransporters.lock();
  7394. transporters[transport_id].push_back(transport);
  7395. MTransporters.unlock();
  7396. }
  7397. void ZoneServer::GetTransporters(vector<TransportDestination*>* returnList, Client* client, int32 transport_id){
  7398. if (!returnList)
  7399. return;
  7400. MTransporters.lock();
  7401. if (transporters.count(transport_id) > 0)
  7402. {
  7403. vector<TransportDestination*> list;
  7404. for (int i = 0; i < transporters[transport_id].size(); i++)
  7405. {
  7406. if (transporters[transport_id][i]->min_client_version && client->GetVersion() < transporters[transport_id][i]->min_client_version)
  7407. continue;
  7408. else if (transporters[transport_id][i]->max_client_version && client->GetVersion() > transporters[transport_id][i]->max_client_version)
  7409. continue;
  7410. if (database.CheckExpansionFlags(this, transporters[transport_id][i]->expansion_flag) && database.CheckHolidayFlags(this, transporters[transport_id][i]->holiday_flag))
  7411. {
  7412. returnList->push_back(transporters[transport_id][i]);
  7413. }
  7414. }
  7415. }
  7416. MTransporters.unlock();
  7417. }
  7418. MutexList<LocationTransportDestination*>* ZoneServer::GetLocationTransporters(int32 zone_id){
  7419. MutexList<LocationTransportDestination*>* ret = 0;
  7420. MTransporters.lock();
  7421. if(location_transporters.count(zone_id) > 0)
  7422. ret = location_transporters[zone_id];
  7423. MTransporters.unlock();
  7424. return ret;
  7425. }
  7426. void ZoneServer::DeleteGlobalTransporters(){
  7427. MTransporters.lock();
  7428. map<int32, vector<TransportDestination*> >::iterator itr;
  7429. vector<TransportDestination*>::iterator transport_vector_itr;
  7430. for(itr = transporters.begin(); itr != transporters.end(); itr++){
  7431. for(transport_vector_itr = itr->second.begin(); transport_vector_itr != itr->second.end(); transport_vector_itr++){
  7432. safe_delete(*transport_vector_itr);
  7433. }
  7434. }
  7435. map<int32, MutexList<LocationTransportDestination*>* >::iterator itr2;
  7436. for(itr2 = location_transporters.begin(); itr2 != location_transporters.end(); itr2++){
  7437. itr2->second->clear(true);
  7438. delete itr2->second;
  7439. }
  7440. transporters.clear();
  7441. location_transporters.clear();
  7442. MTransporters.unlock();
  7443. }
  7444. void ZoneServer::DeleteGlobalSpawns() {
  7445. ClearLootTables();
  7446. map<int32, NPC*>::iterator npc_list_iter;
  7447. for(npc_list_iter=npc_list.begin();npc_list_iter!=npc_list.end();npc_list_iter++) {
  7448. safe_delete(npc_list_iter->second);
  7449. }
  7450. npc_list.clear();
  7451. map<int32, Object*>::iterator object_list_iter;
  7452. for(object_list_iter=object_list.begin();object_list_iter!=object_list.end();object_list_iter++) {
  7453. safe_delete(object_list_iter->second);
  7454. }
  7455. object_list.clear();
  7456. map<int32, GroundSpawn*>::iterator groundspawn_list_iter;
  7457. for(groundspawn_list_iter=groundspawn_list.begin();groundspawn_list_iter!=groundspawn_list.end();groundspawn_list_iter++) {
  7458. safe_delete(groundspawn_list_iter->second);
  7459. }
  7460. groundspawn_list.clear();
  7461. map<int32, Widget*>::iterator widget_list_iter;
  7462. for(widget_list_iter=widget_list.begin();widget_list_iter!=widget_list.end();widget_list_iter++) {
  7463. safe_delete(widget_list_iter->second);
  7464. }
  7465. widget_list.clear();
  7466. map<int32, Sign*>::iterator sign_list_iter;
  7467. for(sign_list_iter=sign_list.begin();sign_list_iter!=sign_list.end();sign_list_iter++) {
  7468. safe_delete(sign_list_iter->second);
  7469. }
  7470. sign_list.clear();
  7471. /*map<int32, AppearanceData*>::iterator appearance_list_iter;
  7472. for(appearance_list_iter=npc_appearance_list.begin();appearance_list_iter!=npc_appearance_list.end();appearance_list_iter++) {
  7473. safe_delete(appearance_list_iter->second);
  7474. }
  7475. npc_appearance_list.clear();*/
  7476. ClearEntityCommands();
  7477. DeleteGroundSpawnItems();
  7478. DeleteGlobalTransporters();
  7479. DeleteTransporterMaps();
  7480. }
  7481. void ZoneServer::AddTransportMap(int32 id, string name) {
  7482. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  7483. m_transportMaps[id] = name;
  7484. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  7485. }
  7486. bool ZoneServer::TransportHasMap(int32 id) {
  7487. bool ret = false;
  7488. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  7489. ret = m_transportMaps.count(id) > 0;
  7490. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  7491. return ret;
  7492. }
  7493. string ZoneServer::GetTransportMap(int32 id) {
  7494. string ret;
  7495. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  7496. if (m_transportMaps.count(id) > 0)
  7497. ret = m_transportMaps[id];
  7498. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  7499. return ret;
  7500. }
  7501. void ZoneServer::DeleteTransporterMaps() {
  7502. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  7503. m_transportMaps.clear();
  7504. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  7505. }
  7506. void ZoneServer::ReloadSpawns() {
  7507. if (reloading)
  7508. return;
  7509. reloading = true;
  7510. world.SetReloadingSubsystem("Spawns");
  7511. // Let every one in the zone know what is happening
  7512. HandleBroadcast("Reloading all spawns for this zone.");
  7513. DeleteGlobalSpawns();
  7514. Depop(false, true);
  7515. }
  7516. void ZoneServer::SendStateCommand(Spawn* spawn, int32 state) {
  7517. vector<Client*>::iterator itr;
  7518. MClientList.readlock(__FUNCTION__, __LINE__);
  7519. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7520. Client* client = *itr;
  7521. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7522. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), state);
  7523. }
  7524. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7525. }
  7526. void ZoneServer::AddFlightPath(int32 id, FlightPathInfo* info) {
  7527. if (m_flightPaths.count(id) > 0) {
  7528. LogWrite(ZONE__ERROR, 0, "Zone", "Duplicate flight path (%u)", id);
  7529. safe_delete(info);
  7530. return;
  7531. }
  7532. m_flightPaths[id] = info;
  7533. }
  7534. void ZoneServer::AddFlightPathLocation(int32 id, FlightPathLocation* location) {
  7535. if (m_flightPaths.count(id) == 0) {
  7536. LogWrite(ZONE__ERROR, 0, "Zone", "There is no flight info for this route (%u)", id);
  7537. safe_delete(location);
  7538. return;
  7539. }
  7540. m_flightPathRoutes[id].push_back(location);
  7541. }
  7542. void ZoneServer::DeleteFlightPaths() {
  7543. map<int32, vector<FlightPathLocation*> >::iterator itr;
  7544. vector<FlightPathLocation*>::iterator itr2;
  7545. map<int32, FlightPathInfo*>::iterator itr3;
  7546. for (itr = m_flightPathRoutes.begin(); itr != m_flightPathRoutes.end(); itr++) {
  7547. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  7548. safe_delete(*itr2);
  7549. }
  7550. itr->second.clear();
  7551. }
  7552. m_flightPathRoutes.clear();
  7553. for (itr3 = m_flightPaths.begin(); itr3 != m_flightPaths.end(); itr3++) {
  7554. safe_delete(itr3->second);
  7555. }
  7556. m_flightPaths.clear();
  7557. }
  7558. void ZoneServer::SendFlightPathsPackets(Client* client) {
  7559. // Only send a packet if there are flight paths
  7560. if (m_flightPathRoutes.size() > 0) {
  7561. PacketStruct* packet = configReader.getStruct("WS_FlightPathsMsg", client->GetVersion());
  7562. if (packet) {
  7563. int32 num_routes = m_flightPaths.size();
  7564. packet->setArrayLengthByName("number_of_routes", num_routes);
  7565. packet->setArrayLengthByName("number_of_routes2", num_routes);
  7566. packet->setArrayLengthByName("number_of_routes3", num_routes);
  7567. packet->setArrayLengthByName("number_of_routes4", num_routes);
  7568. map<int32, FlightPathInfo*>::iterator itr;
  7569. int32 i = 0;
  7570. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, i++) {
  7571. packet->setArrayDataByName("route_length", m_flightPathRoutes[itr->first].size(), i);
  7572. packet->setArrayDataByName("ground_mount", itr->second->flying ? 0 : 1, i);
  7573. packet->setArrayDataByName("allow_dismount", itr->second->dismount ? 1 : 0, i);
  7574. packet->setSubArrayLengthByName("route_length2", m_flightPathRoutes[itr->first].size(), i);
  7575. vector<FlightPathLocation*>::iterator itr2;
  7576. int32 j = 0;
  7577. for (itr2 = m_flightPathRoutes[itr->first].begin(); itr2 != m_flightPathRoutes[itr->first].end(); itr2++, j++) {
  7578. packet->setSubArrayDataByName("x", (*itr2)->X, i, j);
  7579. packet->setSubArrayDataByName("y", (*itr2)->Y, i, j);
  7580. packet->setSubArrayDataByName("z", (*itr2)->Z, i, j);
  7581. }
  7582. }
  7583. client->QueuePacket(packet->serialize());
  7584. safe_delete(packet);
  7585. }
  7586. }
  7587. }
  7588. int32 ZoneServer::GetFlightPathIndex(int32 id) {
  7589. int32 index = 0;
  7590. map<int32, FlightPathInfo*>::iterator itr;
  7591. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, index++) {
  7592. if (itr->first == id)
  7593. return index;
  7594. }
  7595. return -1;
  7596. }
  7597. float ZoneServer::GetFlightPathSpeed(int32 id) {
  7598. float speed = 1;
  7599. if (m_flightPaths.count(id) > 0)
  7600. speed = m_flightPaths[id]->speed;
  7601. return speed;
  7602. }
  7603. void ZoneServer::ProcessSpawnConditional(int8 condition) {
  7604. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  7605. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7606. map<int32, Spawn*>::iterator itr;
  7607. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7608. if (itr->second != NULL) // null itr->second still coming into ProcessSpawnConditional
  7609. {
  7610. SpawnLocation* loc = spawn_location_list[itr->second->GetSpawnLocationID()];
  7611. if (loc && loc->conditional > 0) {
  7612. if ((loc->conditional & condition) != condition) {
  7613. Despawn(itr->second, 0);
  7614. }
  7615. }
  7616. }
  7617. }
  7618. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7619. map<int32, SpawnLocation*>::iterator itr2;
  7620. for (itr2 = spawn_location_list.begin(); itr2 != spawn_location_list.end(); itr2++) {
  7621. SpawnLocation* loc = itr2->second;
  7622. if (loc && loc->conditional > 0 && ((loc->conditional & condition) == condition))
  7623. if (GetSpawnByLocationID(loc->placement_id) == NULL)
  7624. ProcessSpawnLocation(loc, nullptr, nullptr, nullptr, nullptr, nullptr);
  7625. }
  7626. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  7627. }
  7628. void ZoneServer::AddSpawnProximities(Spawn* newSpawn) {
  7629. Spawn* spawn = 0;
  7630. map<int32, Spawn*>::iterator itr;
  7631. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  7632. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7633. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7634. spawn = itr->second;
  7635. if (spawn && spawn != newSpawn) {
  7636. if (newSpawn->GetDatabaseID())
  7637. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7638. if (newSpawn->GetSpawnLocationID())
  7639. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7640. if (spawn->GetDatabaseID())
  7641. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7642. if (spawn->GetSpawnLocationID())
  7643. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7644. }
  7645. }
  7646. list<Spawn*>::iterator itr2;
  7647. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7648. spawn = *itr2;
  7649. if (spawn && spawn != newSpawn) {
  7650. if (newSpawn->GetDatabaseID())
  7651. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7652. if (newSpawn->GetSpawnLocationID())
  7653. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7654. if (spawn->GetDatabaseID())
  7655. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7656. if (spawn->GetSpawnLocationID())
  7657. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7658. }
  7659. }
  7660. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7661. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  7662. }
  7663. // we only call this inside a write lock
  7664. void ZoneServer::RemoveSpawnProximities(Spawn* oldSpawn) {
  7665. Spawn* spawn = 0;
  7666. map<int32, Spawn*>::iterator itr;
  7667. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7668. spawn = itr->second;
  7669. if (spawn && spawn != oldSpawn) {
  7670. if (oldSpawn->GetDatabaseID())
  7671. spawn->RemoveSpawnFromProximity(oldSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7672. if (oldSpawn->GetSpawnLocationID())
  7673. spawn->RemoveSpawnFromProximity(oldSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7674. // don't need to remove oldSpawn proximities, we clear them all out
  7675. }
  7676. }
  7677. }
  7678. void ZoneServer::SetSpawnScript(SpawnEntry* entry, Spawn* spawn)
  7679. {
  7680. if (!entry || !spawn)
  7681. return;
  7682. const char* script = 0;
  7683. for (int x = 0; x < 3; x++)
  7684. {
  7685. switch (x)
  7686. {
  7687. case 0:
  7688. script = world.GetSpawnEntryScript(entry->spawn_entry_id);
  7689. break;
  7690. case 1:
  7691. script = world.GetSpawnLocationScript(entry->spawn_location_id);
  7692. break;
  7693. case 2:
  7694. script = world.GetSpawnScript(entry->spawn_id);
  7695. break;
  7696. }
  7697. if (script && lua_interface && lua_interface->GetSpawnScript(script) != 0)
  7698. {
  7699. spawn->SetSpawnScript(string(script));
  7700. break;
  7701. }
  7702. }
  7703. }
  7704. vector<HouseItem> ZoneServer::GetHouseItems(Client* client)
  7705. {
  7706. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7707. return std::vector<HouseItem>();
  7708. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7709. std::vector<HouseItem> items;
  7710. map<int32, Spawn*>::iterator itr;
  7711. Spawn* spawn = 0;
  7712. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7713. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7714. spawn = itr->second;
  7715. if (spawn && spawn->IsObject() && spawn->GetPickupItemID())
  7716. {
  7717. HouseItem tmpItem;
  7718. tmpItem.item_id = spawn->GetPickupItemID();
  7719. tmpItem.unique_id = spawn->GetPickupUniqueItemID();
  7720. tmpItem.spawn_id = spawn->GetID();
  7721. tmpItem.item = master_item_list.GetItem(spawn->GetPickupItemID());
  7722. if (!tmpItem.item)
  7723. continue;
  7724. items.push_back(tmpItem);
  7725. }
  7726. }
  7727. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7728. return items;
  7729. }
  7730. void ZoneServer::SendHouseItems(Client* client)
  7731. {
  7732. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7733. return;
  7734. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7735. if(!packet) {
  7736. return;
  7737. }
  7738. std::vector<HouseItem> items = GetHouseItems(client);
  7739. // setting this to 1 puts it on the door widget
  7740. packet->setDataByName("is_widget_door", 1);
  7741. packet->setArrayLengthByName("num_items", items.size());
  7742. for (int i = 0; i < items.size(); i++)
  7743. {
  7744. HouseItem tmpItem = items[i];
  7745. packet->setArrayDataByName("unique_id", tmpItem.unique_id, i); // unique_id is in fact the item_id...
  7746. packet->setArrayDataByName("item_name", tmpItem.item->name.c_str(), i);
  7747. packet->setArrayDataByName("status_reduction", tmpItem.item->houseitem_info->status_rent_reduction, i);
  7748. // location, 0 = floor, 1 = ceiling
  7749. //packet->setArrayDataByName("location", 1, i, 0);
  7750. // item_state int8
  7751. // 0 = normal (cannot pick up item / move item / toggle visibility)
  7752. // 1 = virtual (toggle visibility available, no move item)
  7753. // 2 = hidden (cannot pick up item / move item / toggle visibility)
  7754. // 3 = virtual/hidden/toggle visibility
  7755. // 4 = none (cannot pick up item / move item / toggle visibility)
  7756. // 5 = none, toggle visibility (cannot pick up item / move item)
  7757. // 8 = none (cannot pick up item / move item / toggle visibility)
  7758. //packet->setArrayDataByName("item_state", tmpvalue, i, 0);
  7759. // makes it so we don't have access to move item/retrieve item
  7760. // cannot use in conjunction with ui_tab_flag1/ui_tab_flag2
  7761. //packet->setArrayDataByName("tradeable", 1, i);
  7762. //packet->setArrayDataByName("item_description", "failboat", i);
  7763. // access to move item/retrieve item, do not use in conjunction with tradeable
  7764. packet->setArrayDataByName("ui_tab_flag1", 1, i, 0);
  7765. packet->setArrayDataByName("ui_tab_flag2", 1, i, 0);
  7766. // both of these can serve as description fields (only one should be used they populate the same area below the item name)
  7767. //packet->setArrayDataByName("first_item_description", "test", i);
  7768. //packet->setArrayDataByName("second_item_description", "Description here!", i);
  7769. packet->setArrayDataByName("icon", tmpItem.item->GetIcon(client->GetVersion()), i);
  7770. }
  7771. EQ2Packet* pack = packet->serialize();
  7772. client->QueuePacket(pack);
  7773. safe_delete(packet);
  7774. }
  7775. Spawn* ZoneServer::GetSpawnFromUniqueItemID(int32 unique_id)
  7776. {
  7777. if (!GetInstanceID() || GetInstanceType() != Instance_Type::PERSONAL_HOUSE_INSTANCE)
  7778. return nullptr;
  7779. map<int32, Spawn*>::iterator itr;
  7780. Spawn* spawn = 0;
  7781. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7782. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7783. spawn = itr->second;
  7784. if (spawn && spawn->IsObject() && spawn->GetPickupUniqueItemID() == unique_id)
  7785. {
  7786. Spawn* tmpSpawn = spawn;
  7787. MSpawnList.releasereadlock();
  7788. return tmpSpawn;
  7789. }
  7790. }
  7791. MSpawnList.releasereadlock();
  7792. return nullptr;
  7793. }
  7794. void ZoneServer::AddPendingSpawnRemove(int32 id)
  7795. {
  7796. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7797. m_pendingSpawnRemove.insert(make_pair(id,true));
  7798. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7799. }
  7800. void ZoneServer::ProcessSpawnRemovals()
  7801. {
  7802. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7803. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7804. if (m_pendingSpawnRemove.size() > 0) {
  7805. map<int32,bool>::iterator itr2;
  7806. for (itr2 = m_pendingSpawnRemove.begin(); itr2 != m_pendingSpawnRemove.end(); itr2++) {
  7807. spawn_list.erase(itr2->first);
  7808. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].erase(itr2->first);
  7809. std::map<int32,int32>::iterator hsmitr = housing_spawn_map.find(itr2->first);
  7810. if(hsmitr != housing_spawn_map.end()) {
  7811. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].erase(hsmitr->second);
  7812. housing_spawn_map.erase(hsmitr);
  7813. }
  7814. }
  7815. m_pendingSpawnRemove.clear();
  7816. }
  7817. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7818. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7819. }
  7820. void ZoneServer::AddSpawnToGroup(Spawn* spawn, int32 group_id)
  7821. {
  7822. if( spawn->GetSpawnGroupID() > 0 )
  7823. spawn->RemoveSpawnFromGroup();
  7824. MutexList<int32>* groupList = &spawn_group_map.Get(group_id);
  7825. MutexList<int32>::iterator itr2 = groupList->begin();
  7826. while(itr2.Next())
  7827. {
  7828. Spawn* groupSpawn = GetSpawnByID(itr2.value);
  7829. if(groupSpawn)
  7830. {
  7831. // found existing group member to add it in
  7832. spawn->AddSpawnToGroup(groupSpawn);
  7833. break;
  7834. }
  7835. }
  7836. groupList->Add(spawn->GetID());
  7837. spawn->SetSpawnGroupID(group_id);
  7838. }
  7839. void ZoneServer::QueueStateCommandToClients(int32 spawn_id, int32 state)
  7840. {
  7841. if(spawn_id < 1)
  7842. return;
  7843. MLuaQueueStateCmd.lock();
  7844. lua_queued_state_commands.insert(make_pair(spawn_id, state));
  7845. MLuaQueueStateCmd.unlock();
  7846. }
  7847. void ZoneServer::QueueDefaultCommand(int32 spawn_id, std::string command, float distance)
  7848. {
  7849. if(spawn_id < 1)
  7850. return;
  7851. MLuaQueueStateCmd.lock();
  7852. lua_spawn_update_command[spawn_id].insert(make_pair(command,distance));
  7853. MLuaQueueStateCmd.unlock();
  7854. }
  7855. void ZoneServer::ProcessQueuedStateCommands() // in a client list lock only
  7856. {
  7857. vector<Client*>::iterator itr;
  7858. MLuaQueueStateCmd.lock();
  7859. if(lua_queued_state_commands.size() > 0)
  7860. {
  7861. std::map<int32, int32>::iterator statecmds;
  7862. for(statecmds = lua_queued_state_commands.begin(); statecmds != lua_queued_state_commands.end(); statecmds++)
  7863. {
  7864. Spawn* spawn = GetSpawnByID(statecmds->first, false);
  7865. if(!spawn)
  7866. continue;
  7867. MClientList.readlock(__FUNCTION__, __LINE__);
  7868. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7869. Client* client = *itr;
  7870. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7871. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), statecmds->second);
  7872. }
  7873. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7874. }
  7875. lua_queued_state_commands.clear();
  7876. }
  7877. if(lua_spawn_update_command.size() > 0)
  7878. {
  7879. std::map<int32, std::map<std::string,float>>::iterator updatecmds;
  7880. for(updatecmds = lua_spawn_update_command.begin(); updatecmds != lua_spawn_update_command.end(); updatecmds++)
  7881. {
  7882. Spawn* spawn = GetSpawnByID(updatecmds->first, false);
  7883. if(!spawn)
  7884. continue;
  7885. std::map<std::string,float>::iterator innermap;
  7886. for(innermap = lua_spawn_update_command[updatecmds->first].begin(); innermap != lua_spawn_update_command[updatecmds->first].end(); innermap++)
  7887. {
  7888. MClientList.readlock(__FUNCTION__, __LINE__);
  7889. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7890. Client* client = *itr;
  7891. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7892. client->SendDefaultCommand(spawn, innermap->first.c_str(), innermap->second);
  7893. }
  7894. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7895. }
  7896. lua_spawn_update_command[updatecmds->first].clear();
  7897. }
  7898. lua_spawn_update_command.clear();
  7899. }
  7900. MLuaQueueStateCmd.unlock();
  7901. }
  7902. void ZoneServer::RemoveClientsFromZone(ZoneServer* zone) {
  7903. vector<Client*>::iterator itr;
  7904. MClientList.readlock(__FUNCTION__, __LINE__);
  7905. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7906. Client* client = *itr;
  7907. if(client->GetCurrentZone() == zone) {
  7908. client->SetCurrentZone(nullptr);
  7909. }
  7910. if(client->GetZoningDestination() == zone) {
  7911. client->SetZoningDestination(nullptr);
  7912. }
  7913. }
  7914. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7915. }
  7916. void ZoneServer::SendSubSpawnUpdates(SUBSPAWN_TYPES subtype) {
  7917. std::map<int32, Spawn*>::iterator subitr;
  7918. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7919. for(subitr = subspawn_list[subtype].begin(); subitr != subspawn_list[subtype].end(); subitr++) {
  7920. subitr->second->changed = true;
  7921. subitr->second->info_changed = true;
  7922. AddChangedSpawn(subitr->second);
  7923. }
  7924. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7925. }
  7926. bool ZoneServer::HouseItemSpawnExists(int32 item_id) {
  7927. bool exists = false;
  7928. std::map<int32, Spawn*>::iterator subitr;
  7929. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7930. subitr = subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].find(item_id);
  7931. if(subitr != subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].end()) {
  7932. exists = true;
  7933. }
  7934. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7935. return exists;
  7936. }
  7937. void ZoneServer::ProcessPendingSpawns() {
  7938. MPendingSpawnListAdd.writelock(__FUNCTION__, __LINE__);
  7939. list<Spawn*>::iterator itr2;
  7940. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7941. Spawn* spawn = *itr2;
  7942. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7943. if (spawn)
  7944. spawn_list[spawn->GetID()] = spawn;
  7945. if(spawn->IsCollector()) {
  7946. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].insert(make_pair(spawn->GetID(),spawn));
  7947. }
  7948. if(spawn->GetPickupItemID()) {
  7949. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].insert(make_pair(spawn->GetPickupItemID(),spawn));
  7950. housing_spawn_map.insert(make_pair(spawn->GetID(), spawn->GetPickupItemID()));
  7951. }
  7952. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7953. CheckSpawnRange(spawn);
  7954. }
  7955. pending_spawn_list_add.clear();
  7956. MPendingSpawnListAdd.releasewritelock(__FUNCTION__, __LINE__);
  7957. spawn_check_add.Trigger();
  7958. }
  7959. void ZoneServer::AddSpawnToGrid(Spawn* spawn, int32 grid_id) {
  7960. if(spawn->GetID() == 0 || spawn->IsDeletedSpawn())
  7961. return;
  7962. MGridMaps.lock_shared();
  7963. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7964. if(grids != grid_maps.end()) {
  7965. grids->second->MSpawns.lock();
  7966. grids->second->spawns.insert(make_pair(spawn->GetID(), spawn));
  7967. grids->second->MSpawns.unlock();
  7968. }
  7969. else {
  7970. MGridMaps.unlock_shared();
  7971. MGridMaps.lock();
  7972. GridMap* gm = new GridMap;
  7973. gm->grid_id = grid_id;
  7974. gm->spawns.insert(make_pair(spawn->GetID(), spawn));
  7975. grid_maps.insert(make_pair(grid_id, gm));
  7976. MGridMaps.unlock();
  7977. return;
  7978. }
  7979. MGridMaps.unlock_shared();
  7980. }
  7981. void ZoneServer::RemoveSpawnFromGrid(Spawn* spawn, int32 grid_id) {
  7982. std::shared_lock lock(MGridMaps);
  7983. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7984. if(grids != grid_maps.end()) {
  7985. grids->second->MSpawns.lock();
  7986. if(grids->second->spawns.count(spawn->GetID()) > 0) {
  7987. grids->second->spawns.erase(spawn->GetID());
  7988. }
  7989. grids->second->MSpawns.unlock();
  7990. }
  7991. }
  7992. int32 ZoneServer::GetSpawnCountInGrid(int32 grid_id) {
  7993. int32 count = 0;
  7994. std::shared_lock lock(MGridMaps);
  7995. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7996. if(grids != grid_maps.end()) {
  7997. grids->second->MSpawns.lock_shared();
  7998. count = grids->second->spawns.size();
  7999. grids->second->MSpawns.unlock_shared();
  8000. }
  8001. return count;
  8002. }
  8003. void ZoneServer::SendClientSpawnListInGrid(Client* client, int32 grid_id){
  8004. std::shared_lock lock(MGridMaps);
  8005. Spawn* spawn = nullptr;
  8006. client->Message(CHANNEL_COLOR_RED, "Grid ID %u has %u spawns.", grid_id, GetSpawnCountInGrid(grid_id));
  8007. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  8008. if(grids != grid_maps.end()) {
  8009. grids->second->MSpawns.lock_shared();
  8010. typedef map <int32, Spawn*> SpawnMapType;
  8011. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  8012. spawn = it->second;
  8013. client->Message(CHANNEL_COLOR_YELLOW, "Spawn %s (%u), Loc X/Y/Z: %f/%f/%f.", spawn->GetName(), spawn->GetID(), spawn->GetX(), spawn->GetY(), spawn->GetZ());
  8014. }
  8015. grids->second->MSpawns.unlock_shared();
  8016. }
  8017. }
  8018. void ZoneServer::AddIgnoredWidget(int32 id) {
  8019. std::unique_lock lock(MIgnoredWidgets);
  8020. if(ignored_widgets.find(id) == ignored_widgets.end()) {
  8021. ignored_widgets.insert(make_pair(id,true));
  8022. }
  8023. }
  8024. void ZoneServer::AddRespawn(Spawn* spawn) {
  8025. AddRespawn(spawn->GetSpawnLocationID(), spawn->GetRespawnTime());
  8026. }
  8027. void ZoneServer::AddRespawn(int32 locationID, int32 respawnTime) {
  8028. if(locationID > 0 && respawnTime > 0) {
  8029. respawn_timers.Put(locationID, Timer::GetCurrentTime2() + respawnTime * 1000);
  8030. }
  8031. }