zoneserver.cpp 295 KB

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