zoneserver.cpp 279 KB

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