zoneserver.cpp 285 KB

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