zoneserver.cpp 240 KB

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