zoneserver.cpp 280 KB

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