zoneserver.cpp 237 KB

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