zoneserver.cpp 241 KB

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