zoneserver.cpp 288 KB

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