zoneserver.cpp 295 KB

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