zoneserver.cpp 269 KB

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