zoneserver.cpp 241 KB

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