zoneserver.cpp 255 KB

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