zoneserver.cpp 247 KB

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