9
3

zoneserver.cpp 289 KB

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