zoneserver.cpp 290 KB

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