zoneserver.cpp 269 KB

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