zoneserver.cpp 295 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601
  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()){
  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->GetHP() == 0)
  1711. dead_list.push_back(client);
  1712. player->SetCharSheetChanged(true);
  1713. SendCharSheetChanges(client);
  1714. SendDamagePacket(0, player, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_SUCCESSFUL, DAMAGE_PACKET_DAMAGE_TYPE_DROWN, damage, 0);
  1715. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You are drowning!");
  1716. }
  1717. }
  1718. }
  1719. if(dead_list.size() > 0){
  1720. vector<Client*>::iterator itr;
  1721. for(itr = dead_list.begin(); itr != dead_list.end(); itr++){
  1722. RemoveDrowningVictim((*itr)->GetPlayer());
  1723. KillSpawn(false, (*itr)->GetPlayer(), 0);
  1724. (*itr)->SimpleMessage(CHANNEL_NARRATIVE, "You are sleeping with the fishes! Glug, glug...");
  1725. }
  1726. }
  1727. }
  1728. void ZoneServer::SendSpawnChanges(){
  1729. std::shared_lock lock(MChangedSpawns);
  1730. if (changed_spawns.size() < 1)
  1731. return;
  1732. set<Spawn*> spawns_to_send;
  1733. Spawn* spawn = 0;
  1734. int count = 0;
  1735. MSpawnList.readlock(__FUNCTION__, __LINE__);
  1736. for( ChangedSpawnMapType::iterator it = changed_spawns.begin(); it != changed_spawns.end(); ++it ) {
  1737. if(!it->second)
  1738. continue;
  1739. spawn = GetSpawnByID(it->first);
  1740. if(spawn){
  1741. spawns_to_send.insert(spawn);
  1742. count++;
  1743. }
  1744. }
  1745. vector<Client*>::iterator client_itr;
  1746. Client* client = 0;
  1747. MClientList.readlock(__FUNCTION__, __LINE__);
  1748. if(clients.size())
  1749. {
  1750. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  1751. client = *client_itr;
  1752. if(client)
  1753. client->SendSpawnChanges(spawns_to_send);
  1754. }
  1755. }
  1756. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1757. for (const auto& spawn : spawns_to_send) {
  1758. spawn->changed = false;
  1759. spawn->position_changed = false;
  1760. spawn->vis_changed = false;
  1761. spawn->info_changed = false;
  1762. }
  1763. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  1764. }
  1765. void ZoneServer::SendPlayerPositionChanges(Player* player){
  1766. if(player){
  1767. player->position_changed = false;
  1768. Client* client = 0;
  1769. vector<Client*>::iterator client_itr;
  1770. MClientList.readlock(__FUNCTION__, __LINE__);
  1771. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  1772. client = *client_itr;
  1773. if(player != client->GetPlayer() && client->GetPlayer()->WasSentSpawn(player->GetID())){
  1774. EQ2Packet* outapp = player->player_position_update_packet(client->GetPlayer(), client->GetVersion());
  1775. if(outapp)
  1776. client->QueuePacket(outapp);
  1777. }
  1778. }
  1779. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1780. }
  1781. }
  1782. void ZoneServer::SendCharSheetChanges(){
  1783. vector<Client*>::iterator client_itr;
  1784. MClientList.readlock(__FUNCTION__, __LINE__);
  1785. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++)
  1786. SendCharSheetChanges(*client_itr);
  1787. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  1788. }
  1789. void ZoneServer::SendCharSheetChanges(Client* client){
  1790. if(client && client->IsConnected() && client->GetPlayer()->GetCharSheetChanged()){
  1791. client->GetPlayer()->SetCharSheetChanged(false);
  1792. ClientPacketFunctions::SendCharacterSheet(client);
  1793. }
  1794. }
  1795. int32 ZoneServer::CalculateSpawnGroup(SpawnLocation* spawnlocation, bool respawn)
  1796. {
  1797. int32 group = 0;
  1798. list<int32>* groups_at_location = GetSpawnGroupsByLocation(spawnlocation->placement_id);
  1799. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  1800. if(groups_at_location){
  1801. list<int32>::iterator group_location_itr;
  1802. float chance = 0;
  1803. float total_chance = 0;
  1804. map<int32, float> tmp_chances;
  1805. set<int32>* associated_groups = 0;
  1806. for (group_location_itr = groups_at_location->begin(); group_location_itr != groups_at_location->end(); group_location_itr++) {
  1807. if(tmp_chances.count(*group_location_itr) > 0)
  1808. continue;
  1809. associated_groups = GetAssociatedGroups(*group_location_itr);
  1810. if(associated_groups){
  1811. set<int32>::iterator group_itr;
  1812. for (group_itr = associated_groups->begin(); group_itr != associated_groups->end(); group_itr++) {
  1813. chance = GetSpawnGroupChance(*group_itr);
  1814. if(chance > 0){
  1815. total_chance += chance;
  1816. tmp_chances[*group_itr] = chance;
  1817. }
  1818. else
  1819. tmp_chances[*group_itr] = 0;
  1820. }
  1821. }
  1822. else{ //single group, no associations
  1823. chance = GetSpawnGroupChance(*group_location_itr);
  1824. total_chance += chance;
  1825. tmp_chances[*group_location_itr] = chance;
  1826. }
  1827. }
  1828. if(tmp_chances.size() > 1){
  1829. //set the default for any chances not set
  1830. map<int32, float>::iterator itr2;
  1831. for(itr2 = tmp_chances.begin(); itr2 != tmp_chances.end(); itr2++){
  1832. if(itr2->second == 0){
  1833. total_chance += 100/tmp_chances.size();
  1834. tmp_chances[itr2->first] = (float)(100 / tmp_chances.size());
  1835. }
  1836. }
  1837. }
  1838. if(tmp_chances.size() > 1){
  1839. float roll = (float)(rand()%((int32)total_chance));
  1840. map<int32, float>::iterator itr3;
  1841. for (itr3 = tmp_chances.begin(); itr3 != tmp_chances.end(); itr3++){
  1842. if(itr3->second >= roll){
  1843. group = itr3->first;
  1844. break;
  1845. }
  1846. else
  1847. roll -= itr3->second;
  1848. }
  1849. }
  1850. else if(tmp_chances.size() == 1)
  1851. group = tmp_chances.begin()->first;
  1852. }
  1853. if(group > 0){
  1854. map<int32, int32>* locations = GetSpawnLocationsByGroup(group);
  1855. if(locations){
  1856. map<int32, int32>::iterator itr;
  1857. Spawn* spawn = 0;
  1858. Spawn* leader = 0;
  1859. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  1860. for (itr = locations->begin(); itr != locations->end(); itr++) {
  1861. if(spawn_location_list.count(itr->second) > 0){
  1862. spawn = ProcessSpawnLocation(spawn_location_list[itr->second], respawn);
  1863. if(!leader && spawn)
  1864. leader = spawn;
  1865. if(leader)
  1866. leader->AddSpawnToGroup(spawn);
  1867. if(spawn){
  1868. //if(spawn_group_map.count(group) == 0)
  1869. // spawn_group_map.Put(group, new MutexList<Spawn*>());
  1870. MutexList<int32>* groupList = &spawn_group_map.Get(group);
  1871. groupList->Add(spawn->GetID());
  1872. spawn->SetSpawnGroupID(group);
  1873. }
  1874. }
  1875. }
  1876. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  1877. }
  1878. }
  1879. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  1880. return group;
  1881. }
  1882. void ZoneServer::ProcessSpawnLocation(int32 location_id, bool respawn)
  1883. {
  1884. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  1885. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  1886. if(spawn_location_list.count(location_id) > 0)
  1887. {
  1888. if(respawn) //see if there are any spawns still in game associated with this spawn's group, if so, dont spawn this
  1889. {
  1890. list<int32>* groups = GetSpawnGroupsByLocation(spawn_location_list[location_id]->placement_id);
  1891. if(groups)
  1892. {
  1893. set<int32>* associated_groups = 0;
  1894. bool should_spawn = true;
  1895. list<int32>::iterator itr;
  1896. for (itr = groups->begin(); itr != groups->end(); itr++) {
  1897. associated_groups = GetAssociatedGroups(*itr);
  1898. if(associated_groups)
  1899. {
  1900. set<int32>::iterator assoc_itr;
  1901. for (assoc_itr = associated_groups->begin(); assoc_itr != associated_groups->end(); assoc_itr++) {
  1902. if(spawn_group_map.count(*assoc_itr) > 0 && spawn_group_map.Get(*assoc_itr).size() > 0)
  1903. should_spawn = false;
  1904. }
  1905. }
  1906. }
  1907. if(should_spawn)
  1908. CalculateSpawnGroup(spawn_location_list[location_id]);
  1909. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  1910. // need to unlock the list before we exit the function
  1911. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  1912. return;
  1913. }
  1914. }
  1915. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  1916. ProcessSpawnLocation(spawn_location_list[location_id], respawn);
  1917. }
  1918. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  1919. }
  1920. Spawn* ZoneServer::ProcessSpawnLocation(SpawnLocation* spawnlocation, bool respawn)
  1921. {
  1922. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  1923. if(!spawnlocation)
  1924. return 0;
  1925. Spawn* spawn = 0;
  1926. float rand_number = MakeRandomFloat(0, spawnlocation->total_percentage);
  1927. for(int32 i=0;i<spawnlocation->entities.size();i++)
  1928. {
  1929. if(spawnlocation->entities[i]->spawn_percentage == 0)
  1930. continue;
  1931. if (spawnlocation->conditional > 0) {
  1932. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_DAY) == SPAWN_CONDITIONAL_DAY && isDusk)
  1933. continue;
  1934. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_NIGHT) == SPAWN_CONDITIONAL_NIGHT && !isDusk)
  1935. continue;
  1936. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_DAY) == SPAWN_CONDITIONAL_NOT_RAINING && rain >= 0.75f)
  1937. continue;
  1938. if ((spawnlocation->conditional & SPAWN_CONDITIONAL_DAY) == SPAWN_CONDITIONAL_RAINING && rain < 0.75f)
  1939. continue;
  1940. }
  1941. if (spawnlocation->entities[i]->spawn_percentage >= rand_number) {
  1942. if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_NPC)
  1943. spawn = AddNPCSpawn(spawnlocation, spawnlocation->entities[i]);
  1944. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN)
  1945. spawn = AddGroundSpawn(spawnlocation, spawnlocation->entities[i]);
  1946. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT)
  1947. spawn = AddObjectSpawn(spawnlocation, spawnlocation->entities[i]);
  1948. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET)
  1949. spawn = AddWidgetSpawn(spawnlocation, spawnlocation->entities[i]);
  1950. else if (spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_SIGN)
  1951. spawn = AddSignSpawn(spawnlocation, spawnlocation->entities[i]);
  1952. if (GetInstanceType() == PERSONAL_HOUSE_INSTANCE)
  1953. database.GetHouseSpawnInstanceData(this, spawn);
  1954. if(spawn && spawn->IsOmittedByDBFlag())
  1955. {
  1956. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) in spawn location id %u was skipped due to a missing expansion / holiday flag being met (ZoneServer::ProcessSpawnLocation)", spawnlocation->entities[i]->spawn_id, spawnlocation->entities[i]->spawn_location_id);
  1957. safe_delete(spawn);
  1958. spawn = 0;
  1959. continue;
  1960. }
  1961. else if (!spawn)
  1962. {
  1963. LogWrite(ZONE__ERROR, 0, "Zone", "Error adding spawn by spawn location to zone %s with location id %u, spawn id %u, spawn type %u.", GetZoneName(), spawnlocation->entities[i]->spawn_location_id, spawnlocation->entities[i]->spawn_id, spawnlocation->entities[i]->spawn_type);
  1964. continue;
  1965. }
  1966. if (spawn)
  1967. {
  1968. if(respawn)
  1969. CallSpawnScript(spawn, SPAWN_SCRIPT_RESPAWN);
  1970. else
  1971. CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  1972. }
  1973. break;
  1974. }
  1975. else
  1976. rand_number -= spawnlocation->entities[i]->spawn_percentage;
  1977. }
  1978. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  1979. return spawn;
  1980. }
  1981. Spawn* ZoneServer::ProcessInstanceSpawnLocation(SpawnLocation* spawnlocation, map<int32,int32>* instNPCs, map<int32,int32>* instGroundSpawns, map<int32,int32>* instObjSpawns, map<int32,int32>* instWidgetSpawns, map<int32,int32>* instSignSpawns, bool respawn)
  1982. {
  1983. if(!spawnlocation)
  1984. return 0;
  1985. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  1986. Spawn* spawn = 0;
  1987. float rand_number = MakeRandomFloat(0, spawnlocation->total_percentage);
  1988. for(int32 i=0;i<spawnlocation->entities.size();i++)
  1989. {
  1990. if(spawnlocation->entities[i]->spawn_percentage == 0)
  1991. continue;
  1992. int32 spawnTime = 0;
  1993. if(spawnlocation->entities[i]->spawn_percentage >= rand_number)
  1994. {
  1995. if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_NPC &&
  1996. (spawnTime = database.CheckSpawnRemoveInfo(instNPCs,spawnlocation->entities[i]->spawn_location_id)) > 0)
  1997. spawn = AddNPCSpawn(spawnlocation, spawnlocation->entities[i]);
  1998. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_GROUNDSPAWN &&
  1999. (spawnTime = database.CheckSpawnRemoveInfo(instGroundSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2000. spawn = AddGroundSpawn(spawnlocation, spawnlocation->entities[i]);
  2001. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_OBJECT &&
  2002. (spawnTime = database.CheckSpawnRemoveInfo(instObjSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2003. spawn = AddObjectSpawn(spawnlocation, spawnlocation->entities[i]);
  2004. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_WIDGET &&
  2005. (spawnTime = database.CheckSpawnRemoveInfo(instWidgetSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2006. spawn = AddWidgetSpawn(spawnlocation, spawnlocation->entities[i]);
  2007. else if(spawnlocation->entities[i]->spawn_type == SPAWN_ENTRY_TYPE_SIGN &&
  2008. (spawnTime = database.CheckSpawnRemoveInfo(instSignSpawns,spawnlocation->entities[i]->spawn_location_id)) > 0)
  2009. spawn = AddSignSpawn(spawnlocation, spawnlocation->entities[i]);
  2010. if(spawn && spawn->IsOmittedByDBFlag())
  2011. {
  2012. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) in spawn location id %u was skipped due to a missing expansion / holiday flag being met (ZoneServer::ProcessInstanceSpawnLocation)", spawnlocation->entities[i]->spawn_id, spawnlocation->entities[i]->spawn_location_id);
  2013. safe_delete(spawn);
  2014. spawn = 0;
  2015. continue;
  2016. }
  2017. if (GetInstanceType() == PERSONAL_HOUSE_INSTANCE)
  2018. database.GetHouseSpawnInstanceData(this, spawn);
  2019. const char* script = 0;
  2020. for(int x=0;x<3;x++)
  2021. {
  2022. switch(x)
  2023. {
  2024. case 0:
  2025. script = world.GetSpawnEntryScript(spawnlocation->entities[i]->spawn_entry_id);
  2026. break;
  2027. case 1:
  2028. script = world.GetSpawnLocationScript(spawnlocation->entities[i]->spawn_location_id);
  2029. break;
  2030. case 2:
  2031. script = world.GetSpawnScript(spawnlocation->entities[i]->spawn_id);
  2032. break;
  2033. }
  2034. if(spawn && script && lua_interface->GetSpawnScript(script) != 0)
  2035. {
  2036. spawn->SetSpawnScript(string(script));
  2037. break;
  2038. }
  2039. }
  2040. if(spawn)
  2041. {
  2042. if (respawn)
  2043. CallSpawnScript(spawn, SPAWN_SCRIPT_RESPAWN);
  2044. else
  2045. CallSpawnScript(spawn, SPAWN_SCRIPT_SPAWN);
  2046. if ( spawnTime > 1 )
  2047. {
  2048. spawn->SetRespawnTime(spawnTime);
  2049. }
  2050. }
  2051. break;
  2052. }
  2053. else
  2054. rand_number -= spawnlocation->entities[i]->spawn_percentage;
  2055. }
  2056. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2057. return spawn;
  2058. }
  2059. void ZoneServer::ProcessSpawnLocations()
  2060. {
  2061. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  2062. map<int32,int32>* instNPCs = NULL;
  2063. map<int32,int32>* instGroundSpawns = NULL;
  2064. map<int32,int32>* instObjSpawns = NULL;
  2065. map<int32,int32>* instWidgetSpawns = NULL;
  2066. map<int32,int32>* instSignSpawns = NULL;
  2067. if ( this->IsInstanceZone() )
  2068. {
  2069. LogWrite(SPAWN__DEBUG, 0, "Spawn", "Processing Instance Removed Spawns...");
  2070. instNPCs = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_NPC );
  2071. instGroundSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_GROUNDSPAWN );
  2072. instObjSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_OBJECT );
  2073. instWidgetSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_WIDGET );
  2074. instSignSpawns = database.GetInstanceRemovedSpawns(this->GetInstanceID() , SPAWN_ENTRY_TYPE_SIGN );
  2075. }
  2076. map<int32, bool> processed_spawn_locations;
  2077. map<int32, SpawnLocation*>::iterator itr;
  2078. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  2079. for (itr = spawn_location_list.begin(); itr != spawn_location_list.end(); itr++) {
  2080. LogWrite(SPAWN__TRACE, 0, "Spawn", "while spawn_location_list itr (#%u)", spawn_location_list.size());
  2081. if(itr->second && processed_spawn_locations.count(itr->second->placement_id) > 0) //if we processed one spawn in a spawn group, we processed them all for that group
  2082. continue;
  2083. if(itr->second && spawn_location_groups.count(itr->second->placement_id) > 0)
  2084. {
  2085. int32 group_id = CalculateSpawnGroup(itr->second);
  2086. if(group_id)
  2087. {
  2088. LogWrite(SPAWN__TRACE, 0, "Spawn", "is group_id");
  2089. set<int32>* associated_groups = GetAssociatedGroups(group_id);
  2090. if(associated_groups)
  2091. {
  2092. LogWrite(SPAWN__TRACE, 0, "Spawn", "is associated_groups");
  2093. vector<int32>* associated_locations = GetAssociatedLocations(associated_groups);
  2094. if(associated_locations)
  2095. {
  2096. LogWrite(SPAWN__TRACE, 0, "Spawn", "is associated_locations");
  2097. for(int32 i=0;i<associated_locations->size();i++)
  2098. {
  2099. LogWrite(SPAWN__DEBUG, 5, "Spawn", "Loading processed_spawn_locations...");
  2100. processed_spawn_locations[associated_locations->at(i)] = true;
  2101. }
  2102. safe_delete(associated_locations);
  2103. }
  2104. }
  2105. }
  2106. }
  2107. else
  2108. {
  2109. if ( this->IsInstanceZone() )
  2110. {
  2111. //LogWrite(SPAWN__DEBUG, 5, "Spawn", "ProcessInstanceSpawnLocation (%u)...", itr->second->placement_id);
  2112. ProcessInstanceSpawnLocation(itr->second,instNPCs,instGroundSpawns,instObjSpawns,instWidgetSpawns,instSignSpawns);
  2113. }
  2114. else
  2115. {
  2116. //LogWrite(SPAWN__DEBUG, 5, "Spawn", "ProcessSpawnLocation (%u)...", itr->second->placement_id);
  2117. ProcessSpawnLocation(itr->second);
  2118. }
  2119. }
  2120. }
  2121. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  2122. safe_delete(instNPCs);
  2123. safe_delete(instGroundSpawns);
  2124. safe_delete(instObjSpawns);
  2125. safe_delete(instWidgetSpawns);
  2126. safe_delete(instSignSpawns);
  2127. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit %s", __FUNCTION__);
  2128. }
  2129. void ZoneServer::AddLoot(NPC* npc, Spawn* killer){
  2130. // this function is ran twice, first on spawn of mob, then at death of mob (gray mob check and no_drop_quest_completed_id check)
  2131. // first we see if the skipping of gray mobs loot is enabled, then we move all non body drops
  2132. if(killer)
  2133. {
  2134. int8 skip_loot_gray_mob_flag = rule_manager.GetGlobalRule(R_Loot, SkipLootGrayMob)->GetInt8();
  2135. if(skip_loot_gray_mob_flag) {
  2136. Player* player = 0;
  2137. if(killer->IsPlayer())
  2138. player = (Player*)killer;
  2139. else if(killer->IsPet()) {
  2140. Spawn* owner = ((Entity*)killer)->GetOwner();
  2141. if(owner->IsPlayer())
  2142. player = (Player*)owner;
  2143. }
  2144. if(player) {
  2145. int8 difficulty = player->GetArrowColor(npc->GetLevel());
  2146. if(difficulty == ARROW_COLOR_GRAY) {
  2147. npc->ClearNonBodyLoot();
  2148. }
  2149. }
  2150. }
  2151. }
  2152. // check for starting loot of Spawn and death of Spawn loot (no_drop_quest_completed_id)
  2153. vector<int32> loot_tables = GetSpawnLootList(npc->GetDatabaseID(), GetZoneID(), npc->GetLevel(), race_types_list.GetRaceType(npc->GetModelType()), npc);
  2154. if(loot_tables.size() > 0){
  2155. vector<LootDrop*>* loot_drops = 0;
  2156. vector<LootDrop*>::iterator loot_drop_itr;
  2157. LootTable* table = 0;
  2158. vector<int32>::iterator loot_list_itr;
  2159. float chancecoin = 0;
  2160. float chancetable = 0;
  2161. float chancedrop = 0;
  2162. float chancetally = 0;
  2163. float droptally = 0;
  2164. // the following loop,loops through each table
  2165. for(loot_list_itr = loot_tables.begin(); loot_list_itr != loot_tables.end(); loot_list_itr++){
  2166. table = GetLootTable(*loot_list_itr);
  2167. // if killer is assigned this is on-death, we already assigned coin
  2168. if(!killer && table && table->maxcoin > 0){
  2169. chancecoin = rand()%100;
  2170. if(table->coin_probability >= chancecoin){
  2171. if(table->maxcoin > table->mincoin)
  2172. npc->AddLootCoins(table->mincoin + rand()%(table->maxcoin - table->mincoin));
  2173. }
  2174. }
  2175. int numberchances = 1;
  2176. //if (table->lootdrop_probability == 100){ }
  2177. //else
  2178. //chancetally += table->lootdrop_probability;
  2179. int maxchance = 0;
  2180. if (table) {
  2181. maxchance = table->maxlootitems;
  2182. for (numberchances; numberchances <= maxchance; numberchances++) {
  2183. chancetable = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / 100));
  2184. //LogWrite(PLAYER__DEBUG, 0, "Player", "Table Chance: '%f'", chancetable);
  2185. float droppercenttotal = 0;
  2186. //--------------------------------------------------------------------------------------------------------------------------------------------------------------
  2187. if (table->lootdrop_probability == 100 || table->lootdrop_probability >= chancetable) {
  2188. //LogWrite(PLAYER__DEBUG, 0, "Player", "Probability:%f Table Chance: '%f'", table->lootdrop_probability, chancetable);
  2189. loot_drops = GetLootDrops(*loot_list_itr);
  2190. if (loot_drops && loot_drops->size() > 0) {
  2191. LootDrop* drop = 0;
  2192. int16 count = 0;
  2193. std::shuffle(loot_drops->begin(), loot_drops->end(), std::default_random_engine(Timer::GetCurrentTime2()));
  2194. int16 IC = 0;
  2195. for (loot_drop_itr = loot_drops->begin(); loot_drop_itr != loot_drops->end(); loot_drop_itr++) {
  2196. drop = *loot_drop_itr;
  2197. droppercenttotal += drop->probability;
  2198. }
  2199. int droplistsize = loot_drops->size();
  2200. float chancedroptally = 0;
  2201. chancedrop = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / 100));
  2202. for (loot_drop_itr = loot_drops->begin(); loot_drop_itr != loot_drops->end(); loot_drop_itr++) {
  2203. drop = *loot_drop_itr;
  2204. // if no killer is provided, then we are instantiating the spawn loot, quest related loot should be added on death of the spawn to check against spawn/group members
  2205. if(drop->no_drop_quest_completed_id && killer == nullptr)
  2206. continue;
  2207. else if(!drop->no_drop_quest_completed_id && killer) // skip since this doesn't have a quest id attached and we are doing after-math of death loot additions
  2208. continue;
  2209. else if(killer && drop->no_drop_quest_completed_id) // check if the player already completed quest related to item
  2210. {
  2211. Player* player = nullptr;
  2212. if(killer->IsPlayer())
  2213. {
  2214. player = (Player*)killer;
  2215. // player has already completed the quest
  2216. if(player->GetCompletedQuest(drop->no_drop_quest_completed_id) && !player->GetGroupMemberInfo())
  2217. {
  2218. LogWrite(PLAYER__DEBUG, 0, "Player", "%s: Player has completed quest %u, skipping loot item %u", npc->GetName(), drop->no_drop_quest_completed_id, drop->item_id);
  2219. continue;
  2220. }
  2221. else if(player->GetGroupMemberInfo() && world.GetGroupManager()->HasGroupCompletedQuest(player->GetGroupMemberInfo()->group_id, drop->no_drop_quest_completed_id))
  2222. {
  2223. LogWrite(PLAYER__DEBUG, 0, "Player", "%s: Group %u has completed quest %u, skipping loot item %u", npc->GetName(), player->GetGroupMemberInfo()->group_id, drop->no_drop_quest_completed_id, drop->item_id);
  2224. continue;
  2225. }
  2226. }
  2227. else
  2228. {
  2229. LogWrite(PLAYER__DEBUG, 0, "Player", "%s: Killer is not a player, skipping loot item %u", npc->GetName(), drop->item_id);
  2230. continue;
  2231. }
  2232. }
  2233. if (npc->HasLootItemID(drop->item_id))
  2234. continue;
  2235. if (droppercenttotal >= 100)
  2236. droppercenttotal = 100;
  2237. chancedroptally += 100 / droppercenttotal * drop->probability;
  2238. //chancedrop = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / 100));
  2239. //LogWrite(PLAYER__DEBUG, 0, "Player", "Loot drop: '%i' Chance: %f Prob tally: %f min: %f", drop, chancedrop, chancedroptally, chancedroptally - drop->probability);
  2240. if ((chancedroptally == 100) || ((chancedroptally >= chancedrop) && (chancedroptally - (100 / droppercenttotal * drop->probability)) <= chancedrop)) {
  2241. //LogWrite(PLAYER__DEBUG, 0, "Player", "Loot drop: '%i' Chance: %f Prob: %f We have a loot drop winner", drop, chancedrop, chancedroptally);
  2242. count++;
  2243. npc->AddLootItem(drop->item_id, drop->item_charges);
  2244. //LogWrite(PLAYER__DEBUG, 0, "Player", "loot Count: '%i'",count);
  2245. //LogWrite(MISC__TODO, 1, "TODO", "Auto-Equip new looted items\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  2246. //if(drop->equip_item)
  2247. }
  2248. if (table->maxlootitems > 0 && count >= table->maxlootitems)
  2249. break;
  2250. }
  2251. }
  2252. }
  2253. }
  2254. }
  2255. }
  2256. }
  2257. }
  2258. void ZoneServer::DeterminePosition(SpawnLocation* spawnlocation, Spawn* spawn){
  2259. if(!spawn || !spawnlocation)
  2260. return;
  2261. int offset = 0;
  2262. if(spawnlocation->x_offset > 0){
  2263. //since rand() deals with integers only, we are going to divide by 1000 later so that we can use fractions of integers
  2264. offset = (int)((spawnlocation->x_offset*1000)+1);
  2265. spawn->SetX(spawnlocation->x + ((float)(rand()%offset - rand()%offset))/1000);
  2266. }
  2267. else
  2268. spawn->SetX(spawnlocation->x);
  2269. if(spawnlocation->y_offset > 0){
  2270. //since rand() deals with integers only, we are going to divide by 1000 later so that we can use fractions of integers
  2271. offset = (int)((spawnlocation->y_offset*1000)+1);
  2272. spawn->SetY(spawnlocation->y + ((float)(rand()%offset - rand()%offset))/1000);
  2273. }
  2274. else
  2275. spawn->SetY(spawnlocation->y, true, true);
  2276. if(spawnlocation->z_offset > 0){
  2277. //since rand() deals with integers only, we are going to divide by 1000 later so that we can use fractions of integers
  2278. offset = (int)((spawnlocation->z_offset*1000)+1);
  2279. spawn->SetZ(spawnlocation->z + ((float)(rand()%offset - rand()%offset))/1000);
  2280. }
  2281. else
  2282. spawn->SetZ(spawnlocation->z);
  2283. spawn->SetHeading(spawnlocation->heading);
  2284. spawn->SetPitch(spawnlocation->pitch);
  2285. spawn->SetRoll(spawnlocation->roll);
  2286. spawn->SetSpawnOrigX(spawn->GetX());
  2287. spawn->SetSpawnOrigY(spawn->GetY());
  2288. spawn->SetSpawnOrigZ(spawn->GetZ());
  2289. spawn->SetSpawnOrigHeading(spawn->GetHeading());
  2290. spawn->SetSpawnOrigPitch(spawnlocation->pitch);
  2291. spawn->SetSpawnOrigRoll(spawnlocation->roll);
  2292. spawn->appearance.pos.grid_id = 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. if(client)
  2820. {
  2821. if (client->GetPlayer())
  2822. client_list.RemovePlayerFromInvisHistory(client->GetPlayer()->GetID());
  2823. LogWrite(ZONE__DEBUG, 0, "Zone", "Sending login equipment appearance updates...");
  2824. loginserver.SendImmediateEquipmentUpdatesForChar(client->GetPlayer()->GetCharacterID());
  2825. if (!client->IsZoning())
  2826. {
  2827. if ((guild = client->GetPlayer()->GetGuild()) != NULL)
  2828. guild->GuildMemberLogoff(client->GetPlayer());
  2829. chat.LeaveAllChannels(client);
  2830. }
  2831. int32 LD_Timer = rule_manager.GetGlobalRule(R_World, LinkDeadTimer)->GetInt32();
  2832. int32 DisconnectClientTimer = rule_manager.GetGlobalRule(R_World, RemoveDisconnectedClientsTimer)->GetInt32();
  2833. if(!zoneShuttingDown && !client->IsZoning())
  2834. {
  2835. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  2836. GroupMemberInfo* gmi = client->GetPlayer()->GetGroupMemberInfo();
  2837. int32 group_id = 0;
  2838. if (gmi) {
  2839. group_id = gmi->group_id;
  2840. }
  2841. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2842. if (group_id) {
  2843. int32 size = world.GetGroupManager()->GetGroupSize(group_id);
  2844. if (size > 1) {
  2845. bool send_left_message = size > 2;
  2846. // removegroupmember can delete the gmi, so make sure we still have a group_id after
  2847. world.GetGroupManager()->RemoveGroupMember(group_id, client->GetPlayer());
  2848. if (send_left_message)
  2849. world.GetGroupManager()->GroupMessage(group_id, "%s has left the group.", client->GetPlayer()->GetName());
  2850. }
  2851. }
  2852. if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0)
  2853. {
  2854. LogWrite(ZONE__DEBUG, 0, "Zone", "Removing client '%s' (%u) due to LD/Exit...", client->GetPlayer()->GetName(), client->GetPlayer()->GetCharacterID());
  2855. //connected_clients.Remove(client, true, LD_Timer + DisconnectClientTimer);
  2856. }
  2857. else
  2858. {
  2859. LogWrite(ZONE__DEBUG, 0, "Zone", "Removing client '%s' (%u) due to Camp/Quit...", client->GetPlayer()->GetName(), client->GetPlayer()->GetCharacterID());
  2860. }
  2861. ((Entity*)client->GetPlayer())->DismissAllPets();
  2862. //}
  2863. }
  2864. else
  2865. {
  2866. LogWrite(ZONE__DEBUG, 0, "Zone", "Removing client '%s' (%u) due to some client zoning...", client->GetPlayer()->GetName(), client->GetPlayer()->GetCharacterID());
  2867. }
  2868. map<int32, int32>::iterator itr;
  2869. for (itr = client->GetPlayer()->SpawnedBots.begin(); itr != client->GetPlayer()->SpawnedBots.end(); itr++) {
  2870. Spawn* spawn = GetSpawnByID(itr->second);
  2871. if (spawn)
  2872. ((Bot*)spawn)->Camp();
  2873. }
  2874. MClientList.writelock(__FUNCTION__, __LINE__);
  2875. LogWrite(ZONE__DEBUG, 0, "Zone", "Calling clients.Remove(client)...");
  2876. UpdateClientSpawnMap(client->GetPlayer(), 0); // address spawn map being prematurely cleared when client list is active
  2877. std::vector<Client*>::iterator itr2 = find(clients.begin(), clients.end(), client);
  2878. if (itr2 != clients.end())
  2879. clients.erase(itr2);
  2880. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  2881. LogWrite(ZONE__INFO, 0, "Zone", "Scheduling client '%s' for removal.", client->GetPlayer()->GetName());
  2882. database.ToggleCharacterOnline(client, 0);
  2883. client->GetPlayer()->DeleteSpellEffects(true);
  2884. if(client->getConnection()) {
  2885. client->getConnection()->ResetSessionAttempts();
  2886. client->getConnection()->SetState(EQStreamState::WAIT_CLOSE);
  2887. }
  2888. RemoveSpawn(client->GetPlayer(), false, true, true, true, true);
  2889. connected_clients.Remove(client, true, DisconnectClientTimer); // changed from a hardcoded 30000 (30 sec) to the DisconnectClientTimer rule
  2890. }
  2891. }
  2892. void ZoneServer::RemoveClientImmediately(Client* client) {
  2893. Guild *guild;
  2894. if(client)
  2895. {
  2896. MClientList.writelock(__FUNCTION__, __LINE__);
  2897. std::vector<Client*>::iterator itr = find(clients.begin(), clients.end(), client);
  2898. if (itr != clients.end())
  2899. clients.erase(itr);
  2900. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  2901. //clients.Remove(client, true);
  2902. }
  2903. }
  2904. void ZoneServer::ClientProcess()
  2905. {
  2906. if(connected_clients.size(true) == 0)
  2907. {
  2908. MIncomingClients.readlock(__FUNCTION__, __LINE__);
  2909. bool shutdownDelayCheck = shutdownDelayTimer.Check();
  2910. if((!AlwaysLoaded() && !shutdownTimer.Enabled()) || shutdownDelayCheck)
  2911. {
  2912. if(incoming_clients && !shutdownDelayTimer.Enabled()) {
  2913. LogWrite(ZONE__INFO, 0, "Zone", "Incoming clients (%u) expected for %s, delaying shutdown timer...", incoming_clients, GetZoneName());
  2914. int32 timerDelay = rule_manager.GetGlobalRule(R_Zone, ShutdownDelayTimer)->GetInt32();
  2915. if(timerDelay < 10) {
  2916. LogWrite(ZONE__INFO, 0, "Zone", "Overriding %s shutdown delay timer as other clients are incoming, value %u too short, setting to 10...", GetZoneName(), timerDelay);
  2917. timerDelay = 10;
  2918. }
  2919. shutdownDelayTimer.Start(timerDelay, true);
  2920. }
  2921. else if(!incoming_clients || shutdownDelayCheck) {
  2922. if(!shutdownTimer.Enabled()) {
  2923. LogWrite(ZONE__INFO, 0, "Zone", "Starting zone shutdown timer for %s...", GetZoneName());
  2924. shutdownTimer.Start();
  2925. }
  2926. else {
  2927. LogWrite(ZONE__INFO, 0, "Zone", "zone shutdown timer for %s has %u remaining...", GetZoneName(), shutdownTimer.GetRemainingTime());
  2928. }
  2929. }
  2930. }
  2931. MIncomingClients.releasereadlock(__FUNCTION__, __LINE__);
  2932. return;
  2933. }
  2934. shutdownTimer.Disable();
  2935. shutdownDelayTimer.Disable();
  2936. Client* client = 0;
  2937. MutexList<Client*>::iterator iterator = connected_clients.begin();
  2938. while(iterator.Next())
  2939. {
  2940. client = iterator->value;
  2941. #ifndef NO_CATCH
  2942. try
  2943. {
  2944. #endif
  2945. if(zoneShuttingDown || !client->Process(true))
  2946. {
  2947. if(client->getConnection() && client->getConnection()->HasSessionAttempts()) {
  2948. printf("Client has an attempt to reconnect..\n");
  2949. continue;
  2950. }
  2951. if(!zoneShuttingDown && !client->IsZoning())
  2952. {
  2953. LogWrite(ZONE__DEBUG, 0, "Zone", "Client is disconnecting in %s (camping = %s)", __FUNCTION__, (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 ? "false" : "true");
  2954. if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 )
  2955. {
  2956. //only set LD flag if we're disconnecting but not camping/quitting
  2957. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() + ACTIVITY_STATUS_LINKDEAD);
  2958. if(client->GetPlayer()->GetGroupMemberInfo())
  2959. {
  2960. LogWrite(ZONE__DEBUG, 0, "Zone", "Telling client's group they are disconnecting");
  2961. world.GetGroupManager()->GroupMessage(client->GetPlayer()->GetGroupMemberInfo()->group_id, "%s has gone Linkdead.", client->GetPlayer()->GetName());
  2962. }
  2963. }
  2964. }
  2965. RemoveClient(client);
  2966. client->Disconnect();
  2967. }
  2968. #ifndef NO_CATCH
  2969. }
  2970. catch(...)
  2971. {
  2972. LogWrite(ZONE__ERROR, 0, "Zone", "Exception caught when in ZoneServer::ClientProcess() for zone '%s'!\n%s, %i", GetZoneName(), __FUNCTION__, __LINE__);
  2973. try{
  2974. if(!client->IsZoning())
  2975. {
  2976. if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 )
  2977. {
  2978. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() + ACTIVITY_STATUS_LINKDEAD);
  2979. if(client->GetPlayer()->GetGroupMemberInfo())
  2980. world.GetGroupManager()->GroupMessage(client->GetPlayer()->GetGroupMemberInfo()->group_id, "%s has gone Linkdead.", client->GetPlayer()->GetName());
  2981. }
  2982. }
  2983. RemoveClient(client);
  2984. client->Disconnect();
  2985. }
  2986. catch(...){
  2987. LogWrite(ZONE__ERROR, 0, "Zone", "Exception caught when in ZoneServer::ClientProcess(), second try\n%s, %i", __FUNCTION__, __LINE__);
  2988. }
  2989. }
  2990. #endif
  2991. }
  2992. }
  2993. void ZoneServer::SimpleMessage(int8 type, const char* message, Spawn* from, float distance, bool send_to_sender){
  2994. Client* client = 0;
  2995. vector<Client*>::iterator client_itr;
  2996. MClientList.readlock(__FUNCTION__, __LINE__);
  2997. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  2998. client = *client_itr;
  2999. if(from && client && client->IsConnected() && (send_to_sender || from != client->GetPlayer()) && from->GetDistance(client->GetPlayer()) <= distance){
  3000. client->SimpleMessage(type, message);
  3001. }
  3002. }
  3003. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3004. }
  3005. 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) {
  3006. if ((!distance || from->GetDistance(client->GetPlayer()) <= distance) && (!from || !client->GetPlayer()->IsIgnored(from->GetName()))) {
  3007. PacketStruct* packet = configReader.getStruct("WS_HearChat", client->GetVersion());
  3008. if (packet) {
  3009. if (from)
  3010. packet->setMediumStringByName("from", from->GetName());
  3011. if (client->GetPlayer() != from)
  3012. packet->setMediumStringByName("to", client->GetPlayer()->GetName());
  3013. packet->setDataByName("channel", client->GetMessageChannelColor(channel));
  3014. if (from && ((from == client->GetPlayer()) || (client->GetPlayer()->WasSentSpawn(from->GetID()))))
  3015. packet->setDataByName("from_spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(from));
  3016. else
  3017. packet->setDataByName("from_spawn_id", 0xFFFFFFFF);
  3018. packet->setDataByName("to_spawn_id", 0xFFFFFFFF);
  3019. packet->setMediumStringByName("message", message);
  3020. packet->setDataByName("language", language);
  3021. bool hasLanguage = client->GetPlayer()->HasLanguage(language);
  3022. if (language > 0 && !hasLanguage)
  3023. packet->setDataByName("understood", 0);
  3024. else
  3025. packet->setDataByName("understood", 1);
  3026. show_bubble == true ? packet->setDataByName("show_bubble", 1) : packet->setDataByName("show_bubble", 0);
  3027. if (channel_name)
  3028. packet->setMediumStringByName("channel_name", channel_name);
  3029. EQ2Packet* outapp = packet->serialize();
  3030. //DumpPacket(outapp);
  3031. client->QueuePacket(outapp);
  3032. safe_delete(packet);
  3033. }
  3034. }
  3035. }
  3036. void ZoneServer::HandleChatMessage(Spawn* from, const char* to, int16 channel, const char* message, float distance, const char* channel_name, bool show_bubble, int32 language){
  3037. vector<Client*>::iterator client_itr;
  3038. Client* client = 0;
  3039. MClientList.readlock(__FUNCTION__, __LINE__);
  3040. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3041. client = *client_itr;
  3042. if(client && client->IsConnected())
  3043. HandleChatMessage(client, from, to, channel, message, distance, channel_name, show_bubble, language);
  3044. }
  3045. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3046. }
  3047. void ZoneServer::HandleBroadcast(const char* message) {
  3048. vector<Client*>::iterator client_itr;
  3049. Client* client = 0;
  3050. MClientList.readlock(__FUNCTION__, __LINE__);
  3051. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3052. client = *client_itr;
  3053. if(client && client->IsConnected())
  3054. client->SimpleMessage(CHANNEL_BROADCAST, message);
  3055. }
  3056. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3057. }
  3058. void ZoneServer::HandleAnnouncement(const char* message) {
  3059. vector<Client*>::iterator client_itr;
  3060. Client* client = 0;
  3061. int32 words = ::CountWordsInString(message);
  3062. if (words < 5)
  3063. words = 5;
  3064. MClientList.readlock(__FUNCTION__, __LINE__);
  3065. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3066. client = *client_itr;
  3067. if(client && client->IsConnected()) {
  3068. client->SimpleMessage(CHANNEL_BROADCAST, message);
  3069. client->SendPopupMessage(10, message, "ui_harvest_normal", words, 0xFF, 0xFF, 0x00);
  3070. }
  3071. }
  3072. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3073. }
  3074. void ZoneServer::SendTimeUpdate(Client* client){
  3075. if(client){
  3076. PacketStruct* packet = world.GetWorldTime(client->GetVersion());
  3077. if(packet){
  3078. client->QueuePacket(packet->serialize());
  3079. safe_delete(packet);
  3080. }
  3081. }
  3082. }
  3083. void ZoneServer::SendTimeUpdateToAllClients(){
  3084. Client* client = 0;
  3085. vector<Client*>::iterator client_itr;
  3086. MClientList.readlock(__FUNCTION__, __LINE__);
  3087. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3088. client = *client_itr;
  3089. if(client && client->IsConnected())
  3090. SendTimeUpdate(client);
  3091. }
  3092. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3093. }
  3094. void ZoneServer::UpdateVitality(float amount){
  3095. Client* client = 0;
  3096. vector<Client*>::iterator client_itr;
  3097. MClientList.readlock(__FUNCTION__, __LINE__);
  3098. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3099. client = *client_itr;
  3100. if(client && client->GetPlayer()->GetInfoStruct()->get_xp_vitality() < 100){
  3101. if((client->GetPlayer()->GetInfoStruct()->get_xp_vitality() + amount) > 100)
  3102. client->GetPlayer()->GetInfoStruct()->set_xp_vitality(100);
  3103. else
  3104. client->GetPlayer()->GetInfoStruct()->add_xp_vitality(amount);
  3105. client->GetPlayer()->SetCharSheetChanged(true);
  3106. }
  3107. }
  3108. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3109. }
  3110. void ZoneServer::SendSpawn(Spawn* spawn, Client* client){
  3111. EQ2Packet* outapp = spawn->serialize(client->GetPlayer(), client->GetVersion());
  3112. if(!client->GetPlayer()->IsSendingSpawn(spawn->GetID())) {
  3113. safe_delete(outapp);
  3114. }
  3115. else {
  3116. LogWrite(ZONE__DEBUG, 7, "Zone", "%s: Processing SendSpawn for spawn index %u (%s)...", client->GetPlayer()->GetName(), client->GetPlayer()->GetIndexForSpawn(spawn), spawn->GetName());
  3117. if(outapp)
  3118. client->QueuePacket(outapp, true);
  3119. client->GetPlayer()->SetSpawnSentState(spawn, SpawnState::SPAWN_STATE_SENT_WAIT);
  3120. }
  3121. /*
  3122. vis flags:
  3123. 2 = show icon
  3124. 4 = targetable
  3125. 16 = show name
  3126. 32 = show level/border
  3127. activity_status:
  3128. 4 - linkdead
  3129. 8 - camping
  3130. 16 - LFG
  3131. 32 - LFW
  3132. 2048 - mentoring
  3133. 4096 - displays shield
  3134. 8192 - immunity gained
  3135. 16384 - immunity remaining
  3136. attackable_status
  3137. 1 - no_hp_bar
  3138. 4 - not attackable
  3139. npc_con
  3140. -4 = scowls
  3141. -3 = threatening
  3142. -2 = dubiously
  3143. -1 = apprehensively
  3144. 0 = indifferent
  3145. 1 = amiably
  3146. 2 = kindly
  3147. 3 = warmly
  3148. 4 = ally
  3149. quest_flag
  3150. 1 = new quest
  3151. 2 = update and new quest
  3152. 3 = update
  3153. */
  3154. if(spawn->IsEntity() && spawn->HasTrapTriggered())
  3155. client->QueueStateCommand(client->GetPlayer()->GetIDWithPlayerSpawn(spawn), spawn->GetTrapState());
  3156. }
  3157. Client* ZoneServer::GetClientBySpawn(Spawn* spawn){
  3158. return client_spawn_map.Get(spawn);
  3159. }
  3160. Client* ZoneServer::GetClientByName(char* name) {
  3161. Client* ret = 0;
  3162. vector<Client*>::iterator itr;
  3163. MClientList.readlock(__FUNCTION__, __LINE__);
  3164. for (itr = clients.begin(); itr != clients.end(); itr++) {
  3165. if ((*itr)->GetPlayer()) {
  3166. if (strncmp((*itr)->GetPlayer()->GetName(), name, strlen(name)) == 0) {
  3167. ret = *itr;
  3168. break;
  3169. }
  3170. }
  3171. }
  3172. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3173. return ret;
  3174. }
  3175. Client* ZoneServer::GetClientByCharID(int32 charid) {
  3176. Client* ret = 0;
  3177. vector<Client*>::iterator itr;
  3178. MClientList.readlock(__FUNCTION__, __LINE__);
  3179. for (itr = clients.begin(); itr != clients.end(); itr++) {
  3180. if ((*itr)->GetCharacterID() == charid) {
  3181. ret = *itr;
  3182. break;
  3183. }
  3184. }
  3185. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3186. return ret;
  3187. }
  3188. void ZoneServer::AddMovementNPC(Spawn* spawn){
  3189. if (spawn)
  3190. movement_spawns.Put(spawn->GetID(), 1);
  3191. }
  3192. void ZoneServer::RemoveMovementNPC(Spawn* spawn){
  3193. if (spawn)
  3194. remove_movement_spawns.Add(spawn->GetID());
  3195. }
  3196. void ZoneServer::PlayFlavor(Client* client, Spawn* spawn, const char* mp3, const char* text, const char* emote, int32 key1, int32 key2, int8 language){
  3197. if(!client || !spawn)
  3198. return;
  3199. PacketStruct* packet = configReader.getStruct("WS_PlayFlavor", client->GetVersion());
  3200. if(packet){
  3201. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  3202. packet->setDataByName("unknown1", 0xFFFFFFFF);
  3203. packet->setDataByName("unknown5", 1, 1);
  3204. packet->setDataByName("unknown5", 1, 6);
  3205. if(mp3){
  3206. packet->setMediumStringByName("mp3", mp3);
  3207. packet->setDataByName("key", key1);
  3208. packet->setDataByName("key", key2, 1);
  3209. }
  3210. packet->setMediumStringByName("name", spawn->GetName());
  3211. if(text)
  3212. packet->setMediumStringByName("text", text);
  3213. if(emote)
  3214. packet->setMediumStringByName("emote", emote);
  3215. if (language != 0)
  3216. packet->setDataByName("language", language);
  3217. //We should probably add Common = language id 0 or 0xFF so admins can customize more..
  3218. if (language == 0 || client->GetPlayer()->HasLanguage(language))
  3219. packet->setDataByName("understood", 1);
  3220. EQ2Packet* app = packet->serialize();
  3221. //DumpPacket(app);
  3222. client->QueuePacket(app);
  3223. safe_delete(packet);
  3224. }
  3225. }
  3226. void ZoneServer::PlayVoice(Client* client, Spawn* spawn, const char* mp3, int32 key1, int32 key2){
  3227. if(!client || !spawn)
  3228. return;
  3229. PacketStruct* packet = configReader.getStruct("WS_PlayVoice", client->GetVersion());
  3230. if(packet){
  3231. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  3232. packet->setMediumStringByName("mp3", mp3);
  3233. packet->setDataByName("key", key1);
  3234. packet->setDataByName("key", key2, 1);
  3235. client->QueuePacket(packet->serialize());
  3236. safe_delete(packet);
  3237. }
  3238. }
  3239. void ZoneServer::PlayFlavor(Spawn* spawn, const char* mp3, const char* text, const char* emote, int32 key1, int32 key2, int8 language){
  3240. if(!spawn)
  3241. return;
  3242. Client* client = 0;
  3243. vector<Client*>::iterator client_itr;
  3244. MClientList.readlock(__FUNCTION__, __LINE__);
  3245. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3246. client = *client_itr;
  3247. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->GetDistance(spawn) > 30)
  3248. continue;
  3249. PlayFlavor(client, spawn, mp3, text, emote, key1, key2, language);
  3250. }
  3251. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3252. }
  3253. void ZoneServer::PlayFlavorID(Spawn* spawn, int8 type, int32 id, int16 index, int8 language){
  3254. if(!spawn)
  3255. return;
  3256. Client* client = 0;
  3257. vector<Client*>::iterator client_itr;
  3258. VoiceOverStruct non_garble, garble;
  3259. bool garble_success = false;
  3260. bool success = world.FindVoiceOver(type, id, index, &non_garble, &garble_success, &garble);
  3261. VoiceOverStruct* resStruct = nullptr;
  3262. MClientList.readlock(__FUNCTION__, __LINE__);
  3263. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3264. client = *client_itr;
  3265. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->GetDistance(spawn) > 30)
  3266. continue;
  3267. client->SendPlayFlavor(spawn, language, &non_garble, &garble, success, garble_success);
  3268. }
  3269. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3270. }
  3271. void ZoneServer::PlayVoice(Spawn* spawn, const char* mp3, int32 key1, int32 key2){
  3272. if(!spawn || !mp3)
  3273. return;
  3274. Client* client = 0;
  3275. vector<Client*>::iterator client_itr;
  3276. MClientList.readlock(__FUNCTION__, __LINE__);
  3277. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3278. client = *client_itr;
  3279. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  3280. continue;
  3281. PlayVoice(client, spawn, mp3, key1, key2);
  3282. }
  3283. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3284. }
  3285. void ZoneServer::PlaySoundFile(Client* client, const char* name, float origin_x, float origin_y, float origin_z){
  3286. if(!name)
  3287. return;
  3288. PacketStruct* packet = 0;
  3289. if(client){
  3290. packet = configReader.getStruct("WS_Play3DSound", client->GetVersion());
  3291. if(packet){
  3292. packet->setMediumStringByName("name", name);
  3293. packet->setDataByName("x", origin_x);
  3294. packet->setDataByName("y", origin_y);
  3295. packet->setDataByName("z", origin_z);
  3296. packet->setDataByName("unknown1", 1);
  3297. packet->setDataByName("unknown2", 2.5);
  3298. packet->setDataByName("unknown3", 15);
  3299. client->QueuePacket(packet->serialize());
  3300. safe_delete(packet);
  3301. }
  3302. }
  3303. else{
  3304. EQ2Packet* outapp = 0;
  3305. int16 packet_version = 0;
  3306. vector<Client*>::iterator client_itr;
  3307. MClientList.readlock(__FUNCTION__, __LINE__);
  3308. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3309. client = *client_itr;
  3310. if(client && (!packet || packet_version != client->GetVersion())){
  3311. safe_delete(packet);
  3312. safe_delete(outapp);
  3313. packet_version = client->GetVersion();
  3314. packet = configReader.getStruct("WS_Play3DSound", packet_version);
  3315. if(packet){
  3316. packet->setMediumStringByName("name", name);
  3317. packet->setDataByName("x", origin_x);
  3318. packet->setDataByName("y", origin_y);
  3319. packet->setDataByName("z", origin_z);
  3320. packet->setDataByName("unknown1", 1);
  3321. packet->setDataByName("unknown2", 2.5);
  3322. packet->setDataByName("unknown3", 15);
  3323. outapp = packet->serialize();
  3324. }
  3325. }
  3326. if(outapp && client && client->IsReadyForUpdates())
  3327. client->QueuePacket(outapp->Copy());
  3328. }
  3329. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3330. safe_delete(packet);
  3331. safe_delete(outapp);
  3332. }
  3333. }
  3334. bool ZoneServer::HasWidgetTimer(Spawn* widget){
  3335. bool ret = false;
  3336. if (widget) {
  3337. int32 id = widget->GetID();
  3338. map<int32, int32>::iterator itr;
  3339. MWidgetTimers.readlock(__FUNCTION__, __LINE__);
  3340. for (itr = widget_timers.begin(); itr != widget_timers.end(); itr++) {
  3341. if(itr->first == id){
  3342. ret = true;
  3343. break;
  3344. }
  3345. }
  3346. MWidgetTimers.releasereadlock(__FUNCTION__, __LINE__);
  3347. }
  3348. return ret;
  3349. }
  3350. void ZoneServer::CheckWidgetTimers(){
  3351. vector<int32> remove_list;
  3352. map<int32, int32>::iterator itr;
  3353. MWidgetTimers.readlock(__FUNCTION__, __LINE__);
  3354. for (itr = widget_timers.begin(); itr != widget_timers.end(); itr++) {
  3355. if(Timer::GetCurrentTime2() >= itr->second){
  3356. /*Spawn* widget = GetSpawnByID(itr->first);
  3357. if (widget && widget->IsWidget())
  3358. ((Widget*)widget)->HandleTimerUpdate();*/
  3359. remove_list.push_back(itr->first);
  3360. }
  3361. }
  3362. MWidgetTimers.releasereadlock(__FUNCTION__, __LINE__);
  3363. for (int32 i = 0; i < remove_list.size(); i++) {
  3364. Spawn* widget = GetSpawnByID(remove_list[i]);
  3365. if (widget && widget->IsWidget())
  3366. ((Widget*)widget)->HandleTimerUpdate();
  3367. }
  3368. MWidgetTimers.writelock(__FUNCTION__, __LINE__);
  3369. for(int32 i=0;i<remove_list.size(); i++)
  3370. widget_timers.erase(remove_list[i]);
  3371. MWidgetTimers.releasewritelock(__FUNCTION__, __LINE__);
  3372. }
  3373. void ZoneServer::AddWidgetTimer(Spawn* widget, float time) {
  3374. if (widget && widget->IsWidget()) {
  3375. MWidgetTimers.writelock(__FUNCTION__, __LINE__);
  3376. widget_timers[widget->GetID()] = ((int32)(time * 1000)) + Timer::GetCurrentTime2();
  3377. MWidgetTimers.releasewritelock(__FUNCTION__, __LINE__);
  3378. }
  3379. }
  3380. Spawn* ZoneServer::GetSpawnGroup(int32 id){
  3381. Spawn* ret = 0;
  3382. Spawn* spawn = 0;
  3383. if(id < 1)
  3384. return 0;
  3385. bool lookup = false;
  3386. if(quick_group_id_lookup.count(id) > 0) {
  3387. ret = GetSpawnByID(quick_group_id_lookup.Get(id));
  3388. lookup = true;
  3389. }
  3390. if(ret == NULL) {
  3391. if(lookup)
  3392. quick_group_id_lookup.erase(id);
  3393. map<int32, Spawn*>::iterator itr;
  3394. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3395. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3396. spawn = itr->second;
  3397. if(spawn){
  3398. if(spawn->GetSpawnGroupID() == id){
  3399. ret = spawn;
  3400. quick_group_id_lookup.Put(id, spawn->GetID());
  3401. break;
  3402. }
  3403. }
  3404. }
  3405. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3406. }
  3407. return ret;
  3408. }
  3409. Spawn* ZoneServer::GetSpawnByLocationID(int32 location_id) {
  3410. Spawn* ret = 0;
  3411. Spawn* current_spawn = 0;
  3412. if(location_id < 1)
  3413. return 0;
  3414. bool lookup = false;
  3415. if(quick_location_id_lookup.count(location_id) > 0) {
  3416. ret = GetSpawnByID(quick_location_id_lookup.Get(location_id));
  3417. lookup = true;
  3418. }
  3419. if(ret == NULL) {
  3420. if(lookup)
  3421. quick_location_id_lookup.erase(location_id);
  3422. map<int32, Spawn*>::iterator itr;
  3423. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3424. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3425. current_spawn = itr->second;
  3426. if (current_spawn && current_spawn->GetSpawnLocationID() == location_id) {
  3427. ret = current_spawn;
  3428. quick_location_id_lookup.Put(location_id, ret->GetID());
  3429. break;
  3430. }
  3431. }
  3432. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3433. }
  3434. return ret;
  3435. }
  3436. Spawn* ZoneServer::GetSpawnByDatabaseID(int32 id){
  3437. Spawn* ret = 0;
  3438. if(id < 1)
  3439. return 0;
  3440. bool lookup = false;
  3441. if(quick_database_id_lookup.count(id) > 0) {
  3442. ret = GetSpawnByID(quick_database_id_lookup.Get(id));
  3443. lookup = true;
  3444. }
  3445. if(ret == NULL){
  3446. if(lookup)
  3447. quick_database_id_lookup.erase(id);
  3448. Spawn* spawn = 0;
  3449. map<int32, Spawn*>::iterator itr;
  3450. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3451. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++){
  3452. spawn = itr->second;
  3453. if(spawn){
  3454. if(spawn->GetDatabaseID() == id){
  3455. quick_database_id_lookup.Put(id, spawn->GetID());
  3456. ret = spawn;
  3457. break;
  3458. }
  3459. }
  3460. }
  3461. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3462. }
  3463. return ret;
  3464. }
  3465. Spawn* ZoneServer::GetSpawnByID(int32 id, bool spawnListLocked) {
  3466. Spawn* ret = 0;
  3467. if (!spawnListLocked )
  3468. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3469. if (spawn_list.count(id) > 0)
  3470. ret = spawn_list[id];
  3471. if (!spawnListLocked)
  3472. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3473. return ret;
  3474. }
  3475. bool ZoneServer::SendRemoveSpawn(Client* client, Spawn* spawn, PacketStruct* packet, bool delete_spawn)
  3476. {
  3477. if(!client || !spawn || (client && client->GetPlayer() == spawn))
  3478. return false;
  3479. if(client->GetPlayerPOVGhostSpawnID() == spawn->GetID()) {
  3480. client->SetPlayerPOVGhost(nullptr);
  3481. }
  3482. spawn->RemoveSpawnFromPlayer(client->GetPlayer());
  3483. // 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);
  3484. /* if(packet && index > 0 && !wasRemoved)
  3485. {
  3486. packet->ResetData();
  3487. packet->setDataByName("spawn_index", index);
  3488. spawn->RemoveSpawnFromPlayer(client->GetPlayer());
  3489. if(delete_spawn)
  3490. packet->setDataByName("delete", 1);
  3491. client->QueuePacket(packet->serialize());
  3492. return true;
  3493. }
  3494. return false;*/
  3495. return true;
  3496. }
  3497. void ZoneServer::SetSpawnCommand(Spawn* spawn, int8 type, char* value, Client* client){
  3498. //commands
  3499. LogWrite(MISC__TODO, 1, "TODO", "%s does nothing!\n%s, %i", __FUNCTION__, __FILE__, __LINE__);
  3500. }
  3501. void ZoneServer::SetSpawnCommand(int32 spawn_id, int8 type, char* value, Client* client){
  3502. LogWrite(MISC__TODO, 1, "TODO", "%s does nothing!\n%s, %i", __FUNCTION__, __FILE__, __LINE__);
  3503. }
  3504. void ZoneServer::ApplySetSpawnCommand(Client* client, Spawn* target, int8 type, const char* value){
  3505. // 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
  3506. // location values (x, y, z, heading, grid) for all spawns in the zone with the same DB ID, only the targeted spawn
  3507. if(type == SPAWN_SET_VALUE_SPAWNENTRY_SCRIPT || type == SPAWN_SET_VALUE_SPAWNLOCATION_SCRIPT || (type >= SPAWN_SET_VALUE_X && type <= SPAWN_SET_VALUE_LOCATION) ||
  3508. type == SPAWN_SET_VALUE_PITCH || type == SPAWN_SET_VALUE_ROLL)
  3509. return;
  3510. Spawn* tmp = 0;
  3511. if(target->IsNPC())
  3512. tmp = GetNPC(target->GetDatabaseID());
  3513. else if(target->IsObject())
  3514. tmp = GetObject(target->GetDatabaseID());
  3515. else if(target->IsGroundSpawn())
  3516. tmp = GetGroundSpawn(target->GetDatabaseID());
  3517. else if(target->IsSign())
  3518. tmp = GetSign(target->GetDatabaseID());
  3519. else if(target->IsWidget())
  3520. tmp = GetWidget(target->GetDatabaseID());
  3521. if(tmp && type == SPAWN_SET_VALUE_SPAWN_SCRIPT)
  3522. tmp->SetSpawnScript(value);
  3523. else if(tmp)
  3524. commands.SetSpawnCommand(client, tmp, type, value); // set the master spawn
  3525. Spawn* spawn = 0;
  3526. // this check needs to be here otherwise every spawn with 0 will be set
  3527. if ( target->GetDatabaseID ( ) > 0 )
  3528. {
  3529. map<int32, Spawn*>::iterator itr;
  3530. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3531. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3532. spawn = itr->second;
  3533. if(spawn && spawn->GetDatabaseID() == target->GetDatabaseID()){
  3534. if(type == SPAWN_SET_VALUE_SPAWN_SCRIPT)
  3535. spawn->SetSpawnScript(value);
  3536. else
  3537. commands.SetSpawnCommand(client, spawn, type, value);
  3538. }
  3539. }
  3540. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3541. }
  3542. }
  3543. void ZoneServer::StopSpawnScriptTimer(Spawn* spawn, std::string functionName){
  3544. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3545. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3546. if(spawn_script_timers.size() > 0){
  3547. set<SpawnScriptTimer*>::iterator itr;
  3548. SpawnScriptTimer* timer = 0;
  3549. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3550. timer = *itr;
  3551. if(timer->spawn == spawn->GetID() && (functionName == "" || timer->function == functionName) && remove_spawn_script_timers_list.count(timer) == 0) {
  3552. itr = spawn_script_timers.erase(itr);
  3553. safe_delete(timer);
  3554. }
  3555. }
  3556. }
  3557. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3558. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3559. }
  3560. void ZoneServer::DeleteSpawnScriptTimers(Spawn* spawn, bool all){
  3561. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3562. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3563. if(spawn_script_timers.size() > 0){
  3564. set<SpawnScriptTimer*>::iterator itr;
  3565. SpawnScriptTimer* timer = 0;
  3566. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3567. timer = *itr;
  3568. if((all || timer->spawn == spawn->GetID()) && remove_spawn_script_timers_list.count(timer) == 0)
  3569. remove_spawn_script_timers_list.insert(timer);
  3570. }
  3571. if(all)
  3572. spawn_script_timers.clear();
  3573. }
  3574. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3575. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3576. }
  3577. void ZoneServer::DeleteSpawnScriptTimers() {
  3578. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3579. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3580. if(remove_spawn_script_timers_list.size() > 0){
  3581. set<SpawnScriptTimer*>::iterator itr;
  3582. SpawnScriptTimer* timer = 0;
  3583. for (itr = remove_spawn_script_timers_list.begin(); itr != remove_spawn_script_timers_list.end(); itr++) {
  3584. timer = *itr;
  3585. spawn_script_timers.erase(timer);
  3586. safe_delete(timer);
  3587. }
  3588. remove_spawn_script_timers_list.clear();
  3589. }
  3590. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3591. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3592. }
  3593. void ZoneServer::CheckSpawnScriptTimers(){
  3594. DeleteSpawnScriptTimers();
  3595. SpawnScriptTimer* timer = 0;
  3596. vector<SpawnScriptTimer> call_timers;
  3597. MSpawnScriptTimers.readlock(__FUNCTION__, __LINE__);
  3598. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3599. if(spawn_script_timers.size() > 0){
  3600. int32 current_time = Timer::GetCurrentTime2();
  3601. set<SpawnScriptTimer*>::iterator itr;
  3602. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3603. timer = *itr;
  3604. if(remove_spawn_script_timers_list.count(timer) == 0 &&
  3605. timer->current_count < timer->max_count && current_time >= timer->timer){
  3606. timer->current_count++;
  3607. SpawnScriptTimer tmpTimer;
  3608. tmpTimer.current_count = timer->current_count;
  3609. tmpTimer.function = timer->function;
  3610. tmpTimer.player = timer->player;
  3611. tmpTimer.spawn = timer->spawn;
  3612. tmpTimer.max_count = timer->max_count;
  3613. call_timers.push_back(tmpTimer);
  3614. }
  3615. if(timer->current_count >= timer->max_count && remove_spawn_script_timers_list.count(timer) == 0)
  3616. remove_spawn_script_timers_list.insert(timer);
  3617. }
  3618. }
  3619. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3620. MSpawnScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  3621. if(call_timers.size() > 0){
  3622. vector<SpawnScriptTimer>::iterator itr;
  3623. for(itr = call_timers.begin(); itr != call_timers.end(); itr++){
  3624. SpawnScriptTimer tmpTimer = (SpawnScriptTimer)*itr;
  3625. CallSpawnScript(GetSpawnByID(tmpTimer.spawn), SPAWN_SCRIPT_TIMER, tmpTimer.player > 0 ? GetSpawnByID(tmpTimer.player) : 0, tmpTimer.function.c_str());
  3626. }
  3627. }
  3628. }
  3629. void ZoneServer::KillSpawnByDistance(Spawn* spawn, float max_distance, bool include_players, bool send_packet){
  3630. std::shared_lock lock(MGridMaps);
  3631. if(!spawn)
  3632. return;
  3633. Spawn* test_spawn = 0;
  3634. std::map<int32, GridMap*>::iterator grids = grid_maps.find(spawn->GetLocation());
  3635. if(grids != grid_maps.end()) {
  3636. grids->second->MSpawns.lock_shared();
  3637. typedef map <int32, Spawn*> SpawnMapType;
  3638. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  3639. test_spawn = it->second;
  3640. if(test_spawn && test_spawn->Alive() && test_spawn->GetID() > 0 && test_spawn->GetID() != spawn->GetID() && test_spawn->IsEntity() &&
  3641. (!test_spawn->IsPlayer() || include_players)){
  3642. if(test_spawn->GetDistance(spawn) < max_distance)
  3643. KillSpawn(true, test_spawn, spawn, send_packet);
  3644. }
  3645. }
  3646. grids->second->MSpawns.unlock_shared();
  3647. }
  3648. }
  3649. void ZoneServer::SpawnSetByDistance(Spawn* spawn, float max_distance, string field, string value){
  3650. std::shared_lock lock(MGridMaps);
  3651. if(!spawn)
  3652. return;
  3653. Spawn* test_spawn = 0;
  3654. int32 type = commands.GetSpawnSetType(field);
  3655. if(type == 0xFFFFFFFF)
  3656. return;
  3657. std::map<int32, GridMap*>::iterator grids = grid_maps.find(spawn->GetLocation());
  3658. if(grids != grid_maps.end()) {
  3659. grids->second->MSpawns.lock_shared();
  3660. typedef map <int32, Spawn*> SpawnMapType;
  3661. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  3662. test_spawn = it->second;
  3663. if(test_spawn && test_spawn->GetID() > 0 && test_spawn->GetID() != spawn->GetID() && !test_spawn->IsPlayer()){
  3664. if(test_spawn->GetDistance(spawn) < max_distance){
  3665. commands.SetSpawnCommand(0, test_spawn, type, value.c_str());
  3666. }
  3667. }
  3668. }
  3669. grids->second->MSpawns.unlock_shared();
  3670. }
  3671. }
  3672. void ZoneServer::AddSpawnScriptTimer(SpawnScriptTimer* timer){
  3673. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3674. spawn_script_timers.insert(timer);
  3675. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3676. }
  3677. /*
  3678. void ZoneServer::RemoveFromRangeMap(Client* client){
  3679. spawn_range_map.erase(client);
  3680. }
  3681. */
  3682. void ZoneServer::RemoveSpawn(Spawn* spawn, bool delete_spawn, bool respawn, bool lock, bool erase_from_spawn_list, bool lock_spell_process)
  3683. {
  3684. LogWrite(ZONE__DEBUG, 3, "Zone", "Processing RemoveSpawn function for %s (%i)...", spawn->GetName(),spawn->GetID());
  3685. PacketStruct* packet = 0;
  3686. int16 packet_version = 0;
  3687. Client* client = 0;
  3688. vector<Client*>::iterator client_itr;
  3689. MClientList.readlock(__FUNCTION__, __LINE__);
  3690. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3691. client = *client_itr;
  3692. if (client && (client->GetVersion() > 283 || !client->IsZoning() || client->GetPlayer() != spawn)) { //don't send destroy ghost of 283 client when zoning
  3693. if (client->GetPlayer()->HasTarget() && client->GetPlayer()->GetTarget() == spawn)
  3694. client->GetPlayer()->SetTarget(0);
  3695. if(client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->IsSendingSpawn(spawn->GetID()))
  3696. SendRemoveSpawn(client, spawn, packet, delete_spawn);
  3697. if (spawn_range_map.count(client) > 0)
  3698. spawn_range_map.Get(client)->erase(spawn->GetID());
  3699. }
  3700. }
  3701. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3702. safe_delete(packet);
  3703. spawn->RemoveSpawnProximities();
  3704. RemoveSpawnProximities(spawn);
  3705. if (movementMgr != nullptr && spawn->IsEntity()) {
  3706. movementMgr->RemoveMob((Entity*)spawn);
  3707. }
  3708. RemoveSpawnSupportFunctions(spawn, lock_spell_process);
  3709. if (reloading)
  3710. RemoveDeadEnemyList(spawn);
  3711. if (lock)
  3712. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  3713. if (dead_spawns.count(spawn->GetID()) > 0)
  3714. dead_spawns.erase(spawn->GetID());
  3715. if (lock)
  3716. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  3717. if (spawn_expire_timers.count(spawn->GetID()) > 0)
  3718. spawn_expire_timers.erase(spawn->GetID());
  3719. spawn->SetDeletedSpawn(true);
  3720. // 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)
  3721. if(erase_from_spawn_list)
  3722. AddPendingSpawnRemove(spawn->GetID());
  3723. if(respawn && !spawn->IsPlayer() && spawn->GetRespawnTime() > 0 && spawn->GetSpawnLocationID() > 0)
  3724. {
  3725. LogWrite(ZONE__DEBUG, 3, "Zone", "Handle NPC Respawn for '%s'.", spawn->GetName());
  3726. // handle instance spawn db info
  3727. // we don't care if a NPC or a client kills the spawn, we could have events that cause NPCs to kill NPCs.
  3728. if(spawn->GetZone()->GetInstanceID() > 0 && spawn->GetSpawnLocationID() > 0)
  3729. {
  3730. LogWrite(ZONE__DEBUG, 3, "Zone", "Handle NPC Respawn in an Instance.");
  3731. // use respawn time to either insert/update entry (likely insert in this situation)
  3732. if ( spawn->IsNPC() )
  3733. {
  3734. database.CreateInstanceSpawnRemoved(spawn->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC,
  3735. spawn->GetRespawnTime(),spawn->GetZone()->GetInstanceID());
  3736. }
  3737. else if ( spawn->IsObject ( ) )
  3738. {
  3739. database.CreateInstanceSpawnRemoved(spawn->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT,
  3740. spawn->GetRespawnTime(),spawn->GetZone()->GetInstanceID());
  3741. }
  3742. }
  3743. else
  3744. {
  3745. int32 spawnLocationID = spawn->GetSpawnLocationID();
  3746. int32 spawnRespawnTime = spawn->GetRespawnTime();
  3747. respawn_timers.Put(spawnLocationID, Timer::GetCurrentTime2() + spawnRespawnTime * 1000);
  3748. }
  3749. }
  3750. RemoveSpawnFromGrid(spawn, spawn->GetLocation());
  3751. // 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
  3752. if (lock && !respawn)
  3753. MDeadSpawns.readlock(__FUNCTION__, __LINE__);
  3754. if(delete_spawn && dead_spawns.count(spawn->GetID()) == 0)
  3755. AddPendingDelete(spawn);
  3756. if (lock && !respawn)
  3757. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  3758. LogWrite(ZONE__DEBUG, 3, "Zone", "Done processing RemoveSpawn function...");
  3759. }
  3760. Spawn* ZoneServer::GetClosestSpawn(Spawn* spawn, int32 spawn_id){
  3761. Spawn* closest_spawn = 0;
  3762. Spawn* test_spawn = 0;
  3763. float closest_distance = 1000000;
  3764. float test_distance = 0;
  3765. map<int32, Spawn*>::iterator itr;
  3766. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3767. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3768. test_spawn = itr->second;
  3769. if(test_spawn && test_spawn != spawn && test_spawn->GetDatabaseID() == spawn_id){
  3770. test_distance = test_spawn->GetDistance(spawn);
  3771. if(test_distance < closest_distance){
  3772. closest_distance = test_distance;
  3773. closest_spawn = test_spawn;
  3774. }
  3775. }
  3776. }
  3777. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3778. return closest_spawn;
  3779. }
  3780. int32 ZoneServer::GetClosestLocation(Spawn* spawn){
  3781. Spawn* closest_spawn = 0;
  3782. Spawn* test_spawn = 0;
  3783. float closest_distance = 1000000;
  3784. float test_distance = 0;
  3785. map<int32, Spawn*>::iterator itr;
  3786. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3787. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3788. test_spawn = itr->second;
  3789. if(test_spawn){
  3790. test_distance = test_spawn->GetDistance(spawn);
  3791. if(test_distance < closest_distance){
  3792. closest_distance = test_distance;
  3793. closest_spawn = test_spawn;
  3794. if(closest_distance < 10)
  3795. break;
  3796. }
  3797. }
  3798. }
  3799. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3800. if(closest_spawn)
  3801. return closest_spawn->GetLocation();
  3802. return 0;
  3803. }
  3804. void ZoneServer::SendQuestUpdates(Client* client, Spawn* spawn){
  3805. if(!client)
  3806. return;
  3807. if(spawn){
  3808. if(client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  3809. SendSpawnChanges(spawn, client, false, true);
  3810. }
  3811. else{
  3812. client->GetCurrentZone()->SendAllSpawnsForVisChange(client);
  3813. }
  3814. }
  3815. void ZoneServer::SendAllSpawnsForLevelChange(Client* client) {
  3816. Spawn* spawn = 0;
  3817. if (spawn_range_map.count(client) > 0) {
  3818. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3819. while (itr.Next()) {
  3820. spawn = GetSpawnByID(itr->first);
  3821. if (spawn && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  3822. SendSpawnChanges(spawn, client, false, true);
  3823. // Attempt to slow down the packet spam sent to the client
  3824. // who the bloody fuck put a Sleep here
  3825. //Sleep(5);
  3826. }
  3827. }
  3828. }
  3829. }
  3830. void ZoneServer::SendAllSpawnsForSeeInvisChange(Client* client) {
  3831. Spawn* spawn = 0;
  3832. if (spawn_range_map.count(client) > 0) {
  3833. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3834. while (itr.Next()) {
  3835. spawn = GetSpawnByID(itr->first);
  3836. if (spawn && spawn->IsEntity() && (((Entity*)spawn)->IsInvis() || ((Entity*)spawn)->IsStealthed()) && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  3837. SendSpawnChanges(spawn, client, true, true);
  3838. }
  3839. }
  3840. }
  3841. }
  3842. void ZoneServer::SendAllSpawnsForVisChange(Client* client, bool limitToEntities) {
  3843. Spawn* spawn = 0;
  3844. if (spawn_range_map.count(client) > 0) {
  3845. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3846. while (itr.Next()) {
  3847. spawn = GetSpawnByID(itr->first);
  3848. if (spawn && (!limitToEntities || (limitToEntities && spawn->IsEntity())) && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  3849. SendSpawnChanges(spawn, client, false, true);
  3850. }
  3851. }
  3852. }
  3853. }
  3854. void ZoneServer::StartZoneSpawnsForLevelThread(Client* client){
  3855. if(zoneShuttingDown)
  3856. return;
  3857. #ifdef WIN32
  3858. _beginthread(SendLevelChangedSpawns, 0, client);
  3859. #else
  3860. pthread_t thread;
  3861. pthread_create(&thread, NULL, SendLevelChangedSpawns, client);
  3862. pthread_detach(thread);
  3863. #endif
  3864. }
  3865. void ZoneServer::ReloadClientQuests(){
  3866. Client* client = 0;
  3867. vector<Client*>::iterator client_itr;
  3868. MClientList.readlock(__FUNCTION__, __LINE__);
  3869. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3870. client = *client_itr;
  3871. if(client)
  3872. client->ReloadQuests();
  3873. }
  3874. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3875. }
  3876. void ZoneServer::SendCalculatedXP(Player* player, Spawn* victim){
  3877. if (player && victim) {
  3878. if (player->GetGroupMemberInfo()) {
  3879. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  3880. PlayerGroup* group = world.GetGroupManager()->GetGroup(player->GetGroupMemberInfo()->group_id);
  3881. if (group)
  3882. {
  3883. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  3884. deque<GroupMemberInfo*>* members = group->GetMembers();
  3885. deque<GroupMemberInfo*>::iterator itr;
  3886. bool skipGrayMob = false;
  3887. for (itr = members->begin(); itr != members->end(); itr++) {
  3888. GroupMemberInfo* gmi = *itr;
  3889. if (gmi->client) {
  3890. Player* group_member = gmi->client->GetPlayer();
  3891. if(group_member && group_member->GetArrowColor(victim->GetLevel()) == ARROW_COLOR_GRAY) {
  3892. skipGrayMob = true;
  3893. break;
  3894. }
  3895. }
  3896. }
  3897. for (itr = members->begin(); !skipGrayMob && itr != members->end(); itr++) {
  3898. GroupMemberInfo* gmi = *itr;
  3899. if (gmi->client) {
  3900. Player* group_member = gmi->client->GetPlayer();
  3901. if(group_member) {
  3902. float xp = group_member->CalculateXP(victim) / members->size();
  3903. if (xp > 0) {
  3904. group_member->AddXP((int32)xp);
  3905. }
  3906. }
  3907. }
  3908. }
  3909. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  3910. }
  3911. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  3912. }
  3913. else {
  3914. float xp = player->CalculateXP(victim);
  3915. if (xp > 0) {
  3916. Client* client = GetClientBySpawn(player);
  3917. if(!client)
  3918. return;
  3919. player->AddXP((int32)xp);
  3920. }
  3921. }
  3922. }
  3923. }
  3924. void ZoneServer::ProcessFaction(Spawn* spawn, Client* client)
  3925. {
  3926. if(client && !spawn->IsPlayer() && spawn->GetFactionID() > 10)
  3927. {
  3928. bool update_result = false;
  3929. Faction* faction = 0;
  3930. vector<int32>* factions = 0;
  3931. Player* player = client->GetPlayer();
  3932. bool hasfaction = database.VerifyFactionID(player->GetCharacterID(), spawn->GetFactionID());
  3933. //if 0 they dont have an entry in the db for this faction. if one they do and we can skip it.
  3934. if(hasfaction == 0) {
  3935. //Find out the default for this faction
  3936. sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
  3937. //add the default faction for the player.
  3938. player->SetFactionValue(spawn->GetFactionID(), defaultfaction);
  3939. //save the character so the new default gets written to the db.
  3940. client->Save();
  3941. }
  3942. if(player->GetFactions()->ShouldDecrease(spawn->GetFactionID()))
  3943. {
  3944. update_result = player->GetFactions()->DecreaseFaction(spawn->GetFactionID());
  3945. faction = master_faction_list.GetFaction(spawn->GetFactionID());
  3946. if(faction && update_result)
  3947. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  3948. else if(faction)
  3949. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  3950. factions = master_faction_list.GetHostileFactions(spawn->GetFactionID());
  3951. if(factions)
  3952. {
  3953. vector<int32>::iterator itr;
  3954. for(itr = factions->begin(); itr != factions->end(); itr++)
  3955. {
  3956. if(player->GetFactions()->ShouldIncrease(*itr))
  3957. {
  3958. update_result = player->GetFactions()->IncreaseFaction(*itr);
  3959. faction = master_faction_list.GetFaction(*itr);
  3960. if(faction && update_result)
  3961. client->Message(CHANNEL_FACTION, "Your faction standing with %s got better.", faction->name.c_str());
  3962. else if(faction)
  3963. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any better.", faction->name.c_str());
  3964. }
  3965. }
  3966. }
  3967. }
  3968. factions = master_faction_list.GetFriendlyFactions(spawn->GetFactionID());
  3969. if(factions)
  3970. {
  3971. vector<int32>::iterator itr;
  3972. for(itr = factions->begin(); itr != factions->end(); itr++)
  3973. {
  3974. if(player->GetFactions()->ShouldDecrease(*itr))
  3975. {
  3976. bool hasfaction = database.VerifyFactionID(player->GetCharacterID(),spawn->GetFactionID());
  3977. if(hasfaction == 0) {
  3978. //they do not have the faction. Lets get the default value and feed it in.
  3979. sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
  3980. //add the default faction for the player.
  3981. player->SetFactionValue(spawn->GetFactionID(), defaultfaction);
  3982. }
  3983. update_result = player->GetFactions()->DecreaseFaction(*itr);
  3984. faction = master_faction_list.GetFaction(*itr);
  3985. if(faction && update_result)
  3986. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  3987. else if(faction)
  3988. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  3989. }
  3990. }
  3991. }
  3992. EQ2Packet* outapp = client->GetPlayer()->GetFactions()->FactionUpdate(client->GetVersion());
  3993. if(outapp)
  3994. client->QueuePacket(outapp);
  3995. }
  3996. }
  3997. void ZoneServer::Despawn(Spawn* spawn, int32 timer){
  3998. if (spawn && movementMgr != nullptr) {
  3999. movementMgr->RemoveMob((Entity*)spawn);
  4000. }
  4001. if(!spawn || spawn->IsPlayer())
  4002. return;
  4003. if(spawn->IsEntity())
  4004. ((Entity*)spawn)->InCombat(false);
  4005. if(timer == 0)
  4006. timer = 1;
  4007. AddDeadSpawn(spawn, timer);
  4008. }
  4009. void ZoneServer::KillSpawn(bool spawnListLocked, Spawn* dead, Spawn* killer, bool send_packet, int8 type, int8 damage_type, int16 kill_blow_type)
  4010. {
  4011. bool isSpell = (type == DAMAGE_PACKET_TYPE_SIPHON_SPELL || type == DAMAGE_PACKET_TYPE_SIPHON_SPELL2 ||
  4012. type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE || type == DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG ||
  4013. type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE2 || type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE3);
  4014. MDeadSpawns.readlock(__FUNCTION__, __LINE__);
  4015. if(!dead || this->dead_spawns.count(dead->GetID()) > 0) {
  4016. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  4017. return;
  4018. }
  4019. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  4020. PacketStruct* packet = 0;
  4021. Client* client = 0;
  4022. vector<int32>* encounter = 0;
  4023. bool killer_in_encounter = false;
  4024. if(dead->IsEntity())
  4025. {
  4026. // add any special quest related loot (no_drop_quest_completed)
  4027. if(dead->IsNPC() && killer && killer != dead)
  4028. AddLoot((NPC*)dead, killer);
  4029. ((Entity*)dead)->InCombat(false);
  4030. dead->SetInitialState(16512, false); // This will make aerial npc's fall after death
  4031. dead->SetHP(0);
  4032. dead->SetSpawnType(3);
  4033. dead->appearance.attackable = 0;
  4034. // Remove hate towards dead from all npc's in the zone
  4035. ClearHate((Entity*)dead);
  4036. // Check kill and death procs
  4037. if (killer && dead != killer){
  4038. if (dead->IsEntity())
  4039. ((Entity*)dead)->CheckProcs(PROC_TYPE_DEATH, killer);
  4040. if (killer->IsEntity())
  4041. ((Entity*)killer)->CheckProcs(PROC_TYPE_KILL, dead);
  4042. }
  4043. //Check if caster is alive after death proc called, incase of deathsave
  4044. if (dead->Alive())
  4045. return;
  4046. RemoveSpellTimersFromSpawn(dead, true, !dead->IsPlayer(), true, !isSpell);
  4047. ((Entity*)dead)->IsCasting(false);
  4048. if(dead->IsPlayer())
  4049. {
  4050. ((Player*)dead)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_DEATHS, 1);
  4051. client = GetClientBySpawn(dead);
  4052. ((Entity*)dead)->HandleDeathExperienceDebt(killer);
  4053. if(client) {
  4054. if(client->GetPlayer()->DamageEquippedItems(10, client))
  4055. client->QueuePacket(client->GetPlayer()->GetEquipmentList()->serialize(client->GetVersion(), client->GetPlayer()));
  4056. client->DisplayDeadWindow();
  4057. }
  4058. }
  4059. else if (dead->IsNPC()) {
  4060. encounter = ((NPC*)dead)->Brain()->GetEncounter();
  4061. }
  4062. }
  4063. dead->SetActionState(0);
  4064. dead->SetTempActionState(0);
  4065. // Needs npc to have access to the encounter list for who is allowed to loot
  4066. NPC* chest = 0;
  4067. if (dead->IsNPC() && !((NPC*)dead)->Brain()->PlayerInEncounter()) {
  4068. dead->SetLootCoins(0);
  4069. dead->ClearLoot();
  4070. }
  4071. Spawn* groupMemberAlive = nullptr;
  4072. // If dead has loot attempt to drop a chest
  4073. if (dead->HasLoot()) {
  4074. if(!(groupMemberAlive = dead->IsSpawnGroupMembersAlive(dead))) {
  4075. chest = ((Entity*)dead)->DropChest();
  4076. }
  4077. else {
  4078. switch(dead->GetLootDropType()) {
  4079. case 0:
  4080. // default drop all chest type as a group
  4081. dead->TransferLoot(groupMemberAlive);
  4082. break;
  4083. case 1:
  4084. // this is a primary mob it drops its own loot
  4085. chest = ((Entity*)dead)->DropChest();
  4086. break;
  4087. }
  4088. }
  4089. }
  4090. // If dead is an npc get the encounter and loop through it giving out the rewards, no rewards for pets
  4091. if (dead->IsNPC() && !dead->IsPet() && !dead->IsBot()) {
  4092. Spawn* spawn = 0;
  4093. int8 size = encounter->size();
  4094. for (int8 i = 0; i < encounter->size(); i++) {
  4095. spawn = GetSpawnByID(encounter->at(i), spawnListLocked);
  4096. // set a flag to let us know if the killer is in the encounter
  4097. if (!killer_in_encounter && spawn == killer)
  4098. killer_in_encounter = true;
  4099. if (spawn && spawn->IsPlayer()) {
  4100. // Update players total kill count
  4101. ((Player*)spawn)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_NPC_KILLS, 1);
  4102. // If this was an epic mob kill send the announcement for this player
  4103. if (dead->GetEncounterLevel() >= 10)
  4104. SendEpicMobDeathToGuild((Player*)spawn, dead);
  4105. // Clear hostile spells from the players spell queue
  4106. spellProcess->RemoveSpellFromQueue((Player*)spawn, true);
  4107. // Get the client of the player
  4108. client = GetClientBySpawn(spawn);
  4109. // valid client?
  4110. if (client) {
  4111. // Check for quest kill updates
  4112. client->CheckPlayerQuestsKillUpdate(dead);
  4113. // 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
  4114. if (!dead->IsPlayer() && dead->GetFactionID() > 10)
  4115. ProcessFaction(dead, client);
  4116. // Send xp...this is currently wrong fix it
  4117. if (spawn != dead && ((Player*)spawn)->GetArrowColor(dead->GetLevel()) >= ARROW_COLOR_GREEN) {
  4118. //SendCalculatedXP((Player*)spawn, dead);
  4119. float xp = ((Player*)spawn)->CalculateXP(dead) / size;
  4120. if (xp > 0) {
  4121. ((Player*)spawn)->AddXP((int32)xp);
  4122. }
  4123. }
  4124. }
  4125. }
  4126. // If a chest is being dropped add this spawn to the chest's encounter so they can loot it
  4127. if (chest && spawn && spawn->IsEntity())
  4128. chest->Brain()->AddToEncounter((Entity*)spawn);
  4129. }
  4130. }
  4131. // If a chest is being dropped add it to the world and set the timer to remove it.
  4132. if (chest) {
  4133. AddSpawn(chest);
  4134. AddDeadSpawn(chest, 0xFFFFFFFF);
  4135. LogWrite(LOOT__DEBUG, 0, "Loot", "Adding a chest to the world...");
  4136. }
  4137. // Reset client pointer
  4138. client = 0;
  4139. // Killer was not in the encounter, give them the faction hit but no xp
  4140. if (!killer_in_encounter) {
  4141. // make sure the killer is a player and the dead spawn had a faction and wasn't a player
  4142. if (killer && killer->IsPlayer()) {
  4143. if (!dead->IsPlayer() && dead->GetFactionID() > 10) {
  4144. client = GetClientBySpawn(killer);
  4145. if (client)
  4146. ProcessFaction(dead, client);
  4147. }
  4148. // Clear hostile spells from the killers spell queue
  4149. spellProcess->RemoveSpellFromQueue((Player*)killer, true);
  4150. }
  4151. }
  4152. // Reset client pointer
  4153. client = 0;
  4154. vector<Spawn*>* group = dead->GetSpawnGroup();
  4155. if (group && group->size() == 1)
  4156. CallSpawnScript(dead, SPAWN_SCRIPT_GROUP_DEAD, killer);
  4157. safe_delete(group);
  4158. // Remove the support functions for the dead spawn
  4159. RemoveSpawnSupportFunctions(dead, !isSpell);
  4160. // Erase the expire timer if it has one
  4161. if (spawn_expire_timers.count(dead->GetID()) > 0)
  4162. spawn_expire_timers.erase(dead->GetID());
  4163. // If dead is an npc or object call the spawn scrip and handle instance stuff
  4164. if(dead->IsNPC() || dead->IsObject())
  4165. {
  4166. // handle instance spawn db info
  4167. // we don't care if a NPC or a client kills the spawn, we could have events that cause NPCs to kill NPCs.
  4168. if(dead->GetZone()->GetInstanceID() > 0 && dead->GetSpawnLocationID() > 0)
  4169. {
  4170. // use respawn time to either insert/update entry (likely insert in this situation)
  4171. if(dead->IsNPC())
  4172. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4173. else if ( dead->IsObject ( ) )
  4174. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4175. }
  4176. // Call the spawn scripts death() function
  4177. CallSpawnScript(dead, SPAWN_SCRIPT_DEATH, killer);
  4178. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  4179. if (zone_script && lua_interface)
  4180. lua_interface->RunZoneScript(zone_script, "spawn_killed", this, dead, 0, 0, killer);
  4181. }
  4182. int32 victim_id = dead->GetID();
  4183. int32 attacker_id = 0xFFFFFFFF;
  4184. if(killer)
  4185. attacker_id = killer->GetID();
  4186. if(send_packet)
  4187. {
  4188. vector<Client*>::iterator client_itr;
  4189. MClientList.readlock(__FUNCTION__, __LINE__);
  4190. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4191. client = *client_itr;
  4192. if(!client->GetPlayer()->WasSentSpawn(victim_id) || (attacker_id != 0xFFFFFFFF && !client->GetPlayer()->WasSentSpawn(attacker_id)) )
  4193. continue;
  4194. else if(killer && killer->GetDistance(client->GetPlayer()) > HEAR_SPAWN_DISTANCE)
  4195. continue;
  4196. packet = configReader.getStruct("WS_HearDeath", client->GetVersion());
  4197. if(packet)
  4198. {
  4199. if(killer)
  4200. packet->setDataByName("attacker", client->GetPlayer()->GetIDWithPlayerSpawn(killer));
  4201. else
  4202. packet->setDataByName("attacker", 0xFFFFFFFF);
  4203. packet->setDataByName("defender", client->GetPlayer()->GetIDWithPlayerSpawn(dead));
  4204. packet->setDataByName("damage_type", damage_type);
  4205. packet->setDataByName("blow_type", kill_blow_type);
  4206. client->QueuePacket(packet->serialize());
  4207. LogWrite(COMBAT__DEBUG, 0, "Combat", "Zone Killing '%s'", dead->GetName());
  4208. safe_delete(packet);
  4209. }
  4210. }
  4211. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4212. }
  4213. int32 pop_timer = 0xFFFFFFFF;
  4214. if(killer && killer->IsNPC())
  4215. {
  4216. // Call the spawn scripts killed() function
  4217. CallSpawnScript(killer, SPAWN_SCRIPT_KILLED, dead);
  4218. if(!dead->IsPlayer())
  4219. {
  4220. LogWrite(MISC__TODO, 1, "TODO", "Whenever pets are added, check for pet kills\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  4221. // Set the time for the corpse to linger to 5 sec
  4222. //pop_timer = 5000;
  4223. // commented out the timer so in the event the killer is not a player (pet, guard, something else i haven't thought of)
  4224. // the corpse doesn't vanish sooner then it should if it had loot for the players. AddDeadSpawn() will set timers based on if
  4225. // the corpse has loot or not if the timer value (pop_timer) is 0xFFFFFFFF
  4226. }
  4227. }
  4228. // If the dead spawns was not a player add it to the dead spawn list
  4229. if (!dead->IsPlayer() && !dead->IsBot())
  4230. AddDeadSpawn(dead, pop_timer);
  4231. // if dead was a player clear hostile spells from its spell queue
  4232. if (dead->IsPlayer())
  4233. spellProcess->RemoveSpellFromQueue((Player*)dead, true);
  4234. if (dead->IsNPC())
  4235. ((NPC*)dead)->Brain()->ClearHate();
  4236. // ResetPetInfo() is called in DismissPet(), might not need to be here
  4237. // Players pet is killed, clear the pet info from char sheet
  4238. /*if (dead->IsNPC() && ((NPC*)dead)->IsPet() && ((NPC*)dead)->GetOwner()->IsPlayer())
  4239. ((Player*)((NPC*)dead)->GetOwner())->ResetPetInfo();*/
  4240. safe_delete(encounter);
  4241. }
  4242. void ZoneServer::SendDamagePacket(Spawn* attacker, Spawn* victim, int8 type1, int8 type2, int8 damage_type, int16 damage, const char* spell_name) {
  4243. //Scat: was set but never being used anywhere. i see references to 0xFFFFFFFF below so could be old code not used anymore
  4244. //int32 attacker_id = 0xFFFFFFFF;
  4245. //if(attacker)
  4246. // attacker_id = attacker->GetID();
  4247. PacketStruct* packet = 0;
  4248. Client* client = 0;
  4249. if (attacker && victim && victim->IsPlayer() && victim->GetTarget() == 0) {
  4250. client = GetClientBySpawn(victim);
  4251. if (client)
  4252. client->TargetSpawn(attacker);
  4253. }
  4254. vector<Client*>::iterator client_itr;
  4255. MClientList.readlock(__FUNCTION__, __LINE__);
  4256. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4257. client = *client_itr;
  4258. if (!client || (client->GetPlayer() != attacker && client->GetPlayer() != victim && ((attacker && client->GetPlayer()->WasSentSpawn(attacker->GetID()) == false) || (victim && client->GetPlayer()->WasSentSpawn(victim->GetID()) == false))))
  4259. continue;
  4260. if (attacker && attacker->GetDistance(client->GetPlayer()) > 50)
  4261. continue;
  4262. if (victim && victim->GetDistance(client->GetPlayer()) > 50)
  4263. continue;
  4264. switch (type1) {
  4265. case DAMAGE_PACKET_TYPE_SIPHON_SPELL:
  4266. case DAMAGE_PACKET_TYPE_SIPHON_SPELL2:
  4267. packet = configReader.getStruct("WS_HearSiphonSpellDamage", client->GetVersion());
  4268. break;
  4269. case DAMAGE_PACKET_TYPE_MULTIPLE_DAMAGE:
  4270. if (client->GetVersion() > 546)
  4271. packet = configReader.getStruct("WS_HearMultipleDamage", client->GetVersion());
  4272. else
  4273. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4274. break;
  4275. case DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG:
  4276. case DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE:
  4277. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4278. break;
  4279. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE2:
  4280. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE3:
  4281. case DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG:
  4282. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE:
  4283. if (client->GetVersion() > 546)
  4284. packet = configReader.getStruct("WS_HearSpellDamage", client->GetVersion());
  4285. else
  4286. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4287. if (packet)
  4288. packet->setSubstructDataByName("header", "unknown", 5);
  4289. break;
  4290. case DAMAGE_PACKET_TYPE_RANGE_DAMAGE:
  4291. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4292. break;
  4293. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG:
  4294. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG2:
  4295. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4296. break;
  4297. default:
  4298. LogWrite(ZONE__ERROR, 0, "Zone", "Unknown Damage Packet type: %i in ZoneServer::SendDamagePacket.", type1);
  4299. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4300. return;
  4301. }
  4302. if (packet) {
  4303. if (client->GetVersion() > 546) {
  4304. packet->setSubstructDataByName("header", "packet_type", type1);
  4305. packet->setSubstructDataByName("header", "result_type", type2);
  4306. packet->setDataByName("damage_type", damage_type);
  4307. packet->setDataByName("damage", damage);
  4308. }
  4309. else {
  4310. switch (type2) {
  4311. case DAMAGE_PACKET_RESULT_MISS:
  4312. packet->setSubstructDataByName("header", "result_type", 1);
  4313. break;
  4314. case DAMAGE_PACKET_RESULT_DODGE:
  4315. packet->setSubstructDataByName("header", "result_type", 2);
  4316. break;
  4317. case DAMAGE_PACKET_RESULT_PARRY:
  4318. packet->setSubstructDataByName("header", "result_type", 3);
  4319. break;
  4320. case DAMAGE_PACKET_RESULT_RIPOSTE:
  4321. packet->setSubstructDataByName("header", "result_type", 4);
  4322. break;
  4323. case DAMAGE_PACKET_RESULT_BLOCK:
  4324. packet->setSubstructDataByName("header", "result_type", 5);
  4325. break;
  4326. case DAMAGE_PACKET_RESULT_INVULNERABLE:
  4327. packet->setSubstructDataByName("header", "result_type", 7);
  4328. break;
  4329. case DAMAGE_PACKET_RESULT_RESIST:
  4330. packet->setSubstructDataByName("header", "result_type", 9);
  4331. break;
  4332. case DAMAGE_PACKET_RESULT_REFLECT:
  4333. packet->setSubstructDataByName("header", "result_type", 10);
  4334. break;
  4335. case DAMAGE_PACKET_RESULT_IMMUNE:
  4336. packet->setSubstructDataByName("header", "result_type", 11);
  4337. break;
  4338. }
  4339. packet->setArrayLengthByName("num_dmg", 1);
  4340. packet->setSubstructDataByName("header", "defender_proxy", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4341. packet->setArrayDataByName("damage_type", damage_type);
  4342. packet->setArrayDataByName("damage", damage);
  4343. }
  4344. if (!attacker)
  4345. packet->setSubstructDataByName("header", "attacker", 0xFFFFFFFF);
  4346. else
  4347. packet->setSubstructDataByName("header", "attacker", client->GetPlayer()->GetIDWithPlayerSpawn(attacker));
  4348. packet->setSubstructDataByName("header", "defender", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4349. if (spell_name) {
  4350. packet->setDataByName("spell", 1);
  4351. packet->setDataByName("spell_name", spell_name);
  4352. }
  4353. EQ2Packet* app = packet->serialize();
  4354. //DumpPacket(app);
  4355. client->QueuePacket(app);
  4356. safe_delete(packet);
  4357. packet = 0;
  4358. }
  4359. }
  4360. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4361. }
  4362. void ZoneServer::SendHealPacket(Spawn* caster, Spawn* target, int16 heal_type, int32 heal_amt, const char* spell_name){
  4363. Client* client = 0;
  4364. vector<Client*>::iterator client_itr;
  4365. MClientList.readlock(__FUNCTION__, __LINE__);
  4366. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4367. client = *client_itr;
  4368. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4369. continue;
  4370. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4371. continue;
  4372. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4373. continue;
  4374. PacketStruct* packet = configReader.getStruct("WS_HearHeal", client->GetVersion());
  4375. if (packet) {
  4376. packet->setDataByName("caster", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4377. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4378. packet->setDataByName("heal_amt", heal_amt);
  4379. packet->setDataByName("spellname", spell_name);
  4380. packet->setDataByName("type", heal_type);
  4381. packet->setDataByName("unknown2", 1);
  4382. EQ2Packet* app = packet->serialize();
  4383. client->QueuePacket(app);
  4384. safe_delete(packet);
  4385. }
  4386. }
  4387. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4388. }
  4389. void ZoneServer::SendThreatPacket(Spawn* caster, Spawn* target, int32 threat_amt, const char* spell_name) {
  4390. Client* client = 0;
  4391. vector<Client*>::iterator client_itr;
  4392. MClientList.readlock(__FUNCTION__, __LINE__);
  4393. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4394. client = *client_itr;
  4395. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4396. continue;
  4397. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4398. continue;
  4399. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4400. continue;
  4401. PacketStruct* packet = configReader.getStruct("WS_HearThreatCmd", client->GetVersion());
  4402. if (packet) {
  4403. packet->setDataByName("spell_name", spell_name);
  4404. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4405. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4406. packet->setDataByName("threat_amount", threat_amt);
  4407. client->QueuePacket(packet->serialize());
  4408. }
  4409. safe_delete(packet);
  4410. }
  4411. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4412. }
  4413. void ZoneServer::SendSpellFailedPacket(Client* client, int16 error){
  4414. if(!client)
  4415. return;
  4416. PacketStruct* packet = configReader.getStruct("WS_DisplaySpellFailed", client->GetVersion());
  4417. if(packet){
  4418. /* Temp solution, need to modify the error code before this function and while we still have access to the spell/combat art */
  4419. error = master_spell_list.GetSpellErrorValue(client->GetVersion(), error);
  4420. packet->setDataByName("error_code", error);
  4421. //packet->PrintPacket();
  4422. client->QueuePacket(packet->serialize());
  4423. safe_delete(packet);
  4424. }
  4425. }
  4426. void ZoneServer::SendInterruptPacket(Spawn* interrupted, LuaSpell* spell, bool fizzle){
  4427. if(!interrupted || !spell)
  4428. return;
  4429. EQ2Packet* outapp = 0;
  4430. PacketStruct* packet = 0;
  4431. Client* client = 0;
  4432. vector<Client*>::iterator client_itr;
  4433. MClientList.readlock(__FUNCTION__, __LINE__);
  4434. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4435. client = *client_itr;
  4436. if(!client || !client->GetPlayer()->WasSentSpawn(interrupted->GetID()))
  4437. continue;
  4438. packet = configReader.getStruct(fizzle ? "WS_SpellFizzle" : "WS_Interrupt", client->GetVersion());
  4439. if(packet){
  4440. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(interrupted));
  4441. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4442. for (int32 i = 0; i < spell->targets.size(); i++)
  4443. packet->setArrayDataByName("target_id", client->GetPlayer()->GetIDWithPlayerSpawn(client->GetPlayer()->GetZone()->GetSpawnByID(spell->targets[i])), i);
  4444. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4445. outapp = packet->serialize();
  4446. client->QueuePacket(outapp);
  4447. safe_delete(packet);
  4448. }
  4449. }
  4450. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4451. safe_delete(packet);
  4452. }
  4453. void ZoneServer::SendCastSpellPacket(LuaSpell* spell, Entity* caster){
  4454. EQ2Packet* outapp = 0;
  4455. PacketStruct* packet = 0;
  4456. Client* client = 0;
  4457. if(!caster || !spell || !spell->spell || spell->interrupted)
  4458. return;
  4459. vector<Client*>::iterator client_itr;
  4460. MClientList.readlock(__FUNCTION__, __LINE__);
  4461. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4462. client = *client_itr;
  4463. if(!client)
  4464. continue;
  4465. packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4466. if(packet){
  4467. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4468. if(!caster_id)
  4469. continue;
  4470. packet->setDataByName("spawn_id", caster_id);
  4471. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4472. for (int32 i = 0; i < spell->targets.size(); i++) {
  4473. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(spell->caster->GetZone()->GetSpawnByID(spell->targets[i]));
  4474. if(target_id) {
  4475. packet->setArrayDataByName("target", target_id, i);
  4476. }
  4477. else {
  4478. packet->setArrayDataByName("target", 0xFFFFFFFF, i);
  4479. }
  4480. }
  4481. packet->setDataByName("spell_visual", spell->spell->GetSpellData()->spell_visual); //result
  4482. packet->setDataByName("cast_time", spell->spell->GetSpellData()->cast_time*.01); //delay
  4483. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4484. packet->setDataByName("spell_level", 1);
  4485. packet->setDataByName("spell_tier", spell->spell->GetSpellData()->tier);
  4486. outapp = packet->serialize();
  4487. client->QueuePacket(outapp);
  4488. safe_delete(packet);
  4489. }
  4490. }
  4491. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4492. safe_delete(packet);
  4493. }
  4494. void ZoneServer::SendCastSpellPacket(int32 spell_visual, Spawn* target, Spawn* caster) {
  4495. if (target) {
  4496. vector<Client*>::iterator client_itr;
  4497. MClientList.readlock(__FUNCTION__, __LINE__);
  4498. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4499. Client* client = *client_itr;
  4500. if (!client)
  4501. continue;
  4502. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4503. if (packet) {
  4504. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4505. if(!target_id) // client is not aware of spawn
  4506. continue;
  4507. if (!caster) {
  4508. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  4509. }
  4510. else {
  4511. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4512. if(!caster_id) // client is not aware of spawn
  4513. continue;
  4514. packet->setDataByName("spawn_id", caster_id);
  4515. }
  4516. packet->setArrayLengthByName("num_targets", 1);
  4517. packet->setArrayDataByName("target", target_id);
  4518. packet->setDataByName("spell_visual", spell_visual);
  4519. packet->setDataByName("cast_time", 0);
  4520. packet->setDataByName("spell_id", 0);
  4521. packet->setDataByName("spell_level", 0);
  4522. packet->setDataByName("spell_tier", 1);
  4523. client->QueuePacket(packet->serialize());
  4524. safe_delete(packet);
  4525. }
  4526. }
  4527. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4528. }
  4529. }
  4530. void ZoneServer::SendCastEntityCommandPacket(EntityCommand* entity_command, int32 spawn_id, int32 target_id) {
  4531. if (entity_command) {
  4532. Spawn* spawn = GetSpawnByID(spawn_id);
  4533. Spawn* target = GetSpawnByID(target_id);
  4534. if (!spawn || !target)
  4535. return;
  4536. Client* client = 0;
  4537. vector<Client*>::iterator client_itr;
  4538. MClientList.readlock(__FUNCTION__, __LINE__);
  4539. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4540. client = *client_itr;
  4541. if (!client || !client->GetPlayer()->WasSentSpawn(spawn_id) || !client->GetPlayer()->WasSentSpawn(target_id))
  4542. continue;
  4543. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4544. if (packet) {
  4545. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  4546. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4547. if(!caster_id || !target_id)
  4548. continue;
  4549. packet->setDataByName("spawn_id", caster_id);
  4550. packet->setArrayLengthByName("num_targets", 1);
  4551. packet->setArrayDataByName("target", target_id);
  4552. packet->setDataByName("num_targets", 1);
  4553. packet->setDataByName("spell_visual", entity_command->spell_visual); //result
  4554. packet->setDataByName("cast_time", entity_command->cast_time * 0.01); //delay
  4555. packet->setDataByName("spell_id", 1);
  4556. packet->setDataByName("spell_level", 1);
  4557. packet->setDataByName("spell_tier", 1);
  4558. EQ2Packet* outapp = packet->serialize();
  4559. client->QueuePacket(outapp);
  4560. safe_delete(packet);
  4561. }
  4562. }
  4563. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4564. }
  4565. }
  4566. void ZoneServer::StartZoneInitialSpawnThread(Client* client){
  4567. if(zoneShuttingDown)
  4568. return;
  4569. #ifdef WIN32
  4570. _beginthread(SendInitialSpawns, 0, client);
  4571. #else
  4572. pthread_t thread;
  4573. pthread_create(&thread, NULL, SendInitialSpawns, client);
  4574. pthread_detach(thread);
  4575. #endif
  4576. }
  4577. void ZoneServer::SendZoneSpawns(Client* client){
  4578. int8 count = 0;
  4579. while (LoadingData && count <= 6000) { //sleep for max of 60 seconds (60000ms) while the maps are loading
  4580. count++;
  4581. Sleep(10);
  4582. }
  4583. count = 0;
  4584. int16 size = 0;
  4585. //give the spawn thread a tad bit of time to add the pending_spawns to spawn_list (up to 10 seconds)
  4586. while (count < 1000) {
  4587. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  4588. size = pending_spawn_list_add.size();
  4589. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  4590. if (size == 0)
  4591. break;
  4592. Sleep(10);
  4593. count++;
  4594. }
  4595. initial_spawn_threads_active++;
  4596. map<int32, Spawn*>::iterator itr;
  4597. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4598. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4599. Spawn* spawn = itr->second;
  4600. if (spawn) {
  4601. if(spawn == client->GetPlayer() && (client->IsReloadingZone() || client->GetPlayer()->IsReturningFromLD()))
  4602. {
  4603. if(!client->GetPlayer()->SetSpawnMap(spawn))
  4604. continue;
  4605. }
  4606. CheckSpawnRange(client, spawn, true);
  4607. }
  4608. }
  4609. CheckSendSpawnToClient(client, true);
  4610. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4611. client->SetConnected(true);
  4612. ClientPacketFunctions::SendFinishedEntitiesList(client);
  4613. initial_spawn_threads_active--;
  4614. }
  4615. vector<Entity*> ZoneServer::GetPlayers(){
  4616. vector<Entity*> ret;
  4617. Client* client = 0;
  4618. vector<Client*>::iterator client_itr;
  4619. MClientList.readlock(__FUNCTION__, __LINE__);
  4620. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4621. client = *client_itr;
  4622. ret.push_back(client->GetPlayer());
  4623. }
  4624. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4625. return ret;
  4626. }
  4627. int16 ZoneServer::SetSpawnTargetable(Spawn* spawn, float distance){
  4628. Spawn* test_spawn = 0;
  4629. int16 ret_val = 0;
  4630. map<int32, Spawn*>::iterator itr;
  4631. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4632. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4633. test_spawn = itr->second;
  4634. if(test_spawn){
  4635. if(test_spawn->GetDistance(spawn) <= distance){
  4636. test_spawn->SetTargetable(1);
  4637. ret_val++;
  4638. }
  4639. }
  4640. }
  4641. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4642. return ret_val;
  4643. }
  4644. int16 ZoneServer::SetSpawnTargetable(int32 spawn_id){
  4645. Spawn* spawn = 0;
  4646. int16 ret_val = 0;
  4647. map<int32, Spawn*>::iterator itr;
  4648. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4649. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4650. spawn = itr->second;
  4651. if(spawn){
  4652. if(spawn->GetDatabaseID() == spawn_id){
  4653. spawn->SetTargetable(1);
  4654. ret_val++;
  4655. }
  4656. }
  4657. }
  4658. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4659. return ret_val;
  4660. }
  4661. 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) {
  4662. ZoneInfoSlideStructInfo* info = new ZoneInfoSlideStructInfo();
  4663. memset(info, 0, sizeof(ZoneInfoSlideStructInfo));
  4664. info->unknown1[0] = unknown1a;
  4665. info->unknown1[1] = unknown1b;
  4666. info->unknown2[0] = unknown2a;
  4667. info->unknown2[1] = unknown2b;
  4668. info->unknown3 = unknown3;
  4669. info->unknown4 = unknown4;
  4670. int8 length = strlen(slide);
  4671. if (length >= 128)
  4672. length = 127;
  4673. strncpy(info->slide, slide, length);
  4674. length = strlen(voiceover);
  4675. if (length >= 128)
  4676. length = 127;
  4677. strncpy(info->voiceover, voiceover, length);
  4678. info->key1 = key1;
  4679. info->key2 = key2;
  4680. ZoneInfoSlideStruct* ret = new ZoneInfoSlideStruct();
  4681. ret->info = info;
  4682. return ret;
  4683. }
  4684. void ZoneServer::AddZoneInfoSlideStructTransitionInfo(ZoneInfoSlideStruct* info, int32 x, int32 y, float zoom, float transition_time) {
  4685. ZoneInfoSlideStructTransitionInfo* transition_info = new ZoneInfoSlideStructTransitionInfo();
  4686. transition_info->transition_x = x;
  4687. transition_info->transition_y = y;
  4688. transition_info->transition_zoom = zoom;
  4689. transition_info->transition_time = transition_time;
  4690. info->slide_transition_info.push_back(transition_info);
  4691. }
  4692. vector<ZoneInfoSlideStruct*>* ZoneServer::GenerateTutorialSlides() {
  4693. vector<ZoneInfoSlideStruct*>* slides = new vector<ZoneInfoSlideStruct*>();
  4694. 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);
  4695. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4696. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 16);
  4697. slides->push_back(slide);
  4698. 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);
  4699. AddZoneInfoSlideStructTransitionInfo(slide, 600, 365, 1.60000002384186, 0);
  4700. AddZoneInfoSlideStructTransitionInfo(slide, 800, 370, 1.39999997615814, 9);
  4701. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 8);
  4702. slides->push_back(slide);
  4703. 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);
  4704. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 0);
  4705. AddZoneInfoSlideStructTransitionInfo(slide, 750, 320, 1.60000002384186, 10);
  4706. AddZoneInfoSlideStructTransitionInfo(slide, 575, 265, 2.29999995231628, 10);
  4707. slides->push_back(slide);
  4708. 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);
  4709. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 2.5, 0);
  4710. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.70000004768372, 8);
  4711. AddZoneInfoSlideStructTransitionInfo(slide, 675, 390, 2.20000004768372, 11);
  4712. slides->push_back(slide);
  4713. 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);
  4714. AddZoneInfoSlideStructTransitionInfo(slide, 750, 500, 2.79999995231628, 0);
  4715. AddZoneInfoSlideStructTransitionInfo(slide, 720, 300, 2.5, 9);
  4716. AddZoneInfoSlideStructTransitionInfo(slide, 975, 270, 2.20000004768372, 9);
  4717. slides->push_back(slide);
  4718. 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);
  4719. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.89999997615814, 0);
  4720. AddZoneInfoSlideStructTransitionInfo(slide, 920, 475, 1, 24);
  4721. slides->push_back(slide);
  4722. 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);
  4723. AddZoneInfoSlideStructTransitionInfo(slide, 1400, 420, 2.40000009536743, 0);
  4724. AddZoneInfoSlideStructTransitionInfo(slide, 1200, 375, 1.70000004768372, 7);
  4725. AddZoneInfoSlideStructTransitionInfo(slide, 800, 225, 2.29999995231628, 7);
  4726. slides->push_back(slide);
  4727. 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);
  4728. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4729. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 27);
  4730. slides->push_back(slide);
  4731. 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);
  4732. AddZoneInfoSlideStructTransitionInfo(slide, 715, 305, 2.40000009536743, 0);
  4733. AddZoneInfoSlideStructTransitionInfo(slide, 730, 325, 1.79999995231628, 6);
  4734. AddZoneInfoSlideStructTransitionInfo(slide, 920, 395, 1.5, 5);
  4735. AddZoneInfoSlideStructTransitionInfo(slide, 1360, 330, 1.79999995231628, 9);
  4736. slides->push_back(slide);
  4737. 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);
  4738. AddZoneInfoSlideStructTransitionInfo(slide, 670, 675, 2.20000004768372, 0);
  4739. AddZoneInfoSlideStructTransitionInfo(slide, 710, 390, 1.79999995231628, 7);
  4740. AddZoneInfoSlideStructTransitionInfo(slide, 920, 415, 1.60000002384186, 5.5);
  4741. AddZoneInfoSlideStructTransitionInfo(slide, 1250, 675, 1.79999995231628, 8);
  4742. slides->push_back(slide);
  4743. 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);
  4744. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4745. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 19);
  4746. slides->push_back(slide);
  4747. 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);
  4748. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 0);
  4749. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 43);
  4750. slides->push_back(slide);
  4751. return slides;
  4752. }
  4753. EQ2Packet* ZoneServer::GetZoneInfoPacket(Client* client){
  4754. // this takes place when we get the LoginInfo for returning LD players
  4755. if(!client->GetPlayer()->IsReturningFromLD())
  4756. UpdateClientSpawnMap(client->GetPlayer(), client);
  4757. PacketStruct* packet = configReader.getStruct("WS_ZoneInfo", client->GetVersion());
  4758. packet->setSmallStringByName("server1",net.GetWorldName());
  4759. packet->setSmallStringByName("server2",net.GetWorldName());
  4760. packet->setDataByName("unknown1", 1, 1);//1, 1
  4761. int32 expansions = EXPANSION_UNKNOWN + EXPANSION_DOF + EXPANSION_KOS + EXPANSION_EOF + EXPANSION_ROK + EXPANSION_TSO + EXPANSION_DOV;
  4762. //packet->setDataByName("expansions_enabled", 82313211);//expansions 63181
  4763. //packet->setDataByName("expansions_enabled", 552075103);//expansions 63182
  4764. packet->setDataByName("expansions_enabled", 4294967295);//expansions 1096 //612499455 works
  4765. if (client->GetVersion() >= 1193) {
  4766. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4767. packet->setDataByName("unknown3", 4294967295, 1); //COE and up
  4768. packet->setDataByName("unknown3", 4294967295, 2);
  4769. }
  4770. else
  4771. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4772. packet->setSmallStringByName("auction_website", "eq2emulator.net");
  4773. packet->setDataByName("auction_port", 80);
  4774. packet->setSmallStringByName("upload_page", "test_upload.m");
  4775. packet->setSmallStringByName("upload_key", "dsya987yda9");
  4776. packet->setSmallStringByName("zone", GetZoneFile());
  4777. //packet->setSmallStringByName("zone2", GetZoneName());
  4778. //if ( strlen(GetZoneSkyFile()) > 0 )
  4779. // packet->setSmallStringByName("zone_unknown2", GetZoneSkyFile()); // used for the sky map
  4780. packet->setSmallStringByName("zone_desc", GetZoneDescription());
  4781. packet->setSmallStringByName("char_name", client->GetPlayer()->GetName());
  4782. packet->setDataByName("x", client->GetPlayer()->GetX());
  4783. packet->setDataByName("y", client->GetPlayer()->GetY());
  4784. packet->setDataByName("z", client->GetPlayer()->GetZ());
  4785. 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)
  4786. vector<ZoneInfoSlideStruct*>* slides = GenerateTutorialSlides();
  4787. if (slides) {
  4788. packet->setArrayLengthByName("num_slides", slides->size());
  4789. ZoneInfoSlideStruct* slide = 0;
  4790. for (int8 i = 0; i < slides->size(); i++) {
  4791. slide = slides->at(i);
  4792. packet->setArrayDataByName("unknown1", slide->info->unknown1[0], i, 0);
  4793. packet->setArrayDataByName("unknown1", slide->info->unknown1[1], i, 1);
  4794. packet->setArrayDataByName("unknown2", slide->info->unknown2[0], i, 0);
  4795. packet->setArrayDataByName("unknown2", slide->info->unknown2[1], i, 1);
  4796. packet->setArrayDataByName("unknown3", slide->info->unknown3, i);
  4797. packet->setArrayDataByName("unknown4", slide->info->unknown4, i);
  4798. packet->setArrayDataByName("slide", slide->info->slide, i);
  4799. packet->setArrayDataByName("voiceover", slide->info->voiceover, i);
  4800. packet->setArrayDataByName("key1", slide->info->key1, i);
  4801. packet->setArrayDataByName("key2", slide->info->key2, i);
  4802. packet->setSubArrayLengthByName("num_transitions", slide->slide_transition_info.size(), i);
  4803. for (int8 x = 0; x < slide->slide_transition_info.size(); x++) {
  4804. packet->setSubArrayDataByName("transition_x", slide->slide_transition_info[x]->transition_x, i, x);
  4805. packet->setSubArrayDataByName("transition_y", slide->slide_transition_info[x]->transition_y, i, x);
  4806. packet->setSubArrayDataByName("transition_zoom", slide->slide_transition_info[x]->transition_zoom, i, x);
  4807. packet->setSubArrayDataByName("transition_time", slide->slide_transition_info[x]->transition_time, i, x);
  4808. safe_delete(slide->slide_transition_info[x]);
  4809. }
  4810. safe_delete(slide->info);
  4811. safe_delete(slide);
  4812. }
  4813. }
  4814. safe_delete(slides);
  4815. }
  4816. if(rule_manager.GetGlobalRule(R_Zone, UseMapUnderworldCoords)->GetBool() && client->GetPlayer()->GetMap()) {
  4817. packet->setDataByName("underworld", client->GetPlayer()->GetMap()->GetMinY() + rule_manager.GetGlobalRule(R_Zone, MapUnderworldCoordOffset)->GetFloat());
  4818. }
  4819. else {
  4820. packet->setDataByName("underworld", underworld);
  4821. }
  4822. // unknown3 can prevent screen shots from being taken if
  4823. //packet->setDataByName("unknown3", 2094661567, 1); // Screenshots allowed with this value
  4824. //packet->setDataByName("unknown3", 3815767999, 1); // Screenshots disabled with this value
  4825. //packet->setDataByName("unknown3", 1, 2);
  4826. /*if (client->GetVersion() >= 63587) {
  4827. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4828. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4829. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4830. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4831. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4832. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4833. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4834. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4835. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4836. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4837. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4838. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4839. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4840. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4841. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4842. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4843. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4844. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4845. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4846. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4847. }
  4848. else if (client->GetVersion() >= 63214) {
  4849. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4850. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4851. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4852. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4853. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4854. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4855. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4856. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4857. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4858. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4859. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4860. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4861. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4862. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4863. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4864. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4865. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4866. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4867. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4868. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4869. }*/
  4870. if (client->GetVersion() >= 64644) {
  4871. packet->setDataByName("unknown3a", 12598924);
  4872. packet->setDataByName("unknown3b", 3992452959);
  4873. packet->setDataByName("unknown3c", 4294967183);
  4874. packet->setDataByName("unknown2a", 9);
  4875. packet->setDataByName("unknown2b", 9);
  4876. }
  4877. else if (client->GetVersion() >= 63181) {
  4878. packet->setDataByName("unknown3a", 750796556);//63182 73821356
  4879. packet->setDataByName("unknown3b", 3991404383);// 63182 3991404383
  4880. packet->setDataByName("unknown3c", 4278189967);// 63182 4278189967
  4881. packet->setDataByName("unknown2a", 8);// 63182
  4882. packet->setDataByName("unknown2b", 8);// 63182
  4883. }
  4884. else{
  4885. //packet->setDataByName("unknown3", 872447025,0);//63181
  4886. //packet->setDataByName("unknown3", 3085434875,1);// 63181
  4887. //packet->setDataByName("unknown3", 2147483633,2);// 63181
  4888. }
  4889. packet->setDataByName("year", world.GetWorldTimeStruct()->year);
  4890. packet->setDataByName("month", world.GetWorldTimeStruct()->month);
  4891. packet->setDataByName("day", world.GetWorldTimeStruct()->day);
  4892. packet->setDataByName("hour", world.GetWorldTimeStruct()->hour);
  4893. packet->setDataByName("minute", world.GetWorldTimeStruct()->minute);
  4894. packet->setDataByName("unknown", 0);
  4895. packet->setDataByName("unknown7", 1);
  4896. packet->setDataByName("unknown7", 1, 1);
  4897. packet->setDataByName("unknown9", 13);
  4898. //packet->setDataByName("unknown10", 25188959);4294967295
  4899. //packet->setDataByName("unknown10", 25190239);
  4900. packet->setDataByName("unknown10", 25191524);//25191524
  4901. packet->setDataByName("unknown10b", 1);
  4902. 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
  4903. packet->setDataByName("num_adv", 9);
  4904. packet->setArrayDataByName("adv_name", "adv02_dun_drowned_caverns", 0);
  4905. packet->setArrayDataByName("adv_id", 6, 0);
  4906. packet->setArrayDataByName("adv_name", "adv02_dun_sundered_splitpaw_hub", 1);
  4907. packet->setArrayDataByName("adv_id", 5, 1);
  4908. packet->setArrayDataByName("adv_name", "exp03_rgn_butcherblock", 2);
  4909. packet->setArrayDataByName("adv_id", 8, 2);
  4910. packet->setArrayDataByName("adv_name", "exp03_rgn_greater_faydark", 3);
  4911. packet->setArrayDataByName("adv_id", 7, 3);
  4912. packet->setArrayDataByName("adv_name", "mod01_dun_crypt_of_thaen", 4);
  4913. packet->setArrayDataByName("adv_id", 3, 4);
  4914. packet->setArrayDataByName("adv_name", "mod01_dun_tombs_of_night", 5);
  4915. packet->setArrayDataByName("adv_id", 4, 5);
  4916. packet->setArrayDataByName("adv_name", "nektulos_mini01", 6);
  4917. packet->setArrayDataByName("adv_id", 0, 6);
  4918. packet->setArrayDataByName("adv_name", "nektulos_mini02", 7);
  4919. packet->setArrayDataByName("adv_id", 1, 7);
  4920. packet->setArrayDataByName("adv_name", "nektulos_mini03", 8);
  4921. packet->setArrayDataByName("adv_id", 2, 8);
  4922. LogWrite(MISC__TODO, 0, "TODO", "Put cl_ client commands back in variables (not Rules) so they can be dynamically maintained");
  4923. vector<Variable*>* variables = world.GetClientVariables();
  4924. packet->setArrayLengthByName("num_client_setup", variables->size());
  4925. for(int i=variables->size()-1;i>=0;i--)
  4926. packet->setArrayDataByName("client_cmds", variables->at(i)->GetNameValuePair().c_str(), i);
  4927. // For AoM clients so item link work
  4928. if (client->GetVersion() >= 60114)
  4929. packet->setArrayDataByName("client_cmds", "chat_linkbrackets_item 1", variables->size());
  4930. safe_delete(variables);
  4931. //packet->setDataByName("unknown8", ); story?
  4932. // AA Tabs for 1193+ clients
  4933. if (client->GetVersion() >= 1193) {
  4934. packet->setArrayLengthByName("tab_count", 48);
  4935. int8 i = 0;
  4936. packet->setArrayDataByName("tab_index", i, i);
  4937. packet->setArrayDataByName("tab_name", ":es24a58bd8fcaac8c2:All", i);
  4938. i++;
  4939. packet->setArrayDataByName("tab_index", i, i);
  4940. packet->setArrayDataByName("tab_name", ":410385c727bd47a6:Racial Innate", i);
  4941. i++;
  4942. packet->setArrayDataByName("tab_index", i, i);
  4943. packet->setArrayDataByName("tab_name", ":410385c75a96e23c:Tradeskill Advancement", i);
  4944. i++;
  4945. packet->setArrayDataByName("tab_index", i, i);
  4946. packet->setArrayDataByName("tab_name", ":410385c744f1fd99:Focus Effects", i);
  4947. i++;
  4948. packet->setArrayDataByName("tab_index", i, i);
  4949. packet->setArrayDataByName("tab_name", ":410385c71edd2a66:Heroic", i);
  4950. i++;
  4951. packet->setArrayDataByName("tab_index", i, i);
  4952. packet->setArrayDataByName("tab_name", ":410385c76ee6239f:Shadows", i);
  4953. i++;
  4954. packet->setArrayDataByName("tab_index", i, i);
  4955. packet->setArrayDataByName("tab_name", ":410385c7e678b977:Prestige", i);
  4956. i++;
  4957. packet->setArrayDataByName("tab_index", i, i);
  4958. packet->setArrayDataByName("tab_name", ":410385c77ee422d7:Animalist", i);
  4959. i++;
  4960. packet->setArrayDataByName("tab_index", i, i);
  4961. packet->setArrayDataByName("tab_name", ":410385c7f165af77:Bard", i);
  4962. i++;
  4963. packet->setArrayDataByName("tab_index", i, i);
  4964. packet->setArrayDataByName("tab_name", ":410385c7421b9375:Brawler", i);
  4965. i++;
  4966. packet->setArrayDataByName("tab_index", i, i);
  4967. packet->setArrayDataByName("tab_name", ":410385c7a03ae7d1:Cleric", i);
  4968. i++;
  4969. packet->setArrayDataByName("tab_index", i, i);
  4970. packet->setArrayDataByName("tab_name", ":410385c7c9605e9f:Crusader", i);
  4971. i++;
  4972. packet->setArrayDataByName("tab_index", i, i);
  4973. packet->setArrayDataByName("tab_name", ":410385c7f9424168:Druid", i);
  4974. i++;
  4975. packet->setArrayDataByName("tab_index", i, i);
  4976. packet->setArrayDataByName("tab_name", ":410385c79cb9556c:Enchanter", i);
  4977. i++;
  4978. packet->setArrayDataByName("tab_index", i, i);
  4979. packet->setArrayDataByName("tab_name", ":410385c70c8b6aa4:Predator", i);
  4980. i++;
  4981. packet->setArrayDataByName("tab_index", i, i);
  4982. packet->setArrayDataByName("tab_name", ":410385c73a43b6dd:Rogue", i);
  4983. i++;
  4984. packet->setArrayDataByName("tab_index", i, i);
  4985. packet->setArrayDataByName("tab_name", ":410385c759fe7d15:Sorcerer", i);
  4986. i++;
  4987. packet->setArrayDataByName("tab_index", i, i);
  4988. packet->setArrayDataByName("tab_name", ":410385c7ad610aca:Summoner", i);
  4989. i++;
  4990. packet->setArrayDataByName("tab_index", i, i);
  4991. packet->setArrayDataByName("tab_name", ":410385c71e056728:Warrior", i);
  4992. i++;
  4993. packet->setArrayDataByName("tab_index", i, i);
  4994. packet->setArrayDataByName("tab_name", ":410385c7ba864c0b:Assassin", i);
  4995. i++;
  4996. packet->setArrayDataByName("tab_index", i, i);
  4997. packet->setArrayDataByName("tab_name", ":410385c7b8116aad:Beastlord", i);
  4998. i++;
  4999. packet->setArrayDataByName("tab_index", i, i);
  5000. packet->setArrayDataByName("tab_name", ":410385c7f53feb7b:Berserker", i);
  5001. i++;
  5002. packet->setArrayDataByName("tab_index", i, i);
  5003. packet->setArrayDataByName("tab_name", ":410385c73d8a70e2:Brigand", i);
  5004. i++;
  5005. packet->setArrayDataByName("tab_index", i, i);
  5006. packet->setArrayDataByName("tab_name", ":410385c770c766d6:Bruiser", i);
  5007. i++;
  5008. packet->setArrayDataByName("tab_index", i, i);
  5009. packet->setArrayDataByName("tab_name", ":410385c79226984b:Coercer", i);
  5010. i++;
  5011. packet->setArrayDataByName("tab_index", i, i);
  5012. packet->setArrayDataByName("tab_name", ":410385c70c58bb30:Conjurer", i);
  5013. i++;
  5014. packet->setArrayDataByName("tab_index", i, i);
  5015. packet->setArrayDataByName("tab_name", ":410385c73dfe68d0:Defiler", i);
  5016. i++;
  5017. packet->setArrayDataByName("tab_index", i, i);
  5018. packet->setArrayDataByName("tab_name", ":410385c792919a6b:Dirge", i);
  5019. i++;
  5020. packet->setArrayDataByName("tab_index", i, i);
  5021. packet->setArrayDataByName("tab_name", ":410385c7062e5f55:Fury", i);
  5022. i++;
  5023. packet->setArrayDataByName("tab_index", i, i);
  5024. packet->setArrayDataByName("tab_name", ":410385c762c1fdfc:Guardian", i);
  5025. i++;
  5026. packet->setArrayDataByName("tab_index", i, i);
  5027. packet->setArrayDataByName("tab_name", ":410385c78addfbf4:Illusionist", i);
  5028. i++;
  5029. packet->setArrayDataByName("tab_index", i, i);
  5030. packet->setArrayDataByName("tab_name", ":410385c7ece054a7:Inquisitor", i);
  5031. i++;
  5032. packet->setArrayDataByName("tab_index", i, i);
  5033. packet->setArrayDataByName("tab_name", ":410385c7d550d2e7:Monk", i);
  5034. i++;
  5035. packet->setArrayDataByName("tab_index", i, i);
  5036. packet->setArrayDataByName("tab_name", ":410385c743cfeaa2:Mystic", i);
  5037. i++;
  5038. packet->setArrayDataByName("tab_index", i, i);
  5039. packet->setArrayDataByName("tab_name", ":410385c7f63c9c8c:Necromancer", i);
  5040. i++;
  5041. packet->setArrayDataByName("tab_index", i, i);
  5042. packet->setArrayDataByName("tab_name", ":410385c70c5de0ae:Paladin", i);
  5043. i++;
  5044. packet->setArrayDataByName("tab_index", i, i);
  5045. packet->setArrayDataByName("tab_name", ":410385c79bc97b3a:Ranger", i);
  5046. i++;
  5047. packet->setArrayDataByName("tab_index", i, i);
  5048. packet->setArrayDataByName("tab_name", ":410385c78fbd2256:Shadowknight", i);
  5049. i++;
  5050. packet->setArrayDataByName("tab_index", i, i);
  5051. packet->setArrayDataByName("tab_name", ":410385c7781cc625:Shaman", i);
  5052. i++;
  5053. packet->setArrayDataByName("tab_index", i, i);
  5054. packet->setArrayDataByName("tab_name", ":410385c77eecdcdb:Swashbuckler", i);
  5055. i++;
  5056. packet->setArrayDataByName("tab_index", i, i);
  5057. packet->setArrayDataByName("tab_name", ":410385c7648d181e:Templar", i);
  5058. i++;
  5059. packet->setArrayDataByName("tab_index", i, i);
  5060. packet->setArrayDataByName("tab_name", ":410385c78df47d77:Troubador", i);
  5061. i++;
  5062. packet->setArrayDataByName("tab_index", i, i);
  5063. packet->setArrayDataByName("tab_name", ":410385c7c78ce0b8:Warden", i);
  5064. i++;
  5065. packet->setArrayDataByName("tab_index", i, i);
  5066. packet->setArrayDataByName("tab_name", ":410385c76290dcfa:Warlock", i);
  5067. i++;
  5068. packet->setArrayDataByName("tab_index", i, i);
  5069. packet->setArrayDataByName("tab_name", ":410385c7d1d52cf5:Wizard", i);
  5070. i++;
  5071. packet->setArrayDataByName("tab_index", i, i);
  5072. packet->setArrayDataByName("tab_name", ":410385c71c8f6f4d:Shaper", i);
  5073. i++;
  5074. packet->setArrayDataByName("tab_index", i, i);
  5075. packet->setArrayDataByName("tab_name", ":410385c72f6e354d:Channeler", i);
  5076. i++;
  5077. packet->setArrayDataByName("tab_index", i, i);
  5078. packet->setArrayDataByName("tab_name", ":410385c7df8bd37d:Dragon", i);
  5079. }
  5080. packet->setDataByName("unknown_mj", 1);//int8
  5081. packet->setDataByName("unknown_mj1", 335544320);//int32
  5082. packet->setDataByName("unknown_mj2", 4);//int32
  5083. packet->setDataByName("unknown_mj3", 3962504088);//int32
  5084. packet->setDataByName("unknown_mj4", 3985947216);//int32
  5085. packet->setDataByName("unknown_mj5", 1);//int32
  5086. packet->setDataByName("unknown_mj6", 386);//int32
  5087. packet->setDataByName("unknown_mj7", 4294967295);//int32
  5088. packet->setDataByName("unknown_mj8", 2716312211);//int32
  5089. packet->setDataByName("unknown_mj9", 1774338333);//int32
  5090. packet->setDataByName("unknown_mj10", 1);//int32
  5091. packet->setDataByName("unknown_mj11", 391);//int32
  5092. packet->setDataByName("unknown_mj12", 4294967295);//int32
  5093. packet->setDataByName("unknown_mj13", 3168965163);//int32
  5094. packet->setDataByName("unknown_mj14", 4117025286);//int32
  5095. packet->setDataByName("unknown_mj15", 1);//int32
  5096. packet->setDataByName("unknown_mj16", 394);//int32
  5097. packet->setDataByName("unknown_mj17", 4294967295);//int32
  5098. packet->setDataByName("unknown_mj18", 1790669110);//int32
  5099. packet->setDataByName("unknown_mj19", 107158108);//int32
  5100. packet->setDataByName("unknown_mj20", 1);//int32
  5101. packet->setDataByName("unknown_mj21", 393);//int32
  5102. packet->setDataByName("unknown_mj22", 4294967295);//int32
  5103. EQ2Packet* outapp = packet->serialize();
  5104. //packet->PrintPacket();
  5105. //DumpPacket(outapp);
  5106. safe_delete(packet);
  5107. return outapp;
  5108. }
  5109. void ZoneServer::SendUpdateDefaultCommand(Spawn* spawn, const char* command, float distance, Spawn* toPlayer){
  5110. if (spawn == nullptr || command == nullptr)
  5111. return;
  5112. if (toPlayer)
  5113. {
  5114. if (!toPlayer->IsPlayer())
  5115. return;
  5116. Client* client = GetClientBySpawn(toPlayer);
  5117. if (client)
  5118. {
  5119. client->SendDefaultCommand(spawn, command, distance);
  5120. }
  5121. // we don't override the primary command cause that would change ALL clients
  5122. return;
  5123. }
  5124. QueueDefaultCommand(spawn->GetID(), std::string(command), distance);
  5125. if (strlen(command)>0)
  5126. spawn->SetPrimaryCommand(command, command, distance);
  5127. }
  5128. void ZoneServer::CheckPlayerProximity(Spawn* spawn, Client* client){
  5129. if (player_proximities.size() < 1)
  5130. return;
  5131. if(player_proximities.count(spawn->GetID()) > 0){
  5132. PlayerProximity* prox = player_proximities.Get(spawn->GetID());
  5133. 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){
  5134. prox->clients_in_proximity[client] = true;
  5135. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->in_range_lua_function.c_str());
  5136. }
  5137. 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){
  5138. if(prox->leaving_range_lua_function.length() > 0)
  5139. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->leaving_range_lua_function.c_str());
  5140. prox->clients_in_proximity.erase(client);
  5141. }
  5142. }
  5143. }
  5144. void ZoneServer::AddPlayerProximity(Spawn* spawn, float distance, string in_range_function, string leaving_range_function){
  5145. RemovePlayerProximity(spawn);
  5146. PlayerProximity* prox = new PlayerProximity;
  5147. prox->distance = distance;
  5148. prox->in_range_lua_function = in_range_function;
  5149. prox->leaving_range_lua_function = leaving_range_function;
  5150. player_proximities.Put(spawn->GetID(), prox);
  5151. }
  5152. void ZoneServer::RemovePlayerProximity(Client* client){
  5153. PlayerProximity* prox = 0;
  5154. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5155. while(itr.Next()){
  5156. prox = itr->second;
  5157. if(prox->clients_in_proximity.count(client) > 0)
  5158. prox->clients_in_proximity.erase(client);
  5159. }
  5160. }
  5161. void ZoneServer::RemovePlayerProximity(Spawn* spawn, bool all){
  5162. if(all){
  5163. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5164. while(itr.Next()){
  5165. player_proximities.erase(itr->first, false, true, 10000);
  5166. }
  5167. }
  5168. else if(player_proximities.count(spawn->GetID()) > 0){
  5169. player_proximities.erase(spawn->GetID(), false, true, 10000);
  5170. }
  5171. }
  5172. void ZoneServer::AddLocationProximity(float x, float y, float z, float max_variation, string in_range_function, string leaving_range_function) {
  5173. LocationProximity* prox = new LocationProximity;
  5174. prox->x = x;
  5175. prox->y = y;
  5176. prox->z = z;
  5177. prox->max_variation = max_variation;
  5178. prox->in_range_lua_function = in_range_function;
  5179. prox->leaving_range_lua_function = leaving_range_function;
  5180. location_proximities.Add(prox);
  5181. }
  5182. void ZoneServer::CheckLocationProximity() {
  5183. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  5184. if (!zone_script)
  5185. return;
  5186. if (location_proximities.size() > 0 && connected_clients.size() > 0) {
  5187. Client* client = 0;
  5188. MutexList<Client*>::iterator iterator = connected_clients.begin();
  5189. while(iterator.Next()){
  5190. client = iterator->value;
  5191. if (client->IsConnected() && client->IsReadyForUpdates() && !client->IsZoning()) {
  5192. try {
  5193. MutexList<LocationProximity*>::iterator itr = location_proximities.begin();
  5194. LocationProximity* prox = 0;
  5195. while(itr.Next()){
  5196. prox = itr->value;
  5197. bool in_range = false;
  5198. float char_x = client->GetPlayer()->GetX();
  5199. float char_y = client->GetPlayer()->GetY();
  5200. float char_z = client->GetPlayer()->GetZ();
  5201. float x = prox->x;
  5202. float y = prox->y;
  5203. float z = prox->z;
  5204. float max_variation = prox->max_variation;
  5205. float total_diff = 0;
  5206. float diff = x - char_x; //Check X
  5207. if(diff < 0)
  5208. diff *= -1;
  5209. if(diff <= max_variation) {
  5210. total_diff += diff;
  5211. diff = z - char_z; //Check Z (we check Z first because it is far more likely to be a much greater variation than y)
  5212. if(diff < 0)
  5213. diff *= -1;
  5214. if(diff <= max_variation) {
  5215. total_diff += diff;
  5216. if(total_diff <= max_variation) { //Check Total
  5217. diff = y - char_y; //Check Y
  5218. if(diff < 0)
  5219. diff *= -1;
  5220. if(diff <= max_variation) {
  5221. total_diff += diff;
  5222. if(total_diff <= max_variation) {
  5223. in_range = true;
  5224. if(lua_interface && prox->in_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) == 0) { //Check Total
  5225. prox->clients_in_proximity[client] = true;
  5226. lua_interface->RunZoneScript(zone_script, prox->in_range_lua_function.c_str(), this, client->GetPlayer());
  5227. }
  5228. }
  5229. }
  5230. }
  5231. }
  5232. }
  5233. if (!in_range) {
  5234. if(lua_interface && prox->leaving_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) > 0) {
  5235. lua_interface->RunZoneScript(zone_script, prox->leaving_range_lua_function.c_str(), this, client->GetPlayer());
  5236. prox->clients_in_proximity.erase(client);
  5237. }
  5238. }
  5239. }
  5240. }
  5241. catch (...) {
  5242. LogWrite(ZONE__ERROR, 0, "Zone", "Except caught in ZoneServer::CheckLocationProximity");
  5243. return;
  5244. }
  5245. }
  5246. }
  5247. }
  5248. }
  5249. void ZoneServer::CheckLocationGrids() {
  5250. if (connected_clients.size() > 0 && location_grids.size() > 0) {
  5251. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5252. while (client_itr.Next()) {
  5253. Client* client = client_itr.value;
  5254. if (!client)
  5255. continue;
  5256. Player* player = client->GetPlayer();
  5257. float x = player->GetX();
  5258. float y = player->GetY();
  5259. float z = player->GetZ();
  5260. int32 grid_id = player->appearance.pos.grid_id;
  5261. MutexList<LocationGrid*>::iterator location_grid_itr = location_grids.begin();
  5262. while (location_grid_itr.Next()) {
  5263. LocationGrid* grid = location_grid_itr.value;
  5264. if (grid->locations.size() > 0 || /*(grid->grid_id == grid_id ||*/ grid->players.count(player) > 0) {
  5265. float x_small = 0;
  5266. float x_large = 0;
  5267. float y_small = 0;
  5268. float y_large = 0;
  5269. float z_small = 0;
  5270. float z_large = 0;
  5271. bool first = true;
  5272. bool in_grid = false;
  5273. MutexList<Location*>::iterator location_itr = grid->locations.begin();
  5274. while (location_itr.Next()) {
  5275. Location* location = location_itr.value;
  5276. if (first) {
  5277. x_small = location->x;
  5278. x_large = location->x;
  5279. if (grid->include_y) {
  5280. y_small = location->y;
  5281. y_large = location->y;
  5282. }
  5283. z_small = location->z;
  5284. z_large = location->z;
  5285. first = false;
  5286. }
  5287. else {
  5288. if (location->x < x_small)
  5289. x_small = location->x;
  5290. else if (location->x > x_large)
  5291. x_large = location->x;
  5292. if (grid->include_y) {
  5293. if (location->y < y_small)
  5294. y_small = location->y;
  5295. else if (location->y > y_large)
  5296. y_large = location->y;
  5297. }
  5298. if (location->z < z_small)
  5299. z_small = location->z;
  5300. else if (location->z > z_large)
  5301. z_large = location->z;
  5302. }
  5303. }
  5304. if (grid->include_y && (x >= x_small && x <= x_large && y >= y_small && y <= y_large && z >= z_small && z <= z_large))
  5305. in_grid = true;
  5306. else if (x >= x_small && x <= x_large && z >= z_small && z <= z_large)
  5307. in_grid = true;
  5308. if (in_grid && grid->players.count(player) == 0) {
  5309. grid->players.Put(player, true);
  5310. bool show_enter_location_popup = true;
  5311. bool discovery_enabled = rule_manager.GetGlobalRule(R_World, EnablePOIDiscovery)->GetBool();
  5312. if( grid->discovery && discovery_enabled && !player->DiscoveredLocation(grid->id) )
  5313. {
  5314. // check if player has already discovered this location
  5315. // if not, process new discovery
  5316. char tmp[200] = {0};
  5317. sprintf(tmp, "\\#FFE400You have discovered\12\\#FFF283%s", grid->name.c_str());
  5318. client->SendPopupMessage(11, tmp, "ui_discovery", 2.25, 0xFF, 0xFF, 0xFF);
  5319. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' discovered location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5320. player->UpdatePlayerHistory(HISTORY_TYPE_DISCOVERY, HISTORY_SUBTYPE_LOCATION, grid->id);
  5321. show_enter_location_popup = false;
  5322. // else, print standard location entry
  5323. }
  5324. if( show_enter_location_popup )
  5325. {
  5326. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' entering location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5327. client->SendPopupMessage(10, grid->name.c_str(), 0, 2.5, 255, 255, 0);
  5328. }
  5329. }
  5330. else if (!in_grid && grid->players.count(player) > 0) {
  5331. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' leaving location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5332. grid->players.erase(player);
  5333. }
  5334. }
  5335. }
  5336. }
  5337. }
  5338. }
  5339. // Called from a command (client, main zone thread) and the main zone thread
  5340. // so no need for a mutex container
  5341. void ZoneServer::AddLocationGrid(LocationGrid* grid) {
  5342. if (grid)
  5343. location_grids.Add(grid);
  5344. }
  5345. void ZoneServer::RemoveLocationGrids() {
  5346. MutexList<LocationGrid*>::iterator itr = location_grids.begin();
  5347. while (itr.Next())
  5348. itr.value->locations.clear(true);
  5349. location_grids.clear(true);
  5350. }
  5351. void ZoneServer::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast, bool call_expire_function, bool lock_spell_process){
  5352. if(spellProcess)
  5353. spellProcess->RemoveSpellTimersFromSpawn(spawn, remove_all, delete_recast, call_expire_function, lock_spell_process);
  5354. }
  5355. void ZoneServer::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement){
  5356. if(spellProcess)
  5357. spellProcess->Interrupted(caster, interruptor, error_code, cancel, from_movement);
  5358. }
  5359. Spell* ZoneServer::GetSpell(Entity* caster){
  5360. Spell* spell = 0;
  5361. if(spellProcess)
  5362. spell = spellProcess->GetSpell(caster);
  5363. return spell;
  5364. }
  5365. void ZoneServer::ProcessSpell(Spell* spell, Entity* caster, Spawn* target, bool lock, bool harvest_spell, LuaSpell* customSpell, int16 custom_cast_time, bool in_heroic_opp){
  5366. if(spellProcess)
  5367. spellProcess->ProcessSpell(this, spell, caster, target, lock, harvest_spell, customSpell, custom_cast_time, in_heroic_opp);
  5368. }
  5369. void ZoneServer::ProcessEntityCommand(EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock) {
  5370. if (target && target->GetSpawnScript()) {
  5371. Player* player = 0;
  5372. if (caster && caster->IsPlayer())
  5373. player = (Player*)caster;
  5374. CallSpawnScript(target, SPAWN_SCRIPT_CUSTOM, player, entity_command->command.c_str());
  5375. }
  5376. if (spellProcess)
  5377. spellProcess->ProcessEntityCommand(this, entity_command, caster, target, lock);
  5378. }
  5379. void ZoneServer::RemoveSpawnSupportFunctions(Spawn* spawn, bool lock_spell_process) {
  5380. if(!spawn)
  5381. return;
  5382. if(spawn->IsPlayer() && spawn->GetZone())
  5383. spawn->GetZone()->RemovePlayerPassenger(((Player*)spawn)->GetCharacterID());
  5384. if(spawn->IsEntity())
  5385. RemoveSpellTimersFromSpawn((Entity*)spawn, true, true, true, lock_spell_process);
  5386. RemoveDamagedSpawn(spawn);
  5387. spawn->SendSpawnChanges(false);
  5388. RemoveChangedSpawn(spawn);
  5389. // Everything inside this if will be nuked during a reload in other spots, no need to do it twice
  5390. if (!reloading) {
  5391. RemoveDeadEnemyList(spawn);
  5392. spawn->changed = true;
  5393. spawn->info_changed = true;
  5394. spawn->vis_changed = true;
  5395. spawn->position_changed = true;
  5396. SendSpawnChanges(spawn);
  5397. if (spawn->GetSpawnGroupID() > 0) {
  5398. int32 group_id = spawn->GetSpawnGroupID();
  5399. spawn->RemoveSpawnFromGroup();
  5400. if (spawn_group_map.count(group_id) > 0)
  5401. spawn_group_map.Get(group_id).Remove(spawn->GetID());
  5402. }
  5403. if (!spawn->IsPlayer()) {
  5404. if(quick_database_id_lookup.count(spawn->GetDatabaseID()) > 0)
  5405. quick_database_id_lookup.erase(spawn->GetDatabaseID());
  5406. if(spawn->GetSpawnLocationID() > 0 && quick_location_id_lookup.count(spawn->GetSpawnLocationID()) > 0 && quick_location_id_lookup.Get(spawn->GetSpawnLocationID()) == spawn->GetID())
  5407. quick_location_id_lookup.erase(spawn->GetSpawnLocationID());
  5408. if(spawn->GetSpawnGroupID() > 0 && quick_group_id_lookup.count(spawn->GetSpawnGroupID()) > 0 && quick_group_id_lookup.Get(spawn->GetSpawnGroupID()) == spawn->GetID())
  5409. quick_group_id_lookup.erase(spawn->GetSpawnGroupID());
  5410. }
  5411. DeleteSpawnScriptTimers(spawn);
  5412. RemovePlayerProximity(spawn);
  5413. }
  5414. // We don't use RemoveMovementNPC() here as it caused a hell of a delay during reloads
  5415. // instead we remove it from the list directly
  5416. if (spawn->IsNPC())
  5417. movement_spawns.erase(spawn->GetID());
  5418. }
  5419. void ZoneServer::HandleEmote(Client* originator, string name) {
  5420. if (!originator) {
  5421. LogWrite(ZONE__ERROR, 0, "Zone", "HandleEmote called with an invalid client");
  5422. return;
  5423. }
  5424. Client* client = 0;
  5425. Emote* origEmote = visual_states.FindEmote(name, originator->GetVersion());
  5426. if(!origEmote){
  5427. 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());
  5428. return;
  5429. }
  5430. Emote* emote = origEmote;
  5431. PacketStruct* packet = 0;
  5432. char* emoteResponse = 0;
  5433. vector<Client*>::iterator client_itr;
  5434. int32 cur_client_version = originator->GetVersion();
  5435. map<int32, Emote*> emote_version_range;
  5436. MClientList.readlock(__FUNCTION__, __LINE__);
  5437. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  5438. client = *client_itr;
  5439. if(!client || (client && client->GetPlayer()->IsIgnored(originator->GetPlayer()->GetName())))
  5440. continue;
  5441. // establish appropriate emote for the version used by the client
  5442. if (client->GetVersion() != originator->GetVersion())
  5443. {
  5444. map<int32, Emote*>::iterator rangeitr = emote_version_range.find(client->GetVersion());
  5445. if (rangeitr == emote_version_range.end())
  5446. {
  5447. Emote* tmp_new_emote = visual_states.FindEmote(name, client->GetVersion());
  5448. if (tmp_new_emote)
  5449. {
  5450. emote_version_range.insert(make_pair(client->GetVersion(), tmp_new_emote));
  5451. emote = tmp_new_emote;
  5452. } // else its missing just use the current clients default
  5453. }
  5454. else // we have an existing emote already cached
  5455. emote = rangeitr->second;
  5456. }
  5457. else // since the client and originator client match use the original emote
  5458. emote = origEmote;
  5459. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  5460. if(packet){
  5461. packet->setDataByName("spawn_id" , client->GetPlayer()->GetIDWithPlayerSpawn(originator->GetPlayer()));
  5462. if(!emoteResponse){
  5463. string message;
  5464. if(originator->GetPlayer()->GetTarget() && originator->GetPlayer()->GetTarget()->GetID() != originator->GetPlayer()->GetID()){
  5465. message = emote->GetTargetedMessageString();
  5466. if(message.find("%t") < 0xFFFFFFFF)
  5467. message.replace(message.find("%t"), 2, originator->GetPlayer()->GetTarget()->GetName());
  5468. }
  5469. if(message.length() == 0)
  5470. message = emote->GetMessageString();
  5471. if(message.find("%g1") < 0xFFFFFFFF){
  5472. if(originator->GetPlayer()->GetGender() == 1)
  5473. message.replace(message.find("%g1"), 3, "his");
  5474. else
  5475. message.replace(message.find("%g1"), 3, "her");
  5476. }
  5477. if(message.find("%g2") < 0xFFFFFFFF){
  5478. if(originator->GetPlayer()->GetGender() == 1)
  5479. message.replace(message.find("%g2"), 3, "him");
  5480. else
  5481. message.replace(message.find("%g2"), 3, "her");
  5482. }
  5483. if(message.find("%g3") < 0xFFFFFFFF){
  5484. if(originator->GetPlayer()->GetGender() == 1)
  5485. message.replace(message.find("%g3"), 3, "he");
  5486. else
  5487. message.replace(message.find("%g3"), 3, "she");
  5488. }
  5489. if(message.length() > 0){
  5490. emoteResponse = new char[message.length() + strlen(originator->GetPlayer()->GetName()) + 10];
  5491. sprintf(emoteResponse,"%s %s", originator->GetPlayer()->GetName(), message.c_str());
  5492. }
  5493. else{
  5494. originator->Message(CHANNEL_COLOR_YELLOW, "%s is not properly configured, be sure to submit a /bug report.", name.c_str());
  5495. safe_delete(packet);
  5496. break;
  5497. }
  5498. }
  5499. packet->setMediumStringByName("emote_msg", emoteResponse);
  5500. packet->setDataByName("anim_type", emote->GetVisualState());
  5501. client->QueuePacket(packet->serialize());
  5502. safe_delete(packet);
  5503. safe_delete_array(emoteResponse);
  5504. }
  5505. }
  5506. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5507. }
  5508. void ZoneServer::SetupInstance(int32 createdInstanceID) {
  5509. if ( createdInstanceID == 0 ) // if this happens that isn't good!
  5510. instanceID = ++MinInstanceID;
  5511. else // db should pass the good ID
  5512. instanceID = createdInstanceID;
  5513. }
  5514. void ZoneServer::RemoveDeadSpawn(Spawn* spawn){
  5515. AddDeadSpawn(spawn, 0);
  5516. }
  5517. void ZoneServer::AddDeadSpawn(Spawn* spawn, int32 timer){
  5518. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  5519. if (dead_spawns.count(spawn->GetID()) > 0)
  5520. dead_spawns[spawn->GetID()] = Timer::GetCurrentTime2() + timer;
  5521. else if(timer != 0xFFFFFFFF)
  5522. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + timer));
  5523. else{
  5524. if(spawn->IsEntity() && spawn->HasLoot()){
  5525. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + (15000 * spawn->GetLevel() + 240000)));
  5526. SendUpdateDefaultCommand(spawn, "loot", 10);
  5527. }
  5528. else
  5529. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + 10000));
  5530. }
  5531. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5532. }
  5533. void ZoneServer::WritePlayerStatistics() {
  5534. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5535. while(client_itr.Next())
  5536. client_itr->value->GetPlayer()->WritePlayerStatistics();
  5537. }
  5538. bool ZoneServer::SendRadiusSpawnInfo(Client* client, float radius) {
  5539. if (!client)
  5540. return false;
  5541. Spawn* spawn = 0;
  5542. bool ret = false;
  5543. map<int32, Spawn*>::iterator itr;
  5544. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5545. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5546. spawn = itr->second;
  5547. if (spawn && spawn != client->GetPlayer() && !spawn->IsPlayer() && spawn->GetDistance(client->GetPlayer()) <= radius) {
  5548. const char* type = "NPC";
  5549. const char* specialTypeID = "N/A";
  5550. int32 specialID = 0, spawnEntryID = spawn->GetSpawnEntryID();
  5551. if (spawn->IsObject())
  5552. {
  5553. Object* obj = (Object*)spawn;
  5554. specialID = obj->GetID();
  5555. specialTypeID = "GetID";
  5556. type = "Object";
  5557. }
  5558. else if (spawn->IsSign())
  5559. {
  5560. Sign* sign = (Sign*)spawn;
  5561. specialID = sign->GetWidgetID();
  5562. specialTypeID = "WidgetID";
  5563. type = "Sign";
  5564. }
  5565. else if (spawn->IsWidget())
  5566. {
  5567. Widget* widget = (Widget*)spawn;
  5568. specialID = widget->GetWidgetID();
  5569. specialTypeID = "WidgetID";
  5570. if ( specialID == 0xFFFFFFFF )
  5571. specialTypeID = "WidgetID(spawn_widgets entry missing)";
  5572. type = "Widget";
  5573. }
  5574. else if (spawn->IsGroundSpawn())
  5575. {
  5576. GroundSpawn* gs = (GroundSpawn*)spawn;
  5577. specialID = gs->GetGroundSpawnEntryID();
  5578. specialTypeID = "GroundSpawnEntryID";
  5579. type = "GroundSpawn";
  5580. }
  5581. client->Message(CHANNEL_COLOR_RED, "Name: %s (%s), Spawn Table ID: %u, %s: %u", spawn->GetName(), type, spawn->GetDatabaseID(), specialTypeID, specialID);
  5582. 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());
  5583. 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());
  5584. client->Message(CHANNEL_COLOR_YELLOW, "=============================");
  5585. ret = true;
  5586. }
  5587. }
  5588. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5589. return ret;
  5590. }
  5591. void ZoneServer::FindSpawn(Client* client, char* regSearchStr)
  5592. {
  5593. if (!regSearchStr || strlen(regSearchStr) < 1)
  5594. {
  5595. client->SimpleMessage(CHANNEL_COLOR_RED, "Bad ZoneServer::FindSpawn(Client*, char* regSearchStr) attempt, regSearchStr is NULL or empty.");
  5596. return;
  5597. }
  5598. string resString = string(regSearchStr);
  5599. try
  5600. {
  5601. std::regex pre_re_check("^[a-zA-Z0-9_ ]+$");
  5602. bool output = std::regex_match(resString, pre_re_check);
  5603. if (output)
  5604. {
  5605. string newStr(".*");
  5606. newStr.append(regSearchStr);
  5607. newStr.append(".*");
  5608. resString = newStr;
  5609. }
  5610. }
  5611. catch (...)
  5612. {
  5613. client->SimpleMessage(CHANNEL_COLOR_RED, "Try/Catch ZoneServer::FindSpawn(Client*, char* regSearchStr) failure.");
  5614. return;
  5615. }
  5616. std::regex re;
  5617. try {
  5618. re = std::regex(resString, std::regex_constants::icase);
  5619. }
  5620. catch(...) {
  5621. client->SimpleMessage(CHANNEL_COLOR_RED, "Invalid regex for FindSpawn.");
  5622. return;
  5623. }
  5624. client->Message(CHANNEL_NARRATIVE, "RegEx Search Spawn List: %s", regSearchStr);
  5625. client->Message(CHANNEL_NARRATIVE, "Database ID | Spawn Name | X , Y , Z");
  5626. client->Message(CHANNEL_NARRATIVE, "========================");
  5627. map<int32, Spawn*>::iterator itr;
  5628. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5629. int32 spawnsFound = 0;
  5630. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5631. Spawn* spawn = itr->second;
  5632. if (!spawn || !spawn->GetName())
  5633. continue;
  5634. bool output = false;
  5635. try {
  5636. output = std::regex_match(string(spawn->GetName()), re);
  5637. }
  5638. catch (...)
  5639. {
  5640. continue;
  5641. }
  5642. if (output)
  5643. {
  5644. client->Message(CHANNEL_NARRATIVE, "%i | %s | %f , %f , %f", spawn->GetDatabaseID(), spawn->GetName(), spawn->GetX(), spawn->GetY(), spawn->GetZ());
  5645. spawnsFound++;
  5646. }
  5647. }
  5648. client->Message(CHANNEL_NARRATIVE, "========================", spawnsFound);
  5649. client->Message(CHANNEL_NARRATIVE, "%u Results Found.", spawnsFound);
  5650. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5651. }
  5652. void ZoneServer::AddPlayerTracking(Player* player) {
  5653. if (player && !player->GetIsTracking() && players_tracking.count(player->GetDatabaseID()) == 0) {
  5654. Client* client = GetClientBySpawn(player);
  5655. if (client) {
  5656. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5657. if (packet) {
  5658. player->SetIsTracking(true);
  5659. players_tracking.Put(client->GetCharacterID(), player);
  5660. packet->setDataByName("mode", TRACKING_START);
  5661. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5662. client->QueuePacket(packet->serialize());
  5663. safe_delete(packet);
  5664. }
  5665. }
  5666. }
  5667. }
  5668. void ZoneServer::RemovePlayerTracking(Player* player, int8 mode) {
  5669. if (player && player->GetIsTracking()) {
  5670. Client* client = GetClientBySpawn(player);
  5671. if (client) {
  5672. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5673. if (packet) {
  5674. player->SetIsTracking(false);
  5675. players_tracking.erase(client->GetCharacterID());
  5676. packet->setDataByName("mode", mode);
  5677. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5678. client->QueuePacket(packet->serialize());
  5679. safe_delete(packet);
  5680. }
  5681. }
  5682. }
  5683. }
  5684. void ZoneServer::ProcessTracking() {
  5685. MutexMap<int32, Player*>::iterator itr = players_tracking.begin();
  5686. while (itr.Next())
  5687. ProcessTracking(GetClientBySpawn(itr->second));
  5688. }
  5689. void ZoneServer::ProcessTracking(Client* client) {
  5690. if (!client)
  5691. return;
  5692. Player* player = client->GetPlayer();
  5693. if (player && player->GetIsTracking()) {
  5694. MutexMap<int32, Spawn*>::iterator spawn_itr;
  5695. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5696. if (packet) {
  5697. packet->setDataByName("mode", TRACKING_UPDATE);
  5698. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5699. vector<TrackedSpawn*> spawns_tracked;
  5700. while (spawn_itr.Next()) {
  5701. Spawn* spawn = spawn_itr->second;
  5702. float distance = player->GetDistance(spawn);
  5703. if (spawn->IsEntity() && distance <= 80 && spawn != player) {
  5704. TrackedSpawn* ts = new TrackedSpawn;
  5705. ts->spawn = spawn;
  5706. ts->distance = distance;
  5707. /* Add spawns in ascending order from closest to furthest */
  5708. if (spawns_tracked.empty())
  5709. spawns_tracked.push_back(ts);
  5710. else {
  5711. vector<TrackedSpawn*>::iterator tracked_itr;
  5712. bool added = false;
  5713. for (tracked_itr = spawns_tracked.begin(); tracked_itr != spawns_tracked.end(); tracked_itr++) {
  5714. TrackedSpawn* cur_ts = *tracked_itr;
  5715. if (ts->distance <= cur_ts->distance) {
  5716. spawns_tracked.insert(tracked_itr, ts);
  5717. added = true;
  5718. break;
  5719. }
  5720. }
  5721. if (!added)
  5722. spawns_tracked.push_back(ts);
  5723. }
  5724. }
  5725. }
  5726. packet->setArrayLengthByName("num_spawns", spawns_tracked.size());
  5727. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5728. TrackedSpawn* ts = spawns_tracked[i];
  5729. LogWrite(ZONE__DEBUG, 0, "Zone", "%s (%f)", ts->spawn->GetName(), ts->distance);
  5730. packet->setArrayDataByName("spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5731. packet->setArrayDataByName("spawn_name", ts->spawn->GetName(), i);
  5732. if (ts->spawn->IsPlayer())
  5733. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_PC, i);
  5734. else
  5735. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_NPC, i);
  5736. packet->setArrayDataByName("spawn_con_color", player->GetArrowColor(ts->spawn->GetLevel()), i);
  5737. }
  5738. packet->setArrayLengthByName("num_array1", 0);
  5739. //for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5740. //}
  5741. packet->setArrayLengthByName("num_spawns2", spawns_tracked.size());
  5742. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5743. TrackedSpawn* ts = spawns_tracked[i];
  5744. packet->setArrayDataByName("list_spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5745. packet->setArrayDataByName("list_number", i, i);
  5746. }
  5747. client->QueuePacket(packet->serialize());
  5748. safe_delete(packet);
  5749. for (int32 i = 0; i < spawns_tracked.size(); i++)
  5750. safe_delete(spawns_tracked[i]);
  5751. }
  5752. }
  5753. }
  5754. void ZoneServer::SendEpicMobDeathToGuild(Player* killer, Spawn* victim) {
  5755. if (killer && victim) {
  5756. LogWrite(MISC__TODO, 1, "TODO" , "Check if player is in raid\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  5757. if (killer->GetGroupMemberInfo()) {
  5758. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  5759. deque<GroupMemberInfo*>::iterator itr;
  5760. PlayerGroup* group = world.GetGroupManager()->GetGroup(killer->GetGroupMemberInfo()->group_id);
  5761. if (group)
  5762. {
  5763. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  5764. deque<GroupMemberInfo*>* members = group->GetMembers();
  5765. for (itr = members->begin(); itr != members->end(); itr++) {
  5766. GroupMemberInfo* gmi = *itr;
  5767. if (gmi->client) {
  5768. Player* group_member = gmi->client->GetPlayer();
  5769. if (group_member && group_member->GetGuild()) {
  5770. Guild* guild = group_member->GetGuild();
  5771. string message = Guild::GetEpicMobDeathMessage(group_member->GetName(), victim->GetName());
  5772. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5773. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5774. }
  5775. }
  5776. }
  5777. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  5778. }
  5779. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  5780. }
  5781. else if (killer->GetGuild()) {
  5782. Guild* guild = killer->GetGuild();
  5783. string message = Guild::GetEpicMobDeathMessage(killer->GetName(), victim->GetName());
  5784. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5785. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5786. }
  5787. }
  5788. }
  5789. void ZoneServer::ProcessAggroChecks(Spawn* spawn) {
  5790. if (spawn->GetFactionID() < 1 || spawn->EngagedInCombat())
  5791. return;
  5792. // If faction based combat is not allowed then no need to run the loops so just return out
  5793. if(!rule_manager.GetGlobalRule(R_Faction, AllowFactionBasedCombat)->GetBool())
  5794. return;
  5795. if (spawn && spawn->IsNPC() && spawn->Alive())
  5796. CheckEnemyList((NPC*)spawn);
  5797. }
  5798. void ZoneServer::SendUpdateTitles(Client* client, Title* suffix, Title* prefix) {
  5799. assert(client);
  5800. if (client->GetVersion() > 546)
  5801. SendUpdateTitles(client->GetPlayer(), suffix, prefix);
  5802. }
  5803. void ZoneServer::SendUpdateTitles(Spawn *spawn, Title *suffix, Title *prefix) {
  5804. if (!spawn)
  5805. return;
  5806. vector<Client*>::iterator itr;
  5807. PacketStruct *packet;
  5808. Client* current_client;
  5809. MClientList.readlock(__FUNCTION__, __LINE__);
  5810. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5811. current_client = *itr;
  5812. if (current_client->GetVersion() <= 546)
  5813. continue;
  5814. if (!(packet = configReader.getStruct("WS_UpdateTitle", current_client->GetVersion())))
  5815. continue;
  5816. packet->setDataByName("player_id", current_client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  5817. packet->setDataByName("player_name", spawn->GetName());
  5818. packet->setDataByName("unknown1", 1, 1);
  5819. if(suffix)
  5820. packet->setDataByName("suffix_title", suffix->GetName());
  5821. else
  5822. packet->setDataByName("suffix_title", spawn->GetSuffixTitle());
  5823. if(prefix)
  5824. packet->setDataByName("prefix_title", prefix->GetName());
  5825. else
  5826. packet->setDataByName("prefix_title", spawn->GetPrefixTitle());
  5827. packet->setDataByName("last_name", spawn->GetLastName());
  5828. packet->setDataByName("sub_title", spawn->GetSubTitle());
  5829. current_client->QueuePacket(packet->serialize());
  5830. safe_delete(packet);
  5831. }
  5832. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5833. }
  5834. void ZoneServer::AddTransportSpawn(Spawn* spawn){
  5835. if(!spawn)
  5836. return;
  5837. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5838. transport_spawns.push_back(spawn->GetID());
  5839. spawn->SetTransportSpawn(true);
  5840. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5841. }
  5842. Spawn* ZoneServer::GetClosestTransportSpawn(float x, float y, float z){
  5843. Spawn* spawn = 0;
  5844. Spawn* closest_spawn = 0;
  5845. float closest_distance = 0.0;
  5846. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5847. vector<int32>::iterator itr = transport_spawns.begin();
  5848. while(itr != transport_spawns.end()){
  5849. spawn = GetSpawnByID(*itr);
  5850. if(spawn){
  5851. if(closest_distance == 0.0){
  5852. closest_spawn = spawn;
  5853. closest_distance = spawn->GetDistance(x, y, z);
  5854. }
  5855. else if(spawn->GetDistance(x, y, z) < closest_distance){
  5856. closest_spawn = spawn;
  5857. closest_distance = spawn->GetDistance(x, y, z);
  5858. }
  5859. itr++;
  5860. }
  5861. else
  5862. itr = transport_spawns.erase(itr);
  5863. }
  5864. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5865. return closest_spawn;
  5866. }
  5867. Spawn* ZoneServer::GetTransportByRailID(sint64 rail_id){
  5868. Spawn* spawn = 0;
  5869. Spawn* closest_spawn = 0;
  5870. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  5871. vector<int32>::iterator itr = transport_spawns.begin();
  5872. while(itr != transport_spawns.end()){
  5873. spawn = GetSpawnByID(*itr);
  5874. //printf("Rail id: %i vs %i\n", spawn ? spawn->GetRailID() : 0, rail_id);
  5875. if(spawn && spawn->GetRailID() == rail_id){
  5876. closest_spawn = spawn;
  5877. break;
  5878. }
  5879. itr++;
  5880. }
  5881. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  5882. return closest_spawn;
  5883. }
  5884. void ZoneServer::SetRain(float val) {
  5885. rain = val;
  5886. vector<Client*>::iterator itr;
  5887. MClientList.readlock(__FUNCTION__, __LINE__);
  5888. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5889. Client* client = *itr;
  5890. client->GetPlayer()->GetInfoStruct()->set_rain(val);
  5891. client->GetPlayer()->SetCharSheetChanged(true);
  5892. if( val >= 0.75 && !weather_signaled )
  5893. {
  5894. client->SimpleMessage(CHANNEL_NARRATIVE, "It starts to rain.");
  5895. }
  5896. else if( val < 0.75 && weather_signaled )
  5897. {
  5898. client->SimpleMessage(CHANNEL_NARRATIVE, "It stops raining.");
  5899. }
  5900. }
  5901. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5902. if (val >= 0.75 && !weather_signaled) {
  5903. weather_signaled = true;
  5904. ProcessSpawnConditional(SPAWN_CONDITIONAL_RAINING);
  5905. }
  5906. else if (val < 0.75 && weather_signaled) {
  5907. weather_signaled = false;
  5908. ProcessSpawnConditional(SPAWN_CONDITIONAL_NOT_RAINING);
  5909. }
  5910. }
  5911. void ZoneServer::SetWind(float val) {
  5912. vector<Client*>::iterator itr;
  5913. MClientList.readlock(__FUNCTION__, __LINE__);
  5914. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5915. Client* client = *itr;
  5916. client->GetPlayer()->GetInfoStruct()->set_wind(val);
  5917. client->GetPlayer()->SetCharSheetChanged(true);
  5918. }
  5919. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5920. }
  5921. void ZoneServer::ProcessWeather()
  5922. {
  5923. // if the global rule to disable weather is set, or if the `weather_allowed` field in the zone record == 0, do not process weather
  5924. if( !weather_enabled || !isWeatherAllowed() )
  5925. return;
  5926. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Processing weather changes", zone_name);
  5927. float new_weather = 0;
  5928. float weather_offset = 0;
  5929. bool change_weather = false;
  5930. // check to see if it is time to change the weather according to weather_frequency (time between changes)
  5931. if( weather_last_changed_time <= (Timer::GetUnixTimeStamp() - weather_frequency) )
  5932. {
  5933. LogWrite(ZONE__DEBUG, 2, "Zone", "%s: Checking for weather changes", zone_name);
  5934. // reset last changed time (frequency check)
  5935. weather_last_changed_time = Timer::GetUnixTimeStamp();
  5936. // this is the chance a weather change occurs at all at the expired interval
  5937. int8 weather_random = MakeRandomInt(1, 100);
  5938. 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");
  5939. if( weather_random <= weather_change_chance )
  5940. {
  5941. change_weather = true;
  5942. weather_offset = weather_change_amount;
  5943. if( weather_type == 3 ) // chaotic weather patterns, random weather between min/max
  5944. {
  5945. new_weather = MakeRandomFloat(weather_min_severity, weather_max_severity);
  5946. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Chaotic weather severity changed to %.2f", zone_name, new_weather);
  5947. weather_pattern = 2;
  5948. }
  5949. else if( weather_type == 2 ) // random weather patterns, combination of normal + dynamic + max_offset
  5950. {
  5951. weather_offset = MakeRandomFloat(weather_change_amount, weather_dynamic_offset);
  5952. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Random weather severity changed by %.2f", zone_name, weather_offset);
  5953. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  5954. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  5955. if( weather_random <= weather_alter )
  5956. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  5957. }
  5958. else if( weather_type == 1 ) // dynamic weather patterns, weather may not reach min/max
  5959. {
  5960. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  5961. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  5962. if( weather_random <= weather_alter )
  5963. {
  5964. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  5965. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Dynamic weather pattern changed to %i", zone_name, weather_pattern);
  5966. }
  5967. }
  5968. else // normal weather patterns, weather starts at min, goes to max, then back down again
  5969. {
  5970. // do nothing (processed below)
  5971. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Normal weather severity changed by %.2f", zone_name, weather_offset);
  5972. }
  5973. // when all done, change the weather
  5974. if( change_weather )
  5975. {
  5976. if( weather_pattern == 1 )
  5977. {
  5978. // weather is getting worse, til it reaches weather_max_severity
  5979. new_weather = ( weather_current_severity <= weather_max_severity ) ? weather_current_severity + weather_offset : weather_max_severity;
  5980. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Increased weather severity by %.2f", zone_name, weather_offset);
  5981. if(new_weather > weather_max_severity)
  5982. {
  5983. new_weather = weather_max_severity - weather_offset;
  5984. weather_pattern = 0;
  5985. }
  5986. }
  5987. else if( weather_pattern == 0 )
  5988. {
  5989. // weather is clearing up, til it reaches weather_min_severity
  5990. new_weather = ( weather_current_severity >= weather_min_severity ) ? weather_current_severity - weather_offset : weather_min_severity;
  5991. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Decreased weather severity by %.2f", zone_name, weather_offset);
  5992. if(new_weather < weather_min_severity)
  5993. {
  5994. new_weather = weather_min_severity + weather_offset;
  5995. weather_pattern = 1;
  5996. }
  5997. }
  5998. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather change triggered from %.2f to %.2f", zone_name, weather_current_severity, new_weather);
  5999. this->SetRain(new_weather);
  6000. weather_current_severity = new_weather;
  6001. }
  6002. }
  6003. }
  6004. else
  6005. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Not time to change weather yet", zone_name);
  6006. }
  6007. void ZoneServer::HidePrivateSpawn(Spawn* spawn) {
  6008. if (!spawn->IsPrivateSpawn())
  6009. return;
  6010. Client* client = 0;
  6011. Player* player = 0;
  6012. PacketStruct* packet = 0;
  6013. int32 packet_version = 0;
  6014. MutexList<Client*>::iterator itr = connected_clients.begin();
  6015. while (itr->Next()) {
  6016. client = itr->value;
  6017. player = client->GetPlayer();
  6018. if (player->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->IsSendingSpawn(spawn->GetID())) {
  6019. if (!packet || packet_version != client->GetVersion()) {
  6020. safe_delete(packet);
  6021. packet_version = client->GetVersion();
  6022. packet = configReader.getStruct("WS_DestroyGhostCmd", packet_version);
  6023. }
  6024. SendRemoveSpawn(client, spawn, packet);
  6025. if(spawn_range_map.count(client) > 0)
  6026. spawn_range_map.Get(client)->erase(spawn->GetID());
  6027. if(player->GetTarget() == spawn)
  6028. player->SetTarget(0);
  6029. }
  6030. }
  6031. safe_delete(packet);
  6032. }
  6033. SpawnLocation* ZoneServer::GetSpawnLocation(int32 id) {
  6034. SpawnLocation* ret = 0;
  6035. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  6036. if (spawn_location_list.count(id) > 0)
  6037. ret = spawn_location_list[id];
  6038. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  6039. return ret;
  6040. }
  6041. void ZoneServer::PlayAnimation(Spawn* spawn, int32 visual_state, Spawn* spawn2, int8 hide_type){
  6042. Client* client = 0;
  6043. PacketStruct* packet = 0;
  6044. Spawn* exclude_spawn = 0;
  6045. if (!spawn)
  6046. return;
  6047. if (spawn2){
  6048. if(hide_type == 1){
  6049. client = GetClientBySpawn(spawn2);
  6050. if(client){
  6051. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6052. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  6053. packet->setDataByName("anim_type", visual_state);
  6054. client->QueuePacket(packet->serialize());
  6055. }
  6056. safe_delete(packet);
  6057. return;
  6058. }
  6059. if(hide_type == 2)
  6060. exclude_spawn = spawn2;
  6061. }
  6062. vector<Client*>::iterator client_itr;
  6063. MClientList.readlock(__FUNCTION__, __LINE__);
  6064. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  6065. client = *client_itr;
  6066. if(spawn->GetDistance(client->GetPlayer()) > 50)
  6067. continue;
  6068. if(exclude_spawn == client->GetPlayer())
  6069. continue;
  6070. if(!client->IsReadyForUpdates()) // client is not in world yet so we shouldn't be sending animations of spawns yet
  6071. continue;
  6072. if(!packet || packet->GetVersion() != client->GetVersion()) {
  6073. safe_delete(packet);
  6074. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6075. }
  6076. if (packet) {
  6077. int32 spawn_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  6078. if(spawn_id) {
  6079. packet->setDataByName("spawn_id", spawn_id);
  6080. packet->setDataByName("anim_type", visual_state);
  6081. client->QueuePacket(packet->serialize());
  6082. }
  6083. }
  6084. }
  6085. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6086. safe_delete(packet);
  6087. }
  6088. vector<Spawn*> ZoneServer::GetSpawnsByID(int32 id) {
  6089. vector<Spawn*> tmp_list;
  6090. Spawn* spawn;
  6091. map<int32, Spawn*>::iterator itr;
  6092. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6093. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6094. spawn = itr->second;
  6095. if (spawn && (spawn->GetDatabaseID() == id))
  6096. tmp_list.push_back(spawn);
  6097. }
  6098. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6099. return tmp_list;
  6100. }
  6101. vector<Spawn*> ZoneServer::GetSpawnsByRailID(sint64 rail_id) {
  6102. vector<Spawn*> tmp_list;
  6103. Spawn* spawn;
  6104. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6105. vector<int32>::iterator itr = transport_spawns.begin();
  6106. while(itr != transport_spawns.end()){
  6107. spawn = GetSpawnByID(*itr);
  6108. if(spawn && spawn->GetRailID() == rail_id){
  6109. tmp_list.push_back(spawn);
  6110. }
  6111. itr++;
  6112. }
  6113. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6114. return tmp_list;
  6115. }
  6116. void ZoneServer::RemovePlayerPassenger(int32 char_id) {
  6117. vector<Spawn*> tmp_list;
  6118. Spawn* spawn;
  6119. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6120. vector<int32>::iterator itr = transport_spawns.begin();
  6121. while(itr != transport_spawns.end()){
  6122. spawn = GetSpawnByID(*itr);
  6123. if(spawn) {
  6124. spawn->RemoveRailPassenger(char_id);
  6125. }
  6126. itr++;
  6127. }
  6128. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6129. }
  6130. vector<int32> ZoneServer::GetAttackableSpawnsByDistance(Spawn* caster, float distance) {
  6131. vector<int32> ret;
  6132. Spawn* spawn = 0;
  6133. std::shared_lock lock(MGridMaps);
  6134. std::map<int32, GridMap*>::iterator grids = grid_maps.find(caster->GetLocation());
  6135. if(grids != grid_maps.end()) {
  6136. grids->second->MSpawns.lock_shared();
  6137. typedef map <int32, Spawn*> SpawnMapType;
  6138. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  6139. Spawn* spawn = it->second;
  6140. if (spawn && spawn->IsNPC() && spawn->appearance.attackable > 0 && spawn->GetID() > 0 && spawn->GetID() != caster->GetID() &&
  6141. spawn->Alive() && spawn->GetDistance(caster, true) <= distance) {
  6142. ret.push_back(spawn->GetID());
  6143. }
  6144. }
  6145. grids->second->MSpawns.unlock_shared();
  6146. }
  6147. return ret;
  6148. }
  6149. void ZoneServer::ResurrectSpawn(Spawn* spawn, Client* client) {
  6150. if(!client || !spawn)
  6151. return;
  6152. PendingResurrection* rez = client->GetCurrentRez();
  6153. if(!rez || !rez->caster)
  6154. return;
  6155. PacketStruct* packet = 0;
  6156. float power_perc = rez->mp_perc;
  6157. float health_perc = rez->hp_perc;
  6158. Spawn* caster_spawn = rez->caster;
  6159. sint32 heal_amt = 0;
  6160. sint32 power_amt = 0;
  6161. bool no_calcs = rez->no_calcs;
  6162. int8 crit_mod = rez->crit_mod;
  6163. Entity* caster = 0;
  6164. InfoStruct* info = 0;
  6165. bool crit = false;
  6166. string heal_spell = rez->heal_name;
  6167. int16 heal_packet_type = 0;
  6168. int16 power_packet_type = 0;
  6169. //Calculations for how much to heal the spawn
  6170. if(health_perc > 0)
  6171. heal_amt = (spawn->GetTotalHP() * (health_perc / 100));
  6172. if(power_perc > 0)
  6173. power_amt = (spawn->GetTotalPower() * (power_perc / 100));
  6174. if(caster_spawn->IsEntity()){
  6175. caster = ((Entity*)caster_spawn);
  6176. info = caster->GetInfoStruct();
  6177. }
  6178. if(!no_calcs && caster){
  6179. heal_amt = caster->CalculateHealAmount(spawn, heal_amt, crit_mod, &crit);
  6180. power_amt = caster->CalculateHealAmount(spawn, power_amt, crit_mod, &crit);
  6181. }
  6182. //Set this rez as a crit to be passed to subspell (not yet used)
  6183. rez->crit = true;
  6184. //Set Heal amt to 1 if 0 now so the player has health
  6185. if(heal_amt == 0)
  6186. heal_amt = 1;
  6187. if(heal_amt > spawn->GetTotalHP())
  6188. heal_amt = spawn->GetTotalHP();
  6189. if(power_amt > spawn->GetTotalPower())
  6190. power_amt = spawn->GetTotalPower();
  6191. spawn->SetHP(heal_amt);
  6192. if(power_amt > 0)
  6193. spawn->SetPower(power_amt);
  6194. if(client && caster){
  6195. EQ2Packet* move = ((Player*)spawn)->Move(caster->GetX(), caster->GetY(), caster->GetZ(), client->GetVersion());
  6196. if(move)
  6197. client->QueuePacket(move);
  6198. }
  6199. if(crit){
  6200. power_packet_type = HEAL_PACKET_TYPE_CRIT_MANA;
  6201. heal_packet_type = HEAL_PACKET_TYPE_CRIT_HEAL;
  6202. }
  6203. else {
  6204. power_packet_type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  6205. heal_packet_type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  6206. }
  6207. SendHealPacket(caster, spawn, heal_packet_type, heal_amt, heal_spell.c_str());
  6208. if(power_amt > 0)
  6209. SendHealPacket(caster, spawn, power_packet_type, power_amt, heal_spell.c_str());
  6210. //The following code sets the spawn as alive
  6211. if(dead_spawns.count(spawn->GetID()) > 0)
  6212. dead_spawns.erase(spawn->GetID());
  6213. if(spawn->IsPlayer()){
  6214. spawn->SetSpawnType(4);
  6215. client = GetClientBySpawn(spawn);
  6216. if(client){
  6217. packet = configReader.getStruct("WS_Resurrected", client->GetVersion());
  6218. if(packet){
  6219. client->QueuePacket(packet->serialize());
  6220. }
  6221. packet = configReader.getStruct("WS_ServerControlFlags", client->GetVersion());
  6222. if(packet)
  6223. {
  6224. packet->setDataByName("parameter1", 8);
  6225. client->QueuePacket(packet->serialize());
  6226. packet->setDataByName("parameter1", 16);
  6227. client->QueuePacket(packet->serialize());
  6228. }
  6229. safe_delete(packet);
  6230. client->SimpleMessage(CHANNEL_NARRATIVE, "You regain consciousness!");
  6231. }
  6232. }
  6233. spawn->SendSpawnChanges(true);
  6234. spawn->SetTempActionState(-1);
  6235. spawn->appearance.attackable = 1;
  6236. }
  6237. void ZoneServer::SendDispellPacket(Entity* caster, Spawn* target, string dispell_name, string spell_name, int8 dispell_type){
  6238. if(!caster || !target)
  6239. return;
  6240. Client* client = 0;
  6241. Player* player = 0;
  6242. PacketStruct* packet = 0;
  6243. vector<Client*>::iterator client_itr;
  6244. MClientList.readlock(__FUNCTION__, __LINE__);
  6245. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++){
  6246. client = *client_itr;
  6247. if(!client || !(player = client->GetPlayer()) || (player != caster && ((caster && player->WasSentSpawn(caster->GetID()) == false) || (target && player->WasSentSpawn(target->GetID()) == false))))
  6248. continue;
  6249. if(caster && caster->GetDistance(player) > 50)
  6250. continue;
  6251. if(target && target->GetDistance(player) > 50)
  6252. continue;
  6253. packet = configReader.getStruct("WS_HearDispell", client->GetVersion());
  6254. if(packet){
  6255. packet->setDataByName("spell_name", spell_name.c_str());
  6256. packet->setDataByName("dispell_name", dispell_name.c_str());
  6257. packet->setDataByName("caster", player->GetIDWithPlayerSpawn(caster));
  6258. packet->setDataByName("target", player->GetIDWithPlayerSpawn(target));
  6259. packet->setDataByName("type", dispell_type);
  6260. client->QueuePacket(packet->serialize());
  6261. }
  6262. safe_delete(packet);
  6263. }
  6264. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6265. }
  6266. void ZoneServer::DismissAllPets() {
  6267. Spawn* spawn = 0;
  6268. map<int32, Spawn*>::iterator itr;
  6269. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6270. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6271. spawn = itr->second;
  6272. if (spawn && spawn->IsEntity())
  6273. ((Entity*)spawn)->DismissAllPets();
  6274. }
  6275. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6276. }
  6277. void ZoneServer::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target, bool remove_caster){
  6278. if (spellProcess)
  6279. spellProcess->RemoveTargetFromSpell(spell, target, remove_caster);
  6280. }
  6281. void ZoneServer::ClearHate(Entity* entity) {
  6282. Spawn* spawn = 0;
  6283. map<int32, Spawn*>::iterator itr;
  6284. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6285. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6286. spawn = itr->second;
  6287. if (spawn && spawn->IsNPC() && ((NPC*)spawn)->Brain())
  6288. ((NPC*)spawn)->Brain()->ClearHate(entity);
  6289. }
  6290. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6291. }
  6292. ThreadReturnType ZoneLoop(void* tmp) {
  6293. #ifdef WIN32
  6294. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6295. #endif
  6296. if (tmp == 0) {
  6297. ThrowError("ZoneLoop(): tmp = 0!");
  6298. THREAD_RETURN(NULL);
  6299. }
  6300. ZoneServer* zs = (ZoneServer*) tmp;
  6301. while (zs->Process()) {
  6302. if(zs->GetClientCount() == 0)
  6303. Sleep(1000);
  6304. else
  6305. Sleep(10);
  6306. }
  6307. zs->Process(); //run loop once more to clean up some functions
  6308. safe_delete(zs);
  6309. THREAD_RETURN(NULL);
  6310. }
  6311. ThreadReturnType SpawnLoop(void* tmp) {
  6312. #ifdef WIN32
  6313. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6314. #endif
  6315. if (tmp == 0) {
  6316. ThrowError("SpawnLoop(): tmp = 0!");
  6317. THREAD_RETURN(NULL);
  6318. }
  6319. ZoneServer* zs = (ZoneServer*) tmp;
  6320. #ifndef NO_CATCH
  6321. try {
  6322. #endif
  6323. zs->spawnthread_active = true;
  6324. while (zs->SpawnProcess()) {
  6325. if(zs->GetClientCount() == 0)
  6326. Sleep(1000);
  6327. else
  6328. Sleep(20);
  6329. }
  6330. zs->spawnthread_active = false;
  6331. #ifndef NO_CATCH
  6332. }
  6333. catch(...) {
  6334. zs->spawnthread_active = false;
  6335. zs->initial_spawn_threads_active = 0;
  6336. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop, shutting down zone '%s'...", zs->GetZoneName());
  6337. try{
  6338. zs->Shutdown();
  6339. }
  6340. catch(...){
  6341. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop while shutting down zone '%s'...", zs->GetZoneName());
  6342. throw;
  6343. }
  6344. throw;
  6345. }
  6346. #endif
  6347. THREAD_RETURN(NULL);
  6348. }
  6349. ThreadReturnType SendInitialSpawns(void* tmp) {
  6350. #ifdef WIN32
  6351. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  6352. #endif
  6353. if (tmp == 0) {
  6354. ThrowError("SendInitialSpawns(): tmp = 0!");
  6355. THREAD_RETURN(NULL);
  6356. }
  6357. Client* client = (Client*) tmp;
  6358. client->GetCurrentZone()->SendZoneSpawns(client);
  6359. THREAD_RETURN(NULL);
  6360. }
  6361. ThreadReturnType SendLevelChangedSpawns(void* tmp) {
  6362. #ifdef WIN32
  6363. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6364. #endif
  6365. if (tmp == 0) {
  6366. ThrowError("SendLevelChangedSpawns(): tmp = 0!");
  6367. THREAD_RETURN(NULL);
  6368. }
  6369. Client* client = (Client*)tmp;
  6370. client->GetCurrentZone()->SendAllSpawnsForLevelChange(client);
  6371. THREAD_RETURN(NULL);
  6372. }
  6373. void ZoneServer::SetSpawnStructs(Client* client) {
  6374. int16 client_ver = client->GetVersion();
  6375. Player* player = client->GetPlayer();
  6376. //Save a copy of the correct spawn substructs for the client's player, save here a copy if we don't have one
  6377. PacketStruct* pos = configReader.getStruct("Substruct_SpawnPositionStruct", client_ver);
  6378. player->SetSpawnPosStruct(pos);
  6379. if (versioned_pos_structs.count(pos->GetVersion()) == 0)
  6380. versioned_pos_structs[pos->GetVersion()] = new PacketStruct(pos, true);
  6381. PacketStruct* vis = configReader.getStruct("Substruct_SpawnVisualizationInfoStruct", client_ver);
  6382. player->SetSpawnVisStruct(vis);
  6383. if (versioned_vis_structs.count(vis->GetVersion()) == 0)
  6384. versioned_vis_structs[vis->GetVersion()] = new PacketStruct(vis, true);
  6385. PacketStruct* info = configReader.getStruct("Substruct_SpawnInfoStruct", client_ver);
  6386. player->SetSpawnInfoStruct(info);
  6387. if (versioned_info_structs.count(info->GetVersion()) == 0)
  6388. versioned_info_structs[info->GetVersion()] = new PacketStruct(info, true);
  6389. PacketStruct* header = configReader.getStruct("WS_SpawnStruct_Header", client_ver);
  6390. player->SetSpawnHeaderStruct(header);
  6391. PacketStruct* footer = configReader.getStruct("WS_SpawnStruct_Footer", client_ver);
  6392. player->SetSpawnFooterStruct(footer);
  6393. PacketStruct* sfooter = configReader.getStruct("WS_SignWidgetSpawnStruct_Footer", client_ver);
  6394. player->SetSignFooterStruct(sfooter);
  6395. PacketStruct* wfooter = configReader.getStruct("WS_WidgetSpawnStruct_Footer", client_ver);
  6396. player->SetWidgetFooterStruct(wfooter);
  6397. }
  6398. Spawn* ZoneServer::GetSpawn(int32 id){
  6399. Spawn* ret = 0;
  6400. if(GetNPC(id))
  6401. ret = GetNewNPC(id);
  6402. else if(this->GetObject(id))
  6403. ret = GetNewObject(id);
  6404. else if(GetWidget(id))
  6405. ret = GetNewWidget(id);
  6406. else if(GetSign(id))
  6407. ret = GetNewSign(id);
  6408. else if(GetGroundSpawn(id))
  6409. ret = GetNewGroundSpawn(id);
  6410. // Unable to find the spawn in the list lets attempt to add it if we are not currently reloading
  6411. else if (!reloading && database.LoadNPC(this, id)) {
  6412. if (GetNPC(id))
  6413. ret = GetNewNPC(id);
  6414. else
  6415. LogWrite(NPC__ERROR, 0, "NPC", "Database inserted npc (%u) but was still unable to retrieve it!", id);
  6416. }
  6417. else if (!reloading && database.LoadObject(this, id)) {
  6418. if (this->GetObject(id))
  6419. ret = GetNewObject(id);
  6420. else
  6421. LogWrite(OBJECT__ERROR, 0, "Object", "Database inserted object (%u) but was still unable to retrieve it!", id);
  6422. }
  6423. else if (!reloading && database.LoadWidget(this, id)) {
  6424. if (GetWidget(id))
  6425. ret = GetNewWidget(id);
  6426. else
  6427. LogWrite(WIDGET__ERROR, 0, "Widget", "Database inserted widget (%u) but was still unable to retrieve it!", id);
  6428. }
  6429. else if (!reloading && database.LoadSign(this, id)) {
  6430. if (GetSign(id))
  6431. ret = GetNewSign(id);
  6432. else
  6433. LogWrite(SIGN__ERROR, 0, "Sign", "Database inserted sign (%u) but was still unable to retrieve it!", id);
  6434. }
  6435. else if (!reloading && database.LoadGroundSpawn(this, id)) {
  6436. if (GetGroundSpawn(id))
  6437. ret = GetNewGroundSpawn(id);
  6438. else
  6439. LogWrite(GROUNDSPAWN__ERROR, 0, "GSpawn", "Database inserted ground spawn (%u) but was still unable to retrieve it!", id);
  6440. }
  6441. if(ret && ret->IsOmittedByDBFlag())
  6442. {
  6443. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) was skipped due to a missing expansion / holiday flag being met.", id);
  6444. safe_delete(ret);
  6445. ret = 0;
  6446. }
  6447. if(ret)
  6448. ret->SetID(Spawn::NextID());
  6449. return ret;
  6450. }
  6451. vector<EntityCommand*>* ZoneServer::GetEntityCommandList(int32 id){
  6452. if(entity_command_list.count(id) > 0)
  6453. return entity_command_list[id];
  6454. else
  6455. return 0;
  6456. }
  6457. void ZoneServer::SetEntityCommandList(int32 id, EntityCommand* command) {
  6458. if (entity_command_list.count(id) == 0)
  6459. entity_command_list[id] = new vector<EntityCommand*>;
  6460. entity_command_list[id]->push_back(command);
  6461. }
  6462. EntityCommand* ZoneServer::GetEntityCommand(int32 id, string name) {
  6463. EntityCommand* ret = 0;
  6464. if (entity_command_list.count(id) == 0)
  6465. return ret;
  6466. vector<EntityCommand*>::iterator itr;
  6467. for (itr = entity_command_list[id]->begin(); itr != entity_command_list[id]->end(); itr++) {
  6468. if ((*itr)->name == name) {
  6469. ret = (*itr);
  6470. break;
  6471. }
  6472. }
  6473. return ret;
  6474. }
  6475. void ZoneServer::ClearEntityCommands() {
  6476. if (entity_command_list.size() > 0) {
  6477. map<int32, vector<EntityCommand*>* >::iterator itr;
  6478. for (itr = entity_command_list.begin(); itr != entity_command_list.end(); itr++) {
  6479. vector<EntityCommand*>* entity_commands = itr->second;
  6480. if (entity_commands && entity_commands->size() > 0) {
  6481. vector<EntityCommand*>::iterator v_itr;
  6482. for (v_itr = entity_commands->begin(); v_itr != entity_commands->end(); v_itr++)
  6483. safe_delete(*v_itr);
  6484. entity_commands->clear();
  6485. }
  6486. safe_delete(entity_commands);
  6487. }
  6488. entity_command_list.clear();
  6489. }
  6490. }
  6491. void ZoneServer::AddNPCSkill(int32 list_id, int32 skill_id, int16 value){
  6492. npc_skill_list[list_id][skill_id] = value;
  6493. }
  6494. map<string, Skill*>* ZoneServer::GetNPCSkills(int32 primary_list, int32 secondary_list){
  6495. map<string, Skill*>* ret = 0;
  6496. if(npc_skill_list.count(primary_list) > 0){
  6497. ret = new map<string, Skill*>();
  6498. map<int32, int16>::iterator itr;
  6499. Skill* tmpSkill = 0;
  6500. for(itr = npc_skill_list[primary_list].begin(); itr != npc_skill_list[primary_list].end(); itr++){
  6501. tmpSkill = master_skill_list.GetSkill(itr->first);
  6502. if(tmpSkill){
  6503. tmpSkill = new Skill(tmpSkill);
  6504. tmpSkill->current_val = itr->second;
  6505. tmpSkill->max_val = tmpSkill->current_val+5;
  6506. (*ret)[tmpSkill->name.data] = tmpSkill;
  6507. }
  6508. }
  6509. }
  6510. if(npc_skill_list.count(secondary_list) > 0){
  6511. if(!ret)
  6512. ret = new map<string, Skill*>();
  6513. map<int32, int16>::iterator itr;
  6514. Skill* tmpSkill = 0;
  6515. for(itr = npc_skill_list[secondary_list].begin(); itr != npc_skill_list[secondary_list].end(); itr++){
  6516. tmpSkill = master_skill_list.GetSkill(itr->first);
  6517. if(tmpSkill){
  6518. tmpSkill = new Skill(tmpSkill);
  6519. tmpSkill->current_val = itr->second;
  6520. tmpSkill->max_val = tmpSkill->current_val+5;
  6521. (*ret)[tmpSkill->name.data] = tmpSkill;
  6522. }
  6523. }
  6524. }
  6525. if(ret && ret->size() == 0){
  6526. safe_delete(ret);
  6527. ret = 0;
  6528. }
  6529. return ret;
  6530. }
  6531. void ZoneServer::AddNPCEquipment(int32 list_id, int32 item_id){
  6532. npc_equipment_list[list_id].push_back(item_id);
  6533. }
  6534. void ZoneServer::SetNPCEquipment(NPC* npc) {
  6535. if(npc_equipment_list.count(npc->GetEquipmentListID()) > 0){
  6536. Item* tmpItem = 0;
  6537. int8 slot = 0;
  6538. vector<int32>::iterator itr;
  6539. for(itr = npc_equipment_list[npc->GetEquipmentListID()].begin(); itr != npc_equipment_list[npc->GetEquipmentListID()].end(); itr++){
  6540. tmpItem = master_item_list.GetItem(*itr);
  6541. if(tmpItem){
  6542. slot = npc->GetEquipmentList()->GetFreeSlot(tmpItem);
  6543. if(slot < 255){
  6544. tmpItem = new Item(tmpItem);
  6545. npc->GetEquipmentList()->SetItem(slot, tmpItem);
  6546. }
  6547. }
  6548. }
  6549. }
  6550. }
  6551. void ZoneServer::AddNPC(int32 id, NPC* npc) {
  6552. npc_list[id] = npc;
  6553. }
  6554. void ZoneServer::AddWidget(int32 id, Widget* widget) {
  6555. widget_list[id] = widget;
  6556. }
  6557. Widget* ZoneServer::GetWidget(int32 id, bool override_loading) {
  6558. if((!reloading || override_loading) && widget_list.count(id) > 0)
  6559. return widget_list[id];
  6560. else
  6561. return 0;
  6562. }
  6563. Widget* ZoneServer::GetNewWidget(int32 id) {
  6564. if(!reloading && widget_list.count(id) > 0)
  6565. return widget_list[id]->Copy();
  6566. else
  6567. return 0;
  6568. }
  6569. void ZoneServer::LoadGroundSpawnEntries(){
  6570. MGroundSpawnItems.lock();
  6571. database.LoadGroundSpawnEntries(this);
  6572. MGroundSpawnItems.unlock();
  6573. }
  6574. void ZoneServer::LoadGroundSpawnItems() {
  6575. }
  6576. 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) {
  6577. GroundSpawnEntry* entry = new GroundSpawnEntry;
  6578. entry->min_skill_level = min_skill_level;
  6579. entry->min_adventure_level = min_adventure_level;
  6580. entry->bonus_table = bonus_table;
  6581. entry->harvest1 = harvest1;
  6582. entry->harvest3 = harvest3;
  6583. entry->harvest5 = harvest5;
  6584. entry->harvest_imbue = harvest_imbue;
  6585. entry->harvest_rare = harvest_rare;
  6586. entry->harvest10 = harvest10;
  6587. entry->harvest_coin = harvest_coin;
  6588. groundspawn_entries[groundspawn_id].push_back(entry);
  6589. }
  6590. void ZoneServer::AddGroundSpawnItem(int32 groundspawn_id, int32 item_id, int8 is_rare, int32 grid_id) {
  6591. GroundSpawnEntryItem* entry = new GroundSpawnEntryItem;
  6592. entry->item_id = item_id;
  6593. entry->is_rare = is_rare;
  6594. entry->grid_id = grid_id;
  6595. groundspawn_items[groundspawn_id].push_back(entry);
  6596. }
  6597. vector<GroundSpawnEntry*>* ZoneServer::GetGroundSpawnEntries(int32 id){
  6598. vector<GroundSpawnEntry*>* ret = 0;
  6599. MGroundSpawnItems.lock();
  6600. if(groundspawn_entries.count(id) > 0)
  6601. ret = &groundspawn_entries[id];
  6602. MGroundSpawnItems.unlock();
  6603. return ret;
  6604. }
  6605. vector<GroundSpawnEntryItem*>* ZoneServer::GetGroundSpawnEntryItems(int32 id){
  6606. vector<GroundSpawnEntryItem*>* ret = 0;
  6607. if(groundspawn_items.count(id) > 0)
  6608. ret = &groundspawn_items[id];
  6609. return ret;
  6610. }
  6611. // TODO - mis-named, should be DeleteGroundSpawnEntries() but this is ok for now :)
  6612. void ZoneServer::DeleteGroundSpawnItems()
  6613. {
  6614. MGroundSpawnItems.lock();
  6615. map<int32, vector<GroundSpawnEntry*> >::iterator groundspawnentry_map_itr;
  6616. vector<GroundSpawnEntry*>::iterator groundspawnentry_itr;
  6617. for(groundspawnentry_map_itr = groundspawn_entries.begin(); groundspawnentry_map_itr != groundspawn_entries.end(); groundspawnentry_map_itr++)
  6618. {
  6619. for(groundspawnentry_itr = groundspawnentry_map_itr->second.begin(); groundspawnentry_itr != groundspawnentry_map_itr->second.end(); groundspawnentry_itr++)
  6620. {
  6621. safe_delete(*groundspawnentry_itr);
  6622. }
  6623. }
  6624. groundspawn_entries.clear();
  6625. map<int32, vector<GroundSpawnEntryItem*> >::iterator groundspawnitem_map_itr;
  6626. vector<GroundSpawnEntryItem*>::iterator groundspawnitem_itr;
  6627. for(groundspawnitem_map_itr = groundspawn_items.begin(); groundspawnitem_map_itr != groundspawn_items.end(); groundspawnitem_map_itr++)
  6628. {
  6629. for(groundspawnitem_itr = groundspawnitem_map_itr->second.begin(); groundspawnitem_itr != groundspawnitem_map_itr->second.end(); groundspawnitem_itr++)
  6630. {
  6631. safe_delete(*groundspawnitem_itr);
  6632. }
  6633. }
  6634. groundspawn_items.clear();
  6635. MGroundSpawnItems.unlock();
  6636. }
  6637. void ZoneServer::AddGroundSpawn(int32 id, GroundSpawn* spawn) {
  6638. groundspawn_list[id] = spawn;
  6639. }
  6640. GroundSpawn* ZoneServer::GetGroundSpawn(int32 id, bool override_loading) {
  6641. if((!reloading || override_loading) && groundspawn_list.count(id) > 0)
  6642. return groundspawn_list[id];
  6643. else
  6644. return 0;
  6645. }
  6646. GroundSpawn* ZoneServer::GetNewGroundSpawn(int32 id) {
  6647. if(!reloading && groundspawn_list.count(id) > 0)
  6648. return groundspawn_list[id]->Copy();
  6649. else
  6650. return 0;
  6651. }
  6652. void ZoneServer::AddLootTable(int32 id, LootTable* table){
  6653. loot_tables[id] = table;
  6654. }
  6655. void ZoneServer::AddLootDrop(int32 id, LootDrop* drop){
  6656. loot_drops[id].push_back(drop);
  6657. }
  6658. void ZoneServer::AddSpawnLootList(int32 spawn_id, int32 id) {
  6659. spawn_loot_list[spawn_id].push_back(id);
  6660. }
  6661. void ZoneServer::ClearSpawnLootList(int32 spawn_id) {
  6662. spawn_loot_list[spawn_id].clear();
  6663. }
  6664. void ZoneServer::AddLevelLootList(GlobalLoot* loot) {
  6665. level_loot_list.push_back(loot);
  6666. }
  6667. void ZoneServer::AddRacialLootList(int16 racial_id, GlobalLoot* loot) {
  6668. racial_loot_list[racial_id].push_back(loot);
  6669. }
  6670. void ZoneServer::AddZoneLootList(int32 zone, GlobalLoot* loot) {
  6671. zone_loot_list[zone].push_back(loot);
  6672. }
  6673. void ZoneServer::ClearLootTables(){
  6674. map<int32,LootTable*>::iterator table_itr;
  6675. for(table_itr = loot_tables.begin(); table_itr != loot_tables.end(); table_itr++){
  6676. safe_delete(table_itr->second);
  6677. }
  6678. map<int32, vector<LootDrop*> >::iterator drop_itr;
  6679. vector<LootDrop*>::iterator drop_itr2;
  6680. for(drop_itr = loot_drops.begin(); drop_itr != loot_drops.end(); drop_itr++){
  6681. for(drop_itr2 = drop_itr->second.begin(); drop_itr2 != drop_itr->second.end(); drop_itr2++){
  6682. safe_delete(*drop_itr2);
  6683. }
  6684. }
  6685. vector<GlobalLoot*>::iterator level_itr;
  6686. for (level_itr = level_loot_list.begin(); level_itr != level_loot_list.end(); level_itr++) {
  6687. safe_delete(*level_itr);
  6688. }
  6689. map<int16, vector<GlobalLoot*> >::iterator race_itr;
  6690. vector<GlobalLoot*>::iterator race_itr2;
  6691. for (race_itr = racial_loot_list.begin(); race_itr != racial_loot_list.end(); race_itr++) {
  6692. for (race_itr2 = race_itr->second.begin(); race_itr2 != race_itr->second.end(); race_itr2++) {
  6693. safe_delete(*race_itr2);
  6694. }
  6695. }
  6696. map<int32, vector<GlobalLoot*> >::iterator zone_itr;
  6697. vector<GlobalLoot*>::iterator zone_itr2;
  6698. for(zone_itr = zone_loot_list.begin(); zone_itr != zone_loot_list.end(); zone_itr++) {
  6699. for (zone_itr2 = zone_itr->second.begin(); zone_itr2 != zone_itr->second.end(); zone_itr2++) {
  6700. safe_delete(*zone_itr2);
  6701. }
  6702. }
  6703. loot_tables.clear();
  6704. loot_drops.clear();
  6705. spawn_loot_list.clear();
  6706. level_loot_list.clear();
  6707. racial_loot_list.clear();
  6708. zone_loot_list.clear();
  6709. }
  6710. vector<int32> ZoneServer::GetSpawnLootList(int32 spawn_id, int32 zone_id, int8 spawn_level, int16 racial_id, Spawn* spawn) {
  6711. vector<int32> ret;
  6712. int32 returnValue = 0;
  6713. if(reloading)
  6714. return ret;
  6715. if (spawn_loot_list.count(spawn_id) > 0)
  6716. ret.insert(ret.end(), spawn_loot_list[spawn_id].begin(), spawn_loot_list[spawn_id].end());
  6717. if (level_loot_list.size() > 0) {
  6718. vector<GlobalLoot*>::iterator itr;
  6719. for (itr = level_loot_list.begin(); itr != level_loot_list.end(); itr++) {
  6720. GlobalLoot* loot = *itr;
  6721. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6722. returnValue = 0; // reset since this can override the database setting
  6723. if(zone_script)
  6724. {
  6725. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_level", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6726. continue;
  6727. }
  6728. bool entryAdded = false;
  6729. 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
  6730. ret.push_back(loot->table_id);
  6731. else {
  6732. 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
  6733. ret.push_back(loot->table_id);
  6734. }
  6735. if(!entryAdded && returnValue) // DB override via LUA scripting
  6736. ret.push_back(loot->table_id);
  6737. }
  6738. }
  6739. if (racial_loot_list.count(racial_id) > 0) {
  6740. vector<GlobalLoot*>::iterator itr;
  6741. for (itr = racial_loot_list[racial_id].begin(); itr != racial_loot_list[racial_id].end(); itr++) {
  6742. GlobalLoot* loot = *itr;
  6743. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6744. returnValue = 0; // reset since this can override the database setting
  6745. if(zone_script)
  6746. {
  6747. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_racial", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6748. continue;
  6749. }
  6750. bool entryAdded = false;
  6751. 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
  6752. ret.push_back(loot->table_id);
  6753. else {
  6754. 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
  6755. ret.push_back(loot->table_id);
  6756. }
  6757. if(!entryAdded && returnValue) // DB override via LUA scripting
  6758. ret.push_back(loot->table_id);
  6759. }
  6760. }
  6761. if (zone_loot_list.count(zone_id) > 0) {
  6762. vector<GlobalLoot*>::iterator itr;
  6763. for (itr = zone_loot_list[zone_id].begin(); itr != zone_loot_list[zone_id].end(); itr++) {
  6764. GlobalLoot* loot = *itr;
  6765. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6766. returnValue = 0; // reset since this can override the database setting
  6767. if(zone_script)
  6768. {
  6769. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_zone", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6770. continue;
  6771. }
  6772. bool entryAdded = false;
  6773. 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
  6774. ret.push_back(loot->table_id);
  6775. else {
  6776. 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
  6777. ret.push_back(loot->table_id);
  6778. }
  6779. if(!entryAdded && returnValue) // DB override via LUA scripting
  6780. ret.push_back(loot->table_id);
  6781. }
  6782. }
  6783. return ret;
  6784. }
  6785. vector<LootDrop*>* ZoneServer::GetLootDrops(int32 table_id){
  6786. if(!reloading && loot_drops.count(table_id) > 0)
  6787. return &(loot_drops[table_id]);
  6788. else
  6789. return 0;
  6790. }
  6791. LootTable* ZoneServer::GetLootTable(int32 table_id){
  6792. return loot_tables[table_id];
  6793. }
  6794. 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){
  6795. LocationTransportDestination* loc = new LocationTransportDestination;
  6796. loc->message = message;
  6797. loc->trigger_x = trigger_x;
  6798. loc->trigger_y = trigger_y;
  6799. loc->trigger_z = trigger_z;
  6800. loc->trigger_radius = trigger_radius;
  6801. loc->destination_zone_id = destination_zone_id;
  6802. loc->destination_x = destination_x;
  6803. loc->destination_y = destination_y;
  6804. loc->destination_z = destination_z;
  6805. loc->destination_heading = destination_heading;
  6806. loc->cost = cost;
  6807. loc->unique_id = unique_id;
  6808. MTransporters.lock();
  6809. if(location_transporters.count(zone_id) == 0)
  6810. location_transporters[zone_id] = new MutexList<LocationTransportDestination*>();
  6811. location_transporters[zone_id]->Add(loc);
  6812. MTransporters.unlock();
  6813. }
  6814. 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,
  6815. 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,
  6816. int32 max_client_version, int32 flight_path_id, int16 mount_id, int8 mount_red_color, int8 mount_green_color, int8 mount_blue_color){
  6817. TransportDestination* transport = new TransportDestination;
  6818. transport->type = type;
  6819. transport->display_name = name;
  6820. transport->message = message;
  6821. transport->destination_zone_id = destination_zone_id;
  6822. transport->destination_x = destination_x;
  6823. transport->destination_y = destination_y;
  6824. transport->destination_z = destination_z;
  6825. transport->destination_heading = destination_heading;
  6826. transport->cost = cost;
  6827. transport->unique_id = unique_id;
  6828. transport->min_level = min_level;
  6829. transport->max_level = max_level;
  6830. transport->req_quest = quest_req;
  6831. transport->req_quest_step = quest_step_req;
  6832. transport->req_quest_complete = quest_complete;
  6833. transport->map_x = map_x;
  6834. transport->map_y = map_y;
  6835. transport->expansion_flag = expansion_flag;
  6836. transport->holiday_flag = holiday_flag;
  6837. transport->min_client_version = min_client_version;
  6838. transport->max_client_version = max_client_version;
  6839. transport->flight_path_id = flight_path_id;
  6840. transport->mount_id = mount_id;
  6841. transport->mount_red_color = mount_red_color;
  6842. transport->mount_green_color = mount_green_color;
  6843. transport->mount_blue_color = mount_blue_color;
  6844. MTransporters.lock();
  6845. transporters[transport_id].push_back(transport);
  6846. MTransporters.unlock();
  6847. }
  6848. void ZoneServer::GetTransporters(vector<TransportDestination*>* returnList, Client* client, int32 transport_id){
  6849. if (!returnList)
  6850. return;
  6851. MTransporters.lock();
  6852. if (transporters.count(transport_id) > 0)
  6853. {
  6854. vector<TransportDestination*> list;
  6855. for (int i = 0; i < transporters[transport_id].size(); i++)
  6856. {
  6857. if (transporters[transport_id][i]->min_client_version && client->GetVersion() < transporters[transport_id][i]->min_client_version)
  6858. continue;
  6859. else if (transporters[transport_id][i]->max_client_version && client->GetVersion() > transporters[transport_id][i]->max_client_version)
  6860. continue;
  6861. if (database.CheckExpansionFlags(this, transporters[transport_id][i]->expansion_flag) && database.CheckHolidayFlags(this, transporters[transport_id][i]->holiday_flag))
  6862. {
  6863. returnList->push_back(transporters[transport_id][i]);
  6864. }
  6865. }
  6866. }
  6867. MTransporters.unlock();
  6868. }
  6869. MutexList<LocationTransportDestination*>* ZoneServer::GetLocationTransporters(int32 zone_id){
  6870. MutexList<LocationTransportDestination*>* ret = 0;
  6871. MTransporters.lock();
  6872. if(location_transporters.count(zone_id) > 0)
  6873. ret = location_transporters[zone_id];
  6874. MTransporters.unlock();
  6875. return ret;
  6876. }
  6877. void ZoneServer::DeleteGlobalTransporters(){
  6878. MTransporters.lock();
  6879. map<int32, vector<TransportDestination*> >::iterator itr;
  6880. vector<TransportDestination*>::iterator transport_vector_itr;
  6881. for(itr = transporters.begin(); itr != transporters.end(); itr++){
  6882. for(transport_vector_itr = itr->second.begin(); transport_vector_itr != itr->second.end(); transport_vector_itr++){
  6883. safe_delete(*transport_vector_itr);
  6884. }
  6885. }
  6886. map<int32, MutexList<LocationTransportDestination*>* >::iterator itr2;
  6887. for(itr2 = location_transporters.begin(); itr2 != location_transporters.end(); itr2++){
  6888. itr2->second->clear(true);
  6889. delete itr2->second;
  6890. }
  6891. transporters.clear();
  6892. location_transporters.clear();
  6893. MTransporters.unlock();
  6894. }
  6895. void ZoneServer::DeleteGlobalSpawns() {
  6896. ClearLootTables();
  6897. map<int32, NPC*>::iterator npc_list_iter;
  6898. for(npc_list_iter=npc_list.begin();npc_list_iter!=npc_list.end();npc_list_iter++) {
  6899. safe_delete(npc_list_iter->second);
  6900. }
  6901. npc_list.clear();
  6902. map<int32, Object*>::iterator object_list_iter;
  6903. for(object_list_iter=object_list.begin();object_list_iter!=object_list.end();object_list_iter++) {
  6904. safe_delete(object_list_iter->second);
  6905. }
  6906. object_list.clear();
  6907. map<int32, GroundSpawn*>::iterator groundspawn_list_iter;
  6908. for(groundspawn_list_iter=groundspawn_list.begin();groundspawn_list_iter!=groundspawn_list.end();groundspawn_list_iter++) {
  6909. safe_delete(groundspawn_list_iter->second);
  6910. }
  6911. groundspawn_list.clear();
  6912. map<int32, Widget*>::iterator widget_list_iter;
  6913. for(widget_list_iter=widget_list.begin();widget_list_iter!=widget_list.end();widget_list_iter++) {
  6914. safe_delete(widget_list_iter->second);
  6915. }
  6916. widget_list.clear();
  6917. map<int32, Sign*>::iterator sign_list_iter;
  6918. for(sign_list_iter=sign_list.begin();sign_list_iter!=sign_list.end();sign_list_iter++) {
  6919. safe_delete(sign_list_iter->second);
  6920. }
  6921. sign_list.clear();
  6922. /*map<int32, AppearanceData*>::iterator appearance_list_iter;
  6923. for(appearance_list_iter=npc_appearance_list.begin();appearance_list_iter!=npc_appearance_list.end();appearance_list_iter++) {
  6924. safe_delete(appearance_list_iter->second);
  6925. }
  6926. npc_appearance_list.clear();*/
  6927. ClearEntityCommands();
  6928. DeleteGroundSpawnItems();
  6929. DeleteGlobalTransporters();
  6930. DeleteTransporterMaps();
  6931. }
  6932. void ZoneServer::AddTransportMap(int32 id, string name) {
  6933. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  6934. m_transportMaps[id] = name;
  6935. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  6936. }
  6937. bool ZoneServer::TransportHasMap(int32 id) {
  6938. bool ret = false;
  6939. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  6940. ret = m_transportMaps.count(id) > 0;
  6941. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  6942. return ret;
  6943. }
  6944. string ZoneServer::GetTransportMap(int32 id) {
  6945. string ret;
  6946. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  6947. if (m_transportMaps.count(id) > 0)
  6948. ret = m_transportMaps[id];
  6949. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  6950. return ret;
  6951. }
  6952. void ZoneServer::DeleteTransporterMaps() {
  6953. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  6954. m_transportMaps.clear();
  6955. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  6956. }
  6957. void ZoneServer::ReloadSpawns() {
  6958. if (reloading)
  6959. return;
  6960. reloading = true;
  6961. world.SetReloadingSubsystem("Spawns");
  6962. // Let every one in the zone know what is happening
  6963. HandleBroadcast("Reloading all spawns for this zone.");
  6964. DeleteGlobalSpawns();
  6965. Depop(false, true);
  6966. }
  6967. void ZoneServer::SendStateCommand(Spawn* spawn, int32 state) {
  6968. vector<Client*>::iterator itr;
  6969. MClientList.readlock(__FUNCTION__, __LINE__);
  6970. for (itr = clients.begin(); itr != clients.end(); itr++) {
  6971. Client* client = *itr;
  6972. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  6973. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), state);
  6974. }
  6975. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6976. }
  6977. void ZoneServer::AddFlightPath(int32 id, FlightPathInfo* info) {
  6978. if (m_flightPaths.count(id) > 0) {
  6979. LogWrite(ZONE__ERROR, 0, "Zone", "Duplicate flight path (%u)", id);
  6980. safe_delete(info);
  6981. return;
  6982. }
  6983. m_flightPaths[id] = info;
  6984. }
  6985. void ZoneServer::AddFlightPathLocation(int32 id, FlightPathLocation* location) {
  6986. if (m_flightPaths.count(id) == 0) {
  6987. LogWrite(ZONE__ERROR, 0, "Zone", "There is no flight info for this route (%u)", id);
  6988. safe_delete(location);
  6989. return;
  6990. }
  6991. m_flightPathRoutes[id].push_back(location);
  6992. }
  6993. void ZoneServer::DeleteFlightPaths() {
  6994. map<int32, vector<FlightPathLocation*> >::iterator itr;
  6995. vector<FlightPathLocation*>::iterator itr2;
  6996. map<int32, FlightPathInfo*>::iterator itr3;
  6997. for (itr = m_flightPathRoutes.begin(); itr != m_flightPathRoutes.end(); itr++) {
  6998. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  6999. safe_delete(*itr2);
  7000. }
  7001. itr->second.clear();
  7002. }
  7003. m_flightPathRoutes.clear();
  7004. for (itr3 = m_flightPaths.begin(); itr3 != m_flightPaths.end(); itr3++) {
  7005. safe_delete(itr3->second);
  7006. }
  7007. m_flightPaths.clear();
  7008. }
  7009. void ZoneServer::SendFlightPathsPackets(Client* client) {
  7010. // Only send a packet if there are flight paths
  7011. if (m_flightPathRoutes.size() > 0) {
  7012. PacketStruct* packet = configReader.getStruct("WS_FlightPathsMsg", client->GetVersion());
  7013. if (packet) {
  7014. int32 num_routes = m_flightPaths.size();
  7015. packet->setArrayLengthByName("number_of_routes", num_routes);
  7016. packet->setArrayLengthByName("number_of_routes2", num_routes);
  7017. packet->setArrayLengthByName("number_of_routes3", num_routes);
  7018. packet->setArrayLengthByName("number_of_routes4", num_routes);
  7019. map<int32, FlightPathInfo*>::iterator itr;
  7020. int32 i = 0;
  7021. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, i++) {
  7022. packet->setArrayDataByName("route_length", m_flightPathRoutes[itr->first].size(), i);
  7023. packet->setArrayDataByName("ground_mount", itr->second->flying ? 0 : 1, i);
  7024. packet->setArrayDataByName("allow_dismount", itr->second->dismount ? 1 : 0, i);
  7025. packet->setSubArrayLengthByName("route_length2", m_flightPathRoutes[itr->first].size(), i);
  7026. vector<FlightPathLocation*>::iterator itr2;
  7027. int32 j = 0;
  7028. for (itr2 = m_flightPathRoutes[itr->first].begin(); itr2 != m_flightPathRoutes[itr->first].end(); itr2++, j++) {
  7029. packet->setSubArrayDataByName("x", (*itr2)->X, i, j);
  7030. packet->setSubArrayDataByName("y", (*itr2)->Y, i, j);
  7031. packet->setSubArrayDataByName("z", (*itr2)->Z, i, j);
  7032. }
  7033. }
  7034. client->QueuePacket(packet->serialize());
  7035. safe_delete(packet);
  7036. }
  7037. }
  7038. }
  7039. int32 ZoneServer::GetFlightPathIndex(int32 id) {
  7040. int32 index = 0;
  7041. map<int32, FlightPathInfo*>::iterator itr;
  7042. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, index++) {
  7043. if (itr->first == id)
  7044. return index;
  7045. }
  7046. return -1;
  7047. }
  7048. float ZoneServer::GetFlightPathSpeed(int32 id) {
  7049. float speed = 1;
  7050. if (m_flightPaths.count(id) > 0)
  7051. speed = m_flightPaths[id]->speed;
  7052. return speed;
  7053. }
  7054. void ZoneServer::ProcessSpawnConditional(int8 condition) {
  7055. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  7056. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7057. map<int32, Spawn*>::iterator itr;
  7058. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7059. if (itr->second != NULL) // null itr->second still coming into ProcessSpawnConditional
  7060. {
  7061. SpawnLocation* loc = spawn_location_list[itr->second->GetSpawnLocationID()];
  7062. if (loc && loc->conditional > 0) {
  7063. if ((loc->conditional & condition) != condition) {
  7064. Despawn(itr->second, 0);
  7065. }
  7066. }
  7067. }
  7068. }
  7069. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7070. map<int32, SpawnLocation*>::iterator itr2;
  7071. for (itr2 = spawn_location_list.begin(); itr2 != spawn_location_list.end(); itr2++) {
  7072. SpawnLocation* loc = itr2->second;
  7073. if (loc && loc->conditional > 0 && ((loc->conditional & condition) == condition))
  7074. if (GetSpawnByLocationID(loc->placement_id) == NULL)
  7075. ProcessSpawnLocation(loc);
  7076. }
  7077. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  7078. }
  7079. void ZoneServer::AddSpawnProximities(Spawn* newSpawn) {
  7080. Spawn* spawn = 0;
  7081. map<int32, Spawn*>::iterator itr;
  7082. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7083. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  7084. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7085. spawn = itr->second;
  7086. if (spawn && spawn != newSpawn) {
  7087. if (newSpawn->GetDatabaseID())
  7088. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7089. if (newSpawn->GetSpawnLocationID())
  7090. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7091. if (spawn->GetDatabaseID())
  7092. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7093. if (spawn->GetSpawnLocationID())
  7094. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7095. }
  7096. }
  7097. list<Spawn*>::iterator itr2;
  7098. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7099. spawn = *itr2;
  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. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  7112. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7113. }
  7114. // we only call this inside a write lock
  7115. void ZoneServer::RemoveSpawnProximities(Spawn* oldSpawn) {
  7116. Spawn* spawn = 0;
  7117. map<int32, Spawn*>::iterator itr;
  7118. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7119. spawn = itr->second;
  7120. if (spawn && spawn != oldSpawn) {
  7121. if (oldSpawn->GetDatabaseID())
  7122. spawn->RemoveSpawnFromProximity(oldSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7123. if (oldSpawn->GetSpawnLocationID())
  7124. spawn->RemoveSpawnFromProximity(oldSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7125. // don't need to remove oldSpawn proximities, we clear them all out
  7126. }
  7127. }
  7128. }
  7129. void ZoneServer::SetSpawnScript(SpawnEntry* entry, Spawn* spawn)
  7130. {
  7131. if (!entry || !spawn)
  7132. return;
  7133. const char* script = 0;
  7134. for (int x = 0; x < 3; x++)
  7135. {
  7136. switch (x)
  7137. {
  7138. case 0:
  7139. script = world.GetSpawnEntryScript(entry->spawn_entry_id);
  7140. break;
  7141. case 1:
  7142. script = world.GetSpawnLocationScript(entry->spawn_location_id);
  7143. break;
  7144. case 2:
  7145. script = world.GetSpawnScript(entry->spawn_id);
  7146. break;
  7147. }
  7148. if (script && lua_interface && lua_interface->GetSpawnScript(script) != 0)
  7149. {
  7150. spawn->SetSpawnScript(string(script));
  7151. break;
  7152. }
  7153. }
  7154. }
  7155. vector<HouseItem> ZoneServer::GetHouseItems(Client* client)
  7156. {
  7157. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7158. return std::vector<HouseItem>();
  7159. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7160. std::vector<HouseItem> items;
  7161. map<int32, Spawn*>::iterator itr;
  7162. Spawn* spawn = 0;
  7163. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7164. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7165. spawn = itr->second;
  7166. if (spawn && spawn->IsObject() && spawn->GetPickupItemID())
  7167. {
  7168. HouseItem tmpItem;
  7169. tmpItem.item_id = spawn->GetPickupItemID();
  7170. tmpItem.unique_id = spawn->GetPickupUniqueItemID();
  7171. tmpItem.spawn_id = spawn->GetID();
  7172. tmpItem.item = master_item_list.GetItem(spawn->GetPickupItemID());
  7173. if (!tmpItem.item)
  7174. continue;
  7175. items.push_back(tmpItem);
  7176. }
  7177. }
  7178. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7179. return items;
  7180. }
  7181. void ZoneServer::SendHouseItems(Client* client)
  7182. {
  7183. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7184. return;
  7185. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7186. std::vector<HouseItem> items = GetHouseItems(client);
  7187. // setting this to 1 puts it on the door widget
  7188. packet->setDataByName("is_widget_door", 1);
  7189. packet->setArrayLengthByName("num_items", items.size());
  7190. for (int i = 0; i < items.size(); i++)
  7191. {
  7192. HouseItem tmpItem = items[i];
  7193. packet->setArrayDataByName("unique_id", tmpItem.unique_id, i); // unique_id is in fact the item_id...
  7194. packet->setArrayDataByName("item_name", tmpItem.item->name.c_str(), i);
  7195. packet->setArrayDataByName("status_reduction", tmpItem.item->houseitem_info->status_rent_reduction, i);
  7196. // location, 0 = floor, 1 = ceiling
  7197. //packet->setArrayDataByName("location", 1, i, 0);
  7198. // item_state int8
  7199. // 0 = normal (cannot pick up item / move item / toggle visibility)
  7200. // 1 = virtual (toggle visibility available, no move item)
  7201. // 2 = hidden (cannot pick up item / move item / toggle visibility)
  7202. // 3 = virtual/hidden/toggle visibility
  7203. // 4 = none (cannot pick up item / move item / toggle visibility)
  7204. // 5 = none, toggle visibility (cannot pick up item / move item)
  7205. // 8 = none (cannot pick up item / move item / toggle visibility)
  7206. //packet->setArrayDataByName("item_state", tmpvalue, i, 0);
  7207. // makes it so we don't have access to move item/retrieve item
  7208. // cannot use in conjunction with ui_tab_flag1/ui_tab_flag2
  7209. //packet->setArrayDataByName("tradeable", 1, i);
  7210. //packet->setArrayDataByName("item_description", "failboat", i);
  7211. // access to move item/retrieve item, do not use in conjunction with tradeable
  7212. packet->setArrayDataByName("ui_tab_flag1", 1, i, 0);
  7213. packet->setArrayDataByName("ui_tab_flag2", 1, i, 0);
  7214. // both of these can serve as description fields (only one should be used they populate the same area below the item name)
  7215. //packet->setArrayDataByName("first_item_description", "test", i);
  7216. //packet->setArrayDataByName("second_item_description", "Description here!", i);
  7217. packet->setArrayDataByName("icon", tmpItem.item->details.icon, i);
  7218. }
  7219. EQ2Packet* pack = packet->serialize();
  7220. client->QueuePacket(pack);
  7221. safe_delete(packet);
  7222. }
  7223. Spawn* ZoneServer::GetSpawnFromUniqueItemID(int32 unique_id)
  7224. {
  7225. if (!GetInstanceID() || GetInstanceType() != Instance_Type::PERSONAL_HOUSE_INSTANCE)
  7226. return nullptr;
  7227. map<int32, Spawn*>::iterator itr;
  7228. Spawn* spawn = 0;
  7229. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7230. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7231. spawn = itr->second;
  7232. if (spawn && spawn->IsObject() && spawn->GetPickupUniqueItemID() == unique_id)
  7233. {
  7234. Spawn* tmpSpawn = spawn;
  7235. MSpawnList.releasereadlock();
  7236. return tmpSpawn;
  7237. }
  7238. }
  7239. MSpawnList.releasereadlock();
  7240. return nullptr;
  7241. }
  7242. void ZoneServer::AddPendingSpawnRemove(int32 id)
  7243. {
  7244. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7245. m_pendingSpawnRemove.insert(make_pair(id,true));
  7246. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7247. }
  7248. void ZoneServer::ProcessSpawnRemovals()
  7249. {
  7250. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7251. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7252. if (m_pendingSpawnRemove.size() > 0) {
  7253. map<int32,bool>::iterator itr2;
  7254. for (itr2 = m_pendingSpawnRemove.begin(); itr2 != m_pendingSpawnRemove.end(); itr2++) {
  7255. spawn_list.erase(itr2->first);
  7256. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].erase(itr2->first);
  7257. std::map<int32,int32>::iterator hsmitr = housing_spawn_map.find(itr2->first);
  7258. if(hsmitr != housing_spawn_map.end()) {
  7259. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].erase(hsmitr->second);
  7260. housing_spawn_map.erase(hsmitr);
  7261. }
  7262. }
  7263. m_pendingSpawnRemove.clear();
  7264. }
  7265. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7266. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7267. }
  7268. void ZoneServer::AddSpawnToGroup(Spawn* spawn, int32 group_id)
  7269. {
  7270. if( spawn->GetSpawnGroupID() > 0 )
  7271. spawn->RemoveSpawnFromGroup();
  7272. MutexList<int32>* groupList = &spawn_group_map.Get(group_id);
  7273. MutexList<int32>::iterator itr2 = groupList->begin();
  7274. while(itr2.Next())
  7275. {
  7276. Spawn* groupSpawn = GetSpawnByID(itr2.value);
  7277. if(groupSpawn)
  7278. {
  7279. // found existing group member to add it in
  7280. spawn->AddSpawnToGroup(groupSpawn);
  7281. break;
  7282. }
  7283. }
  7284. groupList->Add(spawn->GetID());
  7285. spawn->SetSpawnGroupID(group_id);
  7286. }
  7287. void ZoneServer::QueueStateCommandToClients(int32 spawn_id, int32 state)
  7288. {
  7289. if(spawn_id < 1)
  7290. return;
  7291. MLuaQueueStateCmd.lock();
  7292. lua_queued_state_commands.insert(make_pair(spawn_id, state));
  7293. MLuaQueueStateCmd.unlock();
  7294. }
  7295. void ZoneServer::QueueDefaultCommand(int32 spawn_id, std::string command, float distance)
  7296. {
  7297. if(spawn_id < 1)
  7298. return;
  7299. MLuaQueueStateCmd.lock();
  7300. lua_spawn_update_command[spawn_id].insert(make_pair(command,distance));
  7301. MLuaQueueStateCmd.unlock();
  7302. }
  7303. void ZoneServer::ProcessQueuedStateCommands() // in a client list lock only
  7304. {
  7305. vector<Client*>::iterator itr;
  7306. MLuaQueueStateCmd.lock();
  7307. if(lua_queued_state_commands.size() > 0)
  7308. {
  7309. std::map<int32, int32>::iterator statecmds;
  7310. for(statecmds = lua_queued_state_commands.begin(); statecmds != lua_queued_state_commands.end(); statecmds++)
  7311. {
  7312. Spawn* spawn = GetSpawnByID(statecmds->first, false);
  7313. if(!spawn)
  7314. continue;
  7315. MClientList.readlock(__FUNCTION__, __LINE__);
  7316. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7317. Client* client = *itr;
  7318. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7319. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), statecmds->second);
  7320. }
  7321. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7322. }
  7323. lua_queued_state_commands.clear();
  7324. }
  7325. if(lua_spawn_update_command.size() > 0)
  7326. {
  7327. std::map<int32, std::map<std::string,float>>::iterator updatecmds;
  7328. for(updatecmds = lua_spawn_update_command.begin(); updatecmds != lua_spawn_update_command.end(); updatecmds++)
  7329. {
  7330. Spawn* spawn = GetSpawnByID(updatecmds->first, false);
  7331. if(!spawn)
  7332. continue;
  7333. std::map<std::string,float>::iterator innermap;
  7334. for(innermap = lua_spawn_update_command[updatecmds->first].begin(); innermap != lua_spawn_update_command[updatecmds->first].end(); innermap++)
  7335. {
  7336. MClientList.readlock(__FUNCTION__, __LINE__);
  7337. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7338. Client* client = *itr;
  7339. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7340. client->SendDefaultCommand(spawn, innermap->first.c_str(), innermap->second);
  7341. }
  7342. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7343. }
  7344. lua_spawn_update_command[updatecmds->first].clear();
  7345. }
  7346. lua_spawn_update_command.clear();
  7347. }
  7348. MLuaQueueStateCmd.unlock();
  7349. }
  7350. void ZoneServer::UpdateClientSpawnMap(Player* player, Client* client)
  7351. {
  7352. // client may be null when passed
  7353. client_spawn_map.Put(player, client);
  7354. }
  7355. void ZoneServer::RemoveClientsFromZone(ZoneServer* zone) {
  7356. vector<Client*>::iterator itr;
  7357. MClientList.readlock(__FUNCTION__, __LINE__);
  7358. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7359. Client* client = *itr;
  7360. if(client->GetCurrentZone() == zone) {
  7361. client->SetCurrentZone(nullptr);
  7362. }
  7363. if(client->GetZoningDestination() == zone) {
  7364. client->SetZoningDestination(nullptr);
  7365. }
  7366. }
  7367. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7368. }
  7369. void ZoneServer::SendSubSpawnUpdates(SUBSPAWN_TYPES subtype) {
  7370. std::map<int32, Spawn*>::iterator subitr;
  7371. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7372. for(subitr = subspawn_list[subtype].begin(); subitr != subspawn_list[subtype].end(); subitr++) {
  7373. subitr->second->changed = true;
  7374. subitr->second->info_changed = true;
  7375. AddChangedSpawn(subitr->second);
  7376. }
  7377. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7378. }
  7379. bool ZoneServer::HouseItemSpawnExists(int32 item_id) {
  7380. bool exists = false;
  7381. std::map<int32, Spawn*>::iterator subitr;
  7382. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7383. subitr = subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].find(item_id);
  7384. if(subitr != subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].end()) {
  7385. exists = true;
  7386. }
  7387. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7388. return exists;
  7389. }
  7390. void ZoneServer::ProcessPendingSpawns() {
  7391. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7392. MPendingSpawnListAdd.writelock(__FUNCTION__, __LINE__);
  7393. list<Spawn*>::iterator itr2;
  7394. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7395. Spawn* spawn = *itr2;
  7396. if (spawn)
  7397. spawn_list[spawn->GetID()] = spawn;
  7398. if(spawn->IsCollector()) {
  7399. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].insert(make_pair(spawn->GetID(),spawn));
  7400. }
  7401. if(spawn->GetPickupItemID()) {
  7402. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].insert(make_pair(spawn->GetPickupItemID(),spawn));
  7403. housing_spawn_map.insert(make_pair(spawn->GetID(), spawn->GetPickupItemID()));
  7404. }
  7405. }
  7406. pending_spawn_list_add.clear();
  7407. MPendingSpawnListAdd.releasewritelock(__FUNCTION__, __LINE__);
  7408. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7409. }
  7410. void ZoneServer::AddSpawnToGrid(Spawn* spawn, int32 grid_id) {
  7411. if(spawn->GetID() == 0 || spawn->IsDeletedSpawn())
  7412. return;
  7413. MGridMaps.lock_shared();
  7414. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7415. if(grids != grid_maps.end()) {
  7416. grids->second->MSpawns.lock();
  7417. grids->second->spawns.insert(make_pair(spawn->GetID(), spawn));
  7418. grids->second->MSpawns.unlock();
  7419. }
  7420. else {
  7421. MGridMaps.unlock_shared();
  7422. MGridMaps.lock();
  7423. GridMap* gm = new GridMap;
  7424. gm->grid_id = grid_id;
  7425. gm->spawns.insert(make_pair(spawn->GetID(), spawn));
  7426. grid_maps.insert(make_pair(grid_id, gm));
  7427. MGridMaps.unlock();
  7428. return;
  7429. }
  7430. MGridMaps.unlock_shared();
  7431. }
  7432. void ZoneServer::RemoveSpawnFromGrid(Spawn* spawn, int32 grid_id) {
  7433. std::shared_lock lock(MGridMaps);
  7434. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7435. if(grids != grid_maps.end()) {
  7436. grids->second->MSpawns.lock();
  7437. if(grids->second->spawns.count(spawn->GetID()) > 0) {
  7438. grids->second->spawns.erase(spawn->GetID());
  7439. }
  7440. grids->second->MSpawns.unlock();
  7441. }
  7442. }
  7443. int32 ZoneServer::GetSpawnCountInGrid(int32 grid_id) {
  7444. int32 count = 0;
  7445. std::shared_lock lock(MGridMaps);
  7446. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7447. if(grids != grid_maps.end()) {
  7448. grids->second->MSpawns.lock_shared();
  7449. count = grids->second->spawns.size();
  7450. grids->second->MSpawns.unlock_shared();
  7451. }
  7452. return count;
  7453. }
  7454. void ZoneServer::SendClientSpawnListInGrid(Client* client, int32 grid_id){
  7455. std::shared_lock lock(MGridMaps);
  7456. Spawn* spawn = nullptr;
  7457. client->Message(CHANNEL_COLOR_RED, "Grid ID %u has %u spawns.", grid_id, GetSpawnCountInGrid(grid_id));
  7458. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7459. if(grids != grid_maps.end()) {
  7460. grids->second->MSpawns.lock_shared();
  7461. typedef map <int32, Spawn*> SpawnMapType;
  7462. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  7463. spawn = it->second;
  7464. 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());
  7465. }
  7466. grids->second->MSpawns.unlock_shared();
  7467. }
  7468. }