zoneserver.cpp 269 KB

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