zoneserver.cpp 259 KB

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