zoneserver.cpp 268 KB

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