zoneserver.cpp 293 KB

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