zoneserver.cpp 298 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723
  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. int32 encounter_player_bot_count = 1;
  4071. bool killer_in_encounter = false;
  4072. int8 loot_state = dead->GetLockedNoLoot();
  4073. if(dead->IsEntity())
  4074. {
  4075. // add any special quest related loot (no_drop_quest_completed)
  4076. if(dead->IsNPC() && ((NPC*)dead)->Brain()) {
  4077. if(!((NPC*)dead)->Brain()->PlayerInEncounter() || (loot_state != ENCOUNTER_STATE_LOCKED && loot_state != ENCOUNTER_STATE_OVERMATCHED)) {
  4078. LogWrite(LOOT__DEBUG, 0, "Loot", "NPC %s bypassed loot drop due to no player in encounter, or encounter state not locked.", ((NPC*)dead)->GetName());
  4079. }
  4080. else {
  4081. Entity* hated = ((NPC*)dead)->Brain()->GetMostHated();
  4082. if(hated) {
  4083. AddLoot((NPC*)dead, hated);
  4084. }
  4085. }
  4086. }
  4087. ((Entity*)dead)->InCombat(false);
  4088. dead->SetInitialState(16512, false); // This will make aerial npc's fall after death
  4089. dead->SetHP(0);
  4090. dead->SetSpawnType(3);
  4091. dead->appearance.attackable = 0;
  4092. // Remove hate towards dead from all npc's in the zone
  4093. ClearHate((Entity*)dead);
  4094. // Check kill and death procs
  4095. if (killer && dead != killer){
  4096. if (dead->IsEntity())
  4097. ((Entity*)dead)->CheckProcs(PROC_TYPE_DEATH, killer);
  4098. if (killer->IsEntity())
  4099. ((Entity*)killer)->CheckProcs(PROC_TYPE_KILL, dead);
  4100. }
  4101. //Check if caster is alive after death proc called, incase of deathsave
  4102. if (dead->Alive())
  4103. return;
  4104. RemoveSpellTimersFromSpawn(dead, true, !dead->IsPlayer(), true, !isSpell);
  4105. ((Entity*)dead)->IsCasting(false);
  4106. if(dead->IsPlayer())
  4107. {
  4108. ((Player*)dead)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_DEATHS, 1);
  4109. client = GetClientBySpawn(dead);
  4110. ((Entity*)dead)->HandleDeathExperienceDebt(killer);
  4111. if(client) {
  4112. if(client->GetPlayer()->DamageEquippedItems(10, client))
  4113. client->QueuePacket(client->GetPlayer()->GetEquipmentList()->serialize(client->GetVersion(), client->GetPlayer()));
  4114. client->DisplayDeadWindow();
  4115. }
  4116. }
  4117. else if (dead->IsNPC()) {
  4118. encounter = ((NPC*)dead)->Brain()->GetEncounter();
  4119. encounter_player_bot_count = ((NPC*)dead)->Brain()->CountPlayerBotInEncounter();
  4120. if(encounter_player_bot_count < 1)
  4121. encounter_player_bot_count = 1;
  4122. }
  4123. }
  4124. dead->SetActionState(0);
  4125. dead->SetTempActionState(0);
  4126. // Needs npc to have access to the encounter list for who is allowed to loot
  4127. NPC* chest = 0;
  4128. if (dead->IsNPC() && !((NPC*)dead)->Brain()->PlayerInEncounter()) {
  4129. dead->SetLootCoins(0);
  4130. dead->ClearLoot();
  4131. }
  4132. Spawn* groupMemberAlive = nullptr;
  4133. // If dead has loot attempt to drop a chest
  4134. if (dead->HasLoot()) {
  4135. if(!(groupMemberAlive = dead->IsSpawnGroupMembersAlive(dead))) {
  4136. chest = ((Entity*)dead)->DropChest();
  4137. }
  4138. else {
  4139. switch(dead->GetLootDropType()) {
  4140. case 0:
  4141. // default drop all chest type as a group
  4142. dead->TransferLoot(groupMemberAlive);
  4143. break;
  4144. case 1:
  4145. // this is a primary mob it drops its own loot
  4146. chest = ((Entity*)dead)->DropChest();
  4147. break;
  4148. }
  4149. }
  4150. }
  4151. // If dead is an npc get the encounter and loop through it giving out the rewards, no rewards for pets
  4152. if (dead->IsNPC() && !dead->IsPet() && !dead->IsBot()) {
  4153. Spawn* spawn = 0;
  4154. int8 size = encounter->size();
  4155. for (int8 i = 0; i < encounter->size(); i++) {
  4156. spawn = GetSpawnByID(encounter->at(i), spawnListLocked);
  4157. // set a flag to let us know if the killer is in the encounter
  4158. if (!killer_in_encounter && spawn == killer)
  4159. killer_in_encounter = true;
  4160. if (spawn && spawn->IsPlayer()) {
  4161. // Update players total kill count
  4162. ((Player*)spawn)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_NPC_KILLS, 1);
  4163. // If this was an epic mob kill send the announcement for this player
  4164. if (dead->GetEncounterLevel() >= 10)
  4165. SendEpicMobDeathToGuild((Player*)spawn, dead);
  4166. // Clear hostile spells from the players spell queue
  4167. spellProcess->RemoveSpellFromQueue((Player*)spawn, true);
  4168. // Get the client of the player
  4169. client = GetClientBySpawn(spawn);
  4170. // valid client?
  4171. if (client) {
  4172. // Check for quest kill updates
  4173. if(!dead->IsNPC() || loot_state != ENCOUNTER_STATE_BROKEN) {
  4174. client->CheckPlayerQuestsKillUpdate(dead);
  4175. }
  4176. // 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
  4177. if (!dead->IsPlayer() && dead->GetFactionID() > 10)
  4178. ProcessFaction(dead, client);
  4179. // Send xp...this is currently wrong fix it
  4180. if (spawn != dead && ((Player*)spawn)->GetArrowColor(dead->GetLevel()) >= ARROW_COLOR_GREEN) {
  4181. //SendCalculatedXP((Player*)spawn, dead);
  4182. float xp = ((Player*)spawn)->CalculateXP(dead) / encounter_player_bot_count;
  4183. if (xp > 0) {
  4184. ((Player*)spawn)->AddXP((int32)xp);
  4185. }
  4186. }
  4187. }
  4188. }
  4189. // If a chest is being dropped add this spawn to the chest's encounter so they can loot it
  4190. if (chest && spawn && spawn->IsEntity())
  4191. chest->Brain()->AddToEncounter((Entity*)spawn);
  4192. }
  4193. }
  4194. // If a chest is being dropped add it to the world and set the timer to remove it.
  4195. if (chest) {
  4196. AddSpawn(chest);
  4197. AddDeadSpawn(chest, 0xFFFFFFFF);
  4198. LogWrite(LOOT__DEBUG, 0, "Loot", "Adding a chest to the world...");
  4199. }
  4200. // Reset client pointer
  4201. client = 0;
  4202. // Killer was not in the encounter, give them the faction hit but no xp
  4203. if (!killer_in_encounter) {
  4204. // make sure the killer is a player and the dead spawn had a faction and wasn't a player
  4205. if (killer && killer->IsPlayer()) {
  4206. if (!dead->IsPlayer() && dead->GetFactionID() > 10) {
  4207. client = GetClientBySpawn(killer);
  4208. if (client)
  4209. ProcessFaction(dead, client);
  4210. }
  4211. // Clear hostile spells from the killers spell queue
  4212. spellProcess->RemoveSpellFromQueue((Player*)killer, true);
  4213. }
  4214. }
  4215. // Reset client pointer
  4216. client = 0;
  4217. vector<Spawn*>* group = dead->GetSpawnGroup();
  4218. if (group && group->size() == 1)
  4219. CallSpawnScript(dead, SPAWN_SCRIPT_GROUP_DEAD, killer);
  4220. safe_delete(group);
  4221. // Remove the support functions for the dead spawn
  4222. RemoveSpawnSupportFunctions(dead, !isSpell);
  4223. // Erase the expire timer if it has one
  4224. if (spawn_expire_timers.count(dead->GetID()) > 0)
  4225. spawn_expire_timers.erase(dead->GetID());
  4226. // If dead is an npc or object call the spawn scrip and handle instance stuff
  4227. if(dead->IsNPC() || dead->IsObject())
  4228. {
  4229. // handle instance spawn db info
  4230. // we don't care if a NPC or a client kills the spawn, we could have events that cause NPCs to kill NPCs.
  4231. if(dead->GetZone()->GetInstanceID() > 0 && dead->GetSpawnLocationID() > 0)
  4232. {
  4233. // use respawn time to either insert/update entry (likely insert in this situation)
  4234. if(dead->IsNPC())
  4235. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4236. else if ( dead->IsObject ( ) )
  4237. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  4238. }
  4239. // Call the spawn scripts death() function
  4240. CallSpawnScript(dead, SPAWN_SCRIPT_DEATH, killer);
  4241. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  4242. if (zone_script && lua_interface)
  4243. lua_interface->RunZoneScript(zone_script, "spawn_killed", this, dead, 0, 0, killer);
  4244. }
  4245. int32 victim_id = dead->GetID();
  4246. int32 attacker_id = 0xFFFFFFFF;
  4247. if(killer)
  4248. attacker_id = killer->GetID();
  4249. if(send_packet)
  4250. {
  4251. vector<Client*>::iterator client_itr;
  4252. MClientList.readlock(__FUNCTION__, __LINE__);
  4253. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4254. client = *client_itr;
  4255. if(!client->GetPlayer()->WasSentSpawn(victim_id) || (attacker_id != 0xFFFFFFFF && !client->GetPlayer()->WasSentSpawn(attacker_id)) )
  4256. continue;
  4257. else if(killer && killer->GetDistance(client->GetPlayer()) > HEAR_SPAWN_DISTANCE)
  4258. continue;
  4259. packet = configReader.getStruct("WS_HearDeath", client->GetVersion());
  4260. if(packet)
  4261. {
  4262. if(killer)
  4263. packet->setDataByName("attacker", client->GetPlayer()->GetIDWithPlayerSpawn(killer));
  4264. else
  4265. packet->setDataByName("attacker", 0xFFFFFFFF);
  4266. packet->setDataByName("defender", client->GetPlayer()->GetIDWithPlayerSpawn(dead));
  4267. packet->setDataByName("damage_type", damage_type);
  4268. packet->setDataByName("blow_type", kill_blow_type);
  4269. client->QueuePacket(packet->serialize());
  4270. LogWrite(COMBAT__DEBUG, 0, "Combat", "Zone Killing '%s'", dead->GetName());
  4271. safe_delete(packet);
  4272. }
  4273. }
  4274. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4275. }
  4276. int32 pop_timer = 0xFFFFFFFF;
  4277. if(killer && killer->IsNPC())
  4278. {
  4279. // Call the spawn scripts killed() function
  4280. CallSpawnScript(killer, SPAWN_SCRIPT_KILLED, dead);
  4281. if(!dead->IsPlayer())
  4282. {
  4283. LogWrite(MISC__TODO, 1, "TODO", "Whenever pets are added, check for pet kills\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  4284. // Set the time for the corpse to linger to 5 sec
  4285. //pop_timer = 5000;
  4286. // commented out the timer so in the event the killer is not a player (pet, guard, something else i haven't thought of)
  4287. // the corpse doesn't vanish sooner then it should if it had loot for the players. AddDeadSpawn() will set timers based on if
  4288. // the corpse has loot or not if the timer value (pop_timer) is 0xFFFFFFFF
  4289. }
  4290. }
  4291. // If the dead spawns was not a player add it to the dead spawn list
  4292. if (!dead->IsPlayer() && !dead->IsBot())
  4293. AddDeadSpawn(dead, pop_timer);
  4294. // if dead was a player clear hostile spells from its spell queue
  4295. if (dead->IsPlayer())
  4296. spellProcess->RemoveSpellFromQueue((Player*)dead, true);
  4297. if (dead->IsNPC())
  4298. ((NPC*)dead)->Brain()->ClearHate();
  4299. safe_delete(encounter);
  4300. }
  4301. void ZoneServer::SendDamagePacket(Spawn* attacker, Spawn* victim, int8 type1, int8 type2, int8 damage_type, int16 damage, const char* spell_name) {
  4302. //Scat: was set but never being used anywhere. i see references to 0xFFFFFFFF below so could be old code not used anymore
  4303. //int32 attacker_id = 0xFFFFFFFF;
  4304. //if(attacker)
  4305. // attacker_id = attacker->GetID();
  4306. PacketStruct* packet = 0;
  4307. Client* client = 0;
  4308. if (attacker && victim && victim->IsPlayer() && victim->GetTarget() == 0) {
  4309. client = GetClientBySpawn(victim);
  4310. if (client)
  4311. client->TargetSpawn(attacker);
  4312. }
  4313. if(damage_type == DAMAGE_PACKET_DAMAGE_TYPE_FOCUS) {
  4314. damage_type = 0;
  4315. type2 = DAMAGE_PACKET_RESULT_FOCUS;
  4316. }
  4317. vector<Client*>::iterator client_itr;
  4318. MClientList.readlock(__FUNCTION__, __LINE__);
  4319. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4320. client = *client_itr;
  4321. if (!client || (client->GetPlayer() != attacker && client->GetPlayer() != victim && ((attacker && client->GetPlayer()->WasSentSpawn(attacker->GetID()) == false) || (victim && client->GetPlayer()->WasSentSpawn(victim->GetID()) == false))))
  4322. continue;
  4323. if (attacker && attacker->GetDistance(client->GetPlayer()) > 50)
  4324. continue;
  4325. if (victim && victim->GetDistance(client->GetPlayer()) > 50)
  4326. continue;
  4327. switch (type1) {
  4328. case DAMAGE_PACKET_TYPE_SIPHON_SPELL:
  4329. case DAMAGE_PACKET_TYPE_SIPHON_SPELL2:
  4330. packet = configReader.getStruct("WS_HearSiphonSpellDamage", client->GetVersion());
  4331. break;
  4332. case DAMAGE_PACKET_TYPE_MULTIPLE_DAMAGE:
  4333. if (client->GetVersion() > 546)
  4334. packet = configReader.getStruct("WS_HearMultipleDamage", client->GetVersion());
  4335. else
  4336. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4337. break;
  4338. case DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG:
  4339. case DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE:
  4340. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4341. break;
  4342. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE2:
  4343. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE3:
  4344. case DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG:
  4345. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE:
  4346. if (client->GetVersion() > 546)
  4347. packet = configReader.getStruct("WS_HearSpellDamage", client->GetVersion());
  4348. else
  4349. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  4350. if (packet)
  4351. packet->setSubstructDataByName("header", "unknown", 5);
  4352. break;
  4353. case DAMAGE_PACKET_TYPE_RANGE_DAMAGE:
  4354. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4355. break;
  4356. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG:
  4357. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG2:
  4358. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  4359. break;
  4360. default:
  4361. LogWrite(ZONE__ERROR, 0, "Zone", "Unknown Damage Packet type: %i in ZoneServer::SendDamagePacket.", type1);
  4362. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4363. return;
  4364. }
  4365. if (packet) {
  4366. if (client->GetVersion() > 546) {
  4367. packet->setSubstructDataByName("header", "packet_type", type1);
  4368. packet->setSubstructDataByName("header", "result_type", type2);
  4369. packet->setDataByName("damage_type", damage_type);
  4370. packet->setDataByName("damage", damage);
  4371. }
  4372. else {
  4373. switch (type2) {
  4374. case DAMAGE_PACKET_RESULT_MISS:
  4375. packet->setSubstructDataByName("header", "result_type", 1);
  4376. break;
  4377. case DAMAGE_PACKET_RESULT_DODGE:
  4378. packet->setSubstructDataByName("header", "result_type", 2);
  4379. break;
  4380. case DAMAGE_PACKET_RESULT_PARRY:
  4381. packet->setSubstructDataByName("header", "result_type", 3);
  4382. break;
  4383. case DAMAGE_PACKET_RESULT_RIPOSTE:
  4384. packet->setSubstructDataByName("header", "result_type", 4);
  4385. break;
  4386. case DAMAGE_PACKET_RESULT_BLOCK:
  4387. packet->setSubstructDataByName("header", "result_type", 5);
  4388. break;
  4389. case DAMAGE_PACKET_RESULT_INVULNERABLE:
  4390. packet->setSubstructDataByName("header", "result_type", 7);
  4391. break;
  4392. case DAMAGE_PACKET_RESULT_RESIST:
  4393. packet->setSubstructDataByName("header", "result_type", 9);
  4394. break;
  4395. case DAMAGE_PACKET_RESULT_REFLECT:
  4396. packet->setSubstructDataByName("header", "result_type", 10);
  4397. break;
  4398. case DAMAGE_PACKET_RESULT_IMMUNE:
  4399. packet->setSubstructDataByName("header", "result_type", 11);
  4400. break;
  4401. }
  4402. packet->setArrayLengthByName("num_dmg", 1);
  4403. packet->setSubstructDataByName("header", "defender_proxy", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4404. packet->setArrayDataByName("damage_type", damage_type);
  4405. packet->setArrayDataByName("damage", damage);
  4406. }
  4407. if (!attacker)
  4408. packet->setSubstructDataByName("header", "attacker", 0xFFFFFFFF);
  4409. else
  4410. packet->setSubstructDataByName("header", "attacker", client->GetPlayer()->GetIDWithPlayerSpawn(attacker));
  4411. packet->setSubstructDataByName("header", "defender", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  4412. if (spell_name) {
  4413. packet->setDataByName("spell", 1);
  4414. packet->setDataByName("spell_name", spell_name);
  4415. }
  4416. EQ2Packet* app = packet->serialize();
  4417. //DumpPacket(app);
  4418. client->QueuePacket(app);
  4419. safe_delete(packet);
  4420. packet = 0;
  4421. }
  4422. }
  4423. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4424. }
  4425. void ZoneServer::SendHealPacket(Spawn* caster, Spawn* target, int16 heal_type, int32 heal_amt, const char* spell_name){
  4426. Client* client = 0;
  4427. vector<Client*>::iterator client_itr;
  4428. MClientList.readlock(__FUNCTION__, __LINE__);
  4429. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4430. client = *client_itr;
  4431. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4432. continue;
  4433. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4434. continue;
  4435. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4436. continue;
  4437. PacketStruct* packet = configReader.getStruct("WS_HearHeal", client->GetVersion());
  4438. if (packet) {
  4439. packet->setDataByName("caster", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4440. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4441. packet->setDataByName("heal_amt", heal_amt);
  4442. packet->setDataByName("spellname", spell_name);
  4443. packet->setDataByName("type", heal_type);
  4444. packet->setDataByName("unknown2", 1);
  4445. EQ2Packet* app = packet->serialize();
  4446. client->QueuePacket(app);
  4447. safe_delete(packet);
  4448. }
  4449. }
  4450. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4451. }
  4452. void ZoneServer::SendThreatPacket(Spawn* caster, Spawn* target, int32 threat_amt, const char* spell_name) {
  4453. Client* client = 0;
  4454. vector<Client*>::iterator client_itr;
  4455. MClientList.readlock(__FUNCTION__, __LINE__);
  4456. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4457. client = *client_itr;
  4458. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  4459. continue;
  4460. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  4461. continue;
  4462. if(target && target->GetDistance(client->GetPlayer()) > 50)
  4463. continue;
  4464. PacketStruct* packet = configReader.getStruct("WS_HearThreatCmd", client->GetVersion());
  4465. if (packet) {
  4466. packet->setDataByName("spell_name", spell_name);
  4467. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4468. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4469. packet->setDataByName("threat_amount", threat_amt);
  4470. client->QueuePacket(packet->serialize());
  4471. }
  4472. safe_delete(packet);
  4473. }
  4474. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4475. }
  4476. void ZoneServer::SendYellPacket(Spawn* yeller, float max_distance) {
  4477. Client* client = 0;
  4478. string yellMsg = std::string(yeller->GetName()) + " yelled for help!";
  4479. vector<Client*>::iterator client_itr;
  4480. PacketStruct* packet = nullptr;
  4481. MClientList.readlock(__FUNCTION__, __LINE__);
  4482. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4483. client = *client_itr;
  4484. if(!client || !client->GetPlayer() || client->GetPlayer()->WasSentSpawn(yeller->GetID()) == false)
  4485. continue;
  4486. if(client->GetPlayer()->GetDistance(yeller) > max_distance)
  4487. continue;
  4488. if(packet && packet->GetVersion() == client->GetVersion()) {
  4489. client->QueuePacket(packet->serialize());
  4490. }
  4491. else {
  4492. safe_delete(packet);
  4493. packet = configReader.getStruct("WS_EncounterBroken", client->GetVersion());
  4494. if (packet) {
  4495. packet->setDataByName("message", yellMsg.c_str());
  4496. /* none of the other data seems necessary, keeping for reference for future disassembly
  4497. packet2->setDataByName("unknown2", 0x40);
  4498. packet2->setDataByName("unknown3", 0x40);
  4499. packet2->setDataByName("unknown4", 0xFF);
  4500. packet2->setDataByName("unknown5", 0xFF);
  4501. packet2->setDataByName("unknown6", 0xFF);*/
  4502. client->QueuePacket(packet->serialize());
  4503. }
  4504. }
  4505. }
  4506. safe_delete(packet);
  4507. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4508. }
  4509. void ZoneServer::SendSpellFailedPacket(Client* client, int16 error){
  4510. if(!client)
  4511. return;
  4512. PacketStruct* packet = configReader.getStruct("WS_DisplaySpellFailed", client->GetVersion());
  4513. if(packet){
  4514. /* Temp solution, need to modify the error code before this function and while we still have access to the spell/combat art */
  4515. error = master_spell_list.GetSpellErrorValue(client->GetVersion(), error);
  4516. if(client->GetVersion() <= 546 && error) {
  4517. error += 1;
  4518. }
  4519. packet->setDataByName("error_code", error);
  4520. //packet->PrintPacket();
  4521. client->QueuePacket(packet->serialize());
  4522. safe_delete(packet);
  4523. }
  4524. }
  4525. void ZoneServer::SendInterruptPacket(Spawn* interrupted, LuaSpell* spell, bool fizzle){
  4526. if(!interrupted || !spell)
  4527. return;
  4528. EQ2Packet* outapp = 0;
  4529. PacketStruct* packet = 0;
  4530. Client* client = 0;
  4531. vector<Client*>::iterator client_itr;
  4532. MClientList.readlock(__FUNCTION__, __LINE__);
  4533. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4534. client = *client_itr;
  4535. if(!client || !client->GetPlayer()->WasSentSpawn(interrupted->GetID()))
  4536. continue;
  4537. packet = configReader.getStruct(fizzle ? "WS_SpellFizzle" : "WS_Interrupt", client->GetVersion());
  4538. if(packet){
  4539. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(interrupted));
  4540. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4541. for (int32 i = 0; i < spell->targets.size(); i++)
  4542. packet->setArrayDataByName("target_id", client->GetPlayer()->GetIDWithPlayerSpawn(client->GetPlayer()->GetZone()->GetSpawnByID(spell->targets[i])), i);
  4543. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4544. outapp = packet->serialize();
  4545. client->QueuePacket(outapp);
  4546. safe_delete(packet);
  4547. }
  4548. }
  4549. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4550. safe_delete(packet);
  4551. }
  4552. void ZoneServer::SendCastSpellPacket(LuaSpell* spell, Entity* caster){
  4553. EQ2Packet* outapp = 0;
  4554. PacketStruct* packet = 0;
  4555. Client* client = 0;
  4556. if(!caster || !spell || !spell->spell || spell->interrupted)
  4557. return;
  4558. vector<Client*>::iterator client_itr;
  4559. MClientList.readlock(__FUNCTION__, __LINE__);
  4560. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4561. client = *client_itr;
  4562. if(!client)
  4563. continue;
  4564. packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4565. if(packet){
  4566. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4567. if(!caster_id) {
  4568. safe_delete(packet);
  4569. continue;
  4570. }
  4571. packet->setDataByName("spawn_id", caster_id);
  4572. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4573. for (int32 i = 0; i < spell->targets.size(); i++) {
  4574. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(spell->caster->GetZone()->GetSpawnByID(spell->targets[i]));
  4575. if(target_id) {
  4576. packet->setArrayDataByName("target", target_id, i);
  4577. }
  4578. else {
  4579. packet->setArrayDataByName("target", 0xFFFFFFFF, i);
  4580. }
  4581. }
  4582. packet->setDataByName("spell_visual", spell->spell->GetSpellData()->spell_visual); //result
  4583. packet->setDataByName("cast_time", spell->spell->GetSpellData()->cast_time*.01f); //delay
  4584. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4585. packet->setDataByName("spell_level", 1);
  4586. packet->setDataByName("spell_tier", spell->spell->GetSpellData()->tier);
  4587. outapp = packet->serialize();
  4588. client->QueuePacket(outapp);
  4589. safe_delete(packet);
  4590. }
  4591. }
  4592. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4593. safe_delete(packet);
  4594. }
  4595. void ZoneServer::SendCastSpellPacket(int32 spell_visual, Spawn* target, Spawn* caster) {
  4596. if (target) {
  4597. vector<Client*>::iterator client_itr;
  4598. MClientList.readlock(__FUNCTION__, __LINE__);
  4599. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4600. Client* client = *client_itr;
  4601. if (!client)
  4602. continue;
  4603. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4604. if (packet) {
  4605. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4606. if(!target_id) { // client is not aware of spawn
  4607. safe_delete(packet);
  4608. continue;
  4609. }
  4610. if (!caster) {
  4611. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  4612. }
  4613. else {
  4614. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4615. if(!caster_id) { // client is not aware of spawn
  4616. safe_delete(packet);
  4617. continue;
  4618. }
  4619. packet->setDataByName("spawn_id", caster_id);
  4620. }
  4621. packet->setArrayLengthByName("num_targets", 1);
  4622. packet->setArrayDataByName("target", target_id);
  4623. packet->setDataByName("spell_visual", spell_visual);
  4624. packet->setDataByName("cast_time", 0);
  4625. packet->setDataByName("spell_id", 0);
  4626. packet->setDataByName("spell_level", 0);
  4627. packet->setDataByName("spell_tier", 1);
  4628. client->QueuePacket(packet->serialize());
  4629. safe_delete(packet);
  4630. }
  4631. }
  4632. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4633. }
  4634. }
  4635. void ZoneServer::SendCastEntityCommandPacket(EntityCommand* entity_command, int32 spawn_id, int32 target_id) {
  4636. if (entity_command) {
  4637. Spawn* spawn = GetSpawnByID(spawn_id);
  4638. Spawn* target = GetSpawnByID(target_id);
  4639. if (!spawn || !target)
  4640. return;
  4641. Client* client = 0;
  4642. vector<Client*>::iterator client_itr;
  4643. MClientList.readlock(__FUNCTION__, __LINE__);
  4644. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4645. client = *client_itr;
  4646. if (!client || !client->GetPlayer()->WasSentSpawn(spawn_id) || !client->GetPlayer()->WasSentSpawn(target_id))
  4647. continue;
  4648. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4649. if (packet) {
  4650. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  4651. int32 target_id = client->GetPlayer()->GetIDWithPlayerSpawn(target);
  4652. if(!caster_id || !target_id)
  4653. continue;
  4654. packet->setDataByName("spawn_id", caster_id);
  4655. packet->setArrayLengthByName("num_targets", 1);
  4656. packet->setArrayDataByName("target", target_id);
  4657. packet->setDataByName("num_targets", 1);
  4658. packet->setDataByName("spell_visual", entity_command->spell_visual); //result
  4659. packet->setDataByName("cast_time", entity_command->cast_time * 0.01); //delay
  4660. packet->setDataByName("spell_id", 1);
  4661. packet->setDataByName("spell_level", 1);
  4662. packet->setDataByName("spell_tier", 1);
  4663. EQ2Packet* outapp = packet->serialize();
  4664. client->QueuePacket(outapp);
  4665. safe_delete(packet);
  4666. }
  4667. }
  4668. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4669. }
  4670. }
  4671. void ZoneServer::StartZoneInitialSpawnThread(Client* client){
  4672. if(zoneShuttingDown)
  4673. return;
  4674. #ifdef WIN32
  4675. _beginthread(SendInitialSpawns, 0, client);
  4676. #else
  4677. pthread_t thread;
  4678. pthread_create(&thread, NULL, SendInitialSpawns, client);
  4679. pthread_detach(thread);
  4680. #endif
  4681. }
  4682. void ZoneServer::SendZoneSpawns(Client* client){
  4683. int8 count = 0;
  4684. while (LoadingData && count <= 6000) { //sleep for max of 60 seconds (60000ms) while the maps are loading
  4685. count++;
  4686. Sleep(10);
  4687. }
  4688. count = 0;
  4689. int16 size = 0;
  4690. //give the spawn thread a tad bit of time to add the pending_spawns to spawn_list (up to 10 seconds)
  4691. while (count < 1000) {
  4692. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  4693. size = pending_spawn_list_add.size();
  4694. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  4695. if (size == 0)
  4696. break;
  4697. Sleep(10);
  4698. count++;
  4699. }
  4700. initial_spawn_threads_active++;
  4701. map<int32, Spawn*>::iterator itr;
  4702. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4703. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4704. Spawn* spawn = itr->second;
  4705. if (spawn) {
  4706. if(spawn == client->GetPlayer() && (client->IsReloadingZone() || client->GetPlayer()->IsReturningFromLD()))
  4707. {
  4708. if(!client->GetPlayer()->SetSpawnMap(spawn))
  4709. continue;
  4710. }
  4711. CheckSpawnRange(client, spawn, true);
  4712. }
  4713. }
  4714. CheckSendSpawnToClient(client, true);
  4715. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4716. client->SetConnected(true);
  4717. ClientPacketFunctions::SendFinishedEntitiesList(client);
  4718. initial_spawn_threads_active--;
  4719. }
  4720. vector<Entity*> ZoneServer::GetPlayers(){
  4721. vector<Entity*> ret;
  4722. Client* client = 0;
  4723. vector<Client*>::iterator client_itr;
  4724. MClientList.readlock(__FUNCTION__, __LINE__);
  4725. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4726. client = *client_itr;
  4727. ret.push_back(client->GetPlayer());
  4728. }
  4729. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4730. return ret;
  4731. }
  4732. int16 ZoneServer::SetSpawnTargetable(Spawn* spawn, float distance){
  4733. Spawn* test_spawn = 0;
  4734. int16 ret_val = 0;
  4735. map<int32, Spawn*>::iterator itr;
  4736. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4737. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4738. test_spawn = itr->second;
  4739. if(test_spawn){
  4740. if(test_spawn->GetDistance(spawn) <= distance){
  4741. test_spawn->SetTargetable(1);
  4742. ret_val++;
  4743. }
  4744. }
  4745. }
  4746. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4747. return ret_val;
  4748. }
  4749. int16 ZoneServer::SetSpawnTargetable(int32 spawn_id){
  4750. Spawn* spawn = 0;
  4751. int16 ret_val = 0;
  4752. map<int32, Spawn*>::iterator itr;
  4753. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4754. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4755. spawn = itr->second;
  4756. if(spawn){
  4757. if(spawn->GetDatabaseID() == spawn_id){
  4758. spawn->SetTargetable(1);
  4759. ret_val++;
  4760. }
  4761. }
  4762. }
  4763. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4764. return ret_val;
  4765. }
  4766. 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) {
  4767. ZoneInfoSlideStructInfo* info = new ZoneInfoSlideStructInfo();
  4768. memset(info, 0, sizeof(ZoneInfoSlideStructInfo));
  4769. info->unknown1[0] = unknown1a;
  4770. info->unknown1[1] = unknown1b;
  4771. info->unknown2[0] = unknown2a;
  4772. info->unknown2[1] = unknown2b;
  4773. info->unknown3 = unknown3;
  4774. info->unknown4 = unknown4;
  4775. int8 length = strlen(slide);
  4776. if (length >= 128)
  4777. length = 127;
  4778. strncpy(info->slide, slide, length);
  4779. length = strlen(voiceover);
  4780. if (length >= 128)
  4781. length = 127;
  4782. strncpy(info->voiceover, voiceover, length);
  4783. info->key1 = key1;
  4784. info->key2 = key2;
  4785. ZoneInfoSlideStruct* ret = new ZoneInfoSlideStruct();
  4786. ret->info = info;
  4787. return ret;
  4788. }
  4789. void ZoneServer::AddZoneInfoSlideStructTransitionInfo(ZoneInfoSlideStruct* info, int32 x, int32 y, float zoom, float transition_time) {
  4790. ZoneInfoSlideStructTransitionInfo* transition_info = new ZoneInfoSlideStructTransitionInfo();
  4791. transition_info->transition_x = x;
  4792. transition_info->transition_y = y;
  4793. transition_info->transition_zoom = zoom;
  4794. transition_info->transition_time = transition_time;
  4795. info->slide_transition_info.push_back(transition_info);
  4796. }
  4797. vector<ZoneInfoSlideStruct*>* ZoneServer::GenerateTutorialSlides() {
  4798. vector<ZoneInfoSlideStruct*>* slides = new vector<ZoneInfoSlideStruct*>();
  4799. 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);
  4800. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4801. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 16);
  4802. slides->push_back(slide);
  4803. 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);
  4804. AddZoneInfoSlideStructTransitionInfo(slide, 600, 365, 1.60000002384186, 0);
  4805. AddZoneInfoSlideStructTransitionInfo(slide, 800, 370, 1.39999997615814, 9);
  4806. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 8);
  4807. slides->push_back(slide);
  4808. 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);
  4809. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 0);
  4810. AddZoneInfoSlideStructTransitionInfo(slide, 750, 320, 1.60000002384186, 10);
  4811. AddZoneInfoSlideStructTransitionInfo(slide, 575, 265, 2.29999995231628, 10);
  4812. slides->push_back(slide);
  4813. 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);
  4814. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 2.5, 0);
  4815. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.70000004768372, 8);
  4816. AddZoneInfoSlideStructTransitionInfo(slide, 675, 390, 2.20000004768372, 11);
  4817. slides->push_back(slide);
  4818. 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);
  4819. AddZoneInfoSlideStructTransitionInfo(slide, 750, 500, 2.79999995231628, 0);
  4820. AddZoneInfoSlideStructTransitionInfo(slide, 720, 300, 2.5, 9);
  4821. AddZoneInfoSlideStructTransitionInfo(slide, 975, 270, 2.20000004768372, 9);
  4822. slides->push_back(slide);
  4823. 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);
  4824. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.89999997615814, 0);
  4825. AddZoneInfoSlideStructTransitionInfo(slide, 920, 475, 1, 24);
  4826. slides->push_back(slide);
  4827. 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);
  4828. AddZoneInfoSlideStructTransitionInfo(slide, 1400, 420, 2.40000009536743, 0);
  4829. AddZoneInfoSlideStructTransitionInfo(slide, 1200, 375, 1.70000004768372, 7);
  4830. AddZoneInfoSlideStructTransitionInfo(slide, 800, 225, 2.29999995231628, 7);
  4831. slides->push_back(slide);
  4832. 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);
  4833. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4834. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 27);
  4835. slides->push_back(slide);
  4836. 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);
  4837. AddZoneInfoSlideStructTransitionInfo(slide, 715, 305, 2.40000009536743, 0);
  4838. AddZoneInfoSlideStructTransitionInfo(slide, 730, 325, 1.79999995231628, 6);
  4839. AddZoneInfoSlideStructTransitionInfo(slide, 920, 395, 1.5, 5);
  4840. AddZoneInfoSlideStructTransitionInfo(slide, 1360, 330, 1.79999995231628, 9);
  4841. slides->push_back(slide);
  4842. 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);
  4843. AddZoneInfoSlideStructTransitionInfo(slide, 670, 675, 2.20000004768372, 0);
  4844. AddZoneInfoSlideStructTransitionInfo(slide, 710, 390, 1.79999995231628, 7);
  4845. AddZoneInfoSlideStructTransitionInfo(slide, 920, 415, 1.60000002384186, 5.5);
  4846. AddZoneInfoSlideStructTransitionInfo(slide, 1250, 675, 1.79999995231628, 8);
  4847. slides->push_back(slide);
  4848. 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);
  4849. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4850. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 19);
  4851. slides->push_back(slide);
  4852. 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);
  4853. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 0);
  4854. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 43);
  4855. slides->push_back(slide);
  4856. return slides;
  4857. }
  4858. EQ2Packet* ZoneServer::GetZoneInfoPacket(Client* client){
  4859. // this takes place when we get the LoginInfo for returning LD players
  4860. if(!client->GetPlayer()->IsReturningFromLD())
  4861. UpdateClientSpawnMap(client->GetPlayer(), client);
  4862. PacketStruct* packet = configReader.getStruct("WS_ZoneInfo", client->GetVersion());
  4863. packet->setSmallStringByName("server1",net.GetWorldName());
  4864. packet->setSmallStringByName("server2",net.GetWorldName());
  4865. packet->setDataByName("unknown1", 1, 1);//1, 1
  4866. int32 expansions = EXPANSION_UNKNOWN + EXPANSION_DOF + EXPANSION_KOS + EXPANSION_EOF + EXPANSION_ROK + EXPANSION_TSO + EXPANSION_DOV;
  4867. //packet->setDataByName("expansions_enabled", 82313211);//expansions 63181
  4868. //packet->setDataByName("expansions_enabled", 552075103);//expansions 63182
  4869. packet->setDataByName("expansions_enabled", 4294967295);//expansions 1096 //612499455 works
  4870. if (client->GetVersion() >= 1193) {
  4871. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4872. packet->setDataByName("unknown3", 4294967295, 1); //COE and up
  4873. packet->setDataByName("unknown3", 4294967295, 2);
  4874. }
  4875. else
  4876. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4877. packet->setSmallStringByName("auction_website", "eq2emulator.net");
  4878. packet->setDataByName("auction_port", 80);
  4879. packet->setSmallStringByName("upload_page", "test_upload.m");
  4880. packet->setSmallStringByName("upload_key", "dsya987yda9");
  4881. packet->setSmallStringByName("zone", GetZoneFile());
  4882. //packet->setSmallStringByName("zone2", GetZoneName());
  4883. //if ( strlen(GetZoneSkyFile()) > 0 )
  4884. // packet->setSmallStringByName("zone_unknown2", GetZoneSkyFile()); // used for the sky map
  4885. packet->setSmallStringByName("zone_desc", GetZoneDescription());
  4886. packet->setSmallStringByName("char_name", client->GetPlayer()->GetName());
  4887. packet->setDataByName("x", client->GetPlayer()->GetX());
  4888. packet->setDataByName("y", client->GetPlayer()->GetY());
  4889. packet->setDataByName("z", client->GetPlayer()->GetZ());
  4890. 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)
  4891. vector<ZoneInfoSlideStruct*>* slides = GenerateTutorialSlides();
  4892. if (slides) {
  4893. packet->setArrayLengthByName("num_slides", slides->size());
  4894. ZoneInfoSlideStruct* slide = 0;
  4895. for (int8 i = 0; i < slides->size(); i++) {
  4896. slide = slides->at(i);
  4897. packet->setArrayDataByName("unknown1", slide->info->unknown1[0], i, 0);
  4898. packet->setArrayDataByName("unknown1", slide->info->unknown1[1], i, 1);
  4899. packet->setArrayDataByName("unknown2", slide->info->unknown2[0], i, 0);
  4900. packet->setArrayDataByName("unknown2", slide->info->unknown2[1], i, 1);
  4901. packet->setArrayDataByName("unknown3", slide->info->unknown3, i);
  4902. packet->setArrayDataByName("unknown4", slide->info->unknown4, i);
  4903. packet->setArrayDataByName("slide", slide->info->slide, i);
  4904. packet->setArrayDataByName("voiceover", slide->info->voiceover, i);
  4905. packet->setArrayDataByName("key1", slide->info->key1, i);
  4906. packet->setArrayDataByName("key2", slide->info->key2, i);
  4907. packet->setSubArrayLengthByName("num_transitions", slide->slide_transition_info.size(), i);
  4908. for (int8 x = 0; x < slide->slide_transition_info.size(); x++) {
  4909. packet->setSubArrayDataByName("transition_x", slide->slide_transition_info[x]->transition_x, i, x);
  4910. packet->setSubArrayDataByName("transition_y", slide->slide_transition_info[x]->transition_y, i, x);
  4911. packet->setSubArrayDataByName("transition_zoom", slide->slide_transition_info[x]->transition_zoom, i, x);
  4912. packet->setSubArrayDataByName("transition_time", slide->slide_transition_info[x]->transition_time, i, x);
  4913. safe_delete(slide->slide_transition_info[x]);
  4914. }
  4915. safe_delete(slide->info);
  4916. safe_delete(slide);
  4917. }
  4918. }
  4919. safe_delete(slides);
  4920. }
  4921. if(rule_manager.GetGlobalRule(R_Zone, UseMapUnderworldCoords)->GetBool() && client->GetPlayer()->GetMap()) {
  4922. packet->setDataByName("underworld", client->GetPlayer()->GetMap()->GetMinY() + rule_manager.GetGlobalRule(R_Zone, MapUnderworldCoordOffset)->GetFloat());
  4923. }
  4924. else {
  4925. packet->setDataByName("underworld", underworld);
  4926. }
  4927. // unknown3 can prevent screen shots from being taken if
  4928. //packet->setDataByName("unknown3", 2094661567, 1); // Screenshots allowed with this value
  4929. //packet->setDataByName("unknown3", 3815767999, 1); // Screenshots disabled with this value
  4930. //packet->setDataByName("unknown3", 1, 2);
  4931. /*if (client->GetVersion() >= 63587) {
  4932. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4933. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4934. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4935. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4936. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4937. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4938. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4939. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4940. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4941. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4942. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4943. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4944. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4945. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4946. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4947. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4948. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4949. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4950. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4951. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4952. }
  4953. else if (client->GetVersion() >= 63214) {
  4954. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4955. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4956. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4957. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4958. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4959. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4960. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4961. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4962. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4963. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4964. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4965. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4966. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4967. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4968. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4969. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4970. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4971. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4972. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4973. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4974. }*/
  4975. if (client->GetVersion() >= 64644) {
  4976. packet->setDataByName("unknown3a", 12598924);
  4977. packet->setDataByName("unknown3b", 3992452959);
  4978. packet->setDataByName("unknown3c", 4294967183);
  4979. packet->setDataByName("unknown2a", 9);
  4980. packet->setDataByName("unknown2b", 9);
  4981. }
  4982. else if (client->GetVersion() >= 63181) {
  4983. packet->setDataByName("unknown3a", 750796556);//63182 73821356
  4984. packet->setDataByName("unknown3b", 3991404383);// 63182 3991404383
  4985. packet->setDataByName("unknown3c", 4278189967);// 63182 4278189967
  4986. packet->setDataByName("unknown2a", 8);// 63182
  4987. packet->setDataByName("unknown2b", 8);// 63182
  4988. }
  4989. else{
  4990. //packet->setDataByName("unknown3", 872447025,0);//63181
  4991. //packet->setDataByName("unknown3", 3085434875,1);// 63181
  4992. //packet->setDataByName("unknown3", 2147483633,2);// 63181
  4993. }
  4994. packet->setDataByName("year", world.GetWorldTimeStruct()->year);
  4995. packet->setDataByName("month", world.GetWorldTimeStruct()->month);
  4996. packet->setDataByName("day", world.GetWorldTimeStruct()->day);
  4997. packet->setDataByName("hour", world.GetWorldTimeStruct()->hour);
  4998. packet->setDataByName("minute", world.GetWorldTimeStruct()->minute);
  4999. packet->setDataByName("unknown", 0);
  5000. packet->setDataByName("unknown7", 1);
  5001. packet->setDataByName("unknown7", 1, 1);
  5002. packet->setDataByName("unknown9", 13);
  5003. //packet->setDataByName("unknown10", 25188959);4294967295
  5004. //packet->setDataByName("unknown10", 25190239);
  5005. packet->setDataByName("unknown10", 25191524);//25191524
  5006. packet->setDataByName("unknown10b", 1);
  5007. 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
  5008. packet->setDataByName("num_adv", 9);
  5009. packet->setArrayDataByName("adv_name", "adv02_dun_drowned_caverns", 0);
  5010. packet->setArrayDataByName("adv_id", 6, 0);
  5011. packet->setArrayDataByName("adv_name", "adv02_dun_sundered_splitpaw_hub", 1);
  5012. packet->setArrayDataByName("adv_id", 5, 1);
  5013. packet->setArrayDataByName("adv_name", "exp03_rgn_butcherblock", 2);
  5014. packet->setArrayDataByName("adv_id", 8, 2);
  5015. packet->setArrayDataByName("adv_name", "exp03_rgn_greater_faydark", 3);
  5016. packet->setArrayDataByName("adv_id", 7, 3);
  5017. packet->setArrayDataByName("adv_name", "mod01_dun_crypt_of_thaen", 4);
  5018. packet->setArrayDataByName("adv_id", 3, 4);
  5019. packet->setArrayDataByName("adv_name", "mod01_dun_tombs_of_night", 5);
  5020. packet->setArrayDataByName("adv_id", 4, 5);
  5021. packet->setArrayDataByName("adv_name", "nektulos_mini01", 6);
  5022. packet->setArrayDataByName("adv_id", 0, 6);
  5023. packet->setArrayDataByName("adv_name", "nektulos_mini02", 7);
  5024. packet->setArrayDataByName("adv_id", 1, 7);
  5025. packet->setArrayDataByName("adv_name", "nektulos_mini03", 8);
  5026. packet->setArrayDataByName("adv_id", 2, 8);
  5027. LogWrite(MISC__TODO, 0, "TODO", "Put cl_ client commands back in variables (not Rules) so they can be dynamically maintained");
  5028. vector<Variable*>* variables = world.GetClientVariables();
  5029. packet->setArrayLengthByName("num_client_setup", variables->size());
  5030. for(int i=variables->size()-1;i>=0;i--)
  5031. packet->setArrayDataByName("client_cmds", variables->at(i)->GetNameValuePair().c_str(), i);
  5032. // For AoM clients so item link work
  5033. if (client->GetVersion() >= 60114)
  5034. packet->setArrayDataByName("client_cmds", "chat_linkbrackets_item 1", variables->size());
  5035. safe_delete(variables);
  5036. //packet->setDataByName("unknown8", ); story?
  5037. // AA Tabs for 1193+ clients
  5038. if (client->GetVersion() >= 1193) {
  5039. packet->setArrayLengthByName("tab_count", 48);
  5040. int8 i = 0;
  5041. packet->setArrayDataByName("tab_index", i, i);
  5042. packet->setArrayDataByName("tab_name", ":es24a58bd8fcaac8c2:All", i);
  5043. i++;
  5044. packet->setArrayDataByName("tab_index", i, i);
  5045. packet->setArrayDataByName("tab_name", ":410385c727bd47a6:Racial Innate", i);
  5046. i++;
  5047. packet->setArrayDataByName("tab_index", i, i);
  5048. packet->setArrayDataByName("tab_name", ":410385c75a96e23c:Tradeskill Advancement", i);
  5049. i++;
  5050. packet->setArrayDataByName("tab_index", i, i);
  5051. packet->setArrayDataByName("tab_name", ":410385c744f1fd99:Focus Effects", i);
  5052. i++;
  5053. packet->setArrayDataByName("tab_index", i, i);
  5054. packet->setArrayDataByName("tab_name", ":410385c71edd2a66:Heroic", i);
  5055. i++;
  5056. packet->setArrayDataByName("tab_index", i, i);
  5057. packet->setArrayDataByName("tab_name", ":410385c76ee6239f:Shadows", i);
  5058. i++;
  5059. packet->setArrayDataByName("tab_index", i, i);
  5060. packet->setArrayDataByName("tab_name", ":410385c7e678b977:Prestige", i);
  5061. i++;
  5062. packet->setArrayDataByName("tab_index", i, i);
  5063. packet->setArrayDataByName("tab_name", ":410385c77ee422d7:Animalist", i);
  5064. i++;
  5065. packet->setArrayDataByName("tab_index", i, i);
  5066. packet->setArrayDataByName("tab_name", ":410385c7f165af77:Bard", i);
  5067. i++;
  5068. packet->setArrayDataByName("tab_index", i, i);
  5069. packet->setArrayDataByName("tab_name", ":410385c7421b9375:Brawler", i);
  5070. i++;
  5071. packet->setArrayDataByName("tab_index", i, i);
  5072. packet->setArrayDataByName("tab_name", ":410385c7a03ae7d1:Cleric", i);
  5073. i++;
  5074. packet->setArrayDataByName("tab_index", i, i);
  5075. packet->setArrayDataByName("tab_name", ":410385c7c9605e9f:Crusader", i);
  5076. i++;
  5077. packet->setArrayDataByName("tab_index", i, i);
  5078. packet->setArrayDataByName("tab_name", ":410385c7f9424168:Druid", i);
  5079. i++;
  5080. packet->setArrayDataByName("tab_index", i, i);
  5081. packet->setArrayDataByName("tab_name", ":410385c79cb9556c:Enchanter", i);
  5082. i++;
  5083. packet->setArrayDataByName("tab_index", i, i);
  5084. packet->setArrayDataByName("tab_name", ":410385c70c8b6aa4:Predator", i);
  5085. i++;
  5086. packet->setArrayDataByName("tab_index", i, i);
  5087. packet->setArrayDataByName("tab_name", ":410385c73a43b6dd:Rogue", i);
  5088. i++;
  5089. packet->setArrayDataByName("tab_index", i, i);
  5090. packet->setArrayDataByName("tab_name", ":410385c759fe7d15:Sorcerer", i);
  5091. i++;
  5092. packet->setArrayDataByName("tab_index", i, i);
  5093. packet->setArrayDataByName("tab_name", ":410385c7ad610aca:Summoner", i);
  5094. i++;
  5095. packet->setArrayDataByName("tab_index", i, i);
  5096. packet->setArrayDataByName("tab_name", ":410385c71e056728:Warrior", i);
  5097. i++;
  5098. packet->setArrayDataByName("tab_index", i, i);
  5099. packet->setArrayDataByName("tab_name", ":410385c7ba864c0b:Assassin", i);
  5100. i++;
  5101. packet->setArrayDataByName("tab_index", i, i);
  5102. packet->setArrayDataByName("tab_name", ":410385c7b8116aad:Beastlord", i);
  5103. i++;
  5104. packet->setArrayDataByName("tab_index", i, i);
  5105. packet->setArrayDataByName("tab_name", ":410385c7f53feb7b:Berserker", i);
  5106. i++;
  5107. packet->setArrayDataByName("tab_index", i, i);
  5108. packet->setArrayDataByName("tab_name", ":410385c73d8a70e2:Brigand", i);
  5109. i++;
  5110. packet->setArrayDataByName("tab_index", i, i);
  5111. packet->setArrayDataByName("tab_name", ":410385c770c766d6:Bruiser", i);
  5112. i++;
  5113. packet->setArrayDataByName("tab_index", i, i);
  5114. packet->setArrayDataByName("tab_name", ":410385c79226984b:Coercer", i);
  5115. i++;
  5116. packet->setArrayDataByName("tab_index", i, i);
  5117. packet->setArrayDataByName("tab_name", ":410385c70c58bb30:Conjurer", i);
  5118. i++;
  5119. packet->setArrayDataByName("tab_index", i, i);
  5120. packet->setArrayDataByName("tab_name", ":410385c73dfe68d0:Defiler", i);
  5121. i++;
  5122. packet->setArrayDataByName("tab_index", i, i);
  5123. packet->setArrayDataByName("tab_name", ":410385c792919a6b:Dirge", i);
  5124. i++;
  5125. packet->setArrayDataByName("tab_index", i, i);
  5126. packet->setArrayDataByName("tab_name", ":410385c7062e5f55:Fury", i);
  5127. i++;
  5128. packet->setArrayDataByName("tab_index", i, i);
  5129. packet->setArrayDataByName("tab_name", ":410385c762c1fdfc:Guardian", i);
  5130. i++;
  5131. packet->setArrayDataByName("tab_index", i, i);
  5132. packet->setArrayDataByName("tab_name", ":410385c78addfbf4:Illusionist", i);
  5133. i++;
  5134. packet->setArrayDataByName("tab_index", i, i);
  5135. packet->setArrayDataByName("tab_name", ":410385c7ece054a7:Inquisitor", i);
  5136. i++;
  5137. packet->setArrayDataByName("tab_index", i, i);
  5138. packet->setArrayDataByName("tab_name", ":410385c7d550d2e7:Monk", i);
  5139. i++;
  5140. packet->setArrayDataByName("tab_index", i, i);
  5141. packet->setArrayDataByName("tab_name", ":410385c743cfeaa2:Mystic", i);
  5142. i++;
  5143. packet->setArrayDataByName("tab_index", i, i);
  5144. packet->setArrayDataByName("tab_name", ":410385c7f63c9c8c:Necromancer", i);
  5145. i++;
  5146. packet->setArrayDataByName("tab_index", i, i);
  5147. packet->setArrayDataByName("tab_name", ":410385c70c5de0ae:Paladin", i);
  5148. i++;
  5149. packet->setArrayDataByName("tab_index", i, i);
  5150. packet->setArrayDataByName("tab_name", ":410385c79bc97b3a:Ranger", i);
  5151. i++;
  5152. packet->setArrayDataByName("tab_index", i, i);
  5153. packet->setArrayDataByName("tab_name", ":410385c78fbd2256:Shadowknight", i);
  5154. i++;
  5155. packet->setArrayDataByName("tab_index", i, i);
  5156. packet->setArrayDataByName("tab_name", ":410385c7781cc625:Shaman", i);
  5157. i++;
  5158. packet->setArrayDataByName("tab_index", i, i);
  5159. packet->setArrayDataByName("tab_name", ":410385c77eecdcdb:Swashbuckler", i);
  5160. i++;
  5161. packet->setArrayDataByName("tab_index", i, i);
  5162. packet->setArrayDataByName("tab_name", ":410385c7648d181e:Templar", i);
  5163. i++;
  5164. packet->setArrayDataByName("tab_index", i, i);
  5165. packet->setArrayDataByName("tab_name", ":410385c78df47d77:Troubador", i);
  5166. i++;
  5167. packet->setArrayDataByName("tab_index", i, i);
  5168. packet->setArrayDataByName("tab_name", ":410385c7c78ce0b8:Warden", i);
  5169. i++;
  5170. packet->setArrayDataByName("tab_index", i, i);
  5171. packet->setArrayDataByName("tab_name", ":410385c76290dcfa:Warlock", i);
  5172. i++;
  5173. packet->setArrayDataByName("tab_index", i, i);
  5174. packet->setArrayDataByName("tab_name", ":410385c7d1d52cf5:Wizard", i);
  5175. i++;
  5176. packet->setArrayDataByName("tab_index", i, i);
  5177. packet->setArrayDataByName("tab_name", ":410385c71c8f6f4d:Shaper", i);
  5178. i++;
  5179. packet->setArrayDataByName("tab_index", i, i);
  5180. packet->setArrayDataByName("tab_name", ":410385c72f6e354d:Channeler", i);
  5181. i++;
  5182. packet->setArrayDataByName("tab_index", i, i);
  5183. packet->setArrayDataByName("tab_name", ":410385c7df8bd37d:Dragon", i);
  5184. }
  5185. packet->setDataByName("unknown_mj", 1);//int8
  5186. packet->setDataByName("unknown_mj1", 335544320);//int32
  5187. packet->setDataByName("unknown_mj2", 4);//int32
  5188. packet->setDataByName("unknown_mj3", 3962504088);//int32
  5189. packet->setDataByName("unknown_mj4", 3985947216);//int32
  5190. packet->setDataByName("unknown_mj5", 1);//int32
  5191. packet->setDataByName("unknown_mj6", 386);//int32
  5192. packet->setDataByName("unknown_mj7", 4294967295);//int32
  5193. packet->setDataByName("unknown_mj8", 2716312211);//int32
  5194. packet->setDataByName("unknown_mj9", 1774338333);//int32
  5195. packet->setDataByName("unknown_mj10", 1);//int32
  5196. packet->setDataByName("unknown_mj11", 391);//int32
  5197. packet->setDataByName("unknown_mj12", 4294967295);//int32
  5198. packet->setDataByName("unknown_mj13", 3168965163);//int32
  5199. packet->setDataByName("unknown_mj14", 4117025286);//int32
  5200. packet->setDataByName("unknown_mj15", 1);//int32
  5201. packet->setDataByName("unknown_mj16", 394);//int32
  5202. packet->setDataByName("unknown_mj17", 4294967295);//int32
  5203. packet->setDataByName("unknown_mj18", 1790669110);//int32
  5204. packet->setDataByName("unknown_mj19", 107158108);//int32
  5205. packet->setDataByName("unknown_mj20", 1);//int32
  5206. packet->setDataByName("unknown_mj21", 393);//int32
  5207. packet->setDataByName("unknown_mj22", 4294967295);//int32
  5208. EQ2Packet* outapp = packet->serialize();
  5209. //packet->PrintPacket();
  5210. //DumpPacket(outapp);
  5211. safe_delete(packet);
  5212. return outapp;
  5213. }
  5214. void ZoneServer::SendUpdateDefaultCommand(Spawn* spawn, const char* command, float distance, Spawn* toPlayer){
  5215. if (spawn == nullptr || command == nullptr)
  5216. return;
  5217. if (toPlayer)
  5218. {
  5219. if (!toPlayer->IsPlayer())
  5220. return;
  5221. Client* client = GetClientBySpawn(toPlayer);
  5222. if (client)
  5223. {
  5224. client->SendDefaultCommand(spawn, command, distance);
  5225. }
  5226. // we don't override the primary command cause that would change ALL clients
  5227. return;
  5228. }
  5229. QueueDefaultCommand(spawn->GetID(), std::string(command), distance);
  5230. if (strlen(command)>0)
  5231. spawn->SetPrimaryCommand(command, command, distance);
  5232. }
  5233. void ZoneServer::CheckPlayerProximity(Spawn* spawn, Client* client){
  5234. if (player_proximities.size() < 1)
  5235. return;
  5236. if(player_proximities.count(spawn->GetID()) > 0){
  5237. PlayerProximity* prox = player_proximities.Get(spawn->GetID());
  5238. 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. prox->clients_in_proximity[client] = true;
  5240. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->in_range_lua_function.c_str());
  5241. }
  5242. 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){
  5243. if(prox->leaving_range_lua_function.length() > 0)
  5244. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->leaving_range_lua_function.c_str());
  5245. prox->clients_in_proximity.erase(client);
  5246. }
  5247. }
  5248. }
  5249. void ZoneServer::AddPlayerProximity(Spawn* spawn, float distance, string in_range_function, string leaving_range_function){
  5250. RemovePlayerProximity(spawn);
  5251. PlayerProximity* prox = new PlayerProximity;
  5252. prox->distance = distance;
  5253. prox->in_range_lua_function = in_range_function;
  5254. prox->leaving_range_lua_function = leaving_range_function;
  5255. player_proximities.Put(spawn->GetID(), prox);
  5256. }
  5257. void ZoneServer::RemovePlayerProximity(Client* client){
  5258. PlayerProximity* prox = 0;
  5259. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5260. while(itr.Next()){
  5261. prox = itr->second;
  5262. if(prox->clients_in_proximity.count(client) > 0)
  5263. prox->clients_in_proximity.erase(client);
  5264. }
  5265. }
  5266. void ZoneServer::RemovePlayerProximity(Spawn* spawn, bool all){
  5267. if(all){
  5268. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  5269. while(itr.Next()){
  5270. player_proximities.erase(itr->first, false, true, 10000);
  5271. }
  5272. }
  5273. else if(player_proximities.count(spawn->GetID()) > 0){
  5274. player_proximities.erase(spawn->GetID(), false, true, 10000);
  5275. }
  5276. }
  5277. void ZoneServer::AddLocationProximity(float x, float y, float z, float max_variation, string in_range_function, string leaving_range_function) {
  5278. LocationProximity* prox = new LocationProximity;
  5279. prox->x = x;
  5280. prox->y = y;
  5281. prox->z = z;
  5282. prox->max_variation = max_variation;
  5283. prox->in_range_lua_function = in_range_function;
  5284. prox->leaving_range_lua_function = leaving_range_function;
  5285. location_proximities.Add(prox);
  5286. }
  5287. void ZoneServer::CheckLocationProximity() {
  5288. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  5289. if (!zone_script)
  5290. return;
  5291. if (location_proximities.size() > 0 && connected_clients.size() > 0) {
  5292. Client* client = 0;
  5293. MutexList<Client*>::iterator iterator = connected_clients.begin();
  5294. while(iterator.Next()){
  5295. client = iterator->value;
  5296. if (client->IsConnected() && client->IsReadyForUpdates() && !client->IsZoning()) {
  5297. try {
  5298. MutexList<LocationProximity*>::iterator itr = location_proximities.begin();
  5299. LocationProximity* prox = 0;
  5300. while(itr.Next()){
  5301. prox = itr->value;
  5302. bool in_range = false;
  5303. float char_x = client->GetPlayer()->GetX();
  5304. float char_y = client->GetPlayer()->GetY();
  5305. float char_z = client->GetPlayer()->GetZ();
  5306. float x = prox->x;
  5307. float y = prox->y;
  5308. float z = prox->z;
  5309. float max_variation = prox->max_variation;
  5310. float total_diff = 0;
  5311. float diff = x - char_x; //Check X
  5312. if(diff < 0)
  5313. diff *= -1;
  5314. if(diff <= max_variation) {
  5315. total_diff += diff;
  5316. diff = z - char_z; //Check Z (we check Z first because it is far more likely to be a much greater variation than y)
  5317. if(diff < 0)
  5318. diff *= -1;
  5319. if(diff <= max_variation) {
  5320. total_diff += diff;
  5321. if(total_diff <= max_variation) { //Check Total
  5322. diff = y - char_y; //Check Y
  5323. if(diff < 0)
  5324. diff *= -1;
  5325. if(diff <= max_variation) {
  5326. total_diff += diff;
  5327. if(total_diff <= max_variation) {
  5328. in_range = true;
  5329. if(lua_interface && prox->in_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) == 0) { //Check Total
  5330. prox->clients_in_proximity[client] = true;
  5331. lua_interface->RunZoneScript(zone_script, prox->in_range_lua_function.c_str(), this, client->GetPlayer());
  5332. }
  5333. }
  5334. }
  5335. }
  5336. }
  5337. }
  5338. if (!in_range) {
  5339. if(lua_interface && prox->leaving_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) > 0) {
  5340. lua_interface->RunZoneScript(zone_script, prox->leaving_range_lua_function.c_str(), this, client->GetPlayer());
  5341. prox->clients_in_proximity.erase(client);
  5342. }
  5343. }
  5344. }
  5345. }
  5346. catch (...) {
  5347. LogWrite(ZONE__ERROR, 0, "Zone", "Except caught in ZoneServer::CheckLocationProximity");
  5348. return;
  5349. }
  5350. }
  5351. }
  5352. }
  5353. }
  5354. void ZoneServer::CheckLocationGrids() {
  5355. if (connected_clients.size() > 0 && location_grids.size() > 0) {
  5356. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5357. while (client_itr.Next()) {
  5358. Client* client = client_itr.value;
  5359. if (!client)
  5360. continue;
  5361. Player* player = client->GetPlayer();
  5362. float x = player->GetX();
  5363. float y = player->GetY();
  5364. float z = player->GetZ();
  5365. int32 grid_id = player->GetLocation();
  5366. MutexList<LocationGrid*>::iterator location_grid_itr = location_grids.begin();
  5367. while (location_grid_itr.Next()) {
  5368. LocationGrid* grid = location_grid_itr.value;
  5369. if (grid->locations.size() > 0 || /*(grid->grid_id == grid_id ||*/ grid->players.count(player) > 0) {
  5370. float x_small = 0;
  5371. float x_large = 0;
  5372. float y_small = 0;
  5373. float y_large = 0;
  5374. float z_small = 0;
  5375. float z_large = 0;
  5376. bool first = true;
  5377. bool in_grid = false;
  5378. MutexList<Location*>::iterator location_itr = grid->locations.begin();
  5379. while (location_itr.Next()) {
  5380. Location* location = location_itr.value;
  5381. if (first) {
  5382. x_small = location->x;
  5383. x_large = location->x;
  5384. if (grid->include_y) {
  5385. y_small = location->y;
  5386. y_large = location->y;
  5387. }
  5388. z_small = location->z;
  5389. z_large = location->z;
  5390. first = false;
  5391. }
  5392. else {
  5393. if (location->x < x_small)
  5394. x_small = location->x;
  5395. else if (location->x > x_large)
  5396. x_large = location->x;
  5397. if (grid->include_y) {
  5398. if (location->y < y_small)
  5399. y_small = location->y;
  5400. else if (location->y > y_large)
  5401. y_large = location->y;
  5402. }
  5403. if (location->z < z_small)
  5404. z_small = location->z;
  5405. else if (location->z > z_large)
  5406. z_large = location->z;
  5407. }
  5408. }
  5409. if (grid->include_y && (x >= x_small && x <= x_large && y >= y_small && y <= y_large && z >= z_small && z <= z_large))
  5410. in_grid = true;
  5411. else if (x >= x_small && x <= x_large && z >= z_small && z <= z_large)
  5412. in_grid = true;
  5413. if (in_grid && grid->players.count(player) == 0) {
  5414. grid->players.Put(player, true);
  5415. bool show_enter_location_popup = true;
  5416. bool discovery_enabled = rule_manager.GetGlobalRule(R_World, EnablePOIDiscovery)->GetBool();
  5417. if( grid->discovery && discovery_enabled && !player->DiscoveredLocation(grid->id) )
  5418. {
  5419. // check if player has already discovered this location
  5420. // if not, process new discovery
  5421. char tmp[200] = {0};
  5422. sprintf(tmp, "\\#FFE400You have discovered\12\\#FFF283%s", grid->name.c_str());
  5423. client->SendPopupMessage(11, tmp, "ui_discovery", 2.25, 0xFF, 0xFF, 0xFF);
  5424. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' discovered location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5425. player->UpdatePlayerHistory(HISTORY_TYPE_DISCOVERY, HISTORY_SUBTYPE_LOCATION, grid->id);
  5426. show_enter_location_popup = false;
  5427. // else, print standard location entry
  5428. }
  5429. if( show_enter_location_popup )
  5430. {
  5431. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' entering location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5432. client->SendPopupMessage(10, grid->name.c_str(), 0, 2.5, 255, 255, 0);
  5433. }
  5434. }
  5435. else if (!in_grid && grid->players.count(player) > 0) {
  5436. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' leaving location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  5437. grid->players.erase(player);
  5438. }
  5439. }
  5440. }
  5441. }
  5442. }
  5443. }
  5444. // Called from a command (client, main zone thread) and the main zone thread
  5445. // so no need for a mutex container
  5446. void ZoneServer::AddLocationGrid(LocationGrid* grid) {
  5447. if (grid)
  5448. location_grids.Add(grid);
  5449. }
  5450. void ZoneServer::RemoveLocationGrids() {
  5451. MutexList<LocationGrid*>::iterator itr = location_grids.begin();
  5452. while (itr.Next())
  5453. itr.value->locations.clear(true);
  5454. location_grids.clear(true);
  5455. }
  5456. void ZoneServer::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast, bool call_expire_function, bool lock_spell_process){
  5457. if(spellProcess)
  5458. spellProcess->RemoveSpellTimersFromSpawn(spawn, remove_all, delete_recast, call_expire_function, lock_spell_process);
  5459. }
  5460. void ZoneServer::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement){
  5461. if(spellProcess)
  5462. spellProcess->Interrupted(caster, interruptor, error_code, cancel, from_movement);
  5463. }
  5464. Spell* ZoneServer::GetSpell(Entity* caster){
  5465. Spell* spell = 0;
  5466. if(spellProcess)
  5467. spell = spellProcess->GetSpell(caster);
  5468. return spell;
  5469. }
  5470. void ZoneServer::ProcessSpell(Spell* spell, Entity* caster, Spawn* target, bool lock, bool harvest_spell, LuaSpell* customSpell, int16 custom_cast_time, bool in_heroic_opp){
  5471. if(spellProcess)
  5472. spellProcess->ProcessSpell(this, spell, caster, target, lock, harvest_spell, customSpell, custom_cast_time, in_heroic_opp);
  5473. }
  5474. void ZoneServer::ProcessEntityCommand(EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock) {
  5475. if (target && target->GetSpawnScript()) {
  5476. Player* player = 0;
  5477. if (caster && caster->IsPlayer())
  5478. player = (Player*)caster;
  5479. CallSpawnScript(target, SPAWN_SCRIPT_CUSTOM, player, entity_command->command.c_str());
  5480. }
  5481. if (spellProcess)
  5482. spellProcess->ProcessEntityCommand(this, entity_command, caster, target, lock);
  5483. }
  5484. void ZoneServer::RemoveSpawnSupportFunctions(Spawn* spawn, bool lock_spell_process) {
  5485. if(!spawn)
  5486. return;
  5487. if(spawn->IsPlayer() && spawn->GetZone())
  5488. spawn->GetZone()->RemovePlayerPassenger(((Player*)spawn)->GetCharacterID());
  5489. if(spawn->IsEntity())
  5490. RemoveSpellTimersFromSpawn((Entity*)spawn, true, true, true, lock_spell_process);
  5491. RemoveDamagedSpawn(spawn);
  5492. spawn->SendSpawnChanges(false);
  5493. RemoveChangedSpawn(spawn);
  5494. // Everything inside this if will be nuked during a reload in other spots, no need to do it twice
  5495. if (!reloading) {
  5496. RemoveDeadEnemyList(spawn);
  5497. spawn->changed = true;
  5498. spawn->info_changed = true;
  5499. spawn->vis_changed = true;
  5500. spawn->position_changed = true;
  5501. SendSpawnChanges(spawn);
  5502. if (spawn->GetSpawnGroupID() > 0) {
  5503. int32 group_id = spawn->GetSpawnGroupID();
  5504. spawn->RemoveSpawnFromGroup();
  5505. if (spawn_group_map.count(group_id) > 0)
  5506. spawn_group_map.Get(group_id).Remove(spawn->GetID());
  5507. }
  5508. if (!spawn->IsPlayer()) {
  5509. if(quick_database_id_lookup.count(spawn->GetDatabaseID()) > 0)
  5510. quick_database_id_lookup.erase(spawn->GetDatabaseID());
  5511. if(spawn->GetSpawnLocationID() > 0 && quick_location_id_lookup.count(spawn->GetSpawnLocationID()) > 0 && quick_location_id_lookup.Get(spawn->GetSpawnLocationID()) == spawn->GetID())
  5512. quick_location_id_lookup.erase(spawn->GetSpawnLocationID());
  5513. if(spawn->GetSpawnGroupID() > 0 && quick_group_id_lookup.count(spawn->GetSpawnGroupID()) > 0 && quick_group_id_lookup.Get(spawn->GetSpawnGroupID()) == spawn->GetID())
  5514. quick_group_id_lookup.erase(spawn->GetSpawnGroupID());
  5515. }
  5516. DeleteSpawnScriptTimers(spawn);
  5517. RemovePlayerProximity(spawn);
  5518. }
  5519. // We don't use RemoveMovementNPC() here as it caused a hell of a delay during reloads
  5520. // instead we remove it from the list directly
  5521. if (spawn->IsNPC())
  5522. movement_spawns.erase(spawn->GetID());
  5523. }
  5524. void ZoneServer::HandleEmote(Spawn* originator, string name) {
  5525. if (!originator) {
  5526. LogWrite(ZONE__ERROR, 0, "Zone", "HandleEmote called with an invalid client");
  5527. return;
  5528. }
  5529. Client* orig_client = (originator->IsPlayer() && ((Player*)originator)->GetClient()) ? ((Player*)originator)->GetClient() : nullptr;
  5530. Client* client = 0;
  5531. int32 cur_client_version = orig_client ? orig_client->GetVersion() : 546;
  5532. Emote* origEmote = visual_states.FindEmote(name, cur_client_version);
  5533. if(!origEmote){
  5534. if(orig_client) {
  5535. 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());
  5536. }
  5537. return;
  5538. }
  5539. Emote* emote = origEmote;
  5540. PacketStruct* packet = 0;
  5541. char* emoteResponse = 0;
  5542. vector<Client*>::iterator client_itr;
  5543. map<int32, Emote*> emote_version_range;
  5544. MClientList.readlock(__FUNCTION__, __LINE__);
  5545. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  5546. client = *client_itr;
  5547. if(!client || (client && originator->IsPlayer() && client->GetPlayer()->IsIgnored(originator->GetName())))
  5548. continue;
  5549. // establish appropriate emote for the version used by the client
  5550. if (client->GetVersion() != cur_client_version)
  5551. {
  5552. map<int32, Emote*>::iterator rangeitr = emote_version_range.find(client->GetVersion());
  5553. if (rangeitr == emote_version_range.end())
  5554. {
  5555. Emote* tmp_new_emote = visual_states.FindEmote(name, client->GetVersion());
  5556. if (tmp_new_emote)
  5557. {
  5558. emote_version_range.insert(make_pair(client->GetVersion(), tmp_new_emote));
  5559. emote = tmp_new_emote;
  5560. } // else its missing just use the current clients default
  5561. }
  5562. else // we have an existing emote already cached
  5563. emote = rangeitr->second;
  5564. }
  5565. else // since the client and originator client match use the original emote
  5566. emote = origEmote;
  5567. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  5568. if(packet){
  5569. packet->setDataByName("spawn_id" , client->GetPlayer()->GetIDWithPlayerSpawn(originator));
  5570. if(!emoteResponse){
  5571. string message;
  5572. if(originator->GetTarget() && originator->GetTarget()->GetID() != originator->GetID()){
  5573. message = emote->GetTargetedMessageString();
  5574. if(message.find("%t") < 0xFFFFFFFF)
  5575. message.replace(message.find("%t"), 2, originator->GetTarget()->GetName());
  5576. }
  5577. if(message.length() == 0)
  5578. message = emote->GetMessageString();
  5579. if(message.find("%g1") < 0xFFFFFFFF){
  5580. if(originator->GetGender() == 1)
  5581. message.replace(message.find("%g1"), 3, "his");
  5582. else
  5583. message.replace(message.find("%g1"), 3, "her");
  5584. }
  5585. if(message.find("%g2") < 0xFFFFFFFF){
  5586. if(originator->GetGender() == 1)
  5587. message.replace(message.find("%g2"), 3, "him");
  5588. else
  5589. message.replace(message.find("%g2"), 3, "her");
  5590. }
  5591. if(message.find("%g3") < 0xFFFFFFFF){
  5592. if(originator->GetGender() == 1)
  5593. message.replace(message.find("%g3"), 3, "he");
  5594. else
  5595. message.replace(message.find("%g3"), 3, "she");
  5596. }
  5597. if(message.length() > 0){
  5598. emoteResponse = new char[message.length() + strlen(originator->GetName()) + 10];
  5599. sprintf(emoteResponse,"%s %s", originator->GetName(), message.c_str());
  5600. }
  5601. }
  5602. if(originator->IsPlayer()) {
  5603. packet->setMediumStringByName("emote_msg", emoteResponse ? emoteResponse : "");
  5604. }
  5605. packet->setDataByName("anim_type", emote->GetVisualState());
  5606. client->QueuePacket(packet->serialize());
  5607. safe_delete(packet);
  5608. safe_delete_array(emoteResponse);
  5609. }
  5610. }
  5611. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5612. }
  5613. void ZoneServer::SetupInstance(int32 createdInstanceID) {
  5614. if ( createdInstanceID == 0 ) // if this happens that isn't good!
  5615. instanceID = ++MinInstanceID;
  5616. else // db should pass the good ID
  5617. instanceID = createdInstanceID;
  5618. }
  5619. void ZoneServer::RemoveDeadSpawn(Spawn* spawn){
  5620. AddDeadSpawn(spawn, 0);
  5621. }
  5622. void ZoneServer::AddDeadSpawn(Spawn* spawn, int32 timer){
  5623. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  5624. if (dead_spawns.count(spawn->GetID()) > 0)
  5625. dead_spawns[spawn->GetID()] = Timer::GetCurrentTime2() + timer;
  5626. else if(timer != 0xFFFFFFFF)
  5627. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + timer));
  5628. else{
  5629. if(spawn->IsEntity() && spawn->HasLoot()){
  5630. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + (15000 * spawn->GetLevel() + 240000)));
  5631. SendUpdateDefaultCommand(spawn, "loot", 10);
  5632. }
  5633. else
  5634. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + 10000));
  5635. }
  5636. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5637. }
  5638. void ZoneServer::WritePlayerStatistics() {
  5639. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5640. while(client_itr.Next())
  5641. client_itr->value->GetPlayer()->WritePlayerStatistics();
  5642. }
  5643. bool ZoneServer::SendRadiusSpawnInfo(Client* client, float radius) {
  5644. if (!client)
  5645. return false;
  5646. Spawn* spawn = 0;
  5647. bool ret = false;
  5648. map<int32, Spawn*>::iterator itr;
  5649. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5650. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5651. spawn = itr->second;
  5652. if (spawn && spawn != client->GetPlayer() && !spawn->IsPlayer() && spawn->GetDistance(client->GetPlayer()) <= radius) {
  5653. const char* type = "NPC";
  5654. const char* specialTypeID = "N/A";
  5655. int32 specialID = 0, spawnEntryID = spawn->GetSpawnEntryID();
  5656. if (spawn->IsObject())
  5657. {
  5658. Object* obj = (Object*)spawn;
  5659. specialID = obj->GetID();
  5660. specialTypeID = "GetID";
  5661. type = "Object";
  5662. }
  5663. else if (spawn->IsSign())
  5664. {
  5665. Sign* sign = (Sign*)spawn;
  5666. specialID = sign->GetWidgetID();
  5667. specialTypeID = "WidgetID";
  5668. type = "Sign";
  5669. }
  5670. else if (spawn->IsWidget())
  5671. {
  5672. Widget* widget = (Widget*)spawn;
  5673. specialID = widget->GetWidgetID();
  5674. specialTypeID = "WidgetID";
  5675. if ( specialID == 0xFFFFFFFF )
  5676. specialTypeID = "WidgetID(spawn_widgets entry missing)";
  5677. type = "Widget";
  5678. }
  5679. else if (spawn->IsGroundSpawn())
  5680. {
  5681. GroundSpawn* gs = (GroundSpawn*)spawn;
  5682. specialID = gs->GetGroundSpawnEntryID();
  5683. specialTypeID = "GroundSpawnEntryID";
  5684. type = "GroundSpawn";
  5685. }
  5686. client->Message(CHANNEL_COLOR_RED, "Name: %s (%s), Spawn Table ID: %u, %s: %u", spawn->GetName(), type, spawn->GetDatabaseID(), specialTypeID, specialID);
  5687. 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());
  5688. 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());
  5689. client->Message(CHANNEL_COLOR_YELLOW, "=============================");
  5690. ret = true;
  5691. }
  5692. }
  5693. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5694. return ret;
  5695. }
  5696. void ZoneServer::FindSpawn(Client* client, char* regSearchStr)
  5697. {
  5698. if (!regSearchStr || strlen(regSearchStr) < 1)
  5699. {
  5700. client->SimpleMessage(CHANNEL_COLOR_RED, "Bad ZoneServer::FindSpawn(Client*, char* regSearchStr) attempt, regSearchStr is NULL or empty.");
  5701. return;
  5702. }
  5703. string resString = string(regSearchStr);
  5704. try
  5705. {
  5706. std::regex pre_re_check("^[a-zA-Z0-9_ ]+$");
  5707. bool output = std::regex_match(resString, pre_re_check);
  5708. if (output)
  5709. {
  5710. string newStr(".*");
  5711. newStr.append(regSearchStr);
  5712. newStr.append(".*");
  5713. resString = newStr;
  5714. }
  5715. }
  5716. catch (...)
  5717. {
  5718. client->SimpleMessage(CHANNEL_COLOR_RED, "Try/Catch ZoneServer::FindSpawn(Client*, char* regSearchStr) failure.");
  5719. return;
  5720. }
  5721. std::regex re;
  5722. try {
  5723. re = std::regex(resString, std::regex_constants::icase);
  5724. }
  5725. catch(...) {
  5726. client->SimpleMessage(CHANNEL_COLOR_RED, "Invalid regex for FindSpawn.");
  5727. return;
  5728. }
  5729. client->Message(CHANNEL_NARRATIVE, "RegEx Search Spawn List: %s", regSearchStr);
  5730. client->Message(CHANNEL_NARRATIVE, "Database ID | Spawn Name | X , Y , Z");
  5731. client->Message(CHANNEL_NARRATIVE, "========================");
  5732. map<int32, Spawn*>::iterator itr;
  5733. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5734. int32 spawnsFound = 0;
  5735. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5736. Spawn* spawn = itr->second;
  5737. if (!spawn || !spawn->GetName())
  5738. continue;
  5739. bool output = false;
  5740. try {
  5741. output = std::regex_match(string(spawn->GetName()), re);
  5742. }
  5743. catch (...)
  5744. {
  5745. continue;
  5746. }
  5747. if (output)
  5748. {
  5749. client->Message(CHANNEL_NARRATIVE, "%i | %s | %f , %f , %f", spawn->GetDatabaseID(), spawn->GetName(), spawn->GetX(), spawn->GetY(), spawn->GetZ());
  5750. spawnsFound++;
  5751. }
  5752. }
  5753. client->Message(CHANNEL_NARRATIVE, "========================", spawnsFound);
  5754. client->Message(CHANNEL_NARRATIVE, "%u Results Found.", spawnsFound);
  5755. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5756. }
  5757. void ZoneServer::AddPlayerTracking(Player* player) {
  5758. if (player && !player->GetIsTracking() && players_tracking.count(player->GetDatabaseID()) == 0) {
  5759. Client* client = GetClientBySpawn(player);
  5760. if (client) {
  5761. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5762. if (packet) {
  5763. player->SetIsTracking(true);
  5764. players_tracking.Put(client->GetCharacterID(), player);
  5765. packet->setDataByName("mode", TRACKING_START);
  5766. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5767. client->QueuePacket(packet->serialize());
  5768. safe_delete(packet);
  5769. }
  5770. }
  5771. }
  5772. }
  5773. void ZoneServer::RemovePlayerTracking(Player* player, int8 mode) {
  5774. if (player && player->GetIsTracking()) {
  5775. Client* client = GetClientBySpawn(player);
  5776. if (client) {
  5777. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5778. if (packet) {
  5779. player->SetIsTracking(false);
  5780. players_tracking.erase(client->GetCharacterID());
  5781. packet->setDataByName("mode", mode);
  5782. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5783. client->QueuePacket(packet->serialize());
  5784. safe_delete(packet);
  5785. }
  5786. }
  5787. }
  5788. }
  5789. void ZoneServer::ProcessTracking() {
  5790. MutexMap<int32, Player*>::iterator itr = players_tracking.begin();
  5791. while (itr.Next())
  5792. ProcessTracking(GetClientBySpawn(itr->second));
  5793. }
  5794. void ZoneServer::ProcessTracking(Client* client) {
  5795. if (!client)
  5796. return;
  5797. Player* player = client->GetPlayer();
  5798. if (player && player->GetIsTracking()) {
  5799. MutexMap<int32, Spawn*>::iterator spawn_itr;
  5800. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5801. if (packet) {
  5802. packet->setDataByName("mode", TRACKING_UPDATE);
  5803. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5804. vector<TrackedSpawn*> spawns_tracked;
  5805. while (spawn_itr.Next()) {
  5806. Spawn* spawn = spawn_itr->second;
  5807. float distance = player->GetDistance(spawn);
  5808. if (spawn->IsEntity() && distance <= 80 && spawn != player) {
  5809. TrackedSpawn* ts = new TrackedSpawn;
  5810. ts->spawn = spawn;
  5811. ts->distance = distance;
  5812. /* Add spawns in ascending order from closest to furthest */
  5813. if (spawns_tracked.empty())
  5814. spawns_tracked.push_back(ts);
  5815. else {
  5816. vector<TrackedSpawn*>::iterator tracked_itr;
  5817. bool added = false;
  5818. for (tracked_itr = spawns_tracked.begin(); tracked_itr != spawns_tracked.end(); tracked_itr++) {
  5819. TrackedSpawn* cur_ts = *tracked_itr;
  5820. if (ts->distance <= cur_ts->distance) {
  5821. spawns_tracked.insert(tracked_itr, ts);
  5822. added = true;
  5823. break;
  5824. }
  5825. }
  5826. if (!added)
  5827. spawns_tracked.push_back(ts);
  5828. }
  5829. }
  5830. }
  5831. packet->setArrayLengthByName("num_spawns", spawns_tracked.size());
  5832. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5833. TrackedSpawn* ts = spawns_tracked[i];
  5834. LogWrite(ZONE__DEBUG, 0, "Zone", "%s (%f)", ts->spawn->GetName(), ts->distance);
  5835. packet->setArrayDataByName("spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5836. packet->setArrayDataByName("spawn_name", ts->spawn->GetName(), i);
  5837. if (ts->spawn->IsPlayer())
  5838. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_PC, i);
  5839. else
  5840. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_NPC, i);
  5841. packet->setArrayDataByName("spawn_con_color", player->GetArrowColor(ts->spawn->GetLevel()), i);
  5842. }
  5843. packet->setArrayLengthByName("num_array1", 0);
  5844. //for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5845. //}
  5846. packet->setArrayLengthByName("num_spawns2", spawns_tracked.size());
  5847. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5848. TrackedSpawn* ts = spawns_tracked[i];
  5849. packet->setArrayDataByName("list_spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5850. packet->setArrayDataByName("list_number", i, i);
  5851. }
  5852. client->QueuePacket(packet->serialize());
  5853. safe_delete(packet);
  5854. for (int32 i = 0; i < spawns_tracked.size(); i++)
  5855. safe_delete(spawns_tracked[i]);
  5856. }
  5857. }
  5858. }
  5859. void ZoneServer::SendEpicMobDeathToGuild(Player* killer, Spawn* victim) {
  5860. if (killer && victim) {
  5861. LogWrite(MISC__TODO, 1, "TODO" , "Check if player is in raid\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  5862. if (killer->GetGroupMemberInfo()) {
  5863. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  5864. deque<GroupMemberInfo*>::iterator itr;
  5865. PlayerGroup* group = world.GetGroupManager()->GetGroup(killer->GetGroupMemberInfo()->group_id);
  5866. if (group)
  5867. {
  5868. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  5869. deque<GroupMemberInfo*>* members = group->GetMembers();
  5870. for (itr = members->begin(); itr != members->end(); itr++) {
  5871. GroupMemberInfo* gmi = *itr;
  5872. if (gmi->client) {
  5873. Player* group_member = gmi->client->GetPlayer();
  5874. if (group_member && group_member->GetGuild()) {
  5875. Guild* guild = group_member->GetGuild();
  5876. string message = Guild::GetEpicMobDeathMessage(group_member->GetName(), victim->GetName());
  5877. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5878. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5879. }
  5880. }
  5881. }
  5882. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  5883. }
  5884. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  5885. }
  5886. else if (killer->GetGuild()) {
  5887. Guild* guild = killer->GetGuild();
  5888. string message = Guild::GetEpicMobDeathMessage(killer->GetName(), victim->GetName());
  5889. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5890. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5891. }
  5892. }
  5893. }
  5894. void ZoneServer::ProcessAggroChecks(Spawn* spawn) {
  5895. if (spawn->GetFactionID() < 1 || spawn->EngagedInCombat())
  5896. return;
  5897. // If faction based combat is not allowed then no need to run the loops so just return out
  5898. if(!rule_manager.GetGlobalRule(R_Faction, AllowFactionBasedCombat)->GetBool())
  5899. return;
  5900. if (spawn && spawn->IsNPC() && spawn->Alive())
  5901. CheckEnemyList((NPC*)spawn);
  5902. }
  5903. void ZoneServer::SendUpdateTitles(Client* client, Title* suffix, Title* prefix) {
  5904. assert(client);
  5905. if (client->GetVersion() > 546)
  5906. SendUpdateTitles(client->GetPlayer(), suffix, prefix);
  5907. }
  5908. void ZoneServer::SendUpdateTitles(Spawn *spawn, Title *suffix, Title *prefix) {
  5909. if (!spawn)
  5910. return;
  5911. vector<Client*>::iterator itr;
  5912. PacketStruct *packet;
  5913. Client* current_client;
  5914. MClientList.readlock(__FUNCTION__, __LINE__);
  5915. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5916. current_client = *itr;
  5917. if (current_client->GetVersion() <= 546)
  5918. continue;
  5919. if (!(packet = configReader.getStruct("WS_UpdateTitle", current_client->GetVersion())))
  5920. continue;
  5921. packet->setDataByName("player_id", current_client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  5922. packet->setDataByName("player_name", spawn->GetName());
  5923. packet->setDataByName("unknown1", 1, 1);
  5924. if(suffix)
  5925. packet->setDataByName("suffix_title", suffix->GetName());
  5926. else
  5927. packet->setDataByName("suffix_title", spawn->GetSuffixTitle());
  5928. if(prefix)
  5929. packet->setDataByName("prefix_title", prefix->GetName());
  5930. else
  5931. packet->setDataByName("prefix_title", spawn->GetPrefixTitle());
  5932. packet->setDataByName("last_name", spawn->GetLastName());
  5933. packet->setDataByName("sub_title", spawn->GetSubTitle());
  5934. current_client->QueuePacket(packet->serialize());
  5935. safe_delete(packet);
  5936. }
  5937. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5938. }
  5939. void ZoneServer::AddTransportSpawn(Spawn* spawn){
  5940. if(!spawn)
  5941. return;
  5942. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5943. transport_spawns.push_back(spawn->GetID());
  5944. spawn->SetTransportSpawn(true);
  5945. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5946. }
  5947. Spawn* ZoneServer::GetClosestTransportSpawn(float x, float y, float z){
  5948. Spawn* spawn = 0;
  5949. Spawn* closest_spawn = 0;
  5950. float closest_distance = 0.0;
  5951. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5952. vector<int32>::iterator itr = transport_spawns.begin();
  5953. while(itr != transport_spawns.end()){
  5954. spawn = GetSpawnByID(*itr);
  5955. if(spawn){
  5956. if(closest_distance == 0.0){
  5957. closest_spawn = spawn;
  5958. closest_distance = spawn->GetDistance(x, y, z);
  5959. }
  5960. else if(spawn->GetDistance(x, y, z) < closest_distance){
  5961. closest_spawn = spawn;
  5962. closest_distance = spawn->GetDistance(x, y, z);
  5963. }
  5964. itr++;
  5965. }
  5966. else
  5967. itr = transport_spawns.erase(itr);
  5968. }
  5969. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5970. return closest_spawn;
  5971. }
  5972. Spawn* ZoneServer::GetTransportByRailID(sint64 rail_id){
  5973. Spawn* spawn = 0;
  5974. Spawn* closest_spawn = 0;
  5975. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  5976. vector<int32>::iterator itr = transport_spawns.begin();
  5977. while(itr != transport_spawns.end()){
  5978. spawn = GetSpawnByID(*itr);
  5979. //printf("Rail id: %i vs %i\n", spawn ? spawn->GetRailID() : 0, rail_id);
  5980. if(spawn && spawn->GetRailID() == rail_id){
  5981. closest_spawn = spawn;
  5982. break;
  5983. }
  5984. itr++;
  5985. }
  5986. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  5987. return closest_spawn;
  5988. }
  5989. void ZoneServer::SetRain(float val) {
  5990. rain = val;
  5991. vector<Client*>::iterator itr;
  5992. MClientList.readlock(__FUNCTION__, __LINE__);
  5993. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5994. Client* client = *itr;
  5995. client->GetPlayer()->GetInfoStruct()->set_rain(val);
  5996. client->GetPlayer()->SetCharSheetChanged(true);
  5997. if( val >= 0.75 && !weather_signaled )
  5998. {
  5999. client->SimpleMessage(CHANNEL_NARRATIVE, "It starts to rain.");
  6000. }
  6001. else if( val < 0.75 && weather_signaled )
  6002. {
  6003. client->SimpleMessage(CHANNEL_NARRATIVE, "It stops raining.");
  6004. }
  6005. }
  6006. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6007. if (val >= 0.75 && !weather_signaled) {
  6008. weather_signaled = true;
  6009. ProcessSpawnConditional(SPAWN_CONDITIONAL_RAINING);
  6010. }
  6011. else if (val < 0.75 && weather_signaled) {
  6012. weather_signaled = false;
  6013. ProcessSpawnConditional(SPAWN_CONDITIONAL_NOT_RAINING);
  6014. }
  6015. }
  6016. void ZoneServer::SetWind(float val) {
  6017. vector<Client*>::iterator itr;
  6018. MClientList.readlock(__FUNCTION__, __LINE__);
  6019. for (itr = clients.begin(); itr != clients.end(); itr++) {
  6020. Client* client = *itr;
  6021. client->GetPlayer()->GetInfoStruct()->set_wind(val);
  6022. client->GetPlayer()->SetCharSheetChanged(true);
  6023. }
  6024. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6025. }
  6026. void ZoneServer::ProcessWeather()
  6027. {
  6028. // if the global rule to disable weather is set, or if the `weather_allowed` field in the zone record == 0, do not process weather
  6029. if( !weather_enabled || !isWeatherAllowed() )
  6030. return;
  6031. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Processing weather changes", zone_name);
  6032. float new_weather = 0;
  6033. float weather_offset = 0;
  6034. bool change_weather = false;
  6035. // check to see if it is time to change the weather according to weather_frequency (time between changes)
  6036. if( weather_last_changed_time <= (Timer::GetUnixTimeStamp() - weather_frequency) )
  6037. {
  6038. LogWrite(ZONE__DEBUG, 2, "Zone", "%s: Checking for weather changes", zone_name);
  6039. // reset last changed time (frequency check)
  6040. weather_last_changed_time = Timer::GetUnixTimeStamp();
  6041. // this is the chance a weather change occurs at all at the expired interval
  6042. int8 weather_random = MakeRandomInt(1, 100);
  6043. 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");
  6044. if( weather_random <= weather_change_chance )
  6045. {
  6046. change_weather = true;
  6047. weather_offset = weather_change_amount;
  6048. if( weather_type == 3 ) // chaotic weather patterns, random weather between min/max
  6049. {
  6050. new_weather = MakeRandomFloat(weather_min_severity, weather_max_severity);
  6051. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Chaotic weather severity changed to %.2f", zone_name, new_weather);
  6052. weather_pattern = 2;
  6053. }
  6054. else if( weather_type == 2 ) // random weather patterns, combination of normal + dynamic + max_offset
  6055. {
  6056. weather_offset = MakeRandomFloat(weather_change_amount, weather_dynamic_offset);
  6057. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Random weather severity changed by %.2f", zone_name, weather_offset);
  6058. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  6059. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  6060. if( weather_random <= weather_alter )
  6061. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  6062. }
  6063. else if( weather_type == 1 ) // dynamic weather patterns, weather may not reach min/max
  6064. {
  6065. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  6066. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  6067. if( weather_random <= weather_alter )
  6068. {
  6069. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  6070. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Dynamic weather pattern changed to %i", zone_name, weather_pattern);
  6071. }
  6072. }
  6073. else // normal weather patterns, weather starts at min, goes to max, then back down again
  6074. {
  6075. // do nothing (processed below)
  6076. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Normal weather severity changed by %.2f", zone_name, weather_offset);
  6077. }
  6078. // when all done, change the weather
  6079. if( change_weather )
  6080. {
  6081. if( weather_pattern == 1 )
  6082. {
  6083. // weather is getting worse, til it reaches weather_max_severity
  6084. new_weather = ( weather_current_severity <= weather_max_severity ) ? weather_current_severity + weather_offset : weather_max_severity;
  6085. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Increased weather severity by %.2f", zone_name, weather_offset);
  6086. if(new_weather > weather_max_severity)
  6087. {
  6088. new_weather = weather_max_severity - weather_offset;
  6089. weather_pattern = 0;
  6090. }
  6091. }
  6092. else if( weather_pattern == 0 )
  6093. {
  6094. // weather is clearing up, til it reaches weather_min_severity
  6095. new_weather = ( weather_current_severity >= weather_min_severity ) ? weather_current_severity - weather_offset : weather_min_severity;
  6096. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Decreased weather severity by %.2f", zone_name, weather_offset);
  6097. if(new_weather < weather_min_severity)
  6098. {
  6099. new_weather = weather_min_severity + weather_offset;
  6100. weather_pattern = 1;
  6101. }
  6102. }
  6103. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather change triggered from %.2f to %.2f", zone_name, weather_current_severity, new_weather);
  6104. this->SetRain(new_weather);
  6105. weather_current_severity = new_weather;
  6106. }
  6107. }
  6108. }
  6109. else
  6110. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Not time to change weather yet", zone_name);
  6111. }
  6112. void ZoneServer::HidePrivateSpawn(Spawn* spawn) {
  6113. if (!spawn->IsPrivateSpawn())
  6114. return;
  6115. Client* client = 0;
  6116. Player* player = 0;
  6117. PacketStruct* packet = 0;
  6118. int32 packet_version = 0;
  6119. MutexList<Client*>::iterator itr = connected_clients.begin();
  6120. while (itr->Next()) {
  6121. client = itr->value;
  6122. player = client->GetPlayer();
  6123. if (player->WasSentSpawn(spawn->GetID()) || client->GetPlayer()->IsSendingSpawn(spawn->GetID())) {
  6124. if (!packet || packet_version != client->GetVersion()) {
  6125. safe_delete(packet);
  6126. packet_version = client->GetVersion();
  6127. packet = configReader.getStruct("WS_DestroyGhostCmd", packet_version);
  6128. }
  6129. SendRemoveSpawn(client, spawn, packet);
  6130. if(spawn_range_map.count(client) > 0)
  6131. spawn_range_map.Get(client)->erase(spawn->GetID());
  6132. if(player->GetTarget() == spawn)
  6133. player->SetTarget(0);
  6134. }
  6135. }
  6136. safe_delete(packet);
  6137. }
  6138. SpawnLocation* ZoneServer::GetSpawnLocation(int32 id) {
  6139. SpawnLocation* ret = 0;
  6140. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  6141. if (spawn_location_list.count(id) > 0)
  6142. ret = spawn_location_list[id];
  6143. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  6144. return ret;
  6145. }
  6146. void ZoneServer::PlayAnimation(Spawn* spawn, int32 visual_state, Spawn* spawn2, int8 hide_type){
  6147. Client* client = 0;
  6148. PacketStruct* packet = 0;
  6149. Spawn* exclude_spawn = 0;
  6150. if (!spawn)
  6151. return;
  6152. if (spawn2){
  6153. if(hide_type == 1){
  6154. client = GetClientBySpawn(spawn2);
  6155. if(client){
  6156. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6157. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  6158. packet->setDataByName("anim_type", visual_state);
  6159. client->QueuePacket(packet->serialize());
  6160. }
  6161. safe_delete(packet);
  6162. return;
  6163. }
  6164. if(hide_type == 2)
  6165. exclude_spawn = spawn2;
  6166. }
  6167. vector<Client*>::iterator client_itr;
  6168. MClientList.readlock(__FUNCTION__, __LINE__);
  6169. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  6170. client = *client_itr;
  6171. if(spawn->GetDistance(client->GetPlayer()) > 50)
  6172. continue;
  6173. if(exclude_spawn == client->GetPlayer())
  6174. continue;
  6175. if(!client->IsReadyForUpdates()) // client is not in world yet so we shouldn't be sending animations of spawns yet
  6176. continue;
  6177. if(!packet || packet->GetVersion() != client->GetVersion()) {
  6178. safe_delete(packet);
  6179. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  6180. }
  6181. if (packet) {
  6182. int32 spawn_id = client->GetPlayer()->GetIDWithPlayerSpawn(spawn);
  6183. if(spawn_id) {
  6184. packet->setDataByName("spawn_id", spawn_id);
  6185. packet->setDataByName("anim_type", visual_state);
  6186. client->QueuePacket(packet->serialize());
  6187. }
  6188. }
  6189. }
  6190. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6191. safe_delete(packet);
  6192. }
  6193. vector<Spawn*> ZoneServer::GetSpawnsByID(int32 id) {
  6194. vector<Spawn*> tmp_list;
  6195. Spawn* spawn;
  6196. map<int32, Spawn*>::iterator itr;
  6197. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6198. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6199. spawn = itr->second;
  6200. if (spawn && (spawn->GetDatabaseID() == id))
  6201. tmp_list.push_back(spawn);
  6202. }
  6203. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6204. return tmp_list;
  6205. }
  6206. vector<Spawn*> ZoneServer::GetSpawnsByRailID(sint64 rail_id) {
  6207. vector<Spawn*> tmp_list;
  6208. Spawn* spawn;
  6209. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6210. vector<int32>::iterator itr = transport_spawns.begin();
  6211. while(itr != transport_spawns.end()){
  6212. spawn = GetSpawnByID(*itr);
  6213. if(spawn && spawn->GetRailID() == rail_id){
  6214. tmp_list.push_back(spawn);
  6215. }
  6216. itr++;
  6217. }
  6218. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6219. return tmp_list;
  6220. }
  6221. void ZoneServer::RemovePlayerPassenger(int32 char_id) {
  6222. vector<Spawn*> tmp_list;
  6223. Spawn* spawn;
  6224. MTransportSpawns.readlock(__FUNCTION__, __LINE__);
  6225. vector<int32>::iterator itr = transport_spawns.begin();
  6226. while(itr != transport_spawns.end()){
  6227. spawn = GetSpawnByID(*itr);
  6228. if(spawn) {
  6229. spawn->RemoveRailPassenger(char_id);
  6230. }
  6231. itr++;
  6232. }
  6233. MTransportSpawns.releasereadlock(__FUNCTION__, __LINE__);
  6234. }
  6235. vector<int32> ZoneServer::GetAttackableSpawnsByDistance(Spawn* caster, float distance) {
  6236. vector<int32> ret;
  6237. Spawn* spawn = 0;
  6238. std::shared_lock lock(MGridMaps);
  6239. std::map<int32, GridMap*>::iterator grids = grid_maps.find(caster->GetLocation());
  6240. if(grids != grid_maps.end()) {
  6241. grids->second->MSpawns.lock_shared();
  6242. typedef map <int32, Spawn*> SpawnMapType;
  6243. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  6244. Spawn* spawn = it->second;
  6245. if (spawn && spawn->IsNPC() && spawn->appearance.attackable > 0 && spawn->GetID() > 0 && spawn->GetID() != caster->GetID() &&
  6246. spawn->Alive() && spawn->GetDistance(caster, true) <= distance) {
  6247. ret.push_back(spawn->GetID());
  6248. }
  6249. }
  6250. grids->second->MSpawns.unlock_shared();
  6251. }
  6252. return ret;
  6253. }
  6254. void ZoneServer::ResurrectSpawn(Spawn* spawn, Client* client) {
  6255. if(!client || !spawn)
  6256. return;
  6257. PendingResurrection* rez = client->GetCurrentRez();
  6258. if(!rez || !rez->caster)
  6259. return;
  6260. PacketStruct* packet = 0;
  6261. float power_perc = rez->mp_perc;
  6262. float health_perc = rez->hp_perc;
  6263. Spawn* caster_spawn = rez->caster;
  6264. sint32 heal_amt = 0;
  6265. sint32 power_amt = 0;
  6266. bool no_calcs = rez->no_calcs;
  6267. int8 crit_mod = rez->crit_mod;
  6268. Entity* caster = 0;
  6269. InfoStruct* info = 0;
  6270. bool crit = false;
  6271. string heal_spell = rez->heal_name;
  6272. int16 heal_packet_type = 0;
  6273. int16 power_packet_type = 0;
  6274. //Calculations for how much to heal the spawn
  6275. if(health_perc > 0)
  6276. heal_amt = (spawn->GetTotalHP() * (health_perc / 100));
  6277. if(power_perc > 0)
  6278. power_amt = (spawn->GetTotalPower() * (power_perc / 100));
  6279. if(caster_spawn->IsEntity()){
  6280. caster = ((Entity*)caster_spawn);
  6281. info = caster->GetInfoStruct();
  6282. }
  6283. if(!no_calcs && caster){
  6284. heal_amt = caster->CalculateHealAmount(spawn, heal_amt, crit_mod, &crit);
  6285. power_amt = caster->CalculateHealAmount(spawn, power_amt, crit_mod, &crit);
  6286. }
  6287. //Set this rez as a crit to be passed to subspell (not yet used)
  6288. rez->crit = true;
  6289. //Set Heal amt to 1 if 0 now so the player has health
  6290. if(heal_amt == 0)
  6291. heal_amt = 1;
  6292. if(heal_amt > spawn->GetTotalHP())
  6293. heal_amt = spawn->GetTotalHP();
  6294. if(power_amt > spawn->GetTotalPower())
  6295. power_amt = spawn->GetTotalPower();
  6296. spawn->SetHP(heal_amt);
  6297. if(power_amt > 0)
  6298. spawn->SetPower(power_amt);
  6299. if(client && caster){
  6300. EQ2Packet* move = ((Player*)spawn)->Move(caster->GetX(), caster->GetY(), caster->GetZ(), client->GetVersion());
  6301. if(move)
  6302. client->QueuePacket(move);
  6303. }
  6304. if(crit){
  6305. power_packet_type = HEAL_PACKET_TYPE_CRIT_MANA;
  6306. heal_packet_type = HEAL_PACKET_TYPE_CRIT_HEAL;
  6307. }
  6308. else {
  6309. power_packet_type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  6310. heal_packet_type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  6311. }
  6312. SendHealPacket(caster, spawn, heal_packet_type, heal_amt, heal_spell.c_str());
  6313. if(power_amt > 0)
  6314. SendHealPacket(caster, spawn, power_packet_type, power_amt, heal_spell.c_str());
  6315. //The following code sets the spawn as alive
  6316. if(dead_spawns.count(spawn->GetID()) > 0)
  6317. dead_spawns.erase(spawn->GetID());
  6318. if(spawn->IsPlayer()){
  6319. spawn->SetSpawnType(4);
  6320. client = GetClientBySpawn(spawn);
  6321. if(client){
  6322. packet = configReader.getStruct("WS_Resurrected", client->GetVersion());
  6323. if(packet){
  6324. client->QueuePacket(packet->serialize());
  6325. }
  6326. packet = configReader.getStruct("WS_ServerControlFlags", client->GetVersion());
  6327. if(packet)
  6328. {
  6329. packet->setDataByName("parameter1", 8);
  6330. client->QueuePacket(packet->serialize());
  6331. packet->setDataByName("parameter1", 16);
  6332. client->QueuePacket(packet->serialize());
  6333. }
  6334. safe_delete(packet);
  6335. client->SimpleMessage(CHANNEL_NARRATIVE, "You regain consciousness!");
  6336. }
  6337. }
  6338. spawn->SendSpawnChanges(true);
  6339. spawn->SetTempActionState(-1);
  6340. spawn->appearance.attackable = 1;
  6341. }
  6342. void ZoneServer::SendDispellPacket(Entity* caster, Spawn* target, string dispell_name, string spell_name, int8 dispell_type){
  6343. if(!caster || !target)
  6344. return;
  6345. Client* client = 0;
  6346. Player* player = 0;
  6347. PacketStruct* packet = 0;
  6348. vector<Client*>::iterator client_itr;
  6349. MClientList.readlock(__FUNCTION__, __LINE__);
  6350. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++){
  6351. client = *client_itr;
  6352. if(!client || !(player = client->GetPlayer()) || (player != caster && ((caster && player->WasSentSpawn(caster->GetID()) == false) || (target && player->WasSentSpawn(target->GetID()) == false))))
  6353. continue;
  6354. if(caster && caster->GetDistance(player) > 50)
  6355. continue;
  6356. if(target && target->GetDistance(player) > 50)
  6357. continue;
  6358. packet = configReader.getStruct("WS_HearDispell", client->GetVersion());
  6359. if(packet){
  6360. packet->setDataByName("spell_name", spell_name.c_str());
  6361. packet->setDataByName("dispell_name", dispell_name.c_str());
  6362. packet->setDataByName("caster", player->GetIDWithPlayerSpawn(caster));
  6363. packet->setDataByName("target", player->GetIDWithPlayerSpawn(target));
  6364. packet->setDataByName("type", dispell_type);
  6365. client->QueuePacket(packet->serialize());
  6366. }
  6367. safe_delete(packet);
  6368. }
  6369. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6370. }
  6371. void ZoneServer::DismissAllPets() {
  6372. Spawn* spawn = 0;
  6373. map<int32, Spawn*>::iterator itr;
  6374. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6375. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6376. spawn = itr->second;
  6377. if (spawn && spawn->IsEntity())
  6378. ((Entity*)spawn)->DismissAllPets();
  6379. }
  6380. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6381. }
  6382. void ZoneServer::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target, bool remove_caster){
  6383. if (spellProcess)
  6384. spellProcess->RemoveTargetFromSpell(spell, target, remove_caster);
  6385. }
  6386. void ZoneServer::ClearHate(Entity* entity) {
  6387. Spawn* spawn = 0;
  6388. map<int32, Spawn*>::iterator itr;
  6389. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6390. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6391. spawn = itr->second;
  6392. if (spawn && spawn->IsNPC() && ((NPC*)spawn)->Brain())
  6393. ((NPC*)spawn)->Brain()->ClearHate(entity);
  6394. }
  6395. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6396. }
  6397. ThreadReturnType ZoneLoop(void* tmp) {
  6398. #ifdef WIN32
  6399. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6400. #endif
  6401. if (tmp == 0) {
  6402. ThrowError("ZoneLoop(): tmp = 0!");
  6403. THREAD_RETURN(NULL);
  6404. }
  6405. ZoneServer* zs = (ZoneServer*) tmp;
  6406. while (zs->Process()) {
  6407. if(zs->GetClientCount() == 0)
  6408. Sleep(1000);
  6409. else
  6410. Sleep(10);
  6411. }
  6412. zs->Process(); //run loop once more to clean up some functions
  6413. safe_delete(zs);
  6414. THREAD_RETURN(NULL);
  6415. }
  6416. ThreadReturnType SpawnLoop(void* tmp) {
  6417. #ifdef WIN32
  6418. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6419. #endif
  6420. if (tmp == 0) {
  6421. ThrowError("SpawnLoop(): tmp = 0!");
  6422. THREAD_RETURN(NULL);
  6423. }
  6424. ZoneServer* zs = (ZoneServer*) tmp;
  6425. #ifndef NO_CATCH
  6426. try {
  6427. #endif
  6428. zs->spawnthread_active = true;
  6429. while (zs->SpawnProcess()) {
  6430. if(zs->GetClientCount() == 0)
  6431. Sleep(1000);
  6432. else
  6433. Sleep(20);
  6434. }
  6435. zs->spawnthread_active = false;
  6436. #ifndef NO_CATCH
  6437. }
  6438. catch(...) {
  6439. zs->spawnthread_active = false;
  6440. zs->initial_spawn_threads_active = 0;
  6441. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop, shutting down zone '%s'...", zs->GetZoneName());
  6442. try{
  6443. zs->Shutdown();
  6444. }
  6445. catch(...){
  6446. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop while shutting down zone '%s'...", zs->GetZoneName());
  6447. throw;
  6448. }
  6449. throw;
  6450. }
  6451. #endif
  6452. THREAD_RETURN(NULL);
  6453. }
  6454. ThreadReturnType SendInitialSpawns(void* tmp) {
  6455. #ifdef WIN32
  6456. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  6457. #endif
  6458. if (tmp == 0) {
  6459. ThrowError("SendInitialSpawns(): tmp = 0!");
  6460. THREAD_RETURN(NULL);
  6461. }
  6462. Client* client = (Client*) tmp;
  6463. client->GetCurrentZone()->SendZoneSpawns(client);
  6464. THREAD_RETURN(NULL);
  6465. }
  6466. ThreadReturnType SendLevelChangedSpawns(void* tmp) {
  6467. #ifdef WIN32
  6468. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  6469. #endif
  6470. if (tmp == 0) {
  6471. ThrowError("SendLevelChangedSpawns(): tmp = 0!");
  6472. THREAD_RETURN(NULL);
  6473. }
  6474. Client* client = (Client*)tmp;
  6475. client->GetCurrentZone()->SendAllSpawnsForLevelChange(client);
  6476. THREAD_RETURN(NULL);
  6477. }
  6478. void ZoneServer::SetSpawnStructs(Client* client) {
  6479. int16 client_ver = client->GetVersion();
  6480. Player* player = client->GetPlayer();
  6481. //Save a copy of the correct spawn substructs for the client's player, save here a copy if we don't have one
  6482. PacketStruct* pos = configReader.getStruct("Substruct_SpawnPositionStruct", client_ver);
  6483. player->SetSpawnPosStruct(pos);
  6484. if (versioned_pos_structs.count(pos->GetVersion()) == 0)
  6485. versioned_pos_structs[pos->GetVersion()] = new PacketStruct(pos, true);
  6486. PacketStruct* vis = configReader.getStruct("Substruct_SpawnVisualizationInfoStruct", client_ver);
  6487. player->SetSpawnVisStruct(vis);
  6488. if (versioned_vis_structs.count(vis->GetVersion()) == 0)
  6489. versioned_vis_structs[vis->GetVersion()] = new PacketStruct(vis, true);
  6490. PacketStruct* info = configReader.getStruct("Substruct_SpawnInfoStruct", client_ver);
  6491. player->SetSpawnInfoStruct(info);
  6492. if (versioned_info_structs.count(info->GetVersion()) == 0)
  6493. versioned_info_structs[info->GetVersion()] = new PacketStruct(info, true);
  6494. PacketStruct* header = configReader.getStruct("WS_SpawnStruct_Header", client_ver);
  6495. player->SetSpawnHeaderStruct(header);
  6496. PacketStruct* footer = configReader.getStruct("WS_SpawnStruct_Footer", client_ver);
  6497. player->SetSpawnFooterStruct(footer);
  6498. PacketStruct* sfooter = configReader.getStruct("WS_SignWidgetSpawnStruct_Footer", client_ver);
  6499. player->SetSignFooterStruct(sfooter);
  6500. PacketStruct* wfooter = configReader.getStruct("WS_WidgetSpawnStruct_Footer", client_ver);
  6501. player->SetWidgetFooterStruct(wfooter);
  6502. }
  6503. Spawn* ZoneServer::GetSpawn(int32 id){
  6504. Spawn* ret = 0;
  6505. if(GetNPC(id))
  6506. ret = GetNewNPC(id);
  6507. else if(this->GetObject(id))
  6508. ret = GetNewObject(id);
  6509. else if(GetWidget(id))
  6510. ret = GetNewWidget(id);
  6511. else if(GetSign(id))
  6512. ret = GetNewSign(id);
  6513. else if(GetGroundSpawn(id))
  6514. ret = GetNewGroundSpawn(id);
  6515. // Unable to find the spawn in the list lets attempt to add it if we are not currently reloading
  6516. else if (!reloading && database.LoadNPC(this, id)) {
  6517. if (GetNPC(id))
  6518. ret = GetNewNPC(id);
  6519. else
  6520. LogWrite(NPC__ERROR, 0, "NPC", "Database inserted npc (%u) but was still unable to retrieve it!", id);
  6521. }
  6522. else if (!reloading && database.LoadObject(this, id)) {
  6523. if (this->GetObject(id))
  6524. ret = GetNewObject(id);
  6525. else
  6526. LogWrite(OBJECT__ERROR, 0, "Object", "Database inserted object (%u) but was still unable to retrieve it!", id);
  6527. }
  6528. else if (!reloading && database.LoadWidget(this, id)) {
  6529. if (GetWidget(id))
  6530. ret = GetNewWidget(id);
  6531. else
  6532. LogWrite(WIDGET__ERROR, 0, "Widget", "Database inserted widget (%u) but was still unable to retrieve it!", id);
  6533. }
  6534. else if (!reloading && database.LoadSign(this, id)) {
  6535. if (GetSign(id))
  6536. ret = GetNewSign(id);
  6537. else
  6538. LogWrite(SIGN__ERROR, 0, "Sign", "Database inserted sign (%u) but was still unable to retrieve it!", id);
  6539. }
  6540. else if (!reloading && database.LoadGroundSpawn(this, id)) {
  6541. if (GetGroundSpawn(id))
  6542. ret = GetNewGroundSpawn(id);
  6543. else
  6544. LogWrite(GROUNDSPAWN__ERROR, 0, "GSpawn", "Database inserted ground spawn (%u) but was still unable to retrieve it!", id);
  6545. }
  6546. if(ret && ret->IsOmittedByDBFlag())
  6547. {
  6548. LogWrite(SPAWN__WARNING, 0, "Spawn", "Spawn (%u) was skipped due to a missing expansion / holiday flag being met.", id);
  6549. safe_delete(ret);
  6550. ret = 0;
  6551. }
  6552. if(ret)
  6553. ret->SetID(Spawn::NextID());
  6554. return ret;
  6555. }
  6556. vector<EntityCommand*>* ZoneServer::GetEntityCommandList(int32 id){
  6557. if(entity_command_list.count(id) > 0)
  6558. return entity_command_list[id];
  6559. else
  6560. return 0;
  6561. }
  6562. void ZoneServer::SetEntityCommandList(int32 id, EntityCommand* command) {
  6563. if (entity_command_list.count(id) == 0)
  6564. entity_command_list[id] = new vector<EntityCommand*>;
  6565. entity_command_list[id]->push_back(command);
  6566. }
  6567. EntityCommand* ZoneServer::GetEntityCommand(int32 id, string name) {
  6568. EntityCommand* ret = 0;
  6569. if (entity_command_list.count(id) == 0)
  6570. return ret;
  6571. vector<EntityCommand*>::iterator itr;
  6572. for (itr = entity_command_list[id]->begin(); itr != entity_command_list[id]->end(); itr++) {
  6573. if ((*itr)->name == name) {
  6574. ret = (*itr);
  6575. break;
  6576. }
  6577. }
  6578. return ret;
  6579. }
  6580. void ZoneServer::ClearEntityCommands() {
  6581. if (entity_command_list.size() > 0) {
  6582. map<int32, vector<EntityCommand*>* >::iterator itr;
  6583. for (itr = entity_command_list.begin(); itr != entity_command_list.end(); itr++) {
  6584. vector<EntityCommand*>* entity_commands = itr->second;
  6585. if (entity_commands && entity_commands->size() > 0) {
  6586. vector<EntityCommand*>::iterator v_itr;
  6587. for (v_itr = entity_commands->begin(); v_itr != entity_commands->end(); v_itr++)
  6588. safe_delete(*v_itr);
  6589. entity_commands->clear();
  6590. }
  6591. safe_delete(entity_commands);
  6592. }
  6593. entity_command_list.clear();
  6594. }
  6595. }
  6596. void ZoneServer::AddNPCSkill(int32 list_id, int32 skill_id, int16 value){
  6597. npc_skill_list[list_id][skill_id] = value;
  6598. }
  6599. map<string, Skill*>* ZoneServer::GetNPCSkills(int32 primary_list, int32 secondary_list){
  6600. map<string, Skill*>* ret = 0;
  6601. if(npc_skill_list.count(primary_list) > 0){
  6602. ret = new map<string, Skill*>();
  6603. map<int32, int16>::iterator itr;
  6604. Skill* tmpSkill = 0;
  6605. for(itr = npc_skill_list[primary_list].begin(); itr != npc_skill_list[primary_list].end(); itr++){
  6606. tmpSkill = master_skill_list.GetSkill(itr->first);
  6607. if(tmpSkill){
  6608. tmpSkill = new Skill(tmpSkill);
  6609. tmpSkill->current_val = itr->second;
  6610. tmpSkill->max_val = tmpSkill->current_val+5;
  6611. (*ret)[tmpSkill->name.data] = tmpSkill;
  6612. }
  6613. }
  6614. }
  6615. if(npc_skill_list.count(secondary_list) > 0){
  6616. if(!ret)
  6617. ret = new map<string, Skill*>();
  6618. map<int32, int16>::iterator itr;
  6619. Skill* tmpSkill = 0;
  6620. for(itr = npc_skill_list[secondary_list].begin(); itr != npc_skill_list[secondary_list].end(); itr++){
  6621. tmpSkill = master_skill_list.GetSkill(itr->first);
  6622. if(tmpSkill){
  6623. tmpSkill = new Skill(tmpSkill);
  6624. tmpSkill->current_val = itr->second;
  6625. tmpSkill->max_val = tmpSkill->current_val+5;
  6626. (*ret)[tmpSkill->name.data] = tmpSkill;
  6627. }
  6628. }
  6629. }
  6630. if(ret && ret->size() == 0){
  6631. safe_delete(ret);
  6632. ret = 0;
  6633. }
  6634. return ret;
  6635. }
  6636. void ZoneServer::AddNPCEquipment(int32 list_id, int32 item_id){
  6637. npc_equipment_list[list_id].push_back(item_id);
  6638. }
  6639. void ZoneServer::SetNPCEquipment(NPC* npc) {
  6640. if(npc_equipment_list.count(npc->GetEquipmentListID()) > 0){
  6641. Item* tmpItem = 0;
  6642. int8 slot = 0;
  6643. vector<int32>::iterator itr;
  6644. for(itr = npc_equipment_list[npc->GetEquipmentListID()].begin(); itr != npc_equipment_list[npc->GetEquipmentListID()].end(); itr++){
  6645. tmpItem = master_item_list.GetItem(*itr);
  6646. if(tmpItem){
  6647. slot = npc->GetEquipmentList()->GetFreeSlot(tmpItem);
  6648. if(slot < 255){
  6649. tmpItem = new Item(tmpItem);
  6650. npc->GetEquipmentList()->SetItem(slot, tmpItem);
  6651. }
  6652. }
  6653. }
  6654. }
  6655. }
  6656. void ZoneServer::AddNPC(int32 id, NPC* npc) {
  6657. npc_list[id] = npc;
  6658. }
  6659. void ZoneServer::AddWidget(int32 id, Widget* widget) {
  6660. widget_list[id] = widget;
  6661. }
  6662. Widget* ZoneServer::GetWidget(int32 id, bool override_loading) {
  6663. if((!reloading || override_loading) && widget_list.count(id) > 0)
  6664. return widget_list[id];
  6665. else
  6666. return 0;
  6667. }
  6668. Widget* ZoneServer::GetNewWidget(int32 id) {
  6669. if(!reloading && widget_list.count(id) > 0)
  6670. return widget_list[id]->Copy();
  6671. else
  6672. return 0;
  6673. }
  6674. void ZoneServer::LoadGroundSpawnEntries(){
  6675. MGroundSpawnItems.lock();
  6676. database.LoadGroundSpawnEntries(this);
  6677. MGroundSpawnItems.unlock();
  6678. }
  6679. void ZoneServer::LoadGroundSpawnItems() {
  6680. }
  6681. 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) {
  6682. GroundSpawnEntry* entry = new GroundSpawnEntry;
  6683. entry->min_skill_level = min_skill_level;
  6684. entry->min_adventure_level = min_adventure_level;
  6685. entry->bonus_table = bonus_table;
  6686. entry->harvest1 = harvest1;
  6687. entry->harvest3 = harvest3;
  6688. entry->harvest5 = harvest5;
  6689. entry->harvest_imbue = harvest_imbue;
  6690. entry->harvest_rare = harvest_rare;
  6691. entry->harvest10 = harvest10;
  6692. entry->harvest_coin = harvest_coin;
  6693. groundspawn_entries[groundspawn_id].push_back(entry);
  6694. }
  6695. void ZoneServer::AddGroundSpawnItem(int32 groundspawn_id, int32 item_id, int8 is_rare, int32 grid_id) {
  6696. GroundSpawnEntryItem* entry = new GroundSpawnEntryItem;
  6697. entry->item_id = item_id;
  6698. entry->is_rare = is_rare;
  6699. entry->grid_id = grid_id;
  6700. groundspawn_items[groundspawn_id].push_back(entry);
  6701. }
  6702. vector<GroundSpawnEntry*>* ZoneServer::GetGroundSpawnEntries(int32 id){
  6703. vector<GroundSpawnEntry*>* ret = 0;
  6704. MGroundSpawnItems.lock();
  6705. if(groundspawn_entries.count(id) > 0)
  6706. ret = &groundspawn_entries[id];
  6707. MGroundSpawnItems.unlock();
  6708. return ret;
  6709. }
  6710. vector<GroundSpawnEntryItem*>* ZoneServer::GetGroundSpawnEntryItems(int32 id){
  6711. vector<GroundSpawnEntryItem*>* ret = 0;
  6712. if(groundspawn_items.count(id) > 0)
  6713. ret = &groundspawn_items[id];
  6714. return ret;
  6715. }
  6716. // TODO - mis-named, should be DeleteGroundSpawnEntries() but this is ok for now :)
  6717. void ZoneServer::DeleteGroundSpawnItems()
  6718. {
  6719. MGroundSpawnItems.lock();
  6720. map<int32, vector<GroundSpawnEntry*> >::iterator groundspawnentry_map_itr;
  6721. vector<GroundSpawnEntry*>::iterator groundspawnentry_itr;
  6722. for(groundspawnentry_map_itr = groundspawn_entries.begin(); groundspawnentry_map_itr != groundspawn_entries.end(); groundspawnentry_map_itr++)
  6723. {
  6724. for(groundspawnentry_itr = groundspawnentry_map_itr->second.begin(); groundspawnentry_itr != groundspawnentry_map_itr->second.end(); groundspawnentry_itr++)
  6725. {
  6726. safe_delete(*groundspawnentry_itr);
  6727. }
  6728. }
  6729. groundspawn_entries.clear();
  6730. map<int32, vector<GroundSpawnEntryItem*> >::iterator groundspawnitem_map_itr;
  6731. vector<GroundSpawnEntryItem*>::iterator groundspawnitem_itr;
  6732. for(groundspawnitem_map_itr = groundspawn_items.begin(); groundspawnitem_map_itr != groundspawn_items.end(); groundspawnitem_map_itr++)
  6733. {
  6734. for(groundspawnitem_itr = groundspawnitem_map_itr->second.begin(); groundspawnitem_itr != groundspawnitem_map_itr->second.end(); groundspawnitem_itr++)
  6735. {
  6736. safe_delete(*groundspawnitem_itr);
  6737. }
  6738. }
  6739. groundspawn_items.clear();
  6740. MGroundSpawnItems.unlock();
  6741. }
  6742. void ZoneServer::AddGroundSpawn(int32 id, GroundSpawn* spawn) {
  6743. groundspawn_list[id] = spawn;
  6744. }
  6745. GroundSpawn* ZoneServer::GetGroundSpawn(int32 id, bool override_loading) {
  6746. if((!reloading || override_loading) && groundspawn_list.count(id) > 0)
  6747. return groundspawn_list[id];
  6748. else
  6749. return 0;
  6750. }
  6751. GroundSpawn* ZoneServer::GetNewGroundSpawn(int32 id) {
  6752. if(!reloading && groundspawn_list.count(id) > 0)
  6753. return groundspawn_list[id]->Copy();
  6754. else
  6755. return 0;
  6756. }
  6757. void ZoneServer::AddLootTable(int32 id, LootTable* table){
  6758. loot_tables[id] = table;
  6759. }
  6760. void ZoneServer::AddLootDrop(int32 id, LootDrop* drop){
  6761. loot_drops[id].push_back(drop);
  6762. }
  6763. void ZoneServer::AddSpawnLootList(int32 spawn_id, int32 id) {
  6764. spawn_loot_list[spawn_id].push_back(id);
  6765. }
  6766. void ZoneServer::ClearSpawnLootList(int32 spawn_id) {
  6767. spawn_loot_list[spawn_id].clear();
  6768. }
  6769. void ZoneServer::AddLevelLootList(GlobalLoot* loot) {
  6770. level_loot_list.push_back(loot);
  6771. }
  6772. void ZoneServer::AddRacialLootList(int16 racial_id, GlobalLoot* loot) {
  6773. racial_loot_list[racial_id].push_back(loot);
  6774. }
  6775. void ZoneServer::AddZoneLootList(int32 zone, GlobalLoot* loot) {
  6776. zone_loot_list[zone].push_back(loot);
  6777. }
  6778. void ZoneServer::ClearLootTables(){
  6779. map<int32,LootTable*>::iterator table_itr;
  6780. for(table_itr = loot_tables.begin(); table_itr != loot_tables.end(); table_itr++){
  6781. safe_delete(table_itr->second);
  6782. }
  6783. map<int32, vector<LootDrop*> >::iterator drop_itr;
  6784. vector<LootDrop*>::iterator drop_itr2;
  6785. for(drop_itr = loot_drops.begin(); drop_itr != loot_drops.end(); drop_itr++){
  6786. for(drop_itr2 = drop_itr->second.begin(); drop_itr2 != drop_itr->second.end(); drop_itr2++){
  6787. safe_delete(*drop_itr2);
  6788. }
  6789. }
  6790. vector<GlobalLoot*>::iterator level_itr;
  6791. for (level_itr = level_loot_list.begin(); level_itr != level_loot_list.end(); level_itr++) {
  6792. safe_delete(*level_itr);
  6793. }
  6794. map<int16, vector<GlobalLoot*> >::iterator race_itr;
  6795. vector<GlobalLoot*>::iterator race_itr2;
  6796. for (race_itr = racial_loot_list.begin(); race_itr != racial_loot_list.end(); race_itr++) {
  6797. for (race_itr2 = race_itr->second.begin(); race_itr2 != race_itr->second.end(); race_itr2++) {
  6798. safe_delete(*race_itr2);
  6799. }
  6800. }
  6801. map<int32, vector<GlobalLoot*> >::iterator zone_itr;
  6802. vector<GlobalLoot*>::iterator zone_itr2;
  6803. for(zone_itr = zone_loot_list.begin(); zone_itr != zone_loot_list.end(); zone_itr++) {
  6804. for (zone_itr2 = zone_itr->second.begin(); zone_itr2 != zone_itr->second.end(); zone_itr2++) {
  6805. safe_delete(*zone_itr2);
  6806. }
  6807. }
  6808. loot_tables.clear();
  6809. loot_drops.clear();
  6810. spawn_loot_list.clear();
  6811. level_loot_list.clear();
  6812. racial_loot_list.clear();
  6813. zone_loot_list.clear();
  6814. }
  6815. vector<int32> ZoneServer::GetSpawnLootList(int32 spawn_id, int32 zone_id, int8 spawn_level, int16 racial_id, Spawn* spawn) {
  6816. vector<int32> ret;
  6817. int32 returnValue = 0;
  6818. if(reloading)
  6819. return ret;
  6820. if (spawn_loot_list.count(spawn_id) > 0)
  6821. ret.insert(ret.end(), spawn_loot_list[spawn_id].begin(), spawn_loot_list[spawn_id].end());
  6822. if (level_loot_list.size() > 0) {
  6823. vector<GlobalLoot*>::iterator itr;
  6824. for (itr = level_loot_list.begin(); itr != level_loot_list.end(); itr++) {
  6825. GlobalLoot* loot = *itr;
  6826. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6827. returnValue = 0; // reset since this can override the database setting
  6828. if(zone_script)
  6829. {
  6830. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_level", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6831. continue;
  6832. }
  6833. bool entryAdded = false;
  6834. 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
  6835. ret.push_back(loot->table_id);
  6836. else {
  6837. 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
  6838. ret.push_back(loot->table_id);
  6839. }
  6840. if(!entryAdded && returnValue) // DB override via LUA scripting
  6841. ret.push_back(loot->table_id);
  6842. }
  6843. }
  6844. if (racial_loot_list.count(racial_id) > 0) {
  6845. vector<GlobalLoot*>::iterator itr;
  6846. for (itr = racial_loot_list[racial_id].begin(); itr != racial_loot_list[racial_id].end(); itr++) {
  6847. GlobalLoot* loot = *itr;
  6848. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6849. returnValue = 0; // reset since this can override the database setting
  6850. if(zone_script)
  6851. {
  6852. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_racial", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6853. continue;
  6854. }
  6855. bool entryAdded = false;
  6856. 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
  6857. ret.push_back(loot->table_id);
  6858. else {
  6859. 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
  6860. ret.push_back(loot->table_id);
  6861. }
  6862. if(!entryAdded && returnValue) // DB override via LUA scripting
  6863. ret.push_back(loot->table_id);
  6864. }
  6865. }
  6866. if (zone_loot_list.count(zone_id) > 0) {
  6867. vector<GlobalLoot*>::iterator itr;
  6868. for (itr = zone_loot_list[zone_id].begin(); itr != zone_loot_list[zone_id].end(); itr++) {
  6869. GlobalLoot* loot = *itr;
  6870. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  6871. returnValue = 0; // reset since this can override the database setting
  6872. if(zone_script)
  6873. {
  6874. if(lua_interface->RunZoneScriptWithReturn(zone_script, "loot_criteria_zone", spawn->GetZone(), spawn, loot->table_id, loot->minLevel, loot->maxLevel, &returnValue) && returnValue == 0)
  6875. continue;
  6876. }
  6877. bool entryAdded = false;
  6878. 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
  6879. ret.push_back(loot->table_id);
  6880. else {
  6881. 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
  6882. ret.push_back(loot->table_id);
  6883. }
  6884. if(!entryAdded && returnValue) // DB override via LUA scripting
  6885. ret.push_back(loot->table_id);
  6886. }
  6887. }
  6888. return ret;
  6889. }
  6890. vector<LootDrop*>* ZoneServer::GetLootDrops(int32 table_id){
  6891. if(!reloading && loot_drops.count(table_id) > 0)
  6892. return &(loot_drops[table_id]);
  6893. else
  6894. return 0;
  6895. }
  6896. LootTable* ZoneServer::GetLootTable(int32 table_id){
  6897. return loot_tables[table_id];
  6898. }
  6899. 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){
  6900. LocationTransportDestination* loc = new LocationTransportDestination;
  6901. loc->message = message;
  6902. loc->trigger_x = trigger_x;
  6903. loc->trigger_y = trigger_y;
  6904. loc->trigger_z = trigger_z;
  6905. loc->trigger_radius = trigger_radius;
  6906. loc->destination_zone_id = destination_zone_id;
  6907. loc->destination_x = destination_x;
  6908. loc->destination_y = destination_y;
  6909. loc->destination_z = destination_z;
  6910. loc->destination_heading = destination_heading;
  6911. loc->cost = cost;
  6912. loc->unique_id = unique_id;
  6913. MTransporters.lock();
  6914. if(location_transporters.count(zone_id) == 0)
  6915. location_transporters[zone_id] = new MutexList<LocationTransportDestination*>();
  6916. location_transporters[zone_id]->Add(loc);
  6917. MTransporters.unlock();
  6918. }
  6919. 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,
  6920. 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,
  6921. int32 max_client_version, int32 flight_path_id, int16 mount_id, int8 mount_red_color, int8 mount_green_color, int8 mount_blue_color){
  6922. TransportDestination* transport = new TransportDestination;
  6923. transport->type = type;
  6924. transport->display_name = name;
  6925. transport->message = message;
  6926. transport->destination_zone_id = destination_zone_id;
  6927. transport->destination_x = destination_x;
  6928. transport->destination_y = destination_y;
  6929. transport->destination_z = destination_z;
  6930. transport->destination_heading = destination_heading;
  6931. transport->cost = cost;
  6932. transport->unique_id = unique_id;
  6933. transport->min_level = min_level;
  6934. transport->max_level = max_level;
  6935. transport->req_quest = quest_req;
  6936. transport->req_quest_step = quest_step_req;
  6937. transport->req_quest_complete = quest_complete;
  6938. transport->map_x = map_x;
  6939. transport->map_y = map_y;
  6940. transport->expansion_flag = expansion_flag;
  6941. transport->holiday_flag = holiday_flag;
  6942. transport->min_client_version = min_client_version;
  6943. transport->max_client_version = max_client_version;
  6944. transport->flight_path_id = flight_path_id;
  6945. transport->mount_id = mount_id;
  6946. transport->mount_red_color = mount_red_color;
  6947. transport->mount_green_color = mount_green_color;
  6948. transport->mount_blue_color = mount_blue_color;
  6949. MTransporters.lock();
  6950. transporters[transport_id].push_back(transport);
  6951. MTransporters.unlock();
  6952. }
  6953. void ZoneServer::GetTransporters(vector<TransportDestination*>* returnList, Client* client, int32 transport_id){
  6954. if (!returnList)
  6955. return;
  6956. MTransporters.lock();
  6957. if (transporters.count(transport_id) > 0)
  6958. {
  6959. vector<TransportDestination*> list;
  6960. for (int i = 0; i < transporters[transport_id].size(); i++)
  6961. {
  6962. if (transporters[transport_id][i]->min_client_version && client->GetVersion() < transporters[transport_id][i]->min_client_version)
  6963. continue;
  6964. else if (transporters[transport_id][i]->max_client_version && client->GetVersion() > transporters[transport_id][i]->max_client_version)
  6965. continue;
  6966. if (database.CheckExpansionFlags(this, transporters[transport_id][i]->expansion_flag) && database.CheckHolidayFlags(this, transporters[transport_id][i]->holiday_flag))
  6967. {
  6968. returnList->push_back(transporters[transport_id][i]);
  6969. }
  6970. }
  6971. }
  6972. MTransporters.unlock();
  6973. }
  6974. MutexList<LocationTransportDestination*>* ZoneServer::GetLocationTransporters(int32 zone_id){
  6975. MutexList<LocationTransportDestination*>* ret = 0;
  6976. MTransporters.lock();
  6977. if(location_transporters.count(zone_id) > 0)
  6978. ret = location_transporters[zone_id];
  6979. MTransporters.unlock();
  6980. return ret;
  6981. }
  6982. void ZoneServer::DeleteGlobalTransporters(){
  6983. MTransporters.lock();
  6984. map<int32, vector<TransportDestination*> >::iterator itr;
  6985. vector<TransportDestination*>::iterator transport_vector_itr;
  6986. for(itr = transporters.begin(); itr != transporters.end(); itr++){
  6987. for(transport_vector_itr = itr->second.begin(); transport_vector_itr != itr->second.end(); transport_vector_itr++){
  6988. safe_delete(*transport_vector_itr);
  6989. }
  6990. }
  6991. map<int32, MutexList<LocationTransportDestination*>* >::iterator itr2;
  6992. for(itr2 = location_transporters.begin(); itr2 != location_transporters.end(); itr2++){
  6993. itr2->second->clear(true);
  6994. delete itr2->second;
  6995. }
  6996. transporters.clear();
  6997. location_transporters.clear();
  6998. MTransporters.unlock();
  6999. }
  7000. void ZoneServer::DeleteGlobalSpawns() {
  7001. ClearLootTables();
  7002. map<int32, NPC*>::iterator npc_list_iter;
  7003. for(npc_list_iter=npc_list.begin();npc_list_iter!=npc_list.end();npc_list_iter++) {
  7004. safe_delete(npc_list_iter->second);
  7005. }
  7006. npc_list.clear();
  7007. map<int32, Object*>::iterator object_list_iter;
  7008. for(object_list_iter=object_list.begin();object_list_iter!=object_list.end();object_list_iter++) {
  7009. safe_delete(object_list_iter->second);
  7010. }
  7011. object_list.clear();
  7012. map<int32, GroundSpawn*>::iterator groundspawn_list_iter;
  7013. for(groundspawn_list_iter=groundspawn_list.begin();groundspawn_list_iter!=groundspawn_list.end();groundspawn_list_iter++) {
  7014. safe_delete(groundspawn_list_iter->second);
  7015. }
  7016. groundspawn_list.clear();
  7017. map<int32, Widget*>::iterator widget_list_iter;
  7018. for(widget_list_iter=widget_list.begin();widget_list_iter!=widget_list.end();widget_list_iter++) {
  7019. safe_delete(widget_list_iter->second);
  7020. }
  7021. widget_list.clear();
  7022. map<int32, Sign*>::iterator sign_list_iter;
  7023. for(sign_list_iter=sign_list.begin();sign_list_iter!=sign_list.end();sign_list_iter++) {
  7024. safe_delete(sign_list_iter->second);
  7025. }
  7026. sign_list.clear();
  7027. /*map<int32, AppearanceData*>::iterator appearance_list_iter;
  7028. for(appearance_list_iter=npc_appearance_list.begin();appearance_list_iter!=npc_appearance_list.end();appearance_list_iter++) {
  7029. safe_delete(appearance_list_iter->second);
  7030. }
  7031. npc_appearance_list.clear();*/
  7032. ClearEntityCommands();
  7033. DeleteGroundSpawnItems();
  7034. DeleteGlobalTransporters();
  7035. DeleteTransporterMaps();
  7036. }
  7037. void ZoneServer::AddTransportMap(int32 id, string name) {
  7038. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  7039. m_transportMaps[id] = name;
  7040. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  7041. }
  7042. bool ZoneServer::TransportHasMap(int32 id) {
  7043. bool ret = false;
  7044. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  7045. ret = m_transportMaps.count(id) > 0;
  7046. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  7047. return ret;
  7048. }
  7049. string ZoneServer::GetTransportMap(int32 id) {
  7050. string ret;
  7051. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  7052. if (m_transportMaps.count(id) > 0)
  7053. ret = m_transportMaps[id];
  7054. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  7055. return ret;
  7056. }
  7057. void ZoneServer::DeleteTransporterMaps() {
  7058. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  7059. m_transportMaps.clear();
  7060. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  7061. }
  7062. void ZoneServer::ReloadSpawns() {
  7063. if (reloading)
  7064. return;
  7065. reloading = true;
  7066. world.SetReloadingSubsystem("Spawns");
  7067. // Let every one in the zone know what is happening
  7068. HandleBroadcast("Reloading all spawns for this zone.");
  7069. DeleteGlobalSpawns();
  7070. Depop(false, true);
  7071. }
  7072. void ZoneServer::SendStateCommand(Spawn* spawn, int32 state) {
  7073. vector<Client*>::iterator itr;
  7074. MClientList.readlock(__FUNCTION__, __LINE__);
  7075. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7076. Client* client = *itr;
  7077. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7078. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), state);
  7079. }
  7080. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7081. }
  7082. void ZoneServer::AddFlightPath(int32 id, FlightPathInfo* info) {
  7083. if (m_flightPaths.count(id) > 0) {
  7084. LogWrite(ZONE__ERROR, 0, "Zone", "Duplicate flight path (%u)", id);
  7085. safe_delete(info);
  7086. return;
  7087. }
  7088. m_flightPaths[id] = info;
  7089. }
  7090. void ZoneServer::AddFlightPathLocation(int32 id, FlightPathLocation* location) {
  7091. if (m_flightPaths.count(id) == 0) {
  7092. LogWrite(ZONE__ERROR, 0, "Zone", "There is no flight info for this route (%u)", id);
  7093. safe_delete(location);
  7094. return;
  7095. }
  7096. m_flightPathRoutes[id].push_back(location);
  7097. }
  7098. void ZoneServer::DeleteFlightPaths() {
  7099. map<int32, vector<FlightPathLocation*> >::iterator itr;
  7100. vector<FlightPathLocation*>::iterator itr2;
  7101. map<int32, FlightPathInfo*>::iterator itr3;
  7102. for (itr = m_flightPathRoutes.begin(); itr != m_flightPathRoutes.end(); itr++) {
  7103. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  7104. safe_delete(*itr2);
  7105. }
  7106. itr->second.clear();
  7107. }
  7108. m_flightPathRoutes.clear();
  7109. for (itr3 = m_flightPaths.begin(); itr3 != m_flightPaths.end(); itr3++) {
  7110. safe_delete(itr3->second);
  7111. }
  7112. m_flightPaths.clear();
  7113. }
  7114. void ZoneServer::SendFlightPathsPackets(Client* client) {
  7115. // Only send a packet if there are flight paths
  7116. if (m_flightPathRoutes.size() > 0) {
  7117. PacketStruct* packet = configReader.getStruct("WS_FlightPathsMsg", client->GetVersion());
  7118. if (packet) {
  7119. int32 num_routes = m_flightPaths.size();
  7120. packet->setArrayLengthByName("number_of_routes", num_routes);
  7121. packet->setArrayLengthByName("number_of_routes2", num_routes);
  7122. packet->setArrayLengthByName("number_of_routes3", num_routes);
  7123. packet->setArrayLengthByName("number_of_routes4", num_routes);
  7124. map<int32, FlightPathInfo*>::iterator itr;
  7125. int32 i = 0;
  7126. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, i++) {
  7127. packet->setArrayDataByName("route_length", m_flightPathRoutes[itr->first].size(), i);
  7128. packet->setArrayDataByName("ground_mount", itr->second->flying ? 0 : 1, i);
  7129. packet->setArrayDataByName("allow_dismount", itr->second->dismount ? 1 : 0, i);
  7130. packet->setSubArrayLengthByName("route_length2", m_flightPathRoutes[itr->first].size(), i);
  7131. vector<FlightPathLocation*>::iterator itr2;
  7132. int32 j = 0;
  7133. for (itr2 = m_flightPathRoutes[itr->first].begin(); itr2 != m_flightPathRoutes[itr->first].end(); itr2++, j++) {
  7134. packet->setSubArrayDataByName("x", (*itr2)->X, i, j);
  7135. packet->setSubArrayDataByName("y", (*itr2)->Y, i, j);
  7136. packet->setSubArrayDataByName("z", (*itr2)->Z, i, j);
  7137. }
  7138. }
  7139. client->QueuePacket(packet->serialize());
  7140. safe_delete(packet);
  7141. }
  7142. }
  7143. }
  7144. int32 ZoneServer::GetFlightPathIndex(int32 id) {
  7145. int32 index = 0;
  7146. map<int32, FlightPathInfo*>::iterator itr;
  7147. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, index++) {
  7148. if (itr->first == id)
  7149. return index;
  7150. }
  7151. return -1;
  7152. }
  7153. float ZoneServer::GetFlightPathSpeed(int32 id) {
  7154. float speed = 1;
  7155. if (m_flightPaths.count(id) > 0)
  7156. speed = m_flightPaths[id]->speed;
  7157. return speed;
  7158. }
  7159. void ZoneServer::ProcessSpawnConditional(int8 condition) {
  7160. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  7161. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7162. map<int32, Spawn*>::iterator itr;
  7163. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7164. if (itr->second != NULL) // null itr->second still coming into ProcessSpawnConditional
  7165. {
  7166. SpawnLocation* loc = spawn_location_list[itr->second->GetSpawnLocationID()];
  7167. if (loc && loc->conditional > 0) {
  7168. if ((loc->conditional & condition) != condition) {
  7169. Despawn(itr->second, 0);
  7170. }
  7171. }
  7172. }
  7173. }
  7174. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7175. map<int32, SpawnLocation*>::iterator itr2;
  7176. for (itr2 = spawn_location_list.begin(); itr2 != spawn_location_list.end(); itr2++) {
  7177. SpawnLocation* loc = itr2->second;
  7178. if (loc && loc->conditional > 0 && ((loc->conditional & condition) == condition))
  7179. if (GetSpawnByLocationID(loc->placement_id) == NULL)
  7180. ProcessSpawnLocation(loc);
  7181. }
  7182. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  7183. }
  7184. void ZoneServer::AddSpawnProximities(Spawn* newSpawn) {
  7185. Spawn* spawn = 0;
  7186. map<int32, Spawn*>::iterator itr;
  7187. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  7188. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7189. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7190. spawn = itr->second;
  7191. if (spawn && spawn != newSpawn) {
  7192. if (newSpawn->GetDatabaseID())
  7193. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7194. if (newSpawn->GetSpawnLocationID())
  7195. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7196. if (spawn->GetDatabaseID())
  7197. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7198. if (spawn->GetSpawnLocationID())
  7199. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7200. }
  7201. }
  7202. list<Spawn*>::iterator itr2;
  7203. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7204. spawn = *itr2;
  7205. if (spawn && spawn != newSpawn) {
  7206. if (newSpawn->GetDatabaseID())
  7207. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7208. if (newSpawn->GetSpawnLocationID())
  7209. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7210. if (spawn->GetDatabaseID())
  7211. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7212. if (spawn->GetSpawnLocationID())
  7213. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7214. }
  7215. }
  7216. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7217. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  7218. }
  7219. // we only call this inside a write lock
  7220. void ZoneServer::RemoveSpawnProximities(Spawn* oldSpawn) {
  7221. Spawn* spawn = 0;
  7222. map<int32, Spawn*>::iterator itr;
  7223. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7224. spawn = itr->second;
  7225. if (spawn && spawn != oldSpawn) {
  7226. if (oldSpawn->GetDatabaseID())
  7227. spawn->RemoveSpawnFromProximity(oldSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  7228. if (oldSpawn->GetSpawnLocationID())
  7229. spawn->RemoveSpawnFromProximity(oldSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  7230. // don't need to remove oldSpawn proximities, we clear them all out
  7231. }
  7232. }
  7233. }
  7234. void ZoneServer::SetSpawnScript(SpawnEntry* entry, Spawn* spawn)
  7235. {
  7236. if (!entry || !spawn)
  7237. return;
  7238. const char* script = 0;
  7239. for (int x = 0; x < 3; x++)
  7240. {
  7241. switch (x)
  7242. {
  7243. case 0:
  7244. script = world.GetSpawnEntryScript(entry->spawn_entry_id);
  7245. break;
  7246. case 1:
  7247. script = world.GetSpawnLocationScript(entry->spawn_location_id);
  7248. break;
  7249. case 2:
  7250. script = world.GetSpawnScript(entry->spawn_id);
  7251. break;
  7252. }
  7253. if (script && lua_interface && lua_interface->GetSpawnScript(script) != 0)
  7254. {
  7255. spawn->SetSpawnScript(string(script));
  7256. break;
  7257. }
  7258. }
  7259. }
  7260. vector<HouseItem> ZoneServer::GetHouseItems(Client* client)
  7261. {
  7262. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7263. return std::vector<HouseItem>();
  7264. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7265. std::vector<HouseItem> items;
  7266. map<int32, Spawn*>::iterator itr;
  7267. Spawn* spawn = 0;
  7268. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7269. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7270. spawn = itr->second;
  7271. if (spawn && spawn->IsObject() && spawn->GetPickupItemID())
  7272. {
  7273. HouseItem tmpItem;
  7274. tmpItem.item_id = spawn->GetPickupItemID();
  7275. tmpItem.unique_id = spawn->GetPickupUniqueItemID();
  7276. tmpItem.spawn_id = spawn->GetID();
  7277. tmpItem.item = master_item_list.GetItem(spawn->GetPickupItemID());
  7278. if (!tmpItem.item)
  7279. continue;
  7280. items.push_back(tmpItem);
  7281. }
  7282. }
  7283. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7284. return items;
  7285. }
  7286. void ZoneServer::SendHouseItems(Client* client)
  7287. {
  7288. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  7289. return;
  7290. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  7291. std::vector<HouseItem> items = GetHouseItems(client);
  7292. // setting this to 1 puts it on the door widget
  7293. packet->setDataByName("is_widget_door", 1);
  7294. packet->setArrayLengthByName("num_items", items.size());
  7295. for (int i = 0; i < items.size(); i++)
  7296. {
  7297. HouseItem tmpItem = items[i];
  7298. packet->setArrayDataByName("unique_id", tmpItem.unique_id, i); // unique_id is in fact the item_id...
  7299. packet->setArrayDataByName("item_name", tmpItem.item->name.c_str(), i);
  7300. packet->setArrayDataByName("status_reduction", tmpItem.item->houseitem_info->status_rent_reduction, i);
  7301. // location, 0 = floor, 1 = ceiling
  7302. //packet->setArrayDataByName("location", 1, i, 0);
  7303. // item_state int8
  7304. // 0 = normal (cannot pick up item / move item / toggle visibility)
  7305. // 1 = virtual (toggle visibility available, no move item)
  7306. // 2 = hidden (cannot pick up item / move item / toggle visibility)
  7307. // 3 = virtual/hidden/toggle visibility
  7308. // 4 = none (cannot pick up item / move item / toggle visibility)
  7309. // 5 = none, toggle visibility (cannot pick up item / move item)
  7310. // 8 = none (cannot pick up item / move item / toggle visibility)
  7311. //packet->setArrayDataByName("item_state", tmpvalue, i, 0);
  7312. // makes it so we don't have access to move item/retrieve item
  7313. // cannot use in conjunction with ui_tab_flag1/ui_tab_flag2
  7314. //packet->setArrayDataByName("tradeable", 1, i);
  7315. //packet->setArrayDataByName("item_description", "failboat", i);
  7316. // access to move item/retrieve item, do not use in conjunction with tradeable
  7317. packet->setArrayDataByName("ui_tab_flag1", 1, i, 0);
  7318. packet->setArrayDataByName("ui_tab_flag2", 1, i, 0);
  7319. // both of these can serve as description fields (only one should be used they populate the same area below the item name)
  7320. //packet->setArrayDataByName("first_item_description", "test", i);
  7321. //packet->setArrayDataByName("second_item_description", "Description here!", i);
  7322. packet->setArrayDataByName("icon", tmpItem.item->details.icon, i);
  7323. }
  7324. EQ2Packet* pack = packet->serialize();
  7325. client->QueuePacket(pack);
  7326. safe_delete(packet);
  7327. }
  7328. Spawn* ZoneServer::GetSpawnFromUniqueItemID(int32 unique_id)
  7329. {
  7330. if (!GetInstanceID() || GetInstanceType() != Instance_Type::PERSONAL_HOUSE_INSTANCE)
  7331. return nullptr;
  7332. map<int32, Spawn*>::iterator itr;
  7333. Spawn* spawn = 0;
  7334. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7335. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  7336. spawn = itr->second;
  7337. if (spawn && spawn->IsObject() && spawn->GetPickupUniqueItemID() == unique_id)
  7338. {
  7339. Spawn* tmpSpawn = spawn;
  7340. MSpawnList.releasereadlock();
  7341. return tmpSpawn;
  7342. }
  7343. }
  7344. MSpawnList.releasereadlock();
  7345. return nullptr;
  7346. }
  7347. void ZoneServer::AddPendingSpawnRemove(int32 id)
  7348. {
  7349. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7350. m_pendingSpawnRemove.insert(make_pair(id,true));
  7351. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7352. }
  7353. void ZoneServer::ProcessSpawnRemovals()
  7354. {
  7355. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7356. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  7357. if (m_pendingSpawnRemove.size() > 0) {
  7358. map<int32,bool>::iterator itr2;
  7359. for (itr2 = m_pendingSpawnRemove.begin(); itr2 != m_pendingSpawnRemove.end(); itr2++) {
  7360. spawn_list.erase(itr2->first);
  7361. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].erase(itr2->first);
  7362. std::map<int32,int32>::iterator hsmitr = housing_spawn_map.find(itr2->first);
  7363. if(hsmitr != housing_spawn_map.end()) {
  7364. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].erase(hsmitr->second);
  7365. housing_spawn_map.erase(hsmitr);
  7366. }
  7367. }
  7368. m_pendingSpawnRemove.clear();
  7369. }
  7370. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  7371. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7372. }
  7373. void ZoneServer::AddSpawnToGroup(Spawn* spawn, int32 group_id)
  7374. {
  7375. if( spawn->GetSpawnGroupID() > 0 )
  7376. spawn->RemoveSpawnFromGroup();
  7377. MutexList<int32>* groupList = &spawn_group_map.Get(group_id);
  7378. MutexList<int32>::iterator itr2 = groupList->begin();
  7379. while(itr2.Next())
  7380. {
  7381. Spawn* groupSpawn = GetSpawnByID(itr2.value);
  7382. if(groupSpawn)
  7383. {
  7384. // found existing group member to add it in
  7385. spawn->AddSpawnToGroup(groupSpawn);
  7386. break;
  7387. }
  7388. }
  7389. groupList->Add(spawn->GetID());
  7390. spawn->SetSpawnGroupID(group_id);
  7391. }
  7392. void ZoneServer::QueueStateCommandToClients(int32 spawn_id, int32 state)
  7393. {
  7394. if(spawn_id < 1)
  7395. return;
  7396. MLuaQueueStateCmd.lock();
  7397. lua_queued_state_commands.insert(make_pair(spawn_id, state));
  7398. MLuaQueueStateCmd.unlock();
  7399. }
  7400. void ZoneServer::QueueDefaultCommand(int32 spawn_id, std::string command, float distance)
  7401. {
  7402. if(spawn_id < 1)
  7403. return;
  7404. MLuaQueueStateCmd.lock();
  7405. lua_spawn_update_command[spawn_id].insert(make_pair(command,distance));
  7406. MLuaQueueStateCmd.unlock();
  7407. }
  7408. void ZoneServer::ProcessQueuedStateCommands() // in a client list lock only
  7409. {
  7410. vector<Client*>::iterator itr;
  7411. MLuaQueueStateCmd.lock();
  7412. if(lua_queued_state_commands.size() > 0)
  7413. {
  7414. std::map<int32, int32>::iterator statecmds;
  7415. for(statecmds = lua_queued_state_commands.begin(); statecmds != lua_queued_state_commands.end(); statecmds++)
  7416. {
  7417. Spawn* spawn = GetSpawnByID(statecmds->first, false);
  7418. if(!spawn)
  7419. continue;
  7420. MClientList.readlock(__FUNCTION__, __LINE__);
  7421. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7422. Client* client = *itr;
  7423. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7424. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), statecmds->second);
  7425. }
  7426. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7427. }
  7428. lua_queued_state_commands.clear();
  7429. }
  7430. if(lua_spawn_update_command.size() > 0)
  7431. {
  7432. std::map<int32, std::map<std::string,float>>::iterator updatecmds;
  7433. for(updatecmds = lua_spawn_update_command.begin(); updatecmds != lua_spawn_update_command.end(); updatecmds++)
  7434. {
  7435. Spawn* spawn = GetSpawnByID(updatecmds->first, false);
  7436. if(!spawn)
  7437. continue;
  7438. std::map<std::string,float>::iterator innermap;
  7439. for(innermap = lua_spawn_update_command[updatecmds->first].begin(); innermap != lua_spawn_update_command[updatecmds->first].end(); innermap++)
  7440. {
  7441. MClientList.readlock(__FUNCTION__, __LINE__);
  7442. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7443. Client* client = *itr;
  7444. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()))
  7445. client->SendDefaultCommand(spawn, innermap->first.c_str(), innermap->second);
  7446. }
  7447. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7448. }
  7449. lua_spawn_update_command[updatecmds->first].clear();
  7450. }
  7451. lua_spawn_update_command.clear();
  7452. }
  7453. MLuaQueueStateCmd.unlock();
  7454. }
  7455. void ZoneServer::UpdateClientSpawnMap(Player* player, Client* client)
  7456. {
  7457. // client may be null when passed
  7458. client_spawn_map.Put(player, client);
  7459. }
  7460. void ZoneServer::RemoveClientsFromZone(ZoneServer* zone) {
  7461. vector<Client*>::iterator itr;
  7462. MClientList.readlock(__FUNCTION__, __LINE__);
  7463. for (itr = clients.begin(); itr != clients.end(); itr++) {
  7464. Client* client = *itr;
  7465. if(client->GetCurrentZone() == zone) {
  7466. client->SetCurrentZone(nullptr);
  7467. }
  7468. if(client->GetZoningDestination() == zone) {
  7469. client->SetZoningDestination(nullptr);
  7470. }
  7471. }
  7472. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  7473. }
  7474. void ZoneServer::SendSubSpawnUpdates(SUBSPAWN_TYPES subtype) {
  7475. std::map<int32, Spawn*>::iterator subitr;
  7476. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7477. for(subitr = subspawn_list[subtype].begin(); subitr != subspawn_list[subtype].end(); subitr++) {
  7478. subitr->second->changed = true;
  7479. subitr->second->info_changed = true;
  7480. AddChangedSpawn(subitr->second);
  7481. }
  7482. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7483. }
  7484. bool ZoneServer::HouseItemSpawnExists(int32 item_id) {
  7485. bool exists = false;
  7486. std::map<int32, Spawn*>::iterator subitr;
  7487. MSpawnList.readlock(__FUNCTION__, __LINE__);
  7488. subitr = subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].find(item_id);
  7489. if(subitr != subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].end()) {
  7490. exists = true;
  7491. }
  7492. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  7493. return exists;
  7494. }
  7495. void ZoneServer::ProcessPendingSpawns() {
  7496. MPendingSpawnListAdd.writelock(__FUNCTION__, __LINE__);
  7497. list<Spawn*>::iterator itr2;
  7498. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  7499. Spawn* spawn = *itr2;
  7500. MSpawnList.writelock(__FUNCTION__, __LINE__);
  7501. if (spawn)
  7502. spawn_list[spawn->GetID()] = spawn;
  7503. if(spawn->IsCollector()) {
  7504. subspawn_list[SUBSPAWN_TYPES::COLLECTOR].insert(make_pair(spawn->GetID(),spawn));
  7505. }
  7506. if(spawn->GetPickupItemID()) {
  7507. subspawn_list[SUBSPAWN_TYPES::HOUSE_ITEM_SPAWN].insert(make_pair(spawn->GetPickupItemID(),spawn));
  7508. housing_spawn_map.insert(make_pair(spawn->GetID(), spawn->GetPickupItemID()));
  7509. }
  7510. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  7511. CheckSpawnRange(spawn);
  7512. }
  7513. pending_spawn_list_add.clear();
  7514. MPendingSpawnListAdd.releasewritelock(__FUNCTION__, __LINE__);
  7515. spawn_check_add.Trigger();
  7516. }
  7517. void ZoneServer::AddSpawnToGrid(Spawn* spawn, int32 grid_id) {
  7518. if(spawn->GetID() == 0 || spawn->IsDeletedSpawn())
  7519. return;
  7520. MGridMaps.lock_shared();
  7521. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7522. if(grids != grid_maps.end()) {
  7523. grids->second->MSpawns.lock();
  7524. grids->second->spawns.insert(make_pair(spawn->GetID(), spawn));
  7525. grids->second->MSpawns.unlock();
  7526. }
  7527. else {
  7528. MGridMaps.unlock_shared();
  7529. MGridMaps.lock();
  7530. GridMap* gm = new GridMap;
  7531. gm->grid_id = grid_id;
  7532. gm->spawns.insert(make_pair(spawn->GetID(), spawn));
  7533. grid_maps.insert(make_pair(grid_id, gm));
  7534. MGridMaps.unlock();
  7535. return;
  7536. }
  7537. MGridMaps.unlock_shared();
  7538. }
  7539. void ZoneServer::RemoveSpawnFromGrid(Spawn* spawn, int32 grid_id) {
  7540. std::shared_lock lock(MGridMaps);
  7541. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7542. if(grids != grid_maps.end()) {
  7543. grids->second->MSpawns.lock();
  7544. if(grids->second->spawns.count(spawn->GetID()) > 0) {
  7545. grids->second->spawns.erase(spawn->GetID());
  7546. }
  7547. grids->second->MSpawns.unlock();
  7548. }
  7549. }
  7550. int32 ZoneServer::GetSpawnCountInGrid(int32 grid_id) {
  7551. int32 count = 0;
  7552. std::shared_lock lock(MGridMaps);
  7553. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7554. if(grids != grid_maps.end()) {
  7555. grids->second->MSpawns.lock_shared();
  7556. count = grids->second->spawns.size();
  7557. grids->second->MSpawns.unlock_shared();
  7558. }
  7559. return count;
  7560. }
  7561. void ZoneServer::SendClientSpawnListInGrid(Client* client, int32 grid_id){
  7562. std::shared_lock lock(MGridMaps);
  7563. Spawn* spawn = nullptr;
  7564. client->Message(CHANNEL_COLOR_RED, "Grid ID %u has %u spawns.", grid_id, GetSpawnCountInGrid(grid_id));
  7565. std::map<int32, GridMap*>::iterator grids = grid_maps.find(grid_id);
  7566. if(grids != grid_maps.end()) {
  7567. grids->second->MSpawns.lock_shared();
  7568. typedef map <int32, Spawn*> SpawnMapType;
  7569. for( SpawnMapType::iterator it = grids->second->spawns.begin(); it != grids->second->spawns.end(); ++it ) {
  7570. spawn = it->second;
  7571. 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());
  7572. }
  7573. grids->second->MSpawns.unlock_shared();
  7574. }
  7575. }
  7576. void ZoneServer::AddIgnoredWidget(int32 id) {
  7577. std::unique_lock lock(MIgnoredWidgets);
  7578. if(ignored_widgets.find(id) == ignored_widgets.end()) {
  7579. ignored_widgets.insert(make_pair(id,true));
  7580. }
  7581. }