zoneserver.cpp 246 KB

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