zoneserver.cpp 268 KB

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