zoneserver.cpp 258 KB

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