zoneserver.cpp 281 KB

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