zoneserver.cpp 312 KB

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