zoneserver.cpp 297 KB

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