9
3

zoneserver.cpp 268 KB

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