zoneserver.cpp 267 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781
  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. result = 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. client->GetCurrentZone()->SendAllSpawnsForVisChange(client);
  3422. }
  3423. }
  3424. void ZoneServer::SendAllSpawnsForLevelChange(Client* client) {
  3425. Spawn* spawn = 0;
  3426. if (spawn_range_map.count(client) > 0) {
  3427. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3428. while (itr.Next()) {
  3429. spawn = GetSpawnByID(itr->first);
  3430. if (spawn && client->GetPlayer()->WasSentSpawn(spawn->GetID()) && !client->GetPlayer()->WasSpawnRemoved(spawn)) {
  3431. SendSpawnChanges(spawn, client, false, true);
  3432. // Attempt to slow down the packet spam sent to the client
  3433. Sleep(5);
  3434. }
  3435. }
  3436. }
  3437. }
  3438. void ZoneServer::SendAllSpawnsForSeeInvisChange(Client* client) {
  3439. Spawn* spawn = 0;
  3440. if (spawn_range_map.count(client) > 0) {
  3441. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3442. while (itr.Next()) {
  3443. spawn = GetSpawnByID(itr->first);
  3444. if (spawn && spawn->IsEntity() && (((Entity*)spawn)->IsInvis() || ((Entity*)spawn)->IsStealthed()) && client->GetPlayer()->WasSentSpawn(spawn->GetID()) && !client->GetPlayer()->WasSpawnRemoved(spawn)) {
  3445. SendSpawnChanges(spawn, client, true, true);
  3446. }
  3447. }
  3448. }
  3449. }
  3450. void ZoneServer::SendAllSpawnsForVisChange(Client* client, bool limitToEntities) {
  3451. Spawn* spawn = 0;
  3452. if (spawn_range_map.count(client) > 0) {
  3453. MutexMap<int32, float >::iterator itr = spawn_range_map.Get(client)->begin();
  3454. while (itr.Next()) {
  3455. spawn = GetSpawnByID(itr->first);
  3456. if (spawn && (!limitToEntities || (limitToEntities && spawn->IsEntity())) && client->GetPlayer()->WasSentSpawn(spawn->GetID()) && !client->GetPlayer()->WasSpawnRemoved(spawn)) {
  3457. SendSpawnChanges(spawn, client, false, true);
  3458. }
  3459. }
  3460. }
  3461. }
  3462. void ZoneServer::StartZoneSpawnsForLevelThread(Client* client){
  3463. if(zoneShuttingDown)
  3464. return;
  3465. #ifdef WIN32
  3466. _beginthread(SendLevelChangedSpawns, 0, client);
  3467. #else
  3468. pthread_t thread;
  3469. pthread_create(&thread, NULL, SendLevelChangedSpawns, client);
  3470. pthread_detach(thread);
  3471. #endif
  3472. }
  3473. void ZoneServer::ReloadClientQuests(){
  3474. Client* client = 0;
  3475. vector<Client*>::iterator client_itr;
  3476. MClientList.readlock(__FUNCTION__, __LINE__);
  3477. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3478. client = *client_itr;
  3479. if(client)
  3480. client->ReloadQuests();
  3481. }
  3482. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3483. }
  3484. void ZoneServer::SendCalculatedXP(Player* player, Spawn* victim){
  3485. if (player && victim) {
  3486. if (player->GetGroupMemberInfo()) {
  3487. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  3488. PlayerGroup* group = world.GetGroupManager()->GetGroup(player->GetGroupMemberInfo()->group_id);
  3489. if (group)
  3490. {
  3491. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  3492. deque<GroupMemberInfo*>* members = group->GetMembers();
  3493. deque<GroupMemberInfo*>::iterator itr;
  3494. for (itr = members->begin(); itr != members->end(); itr++) {
  3495. GroupMemberInfo* gmi = *itr;
  3496. if (gmi->client) {
  3497. Player* group_member = gmi->client->GetPlayer();
  3498. float xp = group_member->CalculateXP(victim) / members->size();
  3499. if (xp > 0) {
  3500. int16 level = group_member->GetLevel();
  3501. if (group_member->AddXP((int32)xp)) {
  3502. gmi->client->Message(CHANNEL_REWARD, "You gain %u experience!", (int32)xp);
  3503. LogWrite(PLAYER__DEBUG, 0, "Player", "Player: %s earned %u experience (GroupID %u)", group_member->GetName(), (int32)xp, player->GetGroupMemberInfo()->group_id);
  3504. if (group_member->GetLevel() != level)
  3505. gmi->client->ChangeLevel(level, group_member->GetLevel());
  3506. group_member->SetCharSheetChanged(true);
  3507. }
  3508. }
  3509. }
  3510. }
  3511. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  3512. }
  3513. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  3514. }
  3515. else {
  3516. float xp = player->CalculateXP(victim);
  3517. if (xp > 0) {
  3518. Client* client = GetClientBySpawn(player);
  3519. if(!client)
  3520. return;
  3521. int16 level = player->GetLevel();
  3522. if (player->AddXP((int32)xp)) {
  3523. client->Message(CHANNEL_REWARD, "You gain %u XP!", (int32)xp);
  3524. LogWrite(PLAYER__DEBUG, 0, "Player", "Player: %s earned %u experience.", player->GetName(), (int32)xp);
  3525. if(player->GetLevel() != level)
  3526. client->ChangeLevel(level, player->GetLevel());
  3527. player->SetCharSheetChanged(true);
  3528. }
  3529. }
  3530. }
  3531. }
  3532. }
  3533. void ZoneServer::ProcessFaction(Spawn* spawn, Client* client)
  3534. {
  3535. if(client && !spawn->IsPlayer() && spawn->GetFactionID() > 10)
  3536. {
  3537. bool update_result = false;
  3538. Faction* faction = 0;
  3539. vector<int32>* factions = 0;
  3540. Player* player = client->GetPlayer();
  3541. if(player->GetFactions()->ShouldDecrease(spawn->GetFactionID()))
  3542. {
  3543. update_result = player->GetFactions()->DecreaseFaction(spawn->GetFactionID());
  3544. faction = master_faction_list.GetFaction(spawn->GetFactionID());
  3545. if(faction && update_result)
  3546. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  3547. else if(faction)
  3548. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  3549. factions = master_faction_list.GetHostileFactions(spawn->GetFactionID());
  3550. if(factions)
  3551. {
  3552. vector<int32>::iterator itr;
  3553. for(itr = factions->begin(); itr != factions->end(); itr++)
  3554. {
  3555. if(player->GetFactions()->ShouldIncrease(*itr))
  3556. {
  3557. update_result = player->GetFactions()->IncreaseFaction(*itr);
  3558. faction = master_faction_list.GetFaction(*itr);
  3559. if(faction && update_result)
  3560. client->Message(CHANNEL_FACTION, "Your faction standing with %s got better.", faction->name.c_str());
  3561. else if(faction)
  3562. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any better.", faction->name.c_str());
  3563. }
  3564. }
  3565. }
  3566. }
  3567. factions = master_faction_list.GetFriendlyFactions(spawn->GetFactionID());
  3568. if(factions)
  3569. {
  3570. vector<int32>::iterator itr;
  3571. for(itr = factions->begin(); itr != factions->end(); itr++)
  3572. {
  3573. if(player->GetFactions()->ShouldDecrease(*itr))
  3574. {
  3575. update_result = player->GetFactions()->DecreaseFaction(*itr);
  3576. faction = master_faction_list.GetFaction(*itr);
  3577. if(faction && update_result)
  3578. client->Message(CHANNEL_FACTION, "Your faction standing with %s got worse.", faction->name.c_str());
  3579. else if(faction)
  3580. client->Message(CHANNEL_FACTION, "Your faction standing with %s could not possibly get any worse.", faction->name.c_str());
  3581. }
  3582. }
  3583. }
  3584. EQ2Packet* outapp = client->GetPlayer()->GetFactions()->FactionUpdate(client->GetVersion());
  3585. if(outapp)
  3586. client->QueuePacket(outapp);
  3587. }
  3588. }
  3589. void ZoneServer::Despawn(Spawn* spawn, int32 timer){
  3590. if (spawn && movementMgr != nullptr) {
  3591. movementMgr->RemoveMob((Entity*)spawn);
  3592. }
  3593. if(!spawn || spawn->IsPlayer())
  3594. return;
  3595. RemoveSpawnSupportFunctions(spawn);
  3596. if(spawn->IsEntity())
  3597. ((Entity*)spawn)->InCombat(false);
  3598. if(timer == 0)
  3599. timer = 1;
  3600. AddDeadSpawn(spawn, timer);
  3601. }
  3602. void ZoneServer::KillSpawn(bool spawnListLocked, Spawn* dead, Spawn* killer, bool send_packet, int8 damage_type, int16 kill_blow_type)
  3603. {
  3604. MDeadSpawns.readlock(__FUNCTION__, __LINE__);
  3605. if(!dead || this->dead_spawns.count(dead->GetID()) > 0) {
  3606. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  3607. return;
  3608. }
  3609. MDeadSpawns.releasereadlock(__FUNCTION__, __LINE__);
  3610. PacketStruct* packet = 0;
  3611. Client* client = 0;
  3612. vector<int32>* encounter = 0;
  3613. bool killer_in_encounter = false;
  3614. if(dead->IsEntity())
  3615. {
  3616. ((Entity*)dead)->InCombat(false);
  3617. dead->SetInitialState(16512, false); // This will make aerial npc's fall after death
  3618. dead->SetHP(0);
  3619. dead->SetSpawnType(3);
  3620. dead->appearance.attackable = 0;
  3621. // Remove hate towards dead from all npc's in the zone
  3622. ClearHate((Entity*)dead);
  3623. // Check kill and death procs
  3624. if (killer && dead != killer){
  3625. if (dead->IsEntity())
  3626. ((Entity*)dead)->CheckProcs(PROC_TYPE_DEATH, killer);
  3627. if (killer->IsEntity())
  3628. ((Entity*)killer)->CheckProcs(PROC_TYPE_KILL, dead);
  3629. }
  3630. //Check if caster is alive after death proc called, incase of deathsave
  3631. if (dead->Alive())
  3632. return;
  3633. RemoveSpellTimersFromSpawn(dead, true, !dead->IsPlayer());
  3634. if(dead->IsPlayer())
  3635. {
  3636. ((Player*)dead)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_DEATHS, 1);
  3637. client = GetClientBySpawn(dead);
  3638. if(client) {
  3639. if(client->GetPlayer()->DamageEquippedItems(10, client))
  3640. client->QueuePacket(client->GetPlayer()->GetEquipmentList()->serialize(client->GetVersion()));
  3641. client->DisplayDeadWindow();
  3642. }
  3643. }
  3644. else if (dead->IsNPC()) {
  3645. encounter = ((NPC*)dead)->Brain()->GetEncounter();
  3646. }
  3647. }
  3648. dead->SetActionState(0);
  3649. dead->SetTempActionState(0);
  3650. // If dead is an npc get the encounter and loop through it giving out the rewards, no rewards for pets
  3651. if (dead->IsNPC() && !dead->IsPet() && !dead->IsBot()) {
  3652. Spawn* spawn = 0;
  3653. int8 size = encounter->size();
  3654. // Needs npc to have access to the encounter list for who is allowed to loot
  3655. NPC* chest = 0;
  3656. if (!((NPC*)dead)->Brain()->PlayerInEncounter()) {
  3657. dead->SetLootCoins(0);
  3658. dead->ClearLoot();
  3659. }
  3660. // If dead has loot attempt to drop a chest
  3661. if (dead->HasLoot()) {
  3662. chest = ((NPC*)dead)->DropChest();
  3663. }
  3664. for (int8 i = 0; i < encounter->size(); i++) {
  3665. spawn = GetSpawnByID(encounter->at(i), spawnListLocked);
  3666. // set a flag to let us know if the killer is in the encounter
  3667. if (!killer_in_encounter && spawn == killer)
  3668. killer_in_encounter = true;
  3669. if (spawn && spawn->IsPlayer()) {
  3670. // Update players total kill count
  3671. ((Player*)spawn)->UpdatePlayerStatistic(STAT_PLAYER_TOTAL_NPC_KILLS, 1);
  3672. // If this was an epic mob kill send the announcement for this player
  3673. if (dead->GetEncounterLevel() >= 10)
  3674. SendEpicMobDeathToGuild((Player*)spawn, dead);
  3675. // Clear hostile spells from the players spell queue
  3676. spellProcess->RemoveSpellFromQueue((Player*)spawn, true);
  3677. // Get the client of the player
  3678. client = GetClientBySpawn(spawn);
  3679. // valid client?
  3680. if (client) {
  3681. // Check for quest kill updates
  3682. client->CheckPlayerQuestsKillUpdate(dead);
  3683. // 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
  3684. if (!dead->IsPlayer() && dead->GetFactionID() > 10)
  3685. ProcessFaction(dead, client);
  3686. // Send xp...this is currently wrong fix it
  3687. if (spawn != dead && ((Player*)spawn)->GetArrowColor(dead->GetLevel()) >= ARROW_COLOR_GREEN) {
  3688. //SendCalculatedXP((Player*)spawn, dead);
  3689. float xp = ((Player*)spawn)->CalculateXP(dead) / size;
  3690. if (xp > 0) {
  3691. int16 level = spawn->GetLevel();
  3692. if (((Player*)spawn)->AddXP((int32)xp)) {
  3693. client->Message(CHANNEL_REWARD, "You gain %u XP!", (int32)xp);
  3694. LogWrite(PLAYER__DEBUG, 0, "Player", "Player: %s earned %u experience.", spawn->GetName(), (int32)xp);
  3695. if (spawn->GetLevel() != level)
  3696. client->ChangeLevel(level, spawn->GetLevel());
  3697. ((Player*)spawn)->SetCharSheetChanged(true);
  3698. }
  3699. }
  3700. }
  3701. }
  3702. }
  3703. // If a chest is being dropped add this spawn to the chest's encounter so they can loot it
  3704. if (chest && spawn && spawn->IsEntity())
  3705. chest->Brain()->AddToEncounter((Entity*)spawn);
  3706. }
  3707. // If a chest is being dropped add it to the world and set the timer to remove it.
  3708. if (chest) {
  3709. AddSpawn(chest);
  3710. AddDeadSpawn(chest, 0xFFFFFFFF);
  3711. LogWrite(LOOT__DEBUG, 0, "Loot", "Adding a chest to the world...");
  3712. }
  3713. }
  3714. // Reset client pointer
  3715. client = 0;
  3716. // Killer was not in the encounter, give them the faction hit but no xp
  3717. if (!killer_in_encounter) {
  3718. // make sure the killer is a player and the dead spawn had a faction and wasn't a player
  3719. if (killer && killer->IsPlayer()) {
  3720. if (!dead->IsPlayer() && dead->GetFactionID() > 10) {
  3721. client = GetClientBySpawn(killer);
  3722. if (client)
  3723. ProcessFaction(dead, client);
  3724. }
  3725. // Clear hostile spells from the killers spell queue
  3726. spellProcess->RemoveSpellFromQueue((Player*)killer, true);
  3727. }
  3728. }
  3729. // Reset client pointer
  3730. client = 0;
  3731. vector<Spawn*>* group = dead->GetSpawnGroup();
  3732. if (group && group->size() == 1)
  3733. CallSpawnScript(dead, SPAWN_SCRIPT_GROUP_DEAD, killer);
  3734. safe_delete(group);
  3735. // Remove the support functions for the dead spawn
  3736. RemoveSpawnSupportFunctions(dead);
  3737. // Erase the expire timer if it has one
  3738. if (spawn_expire_timers.count(dead->GetID()) > 0)
  3739. spawn_expire_timers.erase(dead->GetID());
  3740. // If dead is an npc or object call the spawn scrip and handle instance stuff
  3741. if(dead->IsNPC() || dead->IsObject())
  3742. {
  3743. // handle instance spawn db info
  3744. // we don't care if a NPC or a client kills the spawn, we could have events that cause NPCs to kill NPCs.
  3745. if(dead->GetZone()->GetInstanceID() > 0 && dead->GetSpawnLocationID() > 0)
  3746. {
  3747. // use respawn time to either insert/update entry (likely insert in this situation)
  3748. if(dead->IsNPC())
  3749. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_NPC, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  3750. else if ( dead->IsObject ( ) )
  3751. database.CreateInstanceSpawnRemoved(dead->GetSpawnLocationID(),SPAWN_ENTRY_TYPE_OBJECT, dead->GetRespawnTime(),dead->GetZone()->GetInstanceID());
  3752. }
  3753. // Call the spawn scripts death() function
  3754. CallSpawnScript(dead, SPAWN_SCRIPT_DEATH, killer);
  3755. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  3756. if (zone_script && lua_interface)
  3757. lua_interface->RunZoneScript(zone_script, "spawn_killed", this, dead, 0, 0, killer);
  3758. }
  3759. int32 victim_id = dead->GetID();
  3760. int32 attacker_id = 0xFFFFFFFF;
  3761. if(killer)
  3762. attacker_id = killer->GetID();
  3763. if(send_packet)
  3764. {
  3765. vector<Client*>::iterator client_itr;
  3766. MClientList.readlock(__FUNCTION__, __LINE__);
  3767. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3768. client = *client_itr;
  3769. if(!client->GetPlayer()->WasSentSpawn(victim_id) || (attacker_id != 0xFFFFFFFF && !client->GetPlayer()->WasSentSpawn(attacker_id)) )
  3770. continue;
  3771. else if(client->GetPlayer()->WasSpawnRemoved(dead) || (attacker_id != 0xFFFFFFFF && client->GetPlayer()->WasSpawnRemoved(killer)))
  3772. continue;
  3773. else if(killer && killer->GetDistance(client->GetPlayer()) > HEAR_SPAWN_DISTANCE)
  3774. continue;
  3775. packet = configReader.getStruct("WS_HearDeath", client->GetVersion());
  3776. if(packet)
  3777. {
  3778. if(killer)
  3779. packet->setDataByName("attacker", client->GetPlayer()->GetIDWithPlayerSpawn(killer));
  3780. else
  3781. packet->setDataByName("attacker", 0xFFFFFFFF);
  3782. packet->setDataByName("defender", client->GetPlayer()->GetIDWithPlayerSpawn(dead));
  3783. packet->setDataByName("damage_type", damage_type);
  3784. packet->setDataByName("blow_type", kill_blow_type);
  3785. client->QueuePacket(packet->serialize());
  3786. LogWrite(COMBAT__DEBUG, 0, "Combat", "Zone Killing '%s'", dead->GetName());
  3787. safe_delete(packet);
  3788. }
  3789. }
  3790. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3791. }
  3792. int32 pop_timer = 0xFFFFFFFF;
  3793. if(killer && killer->IsNPC())
  3794. {
  3795. // Call the spawn scripts killed() function
  3796. CallSpawnScript(killer, SPAWN_SCRIPT_KILLED, dead);
  3797. if(!dead->IsPlayer())
  3798. {
  3799. LogWrite(MISC__TODO, 1, "TODO", "Whenever pets are added, check for pet kills\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  3800. // Set the time for the corpse to linger to 5 sec
  3801. //pop_timer = 5000;
  3802. // commented out the timer so in the event the killer is not a player (pet, guard, something else i haven't thought of)
  3803. // the corpse doesn't vanish sooner then it should if it had loot for the players. AddDeadSpawn() will set timers based on if
  3804. // the corpse has loot or not if the timer value (pop_timer) is 0xFFFFFFFF
  3805. }
  3806. }
  3807. // If the dead spawns was not a player add it to the dead spawn list
  3808. if (!dead->IsPlayer() && !dead->IsBot())
  3809. AddDeadSpawn(dead, pop_timer);
  3810. // if dead was a player clear hostile spells from its spell queue
  3811. if (dead->IsPlayer())
  3812. spellProcess->RemoveSpellFromQueue((Player*)dead, true);
  3813. if (dead->IsNPC())
  3814. ((NPC*)dead)->Brain()->ClearHate();
  3815. // ResetPetInfo() is called in DismissPet(), might not need to be here
  3816. // Players pet is killed, clear the pet info from char sheet
  3817. /*if (dead->IsNPC() && ((NPC*)dead)->IsPet() && ((NPC*)dead)->GetOwner()->IsPlayer())
  3818. ((Player*)((NPC*)dead)->GetOwner())->ResetPetInfo();*/
  3819. safe_delete(encounter);
  3820. }
  3821. void ZoneServer::SendDamagePacket(Spawn* attacker, Spawn* victim, int8 type1, int8 type2, int8 damage_type, int16 damage, const char* spell_name) {
  3822. //Scat: was set but never being used anywhere. i see references to 0xFFFFFFFF below so could be old code not used anymore
  3823. //int32 attacker_id = 0xFFFFFFFF;
  3824. //if(attacker)
  3825. // attacker_id = attacker->GetID();
  3826. PacketStruct* packet = 0;
  3827. Client* client = 0;
  3828. if (attacker && victim && victim->IsPlayer() && victim->GetTarget() == 0) {
  3829. client = GetClientBySpawn(victim);
  3830. if (client)
  3831. client->TargetSpawn(attacker);
  3832. }
  3833. vector<Client*>::iterator client_itr;
  3834. MClientList.readlock(__FUNCTION__, __LINE__);
  3835. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3836. client = *client_itr;
  3837. if (!client || (client->GetPlayer() != attacker && client->GetPlayer() != victim && ((attacker && client->GetPlayer()->WasSentSpawn(attacker->GetID()) == false) || (victim && client->GetPlayer()->WasSentSpawn(victim->GetID()) == false))))
  3838. continue;
  3839. if ((attacker && client->GetPlayer()->WasSpawnRemoved(attacker)) || (victim && client->GetPlayer()->WasSpawnRemoved(victim)))
  3840. continue;
  3841. if (attacker && attacker->GetDistance(client->GetPlayer()) > 50)
  3842. continue;
  3843. if (victim && victim->GetDistance(client->GetPlayer()) > 50)
  3844. continue;
  3845. switch (type1) {
  3846. case DAMAGE_PACKET_TYPE_SIPHON_SPELL:
  3847. case DAMAGE_PACKET_TYPE_SIPHON_SPELL2:
  3848. packet = configReader.getStruct("WS_HearSiphonSpellDamage", client->GetVersion());
  3849. break;
  3850. case DAMAGE_PACKET_TYPE_MULTIPLE_DAMAGE:
  3851. if (client->GetVersion() > 546)
  3852. packet = configReader.getStruct("WS_HearMultipleDamage", client->GetVersion());
  3853. else
  3854. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  3855. break;
  3856. case DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG:
  3857. case DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE:
  3858. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  3859. break;
  3860. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE2:
  3861. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE3:
  3862. case DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG:
  3863. case DAMAGE_PACKET_TYPE_SPELL_DAMAGE:
  3864. if (client->GetVersion() > 546)
  3865. packet = configReader.getStruct("WS_HearSpellDamage", client->GetVersion());
  3866. else
  3867. packet = configReader.getStruct("WS_HearSimpleDamage", client->GetVersion());
  3868. if (packet)
  3869. packet->setSubstructDataByName("header", "unknown", 5);
  3870. break;
  3871. case DAMAGE_PACKET_TYPE_RANGE_DAMAGE:
  3872. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  3873. break;
  3874. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG:
  3875. case DAMAGE_PACKET_TYPE_RANGE_SPELL_DMG2:
  3876. packet = configReader.getStruct("WS_HearRangeDamage", client->GetVersion());
  3877. break;
  3878. default:
  3879. LogWrite(ZONE__ERROR, 0, "Zone", "Unknown Damage Packet type: %i in ZoneServer::SendDamagePacket.", type1);
  3880. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3881. return;
  3882. }
  3883. if (packet) {
  3884. if (client->GetVersion() > 546) {
  3885. packet->setSubstructDataByName("header", "packet_type", type1);
  3886. packet->setSubstructDataByName("header", "result_type", type2);
  3887. packet->setDataByName("damage_type", damage_type);
  3888. packet->setDataByName("damage", damage);
  3889. }
  3890. else {
  3891. switch (type2) {
  3892. case DAMAGE_PACKET_RESULT_MISS:
  3893. packet->setSubstructDataByName("header", "result_type", 1);
  3894. break;
  3895. case DAMAGE_PACKET_RESULT_DODGE:
  3896. packet->setSubstructDataByName("header", "result_type", 2);
  3897. break;
  3898. case DAMAGE_PACKET_RESULT_PARRY:
  3899. packet->setSubstructDataByName("header", "result_type", 3);
  3900. break;
  3901. case DAMAGE_PACKET_RESULT_RIPOSTE:
  3902. packet->setSubstructDataByName("header", "result_type", 4);
  3903. break;
  3904. case DAMAGE_PACKET_RESULT_BLOCK:
  3905. packet->setSubstructDataByName("header", "result_type", 5);
  3906. break;
  3907. case DAMAGE_PACKET_RESULT_INVULNERABLE:
  3908. packet->setSubstructDataByName("header", "result_type", 7);
  3909. break;
  3910. case DAMAGE_PACKET_RESULT_RESIST:
  3911. packet->setSubstructDataByName("header", "result_type", 9);
  3912. break;
  3913. case DAMAGE_PACKET_RESULT_REFLECT:
  3914. packet->setSubstructDataByName("header", "result_type", 10);
  3915. break;
  3916. case DAMAGE_PACKET_RESULT_IMMUNE:
  3917. packet->setSubstructDataByName("header", "result_type", 11);
  3918. break;
  3919. }
  3920. packet->setArrayLengthByName("num_dmg", 1);
  3921. packet->setSubstructDataByName("header", "defender_proxy", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  3922. packet->setArrayDataByName("damage_type", damage_type);
  3923. packet->setArrayDataByName("damage", damage);
  3924. }
  3925. if (!attacker)
  3926. packet->setSubstructDataByName("header", "attacker", 0xFFFFFFFF);
  3927. else
  3928. packet->setSubstructDataByName("header", "attacker", client->GetPlayer()->GetIDWithPlayerSpawn(attacker));
  3929. packet->setSubstructDataByName("header", "defender", client->GetPlayer()->GetIDWithPlayerSpawn(victim));
  3930. if (spell_name) {
  3931. packet->setDataByName("spell", 1);
  3932. packet->setDataByName("spell_name", spell_name);
  3933. }
  3934. EQ2Packet* app = packet->serialize();
  3935. //DumpPacket(app);
  3936. client->QueuePacket(app);
  3937. safe_delete(packet);
  3938. packet = 0;
  3939. }
  3940. }
  3941. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3942. }
  3943. void ZoneServer::SendHealPacket(Spawn* caster, Spawn* target, int16 heal_type, int32 heal_amt, const char* spell_name){
  3944. Client* client = 0;
  3945. vector<Client*>::iterator client_itr;
  3946. MClientList.readlock(__FUNCTION__, __LINE__);
  3947. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3948. client = *client_itr;
  3949. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  3950. continue;
  3951. if((caster && client->GetPlayer()->WasSpawnRemoved(caster)) || (caster && client->GetPlayer()->WasSpawnRemoved(target)))
  3952. continue;
  3953. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  3954. continue;
  3955. if(target && target->GetDistance(client->GetPlayer()) > 50)
  3956. continue;
  3957. PacketStruct* packet = configReader.getStruct("WS_HearHeal", client->GetVersion());
  3958. if (packet) {
  3959. packet->setDataByName("caster", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  3960. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  3961. packet->setDataByName("heal_amt", heal_amt);
  3962. packet->setDataByName("spellname", spell_name);
  3963. packet->setDataByName("type", heal_type);
  3964. packet->setDataByName("unknown2", 1);
  3965. EQ2Packet* app = packet->serialize();
  3966. client->QueuePacket(app);
  3967. safe_delete(packet);
  3968. }
  3969. }
  3970. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3971. }
  3972. void ZoneServer::SendThreatPacket(Spawn* caster, Spawn* target, int32 threat_amt, const char* spell_name) {
  3973. Client* client = 0;
  3974. vector<Client*>::iterator client_itr;
  3975. MClientList.readlock(__FUNCTION__, __LINE__);
  3976. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  3977. client = *client_itr;
  3978. if(!client || (client->GetPlayer() != caster && ((caster && client->GetPlayer()->WasSentSpawn(caster->GetID()) == false) || (target && client->GetPlayer()->WasSentSpawn(target->GetID()) == false))))
  3979. continue;
  3980. if((caster && client->GetPlayer()->WasSpawnRemoved(caster)) || (caster && client->GetPlayer()->WasSpawnRemoved(target)))
  3981. continue;
  3982. if(caster && caster->GetDistance(client->GetPlayer()) > 50)
  3983. continue;
  3984. if(target && target->GetDistance(client->GetPlayer()) > 50)
  3985. continue;
  3986. PacketStruct* packet = configReader.getStruct("WS_HearThreatCmd", client->GetVersion());
  3987. if (packet) {
  3988. packet->setDataByName("spell_name", spell_name);
  3989. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  3990. packet->setDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  3991. packet->setDataByName("threat_amount", threat_amt);
  3992. client->QueuePacket(packet->serialize());
  3993. }
  3994. safe_delete(packet);
  3995. }
  3996. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  3997. }
  3998. void ZoneServer::SendSpellFailedPacket(Client* client, int16 error){
  3999. if(!client)
  4000. return;
  4001. PacketStruct* packet = configReader.getStruct("WS_DisplaySpellFailed", client->GetVersion());
  4002. if(packet){
  4003. /* Temp solution, need to modify the error code before this function and while we still have access to the spell/combat art */
  4004. error = master_spell_list.GetSpellErrorValue(client->GetVersion(), error);
  4005. packet->setDataByName("error_code", error);
  4006. //packet->PrintPacket();
  4007. client->QueuePacket(packet->serialize());
  4008. safe_delete(packet);
  4009. }
  4010. }
  4011. void ZoneServer::SendInterruptPacket(Spawn* interrupted, LuaSpell* spell){
  4012. if(!interrupted || !spell)
  4013. return;
  4014. EQ2Packet* outapp = 0;
  4015. PacketStruct* packet = 0;
  4016. Client* client = 0;
  4017. vector<Client*>::iterator client_itr;
  4018. MClientList.readlock(__FUNCTION__, __LINE__);
  4019. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4020. client = *client_itr;
  4021. if(!client || !client->GetPlayer()->WasSentSpawn(interrupted->GetID()) || client->GetPlayer()->WasSpawnRemoved(interrupted))
  4022. continue;
  4023. packet = configReader.getStruct("WS_Interrupt", client->GetVersion());
  4024. if(packet){
  4025. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(interrupted));
  4026. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4027. for (int32 i = 0; i < spell->targets.size(); i++)
  4028. packet->setArrayDataByName("target_id", client->GetPlayer()->GetIDWithPlayerSpawn(client->GetPlayer()->GetZone()->GetSpawnByID(spell->targets[i])), i);
  4029. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4030. outapp = packet->serialize();
  4031. client->QueuePacket(outapp);
  4032. safe_delete(packet);
  4033. }
  4034. }
  4035. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4036. safe_delete(packet);
  4037. }
  4038. void ZoneServer::SendCastSpellPacket(LuaSpell* spell, Entity* caster){
  4039. EQ2Packet* outapp = 0;
  4040. PacketStruct* packet = 0;
  4041. Client* client = 0;
  4042. if(!caster || !spell || !spell->spell || spell->interrupted)
  4043. return;
  4044. vector<Client*>::iterator client_itr;
  4045. MClientList.readlock(__FUNCTION__, __LINE__);
  4046. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4047. client = *client_itr;
  4048. if(!client)
  4049. continue;
  4050. packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4051. if(packet){
  4052. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(caster));
  4053. packet->setArrayLengthByName("num_targets", spell->targets.size());
  4054. for (int32 i = 0; i < spell->targets.size(); i++)
  4055. packet->setArrayDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(spell->caster->GetZone()->GetSpawnByID(spell->targets[i])), i);
  4056. packet->setDataByName("spell_visual", spell->spell->GetSpellData()->spell_visual); //result
  4057. packet->setDataByName("cast_time", spell->spell->GetSpellData()->cast_time*.01); //delay
  4058. packet->setDataByName("spell_id", spell->spell->GetSpellID());
  4059. packet->setDataByName("spell_level", 1);
  4060. packet->setDataByName("spell_tier", spell->spell->GetSpellData()->tier);
  4061. outapp = packet->serialize();
  4062. client->QueuePacket(outapp);
  4063. safe_delete(packet);
  4064. }
  4065. }
  4066. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4067. safe_delete(packet);
  4068. }
  4069. void ZoneServer::SendCastSpellPacket(int32 spell_visual, Spawn* target, Spawn* caster) {
  4070. if (target) {
  4071. vector<Client*>::iterator client_itr;
  4072. MClientList.readlock(__FUNCTION__, __LINE__);
  4073. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4074. Client* client = *client_itr;
  4075. if (!client)
  4076. continue;
  4077. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4078. if (packet) {
  4079. if (!caster) {
  4080. packet->setDataByName("spawn_id", 0xFFFFFFFF);
  4081. packet->setDataByName("invoker_id", 0xFFFFFFFF);
  4082. }
  4083. else {
  4084. int32 caster_id = client->GetPlayer()->GetIDWithPlayerSpawn(caster);
  4085. packet->setDataByName("spawn_id", caster_id);
  4086. packet->setDataByName("invoker_id", caster_id);
  4087. }
  4088. packet->setArrayLengthByName("num_targets", 1);
  4089. packet->setArrayDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4090. packet->setDataByName("spell_visual", spell_visual);
  4091. packet->setDataByName("cast_time", 0);
  4092. packet->setDataByName("spell_id", 0);
  4093. packet->setDataByName("spell_level", 0);
  4094. packet->setDataByName("spell_tier", 1);
  4095. client->QueuePacket(packet->serialize());
  4096. safe_delete(packet);
  4097. }
  4098. }
  4099. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4100. }
  4101. }
  4102. void ZoneServer::SendCastEntityCommandPacket(EntityCommand* entity_command, int32 spawn_id, int32 target_id) {
  4103. if (entity_command) {
  4104. Spawn* spawn = GetSpawnByID(spawn_id);
  4105. Spawn* target = GetSpawnByID(target_id);
  4106. if (!spawn || !target)
  4107. return;
  4108. Client* client = 0;
  4109. vector<Client*>::iterator client_itr;
  4110. MClientList.readlock(__FUNCTION__, __LINE__);
  4111. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4112. client = *client_itr;
  4113. if (!client || !client->GetPlayer()->WasSentSpawn(spawn_id) || client->GetPlayer()->WasSpawnRemoved(spawn) || !client->GetPlayer()->WasSentSpawn(target_id) || client->GetPlayer()->WasSpawnRemoved(target))
  4114. continue;
  4115. PacketStruct* packet = configReader.getStruct("WS_HearCastSpell", client->GetVersion());
  4116. if (packet) {
  4117. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  4118. packet->setArrayLengthByName("num_targets", 1);
  4119. packet->setArrayDataByName("target", client->GetPlayer()->GetIDWithPlayerSpawn(target));
  4120. packet->setDataByName("num_targets", 1);
  4121. packet->setDataByName("spell_visual", entity_command->spell_visual); //result
  4122. packet->setDataByName("cast_time", entity_command->cast_time * 0.01); //delay
  4123. packet->setDataByName("spell_id", 1);
  4124. packet->setDataByName("spell_level", 1);
  4125. packet->setDataByName("spell_tier", 1);
  4126. EQ2Packet* outapp = packet->serialize();
  4127. client->QueuePacket(outapp);
  4128. safe_delete(packet);
  4129. }
  4130. }
  4131. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4132. }
  4133. }
  4134. void ZoneServer::StartZoneInitialSpawnThread(Client* client){
  4135. if(zoneShuttingDown)
  4136. return;
  4137. #ifdef WIN32
  4138. _beginthread(SendInitialSpawns, 0, client);
  4139. #else
  4140. pthread_t thread;
  4141. pthread_create(&thread, NULL, SendInitialSpawns, client);
  4142. pthread_detach(thread);
  4143. #endif
  4144. }
  4145. void ZoneServer::SendZoneSpawns(Client* client){
  4146. int8 count = 0;
  4147. while (LoadingData && count <= 6000) { //sleep for max of 60 seconds (60000ms) while the maps are loading
  4148. count++;
  4149. Sleep(10);
  4150. }
  4151. count = 0;
  4152. int16 size = 0;
  4153. //give the spawn thread a tad bit of time to add the pending_spawns to spawn_list (up to 10 seconds)
  4154. while (count < 1000) {
  4155. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  4156. size = pending_spawn_list_add.size();
  4157. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  4158. if (size == 0)
  4159. break;
  4160. Sleep(10);
  4161. count++;
  4162. }
  4163. initial_spawn_threads_active++;
  4164. map<int32, Spawn*>::iterator itr;
  4165. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4166. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4167. Spawn* spawn = itr->second;
  4168. if (spawn) {
  4169. CheckSpawnRange(client, spawn, true);
  4170. }
  4171. }
  4172. CheckSendSpawnToClient(client, true);
  4173. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4174. client->SetConnected(true);
  4175. ClientPacketFunctions::SendFinishedEntitiesList(client);
  4176. initial_spawn_threads_active--;
  4177. }
  4178. vector<Entity*> ZoneServer::GetPlayers(){
  4179. vector<Entity*> ret;
  4180. Client* client = 0;
  4181. vector<Client*>::iterator client_itr;
  4182. MClientList.readlock(__FUNCTION__, __LINE__);
  4183. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4184. client = *client_itr;
  4185. ret.push_back(client->GetPlayer());
  4186. }
  4187. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4188. return ret;
  4189. }
  4190. int16 ZoneServer::SetSpawnTargetable(Spawn* spawn, float distance){
  4191. Spawn* test_spawn = 0;
  4192. int16 ret_val = 0;
  4193. map<int32, Spawn*>::iterator itr;
  4194. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4195. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4196. test_spawn = itr->second;
  4197. if(test_spawn){
  4198. if(test_spawn->GetDistance(spawn) <= distance){
  4199. test_spawn->SetTargetable(1);
  4200. ret_val++;
  4201. }
  4202. }
  4203. }
  4204. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4205. return ret_val;
  4206. }
  4207. int16 ZoneServer::SetSpawnTargetable(int32 spawn_id){
  4208. Spawn* spawn = 0;
  4209. int16 ret_val = 0;
  4210. map<int32, Spawn*>::iterator itr;
  4211. MSpawnList.readlock(__FUNCTION__, __LINE__);
  4212. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  4213. spawn = itr->second;
  4214. if(spawn){
  4215. if(spawn->GetDatabaseID() == spawn_id){
  4216. spawn->SetTargetable(1);
  4217. ret_val++;
  4218. }
  4219. }
  4220. }
  4221. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  4222. return ret_val;
  4223. }
  4224. 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) {
  4225. ZoneInfoSlideStructInfo* info = new ZoneInfoSlideStructInfo();
  4226. memset(info, 0, sizeof(ZoneInfoSlideStructInfo));
  4227. info->unknown1[0] = unknown1a;
  4228. info->unknown1[1] = unknown1b;
  4229. info->unknown2[0] = unknown2a;
  4230. info->unknown2[1] = unknown2b;
  4231. info->unknown3 = unknown3;
  4232. info->unknown4 = unknown4;
  4233. int8 length = strlen(slide);
  4234. if (length >= 128)
  4235. length = 127;
  4236. strncpy(info->slide, slide, length);
  4237. length = strlen(voiceover);
  4238. if (length >= 128)
  4239. length = 127;
  4240. strncpy(info->voiceover, voiceover, length);
  4241. info->key1 = key1;
  4242. info->key2 = key2;
  4243. ZoneInfoSlideStruct* ret = new ZoneInfoSlideStruct();
  4244. ret->info = info;
  4245. return ret;
  4246. }
  4247. void ZoneServer::AddZoneInfoSlideStructTransitionInfo(ZoneInfoSlideStruct* info, int32 x, int32 y, float zoom, float transition_time) {
  4248. ZoneInfoSlideStructTransitionInfo* transition_info = new ZoneInfoSlideStructTransitionInfo();
  4249. transition_info->transition_x = x;
  4250. transition_info->transition_y = y;
  4251. transition_info->transition_zoom = zoom;
  4252. transition_info->transition_time = transition_time;
  4253. info->slide_transition_info.push_back(transition_info);
  4254. }
  4255. vector<ZoneInfoSlideStruct*>* ZoneServer::GenerateTutorialSlides() {
  4256. vector<ZoneInfoSlideStruct*>* slides = new vector<ZoneInfoSlideStruct*>();
  4257. 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);
  4258. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4259. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 16);
  4260. slides->push_back(slide);
  4261. 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);
  4262. AddZoneInfoSlideStructTransitionInfo(slide, 600, 365, 1.60000002384186, 0);
  4263. AddZoneInfoSlideStructTransitionInfo(slide, 800, 370, 1.39999997615814, 9);
  4264. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 8);
  4265. slides->push_back(slide);
  4266. 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);
  4267. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.20000004768372, 0);
  4268. AddZoneInfoSlideStructTransitionInfo(slide, 750, 320, 1.60000002384186, 10);
  4269. AddZoneInfoSlideStructTransitionInfo(slide, 575, 265, 2.29999995231628, 10);
  4270. slides->push_back(slide);
  4271. 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);
  4272. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 2.5, 0);
  4273. AddZoneInfoSlideStructTransitionInfo(slide, 920, 420, 1.70000004768372, 8);
  4274. AddZoneInfoSlideStructTransitionInfo(slide, 675, 390, 2.20000004768372, 11);
  4275. slides->push_back(slide);
  4276. 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);
  4277. AddZoneInfoSlideStructTransitionInfo(slide, 750, 500, 2.79999995231628, 0);
  4278. AddZoneInfoSlideStructTransitionInfo(slide, 720, 300, 2.5, 9);
  4279. AddZoneInfoSlideStructTransitionInfo(slide, 975, 270, 2.20000004768372, 9);
  4280. slides->push_back(slide);
  4281. 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);
  4282. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.89999997615814, 0);
  4283. AddZoneInfoSlideStructTransitionInfo(slide, 920, 475, 1, 24);
  4284. slides->push_back(slide);
  4285. 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);
  4286. AddZoneInfoSlideStructTransitionInfo(slide, 1400, 420, 2.40000009536743, 0);
  4287. AddZoneInfoSlideStructTransitionInfo(slide, 1200, 375, 1.70000004768372, 7);
  4288. AddZoneInfoSlideStructTransitionInfo(slide, 800, 225, 2.29999995231628, 7);
  4289. slides->push_back(slide);
  4290. 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);
  4291. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4292. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1.5, 27);
  4293. slides->push_back(slide);
  4294. 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);
  4295. AddZoneInfoSlideStructTransitionInfo(slide, 715, 305, 2.40000009536743, 0);
  4296. AddZoneInfoSlideStructTransitionInfo(slide, 730, 325, 1.79999995231628, 6);
  4297. AddZoneInfoSlideStructTransitionInfo(slide, 920, 395, 1.5, 5);
  4298. AddZoneInfoSlideStructTransitionInfo(slide, 1360, 330, 1.79999995231628, 9);
  4299. slides->push_back(slide);
  4300. 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);
  4301. AddZoneInfoSlideStructTransitionInfo(slide, 670, 675, 2.20000004768372, 0);
  4302. AddZoneInfoSlideStructTransitionInfo(slide, 710, 390, 1.79999995231628, 7);
  4303. AddZoneInfoSlideStructTransitionInfo(slide, 920, 415, 1.60000002384186, 5.5);
  4304. AddZoneInfoSlideStructTransitionInfo(slide, 1250, 675, 1.79999995231628, 8);
  4305. slides->push_back(slide);
  4306. 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);
  4307. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 0);
  4308. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 19);
  4309. slides->push_back(slide);
  4310. 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);
  4311. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 2, 0);
  4312. AddZoneInfoSlideStructTransitionInfo(slide, 920, 495, 1, 43);
  4313. slides->push_back(slide);
  4314. return slides;
  4315. }
  4316. EQ2Packet* ZoneServer::GetZoneInfoPacket(Client* client){
  4317. client_spawn_map.Put(client->GetPlayer(), client);
  4318. PacketStruct* packet = configReader.getStruct("WS_ZoneInfo", client->GetVersion());
  4319. packet->setSmallStringByName("server1",net.GetWorldName());
  4320. packet->setSmallStringByName("server2",net.GetWorldName());
  4321. packet->setDataByName("unknown1", 1, 1);//1, 1
  4322. int32 expansions = EXPANSION_UNKNOWN + EXPANSION_DOF + EXPANSION_KOS + EXPANSION_EOF + EXPANSION_ROK + EXPANSION_TSO + EXPANSION_DOV;
  4323. //packet->setDataByName("expansions_enabled", 82313211);//expansions 63181
  4324. //packet->setDataByName("expansions_enabled", 552075103);//expansions 63182
  4325. packet->setDataByName("expansions_enabled", 4294967295);//expansions 1096 //612499455 works
  4326. if (client->GetVersion() >= 1193) {
  4327. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4328. packet->setDataByName("unknown3", 4294967295, 1); //COE and up
  4329. packet->setDataByName("unknown3", 4294967295, 2);
  4330. }
  4331. else
  4332. packet->setDataByName("unknown3", 4294967295, 0); // DOV and down
  4333. packet->setSmallStringByName("auction_website", "eq2emulator.net");
  4334. packet->setDataByName("auction_port", 80);
  4335. packet->setSmallStringByName("upload_page", "test_upload.m");
  4336. packet->setSmallStringByName("upload_key", "dsya987yda9");
  4337. packet->setSmallStringByName("zone", GetZoneFile());
  4338. //packet->setSmallStringByName("zone2", GetZoneName());
  4339. //if ( strlen(GetZoneSkyFile()) > 0 )
  4340. // packet->setSmallStringByName("zone_unknown2", GetZoneSkyFile()); // used for the sky map
  4341. packet->setSmallStringByName("zone_desc", GetZoneDescription());
  4342. packet->setSmallStringByName("char_name", client->GetPlayer()->GetName());
  4343. packet->setDataByName("x", client->GetPlayer()->GetX());
  4344. packet->setDataByName("y", client->GetPlayer()->GetY());
  4345. packet->setDataByName("z", client->GetPlayer()->GetZ());
  4346. 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)
  4347. vector<ZoneInfoSlideStruct*>* slides = GenerateTutorialSlides();
  4348. if (slides) {
  4349. packet->setArrayLengthByName("num_slides", slides->size());
  4350. ZoneInfoSlideStruct* slide = 0;
  4351. for (int8 i = 0; i < slides->size(); i++) {
  4352. slide = slides->at(i);
  4353. packet->setArrayDataByName("unknown1", slide->info->unknown1[0], i, 0);
  4354. packet->setArrayDataByName("unknown1", slide->info->unknown1[1], i, 1);
  4355. packet->setArrayDataByName("unknown2", slide->info->unknown2[0], i, 0);
  4356. packet->setArrayDataByName("unknown2", slide->info->unknown2[1], i, 1);
  4357. packet->setArrayDataByName("unknown3", slide->info->unknown3, i);
  4358. packet->setArrayDataByName("unknown4", slide->info->unknown4, i);
  4359. packet->setArrayDataByName("slide", slide->info->slide, i);
  4360. packet->setArrayDataByName("voiceover", slide->info->voiceover, i);
  4361. packet->setArrayDataByName("key1", slide->info->key1, i);
  4362. packet->setArrayDataByName("key2", slide->info->key2, i);
  4363. packet->setSubArrayLengthByName("num_transitions", slide->slide_transition_info.size(), i);
  4364. for (int8 x = 0; x < slide->slide_transition_info.size(); x++) {
  4365. packet->setSubArrayDataByName("transition_x", slide->slide_transition_info[x]->transition_x, i, x);
  4366. packet->setSubArrayDataByName("transition_y", slide->slide_transition_info[x]->transition_y, i, x);
  4367. packet->setSubArrayDataByName("transition_zoom", slide->slide_transition_info[x]->transition_zoom, i, x);
  4368. packet->setSubArrayDataByName("transition_time", slide->slide_transition_info[x]->transition_time, i, x);
  4369. safe_delete(slide->slide_transition_info[x]);
  4370. }
  4371. safe_delete(slide->info);
  4372. safe_delete(slide);
  4373. }
  4374. }
  4375. safe_delete(slides);
  4376. }
  4377. packet->setDataByName("underworld", underworld);
  4378. // unknown3 can prevent screen shots from being taken if
  4379. //packet->setDataByName("unknown3", 2094661567, 1); // Screenshots allowed with this value
  4380. //packet->setDataByName("unknown3", 3815767999, 1); // Screenshots disabled with this value
  4381. //packet->setDataByName("unknown3", 1, 2);
  4382. /*if (client->GetVersion() >= 63587) {
  4383. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4384. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4385. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4386. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4387. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4388. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4389. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4390. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4391. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4392. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4393. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4394. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4395. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4396. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4397. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4398. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4399. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4400. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4401. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4402. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4403. }
  4404. else if (client->GetVersion() >= 63214) {
  4405. packet->setArrayLengthByName("num_exp_feature_bytes", 9);
  4406. packet->setArrayDataByName("exp_feature_bytes", 95, 0);//kos and dof
  4407. packet->setArrayDataByName("exp_feature_bytes", 255, 1);//eof rok tso sf dov coe tov
  4408. packet->setArrayDataByName("exp_feature_bytes", 247, 2);//aom tot ka exp14
  4409. packet->setArrayDataByName("exp_feature_bytes", 32, 3);//rum cellar
  4410. packet->setArrayDataByName("exp_feature_bytes", 140, 4);
  4411. packet->setArrayDataByName("exp_feature_bytes", 62, 5);
  4412. packet->setArrayDataByName("exp_feature_bytes", 0, 6);
  4413. packet->setArrayDataByName("exp_feature_bytes", 45, 7);
  4414. packet->setArrayDataByName("exp_feature_bytes", 128, 8);
  4415. packet->setArrayLengthByName("num_unknown3b_bytes", 9);
  4416. packet->setArrayDataByName("unknown3b_bytes", 95, 0);
  4417. packet->setArrayDataByName("unknown3b_bytes", 255, 1);
  4418. packet->setArrayDataByName("unknown3b_bytes", 247, 2);
  4419. packet->setArrayDataByName("unknown3b_bytes", 237, 3);
  4420. packet->setArrayDataByName("unknown3b_bytes", 143, 4);
  4421. packet->setArrayDataByName("unknown3b_bytes", 255, 5);
  4422. packet->setArrayDataByName("unknown3b_bytes", 255, 6);
  4423. packet->setArrayDataByName("unknown3b_bytes", 255, 7);
  4424. packet->setArrayDataByName("unknown3b_bytes", 128, 8);
  4425. }*/
  4426. if (client->GetVersion() >= 64644) {
  4427. packet->setDataByName("unknown3a", 12598924);
  4428. packet->setDataByName("unknown3b", 3992452959);
  4429. packet->setDataByName("unknown3c", 4294967183);
  4430. packet->setDataByName("unknown2a", 9);
  4431. packet->setDataByName("unknown2b", 9);
  4432. }
  4433. else if (client->GetVersion() >= 63181) {
  4434. packet->setDataByName("unknown3a", 750796556);//63182 73821356
  4435. packet->setDataByName("unknown3b", 3991404383);// 63182 3991404383
  4436. packet->setDataByName("unknown3c", 4278189967);// 63182 4278189967
  4437. packet->setDataByName("unknown2a", 8);// 63182
  4438. packet->setDataByName("unknown2b", 8);// 63182
  4439. }
  4440. else{
  4441. //packet->setDataByName("unknown3", 872447025,0);//63181
  4442. //packet->setDataByName("unknown3", 3085434875,1);// 63181
  4443. //packet->setDataByName("unknown3", 2147483633,2);// 63181
  4444. }
  4445. packet->setDataByName("year", world.GetWorldTimeStruct()->year);
  4446. packet->setDataByName("month", world.GetWorldTimeStruct()->month);
  4447. packet->setDataByName("day", world.GetWorldTimeStruct()->day);
  4448. packet->setDataByName("hour", world.GetWorldTimeStruct()->hour);
  4449. packet->setDataByName("minute", world.GetWorldTimeStruct()->minute);
  4450. packet->setDataByName("unknown", 0);
  4451. packet->setDataByName("unknown7", 1);
  4452. packet->setDataByName("unknown7", 1, 1);
  4453. packet->setDataByName("unknown9", 13);
  4454. //packet->setDataByName("unknown10", 25188959);4294967295
  4455. //packet->setDataByName("unknown10", 25190239);
  4456. packet->setDataByName("unknown10", 25191524);//25191524
  4457. packet->setDataByName("unknown10b", 1);
  4458. 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
  4459. packet->setDataByName("num_adv", 9);
  4460. packet->setArrayDataByName("adv_name", "adv02_dun_drowned_caverns", 0);
  4461. packet->setArrayDataByName("adv_id", 6, 0);
  4462. packet->setArrayDataByName("adv_name", "adv02_dun_sundered_splitpaw_hub", 1);
  4463. packet->setArrayDataByName("adv_id", 5, 1);
  4464. packet->setArrayDataByName("adv_name", "exp03_rgn_butcherblock", 2);
  4465. packet->setArrayDataByName("adv_id", 8, 2);
  4466. packet->setArrayDataByName("adv_name", "exp03_rgn_greater_faydark", 3);
  4467. packet->setArrayDataByName("adv_id", 7, 3);
  4468. packet->setArrayDataByName("adv_name", "mod01_dun_crypt_of_thaen", 4);
  4469. packet->setArrayDataByName("adv_id", 3, 4);
  4470. packet->setArrayDataByName("adv_name", "mod01_dun_tombs_of_night", 5);
  4471. packet->setArrayDataByName("adv_id", 4, 5);
  4472. packet->setArrayDataByName("adv_name", "nektulos_mini01", 6);
  4473. packet->setArrayDataByName("adv_id", 0, 6);
  4474. packet->setArrayDataByName("adv_name", "nektulos_mini02", 7);
  4475. packet->setArrayDataByName("adv_id", 1, 7);
  4476. packet->setArrayDataByName("adv_name", "nektulos_mini03", 8);
  4477. packet->setArrayDataByName("adv_id", 2, 8);
  4478. LogWrite(MISC__TODO, 0, "TODO", "Put cl_ client commands back in variables (not Rules) so they can be dynamically maintained");
  4479. vector<Variable*>* variables = world.GetClientVariables();
  4480. packet->setArrayLengthByName("num_client_setup", variables->size());
  4481. for(int i=variables->size()-1;i>=0;i--)
  4482. packet->setArrayDataByName("client_cmds", variables->at(i)->GetNameValuePair().c_str(), i);
  4483. // For AoM clients so item link work
  4484. if (client->GetVersion() >= 60114)
  4485. packet->setArrayDataByName("client_cmds", "chat_linkbrackets_item 1", variables->size());
  4486. safe_delete(variables);
  4487. //packet->setDataByName("unknown8", ); story?
  4488. // AA Tabs for 1193+ clients
  4489. if (client->GetVersion() >= 1193) {
  4490. packet->setArrayLengthByName("tab_count", 48);
  4491. int8 i = 0;
  4492. packet->setArrayDataByName("tab_index", i, i);
  4493. packet->setArrayDataByName("tab_name", ":es24a58bd8fcaac8c2:All", i);
  4494. i++;
  4495. packet->setArrayDataByName("tab_index", i, i);
  4496. packet->setArrayDataByName("tab_name", ":410385c727bd47a6:Racial Innate", i);
  4497. i++;
  4498. packet->setArrayDataByName("tab_index", i, i);
  4499. packet->setArrayDataByName("tab_name", ":410385c75a96e23c:Tradeskill Advancement", i);
  4500. i++;
  4501. packet->setArrayDataByName("tab_index", i, i);
  4502. packet->setArrayDataByName("tab_name", ":410385c744f1fd99:Focus Effects", i);
  4503. i++;
  4504. packet->setArrayDataByName("tab_index", i, i);
  4505. packet->setArrayDataByName("tab_name", ":410385c71edd2a66:Heroic", i);
  4506. i++;
  4507. packet->setArrayDataByName("tab_index", i, i);
  4508. packet->setArrayDataByName("tab_name", ":410385c76ee6239f:Shadows", i);
  4509. i++;
  4510. packet->setArrayDataByName("tab_index", i, i);
  4511. packet->setArrayDataByName("tab_name", ":410385c7e678b977:Prestige", i);
  4512. i++;
  4513. packet->setArrayDataByName("tab_index", i, i);
  4514. packet->setArrayDataByName("tab_name", ":410385c77ee422d7:Animalist", i);
  4515. i++;
  4516. packet->setArrayDataByName("tab_index", i, i);
  4517. packet->setArrayDataByName("tab_name", ":410385c7f165af77:Bard", i);
  4518. i++;
  4519. packet->setArrayDataByName("tab_index", i, i);
  4520. packet->setArrayDataByName("tab_name", ":410385c7421b9375:Brawler", i);
  4521. i++;
  4522. packet->setArrayDataByName("tab_index", i, i);
  4523. packet->setArrayDataByName("tab_name", ":410385c7a03ae7d1:Cleric", i);
  4524. i++;
  4525. packet->setArrayDataByName("tab_index", i, i);
  4526. packet->setArrayDataByName("tab_name", ":410385c7c9605e9f:Crusader", i);
  4527. i++;
  4528. packet->setArrayDataByName("tab_index", i, i);
  4529. packet->setArrayDataByName("tab_name", ":410385c7f9424168:Druid", i);
  4530. i++;
  4531. packet->setArrayDataByName("tab_index", i, i);
  4532. packet->setArrayDataByName("tab_name", ":410385c79cb9556c:Enchanter", i);
  4533. i++;
  4534. packet->setArrayDataByName("tab_index", i, i);
  4535. packet->setArrayDataByName("tab_name", ":410385c70c8b6aa4:Predator", i);
  4536. i++;
  4537. packet->setArrayDataByName("tab_index", i, i);
  4538. packet->setArrayDataByName("tab_name", ":410385c73a43b6dd:Rogue", i);
  4539. i++;
  4540. packet->setArrayDataByName("tab_index", i, i);
  4541. packet->setArrayDataByName("tab_name", ":410385c759fe7d15:Sorcerer", i);
  4542. i++;
  4543. packet->setArrayDataByName("tab_index", i, i);
  4544. packet->setArrayDataByName("tab_name", ":410385c7ad610aca:Summoner", i);
  4545. i++;
  4546. packet->setArrayDataByName("tab_index", i, i);
  4547. packet->setArrayDataByName("tab_name", ":410385c71e056728:Warrior", i);
  4548. i++;
  4549. packet->setArrayDataByName("tab_index", i, i);
  4550. packet->setArrayDataByName("tab_name", ":410385c7ba864c0b:Assassin", i);
  4551. i++;
  4552. packet->setArrayDataByName("tab_index", i, i);
  4553. packet->setArrayDataByName("tab_name", ":410385c7b8116aad:Beastlord", i);
  4554. i++;
  4555. packet->setArrayDataByName("tab_index", i, i);
  4556. packet->setArrayDataByName("tab_name", ":410385c7f53feb7b:Berserker", i);
  4557. i++;
  4558. packet->setArrayDataByName("tab_index", i, i);
  4559. packet->setArrayDataByName("tab_name", ":410385c73d8a70e2:Brigand", i);
  4560. i++;
  4561. packet->setArrayDataByName("tab_index", i, i);
  4562. packet->setArrayDataByName("tab_name", ":410385c770c766d6:Bruiser", i);
  4563. i++;
  4564. packet->setArrayDataByName("tab_index", i, i);
  4565. packet->setArrayDataByName("tab_name", ":410385c79226984b:Coercer", i);
  4566. i++;
  4567. packet->setArrayDataByName("tab_index", i, i);
  4568. packet->setArrayDataByName("tab_name", ":410385c70c58bb30:Conjurer", i);
  4569. i++;
  4570. packet->setArrayDataByName("tab_index", i, i);
  4571. packet->setArrayDataByName("tab_name", ":410385c73dfe68d0:Defiler", i);
  4572. i++;
  4573. packet->setArrayDataByName("tab_index", i, i);
  4574. packet->setArrayDataByName("tab_name", ":410385c792919a6b:Dirge", i);
  4575. i++;
  4576. packet->setArrayDataByName("tab_index", i, i);
  4577. packet->setArrayDataByName("tab_name", ":410385c7062e5f55:Fury", i);
  4578. i++;
  4579. packet->setArrayDataByName("tab_index", i, i);
  4580. packet->setArrayDataByName("tab_name", ":410385c762c1fdfc:Guardian", i);
  4581. i++;
  4582. packet->setArrayDataByName("tab_index", i, i);
  4583. packet->setArrayDataByName("tab_name", ":410385c78addfbf4:Illusionist", i);
  4584. i++;
  4585. packet->setArrayDataByName("tab_index", i, i);
  4586. packet->setArrayDataByName("tab_name", ":410385c7ece054a7:Inquisitor", i);
  4587. i++;
  4588. packet->setArrayDataByName("tab_index", i, i);
  4589. packet->setArrayDataByName("tab_name", ":410385c7d550d2e7:Monk", i);
  4590. i++;
  4591. packet->setArrayDataByName("tab_index", i, i);
  4592. packet->setArrayDataByName("tab_name", ":410385c743cfeaa2:Mystic", i);
  4593. i++;
  4594. packet->setArrayDataByName("tab_index", i, i);
  4595. packet->setArrayDataByName("tab_name", ":410385c7f63c9c8c:Necromancer", i);
  4596. i++;
  4597. packet->setArrayDataByName("tab_index", i, i);
  4598. packet->setArrayDataByName("tab_name", ":410385c70c5de0ae:Paladin", i);
  4599. i++;
  4600. packet->setArrayDataByName("tab_index", i, i);
  4601. packet->setArrayDataByName("tab_name", ":410385c79bc97b3a:Ranger", i);
  4602. i++;
  4603. packet->setArrayDataByName("tab_index", i, i);
  4604. packet->setArrayDataByName("tab_name", ":410385c78fbd2256:Shadowknight", i);
  4605. i++;
  4606. packet->setArrayDataByName("tab_index", i, i);
  4607. packet->setArrayDataByName("tab_name", ":410385c7781cc625:Shaman", i);
  4608. i++;
  4609. packet->setArrayDataByName("tab_index", i, i);
  4610. packet->setArrayDataByName("tab_name", ":410385c77eecdcdb:Swashbuckler", i);
  4611. i++;
  4612. packet->setArrayDataByName("tab_index", i, i);
  4613. packet->setArrayDataByName("tab_name", ":410385c7648d181e:Templar", i);
  4614. i++;
  4615. packet->setArrayDataByName("tab_index", i, i);
  4616. packet->setArrayDataByName("tab_name", ":410385c78df47d77:Troubador", i);
  4617. i++;
  4618. packet->setArrayDataByName("tab_index", i, i);
  4619. packet->setArrayDataByName("tab_name", ":410385c7c78ce0b8:Warden", i);
  4620. i++;
  4621. packet->setArrayDataByName("tab_index", i, i);
  4622. packet->setArrayDataByName("tab_name", ":410385c76290dcfa:Warlock", i);
  4623. i++;
  4624. packet->setArrayDataByName("tab_index", i, i);
  4625. packet->setArrayDataByName("tab_name", ":410385c7d1d52cf5:Wizard", i);
  4626. i++;
  4627. packet->setArrayDataByName("tab_index", i, i);
  4628. packet->setArrayDataByName("tab_name", ":410385c71c8f6f4d:Shaper", i);
  4629. i++;
  4630. packet->setArrayDataByName("tab_index", i, i);
  4631. packet->setArrayDataByName("tab_name", ":410385c72f6e354d:Channeler", i);
  4632. i++;
  4633. packet->setArrayDataByName("tab_index", i, i);
  4634. packet->setArrayDataByName("tab_name", ":410385c7df8bd37d:Dragon", i);
  4635. }
  4636. packet->setDataByName("unknown_mj", 1);//int8
  4637. packet->setDataByName("unknown_mj1", 335544320);//int32
  4638. packet->setDataByName("unknown_mj2", 4);//int32
  4639. packet->setDataByName("unknown_mj3", 3962504088);//int32
  4640. packet->setDataByName("unknown_mj4", 3985947216);//int32
  4641. packet->setDataByName("unknown_mj5", 1);//int32
  4642. packet->setDataByName("unknown_mj6", 386);//int32
  4643. packet->setDataByName("unknown_mj7", 4294967295);//int32
  4644. packet->setDataByName("unknown_mj8", 2716312211);//int32
  4645. packet->setDataByName("unknown_mj9", 1774338333);//int32
  4646. packet->setDataByName("unknown_mj10", 1);//int32
  4647. packet->setDataByName("unknown_mj11", 391);//int32
  4648. packet->setDataByName("unknown_mj12", 4294967295);//int32
  4649. packet->setDataByName("unknown_mj13", 3168965163);//int32
  4650. packet->setDataByName("unknown_mj14", 4117025286);//int32
  4651. packet->setDataByName("unknown_mj15", 1);//int32
  4652. packet->setDataByName("unknown_mj16", 394);//int32
  4653. packet->setDataByName("unknown_mj17", 4294967295);//int32
  4654. packet->setDataByName("unknown_mj18", 1790669110);//int32
  4655. packet->setDataByName("unknown_mj19", 107158108);//int32
  4656. packet->setDataByName("unknown_mj20", 1);//int32
  4657. packet->setDataByName("unknown_mj21", 393);//int32
  4658. packet->setDataByName("unknown_mj22", 4294967295);//int32
  4659. EQ2Packet* outapp = packet->serialize();
  4660. //packet->PrintPacket();
  4661. //DumpPacket(outapp);
  4662. safe_delete(packet);
  4663. return outapp;
  4664. }
  4665. void ZoneServer::SendUpdateDefaultCommand(Spawn* spawn, const char* command, float distance, Spawn* toPlayer){
  4666. if (spawn == nullptr || command == nullptr)
  4667. return;
  4668. if (toPlayer)
  4669. {
  4670. if (!toPlayer->IsPlayer())
  4671. return;
  4672. Client* client = GetClientBySpawn(toPlayer);
  4673. if (client)
  4674. {
  4675. client->SendDefaultCommand(spawn, command, distance);
  4676. }
  4677. // we don't override the primary command cause that would change ALL clients
  4678. return;
  4679. }
  4680. Client* client = 0;
  4681. PacketStruct* packet = 0;
  4682. vector<Client*>::iterator client_itr;
  4683. MClientList.readlock(__FUNCTION__, __LINE__);
  4684. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4685. client = *client_itr;
  4686. client->SendDefaultCommand(spawn, command, distance);
  4687. }
  4688. if (strlen(command)>0)
  4689. spawn->SetPrimaryCommand(command, command, distance);
  4690. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  4691. }
  4692. void ZoneServer::CheckPlayerProximity(Spawn* spawn, Client* client){
  4693. if (player_proximities.size() < 1)
  4694. return;
  4695. if(player_proximities.count(spawn->GetID()) > 0){
  4696. PlayerProximity* prox = player_proximities.Get(spawn->GetID());
  4697. 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){
  4698. prox->clients_in_proximity[client] = true;
  4699. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->in_range_lua_function.c_str());
  4700. }
  4701. 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){
  4702. if(prox->leaving_range_lua_function.length() > 0)
  4703. CallSpawnScript(spawn, SPAWN_SCRIPT_CUSTOM, client->GetPlayer(), prox->leaving_range_lua_function.c_str());
  4704. prox->clients_in_proximity.erase(client);
  4705. }
  4706. }
  4707. }
  4708. void ZoneServer::AddPlayerProximity(Spawn* spawn, float distance, string in_range_function, string leaving_range_function){
  4709. RemovePlayerProximity(spawn);
  4710. PlayerProximity* prox = new PlayerProximity;
  4711. prox->distance = distance;
  4712. prox->in_range_lua_function = in_range_function;
  4713. prox->leaving_range_lua_function = leaving_range_function;
  4714. player_proximities.Put(spawn->GetID(), prox);
  4715. }
  4716. void ZoneServer::RemovePlayerProximity(Client* client){
  4717. PlayerProximity* prox = 0;
  4718. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  4719. while(itr.Next()){
  4720. prox = itr->second;
  4721. if(prox->clients_in_proximity.count(client) > 0)
  4722. prox->clients_in_proximity.erase(client);
  4723. }
  4724. }
  4725. void ZoneServer::RemovePlayerProximity(Spawn* spawn, bool all){
  4726. if(all){
  4727. MutexMap<int32, PlayerProximity*>::iterator itr = player_proximities.begin();
  4728. while(itr.Next()){
  4729. player_proximities.erase(itr->first, false, true, 10000);
  4730. }
  4731. }
  4732. else if(player_proximities.count(spawn->GetID()) > 0){
  4733. player_proximities.erase(spawn->GetID(), false, true, 10000);
  4734. }
  4735. }
  4736. void ZoneServer::AddLocationProximity(float x, float y, float z, float max_variation, string in_range_function, string leaving_range_function) {
  4737. LocationProximity* prox = new LocationProximity;
  4738. prox->x = x;
  4739. prox->y = y;
  4740. prox->z = z;
  4741. prox->max_variation = max_variation;
  4742. prox->in_range_lua_function = in_range_function;
  4743. prox->leaving_range_lua_function = leaving_range_function;
  4744. location_proximities.Add(prox);
  4745. }
  4746. void ZoneServer::CheckLocationProximity() {
  4747. const char* zone_script = world.GetZoneScript(this->GetZoneID());
  4748. if (!zone_script)
  4749. return;
  4750. if (location_proximities.size() > 0 && connected_clients.size() > 0) {
  4751. Client* client = 0;
  4752. MutexList<Client*>::iterator iterator = connected_clients.begin();
  4753. while(iterator.Next()){
  4754. client = iterator->value;
  4755. if (client->IsConnected() && client->IsReadyForSpawns() && !client->IsZoning()) {
  4756. try {
  4757. MutexList<LocationProximity*>::iterator itr = location_proximities.begin();
  4758. LocationProximity* prox = 0;
  4759. while(itr.Next()){
  4760. prox = itr->value;
  4761. bool in_range = false;
  4762. float char_x = client->GetPlayer()->GetX();
  4763. float char_y = client->GetPlayer()->GetY();
  4764. float char_z = client->GetPlayer()->GetZ();
  4765. float x = prox->x;
  4766. float y = prox->y;
  4767. float z = prox->z;
  4768. float max_variation = prox->max_variation;
  4769. float total_diff = 0;
  4770. float diff = x - char_x; //Check X
  4771. if(diff < 0)
  4772. diff *= -1;
  4773. if(diff <= max_variation) {
  4774. total_diff += diff;
  4775. diff = z - char_z; //Check Z (we check Z first because it is far more likely to be a much greater variation than y)
  4776. if(diff < 0)
  4777. diff *= -1;
  4778. if(diff <= max_variation) {
  4779. total_diff += diff;
  4780. if(total_diff <= max_variation) { //Check Total
  4781. diff = y - char_y; //Check Y
  4782. if(diff < 0)
  4783. diff *= -1;
  4784. if(diff <= max_variation) {
  4785. total_diff += diff;
  4786. if(total_diff <= max_variation) {
  4787. in_range = true;
  4788. if(lua_interface && prox->in_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) == 0) { //Check Total
  4789. prox->clients_in_proximity[client] = true;
  4790. lua_interface->RunZoneScript(zone_script, prox->in_range_lua_function.c_str(), this, client->GetPlayer());
  4791. }
  4792. }
  4793. }
  4794. }
  4795. }
  4796. }
  4797. if (!in_range) {
  4798. if(lua_interface && prox->leaving_range_lua_function.length() > 0 && prox->clients_in_proximity.count(client) > 0) {
  4799. lua_interface->RunZoneScript(zone_script, prox->leaving_range_lua_function.c_str(), this, client->GetPlayer());
  4800. prox->clients_in_proximity.erase(client);
  4801. }
  4802. }
  4803. }
  4804. }
  4805. catch (...) {
  4806. LogWrite(ZONE__ERROR, 0, "Zone", "Except caught in ZoneServer::CheckLocationProximity");
  4807. return;
  4808. }
  4809. }
  4810. }
  4811. }
  4812. }
  4813. void ZoneServer::CheckLocationGrids() {
  4814. if (connected_clients.size() > 0 && location_grids.size() > 0) {
  4815. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  4816. while (client_itr.Next()) {
  4817. Client* client = client_itr.value;
  4818. if (!client)
  4819. continue;
  4820. Player* player = client->GetPlayer();
  4821. float x = player->GetX();
  4822. float y = player->GetY();
  4823. float z = player->GetZ();
  4824. int32 grid_id = player->appearance.pos.grid_id;
  4825. MutexList<LocationGrid*>::iterator location_grid_itr = location_grids.begin();
  4826. while (location_grid_itr.Next()) {
  4827. LocationGrid* grid = location_grid_itr.value;
  4828. if (grid->locations.size() > 0 || /*(grid->grid_id == grid_id ||*/ grid->players.count(player) > 0) {
  4829. float x_small = 0;
  4830. float x_large = 0;
  4831. float y_small = 0;
  4832. float y_large = 0;
  4833. float z_small = 0;
  4834. float z_large = 0;
  4835. bool first = true;
  4836. bool in_grid = false;
  4837. MutexList<Location*>::iterator location_itr = grid->locations.begin();
  4838. while (location_itr.Next()) {
  4839. Location* location = location_itr.value;
  4840. if (first) {
  4841. x_small = location->x;
  4842. x_large = location->x;
  4843. if (grid->include_y) {
  4844. y_small = location->y;
  4845. y_large = location->y;
  4846. }
  4847. z_small = location->z;
  4848. z_large = location->z;
  4849. first = false;
  4850. }
  4851. else {
  4852. if (location->x < x_small)
  4853. x_small = location->x;
  4854. else if (location->x > x_large)
  4855. x_large = location->x;
  4856. if (grid->include_y) {
  4857. if (location->y < y_small)
  4858. y_small = location->y;
  4859. else if (location->y > y_large)
  4860. y_large = location->y;
  4861. }
  4862. if (location->z < z_small)
  4863. z_small = location->z;
  4864. else if (location->z > z_large)
  4865. z_large = location->z;
  4866. }
  4867. }
  4868. if (grid->include_y && (x >= x_small && x <= x_large && y >= y_small && y <= y_large && z >= z_small && z <= z_large))
  4869. in_grid = true;
  4870. else if (x >= x_small && x <= x_large && z >= z_small && z <= z_large)
  4871. in_grid = true;
  4872. if (in_grid && grid->players.count(player) == 0) {
  4873. grid->players.Put(player, true);
  4874. bool show_enter_location_popup = true;
  4875. bool discovery_enabled = rule_manager.GetGlobalRule(R_World, EnablePOIDiscovery)->GetBool();
  4876. if( grid->discovery && discovery_enabled && !player->DiscoveredLocation(grid->id) )
  4877. {
  4878. // check if player has already discovered this location
  4879. // if not, process new discovery
  4880. char tmp[200] = {0};
  4881. sprintf(tmp, "\\#FFE400You have discovered\12\\#FFF283%s", grid->name.c_str());
  4882. client->SendPopupMessage(11, tmp, "ui_discovery", 2.25, 0xFF, 0xFF, 0xFF);
  4883. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' discovered location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  4884. player->UpdatePlayerHistory(HISTORY_TYPE_DISCOVERY, HISTORY_SUBTYPE_LOCATION, grid->id);
  4885. show_enter_location_popup = false;
  4886. // else, print standard location entry
  4887. }
  4888. if( show_enter_location_popup )
  4889. {
  4890. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' entering location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  4891. client->SendPopupMessage(10, grid->name.c_str(), 0, 2.5, 255, 255, 0);
  4892. }
  4893. }
  4894. else if (!in_grid && grid->players.count(player) > 0) {
  4895. LogWrite(ZONE__DEBUG, 0, "Zone", "Player '%s' leaving location '%s' (%u)", player->GetName(), grid->name.c_str(), grid->id);
  4896. grid->players.erase(player);
  4897. }
  4898. }
  4899. }
  4900. }
  4901. }
  4902. }
  4903. // Called from a command (client, main zone thread) and the main zone thread
  4904. // so no need for a mutex container
  4905. void ZoneServer::AddLocationGrid(LocationGrid* grid) {
  4906. if (grid)
  4907. location_grids.Add(grid);
  4908. }
  4909. void ZoneServer::RemoveLocationGrids() {
  4910. MutexList<LocationGrid*>::iterator itr = location_grids.begin();
  4911. while (itr.Next())
  4912. itr.value->locations.clear(true);
  4913. location_grids.clear(true);
  4914. }
  4915. void ZoneServer::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast){
  4916. if(spellProcess)
  4917. spellProcess->RemoveSpellTimersFromSpawn(spawn, remove_all, delete_recast);
  4918. }
  4919. void ZoneServer::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement){
  4920. if(spellProcess)
  4921. spellProcess->Interrupted(caster, interruptor, error_code, cancel, from_movement);
  4922. }
  4923. Spell* ZoneServer::GetSpell(Entity* caster){
  4924. Spell* spell = 0;
  4925. if(spellProcess)
  4926. spell = spellProcess->GetSpell(caster);
  4927. return spell;
  4928. }
  4929. void ZoneServer::ProcessSpell(Spell* spell, Entity* caster, Spawn* target, bool lock, bool harvest_spell, LuaSpell* customSpell, int16 custom_cast_time, bool in_heroic_opp){
  4930. if(spellProcess)
  4931. spellProcess->ProcessSpell(this, spell, caster, target, lock, harvest_spell, customSpell, custom_cast_time, in_heroic_opp);
  4932. }
  4933. void ZoneServer::ProcessEntityCommand(EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock) {
  4934. if (target && target->GetSpawnScript()) {
  4935. Player* player = 0;
  4936. if (caster && caster->IsPlayer())
  4937. player = (Player*)caster;
  4938. CallSpawnScript(target, SPAWN_SCRIPT_CUSTOM, player, entity_command->command.c_str());
  4939. }
  4940. if (spellProcess)
  4941. spellProcess->ProcessEntityCommand(this, entity_command, caster, target, lock);
  4942. }
  4943. void ZoneServer::RemoveSpawnSupportFunctions(Spawn* spawn) {
  4944. if(!spawn)
  4945. return;
  4946. LogWrite(ZONE__DEBUG, 7, "Zone", "Processing RemoveSpawnSupportFunctions...");
  4947. if(spawn->IsEntity())
  4948. RemoveSpellTimersFromSpawn((Entity*)spawn, true);
  4949. RemoveDamagedSpawn(spawn);
  4950. spawn->SendSpawnChanges(false);
  4951. RemoveChangedSpawn(spawn);
  4952. // Everything inside this if will be nuked during a reload in other spots, no need to do it twice
  4953. if (!reloading) {
  4954. RemoveDeadEnemyList(spawn);
  4955. spawn->changed = true;
  4956. spawn->info_changed = true;
  4957. spawn->vis_changed = true;
  4958. spawn->position_changed = true;
  4959. SendSpawnChanges(spawn);
  4960. if (spawn->GetSpawnGroupID() > 0) {
  4961. int32 group_id = spawn->GetSpawnGroupID();
  4962. spawn->RemoveSpawnFromGroup();
  4963. if (spawn_group_map.count(group_id) > 0)
  4964. spawn_group_map.Get(group_id).Remove(spawn->GetID());
  4965. }
  4966. if (!spawn->IsPlayer()) {
  4967. if(quick_database_id_lookup.count(spawn->GetDatabaseID()) > 0)
  4968. quick_database_id_lookup.erase(spawn->GetDatabaseID());
  4969. }
  4970. RemoveHeadingTimer(spawn);
  4971. DeleteSpawnScriptTimers(spawn);
  4972. RemovePlayerProximity(spawn);
  4973. }
  4974. // We don't use RemoveMovementNPC() here as it caused a hell of a delay during reloads
  4975. // instead we remove it from the list directly
  4976. if (spawn->IsNPC())
  4977. movement_spawns.erase(spawn->GetID());
  4978. }
  4979. void ZoneServer::HandleEmote(Client* originator, string name) {
  4980. if (!originator) {
  4981. LogWrite(ZONE__ERROR, 0, "Zone", "HandleEmote called with an invalid client");
  4982. return;
  4983. }
  4984. Client* client = 0;
  4985. Emote* origEmote = visual_states.FindEmote(name, originator->GetVersion());
  4986. if(!origEmote){
  4987. 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());
  4988. return;
  4989. }
  4990. Emote* emote = origEmote;
  4991. PacketStruct* packet = 0;
  4992. char* emoteResponse = 0;
  4993. vector<Client*>::iterator client_itr;
  4994. int32 cur_client_version = originator->GetVersion();
  4995. map<int32, Emote*> emote_version_range;
  4996. MClientList.readlock(__FUNCTION__, __LINE__);
  4997. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  4998. client = *client_itr;
  4999. if(!client || (client && client->GetPlayer()->IsIgnored(originator->GetPlayer()->GetName())))
  5000. continue;
  5001. // establish appropriate emote for the version used by the client
  5002. if (client->GetVersion() != originator->GetVersion())
  5003. {
  5004. map<int32, Emote*>::iterator rangeitr = emote_version_range.find(client->GetVersion());
  5005. if (rangeitr == emote_version_range.end())
  5006. {
  5007. Emote* tmp_new_emote = visual_states.FindEmote(name, client->GetVersion());
  5008. if (tmp_new_emote)
  5009. {
  5010. emote_version_range.insert(make_pair(client->GetVersion(), tmp_new_emote));
  5011. emote = tmp_new_emote;
  5012. } // else its missing just use the current clients default
  5013. }
  5014. else // we have an existing emote already cached
  5015. emote = rangeitr->second;
  5016. }
  5017. else // since the client and originator client match use the original emote
  5018. emote = origEmote;
  5019. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  5020. if(packet){
  5021. packet->setDataByName("spawn_id" , client->GetPlayer()->GetIDWithPlayerSpawn(originator->GetPlayer()));
  5022. if(!emoteResponse){
  5023. string message;
  5024. if(originator->GetPlayer()->GetTarget() && originator->GetPlayer()->GetTarget()->GetID() != originator->GetPlayer()->GetID()){
  5025. message = emote->GetTargetedMessageString();
  5026. if(message.find("%t") < 0xFFFFFFFF)
  5027. message.replace(message.find("%t"), 2, originator->GetPlayer()->GetTarget()->GetName());
  5028. }
  5029. if(message.length() == 0)
  5030. message = emote->GetMessageString();
  5031. if(message.find("%g1") < 0xFFFFFFFF){
  5032. if(originator->GetPlayer()->GetGender() == 1)
  5033. message.replace(message.find("%g1"), 3, "his");
  5034. else
  5035. message.replace(message.find("%g1"), 3, "her");
  5036. }
  5037. if(message.find("%g2") < 0xFFFFFFFF){
  5038. if(originator->GetPlayer()->GetGender() == 1)
  5039. message.replace(message.find("%g2"), 3, "him");
  5040. else
  5041. message.replace(message.find("%g2"), 3, "her");
  5042. }
  5043. if(message.find("%g3") < 0xFFFFFFFF){
  5044. if(originator->GetPlayer()->GetGender() == 1)
  5045. message.replace(message.find("%g3"), 3, "he");
  5046. else
  5047. message.replace(message.find("%g3"), 3, "she");
  5048. }
  5049. if(message.length() > 0){
  5050. emoteResponse = new char[message.length() + strlen(originator->GetPlayer()->GetName()) + 10];
  5051. sprintf(emoteResponse,"%s %s", originator->GetPlayer()->GetName(), message.c_str());
  5052. }
  5053. else{
  5054. originator->Message(CHANNEL_COLOR_YELLOW, "%s is not properly configured, be sure to submit a /bug report.", name.c_str());
  5055. safe_delete(packet);
  5056. break;
  5057. }
  5058. }
  5059. packet->setMediumStringByName("emote_msg", emoteResponse);
  5060. packet->setDataByName("anim_type", emote->GetVisualState());
  5061. client->QueuePacket(packet->serialize());
  5062. safe_delete(packet);
  5063. safe_delete_array(emoteResponse);
  5064. }
  5065. }
  5066. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5067. }
  5068. void ZoneServer::SetupInstance(int32 createdInstanceID) {
  5069. if ( createdInstanceID == 0 ) // if this happens that isn't good!
  5070. instanceID = ++MinInstanceID;
  5071. else // db should pass the good ID
  5072. instanceID = createdInstanceID;
  5073. }
  5074. void ZoneServer::RemoveDeadSpawn(Spawn* spawn){
  5075. AddDeadSpawn(spawn, 0);
  5076. }
  5077. void ZoneServer::AddDeadSpawn(Spawn* spawn, int32 timer){
  5078. MDeadSpawns.writelock(__FUNCTION__, __LINE__);
  5079. if (dead_spawns.count(spawn->GetID()) > 0)
  5080. dead_spawns[spawn->GetID()] = Timer::GetCurrentTime2() + timer;
  5081. else if(timer != 0xFFFFFFFF)
  5082. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + timer));
  5083. else{
  5084. if(spawn->IsEntity() && spawn->HasLoot()){
  5085. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + (15000 * spawn->GetLevel() + 240000)));
  5086. SendUpdateDefaultCommand(spawn, "loot", 10);
  5087. }
  5088. else
  5089. dead_spawns.insert(make_pair(spawn->GetID(), Timer::GetCurrentTime2() + 10000));
  5090. }
  5091. MDeadSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5092. }
  5093. void ZoneServer::WritePlayerStatistics() {
  5094. MutexList<Client*>::iterator client_itr = connected_clients.begin();
  5095. while(client_itr.Next())
  5096. client_itr->value->GetPlayer()->WritePlayerStatistics();
  5097. }
  5098. bool ZoneServer::SendRadiusSpawnInfo(Client* client, float radius) {
  5099. if (!client)
  5100. return false;
  5101. Spawn* spawn = 0;
  5102. bool ret = false;
  5103. map<int32, Spawn*>::iterator itr;
  5104. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5105. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5106. spawn = itr->second;
  5107. if (spawn && spawn != client->GetPlayer() && !spawn->IsPlayer() && spawn->GetDistance(client->GetPlayer()) <= radius) {
  5108. const char* type = "NPC";
  5109. const char* specialTypeID = "N/A";
  5110. int32 specialID = 0, spawnEntryID = spawn->GetSpawnEntryID();
  5111. if (spawn->IsObject())
  5112. {
  5113. Object* obj = (Object*)spawn;
  5114. specialID = obj->GetID();
  5115. specialTypeID = "GetID";
  5116. type = "Object";
  5117. }
  5118. else if (spawn->IsSign())
  5119. {
  5120. Sign* sign = (Sign*)spawn;
  5121. specialID = sign->GetWidgetID();
  5122. specialTypeID = "WidgetID";
  5123. type = "Sign";
  5124. }
  5125. else if (spawn->IsWidget())
  5126. {
  5127. Widget* widget = (Widget*)spawn;
  5128. specialID = widget->GetWidgetID();
  5129. specialTypeID = "WidgetID";
  5130. if ( specialID == 0xFFFFFFFF )
  5131. specialTypeID = "WidgetID(spawn_widgets entry missing)";
  5132. type = "Widget";
  5133. }
  5134. else if (spawn->IsGroundSpawn())
  5135. {
  5136. GroundSpawn* gs = (GroundSpawn*)spawn;
  5137. specialID = gs->GetGroundSpawnEntryID();
  5138. specialTypeID = "GroundSpawnEntryID";
  5139. type = "GroundSpawn";
  5140. }
  5141. client->Message(CHANNEL_COLOR_RED, "Name: %s (%s), Spawn Table ID: %u, %s: %u", spawn->GetName(), type, spawn->GetDatabaseID(), specialTypeID, specialID);
  5142. 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());
  5143. 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());
  5144. client->Message(CHANNEL_COLOR_YELLOW, "=============================");
  5145. ret = true;
  5146. }
  5147. }
  5148. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5149. return ret;
  5150. }
  5151. void ZoneServer::FindSpawn(Client* client, char* regSearchStr)
  5152. {
  5153. if (!regSearchStr || strlen(regSearchStr) < 1)
  5154. {
  5155. client->SimpleMessage(CHANNEL_COLOR_RED, "Bad ZoneServer::FindSpawn(Client*, char* regSearchStr) attempt, regSearchStr is NULL or empty.");
  5156. return;
  5157. }
  5158. string resString = string(regSearchStr);
  5159. try
  5160. {
  5161. std::regex pre_re_check("^[a-zA-Z0-9_ ]+$");
  5162. bool output = std::regex_match(resString, pre_re_check);
  5163. if (output)
  5164. {
  5165. string newStr(".*");
  5166. newStr.append(regSearchStr);
  5167. newStr.append(".*");
  5168. resString = newStr;
  5169. }
  5170. }
  5171. catch (...)
  5172. {
  5173. client->SimpleMessage(CHANNEL_COLOR_RED, "Try/Catch ZoneServer::FindSpawn(Client*, char* regSearchStr) failure.");
  5174. return;
  5175. }
  5176. client->Message(CHANNEL_NARRATIVE, "RegEx Search Spawn List: %s", regSearchStr);
  5177. client->Message(CHANNEL_NARRATIVE, "Database ID | Spawn Name | X , Y , Z");
  5178. client->Message(CHANNEL_NARRATIVE, "========================");
  5179. map<int32, Spawn*>::iterator itr;
  5180. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5181. int32 spawnsFound = 0;
  5182. std::regex re(resString, std::regex_constants::icase);
  5183. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5184. Spawn* spawn = itr->second;
  5185. if (!spawn || !spawn->GetName())
  5186. continue;
  5187. bool output = false;
  5188. try {
  5189. output = std::regex_match(string(spawn->GetName()), re);
  5190. }
  5191. catch (...)
  5192. {
  5193. continue;
  5194. }
  5195. if (output)
  5196. {
  5197. client->Message(CHANNEL_NARRATIVE, "%i | %s | %f , %f , %f", spawn->GetDatabaseID(), spawn->GetName(), spawn->GetX(), spawn->GetY(), spawn->GetZ());
  5198. spawnsFound++;
  5199. }
  5200. }
  5201. client->Message(CHANNEL_NARRATIVE, "========================", spawnsFound);
  5202. client->Message(CHANNEL_NARRATIVE, "%u Results Found.", spawnsFound);
  5203. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5204. }
  5205. void ZoneServer::AddPlayerTracking(Player* player) {
  5206. if (player && !player->GetIsTracking() && players_tracking.count(player->GetDatabaseID()) == 0) {
  5207. Client* client = GetClientBySpawn(player);
  5208. if (client) {
  5209. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5210. if (packet) {
  5211. player->SetIsTracking(true);
  5212. players_tracking.Put(client->GetCharacterID(), player);
  5213. packet->setDataByName("mode", TRACKING_START);
  5214. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5215. client->QueuePacket(packet->serialize());
  5216. safe_delete(packet);
  5217. }
  5218. }
  5219. }
  5220. }
  5221. void ZoneServer::RemovePlayerTracking(Player* player, int8 mode) {
  5222. if (player && player->GetIsTracking()) {
  5223. Client* client = GetClientBySpawn(player);
  5224. if (client) {
  5225. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5226. if (packet) {
  5227. player->SetIsTracking(false);
  5228. players_tracking.erase(client->GetCharacterID());
  5229. packet->setDataByName("mode", mode);
  5230. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5231. client->QueuePacket(packet->serialize());
  5232. safe_delete(packet);
  5233. }
  5234. }
  5235. }
  5236. }
  5237. void ZoneServer::ProcessTracking() {
  5238. MutexMap<int32, Player*>::iterator itr = players_tracking.begin();
  5239. while (itr.Next())
  5240. ProcessTracking(GetClientBySpawn(itr->second));
  5241. }
  5242. void ZoneServer::ProcessTracking(Client* client) {
  5243. if (!client)
  5244. return;
  5245. Player* player = client->GetPlayer();
  5246. if (player && player->GetIsTracking()) {
  5247. MutexMap<int32, Spawn*>::iterator spawn_itr;
  5248. PacketStruct* packet = configReader.getStruct("WS_TrackingUpdate", client->GetVersion());
  5249. if (packet) {
  5250. packet->setDataByName("mode", TRACKING_UPDATE);
  5251. packet->setDataByName("type", TRACKING_TYPE_ENTITIES);
  5252. vector<TrackedSpawn*> spawns_tracked;
  5253. while (spawn_itr.Next()) {
  5254. Spawn* spawn = spawn_itr->second;
  5255. float distance = player->GetDistance(spawn);
  5256. if (spawn->IsEntity() && distance <= 80 && spawn != player) {
  5257. TrackedSpawn* ts = new TrackedSpawn;
  5258. ts->spawn = spawn;
  5259. ts->distance = distance;
  5260. /* Add spawns in ascending order from closest to furthest */
  5261. if (spawns_tracked.empty())
  5262. spawns_tracked.push_back(ts);
  5263. else {
  5264. vector<TrackedSpawn*>::iterator tracked_itr;
  5265. bool added = false;
  5266. for (tracked_itr = spawns_tracked.begin(); tracked_itr != spawns_tracked.end(); tracked_itr++) {
  5267. TrackedSpawn* cur_ts = *tracked_itr;
  5268. if (ts->distance <= cur_ts->distance) {
  5269. spawns_tracked.insert(tracked_itr, ts);
  5270. added = true;
  5271. break;
  5272. }
  5273. }
  5274. if (!added)
  5275. spawns_tracked.push_back(ts);
  5276. }
  5277. }
  5278. }
  5279. packet->setArrayLengthByName("num_spawns", spawns_tracked.size());
  5280. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5281. TrackedSpawn* ts = spawns_tracked[i];
  5282. LogWrite(ZONE__DEBUG, 0, "Zone", "%s (%f)", ts->spawn->GetName(), ts->distance);
  5283. packet->setArrayDataByName("spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5284. packet->setArrayDataByName("spawn_name", ts->spawn->GetName(), i);
  5285. if (ts->spawn->IsPlayer())
  5286. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_PC, i);
  5287. else
  5288. packet->setArrayDataByName("spawn_type", TRACKING_SPAWN_TYPE_NPC, i);
  5289. packet->setArrayDataByName("spawn_con_color", player->GetArrowColor(ts->spawn->GetLevel()), i);
  5290. }
  5291. packet->setArrayLengthByName("num_array1", 0);
  5292. //for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5293. //}
  5294. packet->setArrayLengthByName("num_spawns2", spawns_tracked.size());
  5295. for (int32 i = 0; i < spawns_tracked.size(); i++) {
  5296. TrackedSpawn* ts = spawns_tracked[i];
  5297. packet->setArrayDataByName("list_spawn_id", player->GetIDWithPlayerSpawn(ts->spawn), i);
  5298. packet->setArrayDataByName("list_number", i, i);
  5299. }
  5300. client->QueuePacket(packet->serialize());
  5301. safe_delete(packet);
  5302. for (int32 i = 0; i < spawns_tracked.size(); i++)
  5303. safe_delete(spawns_tracked[i]);
  5304. }
  5305. }
  5306. }
  5307. void ZoneServer::SendEpicMobDeathToGuild(Player* killer, Spawn* victim) {
  5308. if (killer && victim) {
  5309. LogWrite(MISC__TODO, 1, "TODO" , "Check if player is in raid\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  5310. if (killer->GetGroupMemberInfo()) {
  5311. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  5312. deque<GroupMemberInfo*>::iterator itr;
  5313. PlayerGroup* group = world.GetGroupManager()->GetGroup(killer->GetGroupMemberInfo()->group_id);
  5314. if (group)
  5315. {
  5316. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  5317. deque<GroupMemberInfo*>* members = group->GetMembers();
  5318. for (itr = members->begin(); itr != members->end(); itr++) {
  5319. GroupMemberInfo* gmi = *itr;
  5320. if (gmi->client) {
  5321. Player* group_member = gmi->client->GetPlayer();
  5322. if (group_member->GetGuild()) {
  5323. Guild* guild = group_member->GetGuild();
  5324. string message = Guild::GetEpicMobDeathMessage(group_member->GetName(), victim->GetName());
  5325. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5326. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5327. }
  5328. }
  5329. }
  5330. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  5331. }
  5332. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  5333. }
  5334. else if (killer->GetGuild()) {
  5335. Guild* guild = killer->GetGuild();
  5336. string message = Guild::GetEpicMobDeathMessage(killer->GetName(), victim->GetName());
  5337. guild->AddNewGuildEvent(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str(), Timer::GetUnixTimeStamp());
  5338. guild->SendMessageToGuild(GUILD_EVENT_KILLS_EPIC_MONSTER, message.c_str());
  5339. }
  5340. }
  5341. }
  5342. void ZoneServer::ProcessAggroChecks(Spawn* spawn) {
  5343. if (spawn->GetFactionID() < 1 || spawn->EngagedInCombat())
  5344. return;
  5345. // If faction based combat is not allowed then no need to run the loops so just return out
  5346. if(!rule_manager.GetGlobalRule(R_Faction, AllowFactionBasedCombat)->GetBool())
  5347. return;
  5348. if (spawn && spawn->IsNPC() && spawn->Alive())
  5349. CheckEnemyList((NPC*)spawn);
  5350. }
  5351. void ZoneServer::SendUpdateTitles(Client* client, Title* suffix, Title* prefix) {
  5352. assert(client);
  5353. if (client->GetVersion() > 546)
  5354. SendUpdateTitles(client->GetPlayer(), suffix, prefix);
  5355. }
  5356. void ZoneServer::SendUpdateTitles(Spawn *spawn, Title *suffix, Title *prefix) {
  5357. if (!spawn)
  5358. return;
  5359. vector<Client*>::iterator itr;
  5360. PacketStruct *packet;
  5361. Client* current_client;
  5362. MClientList.readlock(__FUNCTION__, __LINE__);
  5363. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5364. current_client = *itr;
  5365. if (current_client->GetVersion() <= 546)
  5366. continue;
  5367. if (!(packet = configReader.getStruct("WS_UpdateTitle", current_client->GetVersion())))
  5368. continue;
  5369. packet->setDataByName("player_id", current_client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  5370. packet->setDataByName("player_name", spawn->GetName());
  5371. packet->setDataByName("unknown1", 1, 1);
  5372. if(suffix)
  5373. packet->setDataByName("suffix_title", suffix->GetName());
  5374. else
  5375. packet->setDataByName("suffix_title", spawn->GetSuffixTitle());
  5376. if(prefix)
  5377. packet->setDataByName("prefix_title", prefix->GetName());
  5378. else
  5379. packet->setDataByName("prefix_title", spawn->GetPrefixTitle());
  5380. packet->setDataByName("last_name", spawn->GetLastName());
  5381. packet->setDataByName("sub_title", spawn->GetSubTitle());
  5382. current_client->QueuePacket(packet->serialize());
  5383. safe_delete(packet);
  5384. }
  5385. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5386. }
  5387. void ZoneServer::AddTransportSpawn(Spawn* spawn){
  5388. if(!spawn)
  5389. return;
  5390. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5391. transport_spawns.push_back(spawn->GetID());
  5392. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5393. }
  5394. Spawn* ZoneServer::GetClosestTransportSpawn(float x, float y, float z){
  5395. Spawn* spawn = 0;
  5396. Spawn* closest_spawn = 0;
  5397. float closest_distance = 0.0;
  5398. MTransportSpawns.writelock(__FUNCTION__, __LINE__);
  5399. vector<int32>::iterator itr = transport_spawns.begin();
  5400. while(itr != transport_spawns.end()){
  5401. spawn = GetSpawnByID(*itr);
  5402. if(spawn){
  5403. if(closest_distance == 0.0){
  5404. closest_spawn = spawn;
  5405. closest_distance = spawn->GetDistance(x, y, z);
  5406. }
  5407. else if(spawn->GetDistance(x, y, z) < closest_distance){
  5408. closest_spawn = spawn;
  5409. closest_distance = spawn->GetDistance(x, y, z);
  5410. }
  5411. itr++;
  5412. }
  5413. else
  5414. itr = transport_spawns.erase(itr);
  5415. }
  5416. MTransportSpawns.releasewritelock(__FUNCTION__, __LINE__);
  5417. return closest_spawn;
  5418. }
  5419. void ZoneServer::SetRain(float val) {
  5420. rain = val;
  5421. vector<Client*>::iterator itr;
  5422. MClientList.readlock(__FUNCTION__, __LINE__);
  5423. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5424. Client* client = *itr;
  5425. client->GetPlayer()->GetInfoStruct()->set_rain(val);
  5426. client->GetPlayer()->SetCharSheetChanged(true);
  5427. if( val >= 0.75 && !weather_signaled )
  5428. {
  5429. client->SimpleMessage(CHANNEL_NARRATIVE, "It starts to rain.");
  5430. }
  5431. else if( val < 0.75 && weather_signaled )
  5432. {
  5433. client->SimpleMessage(CHANNEL_NARRATIVE, "It stops raining.");
  5434. }
  5435. }
  5436. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5437. if (val >= 0.75 && !weather_signaled) {
  5438. weather_signaled = true;
  5439. ProcessSpawnConditional(SPAWN_CONDITIONAL_RAINING);
  5440. }
  5441. else if (val < 0.75 && weather_signaled) {
  5442. weather_signaled = false;
  5443. ProcessSpawnConditional(SPAWN_CONDITIONAL_NOT_RAINING);
  5444. }
  5445. }
  5446. void ZoneServer::SetWind(float val) {
  5447. vector<Client*>::iterator itr;
  5448. MClientList.readlock(__FUNCTION__, __LINE__);
  5449. for (itr = clients.begin(); itr != clients.end(); itr++) {
  5450. Client* client = *itr;
  5451. client->GetPlayer()->GetInfoStruct()->set_wind(val);
  5452. client->GetPlayer()->SetCharSheetChanged(true);
  5453. }
  5454. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5455. }
  5456. void ZoneServer::ProcessWeather()
  5457. {
  5458. // if the global rule to disable weather is set, or if the `weather_allowed` field in the zone record == 0, do not process weather
  5459. if( !weather_enabled || !isWeatherAllowed() )
  5460. return;
  5461. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Processing weather changes", zone_name);
  5462. float new_weather = 0;
  5463. float weather_offset = 0;
  5464. bool change_weather = false;
  5465. // check to see if it is time to change the weather according to weather_frequency (time between changes)
  5466. if( weather_last_changed_time <= (Timer::GetUnixTimeStamp() - weather_frequency) )
  5467. {
  5468. LogWrite(ZONE__DEBUG, 2, "Zone", "%s: Checking for weather changes", zone_name);
  5469. // reset last changed time (frequency check)
  5470. weather_last_changed_time = Timer::GetUnixTimeStamp();
  5471. // this is the chance a weather change occurs at all at the expired interval
  5472. int8 weather_random = MakeRandomInt(1, 100);
  5473. 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");
  5474. if( weather_random <= weather_change_chance )
  5475. {
  5476. change_weather = true;
  5477. weather_offset = weather_change_amount;
  5478. if( weather_type == 3 ) // chaotic weather patterns, random weather between min/max
  5479. {
  5480. new_weather = MakeRandomFloat(weather_min_severity, weather_max_severity);
  5481. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Chaotic weather severity changed to %.2f", zone_name, new_weather);
  5482. weather_pattern = 2;
  5483. }
  5484. else if( weather_type == 2 ) // random weather patterns, combination of normal + dynamic + max_offset
  5485. {
  5486. weather_offset = MakeRandomFloat(weather_change_amount, weather_dynamic_offset);
  5487. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Random weather severity changed by %.2f", zone_name, weather_offset);
  5488. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  5489. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  5490. if( weather_random <= weather_alter )
  5491. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  5492. }
  5493. else if( weather_type == 1 ) // dynamic weather patterns, weather may not reach min/max
  5494. {
  5495. int8 weather_alter = weather_change_chance / 10; // the divide is to prevent too many direction changes in a cycle
  5496. weather_random = MakeRandomInt(1, 100); // chance that the weather changes direction (weather_pattern)
  5497. if( weather_random <= weather_alter )
  5498. {
  5499. weather_pattern = ( weather_pattern == 0 ) ? 1 : 0;
  5500. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Dynamic weather pattern changed to %i", zone_name, weather_pattern);
  5501. }
  5502. }
  5503. else // normal weather patterns, weather starts at min, goes to max, then back down again
  5504. {
  5505. // do nothing (processed below)
  5506. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Normal weather severity changed by %.2f", zone_name, weather_offset);
  5507. }
  5508. // when all done, change the weather
  5509. if( change_weather )
  5510. {
  5511. if( weather_pattern == 1 )
  5512. {
  5513. // weather is getting worse, til it reaches weather_max_severity
  5514. new_weather = ( weather_current_severity <= weather_max_severity ) ? weather_current_severity + weather_offset : weather_max_severity;
  5515. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Increased weather severity by %.2f", zone_name, weather_offset);
  5516. if(new_weather > weather_max_severity)
  5517. {
  5518. new_weather = weather_max_severity - weather_offset;
  5519. weather_pattern = 0;
  5520. }
  5521. }
  5522. else if( weather_pattern == 0 )
  5523. {
  5524. // weather is clearing up, til it reaches weather_min_severity
  5525. new_weather = ( weather_current_severity >= weather_min_severity ) ? weather_current_severity - weather_offset : weather_min_severity;
  5526. LogWrite(ZONE__DEBUG, 3, "Zone", "%s: Decreased weather severity by %.2f", zone_name, weather_offset);
  5527. if(new_weather < weather_min_severity)
  5528. {
  5529. new_weather = weather_min_severity + weather_offset;
  5530. weather_pattern = 1;
  5531. }
  5532. }
  5533. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Weather change triggered from %.2f to %.2f", zone_name, weather_current_severity, new_weather);
  5534. this->SetRain(new_weather);
  5535. weather_current_severity = new_weather;
  5536. }
  5537. }
  5538. }
  5539. else
  5540. LogWrite(ZONE__DEBUG, 1, "Zone", "%s: Not time to change weather yet", zone_name);
  5541. }
  5542. void ZoneServer::HidePrivateSpawn(Spawn* spawn) {
  5543. if (!spawn->IsPrivateSpawn())
  5544. return;
  5545. Client* client = 0;
  5546. Player* player = 0;
  5547. PacketStruct* packet = 0;
  5548. int32 packet_version = 0;
  5549. MutexList<Client*>::iterator itr = connected_clients.begin();
  5550. while (itr->Next()) {
  5551. client = itr->value;
  5552. player = client->GetPlayer();
  5553. if (player->WasSentSpawn(spawn->GetID()) && !player->WasSpawnRemoved(spawn)) {
  5554. if (!packet || packet_version != client->GetVersion()) {
  5555. safe_delete(packet);
  5556. packet_version = client->GetVersion();
  5557. packet = configReader.getStruct("WS_DestroyGhostCmd", packet_version);
  5558. }
  5559. SendRemoveSpawn(client, spawn, packet);
  5560. if(spawn_range_map.count(client) > 0)
  5561. spawn_range_map.Get(client)->erase(spawn->GetID());
  5562. if(player->GetTarget() == spawn)
  5563. player->SetTarget(0);
  5564. }
  5565. }
  5566. safe_delete(packet);
  5567. }
  5568. SpawnLocation* ZoneServer::GetSpawnLocation(int32 id) {
  5569. SpawnLocation* ret = 0;
  5570. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  5571. if (spawn_location_list.count(id) > 0)
  5572. ret = spawn_location_list[id];
  5573. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  5574. return ret;
  5575. }
  5576. void ZoneServer::PlayAnimation(Spawn* spawn, int32 visual_state, Spawn* spawn2, int8 hide_type){
  5577. Client* client = 0;
  5578. PacketStruct* packet = 0;
  5579. Spawn* exclude_spawn = 0;
  5580. if (!spawn)
  5581. return;
  5582. if (spawn2){
  5583. if(hide_type == 1){
  5584. client = GetClientBySpawn(spawn2);
  5585. if(client){
  5586. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  5587. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  5588. packet->setDataByName("anim_type", visual_state);
  5589. client->QueuePacket(packet->serialize());
  5590. }
  5591. return;
  5592. }
  5593. if(hide_type == 2)
  5594. exclude_spawn = spawn2;
  5595. }
  5596. vector<Client*>::iterator client_itr;
  5597. MClientList.readlock(__FUNCTION__, __LINE__);
  5598. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++) {
  5599. client = *client_itr;
  5600. if(spawn->GetDistance(client->GetPlayer()) > 50)
  5601. continue;
  5602. if(exclude_spawn == client->GetPlayer())
  5603. continue;
  5604. packet = configReader.getStruct("WS_CannedEmote", client->GetVersion());
  5605. if (packet) {
  5606. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(spawn));
  5607. packet->setDataByName("anim_type", visual_state);
  5608. client->QueuePacket(packet->serialize());
  5609. safe_delete(packet);
  5610. }
  5611. }
  5612. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5613. }
  5614. vector<Spawn*> ZoneServer::GetSpawnsByID(int32 id) {
  5615. vector<Spawn*> tmp_list;
  5616. Spawn* spawn;
  5617. map<int32, Spawn*>::iterator itr;
  5618. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5619. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5620. spawn = itr->second;
  5621. if (spawn && (spawn->GetDatabaseID() == id))
  5622. tmp_list.push_back(spawn);
  5623. }
  5624. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5625. return tmp_list;
  5626. }
  5627. vector<Spawn*> ZoneServer::GetAttackableSpawnsByDistance(Spawn* caster, float distance) {
  5628. vector<Spawn*> ret;
  5629. Spawn* spawn = 0;
  5630. map<int32, Spawn*>::iterator itr;
  5631. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5632. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5633. spawn = itr->second;
  5634. if (spawn && spawn->IsNPC() && spawn->appearance.attackable > 0 && spawn != caster && spawn->Alive() && spawn->GetDistance(caster, true) <= distance)
  5635. ret.push_back(spawn);
  5636. }
  5637. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5638. return ret;
  5639. }
  5640. void ZoneServer::ResurrectSpawn(Spawn* spawn, Client* client) {
  5641. if(!client || !spawn)
  5642. return;
  5643. PendingResurrection* rez = client->GetCurrentRez();
  5644. if(!rez || !rez->caster)
  5645. return;
  5646. PacketStruct* packet = 0;
  5647. float power_perc = rez->mp_perc;
  5648. float health_perc = rez->hp_perc;
  5649. Spawn* caster_spawn = rez->caster;
  5650. sint32 heal_amt = 0;
  5651. sint32 power_amt = 0;
  5652. bool no_calcs = rez->no_calcs;
  5653. int8 crit_mod = rez->crit_mod;
  5654. Entity* caster = 0;
  5655. InfoStruct* info = 0;
  5656. bool crit = false;
  5657. string heal_spell = rez->heal_name;
  5658. int16 heal_packet_type = 0;
  5659. int16 power_packet_type = 0;
  5660. //Calculations for how much to heal the spawn
  5661. if(health_perc > 0)
  5662. heal_amt = (spawn->GetTotalHP() * (health_perc / 100));
  5663. if(power_perc > 0)
  5664. power_amt = (spawn->GetTotalPower() * (power_perc / 100));
  5665. if(caster_spawn->IsEntity()){
  5666. caster = ((Entity*)caster_spawn);
  5667. info = caster->GetInfoStruct();
  5668. }
  5669. if(!no_calcs && caster){
  5670. //Potency Mod
  5671. heal_amt *= ((caster->stats[ITEM_STAT_POTENCY] / 100) + 1);
  5672. power_amt *= ((caster->stats[ITEM_STAT_POTENCY] / 100) + 1);
  5673. //Ability Mod
  5674. heal_amt += (int32)min((int32)info->get_ability_modifier(), (int32)(heal_amt / 2));
  5675. power_amt += (int32)min((int32)info->get_ability_modifier(), (int32)(power_amt / 2));
  5676. if(!crit_mod || crit_mod == 1){
  5677. if(crit_mod == 1)
  5678. crit = true;
  5679. else {
  5680. // Crit Roll
  5681. float chance = (float)max((float)0, (float)info->get_crit_chance());
  5682. crit = (MakeRandomFloat(0, 100) <= chance);
  5683. }
  5684. if(crit){
  5685. //Apply total crit multiplier with crit bonus
  5686. heal_amt *= ((info->get_crit_bonus() / 100) + 1.3);
  5687. power_amt *= ((info->get_crit_bonus() / 100) + 1.3);
  5688. }
  5689. }
  5690. }
  5691. //Set this rez as a crit to be passed to subspell (not yet used)
  5692. rez->crit = true;
  5693. //Set Heal amt to 1 if 0 now so the player has health
  5694. if(heal_amt == 0)
  5695. heal_amt = 1;
  5696. if(heal_amt > spawn->GetTotalHP())
  5697. heal_amt = spawn->GetTotalHP();
  5698. if(power_amt > spawn->GetTotalPower())
  5699. power_amt = spawn->GetTotalPower();
  5700. spawn->SetHP(heal_amt);
  5701. if(power_amt > 0)
  5702. spawn->SetPower(power_amt);
  5703. if(client && caster){
  5704. EQ2Packet* move = ((Player*)spawn)->Move(caster->GetX(), caster->GetY(), caster->GetZ(), client->GetVersion());
  5705. if(move)
  5706. client->QueuePacket(move);
  5707. }
  5708. if(crit){
  5709. power_packet_type = HEAL_PACKET_TYPE_CRIT_MANA;
  5710. heal_packet_type = HEAL_PACKET_TYPE_CRIT_HEAL;
  5711. }
  5712. else {
  5713. power_packet_type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  5714. heal_packet_type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  5715. }
  5716. SendHealPacket(caster, spawn, heal_packet_type, heal_amt, heal_spell.c_str());
  5717. if(power_amt > 0)
  5718. SendHealPacket(caster, spawn, power_packet_type, power_amt, heal_spell.c_str());
  5719. //The following code sets the spawn as alive
  5720. if(dead_spawns.count(spawn->GetID()) > 0)
  5721. dead_spawns.erase(spawn->GetID());
  5722. if(spawn->IsPlayer()){
  5723. spawn->SetSpawnType(4);
  5724. client = GetClientBySpawn(spawn);
  5725. if(client){
  5726. packet = configReader.getStruct("WS_Resurrected", client->GetVersion());
  5727. if(packet){
  5728. client->QueuePacket(packet->serialize());
  5729. }
  5730. packet = configReader.getStruct("WS_ServerControlFlags", client->GetVersion());
  5731. if(packet)
  5732. {
  5733. packet->setDataByName("parameter1", 8);
  5734. client->QueuePacket(packet->serialize());
  5735. packet->setDataByName("parameter1", 16);
  5736. client->QueuePacket(packet->serialize());
  5737. }
  5738. safe_delete(packet);
  5739. client->SimpleMessage(CHANNEL_NARRATIVE, "You regain consciousness!");
  5740. }
  5741. }
  5742. spawn->SendSpawnChanges(true);
  5743. spawn->SetTempActionState(-1);
  5744. spawn->appearance.attackable = 1;
  5745. }
  5746. void ZoneServer::SendDispellPacket(Entity* caster, Spawn* target, string dispell_name, string spell_name, int8 dispell_type){
  5747. if(!caster || !target)
  5748. return;
  5749. Client* client = 0;
  5750. Player* player = 0;
  5751. PacketStruct* packet = 0;
  5752. vector<Client*>::iterator client_itr;
  5753. MClientList.readlock(__FUNCTION__, __LINE__);
  5754. for (client_itr = clients.begin(); client_itr != clients.end(); client_itr++){
  5755. client = *client_itr;
  5756. if(!client || !(player = client->GetPlayer()) || (player != caster && ((caster && player->WasSentSpawn(caster->GetID()) == false) || (target && player->WasSentSpawn(target->GetID()) == false))))
  5757. continue;
  5758. if((caster && player->WasSpawnRemoved(caster)) || (caster && player->WasSpawnRemoved(target)))
  5759. continue;
  5760. if(caster && caster->GetDistance(player) > 50)
  5761. continue;
  5762. if(target && target->GetDistance(player) > 50)
  5763. continue;
  5764. packet = configReader.getStruct("WS_HearDispell", client->GetVersion());
  5765. if(packet){
  5766. packet->setDataByName("spell_name", spell_name.c_str());
  5767. packet->setDataByName("dispell_name", dispell_name.c_str());
  5768. packet->setDataByName("caster", player->GetIDWithPlayerSpawn(caster));
  5769. packet->setDataByName("target", player->GetIDWithPlayerSpawn(target));
  5770. packet->setDataByName("type", dispell_type);
  5771. client->QueuePacket(packet->serialize());
  5772. }
  5773. safe_delete(packet);
  5774. }
  5775. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  5776. }
  5777. void ZoneServer::DismissAllPets() {
  5778. Spawn* spawn = 0;
  5779. map<int32, Spawn*>::iterator itr;
  5780. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5781. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5782. spawn = itr->second;
  5783. if (spawn && spawn->IsPet() && ((NPC*)spawn)->GetOwner())
  5784. ((NPC*)spawn)->GetOwner()->DismissPet((NPC*)spawn);
  5785. }
  5786. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5787. }
  5788. void ZoneServer::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target){
  5789. if (spellProcess)
  5790. spellProcess->RemoveTargetFromSpell(spell, target);
  5791. }
  5792. void ZoneServer::ClearHate(Entity* entity) {
  5793. Spawn* spawn = 0;
  5794. map<int32, Spawn*>::iterator itr;
  5795. MSpawnList.readlock(__FUNCTION__, __LINE__);
  5796. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  5797. spawn = itr->second;
  5798. if (spawn && spawn->IsNPC() && ((NPC*)spawn)->Brain())
  5799. ((NPC*)spawn)->Brain()->ClearHate(entity);
  5800. }
  5801. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  5802. }
  5803. ThreadReturnType ZoneLoop(void* tmp) {
  5804. #ifdef WIN32
  5805. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  5806. #endif
  5807. if (tmp == 0) {
  5808. ThrowError("ZoneLoop(): tmp = 0!");
  5809. THREAD_RETURN(NULL);
  5810. }
  5811. ZoneServer* zs = (ZoneServer*) tmp;
  5812. while (zs->Process()) {
  5813. if(zs->GetClientCount() == 0)
  5814. Sleep(1000);
  5815. else
  5816. Sleep(10);
  5817. }
  5818. zs->Process(); //run loop once more to clean up some functions
  5819. safe_delete(zs);
  5820. THREAD_RETURN(NULL);
  5821. }
  5822. ThreadReturnType SpawnLoop(void* tmp) {
  5823. #ifdef WIN32
  5824. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  5825. #endif
  5826. if (tmp == 0) {
  5827. ThrowError("SpawnLoop(): tmp = 0!");
  5828. THREAD_RETURN(NULL);
  5829. }
  5830. ZoneServer* zs = (ZoneServer*) tmp;
  5831. #ifndef NO_CATCH
  5832. try {
  5833. #endif
  5834. zs->spawnthread_active = true;
  5835. while (zs->SpawnProcess()) {
  5836. if(zs->GetClientCount() == 0)
  5837. Sleep(1000);
  5838. else
  5839. Sleep(20);
  5840. }
  5841. zs->spawnthread_active = false;
  5842. #ifndef NO_CATCH
  5843. }
  5844. catch(...) {
  5845. zs->spawnthread_active = false;
  5846. zs->initial_spawn_threads_active = 0;
  5847. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop, shutting down zone '%s'...", zs->GetZoneName());
  5848. try{
  5849. zs->Shutdown();
  5850. }
  5851. catch(...){
  5852. LogWrite(ZONE__ERROR, 0, "Zone", "Error Processing SpawnLoop while shutting down zone '%s'...", zs->GetZoneName());
  5853. throw;
  5854. }
  5855. throw;
  5856. }
  5857. #endif
  5858. THREAD_RETURN(NULL);
  5859. }
  5860. ThreadReturnType SendInitialSpawns(void* tmp) {
  5861. #ifdef WIN32
  5862. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  5863. #endif
  5864. if (tmp == 0) {
  5865. ThrowError("SendInitialSpawns(): tmp = 0!");
  5866. THREAD_RETURN(NULL);
  5867. }
  5868. Client* client = (Client*) tmp;
  5869. client->GetCurrentZone()->SendZoneSpawns(client);
  5870. THREAD_RETURN(NULL);
  5871. }
  5872. ThreadReturnType SendLevelChangedSpawns(void* tmp) {
  5873. #ifdef WIN32
  5874. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  5875. #endif
  5876. if (tmp == 0) {
  5877. ThrowError("SendLevelChangedSpawns(): tmp = 0!");
  5878. THREAD_RETURN(NULL);
  5879. }
  5880. Client* client = (Client*)tmp;
  5881. client->GetCurrentZone()->SendAllSpawnsForLevelChange(client);
  5882. THREAD_RETURN(NULL);
  5883. }
  5884. void ZoneServer::SetSpawnStructs(Client* client) {
  5885. int16 client_ver = client->GetVersion();
  5886. Player* player = client->GetPlayer();
  5887. //Save a copy of the correct spawn substructs for the client's player, save here a copy if we don't have one
  5888. PacketStruct* pos = configReader.getStruct("Substruct_SpawnPositionStruct", client_ver);
  5889. player->SetSpawnPosStruct(pos);
  5890. if (versioned_pos_structs.count(pos->GetVersion()) == 0)
  5891. versioned_pos_structs[pos->GetVersion()] = new PacketStruct(pos, true);
  5892. PacketStruct* vis = configReader.getStruct("Substruct_SpawnVisualizationInfoStruct", client_ver);
  5893. player->SetSpawnVisStruct(vis);
  5894. if (versioned_vis_structs.count(vis->GetVersion()) == 0)
  5895. versioned_vis_structs[vis->GetVersion()] = new PacketStruct(vis, true);
  5896. PacketStruct* info = configReader.getStruct("Substruct_SpawnInfoStruct", client_ver);
  5897. player->SetSpawnInfoStruct(info);
  5898. if (versioned_info_structs.count(info->GetVersion()) == 0)
  5899. versioned_info_structs[info->GetVersion()] = new PacketStruct(info, true);
  5900. PacketStruct* header = configReader.getStruct("WS_SpawnStruct_Header", client_ver);
  5901. player->SetSpawnHeaderStruct(header);
  5902. PacketStruct* footer = configReader.getStruct("WS_SpawnStruct_Footer", client_ver);
  5903. player->SetSpawnFooterStruct(footer);
  5904. PacketStruct* sfooter = configReader.getStruct("WS_SignWidgetSpawnStruct_Footer", client_ver);
  5905. player->SetSignFooterStruct(sfooter);
  5906. PacketStruct* wfooter = configReader.getStruct("WS_WidgetSpawnStruct_Footer", client_ver);
  5907. player->SetWidgetFooterStruct(wfooter);
  5908. }
  5909. Spawn* ZoneServer::GetSpawn(int32 id){
  5910. Spawn* ret = 0;
  5911. if(GetNPC(id))
  5912. ret = GetNewNPC(id);
  5913. else if(this->GetObject(id))
  5914. ret = GetNewObject(id);
  5915. else if(GetWidget(id))
  5916. ret = GetNewWidget(id);
  5917. else if(GetSign(id))
  5918. ret = GetNewSign(id);
  5919. else if(GetGroundSpawn(id))
  5920. ret = GetNewGroundSpawn(id);
  5921. // Unable to find the spawn in the list lets attempt to add it if we are not currently reloading
  5922. else if (!reloading && database.LoadNPC(this, id)) {
  5923. if (GetNPC(id))
  5924. ret = GetNewNPC(id);
  5925. else
  5926. LogWrite(NPC__ERROR, 0, "NPC", "Database inserted npc (%u) but was still unable to retrieve it!", id);
  5927. }
  5928. else if (!reloading && database.LoadObject(this, id)) {
  5929. if (this->GetObject(id))
  5930. ret = GetNewObject(id);
  5931. else
  5932. LogWrite(OBJECT__ERROR, 0, "Object", "Database inserted object (%u) but was still unable to retrieve it!", id);
  5933. }
  5934. else if (!reloading && database.LoadWidget(this, id)) {
  5935. if (GetWidget(id))
  5936. ret = GetNewWidget(id);
  5937. else
  5938. LogWrite(WIDGET__ERROR, 0, "Widget", "Database inserted widget (%u) but was still unable to retrieve it!", id);
  5939. }
  5940. else if (!reloading && database.LoadSign(this, id)) {
  5941. if (GetSign(id))
  5942. ret = GetNewSign(id);
  5943. else
  5944. LogWrite(SIGN__ERROR, 0, "Sign", "Database inserted sign (%u) but was still unable to retrieve it!", id);
  5945. }
  5946. else if (!reloading && database.LoadGroundSpawn(this, id)) {
  5947. if (GetGroundSpawn(id))
  5948. ret = GetNewGroundSpawn(id);
  5949. else
  5950. LogWrite(GROUNDSPAWN__ERROR, 0, "GSpawn", "Database inserted ground spawn (%u) but was still unable to retrieve it!", id);
  5951. }
  5952. if(ret)
  5953. ret->SetID(Spawn::NextID());
  5954. return ret;
  5955. }
  5956. vector<EntityCommand*>* ZoneServer::GetEntityCommandList(int32 id){
  5957. if(entity_command_list.count(id) > 0)
  5958. return entity_command_list[id];
  5959. else
  5960. return 0;
  5961. }
  5962. void ZoneServer::SetEntityCommandList(int32 id, EntityCommand* command) {
  5963. if (entity_command_list.count(id) == 0)
  5964. entity_command_list[id] = new vector<EntityCommand*>;
  5965. entity_command_list[id]->push_back(command);
  5966. }
  5967. EntityCommand* ZoneServer::GetEntityCommand(int32 id, string name) {
  5968. EntityCommand* ret = 0;
  5969. if (entity_command_list.count(id) == 0)
  5970. return ret;
  5971. vector<EntityCommand*>::iterator itr;
  5972. for (itr = entity_command_list[id]->begin(); itr != entity_command_list[id]->end(); itr++) {
  5973. if ((*itr)->name == name) {
  5974. ret = (*itr);
  5975. break;
  5976. }
  5977. }
  5978. return ret;
  5979. }
  5980. void ZoneServer::ClearEntityCommands() {
  5981. if (entity_command_list.size() > 0) {
  5982. map<int32, vector<EntityCommand*>* >::iterator itr;
  5983. for (itr = entity_command_list.begin(); itr != entity_command_list.end(); itr++) {
  5984. vector<EntityCommand*>* entity_commands = itr->second;
  5985. if (entity_commands && entity_commands->size() > 0) {
  5986. vector<EntityCommand*>::iterator v_itr;
  5987. for (v_itr = entity_commands->begin(); v_itr != entity_commands->end(); v_itr++)
  5988. safe_delete(*v_itr);
  5989. entity_commands->clear();
  5990. }
  5991. safe_delete(entity_commands);
  5992. }
  5993. entity_command_list.clear();
  5994. }
  5995. }
  5996. void ZoneServer::AddNPCSpell(int32 list_id, int32 spell_id, int8 tier){
  5997. npc_spell_list[list_id][spell_id] = tier;
  5998. }
  5999. vector<Spell*>* ZoneServer::GetNPCSpells(int32 primary_list, int32 secondary_list){
  6000. vector<Spell*>* ret = 0;
  6001. if(npc_spell_list.count(primary_list) > 0){
  6002. ret = new vector<Spell*>();
  6003. map<int32, int8>::iterator itr;
  6004. Spell* tmpSpell = 0;
  6005. for(itr = npc_spell_list[primary_list].begin(); itr != npc_spell_list[primary_list].end(); itr++){
  6006. tmpSpell = master_spell_list.GetSpell(itr->first, itr->second);
  6007. if(tmpSpell)
  6008. ret->push_back(tmpSpell);
  6009. }
  6010. }
  6011. if(npc_spell_list.count(secondary_list) > 0){
  6012. if(!ret)
  6013. ret = new vector<Spell*>();
  6014. map<int32, int8>::iterator itr;
  6015. Spell* tmpSpell = 0;
  6016. for(itr = npc_spell_list[secondary_list].begin(); itr != npc_spell_list[secondary_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(ret && ret->size() == 0){
  6023. safe_delete(ret);
  6024. ret = 0;
  6025. }
  6026. return ret;
  6027. }
  6028. void ZoneServer::AddNPCSkill(int32 list_id, int32 skill_id, int16 value){
  6029. npc_skill_list[list_id][skill_id] = value;
  6030. }
  6031. map<string, Skill*>* ZoneServer::GetNPCSkills(int32 primary_list, int32 secondary_list){
  6032. map<string, Skill*>* ret = 0;
  6033. if(npc_skill_list.count(primary_list) > 0){
  6034. ret = new map<string, Skill*>();
  6035. map<int32, int16>::iterator itr;
  6036. Skill* tmpSkill = 0;
  6037. for(itr = npc_skill_list[primary_list].begin(); itr != npc_skill_list[primary_list].end(); itr++){
  6038. tmpSkill = master_skill_list.GetSkill(itr->first);
  6039. if(tmpSkill){
  6040. tmpSkill = new Skill(tmpSkill);
  6041. tmpSkill->current_val = itr->second;
  6042. tmpSkill->max_val = tmpSkill->current_val+5;
  6043. (*ret)[tmpSkill->name.data] = tmpSkill;
  6044. }
  6045. }
  6046. }
  6047. if(npc_skill_list.count(secondary_list) > 0){
  6048. if(!ret)
  6049. ret = new map<string, Skill*>();
  6050. map<int32, int16>::iterator itr;
  6051. Skill* tmpSkill = 0;
  6052. for(itr = npc_skill_list[secondary_list].begin(); itr != npc_skill_list[secondary_list].end(); itr++){
  6053. tmpSkill = master_skill_list.GetSkill(itr->first);
  6054. if(tmpSkill){
  6055. tmpSkill = new Skill(tmpSkill);
  6056. tmpSkill->current_val = itr->second;
  6057. tmpSkill->max_val = tmpSkill->current_val+5;
  6058. (*ret)[tmpSkill->name.data] = tmpSkill;
  6059. }
  6060. }
  6061. }
  6062. if(ret && ret->size() == 0){
  6063. safe_delete(ret);
  6064. ret = 0;
  6065. }
  6066. return ret;
  6067. }
  6068. void ZoneServer::AddNPCEquipment(int32 list_id, int32 item_id){
  6069. npc_equipment_list[list_id].push_back(item_id);
  6070. }
  6071. void ZoneServer::SetNPCEquipment(NPC* npc) {
  6072. if(npc_equipment_list.count(npc->GetEquipmentListID()) > 0){
  6073. Item* tmpItem = 0;
  6074. int8 slot = 0;
  6075. vector<int32>::iterator itr;
  6076. for(itr = npc_equipment_list[npc->GetEquipmentListID()].begin(); itr != npc_equipment_list[npc->GetEquipmentListID()].end(); itr++){
  6077. tmpItem = master_item_list.GetItem(*itr);
  6078. if(tmpItem){
  6079. slot = npc->GetEquipmentList()->GetFreeSlot(tmpItem);
  6080. if(slot < 255){
  6081. tmpItem = new Item(tmpItem);
  6082. npc->GetEquipmentList()->SetItem(slot, tmpItem);
  6083. }
  6084. }
  6085. }
  6086. }
  6087. }
  6088. void ZoneServer::AddNPC(int32 id, NPC* npc) {
  6089. npc_list[id] = npc;
  6090. }
  6091. void ZoneServer::AddWidget(int32 id, Widget* widget) {
  6092. widget_list[id] = widget;
  6093. }
  6094. Widget* ZoneServer::GetWidget(int32 id, bool override_loading) {
  6095. if((!reloading || override_loading) && widget_list.count(id) > 0)
  6096. return widget_list[id];
  6097. else
  6098. return 0;
  6099. }
  6100. Widget* ZoneServer::GetNewWidget(int32 id) {
  6101. if(!reloading && widget_list.count(id) > 0)
  6102. return widget_list[id]->Copy();
  6103. else
  6104. return 0;
  6105. }
  6106. void ZoneServer::LoadGroundSpawnEntries(){
  6107. MGroundSpawnItems.lock();
  6108. database.LoadGroundSpawnEntries(this);
  6109. MGroundSpawnItems.unlock();
  6110. }
  6111. void ZoneServer::LoadGroundSpawnItems() {
  6112. }
  6113. 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) {
  6114. GroundSpawnEntry* entry = new GroundSpawnEntry;
  6115. entry->min_skill_level = min_skill_level;
  6116. entry->min_adventure_level = min_adventure_level;
  6117. entry->bonus_table = bonus_table;
  6118. entry->harvest1 = harvest1;
  6119. entry->harvest3 = harvest3;
  6120. entry->harvest5 = harvest5;
  6121. entry->harvest_imbue = harvest_imbue;
  6122. entry->harvest_rare = harvest_rare;
  6123. entry->harvest10 = harvest10;
  6124. entry->harvest_coin = harvest_coin;
  6125. groundspawn_entries[groundspawn_id].push_back(entry);
  6126. }
  6127. void ZoneServer::AddGroundSpawnItem(int32 groundspawn_id, int32 item_id, int8 is_rare, int32 grid_id) {
  6128. GroundSpawnEntryItem* entry = new GroundSpawnEntryItem;
  6129. entry->item_id = item_id;
  6130. entry->is_rare = is_rare;
  6131. entry->grid_id = grid_id;
  6132. groundspawn_items[groundspawn_id].push_back(entry);
  6133. }
  6134. vector<GroundSpawnEntry*>* ZoneServer::GetGroundSpawnEntries(int32 id){
  6135. vector<GroundSpawnEntry*>* ret = 0;
  6136. MGroundSpawnItems.lock();
  6137. if(groundspawn_entries.count(id) > 0)
  6138. ret = &groundspawn_entries[id];
  6139. MGroundSpawnItems.unlock();
  6140. return ret;
  6141. }
  6142. vector<GroundSpawnEntryItem*>* ZoneServer::GetGroundSpawnEntryItems(int32 id){
  6143. vector<GroundSpawnEntryItem*>* ret = 0;
  6144. if(groundspawn_items.count(id) > 0)
  6145. ret = &groundspawn_items[id];
  6146. return ret;
  6147. }
  6148. // TODO - mis-named, should be DeleteGroundSpawnEntries() but this is ok for now :)
  6149. void ZoneServer::DeleteGroundSpawnItems()
  6150. {
  6151. MGroundSpawnItems.lock();
  6152. map<int32, vector<GroundSpawnEntry*> >::iterator groundspawnentry_map_itr;
  6153. vector<GroundSpawnEntry*>::iterator groundspawnentry_itr;
  6154. for(groundspawnentry_map_itr = groundspawn_entries.begin(); groundspawnentry_map_itr != groundspawn_entries.end(); groundspawnentry_map_itr++)
  6155. {
  6156. for(groundspawnentry_itr = groundspawnentry_map_itr->second.begin(); groundspawnentry_itr != groundspawnentry_map_itr->second.end(); groundspawnentry_itr++)
  6157. {
  6158. safe_delete(*groundspawnentry_itr);
  6159. }
  6160. }
  6161. groundspawn_entries.clear();
  6162. map<int32, vector<GroundSpawnEntryItem*> >::iterator groundspawnitem_map_itr;
  6163. vector<GroundSpawnEntryItem*>::iterator groundspawnitem_itr;
  6164. for(groundspawnitem_map_itr = groundspawn_items.begin(); groundspawnitem_map_itr != groundspawn_items.end(); groundspawnitem_map_itr++)
  6165. {
  6166. for(groundspawnitem_itr = groundspawnitem_map_itr->second.begin(); groundspawnitem_itr != groundspawnitem_map_itr->second.end(); groundspawnitem_itr++)
  6167. {
  6168. safe_delete(*groundspawnitem_itr);
  6169. }
  6170. }
  6171. groundspawn_items.clear();
  6172. MGroundSpawnItems.unlock();
  6173. }
  6174. void ZoneServer::AddGroundSpawn(int32 id, GroundSpawn* spawn) {
  6175. groundspawn_list[id] = spawn;
  6176. }
  6177. GroundSpawn* ZoneServer::GetGroundSpawn(int32 id, bool override_loading) {
  6178. if((!reloading || override_loading) && groundspawn_list.count(id) > 0)
  6179. return groundspawn_list[id];
  6180. else
  6181. return 0;
  6182. }
  6183. GroundSpawn* ZoneServer::GetNewGroundSpawn(int32 id) {
  6184. if(!reloading && groundspawn_list.count(id) > 0)
  6185. return groundspawn_list[id]->Copy();
  6186. else
  6187. return 0;
  6188. }
  6189. void ZoneServer::AddLootTable(int32 id, LootTable* table){
  6190. loot_tables[id] = table;
  6191. }
  6192. void ZoneServer::AddLootDrop(int32 id, LootDrop* drop){
  6193. loot_drops[id].push_back(drop);
  6194. }
  6195. void ZoneServer::AddSpawnLootList(int32 spawn_id, int32 id) {
  6196. spawn_loot_list[spawn_id].push_back(id);
  6197. }
  6198. void ZoneServer::ClearSpawnLootList(int32 spawn_id) {
  6199. spawn_loot_list[spawn_id].clear();
  6200. }
  6201. void ZoneServer::AddLevelLootList(GlobalLoot* loot) {
  6202. level_loot_list.push_back(loot);
  6203. }
  6204. void ZoneServer::AddRacialLootList(int16 racial_id, GlobalLoot* loot) {
  6205. racial_loot_list[racial_id].push_back(loot);
  6206. }
  6207. void ZoneServer::AddZoneLootList(int32 zone, GlobalLoot* loot) {
  6208. zone_loot_list[zone].push_back(loot);
  6209. }
  6210. void ZoneServer::ClearLootTables(){
  6211. map<int32,LootTable*>::iterator table_itr;
  6212. for(table_itr = loot_tables.begin(); table_itr != loot_tables.end(); table_itr++){
  6213. safe_delete(table_itr->second);
  6214. }
  6215. map<int32, vector<LootDrop*> >::iterator drop_itr;
  6216. vector<LootDrop*>::iterator drop_itr2;
  6217. for(drop_itr = loot_drops.begin(); drop_itr != loot_drops.end(); drop_itr++){
  6218. for(drop_itr2 = drop_itr->second.begin(); drop_itr2 != drop_itr->second.end(); drop_itr2++){
  6219. safe_delete(*drop_itr2);
  6220. }
  6221. }
  6222. vector<GlobalLoot*>::iterator level_itr;
  6223. for (level_itr = level_loot_list.begin(); level_itr != level_loot_list.end(); level_itr++) {
  6224. safe_delete(*level_itr);
  6225. }
  6226. map<int16, vector<GlobalLoot*> >::iterator race_itr;
  6227. vector<GlobalLoot*>::iterator race_itr2;
  6228. for (race_itr = racial_loot_list.begin(); race_itr != racial_loot_list.end(); race_itr++) {
  6229. for (race_itr2 = race_itr->second.begin(); race_itr2 != race_itr->second.end(); race_itr2++) {
  6230. safe_delete(*race_itr2);
  6231. }
  6232. }
  6233. map<int32, vector<GlobalLoot*> >::iterator zone_itr;
  6234. vector<GlobalLoot*>::iterator zone_itr2;
  6235. for(zone_itr = zone_loot_list.begin(); zone_itr != zone_loot_list.end(); zone_itr++) {
  6236. for (zone_itr2 = zone_itr->second.begin(); zone_itr2 != zone_itr->second.end(); zone_itr2++) {
  6237. safe_delete(*zone_itr2);
  6238. }
  6239. }
  6240. loot_tables.clear();
  6241. loot_drops.clear();
  6242. spawn_loot_list.clear();
  6243. level_loot_list.clear();
  6244. racial_loot_list.clear();
  6245. zone_loot_list.clear();
  6246. }
  6247. vector<int32> ZoneServer::GetSpawnLootList(int32 spawn_id, int32 zone_id, int8 spawn_level, int16 racial_id) {
  6248. vector<int32> ret;
  6249. if(reloading)
  6250. return ret;
  6251. if (spawn_loot_list.count(spawn_id) > 0)
  6252. ret.insert(ret.end(), spawn_loot_list[spawn_id].begin(), spawn_loot_list[spawn_id].end());
  6253. if (level_loot_list.size() > 0) {
  6254. vector<GlobalLoot*>::iterator itr;
  6255. for (itr = level_loot_list.begin(); itr != level_loot_list.end(); itr++) {
  6256. GlobalLoot* loot = *itr;
  6257. if (loot->minLevel == 0 && loot->maxLevel == 0)
  6258. ret.push_back(loot->table_id);
  6259. else {
  6260. if (spawn_level >= loot->minLevel && spawn_level <= loot->maxLevel)
  6261. ret.push_back(loot->table_id);
  6262. }
  6263. }
  6264. }
  6265. if (racial_loot_list.count(racial_id) > 0) {
  6266. vector<GlobalLoot*>::iterator itr;
  6267. for (itr = racial_loot_list[racial_id].begin(); itr != racial_loot_list[racial_id].end(); itr++) {
  6268. GlobalLoot* loot = *itr;
  6269. if (loot->minLevel == 0 && loot->maxLevel == 0)
  6270. ret.push_back(loot->table_id);
  6271. else {
  6272. if (spawn_level >= loot->minLevel && spawn_level <= loot->maxLevel)
  6273. ret.push_back(loot->table_id);
  6274. }
  6275. }
  6276. }
  6277. if (zone_loot_list.count(zone_id) > 0) {
  6278. vector<GlobalLoot*>::iterator itr;
  6279. for (itr = zone_loot_list[zone_id].begin(); itr != zone_loot_list[zone_id].end(); itr++) {
  6280. GlobalLoot* loot = *itr;
  6281. if (loot->minLevel == 0 && loot->maxLevel == 0)
  6282. ret.push_back(loot->table_id);
  6283. else {
  6284. if (spawn_level >= loot->minLevel && spawn_level <= loot->maxLevel)
  6285. ret.push_back(loot->table_id);
  6286. }
  6287. }
  6288. }
  6289. return ret;
  6290. }
  6291. vector<LootDrop*>* ZoneServer::GetLootDrops(int32 table_id){
  6292. if(!reloading && loot_drops.count(table_id) > 0)
  6293. return &(loot_drops[table_id]);
  6294. else
  6295. return 0;
  6296. }
  6297. LootTable* ZoneServer::GetLootTable(int32 table_id){
  6298. return loot_tables[table_id];
  6299. }
  6300. 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){
  6301. LocationTransportDestination* loc = new LocationTransportDestination;
  6302. loc->message = message;
  6303. loc->trigger_x = trigger_x;
  6304. loc->trigger_y = trigger_y;
  6305. loc->trigger_z = trigger_z;
  6306. loc->trigger_radius = trigger_radius;
  6307. loc->destination_zone_id = destination_zone_id;
  6308. loc->destination_x = destination_x;
  6309. loc->destination_y = destination_y;
  6310. loc->destination_z = destination_z;
  6311. loc->destination_heading = destination_heading;
  6312. loc->cost = cost;
  6313. loc->unique_id = unique_id;
  6314. MTransporters.lock();
  6315. if(location_transporters.count(zone_id) == 0)
  6316. location_transporters[zone_id] = new MutexList<LocationTransportDestination*>();
  6317. location_transporters[zone_id]->Add(loc);
  6318. MTransporters.unlock();
  6319. }
  6320. 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,
  6321. 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,
  6322. int32 max_client_version, int32 flight_path_id, int16 mount_id, int8 mount_red_color, int8 mount_green_color, int8 mount_blue_color){
  6323. TransportDestination* transport = new TransportDestination;
  6324. transport->type = type;
  6325. transport->display_name = name;
  6326. transport->message = message;
  6327. transport->destination_zone_id = destination_zone_id;
  6328. transport->destination_x = destination_x;
  6329. transport->destination_y = destination_y;
  6330. transport->destination_z = destination_z;
  6331. transport->destination_heading = destination_heading;
  6332. transport->cost = cost;
  6333. transport->unique_id = unique_id;
  6334. transport->min_level = min_level;
  6335. transport->max_level = max_level;
  6336. transport->req_quest = quest_req;
  6337. transport->req_quest_step = quest_step_req;
  6338. transport->req_quest_complete = quest_complete;
  6339. transport->map_x = map_x;
  6340. transport->map_y = map_y;
  6341. transport->expansion_flag = expansion_flag;
  6342. transport->holiday_flag = holiday_flag;
  6343. transport->min_client_version = min_client_version;
  6344. transport->max_client_version = max_client_version;
  6345. transport->flight_path_id = flight_path_id;
  6346. transport->mount_id = mount_id;
  6347. transport->mount_red_color = mount_red_color;
  6348. transport->mount_green_color = mount_green_color;
  6349. transport->mount_blue_color = mount_blue_color;
  6350. MTransporters.lock();
  6351. transporters[transport_id].push_back(transport);
  6352. MTransporters.unlock();
  6353. }
  6354. void ZoneServer::GetTransporters(vector<TransportDestination*>* returnList, Client* client, int32 transport_id){
  6355. if (!returnList)
  6356. return;
  6357. MTransporters.lock();
  6358. if (transporters.count(transport_id) > 0)
  6359. {
  6360. vector<TransportDestination*> list;
  6361. for (int i = 0; i < transporters[transport_id].size(); i++)
  6362. {
  6363. if (transporters[transport_id][i]->min_client_version && client->GetVersion() < transporters[transport_id][i]->min_client_version)
  6364. continue;
  6365. else if (transporters[transport_id][i]->max_client_version && client->GetVersion() > transporters[transport_id][i]->max_client_version)
  6366. continue;
  6367. if (database.CheckExpansionFlags(this, transporters[transport_id][i]->expansion_flag) && database.CheckHolidayFlags(this, transporters[transport_id][i]->holiday_flag))
  6368. {
  6369. returnList->push_back(transporters[transport_id][i]);
  6370. }
  6371. }
  6372. }
  6373. MTransporters.unlock();
  6374. }
  6375. MutexList<LocationTransportDestination*>* ZoneServer::GetLocationTransporters(int32 zone_id){
  6376. MutexList<LocationTransportDestination*>* ret = 0;
  6377. MTransporters.lock();
  6378. if(location_transporters.count(zone_id) > 0)
  6379. ret = location_transporters[zone_id];
  6380. MTransporters.unlock();
  6381. return ret;
  6382. }
  6383. void ZoneServer::DeleteGlobalTransporters(){
  6384. MTransporters.lock();
  6385. map<int32, vector<TransportDestination*> >::iterator itr;
  6386. vector<TransportDestination*>::iterator transport_vector_itr;
  6387. for(itr = transporters.begin(); itr != transporters.end(); itr++){
  6388. for(transport_vector_itr = itr->second.begin(); transport_vector_itr != itr->second.end(); transport_vector_itr++){
  6389. safe_delete(*transport_vector_itr);
  6390. }
  6391. }
  6392. map<int32, MutexList<LocationTransportDestination*>* >::iterator itr2;
  6393. for(itr2 = location_transporters.begin(); itr2 != location_transporters.end(); itr2++){
  6394. itr2->second->clear(true);
  6395. delete itr2->second;
  6396. }
  6397. transporters.clear();
  6398. location_transporters.clear();
  6399. MTransporters.unlock();
  6400. }
  6401. void ZoneServer::DeleteGlobalSpawns() {
  6402. ClearLootTables();
  6403. map<int32, NPC*>::iterator npc_list_iter;
  6404. for(npc_list_iter=npc_list.begin();npc_list_iter!=npc_list.end();npc_list_iter++) {
  6405. safe_delete(npc_list_iter->second);
  6406. }
  6407. npc_list.clear();
  6408. map<int32, Object*>::iterator object_list_iter;
  6409. for(object_list_iter=object_list.begin();object_list_iter!=object_list.end();object_list_iter++) {
  6410. safe_delete(object_list_iter->second);
  6411. }
  6412. object_list.clear();
  6413. map<int32, GroundSpawn*>::iterator groundspawn_list_iter;
  6414. for(groundspawn_list_iter=groundspawn_list.begin();groundspawn_list_iter!=groundspawn_list.end();groundspawn_list_iter++) {
  6415. safe_delete(groundspawn_list_iter->second);
  6416. }
  6417. groundspawn_list.clear();
  6418. map<int32, Widget*>::iterator widget_list_iter;
  6419. for(widget_list_iter=widget_list.begin();widget_list_iter!=widget_list.end();widget_list_iter++) {
  6420. safe_delete(widget_list_iter->second);
  6421. }
  6422. widget_list.clear();
  6423. map<int32, Sign*>::iterator sign_list_iter;
  6424. for(sign_list_iter=sign_list.begin();sign_list_iter!=sign_list.end();sign_list_iter++) {
  6425. safe_delete(sign_list_iter->second);
  6426. }
  6427. sign_list.clear();
  6428. /*map<int32, AppearanceData*>::iterator appearance_list_iter;
  6429. for(appearance_list_iter=npc_appearance_list.begin();appearance_list_iter!=npc_appearance_list.end();appearance_list_iter++) {
  6430. safe_delete(appearance_list_iter->second);
  6431. }
  6432. npc_appearance_list.clear();*/
  6433. ClearEntityCommands();
  6434. DeleteGroundSpawnItems();
  6435. DeleteGlobalTransporters();
  6436. DeleteTransporterMaps();
  6437. }
  6438. void ZoneServer::AddTransportMap(int32 id, string name) {
  6439. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  6440. m_transportMaps[id] = name;
  6441. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  6442. }
  6443. bool ZoneServer::TransportHasMap(int32 id) {
  6444. bool ret = false;
  6445. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  6446. ret = m_transportMaps.count(id) > 0;
  6447. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  6448. return ret;
  6449. }
  6450. string ZoneServer::GetTransportMap(int32 id) {
  6451. string ret;
  6452. MTransportMaps.readlock(__FUNCTION__, __LINE__);
  6453. if (m_transportMaps.count(id) > 0)
  6454. ret = m_transportMaps[id];
  6455. MTransportMaps.releasereadlock(__FUNCTION__, __LINE__);
  6456. return ret;
  6457. }
  6458. void ZoneServer::DeleteTransporterMaps() {
  6459. MTransportMaps.writelock(__FUNCTION__, __LINE__);
  6460. m_transportMaps.clear();
  6461. MTransportMaps.releasewritelock(__FUNCTION__, __LINE__);
  6462. }
  6463. void ZoneServer::ReloadSpawns() {
  6464. if (reloading)
  6465. return;
  6466. reloading = true;
  6467. world.SetReloadingSubsystem("Spawns");
  6468. // Let every one in the zone know what is happening
  6469. HandleBroadcast("Reloading all spawns for this zone.");
  6470. DeleteGlobalSpawns();
  6471. Depop(false, true);
  6472. }
  6473. void ZoneServer::SendStateCommand(Spawn* spawn, int32 state) {
  6474. vector<Client*>::iterator itr;
  6475. MClientList.readlock(__FUNCTION__, __LINE__);
  6476. for (itr = clients.begin(); itr != clients.end(); itr++) {
  6477. Client* client = *itr;
  6478. if (client && client->GetPlayer()->WasSentSpawn(spawn->GetID()) && !client->GetPlayer()->WasSpawnRemoved(spawn))
  6479. ClientPacketFunctions::SendStateCommand(client, client->GetPlayer()->GetIDWithPlayerSpawn(spawn), state);
  6480. }
  6481. MClientList.releasereadlock(__FUNCTION__, __LINE__);
  6482. }
  6483. void ZoneServer::AddFlightPath(int32 id, FlightPathInfo* info) {
  6484. if (m_flightPaths.count(id) > 0) {
  6485. LogWrite(ZONE__ERROR, 0, "Zone", "Duplicate flight path (%u)", id);
  6486. safe_delete(info);
  6487. return;
  6488. }
  6489. m_flightPaths[id] = info;
  6490. }
  6491. void ZoneServer::AddFlightPathLocation(int32 id, FlightPathLocation* location) {
  6492. if (m_flightPaths.count(id) == 0) {
  6493. LogWrite(ZONE__ERROR, 0, "Zone", "There is no flight info for this route (%u)", id);
  6494. safe_delete(location);
  6495. return;
  6496. }
  6497. m_flightPathRoutes[id].push_back(location);
  6498. }
  6499. void ZoneServer::DeleteFlightPaths() {
  6500. map<int32, vector<FlightPathLocation*> >::iterator itr;
  6501. vector<FlightPathLocation*>::iterator itr2;
  6502. map<int32, FlightPathInfo*>::iterator itr3;
  6503. for (itr = m_flightPathRoutes.begin(); itr != m_flightPathRoutes.end(); itr++) {
  6504. for (itr2 = itr->second.begin(); itr2 != itr->second.end(); itr2++) {
  6505. safe_delete(*itr2);
  6506. }
  6507. itr->second.clear();
  6508. }
  6509. m_flightPathRoutes.clear();
  6510. for (itr3 = m_flightPaths.begin(); itr3 != m_flightPaths.end(); itr3++) {
  6511. safe_delete(itr3->second);
  6512. }
  6513. m_flightPaths.clear();
  6514. }
  6515. void ZoneServer::SendFlightPathsPackets(Client* client) {
  6516. // Only send a packet if there are flight paths
  6517. if (m_flightPathRoutes.size() > 0) {
  6518. PacketStruct* packet = configReader.getStruct("WS_FlightPathsMsg", client->GetVersion());
  6519. if (packet) {
  6520. int32 num_routes = m_flightPaths.size();
  6521. packet->setArrayLengthByName("number_of_routes", num_routes);
  6522. packet->setArrayLengthByName("number_of_routes2", num_routes);
  6523. packet->setArrayLengthByName("number_of_routes3", num_routes);
  6524. packet->setArrayLengthByName("number_of_routes4", num_routes);
  6525. map<int32, FlightPathInfo*>::iterator itr;
  6526. int32 i = 0;
  6527. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, i++) {
  6528. packet->setArrayDataByName("route_length", m_flightPathRoutes[itr->first].size(), i);
  6529. packet->setArrayDataByName("ground_mount", itr->second->flying ? 0 : 1, i);
  6530. packet->setArrayDataByName("allow_dismount", itr->second->dismount ? 1 : 0, i);
  6531. packet->setSubArrayLengthByName("route_length2", m_flightPathRoutes[itr->first].size(), i);
  6532. vector<FlightPathLocation*>::iterator itr2;
  6533. int32 j = 0;
  6534. for (itr2 = m_flightPathRoutes[itr->first].begin(); itr2 != m_flightPathRoutes[itr->first].end(); itr2++, j++) {
  6535. packet->setSubArrayDataByName("x", (*itr2)->X, i, j);
  6536. packet->setSubArrayDataByName("y", (*itr2)->Y, i, j);
  6537. packet->setSubArrayDataByName("z", (*itr2)->Z, i, j);
  6538. }
  6539. }
  6540. client->QueuePacket(packet->serialize());
  6541. safe_delete(packet);
  6542. }
  6543. }
  6544. }
  6545. int32 ZoneServer::GetFlightPathIndex(int32 id) {
  6546. int32 index = 0;
  6547. map<int32, FlightPathInfo*>::iterator itr;
  6548. for (itr = m_flightPaths.begin(); itr != m_flightPaths.end(); itr++, index++) {
  6549. if (itr->first == id)
  6550. return index;
  6551. }
  6552. return -1;
  6553. }
  6554. float ZoneServer::GetFlightPathSpeed(int32 id) {
  6555. float speed = 1;
  6556. if (m_flightPaths.count(id) > 0)
  6557. speed = m_flightPaths[id]->speed;
  6558. return speed;
  6559. }
  6560. void ZoneServer::ProcessSpawnConditional(int8 condition) {
  6561. MSpawnLocationList.readlock(__FUNCTION__, __LINE__);
  6562. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6563. map<int32, Spawn*>::iterator itr;
  6564. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6565. if (itr->second != NULL) // null itr->second still coming into ProcessSpawnConditional
  6566. {
  6567. SpawnLocation* loc = spawn_location_list[itr->second->GetSpawnLocationID()];
  6568. if (loc && loc->conditional > 0) {
  6569. if ((loc->conditional & condition) != condition) {
  6570. Despawn(itr->second, 0);
  6571. }
  6572. }
  6573. }
  6574. }
  6575. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6576. map<int32, SpawnLocation*>::iterator itr2;
  6577. for (itr2 = spawn_location_list.begin(); itr2 != spawn_location_list.end(); itr2++) {
  6578. SpawnLocation* loc = itr2->second;
  6579. if (loc && loc->conditional > 0 && ((loc->conditional & condition) == condition))
  6580. if (GetSpawnByLocationID(loc->placement_id) == NULL)
  6581. ProcessSpawnLocation(loc);
  6582. }
  6583. MSpawnLocationList.releasereadlock(__FUNCTION__, __LINE__);
  6584. }
  6585. void ZoneServer::AddSpawnProximities(Spawn* newSpawn) {
  6586. Spawn* spawn = 0;
  6587. map<int32, Spawn*>::iterator itr;
  6588. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6589. MPendingSpawnListAdd.readlock(__FUNCTION__, __LINE__);
  6590. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6591. spawn = itr->second;
  6592. if (spawn && spawn != newSpawn) {
  6593. if (newSpawn->GetDatabaseID())
  6594. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  6595. if (newSpawn->GetSpawnLocationID())
  6596. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  6597. if (spawn->GetDatabaseID())
  6598. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  6599. if (spawn->GetSpawnLocationID())
  6600. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  6601. }
  6602. }
  6603. list<Spawn*>::iterator itr2;
  6604. for (itr2 = pending_spawn_list_add.begin(); itr2 != pending_spawn_list_add.end(); itr2++) {
  6605. spawn = *itr2;
  6606. if (spawn && spawn != newSpawn) {
  6607. if (newSpawn->GetDatabaseID())
  6608. spawn->AddSpawnToProximity(newSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  6609. if (newSpawn->GetSpawnLocationID())
  6610. spawn->AddSpawnToProximity(newSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  6611. if (spawn->GetDatabaseID())
  6612. newSpawn->AddSpawnToProximity(spawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  6613. if (spawn->GetSpawnLocationID())
  6614. newSpawn->AddSpawnToProximity(spawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  6615. }
  6616. }
  6617. MPendingSpawnListAdd.releasereadlock(__FUNCTION__, __LINE__);
  6618. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6619. }
  6620. // we only call this inside a write lock
  6621. void ZoneServer::RemoveSpawnProximities(Spawn* oldSpawn) {
  6622. Spawn* spawn = 0;
  6623. map<int32, Spawn*>::iterator itr;
  6624. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6625. spawn = itr->second;
  6626. if (spawn && spawn != oldSpawn) {
  6627. if (oldSpawn->GetDatabaseID())
  6628. spawn->RemoveSpawnFromProximity(oldSpawn->GetDatabaseID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_DATABASE_ID);
  6629. if (oldSpawn->GetSpawnLocationID())
  6630. spawn->RemoveSpawnFromProximity(oldSpawn->GetSpawnLocationID(), Spawn::SpawnProximityType::SPAWNPROXIMITY_LOCATION_ID);
  6631. // don't need to remove oldSpawn proximities, we clear them all out
  6632. }
  6633. }
  6634. }
  6635. void ZoneServer::SetSpawnScript(SpawnEntry* entry, Spawn* spawn)
  6636. {
  6637. if (!entry || !spawn)
  6638. return;
  6639. const char* script = 0;
  6640. for (int x = 0; x < 3; x++)
  6641. {
  6642. switch (x)
  6643. {
  6644. case 0:
  6645. script = world.GetSpawnEntryScript(entry->spawn_entry_id);
  6646. break;
  6647. case 1:
  6648. script = world.GetSpawnLocationScript(entry->spawn_location_id);
  6649. break;
  6650. case 2:
  6651. script = world.GetSpawnScript(entry->spawn_id);
  6652. break;
  6653. }
  6654. if (script && lua_interface && lua_interface->GetSpawnScript(script) != 0)
  6655. {
  6656. spawn->SetSpawnScript(string(script));
  6657. break;
  6658. }
  6659. }
  6660. }
  6661. vector<HouseItem> ZoneServer::GetHouseItems(Client* client)
  6662. {
  6663. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  6664. return std::vector<HouseItem>();
  6665. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  6666. std::vector<HouseItem> items;
  6667. map<int32, Spawn*>::iterator itr;
  6668. Spawn* spawn = 0;
  6669. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6670. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6671. spawn = itr->second;
  6672. if (spawn && spawn->IsObject() && spawn->GetPickupItemID())
  6673. {
  6674. HouseItem tmpItem;
  6675. tmpItem.item_id = spawn->GetPickupItemID();
  6676. tmpItem.unique_id = spawn->GetPickupUniqueItemID();
  6677. tmpItem.spawn_id = spawn->GetID();
  6678. tmpItem.item = master_item_list.GetItem(spawn->GetPickupItemID());
  6679. if (!tmpItem.item)
  6680. continue;
  6681. items.push_back(tmpItem);
  6682. }
  6683. }
  6684. MSpawnList.releasereadlock(__FUNCTION__, __LINE__);
  6685. return items;
  6686. }
  6687. void ZoneServer::SendHouseItems(Client* client)
  6688. {
  6689. if (!client->GetCurrentZone()->GetInstanceID() || !client->HasOwnerOrEditAccess())
  6690. return;
  6691. PacketStruct* packet = configReader.getStruct("WS_HouseItemsList", client->GetVersion());
  6692. std::vector<HouseItem> items = GetHouseItems(client);
  6693. // setting this to 1 puts it on the door widget
  6694. packet->setDataByName("is_widget_door", 1);
  6695. packet->setArrayLengthByName("num_items", items.size());
  6696. for (int i = 0; i < items.size(); i++)
  6697. {
  6698. HouseItem tmpItem = items[i];
  6699. packet->setArrayDataByName("unique_id", tmpItem.unique_id, i); // unique_id is in fact the item_id...
  6700. packet->setArrayDataByName("item_name", tmpItem.item->name.c_str(), i);
  6701. packet->setArrayDataByName("status_reduction", tmpItem.item->houseitem_info->status_rent_reduction, i);
  6702. // location, 0 = floor, 1 = ceiling
  6703. //packet->setArrayDataByName("location", 1, i, 0);
  6704. // item_state int8
  6705. // 0 = normal (cannot pick up item / move item / toggle visibility)
  6706. // 1 = virtual (toggle visibility available, no move item)
  6707. // 2 = hidden (cannot pick up item / move item / toggle visibility)
  6708. // 3 = virtual/hidden/toggle visibility
  6709. // 4 = none (cannot pick up item / move item / toggle visibility)
  6710. // 5 = none, toggle visibility (cannot pick up item / move item)
  6711. // 8 = none (cannot pick up item / move item / toggle visibility)
  6712. //packet->setArrayDataByName("item_state", tmpvalue, i, 0);
  6713. // makes it so we don't have access to move item/retrieve item
  6714. // cannot use in conjunction with ui_tab_flag1/ui_tab_flag2
  6715. //packet->setArrayDataByName("tradeable", 1, i);
  6716. //packet->setArrayDataByName("item_description", "failboat", i);
  6717. // access to move item/retrieve item, do not use in conjunction with tradeable
  6718. packet->setArrayDataByName("ui_tab_flag1", 1, i, 0);
  6719. packet->setArrayDataByName("ui_tab_flag2", 1, i, 0);
  6720. // both of these can serve as description fields (only one should be used they populate the same area below the item name)
  6721. //packet->setArrayDataByName("first_item_description", "test", i);
  6722. //packet->setArrayDataByName("second_item_description", "Description here!", i);
  6723. packet->setArrayDataByName("icon", tmpItem.item->details.icon, i);
  6724. }
  6725. EQ2Packet* pack = packet->serialize();
  6726. client->QueuePacket(pack);
  6727. safe_delete(packet);
  6728. }
  6729. Spawn* ZoneServer::GetSpawnFromUniqueItemID(int32 unique_id)
  6730. {
  6731. if (!GetInstanceID() || GetInstanceType() != Instance_Type::PERSONAL_HOUSE_INSTANCE)
  6732. return nullptr;
  6733. map<int32, Spawn*>::iterator itr;
  6734. Spawn* spawn = 0;
  6735. MSpawnList.readlock(__FUNCTION__, __LINE__);
  6736. for (itr = spawn_list.begin(); itr != spawn_list.end(); itr++) {
  6737. spawn = itr->second;
  6738. if (spawn && spawn->IsObject() && spawn->GetPickupUniqueItemID() == unique_id)
  6739. {
  6740. Spawn* tmpSpawn = spawn;
  6741. MSpawnList.releasereadlock();
  6742. return tmpSpawn;
  6743. }
  6744. }
  6745. MSpawnList.releasereadlock();
  6746. return nullptr;
  6747. }
  6748. void ZoneServer::AddPendingSpawnRemove(int32 id)
  6749. {
  6750. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  6751. m_pendingSpawnRemove.push_back(id);
  6752. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  6753. }
  6754. void ZoneServer::ProcessSpawnRemovals()
  6755. {
  6756. MPendingSpawnRemoval.writelock(__FUNCTION__, __LINE__);
  6757. if (m_pendingSpawnRemove.size() > 0) {
  6758. MSpawnList.writelock(__FUNCTION__, __LINE__);
  6759. vector<int32>::iterator itr2;
  6760. for (itr2 = m_pendingSpawnRemove.begin(); itr2 != m_pendingSpawnRemove.end(); itr2++)
  6761. spawn_list.erase(*itr2);
  6762. m_pendingSpawnRemove.clear();
  6763. MSpawnList.releasewritelock(__FUNCTION__, __LINE__);
  6764. }
  6765. MPendingSpawnRemoval.releasewritelock(__FUNCTION__, __LINE__);
  6766. }