zoneserver.cpp 298 KB

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