zoneserver.cpp 312 KB

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