zoneserver.cpp 267 KB

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