zoneserver.cpp 269 KB

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