zoneserver.cpp 280 KB

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