zoneserver.cpp 269 KB

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