zoneserver.cpp 295 KB

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