zoneserver.cpp 298 KB

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