9
3

zoneserver.cpp 289 KB

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