zoneserver.cpp 258 KB

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