zoneserver.cpp 282 KB

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