zoneserver.cpp 292 KB

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