zoneserver.cpp 240 KB

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