zoneserver.cpp 317 KB

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