zoneserver.cpp 295 KB

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