zoneserver.cpp 240 KB

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