zoneserver.cpp 268 KB

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