zoneserver.cpp 241 KB

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