zoneserver.cpp 295 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612
  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->Alive())
  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. RemoveSpawn(client->GetPlayer(), false, true, true, true, true);
  2885. connected_clients.Remove(client, true, DisconnectClientTimer); // changed from a hardcoded 30000 (30 sec) to the DisconnectClientTimer rule
  2886. }
  2887. }
  2888. void ZoneServer::RemoveClientImmediately(Client* client) {
  2889. Guild *guild;
  2890. if(client)
  2891. {
  2892. MClientList.writelock(__FUNCTION__, __LINE__);
  2893. std::vector<Client*>::iterator itr = find(clients.begin(), clients.end(), client);
  2894. if (itr != clients.end())
  2895. clients.erase(itr);
  2896. MClientList.releasewritelock(__FUNCTION__, __LINE__);
  2897. //clients.Remove(client, true);
  2898. }
  2899. }
  2900. void ZoneServer::ClientProcess()
  2901. {
  2902. if(connected_clients.size(true) == 0)
  2903. {
  2904. MIncomingClients.readlock(__FUNCTION__, __LINE__);
  2905. bool shutdownDelayCheck = shutdownDelayTimer.Check();
  2906. if((!AlwaysLoaded() && !shutdownTimer.Enabled()) || shutdownDelayCheck)
  2907. {
  2908. if(incoming_clients && !shutdownDelayTimer.Enabled()) {
  2909. LogWrite(ZONE__INFO, 0, "Zone", "Incoming clients (%u) expected for %s, delaying shutdown timer...", incoming_clients, GetZoneName());
  2910. int32 timerDelay = rule_manager.GetGlobalRule(R_Zone, ShutdownDelayTimer)->GetInt32();
  2911. if(timerDelay < 10) {
  2912. LogWrite(ZONE__INFO, 0, "Zone", "Overriding %s shutdown delay timer as other clients are incoming, value %u too short, setting to 10...", GetZoneName(), timerDelay);
  2913. timerDelay = 10;
  2914. }
  2915. shutdownDelayTimer.Start(timerDelay, true);
  2916. }
  2917. else if(!incoming_clients || shutdownDelayCheck) {
  2918. if(!shutdownTimer.Enabled()) {
  2919. LogWrite(ZONE__INFO, 0, "Zone", "Starting zone shutdown timer for %s...", GetZoneName());
  2920. shutdownTimer.Start();
  2921. }
  2922. else {
  2923. LogWrite(ZONE__INFO, 0, "Zone", "zone shutdown timer for %s has %u remaining...", GetZoneName(), shutdownTimer.GetRemainingTime());
  2924. }
  2925. }
  2926. }
  2927. MIncomingClients.releasereadlock(__FUNCTION__, __LINE__);
  2928. return;
  2929. }
  2930. shutdownTimer.Disable();
  2931. shutdownDelayTimer.Disable();
  2932. Client* client = 0;
  2933. MutexList<Client*>::iterator iterator = connected_clients.begin();
  2934. while(iterator.Next())
  2935. {
  2936. client = iterator->value;
  2937. #ifndef NO_CATCH
  2938. try
  2939. {
  2940. #endif
  2941. if(zoneShuttingDown || !client->Process(true))
  2942. {
  2943. if(!zoneShuttingDown && !client->IsZoning())
  2944. {
  2945. // avoid spam of messages while we await linkdead to complete
  2946. if(!client->IsLinkdeadTimerEnabled()) {
  2947. LogWrite(ZONE__DEBUG, 0, "Zone", "Client is disconnecting in %s (camping = %s)", __FUNCTION__, (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 ? "false" : "true");
  2948. }
  2949. if((client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0) {
  2950. client->StartLinkdeadTimer();
  2951. }
  2952. else if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 )
  2953. {
  2954. //only set LD flag if we're disconnecting but not camping/quitting
  2955. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() + ACTIVITY_STATUS_LINKDEAD);
  2956. client->StartLinkdeadTimer();
  2957. }
  2958. else {
  2959. // camp timer completed, remove client
  2960. RemoveClient(client);
  2961. client->Disconnect();
  2962. }
  2963. }
  2964. else {
  2965. // force boot all players or clients zoning
  2966. RemoveClient(client);
  2967. client->Disconnect();
  2968. }
  2969. }
  2970. #ifndef NO_CATCH
  2971. }
  2972. catch(...)
  2973. {
  2974. LogWrite(ZONE__ERROR, 0, "Zone", "Exception caught when in ZoneServer::ClientProcess() for zone '%s'!\n%s, %i", GetZoneName(), __FUNCTION__, __LINE__);
  2975. try{
  2976. if(!client->IsZoning())
  2977. {
  2978. if( (client->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_CAMPING) == 0 )
  2979. {
  2980. client->GetPlayer()->SetActivityStatus(client->GetPlayer()->GetActivityStatus() + ACTIVITY_STATUS_LINKDEAD);
  2981. client->StartLinkdeadTimer();
  2982. if(client->GetPlayer()->GetGroupMemberInfo())
  2983. world.GetGroupManager()->GroupMessage(client->GetPlayer()->GetGroupMemberInfo()->group_id, "%s has gone Linkdead.", client->GetPlayer()->GetName());
  2984. }
  2985. }
  2986. RemoveClient(client);
  2987. client->Disconnect();
  2988. }
  2989. catch(...){
  2990. LogWrite(ZONE__ERROR, 0, "Zone", "Exception caught when in ZoneServer::ClientProcess(), second try\n%s, %i", __FUNCTION__, __LINE__);
  2991. }
  2992. }
  2993. #endif
  2994. }
  2995. }
  2996. void ZoneServer::SimpleMessage(int8 type, const char* message, Spawn* from, float distance, bool send_to_sender){
  2997. Client* client = 0;
  2998. vector<Client*>::iterator client_itr;
  2999. MClientList.readlock(__FUNCTION__, __LINE__);
  3000. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3001. client = *client_itr;
  3002. if(from && client && client->IsConnected() && (send_to_sender || from != client->GetPlayer()) && from->GetDistance(client->GetPlayer()) <= distance){
  3003. client->SimpleMessage(type, message);
  3004. }
  3005. }
  3006. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3007. }
  3008. 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) {
  3009. if ((!distance || from->GetDistance(client->GetPlayer()) <= distance) && (!from || !client->GetPlayer()->IsIgnored(from->GetName()))) {
  3010. PacketStruct* packet = configReader.getStruct("WS_HearChat", client->GetVersion());
  3011. if (packet) {
  3012. if (from)
  3013. packet->setMediumStringByName("from", from->GetName());
  3014. if (client->GetPlayer() != from)
  3015. packet->setMediumStringByName("to", client->GetPlayer()->GetName());
  3016. packet->setDataByName("channel", client->GetMessageChannelColor(channel));
  3017. if (from && ((from == client->GetPlayer()) || (client->GetPlayer()->WasSentSpawn(from->GetID()))))
  3018. packet->setDataByName("from_spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(from));
  3019. else
  3020. packet->setDataByName("from_spawn_id", 0xFFFFFFFF);
  3021. packet->setDataByName("to_spawn_id", 0xFFFFFFFF);
  3022. packet->setMediumStringByName("message", message);
  3023. packet->setDataByName("language", language);
  3024. bool hasLanguage = client->GetPlayer()->HasLanguage(language);
  3025. if (language > 0 && !hasLanguage)
  3026. packet->setDataByName("understood", 0);
  3027. else
  3028. packet->setDataByName("understood", 1);
  3029. show_bubble == true ? packet->setDataByName("show_bubble", 1) : packet->setDataByName("show_bubble", 0);
  3030. if (channel_name)
  3031. packet->setMediumStringByName("channel_name", channel_name);
  3032. EQ2Packet* outapp = packet->serialize();
  3033. //DumpPacket(outapp);
  3034. client->QueuePacket(outapp);
  3035. safe_delete(packet);
  3036. }
  3037. }
  3038. }
  3039. void ZoneServer::HandleChatMessage(Spawn* from, const char* to, int16 channel, const char* message, float distance, const char* channel_name, bool show_bubble, int32 language){
  3040. vector<Client*>::iterator client_itr;
  3041. Client* client = 0;
  3042. MClientList.readlock(__FUNCTION__, __LINE__);
  3043. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3044. client = *client_itr;
  3045. if(client && client->IsConnected())
  3046. HandleChatMessage(client, from, to, channel, message, distance, channel_name, show_bubble, language);
  3047. }
  3048. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3049. }
  3050. void ZoneServer::HandleBroadcast(const char* message) {
  3051. vector<Client*>::iterator client_itr;
  3052. Client* client = 0;
  3053. MClientList.readlock(__FUNCTION__, __LINE__);
  3054. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3055. client = *client_itr;
  3056. if(client && client->IsConnected())
  3057. client->SimpleMessage(CHANNEL_BROADCAST, message);
  3058. }
  3059. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3060. }
  3061. void ZoneServer::HandleAnnouncement(const char* message) {
  3062. vector<Client*>::iterator client_itr;
  3063. Client* client = 0;
  3064. int32 words = ::CountWordsInString(message);
  3065. if (words < 5)
  3066. words = 5;
  3067. MClientList.readlock(__FUNCTION__, __LINE__);
  3068. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3069. client = *client_itr;
  3070. if(client && client->IsConnected()) {
  3071. client->SimpleMessage(CHANNEL_BROADCAST, message);
  3072. client->SendPopupMessage(10, message, "ui_harvest_normal", words, 0xFF, 0xFF, 0x00);
  3073. }
  3074. }
  3075. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3076. }
  3077. void ZoneServer::SendTimeUpdate(Client* client){
  3078. if(client){
  3079. PacketStruct* packet = world.GetWorldTime(client->GetVersion());
  3080. if(packet){
  3081. client->QueuePacket(packet->serialize());
  3082. safe_delete(packet);
  3083. }
  3084. }
  3085. }
  3086. void ZoneServer::SendTimeUpdateToAllClients(){
  3087. Client* client = 0;
  3088. vector<Client*>::iterator client_itr;
  3089. MClientList.readlock(__FUNCTION__, __LINE__);
  3090. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3091. client = *client_itr;
  3092. if(client && client->IsConnected())
  3093. SendTimeUpdate(client);
  3094. }
  3095. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3096. }
  3097. void ZoneServer::UpdateVitality(float amount){
  3098. Client* client = 0;
  3099. vector<Client*>::iterator client_itr;
  3100. MClientList.readlock(__FUNCTION__, __LINE__);
  3101. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3102. client = *client_itr;
  3103. if(client && client->GetPlayer()->GetInfoStruct()->get_xp_vitality() < 100){
  3104. if((client->GetPlayer()->GetInfoStruct()->get_xp_vitality() + amount) > 100)
  3105. client->GetPlayer()->GetInfoStruct()->set_xp_vitality(100);
  3106. else
  3107. client->GetPlayer()->GetInfoStruct()->add_xp_vitality(amount);
  3108. client->GetPlayer()->SetCharSheetChanged(true);
  3109. }
  3110. }
  3111. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3112. }
  3113. void ZoneServer::SendSpawn(Spawn* spawn, Client* client){
  3114. EQ2Packet* outapp = spawn->serialize(client->GetPlayer(), client->GetVersion());
  3115. if(!client->GetPlayer()->IsSendingSpawn(spawn->GetID())) {
  3116. safe_delete(outapp);
  3117. }
  3118. else {
  3119. LogWrite(ZONE__DEBUG, 7, "Zone", "%s: Processing SendSpawn for spawn index %u (%s)...", client->GetPlayer()->GetName(), client->GetPlayer()->GetIndexForSpawn(spawn), spawn->GetName());
  3120. if(outapp)
  3121. client->QueuePacket(outapp, true);
  3122. client->GetPlayer()->SetSpawnSentState(spawn, SpawnState::SPAWN_STATE_SENT_WAIT);
  3123. }
  3124. /*
  3125. vis flags:
  3126. 2 = show icon
  3127. 4 = targetable
  3128. 16 = show name
  3129. 32 = show level/border
  3130. activity_status:
  3131. 4 - linkdead
  3132. 8 - camping
  3133. 16 - LFG
  3134. 32 - LFW
  3135. 2048 - mentoring
  3136. 4096 - displays shield
  3137. 8192 - immunity gained
  3138. 16384 - immunity remaining
  3139. attackable_status
  3140. 1 - no_hp_bar
  3141. 4 - not attackable
  3142. npc_con
  3143. -4 = scowls
  3144. -3 = threatening
  3145. -2 = dubiously
  3146. -1 = apprehensively
  3147. 0 = indifferent
  3148. 1 = amiably
  3149. 2 = kindly
  3150. 3 = warmly
  3151. 4 = ally
  3152. quest_flag
  3153. 1 = new quest
  3154. 2 = update and new quest
  3155. 3 = update
  3156. */
  3157. if(spawn->IsEntity() && spawn->HasTrapTriggered())
  3158. client->QueueStateCommand(client->GetPlayer()->GetIDWithPlayerSpawn(spawn), spawn->GetTrapState());
  3159. }
  3160. Client* ZoneServer::GetClientBySpawn(Spawn* spawn){
  3161. return client_spawn_map.Get(spawn);
  3162. }
  3163. Client* ZoneServer::GetClientByName(char* name) {
  3164. Client* ret = 0;
  3165. vector<Client*>::iterator itr;
  3166. MClientList.readlock(__FUNCTION__, __LINE__);
  3167. for (itr = clients.begin(); itr != clients.end(); itr++) {
  3168. if ((*itr)->GetPlayer()) {
  3169. if (strncmp((*itr)->GetPlayer()->GetName(), name, strlen(name)) == 0) {
  3170. ret = *itr;
  3171. break;
  3172. }
  3173. }
  3174. }
  3175. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3176. return ret;
  3177. }
  3178. Client* ZoneServer::GetClientByCharID(int32 charid) {
  3179. Client* ret = 0;
  3180. vector<Client*>::iterator itr;
  3181. MClientList.readlock(__FUNCTION__, __LINE__);
  3182. for (itr = clients.begin(); itr != clients.end(); itr++) {
  3183. if ((*itr)->GetCharacterID() == charid) {
  3184. ret = *itr;
  3185. break;
  3186. }
  3187. }
  3188. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3189. return ret;
  3190. }
  3191. void ZoneServer::AddMovementNPC(Spawn* spawn){
  3192. if (spawn)
  3193. movement_spawns.Put(spawn->GetID(), 1);
  3194. }
  3195. void ZoneServer::RemoveMovementNPC(Spawn* spawn){
  3196. if (spawn)
  3197. remove_movement_spawns.Add(spawn->GetID());
  3198. }
  3199. void ZoneServer::PlayFlavor(Client* client, Spawn* spawn, const char* mp3, const char* text, const char* emote, int32 key1, int32 key2, int8 language){
  3200. if(!client || !spawn)
  3201. return;
  3202. PacketStruct* packet = configReader.getStruct("WS_PlayFlavor", client->GetVersion());
  3203. if(packet){
  3204. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  3205. packet->setDataByName("unknown1", 0xFFFFFFFF);
  3206. packet->setDataByName("unknown5", 1, 1);
  3207. packet->setDataByName("unknown5", 1, 6);
  3208. if(mp3){
  3209. packet->setMediumStringByName("mp3", mp3);
  3210. packet->setDataByName("key", key1);
  3211. packet->setDataByName("key", key2, 1);
  3212. }
  3213. packet->setMediumStringByName("name", spawn->GetName());
  3214. if(text)
  3215. packet->setMediumStringByName("text", text);
  3216. if(emote)
  3217. packet->setMediumStringByName("emote", emote);
  3218. if (language != 0)
  3219. packet->setDataByName("language", language);
  3220. //We should probably add Common = language id 0 or 0xFF so admins can customize more..
  3221. if (language == 0 || client->GetPlayer()->HasLanguage(language))
  3222. packet->setDataByName("understood", 1);
  3223. EQ2Packet* app = packet->serialize();
  3224. //DumpPacket(app);
  3225. client->QueuePacket(app);
  3226. safe_delete(packet);
  3227. }
  3228. }
  3229. void ZoneServer::PlayVoice(Client* client, Spawn* spawn, const char* mp3, int32 key1, int32 key2){
  3230. if(!client || !spawn)
  3231. return;
  3232. PacketStruct* packet = configReader.getStruct("WS_PlayVoice", client->GetVersion());
  3233. if(packet){
  3234. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  3235. packet->setMediumStringByName("mp3", mp3);
  3236. packet->setDataByName("key", key1);
  3237. packet->setDataByName("key", key2, 1);
  3238. client->QueuePacket(packet->serialize());
  3239. safe_delete(packet);
  3240. }
  3241. }
  3242. void ZoneServer::PlayFlavor(Spawn* spawn, const char* mp3, const char* text, const char* emote, int32 key1, int32 key2, int8 language){
  3243. if(!spawn)
  3244. return;
  3245. Client* client = 0;
  3246. vector<Client*>::iterator client_itr;
  3247. MClientList.readlock(__FUNCTION__, __LINE__);
  3248. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3249. client = *client_itr;
  3250. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->GetDistance(spawn) > 30)
  3251. continue;
  3252. PlayFlavor(client, spawn, mp3, text, emote, key1, key2, language);
  3253. }
  3254. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3255. }
  3256. void ZoneServer::PlayFlavorID(Spawn* spawn, int8 type, int32 id, int16 index, int8 language){
  3257. if(!spawn)
  3258. return;
  3259. Client* client = 0;
  3260. vector<Client*>::iterator client_itr;
  3261. VoiceOverStruct non_garble, garble;
  3262. bool garble_success = false;
  3263. bool success = world.FindVoiceOver(type, id, index, &non_garble, &garble_success, &garble);
  3264. VoiceOverStruct* resStruct = nullptr;
  3265. MClientList.readlock(__FUNCTION__, __LINE__);
  3266. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3267. client = *client_itr;
  3268. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->GetDistance(spawn) > 30)
  3269. continue;
  3270. client->SendPlayFlavor(spawn, language, &non_garble, &garble, success, garble_success);
  3271. }
  3272. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3273. }
  3274. void ZoneServer::PlayVoice(Spawn* spawn, const char* mp3, int32 key1, int32 key2){
  3275. if(!spawn || !mp3)
  3276. return;
  3277. Client* client = 0;
  3278. vector<Client*>::iterator client_itr;
  3279. MClientList.readlock(__FUNCTION__, __LINE__);
  3280. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3281. client = *client_itr;
  3282. if(!client || !client->IsReadyForUpdates() || !client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  3283. continue;
  3284. PlayVoice(client, spawn, mp3, key1, key2);
  3285. }
  3286. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3287. }
  3288. void ZoneServer::PlaySoundFile(Client* client, const char* name, float origin_x, float origin_y, float origin_z){
  3289. if(!name)
  3290. return;
  3291. PacketStruct* packet = 0;
  3292. if(client){
  3293. packet = configReader.getStruct("WS_Play3DSound", client->GetVersion());
  3294. if(packet){
  3295. packet->setMediumStringByName("name", name);
  3296. packet->setDataByName("x", origin_x);
  3297. packet->setDataByName("y", origin_y);
  3298. packet->setDataByName("z", origin_z);
  3299. packet->setDataByName("unknown1", 1);
  3300. packet->setDataByName("unknown2", 2.5);
  3301. packet->setDataByName("unknown3", 15);
  3302. client->QueuePacket(packet->serialize());
  3303. safe_delete(packet);
  3304. }
  3305. }
  3306. else{
  3307. EQ2Packet* outapp = 0;
  3308. int16 packet_version = 0;
  3309. vector<Client*>::iterator client_itr;
  3310. MClientList.readlock(__FUNCTION__, __LINE__);
  3311. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3312. client = *client_itr;
  3313. if(client && (!packet || packet_version != client->GetVersion())){
  3314. safe_delete(packet);
  3315. safe_delete(outapp);
  3316. packet_version = client->GetVersion();
  3317. packet = configReader.getStruct("WS_Play3DSound", packet_version);
  3318. if(packet){
  3319. packet->setMediumStringByName("name", name);
  3320. packet->setDataByName("x", origin_x);
  3321. packet->setDataByName("y", origin_y);
  3322. packet->setDataByName("z", origin_z);
  3323. packet->setDataByName("unknown1", 1);
  3324. packet->setDataByName("unknown2", 2.5);
  3325. packet->setDataByName("unknown3", 15);
  3326. outapp = packet->serialize();
  3327. }
  3328. }
  3329. if(outapp && client && client->IsReadyForUpdates())
  3330. client->QueuePacket(outapp->Copy());
  3331. }
  3332. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3333. safe_delete(packet);
  3334. safe_delete(outapp);
  3335. }
  3336. }
  3337. bool ZoneServer::HasWidgetTimer(Spawn* widget){
  3338. bool ret = false;
  3339. if (widget) {
  3340. int32 id = widget->GetID();
  3341. map<int32, int32>::iterator itr;
  3342. MWidgetTimers.readlock(__FUNCTION__, __LINE__);
  3343. for (itr = widget_timers.begin(); itr != widget_timers.end(); itr++) {
  3344. if(itr->first == id){
  3345. ret = true;
  3346. break;
  3347. }
  3348. }
  3349. MWidgetTimers.releasereadlock(__FUNCTION__, __LINE__);
  3350. }
  3351. return ret;
  3352. }
  3353. void ZoneServer::CheckWidgetTimers(){
  3354. vector<int32> remove_list;
  3355. map<int32, int32>::iterator itr;
  3356. MWidgetTimers.readlock(__FUNCTION__, __LINE__);
  3357. for (itr = widget_timers.begin(); itr != widget_timers.end(); itr++) {
  3358. if(Timer::GetCurrentTime2() >= itr->second){
  3359. /*Spawn* widget = GetSpawnByID(itr->first);
  3360. if (widget && widget->IsWidget())
  3361. ((Widget*)widget)->HandleTimerUpdate();*/
  3362. remove_list.push_back(itr->first);
  3363. }
  3364. }
  3365. MWidgetTimers.releasereadlock(__FUNCTION__, __LINE__);
  3366. for (int32 i = 0; i < remove_list.size(); i++) {
  3367. Spawn* widget = GetSpawnByID(remove_list[i]);
  3368. if (widget && widget->IsWidget())
  3369. ((Widget*)widget)->HandleTimerUpdate();
  3370. }
  3371. MWidgetTimers.writelock(__FUNCTION__, __LINE__);
  3372. for(int32 i=0;i<remove_list.size(); i++)
  3373. widget_timers.erase(remove_list[i]);
  3374. MWidgetTimers.releasewritelock(__FUNCTION__, __LINE__);
  3375. }
  3376. void ZoneServer::AddWidgetTimer(Spawn* widget, float time) {
  3377. if (widget && widget->IsWidget()) {
  3378. MWidgetTimers.writelock(__FUNCTION__, __LINE__);
  3379. widget_timers[widget->GetID()] = ((int32)(time * 1000)) + Timer::GetCurrentTime2();
  3380. MWidgetTimers.releasewritelock(__FUNCTION__, __LINE__);
  3381. }
  3382. }
  3383. Spawn* ZoneServer::GetSpawnGroup(int32 id){
  3384. Spawn* ret = 0;
  3385. Spawn* spawn = 0;
  3386. if(id < 1)
  3387. return 0;
  3388. bool lookup = false;
  3389. if(quick_group_id_lookup.count(id) > 0) {
  3390. ret = GetSpawnByID(quick_group_id_lookup.Get(id));
  3391. lookup = true;
  3392. }
  3393. if(ret == NULL) {
  3394. if(lookup)
  3395. quick_group_id_lookup.erase(id);
  3396. map<int32, Spawn*>::iterator itr;
  3397. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3398. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3399. spawn = itr->second;
  3400. if(spawn){
  3401. if(spawn->GetSpawnGroupID() == id){
  3402. ret = spawn;
  3403. quick_group_id_lookup.Put(id, spawn->GetID());
  3404. break;
  3405. }
  3406. }
  3407. }
  3408. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3409. }
  3410. return ret;
  3411. }
  3412. Spawn* ZoneServer::GetSpawnByLocationID(int32 location_id) {
  3413. Spawn* ret = 0;
  3414. Spawn* current_spawn = 0;
  3415. if(location_id < 1)
  3416. return 0;
  3417. bool lookup = false;
  3418. if(quick_location_id_lookup.count(location_id) > 0) {
  3419. ret = GetSpawnByID(quick_location_id_lookup.Get(location_id));
  3420. lookup = true;
  3421. }
  3422. if(ret == NULL) {
  3423. if(lookup)
  3424. quick_location_id_lookup.erase(location_id);
  3425. map<int32, Spawn*>::iterator itr;
  3426. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3427. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3428. current_spawn = itr->second;
  3429. if (current_spawn && current_spawn->GetSpawnLocationID() == location_id) {
  3430. ret = current_spawn;
  3431. quick_location_id_lookup.Put(location_id, ret->GetID());
  3432. break;
  3433. }
  3434. }
  3435. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3436. }
  3437. return ret;
  3438. }
  3439. Spawn* ZoneServer::GetSpawnByDatabaseID(int32 id){
  3440. Spawn* ret = 0;
  3441. if(id < 1)
  3442. return 0;
  3443. bool lookup = false;
  3444. if(quick_database_id_lookup.count(id) > 0) {
  3445. ret = GetSpawnByID(quick_database_id_lookup.Get(id));
  3446. lookup = true;
  3447. }
  3448. if(ret == NULL){
  3449. if(lookup)
  3450. quick_database_id_lookup.erase(id);
  3451. Spawn* spawn = 0;
  3452. map<int32, Spawn*>::iterator itr;
  3453. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3454. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++){
  3455. spawn = itr->second;
  3456. if(spawn){
  3457. if(spawn->GetDatabaseID() == id){
  3458. quick_database_id_lookup.Put(id, spawn->GetID());
  3459. ret = spawn;
  3460. break;
  3461. }
  3462. }
  3463. }
  3464. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3465. }
  3466. return ret;
  3467. }
  3468. Spawn* ZoneServer::GetSpawnByID(int32 id, bool spawnListLocked) {
  3469. Spawn* ret = 0;
  3470. if (!spawnListLocked )
  3471. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3472. if (spawn_list.count(id) > 0)
  3473. ret = spawn_list[id];
  3474. if (!spawnListLocked)
  3475. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3476. return ret;
  3477. }
  3478. bool ZoneServer::SendRemoveSpawn(Client* client, Spawn* spawn, PacketStruct* packet, bool delete_spawn)
  3479. {
  3480. if(!client || !spawn || (client && client->GetPlayer() == spawn))
  3481. return false;
  3482. if(client->GetPlayerPOVGhostSpawnID() == spawn->GetID()) {
  3483. client->SetPlayerPOVGhost(nullptr);
  3484. }
  3485. spawn->RemoveSpawnFromPlayer(client->GetPlayer());
  3486. // 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);
  3487. /* if(packet && index > 0 && !wasRemoved)
  3488. {
  3489. packet->ResetData();
  3490. packet->setDataByName("spawn_index", index);
  3491. spawn->RemoveSpawnFromPlayer(client->GetPlayer());
  3492. if(delete_spawn)
  3493. packet->setDataByName("delete", 1);
  3494. client->QueuePacket(packet->serialize());
  3495. return true;
  3496. }
  3497. return false;*/
  3498. return true;
  3499. }
  3500. void ZoneServer::SetSpawnCommand(Spawn* spawn, int8 type, char* value, Client* client){
  3501. //commands
  3502. LogWrite(MISC__TODO, 1, "TODO", "%s does nothing!\n%s, %i", __FUNCTION__, __FILE__, __LINE__);
  3503. }
  3504. void ZoneServer::SetSpawnCommand(int32 spawn_id, int8 type, char* value, Client* client){
  3505. LogWrite(MISC__TODO, 1, "TODO", "%s does nothing!\n%s, %i", __FUNCTION__, __FILE__, __LINE__);
  3506. }
  3507. void ZoneServer::ApplySetSpawnCommand(Client* client, Spawn* target, int8 type, const char* value){
  3508. // 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
  3509. // location values (x, y, z, heading, grid) for all spawns in the zone with the same DB ID, only the targeted spawn
  3510. if(type == SPAWN_SET_VALUE_SPAWNENTRY_SCRIPT || type == SPAWN_SET_VALUE_SPAWNLOCATION_SCRIPT || (type >= SPAWN_SET_VALUE_X && type <= SPAWN_SET_VALUE_LOCATION) ||
  3511. type == SPAWN_SET_VALUE_PITCH || type == SPAWN_SET_VALUE_ROLL)
  3512. return;
  3513. Spawn* tmp = 0;
  3514. if(target->IsNPC())
  3515. tmp = GetNPC(target->GetDatabaseID());
  3516. else if(target->IsObject())
  3517. tmp = GetObject(target->GetDatabaseID());
  3518. else if(target->IsGroundSpawn())
  3519. tmp = GetGroundSpawn(target->GetDatabaseID());
  3520. else if(target->IsSign())
  3521. tmp = GetSign(target->GetDatabaseID());
  3522. else if(target->IsWidget())
  3523. tmp = GetWidget(target->GetDatabaseID());
  3524. if(tmp && type == SPAWN_SET_VALUE_SPAWN_SCRIPT)
  3525. tmp->SetSpawnScript(value);
  3526. else if(tmp)
  3527. commands.SetSpawnCommand(client, tmp, type, value); // set the master spawn
  3528. Spawn* spawn = 0;
  3529. // this check needs to be here otherwise every spawn with 0 will be set
  3530. if ( target->GetDatabaseID ( ) > 0 )
  3531. {
  3532. map<int32, Spawn*>::iterator itr;
  3533. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3534. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3535. spawn = itr->second;
  3536. if(spawn && spawn->GetDatabaseID() == target->GetDatabaseID()){
  3537. if(type == SPAWN_SET_VALUE_SPAWN_SCRIPT)
  3538. spawn->SetSpawnScript(value);
  3539. else
  3540. commands.SetSpawnCommand(client, spawn, type, value);
  3541. }
  3542. }
  3543. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3544. }
  3545. }
  3546. void ZoneServer::StopSpawnScriptTimer(Spawn* spawn, std::string functionName){
  3547. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3548. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3549. if(spawn_script_timers.size() > 0){
  3550. set<SpawnScriptTimer*>::iterator itr;
  3551. SpawnScriptTimer* timer = 0;
  3552. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3553. timer = *itr;
  3554. if(timer->spawn == spawn->GetID() && (functionName == "" || timer->function == functionName) && remove_spawn_script_timers_list.count(timer) == 0) {
  3555. itr = spawn_script_timers.erase(itr);
  3556. safe_delete(timer);
  3557. }
  3558. }
  3559. }
  3560. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3561. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3562. }
  3563. void ZoneServer::DeleteSpawnScriptTimers(Spawn* spawn, bool all){
  3564. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3565. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3566. if(spawn_script_timers.size() > 0){
  3567. set<SpawnScriptTimer*>::iterator itr;
  3568. SpawnScriptTimer* timer = 0;
  3569. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3570. timer = *itr;
  3571. if((all || timer->spawn == spawn->GetID()) && remove_spawn_script_timers_list.count(timer) == 0)
  3572. remove_spawn_script_timers_list.insert(timer);
  3573. }
  3574. if(all)
  3575. spawn_script_timers.clear();
  3576. }
  3577. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3578. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3579. }
  3580. void ZoneServer::DeleteSpawnScriptTimers() {
  3581. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3582. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3583. if(remove_spawn_script_timers_list.size() > 0){
  3584. set<SpawnScriptTimer*>::iterator itr;
  3585. SpawnScriptTimer* timer = 0;
  3586. for (itr = remove_spawn_script_timers_list.begin(); itr != remove_spawn_script_timers_list.end(); itr++) {
  3587. timer = *itr;
  3588. spawn_script_timers.erase(timer);
  3589. safe_delete(timer);
  3590. }
  3591. remove_spawn_script_timers_list.clear();
  3592. }
  3593. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3594. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3595. }
  3596. void ZoneServer::CheckSpawnScriptTimers(){
  3597. DeleteSpawnScriptTimers();
  3598. SpawnScriptTimer* timer = 0;
  3599. vector<SpawnScriptTimer> call_timers;
  3600. MSpawnScriptTimers.readlock(__FUNCTION__, __LINE__);
  3601. MRemoveSpawnScriptTimersList.writelock(__FUNCTION__, __LINE__);
  3602. if(spawn_script_timers.size() > 0){
  3603. int32 current_time = Timer::GetCurrentTime2();
  3604. set<SpawnScriptTimer*>::iterator itr;
  3605. for (itr = spawn_script_timers.begin(); itr != spawn_script_timers.end(); itr++) {
  3606. timer = *itr;
  3607. if(remove_spawn_script_timers_list.count(timer) == 0 &&
  3608. timer->current_count < timer->max_count && current_time >= timer->timer){
  3609. timer->current_count++;
  3610. SpawnScriptTimer tmpTimer;
  3611. tmpTimer.current_count = timer->current_count;
  3612. tmpTimer.function = timer->function;
  3613. tmpTimer.player = timer->player;
  3614. tmpTimer.spawn = timer->spawn;
  3615. tmpTimer.max_count = timer->max_count;
  3616. call_timers.push_back(tmpTimer);
  3617. }
  3618. if(timer->current_count >= timer->max_count && remove_spawn_script_timers_list.count(timer) == 0)
  3619. remove_spawn_script_timers_list.insert(timer);
  3620. }
  3621. }
  3622. MRemoveSpawnScriptTimersList.releasewritelock(__FUNCTION__, __LINE__);
  3623. MSpawnScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  3624. if(call_timers.size() > 0){
  3625. vector<SpawnScriptTimer>::iterator itr;
  3626. for(itr = call_timers.begin(); itr != call_timers.end(); itr++){
  3627. SpawnScriptTimer tmpTimer = (SpawnScriptTimer)*itr;
  3628. CallSpawnScript(GetSpawnByID(tmpTimer.spawn), SPAWN_SCRIPT_TIMER, tmpTimer.player > 0 ? GetSpawnByID(tmpTimer.player) : 0, tmpTimer.function.c_str());
  3629. }
  3630. }
  3631. }
  3632. void ZoneServer::KillSpawnByDistance(Spawn* spawn, float max_distance, bool include_players, bool send_packet){
  3633. std::shared_lock lock(MGridMaps);
  3634. if(!spawn)
  3635. return;
  3636. Spawn* test_spawn = 0;
  3637. std::map<int32, GridMap*>::iterator grids = grid_maps.find(spawn->GetLocation());
  3638. if(grids != grid_maps.end()) {
  3639. grids->second->MSpawns.lock_shared();
  3640. typedef map <int32, Spawn*> SpawnMapType;
  3641. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  3642. test_spawn = it->second;
  3643. if(test_spawn && test_spawn->Alive() && test_spawn->GetID() > 0 && test_spawn->GetID() != spawn->GetID() && test_spawn->IsEntity() &&
  3644. (!test_spawn->IsPlayer() || include_players)){
  3645. if(test_spawn->GetDistance(spawn) < max_distance)
  3646. KillSpawn(true, test_spawn, spawn, send_packet);
  3647. }
  3648. }
  3649. grids->second->MSpawns.unlock_shared();
  3650. }
  3651. }
  3652. void ZoneServer::SpawnSetByDistance(Spawn* spawn, float max_distance, string field, string value){
  3653. std::shared_lock lock(MGridMaps);
  3654. if(!spawn)
  3655. return;
  3656. Spawn* test_spawn = 0;
  3657. int32 type = commands.GetSpawnSetType(field);
  3658. if(type == 0xFFFFFFFF)
  3659. return;
  3660. std::map<int32, GridMap*>::iterator grids = grid_maps.find(spawn->GetLocation());
  3661. if(grids != grid_maps.end()) {
  3662. grids->second->MSpawns.lock_shared();
  3663. typedef map <int32, Spawn*> SpawnMapType;
  3664. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  3665. test_spawn = it->second;
  3666. if(test_spawn && test_spawn->GetID() > 0 && test_spawn->GetID() != spawn->GetID() && !test_spawn->IsPlayer()){
  3667. if(test_spawn->GetDistance(spawn) < max_distance){
  3668. commands.SetSpawnCommand(0, test_spawn, type, value.c_str());
  3669. }
  3670. }
  3671. }
  3672. grids->second->MSpawns.unlock_shared();
  3673. }
  3674. }
  3675. void ZoneServer::AddSpawnScriptTimer(SpawnScriptTimer* timer){
  3676. MSpawnScriptTimers.writelock(__FUNCTION__, __LINE__);
  3677. spawn_script_timers.insert(timer);
  3678. MSpawnScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  3679. }
  3680. /*
  3681. void ZoneServer::RemoveFromRangeMap(Client* client){
  3682. spawn_range_map.erase(client);
  3683. }
  3684. */
  3685. void ZoneServer::RemoveSpawn(Spawn* spawn, bool delete_spawn, bool respawn, bool lock, bool erase_from_spawn_list, bool lock_spell_process)
  3686. {
  3687. LogWrite(ZONE__DEBUG, 3, "Zone", "Processing RemoveSpawn function for %s (%i)...", spawn->GetName(),spawn->GetID());
  3688. PacketStruct* packet = 0;
  3689. int16 packet_version = 0;
  3690. Client* client = 0;
  3691. vector<Client*>::iterator client_itr;
  3692. MClientList.readlock(__FUNCTION__, __LINE__);
  3693. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3694. client = *client_itr;
  3695. if (client && (client->GetVersion() > 283 || !client->IsZoning() || client->GetPlayer() != spawn)) { //don't send destroy ghost of 283 client when zoning
  3696. if (client->GetPlayer()->HasTarget() && client->GetPlayer()->GetTarget() == spawn)
  3697. client->GetPlayer()->SetTarget(0);
  3698. if(client->GetPlayer()->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->IsSendingSpawn(spawn->GetID()))
  3699. SendRemoveSpawn(client, spawn, packet, delete_spawn);
  3700. if (spawn_range_map.count(client) > 0)
  3701. spawn_range_map.Get(client)->erase(spawn->GetID());
  3702. }
  3703. }
  3704. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3705. safe_delete(packet);
  3706. spawn->RemoveSpawnProximities();
  3707. RemoveSpawnProximities(spawn);
  3708. if (movementMgr != nullptr && spawn->IsEntity()) {
  3709. movementMgr->RemoveMob((Entity*)spawn);
  3710. }
  3711. RemoveSpawnSupportFunctions(spawn, lock_spell_process);
  3712. if (reloading)
  3713. RemoveDeadEnemyList(spawn);
  3714. if (lock)
  3715. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  3716. if (dead_spawns.count(spawn->GetID()) > 0)
  3717. dead_spawns.erase(spawn->GetID());
  3718. if (lock)
  3719. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  3720. if (spawn_expire_timers.count(spawn->GetID()) > 0)
  3721. spawn_expire_timers.erase(spawn->GetID());
  3722. spawn->SetDeletedSpawn(true);
  3723. // 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)
  3724. if(erase_from_spawn_list)
  3725. AddPendingSpawnRemove(spawn->GetID());
  3726. if(respawn && !spawn->IsPlayer() && spawn->GetRespawnTime() > 0 && spawn->GetSpawnLocationID() > 0)
  3727. {
  3728. LogWrite(ZONE__DEBUG, 3, "Zone", "Handle NPC Respawn for '%s'.", spawn->GetName());
  3729. // handle instance spawn db info
  3730. // we don't care if a NPC or a client kills the spawn, we could have events that cause NPCs to kill NPCs.
  3731. if(spawn->GetZone()->GetInstanceID() > 0 && spawn->GetSpawnLocationID() > 0)
  3732. {
  3733. LogWrite(ZONE__DEBUG, 3, "Zone", "Handle NPC Respawn in an Instance.");
  3734. // use respawn time to either insert/update entry (likely insert in this situation)
  3735. if ( spawn->IsNPC() )
  3736. {
  3737. database.CreateInstanceSpawnRemoved(spawn->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC,
  3738. spawn->GetRespawnTime(),spawn->GetZone()->GetInstanceID());
  3739. }
  3740. else if ( spawn->IsObject ( ) )
  3741. {
  3742. database.CreateInstanceSpawnRemoved(spawn->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT,
  3743. spawn->GetRespawnTime(),spawn->GetZone()->GetInstanceID());
  3744. }
  3745. }
  3746. else
  3747. {
  3748. int32 spawnLocationID = spawn->GetSpawnLocationID();
  3749. int32 spawnRespawnTime = spawn->GetRespawnTime();
  3750. respawn_timers.Put(spawnLocationID, Timer::GetCurrentTime2() + spawnRespawnTime * 1000);
  3751. }
  3752. }
  3753. RemoveSpawnFromGrid(spawn, spawn->GetLocation());
  3754. // 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
  3755. if (lock && !respawn)
  3756. MDeadSpawns.readlock(__FUNCTION__, __LINE__);
  3757. if(delete_spawn && dead_spawns.count(spawn->GetID()) == 0)
  3758. AddPendingDelete(spawn);
  3759. if (lock && !respawn)
  3760. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  3761. LogWrite(ZONE__DEBUG, 3, "Zone", "Done processing RemoveSpawn function...");
  3762. }
  3763. Spawn* ZoneServer::GetClosestSpawn(Spawn* spawn, int32 spawn_id){
  3764. Spawn* closest_spawn = 0;
  3765. Spawn* test_spawn = 0;
  3766. float closest_distance = 1000000;
  3767. float test_distance = 0;
  3768. map<int32, Spawn*>::iterator itr;
  3769. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3770. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3771. test_spawn = itr->second;
  3772. if(test_spawn && test_spawn != spawn && test_spawn->GetDatabaseID() == spawn_id){
  3773. test_distance = test_spawn->GetDistance(spawn);
  3774. if(test_distance < closest_distance){
  3775. closest_distance = test_distance;
  3776. closest_spawn = test_spawn;
  3777. }
  3778. }
  3779. }
  3780. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3781. return closest_spawn;
  3782. }
  3783. int32 ZoneServer::GetClosestLocation(Spawn* spawn){
  3784. Spawn* closest_spawn = 0;
  3785. Spawn* test_spawn = 0;
  3786. float closest_distance = 1000000;
  3787. float test_distance = 0;
  3788. map<int32, Spawn*>::iterator itr;
  3789. MSpawnList.readlock(__FUNCTION__, __LINE__);
  3790. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  3791. test_spawn = itr->second;
  3792. if(test_spawn){
  3793. test_distance = test_spawn->GetDistance(spawn);
  3794. if(test_distance < closest_distance){
  3795. closest_distance = test_distance;
  3796. closest_spawn = test_spawn;
  3797. if(closest_distance < 10)
  3798. break;
  3799. }
  3800. }
  3801. }
  3802. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  3803. if(closest_spawn)
  3804. return closest_spawn->GetLocation();
  3805. return 0;
  3806. }
  3807. void ZoneServer::SendQuestUpdates(Client* client, Spawn* spawn){
  3808. if(!client)
  3809. return;
  3810. if(spawn){
  3811. if(client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  3812. SendSpawnChanges(spawn, client, false, true);
  3813. }
  3814. else{
  3815. client->GetCurrentZone()->SendAllSpawnsForVisChange(client);
  3816. }
  3817. }
  3818. void ZoneServer::SendAllSpawnsForLevelChange(Client* client) {
  3819. Spawn* spawn = 0;
  3820. if (spawn_range_map.count(client) > 0) {
  3821. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3822. while (itr.Next()) {
  3823. spawn = GetSpawnByID(itr->first);
  3824. if (spawn && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  3825. SendSpawnChanges(spawn, client, false, true);
  3826. // Attempt to slow down the packet spam sent to the client
  3827. // who the bloody fuck put a Sleep here
  3828. //Sleep(5);
  3829. }
  3830. }
  3831. }
  3832. }
  3833. void ZoneServer::SendAllSpawnsForSeeInvisChange(Client* client) {
  3834. Spawn* spawn = 0;
  3835. if (spawn_range_map.count(client) > 0) {
  3836. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3837. while (itr.Next()) {
  3838. spawn = GetSpawnByID(itr->first);
  3839. if (spawn && spawn->IsEntity() && (((Entity*)spawn)->IsInvis() || ((Entity*)spawn)->IsStealthed()) && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  3840. SendSpawnChanges(spawn, client, true, true);
  3841. }
  3842. }
  3843. }
  3844. }
  3845. void ZoneServer::SendAllSpawnsForVisChange(Client* client, bool limitToEntities) {
  3846. Spawn* spawn = 0;
  3847. if (spawn_range_map.count(client) > 0) {
  3848. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3849. while (itr.Next()) {
  3850. spawn = GetSpawnByID(itr->first);
  3851. if (spawn && (!limitToEntities || (limitToEntities && spawn->IsEntity())) && client->GetPlayer()->WasSentSpawn(spawn->GetID())) {
  3852. SendSpawnChanges(spawn, client, false, true);
  3853. }
  3854. }
  3855. }
  3856. }
  3857. void ZoneServer::StartZoneSpawnsForLevelThread(Client* client){
  3858. if(zoneShuttingDown)
  3859. return;
  3860. #ifdef WIN32
  3861. _beginthread(SendLevelChangedSpawns, 0, client);
  3862. #else
  3863. pthread_t thread;
  3864. pthread_create(&thread, NULL, SendLevelChangedSpawns, client);
  3865. pthread_detach(thread);
  3866. #endif
  3867. }
  3868. void ZoneServer::ReloadClientQuests(){
  3869. Client* client = 0;
  3870. vector<Client*>::iterator client_itr;
  3871. MClientList.readlock(__FUNCTION__, __LINE__);
  3872. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3873. client = *client_itr;
  3874. if(client)
  3875. client->ReloadQuests();
  3876. }
  3877. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3878. }
  3879. void ZoneServer::SendCalculatedXP(Player* player, Spawn* victim){
  3880. if (player && victim) {
  3881. if (player->GetGroupMemberInfo()) {
  3882. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  3883. PlayerGroup* group = world.GetGroupManager()->GetGroup(player->GetGroupMemberInfo()->group_id);
  3884. if (group)
  3885. {
  3886. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  3887. deque<GroupMemberInfo*>* members = group->GetMembers();
  3888. deque<GroupMemberInfo*>::iterator itr;
  3889. bool skipGrayMob = false;
  3890. for (itr = members->begin(); itr != members->end(); itr++) {
  3891. GroupMemberInfo* gmi = *itr;
  3892. if (gmi->client) {
  3893. Player* group_member = gmi->client->GetPlayer();
  3894. if(group_member && group_member->GetArrowColor(victim->GetLevel()) == ARROW_COLOR_GRAY) {
  3895. skipGrayMob = true;
  3896. break;
  3897. }
  3898. }
  3899. }
  3900. for (itr = members->begin(); !skipGrayMob && itr != members->end(); itr++) {
  3901. GroupMemberInfo* gmi = *itr;
  3902. if (gmi->client) {
  3903. Player* group_member = gmi->client->GetPlayer();
  3904. if(group_member) {
  3905. float xp = group_member->CalculateXP(victim) / members->size();
  3906. if (xp > 0) {
  3907. group_member->AddXP((int32)xp);
  3908. }
  3909. }
  3910. }
  3911. }
  3912. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  3913. }
  3914. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  3915. }
  3916. else {
  3917. float xp = player->CalculateXP(victim);
  3918. if (xp > 0) {
  3919. Client* client = GetClientBySpawn(player);
  3920. if(!client)
  3921. return;
  3922. player->AddXP((int32)xp);
  3923. }
  3924. }
  3925. }
  3926. }
  3927. void ZoneServer::ProcessFaction(Spawn* spawn, Client* client)
  3928. {
  3929. if(client && !spawn->IsPlayer() && spawn->GetFactionID() > 10)
  3930. {
  3931. bool update_result = false;
  3932. Faction* faction = 0;
  3933. vector<int32>* factions = 0;
  3934. Player* player = client->GetPlayer();
  3935. bool hasfaction = database.VerifyFactionID(player->GetCharacterID(), spawn->GetFactionID());
  3936. //if 0 they dont have an entry in the db for this faction. if one they do and we can skip it.
  3937. if(hasfaction == 0) {
  3938. //Find out the default for this faction
  3939. sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
  3940. //add the default faction for the player.
  3941. player->SetFactionValue(spawn->GetFactionID(), defaultfaction);
  3942. //save the character so the new default gets written to the db.
  3943. client->Save();
  3944. }
  3945. if(player->GetFactions()->ShouldDecrease(spawn->GetFactionID()))
  3946. {
  3947. update_result = player->GetFactions()->DecreaseFaction(spawn->GetFactionID());
  3948. faction = master_faction_list.GetFaction(spawn->GetFactionID());
  3949. if(faction && update_result)
  3950. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  3951. else if(faction)
  3952. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  3953. factions = master_faction_list.GetHostileFactions(spawn->GetFactionID());
  3954. if(factions)
  3955. {
  3956. vector<int32>::iterator itr;
  3957. for(itr = factions->begin(); itr != factions->end(); itr++)
  3958. {
  3959. if(player->GetFactions()->ShouldIncrease(*itr))
  3960. {
  3961. update_result = player->GetFactions()->IncreaseFaction(*itr);
  3962. faction = master_faction_list.GetFaction(*itr);
  3963. if(faction && update_result)
  3964. client->Message(CHANNEL_FACTION, "Your faction standing with %s got better.", faction->name.c_str());
  3965. else if(faction)
  3966. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any better.", faction->name.c_str());
  3967. }
  3968. }
  3969. }
  3970. }
  3971. factions = master_faction_list.GetFriendlyFactions(spawn->GetFactionID());
  3972. if(factions)
  3973. {
  3974. vector<int32>::iterator itr;
  3975. for(itr = factions->begin(); itr != factions->end(); itr++)
  3976. {
  3977. if(player->GetFactions()->ShouldDecrease(*itr))
  3978. {
  3979. bool hasfaction = database.VerifyFactionID(player->GetCharacterID(),spawn->GetFactionID());
  3980. if(hasfaction == 0) {
  3981. //they do not have the faction. Lets get the default value and feed it in.
  3982. sint32 defaultfaction = master_faction_list.GetDefaultFactionValue(spawn->GetFactionID());
  3983. //add the default faction for the player.
  3984. player->SetFactionValue(spawn->GetFactionID(), defaultfaction);
  3985. }
  3986. update_result = player->GetFactions()->DecreaseFaction(*itr);
  3987. faction = master_faction_list.GetFaction(*itr);
  3988. if(faction && update_result)
  3989. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  3990. else if(faction)
  3991. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  3992. }
  3993. }
  3994. }
  3995. EQ2Packet* outapp = client->GetPlayer()->GetFactions()->FactionUpdate(client->GetVersion());
  3996. if(outapp)
  3997. client->QueuePacket(outapp);
  3998. }
  3999. }
  4000. void ZoneServer::Despawn(Spawn* spawn, int32 timer){
  4001. if (spawn && movementMgr != nullptr) {
  4002. movementMgr->RemoveMob((Entity*)spawn);
  4003. }
  4004. if(!spawn || spawn->IsPlayer())
  4005. return;
  4006. if(spawn->IsEntity())
  4007. ((Entity*)spawn)->InCombat(false);
  4008. if(timer == 0)
  4009. timer = 1;
  4010. AddDeadSpawn(spawn, timer);
  4011. }
  4012. void ZoneServer::KillSpawn(bool spawnListLocked, Spawn* dead, Spawn* killer, bool send_packet, int8 type, int8 damage_type, int16 kill_blow_type)
  4013. {
  4014. bool isSpell = (type == DAMAGE_PACKET_TYPE_SIPHON_SPELL || type == DAMAGE_PACKET_TYPE_SIPHON_SPELL2 ||
  4015. type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE || type == DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG ||
  4016. type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE2 || type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE3);
  4017. MDeadSpawns.readlock(__FUNCTION__, __LINE__);
  4018. if(!dead || this->dead_spawns.count(dead->GetID()) > 0) {
  4019. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  4020. return;
  4021. }
  4022. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  4023. PacketStruct* packet = 0;
  4024. Client* client = 0;
  4025. vector<int32>* encounter = 0;
  4026. bool killer_in_encounter = false;
  4027. if(dead->IsEntity())
  4028. {
  4029. // add any special quest related loot (no_drop_quest_completed)
  4030. if(dead->IsNPC() && killer && killer != dead)
  4031. AddLoot((NPC*)dead, killer);
  4032. ((Entity*)dead)->InCombat(false);
  4033. dead->SetInitialState(16512, false); // This will make aerial npc's fall after death
  4034. dead->SetHP(0);
  4035. dead->SetSpawnType(3);
  4036. dead->appearance.attackable = 0;
  4037. // Remove hate towards dead from all npc's in the zone
  4038. ClearHate((Entity*)dead);
  4039. // Check kill and death procs
  4040. if (killer && dead != killer){
  4041. if (dead->IsEntity())
  4042. ((Entity*)dead)->CheckProcs(PROC_TYPE_DEATH, killer);
  4043. if (killer->IsEntity())
  4044. ((Entity*)killer)->CheckProcs(PROC_TYPE_KILL, dead);
  4045. }
  4046. //Check if caster is alive after death proc called, incase of deathsave
  4047. if (dead->Alive())
  4048. return;
  4049. RemoveSpellTimersFromSpawn(dead, true, !dead->IsPlayer(), true, !isSpell);
  4050. ((Entity*)dead)->IsCasting(false);
  4051. if(dead->IsPlayer())
  4052. {
  4053. ((Player*)dead)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_DEATHS, 1);
  4054. client = GetClientBySpawn(dead);
  4055. ((Entity*)dead)->HandleDeathExperienceDebt(killer);
  4056. if(client) {
  4057. if(client->GetPlayer()->DamageEquippedItems(10, client))
  4058. client->QueuePacket(client->GetPlayer()->GetEquipmentList()->serialize(client->GetVersion(), client->GetPlayer()));
  4059. client->DisplayDeadWindow();
  4060. }
  4061. }
  4062. else if (dead->IsNPC()) {
  4063. encounter = ((NPC*)dead)->Brain()->GetEncounter();
  4064. }
  4065. }
  4066. dead->SetActionState(0);
  4067. dead->SetTempActionState(0);
  4068. // Needs npc to have access to the encounter list for who is allowed to loot
  4069. NPC* chest = 0;
  4070. if (dead->IsNPC() && !((NPC*)dead)->Brain()->PlayerInEncounter()) {
  4071. dead->SetLootCoins(0);
  4072. dead->ClearLoot();
  4073. }
  4074. Spawn* groupMemberAlive = nullptr;
  4075. // If dead has loot attempt to drop a chest
  4076. if (dead->HasLoot()) {
  4077. if(!(groupMemberAlive = dead->IsSpawnGroupMembersAlive(dead))) {
  4078. chest = ((Entity*)dead)->DropChest();
  4079. }
  4080. else {
  4081. switch(dead->GetLootDropType()) {
  4082. case 0:
  4083. // default drop all chest type as a group
  4084. dead->TransferLoot(groupMemberAlive);
  4085. break;
  4086. case 1:
  4087. // this is a primary mob it drops its own loot
  4088. chest = ((Entity*)dead)->DropChest();
  4089. break;
  4090. }
  4091. }
  4092. }
  4093. // If dead is an npc get the encounter and loop through it giving out the rewards, no rewards for pets
  4094. if (dead->IsNPC() && !dead->IsPet() && !dead->IsBot()) {
  4095. Spawn* spawn = 0;
  4096. int8 size = encounter->size();
  4097. for (int8 i = 0; i < encounter->size(); i++) {
  4098. spawn = GetSpawnByID(encounter->at(i), spawnListLocked);
  4099. // set a flag to let us know if the killer is in the encounter
  4100. if (!killer_in_encounter && spawn == killer)
  4101. killer_in_encounter = true;
  4102. if (spawn && spawn->IsPlayer()) {
  4103. // Update players total kill count
  4104. ((Player*)spawn)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_NPC_KILLS, 1);
  4105. // If this was an epic mob kill send the announcement for this player
  4106. if (dead->GetEncounterLevel() >= 10)
  4107. SendEpicMobDeathToGuild((Player*)spawn, dead);
  4108. // Clear hostile spells from the players spell queue
  4109. spellProcess->RemoveSpellFromQueue((Player*)spawn, true);
  4110. // Get the client of the player
  4111. client = GetClientBySpawn(spawn);
  4112. // valid client?
  4113. if (client) {
  4114. // Check for quest kill updates
  4115. client->CheckPlayerQuestsKillUpdate(dead);
  4116. // 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
  4117. if (!dead->IsPlayer() && dead->GetFactionID() > 10)
  4118. ProcessFaction(dead, client);
  4119. // Send xp...this is currently wrong fix it
  4120. if (spawn != dead && ((Player*)spawn)->GetArrowColor(dead->GetLevel()) >= ARROW_COLOR_GREEN) {
  4121. //SendCalculatedXP((Player*)spawn, dead);
  4122. float xp = ((Player*)spawn)->CalculateXP(dead) / size;
  4123. if (xp > 0) {
  4124. ((Player*)spawn)->AddXP((int32)xp);
  4125. }
  4126. }
  4127. }
  4128. }
  4129. // If a chest is being dropped add this spawn to the chest's encounter so they can loot it
  4130. if (chest && spawn && spawn->IsEntity())
  4131. chest->Brain()->AddToEncounter((Entity*)spawn);
  4132. }
  4133. }
  4134. // If a chest is being dropped add it to the world and set the timer to remove it.
  4135. if (chest) {
  4136. AddSpawn(chest);
  4137. AddDeadSpawn(chest, 0xFFFFFFFF);
  4138. LogWrite(LOOT__DEBUG, 0, "Loot", "Adding a chest to the world...");
  4139. }
  4140. // Reset client pointer
  4141. client = 0;
  4142. // Killer was not in the encounter, give them the faction hit but no xp
  4143. if (!killer_in_encounter) {
  4144. // make sure the killer is a player and the dead spawn had a faction and wasn't a player
  4145. if (killer && killer->IsPlayer()) {
  4146. if (!dead->IsPlayer() && dead->GetFactionID() > 10) {
  4147. client = GetClientBySpawn(killer);
  4148. if (client)
  4149. ProcessFaction(dead, client);
  4150. }
  4151. // Clear hostile spells from the killers spell queue
  4152. spellProcess->RemoveSpellFromQueue((Player*)killer, true);
  4153. }
  4154. }
  4155. // Reset client pointer
  4156. client = 0;
  4157. vector<Spawn*>* group = dead->GetSpawnGroup();
  4158. if (group && group->size() == 1)
  4159. CallSpawnScript(dead, SPAWN_SCRIPT_GROUP_DEAD, killer);
  4160. safe_delete(group);
  4161. // Remove the support functions for the dead spawn
  4162. RemoveSpawnSupportFunctions(dead, !isSpell);
  4163. // Erase the expire timer if it has one
  4164. if (spawn_expire_timers.count(dead->GetID()) > 0)
  4165. spawn_expire_timers.erase(dead->GetID());
  4166. // If dead is an npc or object call the spawn scrip and handle instance stuff
  4167. if(dead->IsNPC() || dead->IsObject())
  4168. {
  4169. // handle instance spawn db info
  4170. // we don't care if a NPC or a client kills the spawn, we could have events that cause NPCs to kill NPCs.
  4171. if(dead->GetZone()->GetInstanceID() > 0 && dead->GetSpawnLocationID() > 0)
  4172. {
  4173. // use respawn time to either insert/update entry (likely insert in this situation)
  4174. if(dead->IsNPC())
  4175. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4176. else if ( dead->IsObject ( ) )
  4177. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4178. }
  4179. // Call the spawn scripts death() function
  4180. CallSpawnScript(dead, SPAWN_SCRIPT_DEATH, killer);
  4181. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  4182. if (zone_script && lua_interface)
  4183. lua_interface->RunZoneScript(zone_script, "spawn_killed", this, dead, 0, 0, killer);
  4184. }
  4185. int32 victim_id = dead->GetID();
  4186. int32 attacker_id = 0xFFFFFFFF;
  4187. if(killer)
  4188. attacker_id = killer->GetID();
  4189. if(send_packet)
  4190. {
  4191. vector<Client*>::iterator client_itr;
  4192. MClientList.readlock(__FUNCTION__, __LINE__);
  4193. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4194. client = *client_itr;
  4195. if(!client->GetPlayer()->WasSentSpawn(victim_id) || (attacker_id != 0xFFFFFFFF && !client->GetPlayer()->WasSentSpawn(attacker_id)) )
  4196. continue;
  4197. else if(killer && killer->GetDistance(client->GetPlayer()) > HEAR_SPAWN_DISTANCE)
  4198. continue;
  4199. packet = configReader.getStruct("WS_HearDeath", client->GetVersion());
  4200. if(packet)
  4201. {
  4202. if(killer)
  4203. packet->setDataByName("attacker", client->GetPlayer()->GetIDWithPlayerSpawn(killer));
  4204. else
  4205. packet->setDataByName("attacker", 0xFFFFFFFF);
  4206. packet->setDataByName("defender", client->GetPlayer()->GetIDWithPlayerSpawn(dead));
  4207. packet->setDataByName("damage_type", damage_type);
  4208. packet->setDataByName("blow_type", kill_blow_type);
  4209. client->QueuePacket(packet->serialize());
  4210. LogWrite(COMBAT__DEBUG, 0, "Combat", "Zone Killing '%s'", dead->GetName());
  4211. safe_delete(packet);
  4212. }
  4213. }
  4214. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4215. }
  4216. int32 pop_timer = 0xFFFFFFFF;
  4217. if(killer && killer->IsNPC())
  4218. {
  4219. // Call the spawn scripts killed() function
  4220. CallSpawnScript(killer, SPAWN_SCRIPT_KILLED, dead);
  4221. if(!dead->IsPlayer())
  4222. {
  4223. LogWrite(MISC__TODO, 1, "TODO", "Whenever pets are added, check for pet kills\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  4224. // Set the time for the corpse to linger to 5 sec
  4225. //pop_timer = 5000;
  4226. // commented out the timer so in the event the killer is not a player (pet, guard, something else i haven't thought of)
  4227. // the corpse doesn't vanish sooner then it should if it had loot for the players. AddDeadSpawn() will set timers based on if
  4228. // the corpse has loot or not if the timer value (pop_timer) is 0xFFFFFFFF
  4229. }
  4230. }
  4231. // If the dead spawns was not a player add it to the dead spawn list
  4232. if (!dead->IsPlayer() && !dead->IsBot())
  4233. AddDeadSpawn(dead, pop_timer);
  4234. // if dead was a player clear hostile spells from its spell queue
  4235. if (dead->IsPlayer())
  4236. spellProcess->RemoveSpellFromQueue((Player*)dead, true);
  4237. if (dead->IsNPC())
  4238. ((NPC*)dead)->Brain()->ClearHate();
  4239. // ResetPetInfo() is called in DismissPet(), might not need to be here
  4240. // Players pet is killed, clear the pet info from char sheet
  4241. /*if (dead->IsNPC() && ((NPC*)dead)->IsPet() && ((NPC*)dead)->GetOwner()->IsPlayer())
  4242. ((Player*)((NPC*)dead)->GetOwner())->ResetPetInfo();*/
  4243. safe_delete(encounter);
  4244. }
  4245. void ZoneServer::SendDamagePacket(Spawn* attacker, Spawn* victim, int8 type1, int8 type2, int8 damage_type, int16 damage, const char* spell_name) {
  4246. //Scat: was set but never being used anywhere. i see references to 0xFFFFFFFF below so could be old code not used anymore
  4247. //int32 attacker_id = 0xFFFFFFFF;
  4248. //if(attacker)
  4249. // attacker_id = attacker->GetID();
  4250. PacketStruct* packet = 0;
  4251. Client* client = 0;
  4252. if (attacker && victim && victim->IsPlayer() && victim->GetTarget() == 0) {
  4253. client = GetClientBySpawn(victim);
  4254. if (client)
  4255. client->TargetSpawn(attacker);
  4256. }
  4257. if(damage_type == DAMAGE_PACKET_DAMAGE_TYPE_FOCUS) {
  4258. damage_type = 0;
  4259. type2 = DAMAGE_PACKET_RESULT_FOCUS;
  4260. }
  4261. vector<Client*>::iterator client_itr;
  4262. MClientList.readlock(__FUNCTION__, __LINE__);
  4263. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4264. client = *client_itr;
  4265. if (!client || (client->GetPlayer() != attacker && client->GetPlayer() != victim && ((attacker && client->GetPlayer()->WasSentSpawn(attacker->GetID()) == false) || (victim && client->GetPlayer()->WasSentSpawn(victim->GetID()) == false))))
  4266. continue;
  4267. if (attacker && attacker->GetDistance(client->GetPlayer()) > 50)
  4268. continue;
  4269. if (victim && victim->GetDistance(client->GetPlayer()) > 50)
  4270. continue;
  4271. switch (type1) {
  4272. case DAMAGE_PACKET_TYPE_SIPHON_SPELL:
  4273. case DAMAGE_PACKET_TYPE_SIPHON_SPELL2:
  4274. packet = configReader.getStruct("WS_HearSiphonSpellDamage", client->GetVersion());
  4275. break;
  4276. case DAMAGE_PACKET_TYPE_MULTIPLE_DAMAGE:
  4277. if (client->GetVersion() > 546)
  4278. packet = configReader.getStruct("WS_HearMultipleDamage", client->GetVersion());
  4279. else
  4280. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4281. break;
  4282. case DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG:
  4283. case DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE:
  4284. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4285. break;
  4286. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE2:
  4287. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE3:
  4288. case DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG:
  4289. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE:
  4290. if (client->GetVersion() > 546)
  4291. packet = configReader.getStruct("WS_HearSpellDamage", client->GetVersion());
  4292. else
  4293. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4294. if (packet)
  4295. packet->setSubstructDataByName("header", "unknown", 5);
  4296. break;
  4297. case DAMAGE_PACKET_TYPE_RANGE_DAMAGE:
  4298. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4299. break;
  4300. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG:
  4301. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG2:
  4302. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4303. break;
  4304. default:
  4305. LogWrite(ZONE__ERROR, 0, "Zone", "Unknown Damage Packet type: %i in ZoneServer::SendDamagePacket.", type1);
  4306. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4307. return;
  4308. }
  4309. if (packet) {
  4310. if (client->GetVersion() > 546) {
  4311. packet->setSubstructDataByName("header", "packet_type", type1);
  4312. packet->setSubstructDataByName("header", "result_type", type2);
  4313. packet->setDataByName("damage_type", damage_type);
  4314. packet->setDataByName("damage", damage);
  4315. }
  4316. else {
  4317. switch (type2) {
  4318. case DAMAGE_PACKET_RESULT_MISS:
  4319. packet->setSubstructDataByName("header", "result_type", 1);
  4320. break;
  4321. case DAMAGE_PACKET_RESULT_DODGE:
  4322. packet->setSubstructDataByName("header", "result_type", 2);
  4323. break;
  4324. case DAMAGE_PACKET_RESULT_PARRY:
  4325. packet->setSubstructDataByName("header", "result_type", 3);
  4326. break;
  4327. case DAMAGE_PACKET_RESULT_RIPOSTE:
  4328. packet->setSubstructDataByName("header", "result_type", 4);
  4329. break;
  4330. case DAMAGE_PACKET_RESULT_BLOCK:
  4331. packet->setSubstructDataByName("header", "result_type", 5);
  4332. break;
  4333. case DAMAGE_PACKET_RESULT_INVULNERABLE:
  4334. packet->setSubstructDataByName("header", "result_type", 7);
  4335. break;
  4336. case DAMAGE_PACKET_RESULT_RESIST:
  4337. packet->setSubstructDataByName("header", "result_type", 9);
  4338. break;
  4339. case DAMAGE_PACKET_RESULT_REFLECT:
  4340. packet->setSubstructDataByName("header", "result_type", 10);
  4341. break;
  4342. case DAMAGE_PACKET_RESULT_IMMUNE:
  4343. packet->setSubstructDataByName("header", "result_type", 11);
  4344. break;
  4345. }
  4346. packet->setArrayLengthByName("num_dmg", 1);
  4347. packet->setSubstructDataByName("header", "defender_proxy", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4348. packet->setArrayDataByName("damage_type", damage_type);
  4349. packet->setArrayDataByName("damage", damage);
  4350. }
  4351. if (!attacker)
  4352. packet->setSubstructDataByName("header", "attacker", 0xFFFFFFFF);
  4353. else
  4354. packet->setSubstructDataByName("header", "attacker", client->GetPlayer()->GetIDWithPlayerSpawn(attacker));
  4355. packet->setSubstructDataByName("header", "defender", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4356. if (spell_name) {
  4357. packet->setDataByName("spell", 1);
  4358. packet->setDataByName("spell_name", spell_name);
  4359. }
  4360. EQ2Packet* app = packet->serialize();
  4361. //DumpPacket(app);
  4362. client->QueuePacket(app);
  4363. safe_delete(packet);
  4364. packet = 0;
  4365. }
  4366. }
  4367. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4368. }
  4369. void ZoneServer::SendHealPacket(Spawn* caster, Spawn* target, int16 heal_type, int32 heal_amt, const char* spell_name){
  4370. Client* client = 0;
  4371. vector<Client*>::iterator client_itr;
  4372. MClientList.readlock(__FUNCTION__, __LINE__);
  4373. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4374. client = *client_itr;
  4375. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4376. continue;
  4377. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4378. continue;
  4379. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4380. continue;
  4381. PacketStruct* packet = configReader.getStruct("WS_HearHeal", client->GetVersion());
  4382. if (packet) {
  4383. packet->setDataByName("caster", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4384. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4385. packet->setDataByName("heal_amt", heal_amt);
  4386. packet->setDataByName("spellname", spell_name);
  4387. packet->setDataByName("type", heal_type);
  4388. packet->setDataByName("unknown2", 1);
  4389. EQ2Packet* app = packet->serialize();
  4390. client->QueuePacket(app);
  4391. safe_delete(packet);
  4392. }
  4393. }
  4394. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4395. }
  4396. void ZoneServer::SendThreatPacket(Spawn* caster, Spawn* target, int32 threat_amt, const char* spell_name) {
  4397. Client* client = 0;
  4398. vector<Client*>::iterator client_itr;
  4399. MClientList.readlock(__FUNCTION__, __LINE__);
  4400. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4401. client = *client_itr;
  4402. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4403. continue;
  4404. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4405. continue;
  4406. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4407. continue;
  4408. PacketStruct* packet = configReader.getStruct("WS_HearThreatCmd", client->GetVersion());
  4409. if (packet) {
  4410. packet->setDataByName("spell_name", spell_name);
  4411. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4412. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4413. packet->setDataByName("threat_amount", threat_amt);
  4414. client->QueuePacket(packet->serialize());
  4415. }
  4416. safe_delete(packet);
  4417. }
  4418. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4419. }
  4420. void ZoneServer::SendSpellFailedPacket(Client* client, int16 error){
  4421. if(!client)
  4422. return;
  4423. PacketStruct* packet = configReader.getStruct("WS_DisplaySpellFailed", client->GetVersion());
  4424. if(packet){
  4425. /* Temp solution, need to modify the error code before this function and while we still have access to the spell/combat art */
  4426. error = master_spell_list.GetSpellErrorValue(client->GetVersion(), error);
  4427. packet->setDataByName("error_code", error);
  4428. //packet->PrintPacket();
  4429. client->QueuePacket(packet->serialize());
  4430. safe_delete(packet);
  4431. }
  4432. }
  4433. void ZoneServer::SendInterruptPacket(Spawn* interrupted, LuaSpell* spell, bool fizzle){
  4434. if(!interrupted || !spell)
  4435. return;
  4436. EQ2Packet* outapp = 0;
  4437. PacketStruct* packet = 0;
  4438. Client* client = 0;
  4439. vector<Client*>::iterator client_itr;
  4440. MClientList.readlock(__FUNCTION__, __LINE__);
  4441. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4442. client = *client_itr;
  4443. if(!client || !client->GetPlayer()->WasSentSpawn(interrupted->GetID()))
  4444. continue;
  4445. packet = configReader.getStruct(fizzle ? "WS_SpellFizzle" : "WS_Interrupt", client->GetVersion());
  4446. if(packet){
  4447. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(interrupted));
  4448. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4449. for (int32 i = 0; i < spell->targets.size(); i++)
  4450. packet->setArrayDataByName("target_id", client->GetPlayer()->GetIDWithPlayerSpawn(client->GetPlayer()->GetZone()->GetSpawnByID(spell->targets[i])), i);
  4451. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4452. outapp = packet->serialize();
  4453. client->QueuePacket(outapp);
  4454. safe_delete(packet);
  4455. }
  4456. }
  4457. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4458. safe_delete(packet);
  4459. }
  4460. void ZoneServer::SendCastSpellPacket(LuaSpell* spell, Entity* caster){
  4461. EQ2Packet* outapp = 0;
  4462. PacketStruct* packet = 0;
  4463. Client* client = 0;
  4464. if(!caster || !spell || !spell->spell || spell->interrupted)
  4465. return;
  4466. vector<Client*>::iterator client_itr;
  4467. MClientList.readlock(__FUNCTION__, __LINE__);
  4468. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4469. client = *client_itr;
  4470. if(!client)
  4471. continue;
  4472. packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4473. if(packet){
  4474. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4475. if(!caster_id) {
  4476. safe_delete(packet);
  4477. continue;
  4478. }
  4479. packet->setDataByName("spawn_id", caster_id);
  4480. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4481. for (int32 i = 0; i < spell->targets.size(); i++) {
  4482. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(spell->caster->GetZone()->GetSpawnByID(spell->targets[i]));
  4483. if(target_id) {
  4484. packet->setArrayDataByName("target", target_id, i);
  4485. }
  4486. else {
  4487. packet->setArrayDataByName("target", 0xFFFFFFFF, i);
  4488. }
  4489. }
  4490. packet->setDataByName("spell_visual", spell->spell->GetSpellData()->spell_visual); //result
  4491. packet->setDataByName("cast_time", spell->spell->GetSpellData()->cast_time*.01); //delay
  4492. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4493. packet->setDataByName("spell_level", 1);
  4494. packet->setDataByName("spell_tier", spell->spell->GetSpellData()->tier);
  4495. outapp = packet->serialize();
  4496. client->QueuePacket(outapp);
  4497. safe_delete(packet);
  4498. }
  4499. }
  4500. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4501. safe_delete(packet);
  4502. }
  4503. void ZoneServer::SendCastSpellPacket(int32 spell_visual, Spawn* target, Spawn* caster) {
  4504. if (target) {
  4505. vector<Client*>::iterator client_itr;
  4506. MClientList.readlock(__FUNCTION__, __LINE__);
  4507. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4508. Client* client = *client_itr;
  4509. if (!client)
  4510. continue;
  4511. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4512. if (packet) {
  4513. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4514. if(!target_id) { // client is not aware of spawn
  4515. safe_delete(packet);
  4516. continue;
  4517. }
  4518. if (!caster) {
  4519. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  4520. }
  4521. else {
  4522. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4523. if(!caster_id) { // client is not aware of spawn
  4524. safe_delete(packet);
  4525. continue;
  4526. }
  4527. packet->setDataByName("spawn_id", caster_id);
  4528. }
  4529. packet->setArrayLengthByName("num_targets", 1);
  4530. packet->setArrayDataByName("target", target_id);
  4531. packet->setDataByName("spell_visual", spell_visual);
  4532. packet->setDataByName("cast_time", 0);
  4533. packet->setDataByName("spell_id", 0);
  4534. packet->setDataByName("spell_level", 0);
  4535. packet->setDataByName("spell_tier", 1);
  4536. client->QueuePacket(packet->serialize());
  4537. safe_delete(packet);
  4538. }
  4539. }
  4540. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4541. }
  4542. }
  4543. void ZoneServer::SendCastEntityCommandPacket(EntityCommand* entity_command, int32 spawn_id, int32 target_id) {
  4544. if (entity_command) {
  4545. Spawn* spawn = GetSpawnByID(spawn_id);
  4546. Spawn* target = GetSpawnByID(target_id);
  4547. if (!spawn || !target)
  4548. return;
  4549. Client* client = 0;
  4550. vector<Client*>::iterator client_itr;
  4551. MClientList.readlock(__FUNCTION__, __LINE__);
  4552. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4553. client = *client_itr;
  4554. if (!client || !client->GetPlayer()->WasSentSpawn(spawn_id) || !client->GetPlayer()->WasSentSpawn(target_id))
  4555. continue;
  4556. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4557. if (packet) {
  4558. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  4559. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4560. if(!caster_id || !target_id)
  4561. continue;
  4562. packet->setDataByName("spawn_id", caster_id);
  4563. packet->setArrayLengthByName("num_targets", 1);
  4564. packet->setArrayDataByName("target", target_id);
  4565. packet->setDataByName("num_targets", 1);
  4566. packet->setDataByName("spell_visual", entity_command->spell_visual); //result
  4567. packet->setDataByName("cast_time", entity_command->cast_time * 0.01); //delay
  4568. packet->setDataByName("spell_id", 1);
  4569. packet->setDataByName("spell_level", 1);
  4570. packet->setDataByName("spell_tier", 1);
  4571. EQ2Packet* outapp = packet->serialize();
  4572. client->QueuePacket(outapp);
  4573. safe_delete(packet);
  4574. }
  4575. }
  4576. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4577. }
  4578. }
  4579. void ZoneServer::StartZoneInitialSpawnThread(Client* client){
  4580. if(zoneShuttingDown)
  4581. return;
  4582. #ifdef WIN32
  4583. _beginthread(SendInitialSpawns, 0, client);
  4584. #else
  4585. pthread_t thread;
  4586. pthread_create(&thread, NULL, SendInitialSpawns, client);
  4587. pthread_detach(thread);
  4588. #endif
  4589. }
  4590. void ZoneServer::SendZoneSpawns(Client* client){
  4591. int8 count = 0;
  4592. while (LoadingData && count <= 6000) { //sleep for max of 60 seconds (60000ms) while the maps are loading
  4593. count++;
  4594. Sleep(10);
  4595. }
  4596. count = 0;
  4597. int16 size = 0;
  4598. //give the spawn thread a tad bit of time to add the pending_spawns to spawn_list (up to 10 seconds)
  4599. while (count < 1000) {
  4600. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  4601. size = pending_spawn_list_add.size();
  4602. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  4603. if (size == 0)
  4604. break;
  4605. Sleep(10);
  4606. count++;
  4607. }
  4608. initial_spawn_threads_active++;
  4609. map<int32, Spawn*>::iterator itr;
  4610. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4611. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4612. Spawn* spawn = itr->second;
  4613. if (spawn) {
  4614. if(spawn == client->GetPlayer() && (client->IsReloadingZone() || client->GetPlayer()->IsReturningFromLD()))
  4615. {
  4616. if(!client->GetPlayer()->SetSpawnMap(spawn))
  4617. continue;
  4618. }
  4619. CheckSpawnRange(client, spawn, true);
  4620. }
  4621. }
  4622. CheckSendSpawnToClient(client, true);
  4623. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4624. client->SetConnected(true);
  4625. ClientPacketFunctions::SendFinishedEntitiesList(client);
  4626. initial_spawn_threads_active--;
  4627. }
  4628. vector<Entity*> ZoneServer::GetPlayers(){
  4629. vector<Entity*> ret;
  4630. Client* client = 0;
  4631. vector<Client*>::iterator client_itr;
  4632. MClientList.readlock(__FUNCTION__, __LINE__);
  4633. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4634. client = *client_itr;
  4635. ret.push_back(client->GetPlayer());
  4636. }
  4637. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4638. return ret;
  4639. }
  4640. int16 ZoneServer::SetSpawnTargetable(Spawn* spawn, float distance){
  4641. Spawn* test_spawn = 0;
  4642. int16 ret_val = 0;
  4643. map<int32, Spawn*>::iterator itr;
  4644. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4645. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4646. test_spawn = itr->second;
  4647. if(test_spawn){
  4648. if(test_spawn->GetDistance(spawn) <= distance){
  4649. test_spawn->SetTargetable(1);
  4650. ret_val++;
  4651. }
  4652. }
  4653. }
  4654. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4655. return ret_val;
  4656. }
  4657. int16 ZoneServer::SetSpawnTargetable(int32 spawn_id){
  4658. Spawn* spawn = 0;
  4659. int16 ret_val = 0;
  4660. map<int32, Spawn*>::iterator itr;
  4661. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4662. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4663. spawn = itr->second;
  4664. if(spawn){
  4665. if(spawn->GetDatabaseID() == spawn_id){
  4666. spawn->SetTargetable(1);
  4667. ret_val++;
  4668. }
  4669. }
  4670. }
  4671. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4672. return ret_val;
  4673. }
  4674. 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) {
  4675. ZoneInfoSlideStructInfo* info = new ZoneInfoSlideStructInfo();
  4676. memset(info, 0, sizeof(ZoneInfoSlideStructInfo));
  4677. info->unknown1[0] = unknown1a;
  4678. info->unknown1[1] = unknown1b;
  4679. info->unknown2[0] = unknown2a;
  4680. info->unknown2[1] = unknown2b;
  4681. info->unknown3 = unknown3;
  4682. info->unknown4 = unknown4;
  4683. int8 length = strlen(slide);
  4684. if (length >= 128)
  4685. length = 127;
  4686. strncpy(info->slide, slide, length);
  4687. length = strlen(voiceover);
  4688. if (length >= 128)
  4689. length = 127;
  4690. strncpy(info->voiceover, voiceover, length);
  4691. info->key1 = key1;
  4692. info->key2 = key2;
  4693. ZoneInfoSlideStruct* ret = new ZoneInfoSlideStruct();
  4694. ret->info = info;
  4695. return ret;
  4696. }
  4697. void ZoneServer::AddZoneInfoSlideStructTransitionInfo(ZoneInfoSlideStruct* info, int32 x, int32 y, float zoom, float transition_time) {
  4698. ZoneInfoSlideStructTransitionInfo* transition_info = new ZoneInfoSlideStructTransitionInfo();
  4699. transition_info->transition_x = x;
  4700. transition_info->transition_y = y;
  4701. transition_info->transition_zoom = zoom;
  4702. transition_info->transition_time = transition_time;
  4703. info->slide_transition_info.push_back(transition_info);
  4704. }
  4705. vector<ZoneInfoSlideStruct*>* ZoneServer::GenerateTutorialSlides() {
  4706. vector<ZoneInfoSlideStruct*>* slides = new vector<ZoneInfoSlideStruct*>();
  4707. 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);
  4708. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4709. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 16);
  4710. slides->push_back(slide);
  4711. 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);
  4712. AddZoneInfoSlideStructTransitionInfo(slide, 600, 365, 1.60000002384186, 0);
  4713. AddZoneInfoSlideStructTransitionInfo(slide, 800, 370, 1.39999997615814, 9);
  4714. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 8);
  4715. slides->push_back(slide);
  4716. 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);
  4717. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 0);
  4718. AddZoneInfoSlideStructTransitionInfo(slide, 750, 320, 1.60000002384186, 10);
  4719. AddZoneInfoSlideStructTransitionInfo(slide, 575, 265, 2.29999995231628, 10);
  4720. slides->push_back(slide);
  4721. 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);
  4722. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 2.5, 0);
  4723. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.70000004768372, 8);
  4724. AddZoneInfoSlideStructTransitionInfo(slide, 675, 390, 2.20000004768372, 11);
  4725. slides->push_back(slide);
  4726. 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);
  4727. AddZoneInfoSlideStructTransitionInfo(slide, 750, 500, 2.79999995231628, 0);
  4728. AddZoneInfoSlideStructTransitionInfo(slide, 720, 300, 2.5, 9);
  4729. AddZoneInfoSlideStructTransitionInfo(slide, 975, 270, 2.20000004768372, 9);
  4730. slides->push_back(slide);
  4731. 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);
  4732. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.89999997615814, 0);
  4733. AddZoneInfoSlideStructTransitionInfo(slide, 920, 475, 1, 24);
  4734. slides->push_back(slide);
  4735. 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);
  4736. AddZoneInfoSlideStructTransitionInfo(slide, 1400, 420, 2.40000009536743, 0);
  4737. AddZoneInfoSlideStructTransitionInfo(slide, 1200, 375, 1.70000004768372, 7);
  4738. AddZoneInfoSlideStructTransitionInfo(slide, 800, 225, 2.29999995231628, 7);
  4739. slides->push_back(slide);
  4740. 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);
  4741. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4742. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 27);
  4743. slides->push_back(slide);
  4744. 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);
  4745. AddZoneInfoSlideStructTransitionInfo(slide, 715, 305, 2.40000009536743, 0);
  4746. AddZoneInfoSlideStructTransitionInfo(slide, 730, 325, 1.79999995231628, 6);
  4747. AddZoneInfoSlideStructTransitionInfo(slide, 920, 395, 1.5, 5);
  4748. AddZoneInfoSlideStructTransitionInfo(slide, 1360, 330, 1.79999995231628, 9);
  4749. slides->push_back(slide);
  4750. 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);
  4751. AddZoneInfoSlideStructTransitionInfo(slide, 670, 675, 2.20000004768372, 0);
  4752. AddZoneInfoSlideStructTransitionInfo(slide, 710, 390, 1.79999995231628, 7);
  4753. AddZoneInfoSlideStructTransitionInfo(slide, 920, 415, 1.60000002384186, 5.5);
  4754. AddZoneInfoSlideStructTransitionInfo(slide, 1250, 675, 1.79999995231628, 8);
  4755. slides->push_back(slide);
  4756. 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);
  4757. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4758. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 19);
  4759. slides->push_back(slide);
  4760. 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);
  4761. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 0);
  4762. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 43);
  4763. slides->push_back(slide);
  4764. return slides;
  4765. }
  4766. EQ2Packet* ZoneServer::GetZoneInfoPacket(Client* client){
  4767. // this takes place when we get the LoginInfo for returning LD players
  4768. if(!client->GetPlayer()->IsReturningFromLD())
  4769. UpdateClientSpawnMap(client->GetPlayer(), client);
  4770. PacketStruct* packet = configReader.getStruct("WS_ZoneInfo", client->GetVersion());
  4771. packet->setSmallStringByName("server1",net.GetWorldName());
  4772. packet->setSmallStringByName("server2",net.GetWorldName());
  4773. packet->setDataByName("unknown1", 1, 1);//1, 1
  4774. int32 expansions = EXPANSION_UNKNOWN + EXPANSION_DOF + EXPANSION_KOS + EXPANSION_EOF + EXPANSION_ROK + EXPANSION_TSO + EXPANSION_DOV;
  4775. //packet->setDataByName("expansions_enabled", 82313211);//expansions 63181
  4776. //packet->setDataByName("expansions_enabled", 552075103);//expansions 63182
  4777. packet->setDataByName("expansions_enabled", 4294967295);//expansions 1096 //612499455 works
  4778. if (client->GetVersion() >= 1193) {
  4779. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4780. packet->setDataByName("unknown3", 4294967295, 1); //COE and up
  4781. packet->setDataByName("unknown3", 4294967295, 2);
  4782. }
  4783. else
  4784. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4785. packet->setSmallStringByName("auction_website", "eq2emulator.net");
  4786. packet->setDataByName("auction_port", 80);
  4787. packet->setSmallStringByName("upload_page", "test_upload.m");
  4788. packet->setSmallStringByName("upload_key", "dsya987yda9");
  4789. packet->setSmallStringByName("zone", GetZoneFile());
  4790. //packet->setSmallStringByName("zone2", GetZoneName());
  4791. //if ( strlen(GetZoneSkyFile()) > 0 )
  4792. // packet->setSmallStringByName("zone_unknown2", GetZoneSkyFile()); // used for the sky map
  4793. packet->setSmallStringByName("zone_desc", GetZoneDescription());
  4794. packet->setSmallStringByName("char_name", client->GetPlayer()->GetName());
  4795. packet->setDataByName("x", client->GetPlayer()->GetX());
  4796. packet->setDataByName("y", client->GetPlayer()->GetY());
  4797. packet->setDataByName("z", client->GetPlayer()->GetZ());
  4798. 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)
  4799. vector<ZoneInfoSlideStruct*>* slides = GenerateTutorialSlides();
  4800. if (slides) {
  4801. packet->setArrayLengthByName("num_slides", slides->size());
  4802. ZoneInfoSlideStruct* slide = 0;
  4803. for (int8 i = 0; i < slides->size(); i++) {
  4804. slide = slides->at(i);
  4805. packet->setArrayDataByName("unknown1", slide->info->unknown1[0], i, 0);
  4806. packet->setArrayDataByName("unknown1", slide->info->unknown1[1], i, 1);
  4807. packet->setArrayDataByName("unknown2", slide->info->unknown2[0], i, 0);
  4808. packet->setArrayDataByName("unknown2", slide->info->unknown2[1], i, 1);
  4809. packet->setArrayDataByName("unknown3", slide->info->unknown3, i);
  4810. packet->setArrayDataByName("unknown4", slide->info->unknown4, i);
  4811. packet->setArrayDataByName("slide", slide->info->slide, i);
  4812. packet->setArrayDataByName("voiceover", slide->info->voiceover, i);
  4813. packet->setArrayDataByName("key1", slide->info->key1, i);
  4814. packet->setArrayDataByName("key2", slide->info->key2, i);
  4815. packet->setSubArrayLengthByName("num_transitions", slide->slide_transition_info.size(), i);
  4816. for (int8 x = 0; x < slide->slide_transition_info.size(); x++) {
  4817. packet->setSubArrayDataByName("transition_x", slide->slide_transition_info[x]->transition_x, i, x);
  4818. packet->setSubArrayDataByName("transition_y", slide->slide_transition_info[x]->transition_y, i, x);
  4819. packet->setSubArrayDataByName("transition_zoom", slide->slide_transition_info[x]->transition_zoom, i, x);
  4820. packet->setSubArrayDataByName("transition_time", slide->slide_transition_info[x]->transition_time, i, x);
  4821. safe_delete(slide->slide_transition_info[x]);
  4822. }
  4823. safe_delete(slide->info);
  4824. safe_delete(slide);
  4825. }
  4826. }
  4827. safe_delete(slides);
  4828. }
  4829. if(rule_manager.GetGlobalRule(R_Zone, UseMapUnderworldCoords)->GetBool() && client->GetPlayer()->GetMap()) {
  4830. packet->setDataByName("underworld", client->GetPlayer()->GetMap()->GetMinY() + rule_manager.GetGlobalRule(R_Zone, MapUnderworldCoordOffset)->GetFloat());
  4831. }
  4832. else {
  4833. packet->setDataByName("underworld", underworld);
  4834. }
  4835. // unknown3 can prevent screen shots from being taken if
  4836. //packet->setDataByName("unknown3", 2094661567, 1); // Screenshots allowed with this value
  4837. //packet->setDataByName("unknown3", 3815767999, 1); // Screenshots disabled with this value
  4838. //packet->setDataByName("unknown3", 1, 2);
  4839. /*if (client->GetVersion() >= 63587) {
  4840. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4841. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4842. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4843. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4844. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4845. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4846. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4847. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4848. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4849. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4850. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4851. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4852. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4853. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4854. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4855. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4856. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4857. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4858. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4859. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4860. }
  4861. else if (client->GetVersion() >= 63214) {
  4862. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4863. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4864. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4865. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4866. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4867. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4868. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4869. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4870. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4871. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4872. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4873. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4874. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4875. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4876. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4877. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4878. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4879. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4880. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4881. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4882. }*/
  4883. if (client->GetVersion() >= 64644) {
  4884. packet->setDataByName("unknown3a", 12598924);
  4885. packet->setDataByName("unknown3b", 3992452959);
  4886. packet->setDataByName("unknown3c", 4294967183);
  4887. packet->setDataByName("unknown2a", 9);
  4888. packet->setDataByName("unknown2b", 9);
  4889. }
  4890. else if (client->GetVersion() >= 63181) {
  4891. packet->setDataByName("unknown3a", 750796556);//63182 73821356
  4892. packet->setDataByName("unknown3b", 3991404383);// 63182 3991404383
  4893. packet->setDataByName("unknown3c", 4278189967);// 63182 4278189967
  4894. packet->setDataByName("unknown2a", 8);// 63182
  4895. packet->setDataByName("unknown2b", 8);// 63182
  4896. }
  4897. else{
  4898. //packet->setDataByName("unknown3", 872447025,0);//63181
  4899. //packet->setDataByName("unknown3", 3085434875,1);// 63181
  4900. //packet->setDataByName("unknown3", 2147483633,2);// 63181
  4901. }
  4902. packet->setDataByName("year", world.GetWorldTimeStruct()->year);
  4903. packet->setDataByName("month", world.GetWorldTimeStruct()->month);
  4904. packet->setDataByName("day", world.GetWorldTimeStruct()->day);
  4905. packet->setDataByName("hour", world.GetWorldTimeStruct()->hour);
  4906. packet->setDataByName("minute", world.GetWorldTimeStruct()->minute);
  4907. packet->setDataByName("unknown", 0);
  4908. packet->setDataByName("unknown7", 1);
  4909. packet->setDataByName("unknown7", 1, 1);
  4910. packet->setDataByName("unknown9", 13);
  4911. //packet->setDataByName("unknown10", 25188959);4294967295
  4912. //packet->setDataByName("unknown10", 25190239);
  4913. packet->setDataByName("unknown10", 25191524);//25191524
  4914. packet->setDataByName("unknown10b", 1);
  4915. 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
  4916. packet->setDataByName("num_adv", 9);
  4917. packet->setArrayDataByName("adv_name", "adv02_dun_drowned_caverns", 0);
  4918. packet->setArrayDataByName("adv_id", 6, 0);
  4919. packet->setArrayDataByName("adv_name", "adv02_dun_sundered_splitpaw_hub", 1);
  4920. packet->setArrayDataByName("adv_id", 5, 1);
  4921. packet->setArrayDataByName("adv_name", "exp03_rgn_butcherblock", 2);
  4922. packet->setArrayDataByName("adv_id", 8, 2);
  4923. packet->setArrayDataByName("adv_name", "exp03_rgn_greater_faydark", 3);
  4924. packet->setArrayDataByName("adv_id", 7, 3);
  4925. packet->setArrayDataByName("adv_name", "mod01_dun_crypt_of_thaen", 4);
  4926. packet->setArrayDataByName("adv_id", 3, 4);
  4927. packet->setArrayDataByName("adv_name", "mod01_dun_tombs_of_night", 5);
  4928. packet->setArrayDataByName("adv_id", 4, 5);
  4929. packet->setArrayDataByName("adv_name", "nektulos_mini01", 6);
  4930. packet->setArrayDataByName("adv_id", 0, 6);
  4931. packet->setArrayDataByName("adv_name", "nektulos_mini02", 7);
  4932. packet->setArrayDataByName("adv_id", 1, 7);
  4933. packet->setArrayDataByName("adv_name", "nektulos_mini03", 8);
  4934. packet->setArrayDataByName("adv_id", 2, 8);
  4935. LogWrite(MISC__TODO, 0, "TODO", "Put cl_ client commands back in variables (not Rules) so they can be dynamically maintained");
  4936. vector<Variable*>* variables = world.GetClientVariables();
  4937. packet->setArrayLengthByName("num_client_setup", variables->size());
  4938. for(int i=variables->size()-1;i>=0;i--)
  4939. packet->setArrayDataByName("client_cmds", variables->at(i)->GetNameValuePair().c_str(), i);
  4940. // For AoM clients so item link work
  4941. if (client->GetVersion() >= 60114)
  4942. packet->setArrayDataByName("client_cmds", "chat_linkbrackets_item 1", variables->size());
  4943. safe_delete(variables);
  4944. //packet->setDataByName("unknown8", ); story?
  4945. // AA Tabs for 1193+ clients
  4946. if (client->GetVersion() >= 1193) {
  4947. packet->setArrayLengthByName("tab_count", 48);
  4948. int8 i = 0;
  4949. packet->setArrayDataByName("tab_index", i, i);
  4950. packet->setArrayDataByName("tab_name", ":es24a58bd8fcaac8c2:All", i);
  4951. i++;
  4952. packet->setArrayDataByName("tab_index", i, i);
  4953. packet->setArrayDataByName("tab_name", ":410385c727bd47a6:Racial Innate", i);
  4954. i++;
  4955. packet->setArrayDataByName("tab_index", i, i);
  4956. packet->setArrayDataByName("tab_name", ":410385c75a96e23c:Tradeskill Advancement", i);
  4957. i++;
  4958. packet->setArrayDataByName("tab_index", i, i);
  4959. packet->setArrayDataByName("tab_name", ":410385c744f1fd99:Focus Effects", i);
  4960. i++;
  4961. packet->setArrayDataByName("tab_index", i, i);
  4962. packet->setArrayDataByName("tab_name", ":410385c71edd2a66:Heroic", i);
  4963. i++;
  4964. packet->setArrayDataByName("tab_index", i, i);
  4965. packet->setArrayDataByName("tab_name", ":410385c76ee6239f:Shadows", i);
  4966. i++;
  4967. packet->setArrayDataByName("tab_index", i, i);
  4968. packet->setArrayDataByName("tab_name", ":410385c7e678b977:Prestige", i);
  4969. i++;
  4970. packet->setArrayDataByName("tab_index", i, i);
  4971. packet->setArrayDataByName("tab_name", ":410385c77ee422d7:Animalist", i);
  4972. i++;
  4973. packet->setArrayDataByName("tab_index", i, i);
  4974. packet->setArrayDataByName("tab_name", ":410385c7f165af77:Bard", i);
  4975. i++;
  4976. packet->setArrayDataByName("tab_index", i, i);
  4977. packet->setArrayDataByName("tab_name", ":410385c7421b9375:Brawler", i);
  4978. i++;
  4979. packet->setArrayDataByName("tab_index", i, i);
  4980. packet->setArrayDataByName("tab_name", ":410385c7a03ae7d1:Cleric", i);
  4981. i++;
  4982. packet->setArrayDataByName("tab_index", i, i);
  4983. packet->setArrayDataByName("tab_name", ":410385c7c9605e9f:Crusader", i);
  4984. i++;
  4985. packet->setArrayDataByName("tab_index", i, i);
  4986. packet->setArrayDataByName("tab_name", ":410385c7f9424168:Druid", i);
  4987. i++;
  4988. packet->setArrayDataByName("tab_index", i, i);
  4989. packet->setArrayDataByName("tab_name", ":410385c79cb9556c:Enchanter", i);
  4990. i++;
  4991. packet->setArrayDataByName("tab_index", i, i);
  4992. packet->setArrayDataByName("tab_name", ":410385c70c8b6aa4:Predator", i);
  4993. i++;
  4994. packet->setArrayDataByName("tab_index", i, i);
  4995. packet->setArrayDataByName("tab_name", ":410385c73a43b6dd:Rogue", i);
  4996. i++;
  4997. packet->setArrayDataByName("tab_index", i, i);
  4998. packet->setArrayDataByName("tab_name", ":410385c759fe7d15:Sorcerer", i);
  4999. i++;
  5000. packet->setArrayDataByName("tab_index", i, i);
  5001. packet->setArrayDataByName("tab_name", ":410385c7ad610aca:Summoner", i);
  5002. i++;
  5003. packet->setArrayDataByName("tab_index", i, i);
  5004. packet->setArrayDataByName("tab_name", ":410385c71e056728:Warrior", i);
  5005. i++;
  5006. packet->setArrayDataByName("tab_index", i, i);
  5007. packet->setArrayDataByName("tab_name", ":410385c7ba864c0b:Assassin", i);
  5008. i++;
  5009. packet->setArrayDataByName("tab_index", i, i);
  5010. packet->setArrayDataByName("tab_name", ":410385c7b8116aad:Beastlord", i);
  5011. i++;
  5012. packet->setArrayDataByName("tab_index", i, i);
  5013. packet->setArrayDataByName("tab_name", ":410385c7f53feb7b:Berserker", i);
  5014. i++;
  5015. packet->setArrayDataByName("tab_index", i, i);
  5016. packet->setArrayDataByName("tab_name", ":410385c73d8a70e2:Brigand", i);
  5017. i++;
  5018. packet->setArrayDataByName("tab_index", i, i);
  5019. packet->setArrayDataByName("tab_name", ":410385c770c766d6:Bruiser", i);
  5020. i++;
  5021. packet->setArrayDataByName("tab_index", i, i);
  5022. packet->setArrayDataByName("tab_name", ":410385c79226984b:Coercer", i);
  5023. i++;
  5024. packet->setArrayDataByName("tab_index", i, i);
  5025. packet->setArrayDataByName("tab_name", ":410385c70c58bb30:Conjurer", i);
  5026. i++;
  5027. packet->setArrayDataByName("tab_index", i, i);
  5028. packet->setArrayDataByName("tab_name", ":410385c73dfe68d0:Defiler", i);
  5029. i++;
  5030. packet->setArrayDataByName("tab_index", i, i);
  5031. packet->setArrayDataByName("tab_name", ":410385c792919a6b:Dirge", i);
  5032. i++;
  5033. packet->setArrayDataByName("tab_index", i, i);
  5034. packet->setArrayDataByName("tab_name", ":410385c7062e5f55:Fury", i);
  5035. i++;
  5036. packet->setArrayDataByName("tab_index", i, i);
  5037. packet->setArrayDataByName("tab_name", ":410385c762c1fdfc:Guardian", i);
  5038. i++;
  5039. packet->setArrayDataByName("tab_index", i, i);
  5040. packet->setArrayDataByName("tab_name", ":410385c78addfbf4:Illusionist", i);
  5041. i++;
  5042. packet->setArrayDataByName("tab_index", i, i);
  5043. packet->setArrayDataByName("tab_name", ":410385c7ece054a7:Inquisitor", i);
  5044. i++;
  5045. packet->setArrayDataByName("tab_index", i, i);
  5046. packet->setArrayDataByName("tab_name", ":410385c7d550d2e7:Monk", i);
  5047. i++;
  5048. packet->setArrayDataByName("tab_index", i, i);
  5049. packet->setArrayDataByName("tab_name", ":410385c743cfeaa2:Mystic", i);
  5050. i++;
  5051. packet->setArrayDataByName("tab_index", i, i);
  5052. packet->setArrayDataByName("tab_name", ":410385c7f63c9c8c:Necromancer", i);
  5053. i++;
  5054. packet->setArrayDataByName("tab_index", i, i);
  5055. packet->setArrayDataByName("tab_name", ":410385c70c5de0ae:Paladin", i);
  5056. i++;
  5057. packet->setArrayDataByName("tab_index", i, i);
  5058. packet->setArrayDataByName("tab_name", ":410385c79bc97b3a:Ranger", i);
  5059. i++;
  5060. packet->setArrayDataByName("tab_index", i, i);
  5061. packet->setArrayDataByName("tab_name", ":410385c78fbd2256:Shadowknight", i);
  5062. i++;
  5063. packet->setArrayDataByName("tab_index", i, i);
  5064. packet->setArrayDataByName("tab_name", ":410385c7781cc625:Shaman", i);
  5065. i++;
  5066. packet->setArrayDataByName("tab_index", i, i);
  5067. packet->setArrayDataByName("tab_name", ":410385c77eecdcdb:Swashbuckler", i);
  5068. i++;
  5069. packet->setArrayDataByName("tab_index", i, i);
  5070. packet->setArrayDataByName("tab_name", ":410385c7648d181e:Templar", i);
  5071. i++;
  5072. packet->setArrayDataByName("tab_index", i, i);
  5073. packet->setArrayDataByName("tab_name", ":410385c78df47d77:Troubador", i);
  5074. i++;
  5075. packet->setArrayDataByName("tab_index", i, i);
  5076. packet->setArrayDataByName("tab_name", ":410385c7c78ce0b8:Warden", i);
  5077. i++;
  5078. packet->setArrayDataByName("tab_index", i, i);
  5079. packet->setArrayDataByName("tab_name", ":410385c76290dcfa:Warlock", i);
  5080. i++;
  5081. packet->setArrayDataByName("tab_index", i, i);
  5082. packet->setArrayDataByName("tab_name", ":410385c7d1d52cf5:Wizard", i);
  5083. i++;
  5084. packet->setArrayDataByName("tab_index", i, i);
  5085. packet->setArrayDataByName("tab_name", ":410385c71c8f6f4d:Shaper", i);
  5086. i++;
  5087. packet->setArrayDataByName("tab_index", i, i);
  5088. packet->setArrayDataByName("tab_name", ":410385c72f6e354d:Channeler", i);
  5089. i++;
  5090. packet->setArrayDataByName("tab_index", i, i);
  5091. packet->setArrayDataByName("tab_name", ":410385c7df8bd37d:Dragon", i);
  5092. }
  5093. packet->setDataByName("unknown_mj", 1);//int8
  5094. packet->setDataByName("unknown_mj1", 335544320);//int32
  5095. packet->setDataByName("unknown_mj2", 4);//int32
  5096. packet->setDataByName("unknown_mj3", 3962504088);//int32
  5097. packet->setDataByName("unknown_mj4", 3985947216);//int32
  5098. packet->setDataByName("unknown_mj5", 1);//int32
  5099. packet->setDataByName("unknown_mj6", 386);//int32
  5100. packet->setDataByName("unknown_mj7", 4294967295);//int32
  5101. packet->setDataByName("unknown_mj8", 2716312211);//int32
  5102. packet->setDataByName("unknown_mj9", 1774338333);//int32
  5103. packet->setDataByName("unknown_mj10", 1);//int32
  5104. packet->setDataByName("unknown_mj11", 391);//int32
  5105. packet->setDataByName("unknown_mj12", 4294967295);//int32
  5106. packet->setDataByName("unknown_mj13", 3168965163);//int32
  5107. packet->setDataByName("unknown_mj14", 4117025286);//int32
  5108. packet->setDataByName("unknown_mj15", 1);//int32
  5109. packet->setDataByName("unknown_mj16", 394);//int32
  5110. packet->setDataByName("unknown_mj17", 4294967295);//int32
  5111. packet->setDataByName("unknown_mj18", 1790669110);//int32
  5112. packet->setDataByName("unknown_mj19", 107158108);//int32
  5113. packet->setDataByName("unknown_mj20", 1);//int32
  5114. packet->setDataByName("unknown_mj21", 393);//int32
  5115. packet->setDataByName("unknown_mj22", 4294967295);//int32
  5116. EQ2Packet* outapp = packet->serialize();
  5117. //packet->PrintPacket();
  5118. //DumpPacket(outapp);
  5119. safe_delete(packet);
  5120. return outapp;
  5121. }
  5122. void ZoneServer::SendUpdateDefaultCommand(Spawn* spawn, const char* command, float distance, Spawn* toPlayer){
  5123. if (spawn == nullptr || command == nullptr)
  5124. return;
  5125. if (toPlayer)
  5126. {
  5127. if (!toPlayer->IsPlayer())
  5128. return;
  5129. Client* client = GetClientBySpawn(toPlayer);
  5130. if (client)
  5131. {
  5132. client->SendDefaultCommand(spawn, command, distance);
  5133. }
  5134. // we don't override the primary command cause that would change ALL clients
  5135. return;
  5136. }
  5137. QueueDefaultCommand(spawn->GetID(), std::string(command), distance);
  5138. if (strlen(command)>0)
  5139. spawn->SetPrimaryCommand(command, command, distance);
  5140. }
  5141. void ZoneServer::CheckPlayerProximity(Spawn* spawn, Client* client){
  5142. if (player_proximities.size() < 1)
  5143. return;
  5144. if(player_proximities.count(spawn->GetID()) > 0){
  5145. PlayerProximity* prox = player_proximities.Get(spawn->GetID());
  5146. 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){
  5147. prox->clients_in_proximity[client] = true;
  5148. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->in_range_lua_function.c_str());
  5149. }
  5150. 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){
  5151. if(prox->leaving_range_lua_function.length() > 0)
  5152. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->leaving_range_lua_function.c_str());
  5153. prox->clients_in_proximity.erase(client);
  5154. }
  5155. }
  5156. }
  5157. void ZoneServer::AddPlayerProximity(Spawn* spawn, float distance, string in_range_function, string leaving_range_function){
  5158. RemovePlayerProximity(spawn);
  5159. PlayerProximity* prox = new PlayerProximity;
  5160. prox->distance = distance;
  5161. prox->in_range_lua_function = in_range_function;
  5162. prox->leaving_range_lua_function = leaving_range_function;
  5163. player_proximities.Put(spawn->GetID(), prox);
  5164. }
  5165. void ZoneServer::RemovePlayerProximity(Client* client){
  5166. PlayerProximity* prox = 0;
  5167. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5168. while(itr.Next()){
  5169. prox = itr->second;
  5170. if(prox->clients_in_proximity.count(client) > 0)
  5171. prox->clients_in_proximity.erase(client);
  5172. }
  5173. }
  5174. void ZoneServer::RemovePlayerProximity(Spawn* spawn, bool all){
  5175. if(all){
  5176. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5177. while(itr.Next()){
  5178. player_proximities.erase(itr->first, false, true, 10000);
  5179. }
  5180. }
  5181. else if(player_proximities.count(spawn->GetID()) > 0){
  5182. player_proximities.erase(spawn->GetID(), false, true, 10000);
  5183. }
  5184. }
  5185. void ZoneServer::AddLocationProximity(float x, float y, float z, float max_variation, string in_range_function, string leaving_range_function) {
  5186. LocationProximity* prox = new LocationProximity;
  5187. prox->x = x;
  5188. prox->y = y;
  5189. prox->z = z;
  5190. prox->max_variation = max_variation;
  5191. prox->in_range_lua_function = in_range_function;
  5192. prox->leaving_range_lua_function = leaving_range_function;
  5193. location_proximities.Add(prox);
  5194. }
  5195. void ZoneServer::CheckLocationProximity() {
  5196. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  5197. if (!zone_script)
  5198. return;
  5199. if (location_proximities.size() > 0 && connected_clients.size() > 0) {
  5200. Client* client = 0;
  5201. MutexList<Client*>::iterator iterator = connected_clients.begin();
  5202. while(iterator.Next()){
  5203. client = iterator->value;
  5204. if (client->IsConnected() && client->IsReadyForUpdates() && !client->IsZoning()) {
  5205. try {
  5206. MutexList<LocationProximity*>::iterator itr = location_proximities.begin();
  5207. LocationProximity* prox = 0;
  5208. while(itr.Next()){
  5209. prox = itr->value;
  5210. bool in_range = false;
  5211. float char_x = client->GetPlayer()->GetX();
  5212. float char_y = client->GetPlayer()->GetY();
  5213. float char_z = client->GetPlayer()->GetZ();
  5214. float x = prox->x;
  5215. float y = prox->y;
  5216. float z = prox->z;
  5217. float max_variation = prox->max_variation;
  5218. float total_diff = 0;
  5219. float diff = x - char_x; //Check X
  5220. if(diff < 0)
  5221. diff *= -1;
  5222. if(diff <= max_variation) {
  5223. total_diff += diff;
  5224. diff = z - char_z; //Check Z (we check Z first because it is far more likely to be a much greater variation than y)
  5225. if(diff < 0)
  5226. diff *= -1;
  5227. if(diff <= max_variation) {
  5228. total_diff += diff;
  5229. if(total_diff <= max_variation) { //Check Total
  5230. diff = y - char_y; //Check Y
  5231. if(diff < 0)
  5232. diff *= -1;
  5233. if(diff <= max_variation) {
  5234. total_diff += diff;
  5235. if(total_diff <= max_variation) {
  5236. in_range = true;
  5237. if(lua_interface && prox->in_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) == 0) { //Check Total
  5238. prox->clients_in_proximity[client] = true;
  5239. lua_interface->RunZoneScript(zone_script, prox->in_range_lua_function.c_str(), this, client->GetPlayer());
  5240. }
  5241. }
  5242. }
  5243. }
  5244. }
  5245. }
  5246. if (!in_range) {
  5247. if(lua_interface && prox->leaving_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) > 0) {
  5248. lua_interface->RunZoneScript(zone_script, prox->leaving_range_lua_function.c_str(), this, client->GetPlayer());
  5249. prox->clients_in_proximity.erase(client);
  5250. }
  5251. }
  5252. }
  5253. }
  5254. catch (...) {
  5255. LogWrite(ZONE__ERROR, 0, "Zone", "Except caught in ZoneServer::CheckLocationProximity");
  5256. return;
  5257. }
  5258. }
  5259. }
  5260. }
  5261. }
  5262. void ZoneServer::CheckLocationGrids() {
  5263. if (connected_clients.size() > 0 && location_grids.size() > 0) {
  5264. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5265. while (client_itr.Next()) {
  5266. Client* client = client_itr.value;
  5267. if (!client)
  5268. continue;
  5269. Player* player = client->GetPlayer();
  5270. float x = player->GetX();
  5271. float y = player->GetY();
  5272. float z = player->GetZ();
  5273. int32 grid_id = player->GetLocation();
  5274. MutexList<LocationGrid*>::iterator location_grid_itr = location_grids.begin();
  5275. while (location_grid_itr.Next()) {
  5276. LocationGrid* grid = location_grid_itr.value;
  5277. if (grid->locations.size() > 0 || /*(grid->grid_id == grid_id ||*/ grid->players.count(player) > 0) {
  5278. float x_small = 0;
  5279. float x_large = 0;
  5280. float y_small = 0;
  5281. float y_large = 0;
  5282. float z_small = 0;
  5283. float z_large = 0;
  5284. bool first = true;
  5285. bool in_grid = false;
  5286. MutexList<Location*>::iterator location_itr = grid->locations.begin();
  5287. while (location_itr.Next()) {
  5288. Location* location = location_itr.value;
  5289. if (first) {
  5290. x_small = location->x;
  5291. x_large = location->x;
  5292. if (grid->include_y) {
  5293. y_small = location->y;
  5294. y_large = location->y;
  5295. }
  5296. z_small = location->z;
  5297. z_large = location->z;
  5298. first = false;
  5299. }
  5300. else {
  5301. if (location->x < x_small)
  5302. x_small = location->x;
  5303. else if (location->x > x_large)
  5304. x_large = location->x;
  5305. if (grid->include_y) {
  5306. if (location->y < y_small)
  5307. y_small = location->y;
  5308. else if (location->y > y_large)
  5309. y_large = location->y;
  5310. }
  5311. if (location->z < z_small)
  5312. z_small = location->z;
  5313. else if (location->z > z_large)
  5314. z_large = location->z;
  5315. }
  5316. }
  5317. if (grid->include_y && (x >= x_small && x <= x_large && y >= y_small && y <= y_large && z >= z_small && z <= z_large))
  5318. in_grid = true;
  5319. else if (x >= x_small && x <= x_large && z >= z_small && z <= z_large)
  5320. in_grid = true;
  5321. if (in_grid && grid->players.count(player) == 0) {
  5322. grid->players.Put(player, true);
  5323. bool show_enter_location_popup = true;
  5324. bool discovery_enabled = rule_manager.GetGlobalRule(R_World, EnablePOIDiscovery)->GetBool();
  5325. if( grid->discovery && discovery_enabled && !player->DiscoveredLocation(grid->id) )
  5326. {
  5327. // check if player has already discovered this location
  5328. // if not, process new discovery
  5329. char tmp[200] = {0};
  5330. sprintf(tmp, "\\#FFE400You have discovered\12\\#FFF283%s", grid->name.c_str());
  5331. client->SendPopupMessage(11, tmp, "ui_discovery", 2.25, 0xFF, 0xFF, 0xFF);
  5332. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' discovered location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5333. player->UpdatePlayerHistory(HISTORY_TYPE_DISCOVERY, HISTORY_SUBTYPE_LOCATION, grid->id);
  5334. show_enter_location_popup = false;
  5335. // else, print standard location entry
  5336. }
  5337. if( show_enter_location_popup )
  5338. {
  5339. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' entering location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5340. client->SendPopupMessage(10, grid->name.c_str(), 0, 2.5, 255, 255, 0);
  5341. }
  5342. }
  5343. else if (!in_grid && grid->players.count(player) > 0) {
  5344. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' leaving location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5345. grid->players.erase(player);
  5346. }
  5347. }
  5348. }
  5349. }
  5350. }
  5351. }
  5352. // Called from a command (client, main zone thread) and the main zone thread
  5353. // so no need for a mutex container
  5354. void ZoneServer::AddLocationGrid(LocationGrid* grid) {
  5355. if (grid)
  5356. location_grids.Add(grid);
  5357. }
  5358. void ZoneServer::RemoveLocationGrids() {
  5359. MutexList<LocationGrid*>::iterator itr = location_grids.begin();
  5360. while (itr.Next())
  5361. itr.value->locations.clear(true);
  5362. location_grids.clear(true);
  5363. }
  5364. void ZoneServer::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast, bool call_expire_function, bool lock_spell_process){
  5365. if(spellProcess)
  5366. spellProcess->RemoveSpellTimersFromSpawn(spawn, remove_all, delete_recast, call_expire_function, lock_spell_process);
  5367. }
  5368. void ZoneServer::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement){
  5369. if(spellProcess)
  5370. spellProcess->Interrupted(caster, interruptor, error_code, cancel, from_movement);
  5371. }
  5372. Spell* ZoneServer::GetSpell(Entity* caster){
  5373. Spell* spell = 0;
  5374. if(spellProcess)
  5375. spell = spellProcess->GetSpell(caster);
  5376. return spell;
  5377. }
  5378. void ZoneServer::ProcessSpell(Spell* spell, Entity* caster, Spawn* target, bool lock, bool harvest_spell, LuaSpell* customSpell, int16 custom_cast_time, bool in_heroic_opp){
  5379. if(spellProcess)
  5380. spellProcess->ProcessSpell(this, spell, caster, target, lock, harvest_spell, customSpell, custom_cast_time, in_heroic_opp);
  5381. }
  5382. void ZoneServer::ProcessEntityCommand(EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock) {
  5383. if (target && target->GetSpawnScript()) {
  5384. Player* player = 0;
  5385. if (caster && caster->IsPlayer())
  5386. player = (Player*)caster;
  5387. CallSpawnScript(target, SPAWN_SCRIPT_CUSTOM, player, entity_command->command.c_str());
  5388. }
  5389. if (spellProcess)
  5390. spellProcess->ProcessEntityCommand(this, entity_command, caster, target, lock);
  5391. }
  5392. void ZoneServer::RemoveSpawnSupportFunctions(Spawn* spawn, bool lock_spell_process) {
  5393. if(!spawn)
  5394. return;
  5395. if(spawn->IsPlayer() && spawn->GetZone())
  5396. spawn->GetZone()->RemovePlayerPassenger(((Player*)spawn)->GetCharacterID());
  5397. if(spawn->IsEntity())
  5398. RemoveSpellTimersFromSpawn((Entity*)spawn, true, true, true, lock_spell_process);
  5399. RemoveDamagedSpawn(spawn);
  5400. spawn->SendSpawnChanges(false);
  5401. RemoveChangedSpawn(spawn);
  5402. // Everything inside this if will be nuked during a reload in other spots, no need to do it twice
  5403. if (!reloading) {
  5404. RemoveDeadEnemyList(spawn);
  5405. spawn->changed = true;
  5406. spawn->info_changed = true;
  5407. spawn->vis_changed = true;
  5408. spawn->position_changed = true;
  5409. SendSpawnChanges(spawn);
  5410. if (spawn->GetSpawnGroupID() > 0) {
  5411. int32 group_id = spawn->GetSpawnGroupID();
  5412. spawn->RemoveSpawnFromGroup();
  5413. if (spawn_group_map.count(group_id) > 0)
  5414. spawn_group_map.Get(group_id).Remove(spawn->GetID());
  5415. }
  5416. if (!spawn->IsPlayer()) {
  5417. if(quick_database_id_lookup.count(spawn->GetDatabaseID()) > 0)
  5418. quick_database_id_lookup.erase(spawn->GetDatabaseID());
  5419. if(spawn->GetSpawnLocationID() > 0 && quick_location_id_lookup.count(spawn->GetSpawnLocationID()) > 0 && quick_location_id_lookup.Get(spawn->GetSpawnLocationID()) == spawn->GetID())
  5420. quick_location_id_lookup.erase(spawn->GetSpawnLocationID());
  5421. if(spawn->GetSpawnGroupID() > 0 && quick_group_id_lookup.count(spawn->GetSpawnGroupID()) > 0 && quick_group_id_lookup.Get(spawn->GetSpawnGroupID()) == spawn->GetID())
  5422. quick_group_id_lookup.erase(spawn->GetSpawnGroupID());
  5423. }
  5424. DeleteSpawnScriptTimers(spawn);
  5425. RemovePlayerProximity(spawn);
  5426. }
  5427. // We don't use RemoveMovementNPC() here as it caused a hell of a delay during reloads
  5428. // instead we remove it from the list directly
  5429. if (spawn->IsNPC())
  5430. movement_spawns.erase(spawn->GetID());
  5431. }
  5432. void ZoneServer::HandleEmote(Client* originator, string name) {
  5433. if (!originator) {
  5434. LogWrite(ZONE__ERROR, 0, "Zone", "HandleEmote called with an invalid client");
  5435. return;
  5436. }
  5437. Client* client = 0;
  5438. Emote* origEmote = visual_states.FindEmote(name, originator->GetVersion());
  5439. if(!origEmote){
  5440. 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());
  5441. return;
  5442. }
  5443. Emote* emote = origEmote;
  5444. PacketStruct* packet = 0;
  5445. char* emoteResponse = 0;
  5446. vector<Client*>::iterator client_itr;
  5447. int32 cur_client_version = originator->GetVersion();
  5448. map<int32, Emote*> emote_version_range;
  5449. MClientList.readlock(__FUNCTION__, __LINE__);
  5450. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  5451. client = *client_itr;
  5452. if(!client || (client && client->GetPlayer()->IsIgnored(originator->GetPlayer()->GetName())))
  5453. continue;
  5454. // establish appropriate emote for the version used by the client
  5455. if (client->GetVersion() != originator->GetVersion())
  5456. {
  5457. map<int32, Emote*>::iterator rangeitr = emote_version_range.find(client->GetVersion());
  5458. if (rangeitr == emote_version_range.end())
  5459. {
  5460. Emote* tmp_new_emote = visual_states.FindEmote(name, client->GetVersion());
  5461. if (tmp_new_emote)
  5462. {
  5463. emote_version_range.insert(make_pair(client->GetVersion(), tmp_new_emote));
  5464. emote = tmp_new_emote;
  5465. } // else its missing just use the current clients default
  5466. }
  5467. else // we have an existing emote already cached
  5468. emote = rangeitr->second;
  5469. }
  5470. else // since the client and originator client match use the original emote
  5471. emote = origEmote;
  5472. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  5473. if(packet){
  5474. packet->setDataByName("spawn_id" , client->GetPlayer()->GetIDWithPlayerSpawn(originator->GetPlayer()));
  5475. if(!emoteResponse){
  5476. string message;
  5477. if(originator->GetPlayer()->GetTarget() && originator->GetPlayer()->GetTarget()->GetID() != originator->GetPlayer()->GetID()){
  5478. message = emote->GetTargetedMessageString();
  5479. if(message.find("%t") < 0xFFFFFFFF)
  5480. message.replace(message.find("%t"), 2, originator->GetPlayer()->GetTarget()->GetName());
  5481. }
  5482. if(message.length() == 0)
  5483. message = emote->GetMessageString();
  5484. if(message.find("%g1") < 0xFFFFFFFF){
  5485. if(originator->GetPlayer()->GetGender() == 1)
  5486. message.replace(message.find("%g1"), 3, "his");
  5487. else
  5488. message.replace(message.find("%g1"), 3, "her");
  5489. }
  5490. if(message.find("%g2") < 0xFFFFFFFF){
  5491. if(originator->GetPlayer()->GetGender() == 1)
  5492. message.replace(message.find("%g2"), 3, "him");
  5493. else
  5494. message.replace(message.find("%g2"), 3, "her");
  5495. }
  5496. if(message.find("%g3") < 0xFFFFFFFF){
  5497. if(originator->GetPlayer()->GetGender() == 1)
  5498. message.replace(message.find("%g3"), 3, "he");
  5499. else
  5500. message.replace(message.find("%g3"), 3, "she");
  5501. }
  5502. if(message.length() > 0){
  5503. emoteResponse = new char[message.length() + strlen(originator->GetPlayer()->GetName()) + 10];
  5504. sprintf(emoteResponse,"%s %s", originator->GetPlayer()->GetName(), message.c_str());
  5505. }
  5506. else{
  5507. originator->Message(CHANNEL_COLOR_YELLOW, "%s is not properly configured, be sure to submit a /bug report.", name.c_str());
  5508. safe_delete(packet);
  5509. break;
  5510. }
  5511. }
  5512. packet->setMediumStringByName("emote_msg", emoteResponse);
  5513. packet->setDataByName("anim_type", emote->GetVisualState());
  5514. client->QueuePacket(packet->serialize());
  5515. safe_delete(packet);
  5516. safe_delete_array(emoteResponse);
  5517. }
  5518. }
  5519. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5520. }
  5521. void ZoneServer::SetupInstance(int32 createdInstanceID) {
  5522. if ( createdInstanceID == 0 ) // if this happens that isn't good!
  5523. instanceID = ++MinInstanceID;
  5524. else // db should pass the good ID
  5525. instanceID = createdInstanceID;
  5526. }
  5527. void ZoneServer::RemoveDeadSpawn(Spawn* spawn){
  5528. AddDeadSpawn(spawn, 0);
  5529. }
  5530. void ZoneServer::AddDeadSpawn(Spawn* spawn, int32 timer){
  5531. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  5532. if (dead_spawns.count(spawn->GetID()) > 0)
  5533. dead_spawns[spawn->GetID()] = Timer::GetCurrentTime2() + timer;
  5534. else if(timer != 0xFFFFFFFF)
  5535. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + timer));
  5536. else{
  5537. if(spawn->IsEntity() && spawn->HasLoot()){
  5538. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + (15000 * spawn->GetLevel() + 240000)));
  5539. SendUpdateDefaultCommand(spawn, "loot", 10);
  5540. }
  5541. else
  5542. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + 10000));
  5543. }
  5544. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5545. }
  5546. void ZoneServer::WritePlayerStatistics() {
  5547. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5548. while(client_itr.Next())
  5549. client_itr->value->GetPlayer()->WritePlayerStatistics();
  5550. }
  5551. bool ZoneServer::SendRadiusSpawnInfo(Client* client, float radius) {
  5552. if (!client)
  5553. return false;
  5554. Spawn* spawn = 0;
  5555. bool ret = false;
  5556. map<int32, Spawn*>::iterator itr;
  5557. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5558. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5559. spawn = itr->second;
  5560. if (spawn && spawn != client->GetPlayer() && !spawn->IsPlayer() && spawn->GetDistance(client->GetPlayer()) <= radius) {
  5561. const char* type = "NPC";
  5562. const char* specialTypeID = "N/A";
  5563. int32 specialID = 0, spawnEntryID = spawn->GetSpawnEntryID();
  5564. if (spawn->IsObject())
  5565. {
  5566. Object* obj = (Object*)spawn;
  5567. specialID = obj->GetID();
  5568. specialTypeID = "GetID";
  5569. type = "Object";
  5570. }
  5571. else if (spawn->IsSign())
  5572. {
  5573. Sign* sign = (Sign*)spawn;
  5574. specialID = sign->GetWidgetID();
  5575. specialTypeID = "WidgetID";
  5576. type = "Sign";
  5577. }
  5578. else if (spawn->IsWidget())
  5579. {
  5580. Widget* widget = (Widget*)spawn;
  5581. specialID = widget->GetWidgetID();
  5582. specialTypeID = "WidgetID";
  5583. if ( specialID == 0xFFFFFFFF )
  5584. specialTypeID = "WidgetID(spawn_widgets entry missing)";
  5585. type = "Widget";
  5586. }
  5587. else if (spawn->IsGroundSpawn())
  5588. {
  5589. GroundSpawn* gs = (GroundSpawn*)spawn;
  5590. specialID = gs->GetGroundSpawnEntryID();
  5591. specialTypeID = "GroundSpawnEntryID";
  5592. type = "GroundSpawn";
  5593. }
  5594. client->Message(CHANNEL_COLOR_RED, "Name: %s (%s), Spawn Table ID: %u, %s: %u", spawn->GetName(), type, spawn->GetDatabaseID(), specialTypeID, specialID);
  5595. 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());
  5596. 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());
  5597. client->Message(CHANNEL_COLOR_YELLOW, "=============================");
  5598. ret = true;
  5599. }
  5600. }
  5601. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5602. return ret;
  5603. }
  5604. void ZoneServer::FindSpawn(Client* client, char* regSearchStr)
  5605. {
  5606. if (!regSearchStr || strlen(regSearchStr) < 1)
  5607. {
  5608. client->SimpleMessage(CHANNEL_COLOR_RED, "Bad ZoneServer::FindSpawn(Client*, char* regSearchStr) attempt, regSearchStr is NULL or empty.");
  5609. return;
  5610. }
  5611. string resString = string(regSearchStr);
  5612. try
  5613. {
  5614. std::regex pre_re_check("^[a-zA-Z0-9_ ]+$");
  5615. bool output = std::regex_match(resString, pre_re_check);
  5616. if (output)
  5617. {
  5618. string newStr(".*");
  5619. newStr.append(regSearchStr);
  5620. newStr.append(".*");
  5621. resString = newStr;
  5622. }
  5623. }
  5624. catch (...)
  5625. {
  5626. client->SimpleMessage(CHANNEL_COLOR_RED, "Try/Catch ZoneServer::FindSpawn(Client*, char* regSearchStr) failure.");
  5627. return;
  5628. }
  5629. std::regex re;
  5630. try {
  5631. re = std::regex(resString, std::regex_constants::icase);
  5632. }
  5633. catch(...) {
  5634. client->SimpleMessage(CHANNEL_COLOR_RED, "Invalid regex for FindSpawn.");
  5635. return;
  5636. }
  5637. client->Message(CHANNEL_NARRATIVE, "RegEx Search Spawn List: %s", regSearchStr);
  5638. client->Message(CHANNEL_NARRATIVE, "Database ID | Spawn Name | X , Y , Z");
  5639. client->Message(CHANNEL_NARRATIVE, "========================");
  5640. map<int32, Spawn*>::iterator itr;
  5641. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5642. int32 spawnsFound = 0;
  5643. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5644. Spawn* spawn = itr->second;
  5645. if (!spawn || !spawn->GetName())
  5646. continue;
  5647. bool output = false;
  5648. try {
  5649. output = std::regex_match(string(spawn->GetName()), re);
  5650. }
  5651. catch (...)
  5652. {
  5653. continue;
  5654. }
  5655. if (output)
  5656. {
  5657. client->Message(CHANNEL_NARRATIVE, "%i | %s | %f , %f , %f", spawn->GetDatabaseID(), spawn->GetName(), spawn->GetX(), spawn->GetY(), spawn->GetZ());
  5658. spawnsFound++;
  5659. }
  5660. }
  5661. client->Message(CHANNEL_NARRATIVE, "========================", spawnsFound);
  5662. client->Message(CHANNEL_NARRATIVE, "%u Results Found.", spawnsFound);
  5663. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5664. }
  5665. void ZoneServer::AddPlayerTracking(Player* player) {
  5666. if (player && !player->GetIsTracking() && players_tracking.count(player->GetDatabaseID()) == 0) {
  5667. Client* client = GetClientBySpawn(player);
  5668. if (client) {
  5669. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5670. if (packet) {
  5671. player->SetIsTracking(true);
  5672. players_tracking.Put(client->GetCharacterID(), player);
  5673. packet->setDataByName("mode", TRACKING_START);
  5674. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5675. client->QueuePacket(packet->serialize());
  5676. safe_delete(packet);
  5677. }
  5678. }
  5679. }
  5680. }
  5681. void ZoneServer::RemovePlayerTracking(Player* player, int8 mode) {
  5682. if (player && player->GetIsTracking()) {
  5683. Client* client = GetClientBySpawn(player);
  5684. if (client) {
  5685. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5686. if (packet) {
  5687. player->SetIsTracking(false);
  5688. players_tracking.erase(client->GetCharacterID());
  5689. packet->setDataByName("mode", mode);
  5690. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5691. client->QueuePacket(packet->serialize());
  5692. safe_delete(packet);
  5693. }
  5694. }
  5695. }
  5696. }
  5697. void ZoneServer::ProcessTracking() {
  5698. MutexMap<int32, Player*>::iterator itr = players_tracking.begin();
  5699. while (itr.Next())
  5700. ProcessTracking(GetClientBySpawn(itr->second));
  5701. }
  5702. void ZoneServer::ProcessTracking(Client* client) {
  5703. if (!client)
  5704. return;
  5705. Player* player = client->GetPlayer();
  5706. if (player && player->GetIsTracking()) {
  5707. MutexMap<int32, Spawn*>::iterator spawn_itr;
  5708. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5709. if (packet) {
  5710. packet->setDataByName("mode", TRACKING_UPDATE);
  5711. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5712. vector<TrackedSpawn*> spawns_tracked;
  5713. while (spawn_itr.Next()) {
  5714. Spawn* spawn = spawn_itr->second;
  5715. float distance = player->GetDistance(spawn);
  5716. if (spawn->IsEntity() && distance <= 80 && spawn != player) {
  5717. TrackedSpawn* ts = new TrackedSpawn;
  5718. ts->spawn = spawn;
  5719. ts->distance = distance;
  5720. /* Add spawns in ascending order from closest to furthest */
  5721. if (spawns_tracked.empty())
  5722. spawns_tracked.push_back(ts);
  5723. else {
  5724. vector<TrackedSpawn*>::iterator tracked_itr;
  5725. bool added = false;
  5726. for (tracked_itr = spawns_tracked.begin(); tracked_itr != spawns_tracked.end(); tracked_itr++) {
  5727. TrackedSpawn* cur_ts = *tracked_itr;
  5728. if (ts->distance <= cur_ts->distance) {
  5729. spawns_tracked.insert(tracked_itr, ts);
  5730. added = true;
  5731. break;
  5732. }
  5733. }
  5734. if (!added)
  5735. spawns_tracked.push_back(ts);
  5736. }
  5737. }
  5738. }
  5739. packet->setArrayLengthByName("num_spawns", spawns_tracked.size());
  5740. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5741. TrackedSpawn* ts = spawns_tracked[i];
  5742. LogWrite(ZONE__DEBUG, 0, "Zone", "%s (%f)", ts->spawn->GetName(), ts->distance);
  5743. packet->setArrayDataByName("spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5744. packet->setArrayDataByName("spawn_name", ts->spawn->GetName(), i);
  5745. if (ts->spawn->IsPlayer())
  5746. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_PC, i);
  5747. else
  5748. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_NPC, i);
  5749. packet->setArrayDataByName("spawn_con_color", player->GetArrowColor(ts->spawn->GetLevel()), i);
  5750. }
  5751. packet->setArrayLengthByName("num_array1", 0);
  5752. //for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5753. //}
  5754. packet->setArrayLengthByName("num_spawns2", spawns_tracked.size());
  5755. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5756. TrackedSpawn* ts = spawns_tracked[i];
  5757. packet->setArrayDataByName("list_spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5758. packet->setArrayDataByName("list_number", i, i);
  5759. }
  5760. client->QueuePacket(packet->serialize());
  5761. safe_delete(packet);
  5762. for (int32 i = 0; i < spawns_tracked.size(); i++)
  5763. safe_delete(spawns_tracked[i]);
  5764. }
  5765. }
  5766. }
  5767. void ZoneServer::SendEpicMobDeathToGuild(Player* killer, Spawn* victim) {
  5768. if (killer && victim) {
  5769. LogWrite(MISC__TODO, 1, "TODO" , "Check if player is in raid\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  5770. if (killer->GetGroupMemberInfo()) {
  5771. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  5772. deque<GroupMemberInfo*>::iterator itr;
  5773. PlayerGroup* group = world.GetGroupManager()->GetGroup(killer->GetGroupMemberInfo()->group_id);
  5774. if (group)
  5775. {
  5776. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  5777. deque<GroupMemberInfo*>* members = group->GetMembers();
  5778. for (itr = members->begin(); itr != members->end(); itr++) {
  5779. GroupMemberInfo* gmi = *itr;
  5780. if (gmi->client) {
  5781. Player* group_member = gmi->client->GetPlayer();
  5782. if (group_member && group_member->GetGuild()) {
  5783. Guild* guild = group_member->GetGuild();
  5784. string message = Guild::GetEpicMobDeathMessage(group_member->GetName(), victim->GetName());
  5785. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5786. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5787. }
  5788. }
  5789. }
  5790. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  5791. }
  5792. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  5793. }
  5794. else if (killer->GetGuild()) {
  5795. Guild* guild = killer->GetGuild();
  5796. string message = Guild::GetEpicMobDeathMessage(killer->GetName(), victim->GetName());
  5797. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5798. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5799. }
  5800. }
  5801. }
  5802. void ZoneServer::ProcessAggroChecks(Spawn* spawn) {
  5803. if (spawn->GetFactionID() < 1 || spawn->EngagedInCombat())
  5804. return;
  5805. // If faction based combat is not allowed then no need to run the loops so just return out
  5806. if(!rule_manager.GetGlobalRule(R_Faction, AllowFactionBasedCombat)->GetBool())
  5807. return;
  5808. if (spawn && spawn->IsNPC() && spawn->Alive())
  5809. CheckEnemyList((NPC*)spawn);
  5810. }
  5811. void ZoneServer::SendUpdateTitles(Client* client, Title* suffix, Title* prefix) {
  5812. assert(client);
  5813. if (client->GetVersion() > 546)
  5814. SendUpdateTitles(client->GetPlayer(), suffix, prefix);
  5815. }
  5816. void ZoneServer::SendUpdateTitles(Spawn *spawn, Title *suffix, Title *prefix) {
  5817. if (!spawn)
  5818. return;
  5819. vector<Client*>::iterator itr;
  5820. PacketStruct *packet;
  5821. Client* current_client;
  5822. MClientList.readlock(__FUNCTION__, __LINE__);
  5823. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5824. current_client = *itr;
  5825. if (current_client->GetVersion() <= 546)
  5826. continue;
  5827. if (!(packet = configReader.getStruct("WS_UpdateTitle", current_client->GetVersion())))
  5828. continue;
  5829. packet->setDataByName("player_id", current_client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  5830. packet->setDataByName("player_name", spawn->GetName());
  5831. packet->setDataByName("unknown1", 1, 1);
  5832. if(suffix)
  5833. packet->setDataByName("suffix_title", suffix->GetName());
  5834. else
  5835. packet->setDataByName("suffix_title", spawn->GetSuffixTitle());
  5836. if(prefix)
  5837. packet->setDataByName("prefix_title", prefix->GetName());
  5838. else
  5839. packet->setDataByName("prefix_title", spawn->GetPrefixTitle());
  5840. packet->setDataByName("last_name", spawn->GetLastName());
  5841. packet->setDataByName("sub_title", spawn->GetSubTitle());
  5842. current_client->QueuePacket(packet->serialize());
  5843. safe_delete(packet);
  5844. }
  5845. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5846. }
  5847. void ZoneServer::AddTransportSpawn(Spawn* spawn){
  5848. if(!spawn)
  5849. return;
  5850. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5851. transport_spawns.push_back(spawn->GetID());
  5852. spawn->SetTransportSpawn(true);
  5853. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5854. }
  5855. Spawn* ZoneServer::GetClosestTransportSpawn(float x, float y, float z){
  5856. Spawn* spawn = 0;
  5857. Spawn* closest_spawn = 0;
  5858. float closest_distance = 0.0;
  5859. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5860. vector<int32>::iterator itr = transport_spawns.begin();
  5861. while(itr != transport_spawns.end()){
  5862. spawn = GetSpawnByID(*itr);
  5863. if(spawn){
  5864. if(closest_distance == 0.0){
  5865. closest_spawn = spawn;
  5866. closest_distance = spawn->GetDistance(x, y, z);
  5867. }
  5868. else if(spawn->GetDistance(x, y, z) < closest_distance){
  5869. closest_spawn = spawn;
  5870. closest_distance = spawn->GetDistance(x, y, z);
  5871. }
  5872. itr++;
  5873. }
  5874. else
  5875. itr = transport_spawns.erase(itr);
  5876. }
  5877. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5878. return closest_spawn;
  5879. }
  5880. Spawn* ZoneServer::GetTransportByRailID(sint64 rail_id){
  5881. Spawn* spawn = 0;
  5882. Spawn* closest_spawn = 0;
  5883. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  5884. vector<int32>::iterator itr = transport_spawns.begin();
  5885. while(itr != transport_spawns.end()){
  5886. spawn = GetSpawnByID(*itr);
  5887. //printf("Rail id: %i vs %i\n", spawn ? spawn->GetRailID() : 0, rail_id);
  5888. if(spawn && spawn->GetRailID() == rail_id){
  5889. closest_spawn = spawn;
  5890. break;
  5891. }
  5892. itr++;
  5893. }
  5894. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  5895. return closest_spawn;
  5896. }
  5897. void ZoneServer::SetRain(float val) {
  5898. rain = val;
  5899. vector<Client*>::iterator itr;
  5900. MClientList.readlock(__FUNCTION__, __LINE__);
  5901. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5902. Client* client = *itr;
  5903. client->GetPlayer()->GetInfoStruct()->set_rain(val);
  5904. client->GetPlayer()->SetCharSheetChanged(true);
  5905. if( val >= 0.75 && !weather_signaled )
  5906. {
  5907. client->SimpleMessage(CHANNEL_NARRATIVE, "It starts to rain.");
  5908. }
  5909. else if( val < 0.75 && weather_signaled )
  5910. {
  5911. client->SimpleMessage(CHANNEL_NARRATIVE, "It stops raining.");
  5912. }
  5913. }
  5914. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5915. if (val >= 0.75 && !weather_signaled) {
  5916. weather_signaled = true;
  5917. ProcessSpawnConditional(SPAWN_CONDITIONAL_RAINING);
  5918. }
  5919. else if (val < 0.75 && weather_signaled) {
  5920. weather_signaled = false;
  5921. ProcessSpawnConditional(SPAWN_CONDITIONAL_NOT_RAINING);
  5922. }
  5923. }
  5924. void ZoneServer::SetWind(float val) {
  5925. vector<Client*>::iterator itr;
  5926. MClientList.readlock(__FUNCTION__, __LINE__);
  5927. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5928. Client* client = *itr;
  5929. client->GetPlayer()->GetInfoStruct()->set_wind(val);
  5930. client->GetPlayer()->SetCharSheetChanged(true);
  5931. }
  5932. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5933. }
  5934. void ZoneServer::ProcessWeather()
  5935. {
  5936. // if the global rule to disable weather is set, or if the `weather_allowed` field in the zone record == 0, do not process weather
  5937. if( !weather_enabled || !isWeatherAllowed() )
  5938. return;
  5939. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Processing weather changes", zone_name);
  5940. float new_weather = 0;
  5941. float weather_offset = 0;
  5942. bool change_weather = false;
  5943. // check to see if it is time to change the weather according to weather_frequency (time between changes)
  5944. if( weather_last_changed_time <= (Timer::GetUnixTimeStamp() - weather_frequency) )
  5945. {
  5946. LogWrite(ZONE__DEBUG, 2, "Zone", "%s: Checking for weather changes", zone_name);
  5947. // reset last changed time (frequency check)
  5948. weather_last_changed_time = Timer::GetUnixTimeStamp();
  5949. // this is the chance a weather change occurs at all at the expired interval
  5950. int8 weather_random = MakeRandomInt(1, 100);
  5951. 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");
  5952. if( weather_random <= weather_change_chance )
  5953. {
  5954. change_weather = true;
  5955. weather_offset = weather_change_amount;
  5956. if( weather_type == 3 ) // chaotic weather patterns, random weather between min/max
  5957. {
  5958. new_weather = MakeRandomFloat(weather_min_severity, weather_max_severity);
  5959. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Chaotic weather severity changed to %.2f", zone_name, new_weather);
  5960. weather_pattern = 2;
  5961. }
  5962. else if( weather_type == 2 ) // random weather patterns, combination of normal + dynamic + max_offset
  5963. {
  5964. weather_offset = MakeRandomFloat(weather_change_amount, weather_dynamic_offset);
  5965. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Random weather severity changed by %.2f", zone_name, weather_offset);
  5966. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  5967. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  5968. if( weather_random <= weather_alter )
  5969. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  5970. }
  5971. else if( weather_type == 1 ) // dynamic weather patterns, weather may not reach min/max
  5972. {
  5973. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  5974. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  5975. if( weather_random <= weather_alter )
  5976. {
  5977. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  5978. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Dynamic weather pattern changed to %i", zone_name, weather_pattern);
  5979. }
  5980. }
  5981. else // normal weather patterns, weather starts at min, goes to max, then back down again
  5982. {
  5983. // do nothing (processed below)
  5984. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Normal weather severity changed by %.2f", zone_name, weather_offset);
  5985. }
  5986. // when all done, change the weather
  5987. if( change_weather )
  5988. {
  5989. if( weather_pattern == 1 )
  5990. {
  5991. // weather is getting worse, til it reaches weather_max_severity
  5992. new_weather = ( weather_current_severity <= weather_max_severity ) ? weather_current_severity + weather_offset : weather_max_severity;
  5993. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Increased weather severity by %.2f", zone_name, weather_offset);
  5994. if(new_weather > weather_max_severity)
  5995. {
  5996. new_weather = weather_max_severity - weather_offset;
  5997. weather_pattern = 0;
  5998. }
  5999. }
  6000. else if( weather_pattern == 0 )
  6001. {
  6002. // weather is clearing up, til it reaches weather_min_severity
  6003. new_weather = ( weather_current_severity >= weather_min_severity ) ? weather_current_severity - weather_offset : weather_min_severity;
  6004. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Decreased weather severity by %.2f", zone_name, weather_offset);
  6005. if(new_weather < weather_min_severity)
  6006. {
  6007. new_weather = weather_min_severity + weather_offset;
  6008. weather_pattern = 1;
  6009. }
  6010. }
  6011. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather change triggered from %.2f to %.2f", zone_name, weather_current_severity, new_weather);
  6012. this->SetRain(new_weather);
  6013. weather_current_severity = new_weather;
  6014. }
  6015. }
  6016. }
  6017. else
  6018. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Not time to change weather yet", zone_name);
  6019. }
  6020. void ZoneServer::HidePrivateSpawn(Spawn* spawn) {
  6021. if (!spawn->IsPrivateSpawn())
  6022. return;
  6023. Client* client = 0;
  6024. Player* player = 0;
  6025. PacketStruct* packet = 0;
  6026. int32 packet_version = 0;
  6027. MutexList<Client*>::iterator itr = connected_clients.begin();
  6028. while (itr->Next()) {
  6029. client = itr->value;
  6030. player = client->GetPlayer();
  6031. if (player->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->IsSendingSpawn(spawn->GetID())) {
  6032. if (!packet || packet_version != client->GetVersion()) {
  6033. safe_delete(packet);
  6034. packet_version = client->GetVersion();
  6035. packet = configReader.getStruct("WS_DestroyGhostCmd", packet_version);
  6036. }
  6037. SendRemoveSpawn(client, spawn, packet);
  6038. if(spawn_range_map.count(client) > 0)
  6039. spawn_range_map.Get(client)->erase(spawn->GetID());
  6040. if(player->GetTarget() == spawn)
  6041. player->SetTarget(0);
  6042. }
  6043. }
  6044. safe_delete(packet);
  6045. }
  6046. SpawnLocation* ZoneServer::GetSpawnLocation(int32 id) {
  6047. SpawnLocation* ret = 0;
  6048. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  6049. if (spawn_location_list.count(id) > 0)
  6050. ret = spawn_location_list[id];
  6051. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  6052. return ret;
  6053. }
  6054. void ZoneServer::PlayAnimation(Spawn* spawn, int32 visual_state, Spawn* spawn2, int8 hide_type){
  6055. Client* client = 0;
  6056. PacketStruct* packet = 0;
  6057. Spawn* exclude_spawn = 0;
  6058. if (!spawn)
  6059. return;
  6060. if (spawn2){
  6061. if(hide_type == 1){
  6062. client = GetClientBySpawn(spawn2);
  6063. if(client){
  6064. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6065. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  6066. packet->setDataByName("anim_type", visual_state);
  6067. client->QueuePacket(packet->serialize());
  6068. }
  6069. safe_delete(packet);
  6070. return;
  6071. }
  6072. if(hide_type == 2)
  6073. exclude_spawn = spawn2;
  6074. }
  6075. vector<Client*>::iterator client_itr;
  6076. MClientList.readlock(__FUNCTION__, __LINE__);
  6077. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  6078. client = *client_itr;
  6079. if(spawn->GetDistance(client->GetPlayer()) > 50)
  6080. continue;
  6081. if(exclude_spawn == client->GetPlayer())
  6082. continue;
  6083. if(!client->IsReadyForUpdates()) // client is not in world yet so we shouldn't be sending animations of spawns yet
  6084. continue;
  6085. if(!packet || packet->GetVersion() != client->GetVersion()) {
  6086. safe_delete(packet);
  6087. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6088. }
  6089. if (packet) {
  6090. int32 spawn_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  6091. if(spawn_id) {
  6092. packet->setDataByName("spawn_id", spawn_id);
  6093. packet->setDataByName("anim_type", visual_state);
  6094. client->QueuePacket(packet->serialize());
  6095. }
  6096. }
  6097. }
  6098. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6099. safe_delete(packet);
  6100. }
  6101. vector<Spawn*> ZoneServer::GetSpawnsByID(int32 id) {
  6102. vector<Spawn*> tmp_list;
  6103. Spawn* spawn;
  6104. map<int32, Spawn*>::iterator itr;
  6105. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6106. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6107. spawn = itr->second;
  6108. if (spawn && (spawn->GetDatabaseID() == id))
  6109. tmp_list.push_back(spawn);
  6110. }
  6111. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6112. return tmp_list;
  6113. }
  6114. vector<Spawn*> ZoneServer::GetSpawnsByRailID(sint64 rail_id) {
  6115. vector<Spawn*> tmp_list;
  6116. Spawn* spawn;
  6117. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6118. vector<int32>::iterator itr = transport_spawns.begin();
  6119. while(itr != transport_spawns.end()){
  6120. spawn = GetSpawnByID(*itr);
  6121. if(spawn && spawn->GetRailID() == rail_id){
  6122. tmp_list.push_back(spawn);
  6123. }
  6124. itr++;
  6125. }
  6126. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6127. return tmp_list;
  6128. }
  6129. void ZoneServer::RemovePlayerPassenger(int32 char_id) {
  6130. vector<Spawn*> tmp_list;
  6131. Spawn* spawn;
  6132. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6133. vector<int32>::iterator itr = transport_spawns.begin();
  6134. while(itr != transport_spawns.end()){
  6135. spawn = GetSpawnByID(*itr);
  6136. if(spawn) {
  6137. spawn->RemoveRailPassenger(char_id);
  6138. }
  6139. itr++;
  6140. }
  6141. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6142. }
  6143. vector<int32> ZoneServer::GetAttackableSpawnsByDistance(Spawn* caster, float distance) {
  6144. vector<int32> ret;
  6145. Spawn* spawn = 0;
  6146. std::shared_lock lock(MGridMaps);
  6147. std::map<int32, GridMap*>::iterator grids = grid_maps.find(caster->GetLocation());
  6148. if(grids != grid_maps.end()) {
  6149. grids->second->MSpawns.lock_shared();
  6150. typedef map <int32, Spawn*> SpawnMapType;
  6151. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  6152. Spawn* spawn = it->second;
  6153. if (spawn && spawn->IsNPC() && spawn->appearance.attackable > 0 && spawn->GetID() > 0 && spawn->GetID() != caster->GetID() &&
  6154. spawn->Alive() && spawn->GetDistance(caster, true) <= distance) {
  6155. ret.push_back(spawn->GetID());
  6156. }
  6157. }
  6158. grids->second->MSpawns.unlock_shared();
  6159. }
  6160. return ret;
  6161. }
  6162. void ZoneServer::ResurrectSpawn(Spawn* spawn, Client* client) {
  6163. if(!client || !spawn)
  6164. return;
  6165. PendingResurrection* rez = client->GetCurrentRez();
  6166. if(!rez || !rez->caster)
  6167. return;
  6168. PacketStruct* packet = 0;
  6169. float power_perc = rez->mp_perc;
  6170. float health_perc = rez->hp_perc;
  6171. Spawn* caster_spawn = rez->caster;
  6172. sint32 heal_amt = 0;
  6173. sint32 power_amt = 0;
  6174. bool no_calcs = rez->no_calcs;
  6175. int8 crit_mod = rez->crit_mod;
  6176. Entity* caster = 0;
  6177. InfoStruct* info = 0;
  6178. bool crit = false;
  6179. string heal_spell = rez->heal_name;
  6180. int16 heal_packet_type = 0;
  6181. int16 power_packet_type = 0;
  6182. //Calculations for how much to heal the spawn
  6183. if(health_perc > 0)
  6184. heal_amt = (spawn->GetTotalHP() * (health_perc / 100));
  6185. if(power_perc > 0)
  6186. power_amt = (spawn->GetTotalPower() * (power_perc / 100));
  6187. if(caster_spawn->IsEntity()){
  6188. caster = ((Entity*)caster_spawn);
  6189. info = caster->GetInfoStruct();
  6190. }
  6191. if(!no_calcs && caster){
  6192. heal_amt = caster->CalculateHealAmount(spawn, heal_amt, crit_mod, &crit);
  6193. power_amt = caster->CalculateHealAmount(spawn, power_amt, crit_mod, &crit);
  6194. }
  6195. //Set this rez as a crit to be passed to subspell (not yet used)
  6196. rez->crit = true;
  6197. //Set Heal amt to 1 if 0 now so the player has health
  6198. if(heal_amt == 0)
  6199. heal_amt = 1;
  6200. if(heal_amt > spawn->GetTotalHP())
  6201. heal_amt = spawn->GetTotalHP();
  6202. if(power_amt > spawn->GetTotalPower())
  6203. power_amt = spawn->GetTotalPower();
  6204. spawn->SetHP(heal_amt);
  6205. if(power_amt > 0)
  6206. spawn->SetPower(power_amt);
  6207. if(client && caster){
  6208. EQ2Packet* move = ((Player*)spawn)->Move(caster->GetX(), caster->GetY(), caster->GetZ(), client->GetVersion());
  6209. if(move)
  6210. client->QueuePacket(move);
  6211. }
  6212. if(crit){
  6213. power_packet_type = HEAL_PACKET_TYPE_CRIT_MANA;
  6214. heal_packet_type = HEAL_PACKET_TYPE_CRIT_HEAL;
  6215. }
  6216. else {
  6217. power_packet_type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  6218. heal_packet_type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  6219. }
  6220. SendHealPacket(caster, spawn, heal_packet_type, heal_amt, heal_spell.c_str());
  6221. if(power_amt > 0)
  6222. SendHealPacket(caster, spawn, power_packet_type, power_amt, heal_spell.c_str());
  6223. //The following code sets the spawn as alive
  6224. if(dead_spawns.count(spawn->GetID()) > 0)
  6225. dead_spawns.erase(spawn->GetID());
  6226. if(spawn->IsPlayer()){
  6227. spawn->SetSpawnType(4);
  6228. client = GetClientBySpawn(spawn);
  6229. if(client){
  6230. packet = configReader.getStruct("WS_Resurrected", client->GetVersion());
  6231. if(packet){
  6232. client->QueuePacket(packet->serialize());
  6233. }
  6234. packet = configReader.getStruct("WS_ServerControlFlags", client->GetVersion());
  6235. if(packet)
  6236. {
  6237. packet->setDataByName("parameter1", 8);
  6238. client->QueuePacket(packet->serialize());
  6239. packet->setDataByName("parameter1", 16);
  6240. client->QueuePacket(packet->serialize());
  6241. }
  6242. safe_delete(packet);
  6243. client->SimpleMessage(CHANNEL_NARRATIVE, "You regain consciousness!");
  6244. }
  6245. }
  6246. spawn->SendSpawnChanges(true);
  6247. spawn->SetTempActionState(-1);
  6248. spawn->appearance.attackable = 1;
  6249. }
  6250. void ZoneServer::SendDispellPacket(Entity* caster, Spawn* target, string dispell_name, string spell_name, int8 dispell_type){
  6251. if(!caster || !target)
  6252. return;
  6253. Client* client = 0;
  6254. Player* player = 0;
  6255. PacketStruct* packet = 0;
  6256. vector<Client*>::iterator client_itr;
  6257. MClientList.readlock(__FUNCTION__, __LINE__);
  6258. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++){
  6259. client = *client_itr;
  6260. if(!client || !(player = client->GetPlayer()) || (player != caster && ((caster && player->WasSentSpawn(caster->GetID()) == false) || (target && player->WasSentSpawn(target->GetID()) == false))))
  6261. continue;
  6262. if(caster && caster->GetDistance(player) > 50)
  6263. continue;
  6264. if(target && target->GetDistance(player) > 50)
  6265. continue;
  6266. packet = configReader.getStruct("WS_HearDispell", client->GetVersion());
  6267. if(packet){
  6268. packet->setDataByName("spell_name", spell_name.c_str());
  6269. packet->setDataByName("dispell_name", dispell_name.c_str());
  6270. packet->setDataByName("caster", player->GetIDWithPlayerSpawn(caster));
  6271. packet->setDataByName("target", player->GetIDWithPlayerSpawn(target));
  6272. packet->setDataByName("type", dispell_type);
  6273. client->QueuePacket(packet->serialize());
  6274. }
  6275. safe_delete(packet);
  6276. }
  6277. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6278. }
  6279. void ZoneServer::DismissAllPets() {
  6280. Spawn* spawn = 0;
  6281. map<int32, Spawn*>::iterator itr;
  6282. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6283. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6284. spawn = itr->second;
  6285. if (spawn && spawn->IsEntity())
  6286. ((Entity*)spawn)->DismissAllPets();
  6287. }
  6288. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6289. }
  6290. void ZoneServer::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target, bool remove_caster){
  6291. if (spellProcess)
  6292. spellProcess->RemoveTargetFromSpell(spell, target, remove_caster);
  6293. }
  6294. void ZoneServer::ClearHate(Entity* entity) {
  6295. Spawn* spawn = 0;
  6296. map<int32, Spawn*>::iterator itr;
  6297. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6298. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6299. spawn = itr->second;
  6300. if (spawn && spawn->IsNPC() && ((NPC*)spawn)->Brain())
  6301. ((NPC*)spawn)->Brain()->ClearHate(entity);
  6302. }
  6303. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6304. }
  6305. ThreadReturnType ZoneLoop(void* tmp) {
  6306. #ifdef WIN32
  6307. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6308. #endif
  6309. if (tmp == 0) {
  6310. ThrowError("ZoneLoop(): tmp = 0!");
  6311. THREAD_RETURN(NULL);
  6312. }
  6313. ZoneServer* zs = (ZoneServer*) tmp;
  6314. while (zs->Process()) {
  6315. if(zs->GetClientCount() == 0)
  6316. Sleep(1000);
  6317. else
  6318. Sleep(10);
  6319. }
  6320. zs->Process(); //run loop once more to clean up some functions
  6321. safe_delete(zs);
  6322. THREAD_RETURN(NULL);
  6323. }
  6324. ThreadReturnType SpawnLoop(void* tmp) {
  6325. #ifdef WIN32
  6326. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6327. #endif
  6328. if (tmp == 0) {
  6329. ThrowError("SpawnLoop(): tmp = 0!");
  6330. THREAD_RETURN(NULL);
  6331. }
  6332. ZoneServer* zs = (ZoneServer*) tmp;
  6333. #ifndef NO_CATCH
  6334. try {
  6335. #endif
  6336. zs->spawnthread_active = true;
  6337. while (zs->SpawnProcess()) {
  6338. if(zs->GetClientCount() == 0)
  6339. Sleep(1000);
  6340. else
  6341. Sleep(20);
  6342. }
  6343. zs->spawnthread_active = false;
  6344. #ifndef NO_CATCH
  6345. }
  6346. catch(...) {
  6347. zs->spawnthread_active = false;
  6348. zs->initial_spawn_threads_active = 0;
  6349. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop, shutting down zone '%s'...", zs->GetZoneName());
  6350. try{
  6351. zs->Shutdown();
  6352. }
  6353. catch(...){
  6354. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop while shutting down zone '%s'...", zs->GetZoneName());
  6355. throw;
  6356. }
  6357. throw;
  6358. }
  6359. #endif
  6360. THREAD_RETURN(NULL);
  6361. }
  6362. ThreadReturnType SendInitialSpawns(void* tmp) {
  6363. #ifdef WIN32
  6364. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  6365. #endif
  6366. if (tmp == 0) {
  6367. ThrowError("SendInitialSpawns(): tmp = 0!");
  6368. THREAD_RETURN(NULL);
  6369. }
  6370. Client* client = (Client*) tmp;
  6371. client->GetCurrentZone()->SendZoneSpawns(client);
  6372. THREAD_RETURN(NULL);
  6373. }
  6374. ThreadReturnType SendLevelChangedSpawns(void* tmp) {
  6375. #ifdef WIN32
  6376. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6377. #endif
  6378. if (tmp == 0) {
  6379. ThrowError("SendLevelChangedSpawns(): tmp = 0!");
  6380. THREAD_RETURN(NULL);
  6381. }
  6382. Client* client = (Client*)tmp;
  6383. client->GetCurrentZone()->SendAllSpawnsForLevelChange(client);
  6384. THREAD_RETURN(NULL);
  6385. }
  6386. void ZoneServer::SetSpawnStructs(Client* client) {
  6387. int16 client_ver = client->GetVersion();
  6388. Player* player = client->GetPlayer();
  6389. //Save a copy of the correct spawn substructs for the client's player, save here a copy if we don't have one
  6390. PacketStruct* pos = configReader.getStruct("Substruct_SpawnPositionStruct", client_ver);
  6391. player->SetSpawnPosStruct(pos);
  6392. if (versioned_pos_structs.count(pos->GetVersion()) == 0)
  6393. versioned_pos_structs[pos->GetVersion()] = new PacketStruct(pos, true);
  6394. PacketStruct* vis = configReader.getStruct("Substruct_SpawnVisualizationInfoStruct", client_ver);
  6395. player->SetSpawnVisStruct(vis);
  6396. if (versioned_vis_structs.count(vis->GetVersion()) == 0)
  6397. versioned_vis_structs[vis->GetVersion()] = new PacketStruct(vis, true);
  6398. PacketStruct* info = configReader.getStruct("Substruct_SpawnInfoStruct", client_ver);
  6399. player->SetSpawnInfoStruct(info);
  6400. if (versioned_info_structs.count(info->GetVersion()) == 0)
  6401. versioned_info_structs[info->GetVersion()] = new PacketStruct(info, true);
  6402. PacketStruct* header = configReader.getStruct("WS_SpawnStruct_Header", client_ver);
  6403. player->SetSpawnHeaderStruct(header);
  6404. PacketStruct* footer = configReader.getStruct("WS_SpawnStruct_Footer", client_ver);
  6405. player->SetSpawnFooterStruct(footer);
  6406. PacketStruct* sfooter = configReader.getStruct("WS_SignWidgetSpawnStruct_Footer", client_ver);
  6407. player->SetSignFooterStruct(sfooter);
  6408. PacketStruct* wfooter = configReader.getStruct("WS_WidgetSpawnStruct_Footer", client_ver);
  6409. player->SetWidgetFooterStruct(wfooter);
  6410. }
  6411. Spawn* ZoneServer::GetSpawn(int32 id){
  6412. Spawn* ret = 0;
  6413. if(GetNPC(id))
  6414. ret = GetNewNPC(id);
  6415. else if(this->GetObject(id))
  6416. ret = GetNewObject(id);
  6417. else if(GetWidget(id))
  6418. ret = GetNewWidget(id);
  6419. else if(GetSign(id))
  6420. ret = GetNewSign(id);
  6421. else if(GetGroundSpawn(id))
  6422. ret = GetNewGroundSpawn(id);
  6423. // Unable to find the spawn in the list lets attempt to add it if we are not currently reloading
  6424. else if (!reloading && database.LoadNPC(this, id)) {
  6425. if (GetNPC(id))
  6426. ret = GetNewNPC(id);
  6427. else
  6428. LogWrite(NPC__ERROR, 0, "NPC", "Database inserted npc (%u) but was still unable to retrieve it!", id);
  6429. }
  6430. else if (!reloading && database.LoadObject(this, id)) {
  6431. if (this->GetObject(id))
  6432. ret = GetNewObject(id);
  6433. else
  6434. LogWrite(OBJECT__ERROR, 0, "Object", "Database inserted object (%u) but was still unable to retrieve it!", id);
  6435. }
  6436. else if (!reloading && database.LoadWidget(this, id)) {
  6437. if (GetWidget(id))
  6438. ret = GetNewWidget(id);
  6439. else
  6440. LogWrite(WIDGET__ERROR, 0, "Widget", "Database inserted widget (%u) but was still unable to retrieve it!", id);
  6441. }
  6442. else if (!reloading && database.LoadSign(this, id)) {
  6443. if (GetSign(id))
  6444. ret = GetNewSign(id);
  6445. else
  6446. LogWrite(SIGN__ERROR, 0, "Sign", "Database inserted sign (%u) but was still unable to retrieve it!", id);
  6447. }
  6448. else if (!reloading && database.LoadGroundSpawn(this, id)) {
  6449. if (GetGroundSpawn(id))
  6450. ret = GetNewGroundSpawn(id);
  6451. else
  6452. LogWrite(GROUNDSPAWN__ERROR, 0, "GSpawn", "Database inserted ground spawn (%u) but was still unable to retrieve it!", id);
  6453. }
  6454. if(ret && ret->IsOmittedByDBFlag())
  6455. {
  6456. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) was skipped due to a missing expansion / holiday flag being met.", id);
  6457. safe_delete(ret);
  6458. ret = 0;
  6459. }
  6460. if(ret)
  6461. ret->SetID(Spawn::NextID());
  6462. return ret;
  6463. }
  6464. vector<EntityCommand*>* ZoneServer::GetEntityCommandList(int32 id){
  6465. if(entity_command_list.count(id) > 0)
  6466. return entity_command_list[id];
  6467. else
  6468. return 0;
  6469. }
  6470. void ZoneServer::SetEntityCommandList(int32 id, EntityCommand* command) {
  6471. if (entity_command_list.count(id) == 0)
  6472. entity_command_list[id] = new vector<EntityCommand*>;
  6473. entity_command_list[id]->push_back(command);
  6474. }
  6475. EntityCommand* ZoneServer::GetEntityCommand(int32 id, string name) {
  6476. EntityCommand* ret = 0;
  6477. if (entity_command_list.count(id) == 0)
  6478. return ret;
  6479. vector<EntityCommand*>::iterator itr;
  6480. for (itr = entity_command_list[id]->begin(); itr != entity_command_list[id]->end(); itr++) {
  6481. if ((*itr)->name == name) {
  6482. ret = (*itr);
  6483. break;
  6484. }
  6485. }
  6486. return ret;
  6487. }
  6488. void ZoneServer::ClearEntityCommands() {
  6489. if (entity_command_list.size() > 0) {
  6490. map<int32, vector<EntityCommand*>* >::iterator itr;
  6491. for (itr = entity_command_list.begin(); itr != entity_command_list.end(); itr++) {
  6492. vector<EntityCommand*>* entity_commands = itr->second;
  6493. if (entity_commands && entity_commands->size() > 0) {
  6494. vector<EntityCommand*>::iterator v_itr;
  6495. for (v_itr = entity_commands->begin(); v_itr != entity_commands->end(); v_itr++)
  6496. safe_delete(*v_itr);
  6497. entity_commands->clear();
  6498. }
  6499. safe_delete(entity_commands);
  6500. }
  6501. entity_command_list.clear();
  6502. }
  6503. }
  6504. void ZoneServer::AddNPCSkill(int32 list_id, int32 skill_id, int16 value){
  6505. npc_skill_list[list_id][skill_id] = value;
  6506. }
  6507. map<string, Skill*>* ZoneServer::GetNPCSkills(int32 primary_list, int32 secondary_list){
  6508. map<string, Skill*>* ret = 0;
  6509. if(npc_skill_list.count(primary_list) > 0){
  6510. ret = new map<string, Skill*>();
  6511. map<int32, int16>::iterator itr;
  6512. Skill* tmpSkill = 0;
  6513. for(itr = npc_skill_list[primary_list].begin(); itr != npc_skill_list[primary_list].end(); itr++){
  6514. tmpSkill = master_skill_list.GetSkill(itr->first);
  6515. if(tmpSkill){
  6516. tmpSkill = new Skill(tmpSkill);
  6517. tmpSkill->current_val = itr->second;
  6518. tmpSkill->max_val = tmpSkill->current_val+5;
  6519. (*ret)[tmpSkill->name.data] = tmpSkill;
  6520. }
  6521. }
  6522. }
  6523. if(npc_skill_list.count(secondary_list) > 0){
  6524. if(!ret)
  6525. ret = new map<string, Skill*>();
  6526. map<int32, int16>::iterator itr;
  6527. Skill* tmpSkill = 0;
  6528. for(itr = npc_skill_list[secondary_list].begin(); itr != npc_skill_list[secondary_list].end(); itr++){
  6529. tmpSkill = master_skill_list.GetSkill(itr->first);
  6530. if(tmpSkill){
  6531. tmpSkill = new Skill(tmpSkill);
  6532. tmpSkill->current_val = itr->second;
  6533. tmpSkill->max_val = tmpSkill->current_val+5;
  6534. (*ret)[tmpSkill->name.data] = tmpSkill;
  6535. }
  6536. }
  6537. }
  6538. if(ret && ret->size() == 0){
  6539. safe_delete(ret);
  6540. ret = 0;
  6541. }
  6542. return ret;
  6543. }
  6544. void ZoneServer::AddNPCEquipment(int32 list_id, int32 item_id){
  6545. npc_equipment_list[list_id].push_back(item_id);
  6546. }
  6547. void ZoneServer::SetNPCEquipment(NPC* npc) {
  6548. if(npc_equipment_list.count(npc->GetEquipmentListID()) > 0){
  6549. Item* tmpItem = 0;
  6550. int8 slot = 0;
  6551. vector<int32>::iterator itr;
  6552. for(itr = npc_equipment_list[npc->GetEquipmentListID()].begin(); itr != npc_equipment_list[npc->GetEquipmentListID()].end(); itr++){
  6553. tmpItem = master_item_list.GetItem(*itr);
  6554. if(tmpItem){
  6555. slot = npc->GetEquipmentList()->GetFreeSlot(tmpItem);
  6556. if(slot < 255){
  6557. tmpItem = new Item(tmpItem);
  6558. npc->GetEquipmentList()->SetItem(slot, tmpItem);
  6559. }
  6560. }
  6561. }
  6562. }
  6563. }
  6564. void ZoneServer::AddNPC(int32 id, NPC* npc) {
  6565. npc_list[id] = npc;
  6566. }
  6567. void ZoneServer::AddWidget(int32 id, Widget* widget) {
  6568. widget_list[id] = widget;
  6569. }
  6570. Widget* ZoneServer::GetWidget(int32 id, bool override_loading) {
  6571. if((!reloading || override_loading) && widget_list.count(id) > 0)
  6572. return widget_list[id];
  6573. else
  6574. return 0;
  6575. }
  6576. Widget* ZoneServer::GetNewWidget(int32 id) {
  6577. if(!reloading && widget_list.count(id) > 0)
  6578. return widget_list[id]->Copy();
  6579. else
  6580. return 0;
  6581. }
  6582. void ZoneServer::LoadGroundSpawnEntries(){
  6583. MGroundSpawnItems.lock();
  6584. database.LoadGroundSpawnEntries(this);
  6585. MGroundSpawnItems.unlock();
  6586. }
  6587. void ZoneServer::LoadGroundSpawnItems() {
  6588. }
  6589. 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) {
  6590. GroundSpawnEntry* entry = new GroundSpawnEntry;
  6591. entry->min_skill_level = min_skill_level;
  6592. entry->min_adventure_level = min_adventure_level;
  6593. entry->bonus_table = bonus_table;
  6594. entry->harvest1 = harvest1;
  6595. entry->harvest3 = harvest3;
  6596. entry->harvest5 = harvest5;
  6597. entry->harvest_imbue = harvest_imbue;
  6598. entry->harvest_rare = harvest_rare;
  6599. entry->harvest10 = harvest10;
  6600. entry->harvest_coin = harvest_coin;
  6601. groundspawn_entries[groundspawn_id].push_back(entry);
  6602. }
  6603. void ZoneServer::AddGroundSpawnItem(int32 groundspawn_id, int32 item_id, int8 is_rare, int32 grid_id) {
  6604. GroundSpawnEntryItem* entry = new GroundSpawnEntryItem;
  6605. entry->item_id = item_id;
  6606. entry->is_rare = is_rare;
  6607. entry->grid_id = grid_id;
  6608. groundspawn_items[groundspawn_id].push_back(entry);
  6609. }
  6610. vector<GroundSpawnEntry*>* ZoneServer::GetGroundSpawnEntries(int32 id){
  6611. vector<GroundSpawnEntry*>* ret = 0;
  6612. MGroundSpawnItems.lock();
  6613. if(groundspawn_entries.count(id) > 0)
  6614. ret = &groundspawn_entries[id];
  6615. MGroundSpawnItems.unlock();
  6616. return ret;
  6617. }
  6618. vector<GroundSpawnEntryItem*>* ZoneServer::GetGroundSpawnEntryItems(int32 id){
  6619. vector<GroundSpawnEntryItem*>* ret = 0;
  6620. if(groundspawn_items.count(id) > 0)
  6621. ret = &groundspawn_items[id];
  6622. return ret;
  6623. }
  6624. // TODO - mis-named, should be DeleteGroundSpawnEntries() but this is ok for now :)
  6625. void ZoneServer::DeleteGroundSpawnItems()
  6626. {
  6627. MGroundSpawnItems.lock();
  6628. map<int32, vector<GroundSpawnEntry*> >::iterator groundspawnentry_map_itr;
  6629. vector<GroundSpawnEntry*>::iterator groundspawnentry_itr;
  6630. for(groundspawnentry_map_itr = groundspawn_entries.begin(); groundspawnentry_map_itr != groundspawn_entries.end(); groundspawnentry_map_itr++)
  6631. {
  6632. for(groundspawnentry_itr = groundspawnentry_map_itr->second.begin(); groundspawnentry_itr != groundspawnentry_map_itr->second.end(); groundspawnentry_itr++)
  6633. {
  6634. safe_delete(*groundspawnentry_itr);
  6635. }
  6636. }
  6637. groundspawn_entries.clear();
  6638. map<int32, vector<GroundSpawnEntryItem*> >::iterator groundspawnitem_map_itr;
  6639. vector<GroundSpawnEntryItem*>::iterator groundspawnitem_itr;
  6640. for(groundspawnitem_map_itr = groundspawn_items.begin(); groundspawnitem_map_itr != groundspawn_items.end(); groundspawnitem_map_itr++)
  6641. {
  6642. for(groundspawnitem_itr = groundspawnitem_map_itr->second.begin(); groundspawnitem_itr != groundspawnitem_map_itr->second.end(); groundspawnitem_itr++)
  6643. {
  6644. safe_delete(*groundspawnitem_itr);
  6645. }
  6646. }
  6647. groundspawn_items.clear();
  6648. MGroundSpawnItems.unlock();
  6649. }
  6650. void ZoneServer::AddGroundSpawn(int32 id, GroundSpawn* spawn) {
  6651. groundspawn_list[id] = spawn;
  6652. }
  6653. GroundSpawn* ZoneServer::GetGroundSpawn(int32 id, bool override_loading) {
  6654. if((!reloading || override_loading) && groundspawn_list.count(id) > 0)
  6655. return groundspawn_list[id];
  6656. else
  6657. return 0;
  6658. }
  6659. GroundSpawn* ZoneServer::GetNewGroundSpawn(int32 id) {
  6660. if(!reloading && groundspawn_list.count(id) > 0)
  6661. return groundspawn_list[id]->Copy();
  6662. else
  6663. return 0;
  6664. }
  6665. void ZoneServer::AddLootTable(int32 id, LootTable* table){
  6666. loot_tables[id] = table;
  6667. }
  6668. void ZoneServer::AddLootDrop(int32 id, LootDrop* drop){
  6669. loot_drops[id].push_back(drop);
  6670. }
  6671. void ZoneServer::AddSpawnLootList(int32 spawn_id, int32 id) {
  6672. spawn_loot_list[spawn_id].push_back(id);
  6673. }
  6674. void ZoneServer::ClearSpawnLootList(int32 spawn_id) {
  6675. spawn_loot_list[spawn_id].clear();
  6676. }
  6677. void ZoneServer::AddLevelLootList(GlobalLoot* loot) {
  6678. level_loot_list.push_back(loot);
  6679. }
  6680. void ZoneServer::AddRacialLootList(int16 racial_id, GlobalLoot* loot) {
  6681. racial_loot_list[racial_id].push_back(loot);
  6682. }
  6683. void ZoneServer::AddZoneLootList(int32 zone, GlobalLoot* loot) {
  6684. zone_loot_list[zone].push_back(loot);
  6685. }
  6686. void ZoneServer::ClearLootTables(){
  6687. map<int32,LootTable*>::iterator table_itr;
  6688. for(table_itr = loot_tables.begin(); table_itr != loot_tables.end(); table_itr++){
  6689. safe_delete(table_itr->second);
  6690. }
  6691. map<int32, vector<LootDrop*> >::iterator drop_itr;
  6692. vector<LootDrop*>::iterator drop_itr2;
  6693. for(drop_itr = loot_drops.begin(); drop_itr != loot_drops.end(); drop_itr++){
  6694. for(drop_itr2 = drop_itr->second.begin(); drop_itr2 != drop_itr->second.end(); drop_itr2++){
  6695. safe_delete(*drop_itr2);
  6696. }
  6697. }
  6698. vector<GlobalLoot*>::iterator level_itr;
  6699. for (level_itr = level_loot_list.begin(); level_itr != level_loot_list.end(); level_itr++) {
  6700. safe_delete(*level_itr);
  6701. }
  6702. map<int16, vector<GlobalLoot*> >::iterator race_itr;
  6703. vector<GlobalLoot*>::iterator race_itr2;
  6704. for (race_itr = racial_loot_list.begin(); race_itr != racial_loot_list.end(); race_itr++) {
  6705. for (race_itr2 = race_itr->second.begin(); race_itr2 != race_itr->second.end(); race_itr2++) {
  6706. safe_delete(*race_itr2);
  6707. }
  6708. }
  6709. map<int32, vector<GlobalLoot*> >::iterator zone_itr;
  6710. vector<GlobalLoot*>::iterator zone_itr2;
  6711. for(zone_itr = zone_loot_list.begin(); zone_itr != zone_loot_list.end(); zone_itr++) {
  6712. for (zone_itr2 = zone_itr->second.begin(); zone_itr2 != zone_itr->second.end(); zone_itr2++) {
  6713. safe_delete(*zone_itr2);
  6714. }
  6715. }
  6716. loot_tables.clear();
  6717. loot_drops.clear();
  6718. spawn_loot_list.clear();
  6719. level_loot_list.clear();
  6720. racial_loot_list.clear();
  6721. zone_loot_list.clear();
  6722. }
  6723. vector<int32> ZoneServer::GetSpawnLootList(int32 spawn_id, int32 zone_id, int8 spawn_level, int16 racial_id, Spawn* spawn) {
  6724. vector<int32> ret;
  6725. int32 returnValue = 0;
  6726. if(reloading)
  6727. return ret;
  6728. if (spawn_loot_list.count(spawn_id) > 0)
  6729. ret.insert(ret.end(), spawn_loot_list[spawn_id].begin(), spawn_loot_list[spawn_id].end());
  6730. if (level_loot_list.size() > 0) {
  6731. vector<GlobalLoot*>::iterator itr;
  6732. for (itr = level_loot_list.begin(); itr != level_loot_list.end(); itr++) {
  6733. GlobalLoot* loot = *itr;
  6734. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6735. returnValue = 0; // reset since this can override the database setting
  6736. if(zone_script)
  6737. {
  6738. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_level", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6739. continue;
  6740. }
  6741. bool entryAdded = false;
  6742. 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
  6743. ret.push_back(loot->table_id);
  6744. else {
  6745. 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
  6746. ret.push_back(loot->table_id);
  6747. }
  6748. if(!entryAdded && returnValue) // DB override via LUA scripting
  6749. ret.push_back(loot->table_id);
  6750. }
  6751. }
  6752. if (racial_loot_list.count(racial_id) > 0) {
  6753. vector<GlobalLoot*>::iterator itr;
  6754. for (itr = racial_loot_list[racial_id].begin(); itr != racial_loot_list[racial_id].end(); itr++) {
  6755. GlobalLoot* loot = *itr;
  6756. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6757. returnValue = 0; // reset since this can override the database setting
  6758. if(zone_script)
  6759. {
  6760. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_racial", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6761. continue;
  6762. }
  6763. bool entryAdded = false;
  6764. 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
  6765. ret.push_back(loot->table_id);
  6766. else {
  6767. 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
  6768. ret.push_back(loot->table_id);
  6769. }
  6770. if(!entryAdded && returnValue) // DB override via LUA scripting
  6771. ret.push_back(loot->table_id);
  6772. }
  6773. }
  6774. if (zone_loot_list.count(zone_id) > 0) {
  6775. vector<GlobalLoot*>::iterator itr;
  6776. for (itr = zone_loot_list[zone_id].begin(); itr != zone_loot_list[zone_id].end(); itr++) {
  6777. GlobalLoot* loot = *itr;
  6778. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6779. returnValue = 0; // reset since this can override the database setting
  6780. if(zone_script)
  6781. {
  6782. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_zone", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6783. continue;
  6784. }
  6785. bool entryAdded = false;
  6786. 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
  6787. ret.push_back(loot->table_id);
  6788. else {
  6789. 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
  6790. ret.push_back(loot->table_id);
  6791. }
  6792. if(!entryAdded && returnValue) // DB override via LUA scripting
  6793. ret.push_back(loot->table_id);
  6794. }
  6795. }
  6796. return ret;
  6797. }
  6798. vector<LootDrop*>* ZoneServer::GetLootDrops(int32 table_id){
  6799. if(!reloading && loot_drops.count(table_id) > 0)
  6800. return &(loot_drops[table_id]);
  6801. else
  6802. return 0;
  6803. }
  6804. LootTable* ZoneServer::GetLootTable(int32 table_id){
  6805. return loot_tables[table_id];
  6806. }
  6807. 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){
  6808. LocationTransportDestination* loc = new LocationTransportDestination;
  6809. loc->message = message;
  6810. loc->trigger_x = trigger_x;
  6811. loc->trigger_y = trigger_y;
  6812. loc->trigger_z = trigger_z;
  6813. loc->trigger_radius = trigger_radius;
  6814. loc->destination_zone_id = destination_zone_id;
  6815. loc->destination_x = destination_x;
  6816. loc->destination_y = destination_y;
  6817. loc->destination_z = destination_z;
  6818. loc->destination_heading = destination_heading;
  6819. loc->cost = cost;
  6820. loc->unique_id = unique_id;
  6821. MTransporters.lock();
  6822. if(location_transporters.count(zone_id) == 0)
  6823. location_transporters[zone_id] = new MutexList<LocationTransportDestination*>();
  6824. location_transporters[zone_id]->Add(loc);
  6825. MTransporters.unlock();
  6826. }
  6827. 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,
  6828. 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,
  6829. int32 max_client_version, int32 flight_path_id, int16 mount_id, int8 mount_red_color, int8 mount_green_color, int8 mount_blue_color){
  6830. TransportDestination* transport = new TransportDestination;
  6831. transport->type = type;
  6832. transport->display_name = name;
  6833. transport->message = message;
  6834. transport->destination_zone_id = destination_zone_id;
  6835. transport->destination_x = destination_x;
  6836. transport->destination_y = destination_y;
  6837. transport->destination_z = destination_z;
  6838. transport->destination_heading = destination_heading;
  6839. transport->cost = cost;
  6840. transport->unique_id = unique_id;
  6841. transport->min_level = min_level;
  6842. transport->max_level = max_level;
  6843. transport->req_quest = quest_req;
  6844. transport->req_quest_step = quest_step_req;
  6845. transport->req_quest_complete = quest_complete;
  6846. transport->map_x = map_x;
  6847. transport->map_y = map_y;
  6848. transport->expansion_flag = expansion_flag;
  6849. transport->holiday_flag = holiday_flag;
  6850. transport->min_client_version = min_client_version;
  6851. transport->max_client_version = max_client_version;
  6852. transport->flight_path_id = flight_path_id;
  6853. transport->mount_id = mount_id;
  6854. transport->mount_red_color = mount_red_color;
  6855. transport->mount_green_color = mount_green_color;
  6856. transport->mount_blue_color = mount_blue_color;
  6857. MTransporters.lock();
  6858. transporters[transport_id].push_back(transport);
  6859. MTransporters.unlock();
  6860. }
  6861. void ZoneServer::GetTransporters(vector<TransportDestination*>* returnList, Client* client, int32 transport_id){
  6862. if (!returnList)
  6863. return;
  6864. MTransporters.lock();
  6865. if (transporters.count(transport_id) > 0)
  6866. {
  6867. vector<TransportDestination*> list;
  6868. for (int i = 0; i < transporters[transport_id].size(); i++)
  6869. {
  6870. if (transporters[transport_id][i]->min_client_version && client->GetVersion() < transporters[transport_id][i]->min_client_version)
  6871. continue;
  6872. else if (transporters[transport_id][i]->max_client_version && client->GetVersion() > transporters[transport_id][i]->max_client_version)
  6873. continue;
  6874. if (database.CheckExpansionFlags(this, transporters[transport_id][i]->expansion_flag) && database.CheckHolidayFlags(this, transporters[transport_id][i]->holiday_flag))
  6875. {
  6876. returnList->push_back(transporters[transport_id][i]);
  6877. }
  6878. }
  6879. }
  6880. MTransporters.unlock();
  6881. }
  6882. MutexList<LocationTransportDestination*>* ZoneServer::GetLocationTransporters(int32 zone_id){
  6883. MutexList<LocationTransportDestination*>* ret = 0;
  6884. MTransporters.lock();
  6885. if(location_transporters.count(zone_id) > 0)
  6886. ret = location_transporters[zone_id];
  6887. MTransporters.unlock();
  6888. return ret;
  6889. }
  6890. void ZoneServer::DeleteGlobalTransporters(){
  6891. MTransporters.lock();
  6892. map<int32, vector<TransportDestination*> >::iterator itr;
  6893. vector<TransportDestination*>::iterator transport_vector_itr;
  6894. for(itr = transporters.begin(); itr != transporters.end(); itr++){
  6895. for(transport_vector_itr = itr->second.begin(); transport_vector_itr != itr->second.end(); transport_vector_itr++){
  6896. safe_delete(*transport_vector_itr);
  6897. }
  6898. }
  6899. map<int32, MutexList<LocationTransportDestination*>* >::iterator itr2;
  6900. for(itr2 = location_transporters.begin(); itr2 != location_transporters.end(); itr2++){
  6901. itr2->second->clear(true);
  6902. delete itr2->second;
  6903. }
  6904. transporters.clear();
  6905. location_transporters.clear();
  6906. MTransporters.unlock();
  6907. }
  6908. void ZoneServer::DeleteGlobalSpawns() {
  6909. ClearLootTables();
  6910. map<int32, NPC*>::iterator npc_list_iter;
  6911. for(npc_list_iter=npc_list.begin();npc_list_iter!=npc_list.end();npc_list_iter++) {
  6912. safe_delete(npc_list_iter->second);
  6913. }
  6914. npc_list.clear();
  6915. map<int32, Object*>::iterator object_list_iter;
  6916. for(object_list_iter=object_list.begin();object_list_iter!=object_list.end();object_list_iter++) {
  6917. safe_delete(object_list_iter->second);
  6918. }
  6919. object_list.clear();
  6920. map<int32, GroundSpawn*>::iterator groundspawn_list_iter;
  6921. for(groundspawn_list_iter=groundspawn_list.begin();groundspawn_list_iter!=groundspawn_list.end();groundspawn_list_iter++) {
  6922. safe_delete(groundspawn_list_iter->second);
  6923. }
  6924. groundspawn_list.clear();
  6925. map<int32, Widget*>::iterator widget_list_iter;
  6926. for(widget_list_iter=widget_list.begin();widget_list_iter!=widget_list.end();widget_list_iter++) {
  6927. safe_delete(widget_list_iter->second);
  6928. }
  6929. widget_list.clear();
  6930. map<int32, Sign*>::iterator sign_list_iter;
  6931. for(sign_list_iter=sign_list.begin();sign_list_iter!=sign_list.end();sign_list_iter++) {
  6932. safe_delete(sign_list_iter->second);
  6933. }
  6934. sign_list.clear();
  6935. /*map<int32, AppearanceData*>::iterator appearance_list_iter;
  6936. for(appearance_list_iter=npc_appearance_list.begin();appearance_list_iter!=npc_appearance_list.end();appearance_list_iter++) {
  6937. safe_delete(appearance_list_iter->second);
  6938. }
  6939. npc_appearance_list.clear();*/
  6940. ClearEntityCommands();
  6941. DeleteGroundSpawnItems();
  6942. DeleteGlobalTransporters();
  6943. DeleteTransporterMaps();
  6944. }
  6945. void ZoneServer::AddTransportMap(int32 id, string name) {
  6946. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  6947. m_transportMaps[id] = name;
  6948. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  6949. }
  6950. bool ZoneServer::TransportHasMap(int32 id) {
  6951. bool ret = false;
  6952. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  6953. ret = m_transportMaps.count(id) > 0;
  6954. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  6955. return ret;
  6956. }
  6957. string ZoneServer::GetTransportMap(int32 id) {
  6958. string ret;
  6959. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  6960. if (m_transportMaps.count(id) > 0)
  6961. ret = m_transportMaps[id];
  6962. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  6963. return ret;
  6964. }
  6965. void ZoneServer::DeleteTransporterMaps() {
  6966. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  6967. m_transportMaps.clear();
  6968. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  6969. }
  6970. void ZoneServer::ReloadSpawns() {
  6971. if (reloading)
  6972. return;
  6973. reloading = true;
  6974. world.SetReloadingSubsystem("Spawns");
  6975. // Let every one in the zone know what is happening
  6976. HandleBroadcast("Reloading all spawns for this zone.");
  6977. DeleteGlobalSpawns();
  6978. Depop(false, true);
  6979. }
  6980. void ZoneServer::SendStateCommand(Spawn* spawn, int32 state) {
  6981. vector<Client*>::iterator itr;
  6982. MClientList.readlock(__FUNCTION__, __LINE__);
  6983. for (itr = clients.begin(); itr != clients.end(); itr++) {
  6984. Client* client = *itr;
  6985. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  6986. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), state);
  6987. }
  6988. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6989. }
  6990. void ZoneServer::AddFlightPath(int32 id, FlightPathInfo* info) {
  6991. if (m_flightPaths.count(id) > 0) {
  6992. LogWrite(ZONE__ERROR, 0, "Zone", "Duplicate flight path (%u)", id);
  6993. safe_delete(info);
  6994. return;
  6995. }
  6996. m_flightPaths[id] = info;
  6997. }
  6998. void ZoneServer::AddFlightPathLocation(int32 id, FlightPathLocation* location) {
  6999. if (m_flightPaths.count(id) == 0) {
  7000. LogWrite(ZONE__ERROR, 0, "Zone", "There is no flight info for this route (%u)", id);
  7001. safe_delete(location);
  7002. return;
  7003. }
  7004. m_flightPathRoutes[id].push_back(location);
  7005. }
  7006. void ZoneServer::DeleteFlightPaths() {
  7007. map<int32, vector<FlightPathLocation*> >::iterator itr;
  7008. vector<FlightPathLocation*>::iterator itr2;
  7009. map<int32, FlightPathInfo*>::iterator itr3;
  7010. for (itr = m_flightPathRoutes.begin(); itr != m_flightPathRoutes.end(); itr++) {
  7011. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  7012. safe_delete(*itr2);
  7013. }
  7014. itr->second.clear();
  7015. }
  7016. m_flightPathRoutes.clear();
  7017. for (itr3 = m_flightPaths.begin(); itr3 != m_flightPaths.end(); itr3++) {
  7018. safe_delete(itr3->second);
  7019. }
  7020. m_flightPaths.clear();
  7021. }
  7022. void ZoneServer::SendFlightPathsPackets(Client* client) {
  7023. // Only send a packet if there are flight paths
  7024. if (m_flightPathRoutes.size() > 0) {
  7025. PacketStruct* packet = configReader.getStruct("WS_FlightPathsMsg", client->GetVersion());
  7026. if (packet) {
  7027. int32 num_routes = m_flightPaths.size();
  7028. packet->setArrayLengthByName("number_of_routes", num_routes);
  7029. packet->setArrayLengthByName("number_of_routes2", num_routes);
  7030. packet->setArrayLengthByName("number_of_routes3", num_routes);
  7031. packet->setArrayLengthByName("number_of_routes4", num_routes);
  7032. map<int32, FlightPathInfo*>::iterator itr;
  7033. int32 i = 0;
  7034. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, i++) {
  7035. packet->setArrayDataByName("route_length", m_flightPathRoutes[itr->first].size(), i);
  7036. packet->setArrayDataByName("ground_mount", itr->second->flying ? 0 : 1, i);
  7037. packet->setArrayDataByName("allow_dismount", itr->second->dismount ? 1 : 0, i);
  7038. packet->setSubArrayLengthByName("route_length2", m_flightPathRoutes[itr->first].size(), i);
  7039. vector<FlightPathLocation*>::iterator itr2;
  7040. int32 j = 0;
  7041. for (itr2 = m_flightPathRoutes[itr->first].begin(); itr2 != m_flightPathRoutes[itr->first].end(); itr2++, j++) {
  7042. packet->setSubArrayDataByName("x", (*itr2)->X, i, j);
  7043. packet->setSubArrayDataByName("y", (*itr2)->Y, i, j);
  7044. packet->setSubArrayDataByName("z", (*itr2)->Z, i, j);
  7045. }
  7046. }
  7047. client->QueuePacket(packet->serialize());
  7048. safe_delete(packet);
  7049. }
  7050. }
  7051. }
  7052. int32 ZoneServer::GetFlightPathIndex(int32 id) {
  7053. int32 index = 0;
  7054. map<int32, FlightPathInfo*>::iterator itr;
  7055. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, index++) {
  7056. if (itr->first == id)
  7057. return index;
  7058. }
  7059. return -1;
  7060. }
  7061. float ZoneServer::GetFlightPathSpeed(int32 id) {
  7062. float speed = 1;
  7063. if (m_flightPaths.count(id) > 0)
  7064. speed = m_flightPaths[id]->speed;
  7065. return speed;
  7066. }
  7067. void ZoneServer::ProcessSpawnConditional(int8 condition) {
  7068. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  7069. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7070. map<int32, Spawn*>::iterator itr;
  7071. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7072. if (itr->second != NULL) // null itr->second still coming into ProcessSpawnConditional
  7073. {
  7074. SpawnLocation* loc = spawn_location_list[itr->second->GetSpawnLocationID()];
  7075. if (loc && loc->conditional > 0) {
  7076. if ((loc->conditional & condition) != condition) {
  7077. Despawn(itr->second, 0);
  7078. }
  7079. }
  7080. }
  7081. }
  7082. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7083. map<int32, SpawnLocation*>::iterator itr2;
  7084. for (itr2 = spawn_location_list.begin(); itr2 != spawn_location_list.end(); itr2++) {
  7085. SpawnLocation* loc = itr2->second;
  7086. if (loc && loc->conditional > 0 && ((loc->conditional & condition) == condition))
  7087. if (GetSpawnByLocationID(loc->placement_id) == NULL)
  7088. ProcessSpawnLocation(loc);
  7089. }
  7090. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  7091. }
  7092. void ZoneServer::AddSpawnProximities(Spawn* newSpawn) {
  7093. Spawn* spawn = 0;
  7094. map<int32, Spawn*>::iterator itr;
  7095. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7096. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  7097. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7098. spawn = itr->second;
  7099. if (spawn && spawn != newSpawn) {
  7100. if (newSpawn->GetDatabaseID())
  7101. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7102. if (newSpawn->GetSpawnLocationID())
  7103. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7104. if (spawn->GetDatabaseID())
  7105. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7106. if (spawn->GetSpawnLocationID())
  7107. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7108. }
  7109. }
  7110. list<Spawn*>::iterator itr2;
  7111. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7112. spawn = *itr2;
  7113. if (spawn && spawn != newSpawn) {
  7114. if (newSpawn->GetDatabaseID())
  7115. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7116. if (newSpawn->GetSpawnLocationID())
  7117. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7118. if (spawn->GetDatabaseID())
  7119. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7120. if (spawn->GetSpawnLocationID())
  7121. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7122. }
  7123. }
  7124. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  7125. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7126. }
  7127. // we only call this inside a write lock
  7128. void ZoneServer::RemoveSpawnProximities(Spawn* oldSpawn) {
  7129. Spawn* spawn = 0;
  7130. map<int32, Spawn*>::iterator itr;
  7131. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7132. spawn = itr->second;
  7133. if (spawn && spawn != oldSpawn) {
  7134. if (oldSpawn->GetDatabaseID())
  7135. spawn->RemoveSpawnFromProximity(oldSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7136. if (oldSpawn->GetSpawnLocationID())
  7137. spawn->RemoveSpawnFromProximity(oldSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7138. // don't need to remove oldSpawn proximities, we clear them all out
  7139. }
  7140. }
  7141. }
  7142. void ZoneServer::SetSpawnScript(SpawnEntry* entry, Spawn* spawn)
  7143. {
  7144. if (!entry || !spawn)
  7145. return;
  7146. const char* script = 0;
  7147. for (int x = 0; x < 3; x++)
  7148. {
  7149. switch (x)
  7150. {
  7151. case 0:
  7152. script = world.GetSpawnEntryScript(entry->spawn_entry_id);
  7153. break;
  7154. case 1:
  7155. script = world.GetSpawnLocationScript(entry->spawn_location_id);
  7156. break;
  7157. case 2:
  7158. script = world.GetSpawnScript(entry->spawn_id);
  7159. break;
  7160. }
  7161. if (script && lua_interface && lua_interface->GetSpawnScript(script) != 0)
  7162. {
  7163. spawn->SetSpawnScript(string(script));
  7164. break;
  7165. }
  7166. }
  7167. }
  7168. vector<HouseItem> ZoneServer::GetHouseItems(Client* client)
  7169. {
  7170. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7171. return std::vector<HouseItem>();
  7172. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7173. std::vector<HouseItem> items;
  7174. map<int32, Spawn*>::iterator itr;
  7175. Spawn* spawn = 0;
  7176. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7177. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7178. spawn = itr->second;
  7179. if (spawn && spawn->IsObject() && spawn->GetPickupItemID())
  7180. {
  7181. HouseItem tmpItem;
  7182. tmpItem.item_id = spawn->GetPickupItemID();
  7183. tmpItem.unique_id = spawn->GetPickupUniqueItemID();
  7184. tmpItem.spawn_id = spawn->GetID();
  7185. tmpItem.item = master_item_list.GetItem(spawn->GetPickupItemID());
  7186. if (!tmpItem.item)
  7187. continue;
  7188. items.push_back(tmpItem);
  7189. }
  7190. }
  7191. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7192. return items;
  7193. }
  7194. void ZoneServer::SendHouseItems(Client* client)
  7195. {
  7196. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7197. return;
  7198. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7199. std::vector<HouseItem> items = GetHouseItems(client);
  7200. // setting this to 1 puts it on the door widget
  7201. packet->setDataByName("is_widget_door", 1);
  7202. packet->setArrayLengthByName("num_items", items.size());
  7203. for (int i = 0; i < items.size(); i++)
  7204. {
  7205. HouseItem tmpItem = items[i];
  7206. packet->setArrayDataByName("unique_id", tmpItem.unique_id, i); // unique_id is in fact the item_id...
  7207. packet->setArrayDataByName("item_name", tmpItem.item->name.c_str(), i);
  7208. packet->setArrayDataByName("status_reduction", tmpItem.item->houseitem_info->status_rent_reduction, i);
  7209. // location, 0 = floor, 1 = ceiling
  7210. //packet->setArrayDataByName("location", 1, i, 0);
  7211. // item_state int8
  7212. // 0 = normal (cannot pick up item / move item / toggle visibility)
  7213. // 1 = virtual (toggle visibility available, no move item)
  7214. // 2 = hidden (cannot pick up item / move item / toggle visibility)
  7215. // 3 = virtual/hidden/toggle visibility
  7216. // 4 = none (cannot pick up item / move item / toggle visibility)
  7217. // 5 = none, toggle visibility (cannot pick up item / move item)
  7218. // 8 = none (cannot pick up item / move item / toggle visibility)
  7219. //packet->setArrayDataByName("item_state", tmpvalue, i, 0);
  7220. // makes it so we don't have access to move item/retrieve item
  7221. // cannot use in conjunction with ui_tab_flag1/ui_tab_flag2
  7222. //packet->setArrayDataByName("tradeable", 1, i);
  7223. //packet->setArrayDataByName("item_description", "failboat", i);
  7224. // access to move item/retrieve item, do not use in conjunction with tradeable
  7225. packet->setArrayDataByName("ui_tab_flag1", 1, i, 0);
  7226. packet->setArrayDataByName("ui_tab_flag2", 1, i, 0);
  7227. // both of these can serve as description fields (only one should be used they populate the same area below the item name)
  7228. //packet->setArrayDataByName("first_item_description", "test", i);
  7229. //packet->setArrayDataByName("second_item_description", "Description here!", i);
  7230. packet->setArrayDataByName("icon", tmpItem.item->details.icon, i);
  7231. }
  7232. EQ2Packet* pack = packet->serialize();
  7233. client->QueuePacket(pack);
  7234. safe_delete(packet);
  7235. }
  7236. Spawn* ZoneServer::GetSpawnFromUniqueItemID(int32 unique_id)
  7237. {
  7238. if (!GetInstanceID() || GetInstanceType() != Instance_Type::PERSONAL_HOUSE_INSTANCE)
  7239. return nullptr;
  7240. map<int32, Spawn*>::iterator itr;
  7241. Spawn* spawn = 0;
  7242. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7243. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7244. spawn = itr->second;
  7245. if (spawn && spawn->IsObject() && spawn->GetPickupUniqueItemID() == unique_id)
  7246. {
  7247. Spawn* tmpSpawn = spawn;
  7248. MSpawnList.releasereadlock();
  7249. return tmpSpawn;
  7250. }
  7251. }
  7252. MSpawnList.releasereadlock();
  7253. return nullptr;
  7254. }
  7255. void ZoneServer::AddPendingSpawnRemove(int32 id)
  7256. {
  7257. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7258. m_pendingSpawnRemove.insert(make_pair(id,true));
  7259. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7260. }
  7261. void ZoneServer::ProcessSpawnRemovals()
  7262. {
  7263. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7264. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7265. if (m_pendingSpawnRemove.size() > 0) {
  7266. map<int32,bool>::iterator itr2;
  7267. for (itr2 = m_pendingSpawnRemove.begin(); itr2 != m_pendingSpawnRemove.end(); itr2++) {
  7268. spawn_list.erase(itr2->first);
  7269. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].erase(itr2->first);
  7270. std::map<int32,int32>::iterator hsmitr = housing_spawn_map.find(itr2->first);
  7271. if(hsmitr != housing_spawn_map.end()) {
  7272. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].erase(hsmitr->second);
  7273. housing_spawn_map.erase(hsmitr);
  7274. }
  7275. }
  7276. m_pendingSpawnRemove.clear();
  7277. }
  7278. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7279. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7280. }
  7281. void ZoneServer::AddSpawnToGroup(Spawn* spawn, int32 group_id)
  7282. {
  7283. if( spawn->GetSpawnGroupID() > 0 )
  7284. spawn->RemoveSpawnFromGroup();
  7285. MutexList<int32>* groupList = &spawn_group_map.Get(group_id);
  7286. MutexList<int32>::iterator itr2 = groupList->begin();
  7287. while(itr2.Next())
  7288. {
  7289. Spawn* groupSpawn = GetSpawnByID(itr2.value);
  7290. if(groupSpawn)
  7291. {
  7292. // found existing group member to add it in
  7293. spawn->AddSpawnToGroup(groupSpawn);
  7294. break;
  7295. }
  7296. }
  7297. groupList->Add(spawn->GetID());
  7298. spawn->SetSpawnGroupID(group_id);
  7299. }
  7300. void ZoneServer::QueueStateCommandToClients(int32 spawn_id, int32 state)
  7301. {
  7302. if(spawn_id < 1)
  7303. return;
  7304. MLuaQueueStateCmd.lock();
  7305. lua_queued_state_commands.insert(make_pair(spawn_id, state));
  7306. MLuaQueueStateCmd.unlock();
  7307. }
  7308. void ZoneServer::QueueDefaultCommand(int32 spawn_id, std::string command, float distance)
  7309. {
  7310. if(spawn_id < 1)
  7311. return;
  7312. MLuaQueueStateCmd.lock();
  7313. lua_spawn_update_command[spawn_id].insert(make_pair(command,distance));
  7314. MLuaQueueStateCmd.unlock();
  7315. }
  7316. void ZoneServer::ProcessQueuedStateCommands() // in a client list lock only
  7317. {
  7318. vector<Client*>::iterator itr;
  7319. MLuaQueueStateCmd.lock();
  7320. if(lua_queued_state_commands.size() > 0)
  7321. {
  7322. std::map<int32, int32>::iterator statecmds;
  7323. for(statecmds = lua_queued_state_commands.begin(); statecmds != lua_queued_state_commands.end(); statecmds++)
  7324. {
  7325. Spawn* spawn = GetSpawnByID(statecmds->first, false);
  7326. if(!spawn)
  7327. continue;
  7328. MClientList.readlock(__FUNCTION__, __LINE__);
  7329. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7330. Client* client = *itr;
  7331. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7332. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), statecmds->second);
  7333. }
  7334. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7335. }
  7336. lua_queued_state_commands.clear();
  7337. }
  7338. if(lua_spawn_update_command.size() > 0)
  7339. {
  7340. std::map<int32, std::map<std::string,float>>::iterator updatecmds;
  7341. for(updatecmds = lua_spawn_update_command.begin(); updatecmds != lua_spawn_update_command.end(); updatecmds++)
  7342. {
  7343. Spawn* spawn = GetSpawnByID(updatecmds->first, false);
  7344. if(!spawn)
  7345. continue;
  7346. std::map<std::string,float>::iterator innermap;
  7347. for(innermap = lua_spawn_update_command[updatecmds->first].begin(); innermap != lua_spawn_update_command[updatecmds->first].end(); innermap++)
  7348. {
  7349. MClientList.readlock(__FUNCTION__, __LINE__);
  7350. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7351. Client* client = *itr;
  7352. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7353. client->SendDefaultCommand(spawn, innermap->first.c_str(), innermap->second);
  7354. }
  7355. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7356. }
  7357. lua_spawn_update_command[updatecmds->first].clear();
  7358. }
  7359. lua_spawn_update_command.clear();
  7360. }
  7361. MLuaQueueStateCmd.unlock();
  7362. }
  7363. void ZoneServer::UpdateClientSpawnMap(Player* player, Client* client)
  7364. {
  7365. // client may be null when passed
  7366. client_spawn_map.Put(player, client);
  7367. }
  7368. void ZoneServer::RemoveClientsFromZone(ZoneServer* zone) {
  7369. vector<Client*>::iterator itr;
  7370. MClientList.readlock(__FUNCTION__, __LINE__);
  7371. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7372. Client* client = *itr;
  7373. if(client->GetCurrentZone() == zone) {
  7374. client->SetCurrentZone(nullptr);
  7375. }
  7376. if(client->GetZoningDestination() == zone) {
  7377. client->SetZoningDestination(nullptr);
  7378. }
  7379. }
  7380. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7381. }
  7382. void ZoneServer::SendSubSpawnUpdates(SUBSPAWN_TYPES subtype) {
  7383. std::map<int32, Spawn*>::iterator subitr;
  7384. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7385. for(subitr = subspawn_list[subtype].begin(); subitr != subspawn_list[subtype].end(); subitr++) {
  7386. subitr->second->changed = true;
  7387. subitr->second->info_changed = true;
  7388. AddChangedSpawn(subitr->second);
  7389. }
  7390. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7391. }
  7392. bool ZoneServer::HouseItemSpawnExists(int32 item_id) {
  7393. bool exists = false;
  7394. std::map<int32, Spawn*>::iterator subitr;
  7395. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7396. subitr = subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].find(item_id);
  7397. if(subitr != subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].end()) {
  7398. exists = true;
  7399. }
  7400. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7401. return exists;
  7402. }
  7403. void ZoneServer::ProcessPendingSpawns() {
  7404. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7405. MPendingSpawnListAdd.writelock(__FUNCTION__, __LINE__);
  7406. list<Spawn*>::iterator itr2;
  7407. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7408. Spawn* spawn = *itr2;
  7409. if (spawn)
  7410. spawn_list[spawn->GetID()] = spawn;
  7411. if(spawn->IsCollector()) {
  7412. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].insert(make_pair(spawn->GetID(),spawn));
  7413. }
  7414. if(spawn->GetPickupItemID()) {
  7415. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].insert(make_pair(spawn->GetPickupItemID(),spawn));
  7416. housing_spawn_map.insert(make_pair(spawn->GetID(), spawn->GetPickupItemID()));
  7417. }
  7418. }
  7419. pending_spawn_list_add.clear();
  7420. MPendingSpawnListAdd.releasewritelock(__FUNCTION__, __LINE__);
  7421. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7422. }
  7423. void ZoneServer::AddSpawnToGrid(Spawn* spawn, int32 grid_id) {
  7424. if(spawn->GetID() == 0 || spawn->IsDeletedSpawn())
  7425. return;
  7426. MGridMaps.lock_shared();
  7427. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7428. if(grids != grid_maps.end()) {
  7429. grids->second->MSpawns.lock();
  7430. grids->second->spawns.insert(make_pair(spawn->GetID(), spawn));
  7431. grids->second->MSpawns.unlock();
  7432. }
  7433. else {
  7434. MGridMaps.unlock_shared();
  7435. MGridMaps.lock();
  7436. GridMap* gm = new GridMap;
  7437. gm->grid_id = grid_id;
  7438. gm->spawns.insert(make_pair(spawn->GetID(), spawn));
  7439. grid_maps.insert(make_pair(grid_id, gm));
  7440. MGridMaps.unlock();
  7441. return;
  7442. }
  7443. MGridMaps.unlock_shared();
  7444. }
  7445. void ZoneServer::RemoveSpawnFromGrid(Spawn* spawn, int32 grid_id) {
  7446. std::shared_lock lock(MGridMaps);
  7447. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7448. if(grids != grid_maps.end()) {
  7449. grids->second->MSpawns.lock();
  7450. if(grids->second->spawns.count(spawn->GetID()) > 0) {
  7451. grids->second->spawns.erase(spawn->GetID());
  7452. }
  7453. grids->second->MSpawns.unlock();
  7454. }
  7455. }
  7456. int32 ZoneServer::GetSpawnCountInGrid(int32 grid_id) {
  7457. int32 count = 0;
  7458. std::shared_lock lock(MGridMaps);
  7459. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7460. if(grids != grid_maps.end()) {
  7461. grids->second->MSpawns.lock_shared();
  7462. count = grids->second->spawns.size();
  7463. grids->second->MSpawns.unlock_shared();
  7464. }
  7465. return count;
  7466. }
  7467. void ZoneServer::SendClientSpawnListInGrid(Client* client, int32 grid_id){
  7468. std::shared_lock lock(MGridMaps);
  7469. Spawn* spawn = nullptr;
  7470. client->Message(CHANNEL_COLOR_RED, "Grid ID %u has %u spawns.", grid_id, GetSpawnCountInGrid(grid_id));
  7471. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7472. if(grids != grid_maps.end()) {
  7473. grids->second->MSpawns.lock_shared();
  7474. typedef map <int32, Spawn*> SpawnMapType;
  7475. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  7476. spawn = it->second;
  7477. 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());
  7478. }
  7479. grids->second->MSpawns.unlock_shared();
  7480. }
  7481. }