zoneserver.cpp 293 KB

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