zoneserver.cpp 298 KB

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