zoneserver.cpp 281 KB

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