zoneserver.cpp 268 KB

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