zoneserver.cpp 267 KB

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