zoneserver.cpp 293 KB

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