zoneserver.cpp 241 KB

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