WorldDatabase.cpp 283 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857
  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 <math.h>
  17. #include <iostream>
  18. #include <sstream>
  19. #include <iomanip>
  20. #include <ios>
  21. #include <assert.h>
  22. #include "WorldDatabase.h"
  23. #include "../common/debug.h"
  24. #include "../common/packet_dump.h"
  25. #include "../common/GlobalHeaders.h"
  26. #include "Items/Items.h"
  27. #include "Factions.h"
  28. #include "World.h"
  29. #include "Variables.h"
  30. #include "VisualStates.h"
  31. #include "Appearances.h"
  32. #include "Skills.h"
  33. #include "Quests.h"
  34. #include "LuaInterface.h"
  35. #include "classes.h"
  36. #include "../common/Log.h"
  37. #include "Rules/Rules.h"
  38. #include "Titles.h"
  39. #include "Languages.h"
  40. #include "Traits/Traits.h"
  41. #include "ClientPacketFunctions.h"
  42. #include "Zone/ChestTrap.h"
  43. #include "../common/version.h"
  44. extern Classes classes;
  45. extern Commands commands;
  46. extern MasterTitlesList master_titles_list;
  47. extern MasterItemList master_item_list;
  48. extern MasterSpellList master_spell_list;
  49. extern MasterTraitList master_trait_list;
  50. extern MasterFactionList master_faction_list;
  51. extern World world;
  52. extern Variables variables;
  53. extern VisualStates visual_states;
  54. extern Appearances master_appearance_list;
  55. extern MasterSkillList master_skill_list;
  56. extern MasterQuestList master_quest_list;
  57. extern LuaInterface* lua_interface;
  58. extern ZoneList zone_list;
  59. extern GuildList guild_list;
  60. extern MasterCollectionList master_collection_list;
  61. extern RuleManager rule_manager;
  62. extern MasterLanguagesList master_languages_list;
  63. extern ChestTrapList chest_trap_list;
  64. //devn00b: Fix for linux builds since we dont use stricmp we use strcasecmp
  65. #if defined(__GNUC__)
  66. #define stricmp strcasecmp
  67. #define strnicmp strncasecmp
  68. #endif
  69. WorldDatabase::WorldDatabase(){
  70. }
  71. WorldDatabase::~WorldDatabase(){
  72. }
  73. bool WorldDatabase::ConnectNewDatabase() {
  74. /*
  75. TESTS
  76. database_new.Connect();
  77. DatabaseResult result;
  78. database_new.Select(&result, "select name from characters where id=1");
  79. if (result.Next()) {
  80. printf("'%s'\n", result.GetStringStr("name"));
  81. printf("'%s'\n", result.GetStringStr("nameBAD"));
  82. printf("'%s'\n", result.GetString(3));
  83. }
  84. return true;
  85. */
  86. return database_new.Connect();
  87. }
  88. void WorldDatabase::PingNewDB()
  89. {
  90. database_new.PingNewDB();
  91. }
  92. void WorldDatabase::DeleteBuyBack(int32 char_id, int32 item_id, int16 quantity, int32 price) {
  93. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "Deleting Buyback - Player: %u, Item ID: %u, Qty: %i, Price: %u", char_id, item_id, quantity, price);
  94. Query query;
  95. query.RunQuery2(Q_DELETE, "DELETE FROM character_buyback WHERE char_id = %u AND item_id = %u AND quantity = %i AND price = %u", char_id, item_id, quantity, price);
  96. }
  97. void WorldDatabase::LoadBuyBacks(Client* client) {
  98. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "Loading Buyback - Player: %u", client->GetCharacterID());
  99. Query query;
  100. MYSQL_ROW row;
  101. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, item_id, quantity, price FROM character_buyback where char_id = %u ORDER BY id desc limit 10", client->GetCharacterID());
  102. int8 count = 0;
  103. int32 last_id = 0;
  104. if(result)
  105. {
  106. while(result && (row = mysql_fetch_row(result)))
  107. {
  108. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "AddBuyBack: item: %u, qty: %i, price: %u", atoul(row[1]), atoi(row[2]), atoul(row[3]));
  109. last_id = atoul(row[0]);
  110. client->AddBuyBack(last_id, atoul(row[1]), atoi(row[2]), atoul(row[3]), false);
  111. count++;
  112. }
  113. if(count >= 10)
  114. {
  115. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "Deleting excess Buyback from Player: %u", client->GetCharacterID());
  116. Query query2;
  117. query2.RunQuery2(Q_DELETE, "DELETE FROM character_buyback WHERE char_id = %u AND id < %u", client->GetCharacterID(), last_id);
  118. }
  119. }
  120. }
  121. void WorldDatabase::SaveBuyBacks(Client* client)
  122. {
  123. LogWrite(MERCHANT__DEBUG, 3, "Merchant", "Saving Buybacks - Player: %u", client->GetCharacterID());
  124. deque<BuyBackItem*>* buybacks = client->GetBuyBacks();
  125. if(buybacks && buybacks->size() > 0)
  126. {
  127. BuyBackItem* item = 0;
  128. deque<BuyBackItem*>::iterator itr;
  129. for(itr = buybacks->begin(); itr != buybacks->end(); itr++)
  130. {
  131. item = *itr;
  132. if(item && item->save_needed)
  133. {
  134. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "SaveBuyBack: char: %u, item: %u, qty: %i, price: %u", client->GetCharacterID(), item->item_id, item->quantity, item->price);
  135. SaveBuyBack(client->GetCharacterID(), item->item_id, item->quantity, item->price);
  136. item->save_needed = false;
  137. }
  138. }
  139. }
  140. }
  141. void WorldDatabase::SaveBuyBack(int32 char_id, int32 item_id, int16 quantity, int32 price)
  142. {
  143. LogWrite(MERCHANT__DEBUG, 3, "Merchant", "Saving Buyback - Player: %u, Item ID: %u, Qty: %i, Price: %u", char_id, item_id, quantity, price);
  144. Query query;
  145. string insert = string("INSERT INTO character_buyback (char_id, item_id, quantity, price) VALUES (%u, %u, %i, %u) ");
  146. query.AddQueryAsync(char_id, this, Q_INSERT, insert.c_str(), char_id, item_id, quantity, price);
  147. }
  148. int32 WorldDatabase::LoadCharacterSpells(int32 char_id, Player* player)
  149. {
  150. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Character Spells for player %s...", player->GetName());
  151. Query query;
  152. MYSQL_ROW row;
  153. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spell_id, tier, knowledge_slot, spell_book_type, linked_timer_id FROM character_spells, spells where character_spells.spell_id = spells.id and character_spells.char_id = %u ORDER BY spell_id, tier desc", char_id);
  154. int32 old_spell_id = 0;
  155. int32 new_spell_id = 0;
  156. int32 count = 0;
  157. if(result && mysql_num_rows(result) >0)
  158. {
  159. while(result && (row = mysql_fetch_row(result)))
  160. {
  161. count++;
  162. new_spell_id = atoul(row[0]);
  163. if(new_spell_id == old_spell_id)
  164. continue;
  165. LogWrite(SPELL__DEBUG, 5, "Spells", "\tLoading SpellID: %u, tier: %i, slot: %i, type: %u linked_timer_id: %u", new_spell_id, atoi(row[1]), atoi(row[2]), atoul(row[3]), atoul(row[4]));
  166. player->AddSpellBookEntry(new_spell_id, atoi(row[1]), atoi(row[2]), atoul(row[3]), atoul(row[4]));
  167. old_spell_id = new_spell_id;
  168. }
  169. }
  170. return count;
  171. }
  172. void WorldDatabase::SavePlayerSpells(Client* client)
  173. {
  174. if(!client || client->GetCharacterID() < 1)
  175. return;
  176. LogWrite(SPELL__DEBUG, 3, "Spells", "Saving Spell(s) for Player: '%s'", client->GetPlayer()->GetName());
  177. vector<SpellBookEntry*>* spells = client->GetPlayer()->GetSpellsSaveNeeded();
  178. if(spells)
  179. {
  180. vector<SpellBookEntry*>::iterator itr;
  181. SpellBookEntry* spell = 0;
  182. for(itr = spells->begin(); itr != spells->end(); itr++)
  183. {
  184. spell = *itr;
  185. Query query;
  186. LogWrite(SPELL__DEBUG, 5, "Spells", "\tSaving SpellID: %u, tier: %i, slot: %i", spell->spell_id, spell->tier, spell->slot);
  187. query.AddQueryAsync(client->GetCharacterID(), this, Q_INSERT, "INSERT INTO character_spells (char_id, spell_id, tier) SELECT %u, %u, %i ON DUPLICATE KEY UPDATE tier = %i",
  188. client->GetPlayer()->GetCharacterID(), spell->spell_id, spell->tier, spell->tier);
  189. spell->save_needed = false;
  190. }
  191. safe_delete(spells);
  192. }
  193. }
  194. int32 WorldDatabase::LoadCharacterSkills(int32 char_id, Player* player)
  195. {
  196. Query query;
  197. MYSQL_ROW row;
  198. int32 count = 0;
  199. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT skill_id, current_val, max_val FROM character_skills, skills where character_skills.skill_id = skills.id and character_skills.char_id = %u", char_id);
  200. if(result && mysql_num_rows(result) >0)
  201. {
  202. while(result && (row = mysql_fetch_row(result)))
  203. {
  204. count++;
  205. LogWrite(SKILL__DEBUG, 5, "Skills", "Loading SkillID: %u, cur_val: %i, max_val: %l", strtoul(row[0], NULL, 0), atoi(row[1]), atoi(row[2]));
  206. player->AddSkill(strtoul(row[0], NULL, 0), atoi(row[1]), atoi(row[2]));
  207. }
  208. }
  209. return count;
  210. }
  211. void WorldDatabase::DeleteCharacterSkill(int32 char_id, Skill* skill)
  212. {
  213. if (char_id > 0 && skill)
  214. {
  215. LogWrite(SKILL__DEBUG, 0, "Skills", "Deleting Skill '%s' (%u) from char_id: %u", skill->name.data.c_str(), skill->skill_id, char_id);
  216. Query query;
  217. query.RunQuery2(Q_DELETE, "DELETE FROM `character_skills` WHERE `char_id`=%u AND `skill_id`=%u", char_id, skill->skill_id);
  218. }
  219. }
  220. int32 WorldDatabase::LoadSkills()
  221. {
  222. int32 total = 0;
  223. Query query;
  224. MYSQL_ROW row;
  225. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, short_name, name, description, skill_type, display FROM skills");
  226. if(result)
  227. {
  228. if (mysql_num_rows(result) >0)
  229. {
  230. Skill* skill = 0;
  231. while(result && (row = mysql_fetch_row(result)))
  232. {
  233. skill = new Skill();
  234. skill->skill_id = strtoul(row[0], NULL, 0);
  235. skill->short_name.data = string(row[1]);
  236. skill->short_name.size = skill->short_name.data.length();
  237. skill->name.data = string(row[2]);
  238. skill->name.size = skill->name.data.length();
  239. skill->description.data = string(row[3]);
  240. skill->description.size = skill->description.data.length();
  241. skill->skill_type = strtoul(row[4], NULL, 0);
  242. skill->display = atoi(row[5]);
  243. master_skill_list.AddSkill(skill);
  244. total++;
  245. LogWrite(SKILL__DEBUG, 5, "Skill", "---Loading Skill: %s (%u)", skill->name.data.c_str(), skill->skill_id);
  246. LogWrite(SKILL__DEBUG, 7, "Skill", "---short_name: %s, type: %i, display: %i", skill->short_name.data.c_str(), skill->skill_type, skill->display);
  247. }
  248. }
  249. }
  250. LogWrite(SKILL__DEBUG, 3, "Skill", "--Loaded %u Skill(s)", total);
  251. return total;
  252. }
  253. map<int8, vector<MacroData*> >* WorldDatabase::LoadCharacterMacros(int32 char_id)
  254. {
  255. Query query;
  256. MYSQL_ROW row;
  257. int32 total = 0;
  258. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT macro_number, macro_name, macro_icon, macro_text FROM character_macros where char_id = %u ORDER BY macro_number, id", char_id);
  259. if(result && mysql_num_rows(result) >0)
  260. {
  261. map<int8, vector<MacroData*> >* macros = new map<int8, vector<MacroData*> >;
  262. while(result && (row = mysql_fetch_row(result)))
  263. {
  264. MacroData* data = new MacroData;
  265. data->name = row[1];
  266. data->icon = atoi(row[2]);
  267. data->text = row[3];
  268. (*macros)[atoi(row[0])].push_back(data);
  269. total++;
  270. LogWrite(PLAYER__DEBUG, 5, "Player", "\tLoading macro: %i. %s for player: %u", atoi(row[0]), row[1], char_id);
  271. }
  272. LogWrite(PLAYER__DEBUG, 0, "Player", "\tLoaded %u macro%s", total, total == 1 ? "" : "s");
  273. return macros;
  274. }
  275. return 0;
  276. }
  277. void WorldDatabase::UpdateCharacterMacro(int32 char_id, int8 number, const char* name, int16 icon, vector<string>* updates)
  278. {
  279. LogWrite(PLAYER__DEBUG, 0, "Player", "Update player id %u macro: %i", char_id, number);
  280. Query query;
  281. Query query2;
  282. query.RunQuery2(Q_DELETE, "delete FROM character_macros where char_id = %u and macro_number = %i", char_id, number);
  283. if(name && updates && updates->size() > 0)
  284. {
  285. for(int8 i=0;i<updates->size();i++)
  286. {
  287. query2.RunQuery2(Q_INSERT, "insert into character_macros (char_id, macro_number, macro_name, macro_icon, macro_text) values(%u, %i, '%s', %i, '%s')", char_id, number, getSafeEscapeString(name).c_str(), icon, getSafeEscapeString(updates->at(i).c_str()).c_str());
  288. LogWrite(PLAYER__DEBUG, 5, "Player", "\tAdding macro: %s, %s (Player: %u)", name, updates->at(i).c_str(), char_id);
  289. }
  290. }
  291. }
  292. //we use our timestamp just in case db is on another server, otherwise times might be off
  293. void WorldDatabase::UpdateVitality(int32 timestamp, float amount){
  294. Query query;
  295. LogWrite(PLAYER__DEBUG, 3, "Player", "Reset Vitality > 100: %f", amount);
  296. query.RunQuery2(Q_UPDATE, "update character_details set xp_vitality=100 where (xp_vitality + %f) > 100", amount);
  297. LogWrite(PLAYER__DEBUG, 3, "Player", "Update Vitality <= 100: %f", amount);
  298. query.RunQuery2(Q_UPDATE, "update character_details set xp_vitality=(xp_vitality+%f) where (xp_vitality + %f) <= 100", amount, amount);
  299. LogWrite(PLAYER__DEBUG, 3, "Player", "Update Vitality Timer: %u", timestamp);
  300. query.RunQuery2(Q_UPDATE, "update variables set variable_value=%u where variable_name='vitalitytimer'", timestamp);
  301. }
  302. void WorldDatabase::SaveVariable(const char* name, const char* value, const char* comment){
  303. LogWrite(WORLD__DEBUG, 0, "Variables", "Saving Variable: %s = %s", name, value);
  304. Query query;
  305. if(comment){
  306. query.RunQuery2(Q_REPLACE, "replace into variables (variable_name, variable_value, comment) values('%s', '%s', '%s')",
  307. getSafeEscapeString(name).c_str(), getSafeEscapeString(value).c_str(), getSafeEscapeString(comment).c_str());
  308. }
  309. else{
  310. query.RunQuery2(Q_REPLACE, "replace into variables (variable_name, variable_value) values('%s', '%s')",
  311. getSafeEscapeString(name).c_str(), getSafeEscapeString(value).c_str());
  312. }
  313. }
  314. void WorldDatabase::LoadGlobalVariables(){
  315. variables.ClearVariables ( );
  316. int32 total = 0;
  317. Query query;
  318. MYSQL_ROW row;
  319. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT variable_name, variable_value, comment FROM variables");
  320. while(result && (row = mysql_fetch_row(result)))
  321. {
  322. Variable* newVar = new Variable(row[0], row[1], row[2]);
  323. variables.AddVariable(newVar);
  324. total++;
  325. LogWrite(WORLD__DEBUG, 5, "World", "---Loading variable: '%s' = '%s'", row[0], row[1]);
  326. }
  327. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u variables", total);
  328. }
  329. void WorldDatabase::LoadAppearanceMasterList()
  330. {
  331. DatabaseResult result;
  332. int32 total = 0;
  333. int32 appearance_id;
  334. int16 appearance_version;
  335. master_appearance_list.ClearAppearances();
  336. database_new.Select(&result, "SELECT appearance_id, `name`, min_client_version FROM appearances ORDER BY appearance_id");
  337. while( result.Next() )
  338. {
  339. appearance_id = result.GetInt32Str("appearance_id");
  340. const char *appearance_name = result.GetStringStr("name");
  341. appearance_version = result.GetInt16Str("min_client_version");
  342. Appearance* a = new Appearance(appearance_id, appearance_name, appearance_version);
  343. master_appearance_list.InsertAppearance(a);
  344. total++;
  345. LogWrite(WORLD__DEBUG, 5, "World", "---Loading appearances: '%s' (%i)", appearance_name, appearance_id);
  346. }
  347. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u appearances", total);
  348. }
  349. void WorldDatabase::LoadVisualStates()
  350. {
  351. visual_states.Reset();
  352. int32 total = 0;
  353. Query query;
  354. Query query2;
  355. MYSQL_ROW row;
  356. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT visual_state_id, name FROM visual_states");
  357. while(result && (row = mysql_fetch_row(result)))
  358. {
  359. VisualState* vs = new VisualState(atoi(row[0]), row[1]);
  360. visual_states.InsertVisualState(vs);
  361. total++;
  362. LogWrite(WORLD__DEBUG, 5, "World", "---Loading visual state: '%s' (%i)", row[1], atoi(row[0]));
  363. }
  364. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u visual states", total);
  365. total = 0;
  366. result = query2.RunQuery2(Q_SELECT, "SELECT name, visual_state_id, message, targeted_message, min_version_range, max_version_range FROM emotes");
  367. while(result && (row = mysql_fetch_row(result)))
  368. {
  369. EmoteVersionRange* range = 0;
  370. if ((range = visual_states.FindEmoteRange(string(row[0]))) == NULL)
  371. {
  372. range = new EmoteVersionRange(row[0]);
  373. visual_states.InsertEmoteRange(range);
  374. }
  375. range->AddVersionRange(atoul(row[4]),atoul(row[5]), row[0], atoi(row[1]), row[2], row[3]);
  376. total++;
  377. LogWrite(WORLD__DEBUG, 5, "World", "---Loading emote state: '%s' (%i)", row[1], atoi(row[0]));
  378. }
  379. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u emote state(s)", total);
  380. }
  381. void WorldDatabase::LoadSubCommandList()
  382. {
  383. int32 total = 0;
  384. Query query;
  385. MYSQL_ROW row;
  386. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT command, subcommand, handler, required_status FROM commands where length(subcommand) > 0 ORDER BY handler asc");
  387. while(result && (row = mysql_fetch_row(result)))
  388. {
  389. commands.GetRemoteCommands()->CheckAddSubCommand(string(row[0]), EQ2_RemoteCommandString(row[1], (int32)strtoul(row[2], NULL, 0), atoi(row[3])));
  390. total++;
  391. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Command: '%s', sub '%s', handler, %u status %i", row[0], row[1], atoul(row[2]), atoi(row[3]));
  392. }
  393. LogWrite(COMMAND__DEBUG, 3, "Command", "--Loaded %i Subcommand(s)", total);
  394. }
  395. void WorldDatabase::LoadCommandList()
  396. {
  397. Query query;
  398. MYSQL_ROW row;
  399. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT command, handler, required_status FROM commands where length(subcommand) = 0 ORDER BY handler asc");
  400. int16 index = 0;
  401. while(result && (row = mysql_fetch_row(result)))
  402. {
  403. int32 handler = strtoul(row[1], NULL, 0);
  404. while(handler>index && handler != 999)
  405. {
  406. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Remote Commands: handler %u, index %u", handler, index);
  407. commands.GetRemoteCommands()->addZero();
  408. index++;
  409. }
  410. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Commands: handler %u, index %u", handler, index);
  411. commands.GetRemoteCommands()->addCommand(EQ2_RemoteCommandString(row[0], handler, atoi(row[2])));
  412. index++;
  413. }
  414. LogWrite(COMMAND__DEBUG, 3, "Command", "--Loaded %i Command%s", index, index > 0 ? "s" : "");
  415. LoadSubCommandList();
  416. }
  417. int32 WorldDatabase::LoadNPCSpells(ZoneServer* zone){
  418. Query query;
  419. MYSQL_ROW row;
  420. int32 count = 0;
  421. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spell_list_id, spell_id, spell_tier FROM spawn_npc_spells where spell_list_id > 0");
  422. while(result && (row = mysql_fetch_row(result))){
  423. zone->AddNPCSpell(atoul(row[0]), atoul(row[1]), atoi(row[2]));
  424. count++;
  425. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC Spell List: %u, spell id: %u, tier: %i", atoul(row[0]), atoul(row[1]), atoi(row[2]));
  426. }
  427. return count;
  428. }
  429. int32 WorldDatabase::LoadNPCSkills(ZoneServer* zone){
  430. Query query;
  431. MYSQL_ROW row;
  432. int32 count = 0;
  433. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT skill_list_id, skill_id, starting_value FROM spawn_npc_skills");
  434. while(result && (row = mysql_fetch_row(result))){
  435. zone->AddNPCSkill(atoul(row[0]), atoul(row[1]), atoi(row[2]));
  436. count++;
  437. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC Skill List: %u, skill id: %u, value: %i", atoul(row[0]), atoul(row[1]), atoi(row[2]));
  438. }
  439. return count;
  440. }
  441. int32 WorldDatabase::LoadNPCEquipment(ZoneServer* zone){
  442. Query query;
  443. MYSQL_ROW row;
  444. int32 count = 0;
  445. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT equipment_list_id, item_id FROM spawn_npc_equipment");
  446. while(result && (row = mysql_fetch_row(result))){
  447. zone->AddNPCEquipment(atoul(row[0]), atoul(row[1]));
  448. count++;
  449. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC Equipment List: %u, item: %u", atoul(row[0]), atoul(row[1]));
  450. }
  451. return count;
  452. }
  453. int8 WorldDatabase::GetAppearanceType(string type){
  454. int8 ret = 255;
  455. if (type == "soga_hair_face_highlight_color")
  456. ret = APPEARANCE_SOGA_HFHC;
  457. else if (type == "soga_hair_type_highlight_color")
  458. ret = APPEARANCE_SOGA_HTHC;
  459. else if (type == "soga_hair_face_color")
  460. ret = APPEARANCE_SOGA_HFC;
  461. else if (type == "soga_hair_type_color")
  462. ret = APPEARANCE_SOGA_HTC;
  463. else if (type == "soga_hair_highlight")
  464. ret = APPEARANCE_SOGA_HH;
  465. else if (type == "soga_hair_color1")
  466. ret = APPEARANCE_SOGA_HC1;
  467. else if (type == "soga_hair_color2")
  468. ret = APPEARANCE_SOGA_HC2;
  469. else if (type == "hair_type_color")
  470. ret = APPEARANCE_HTC;
  471. else if (type == "soga_skin_color")
  472. ret = APPEARANCE_SOGA_SC;
  473. else if (type == "soga_eye_color")
  474. ret = APPEARANCE_SOGA_EC;
  475. else if (type == "hair_type_highlight_color")
  476. ret = APPEARANCE_HTHC;
  477. else if (type == "hair_face_highlight_color")
  478. ret = APPEARANCE_HFHC;
  479. else if (type == "hair_face_color")
  480. ret = APPEARANCE_HFC;
  481. else if (type == "hair_highlight")
  482. ret = APPEARANCE_HH;
  483. else if (type == "hair_color1")
  484. ret = APPEARANCE_HC1;
  485. else if (type == "wing_color1")
  486. ret = APPEARANCE_WC1;
  487. else if (type == "hair_color2")
  488. ret = APPEARANCE_HC2;
  489. else if (type == "wing_color2")
  490. ret = APPEARANCE_WC2;
  491. else if (type == "skin_color")
  492. ret = APPEARANCE_SC;
  493. else if (type == "eye_color")
  494. ret = APPEARANCE_EC;
  495. else if (type == "soga_eye_brow_type")
  496. ret = APPEARANCE_SOGA_EBT;
  497. else if (type == "soga_cheek_type")
  498. ret = APPEARANCE_SOGA_CHEEKT;
  499. else if (type == "soga_nose_type")
  500. ret = APPEARANCE_SOGA_NT;
  501. else if (type == "soga_chin_type")
  502. ret = APPEARANCE_SOGA_CHINT;
  503. else if (type == "soga_lip_type")
  504. ret = APPEARANCE_SOGA_LT;
  505. else if (type == "eye_brow_type")
  506. ret = APPEARANCE_EBT;
  507. else if (type == "soga_ear_type")
  508. ret = APPEARANCE_SOGA_EART;
  509. else if (type == "soga_eye_type")
  510. ret = APPEARANCE_SOGA_EYET;
  511. else if (type == "cheek_type")
  512. ret = APPEARANCE_CHEEKT;
  513. else if (type == "nose_type")
  514. ret = APPEARANCE_NT;
  515. else if (type == "chin_type")
  516. ret = APPEARANCE_CHINT;
  517. else if (type == "ear_type")
  518. ret = APPEARANCE_EART;
  519. else if (type == "eye_type")
  520. ret = APPEARANCE_EYET;
  521. else if (type == "lip_type")
  522. ret = APPEARANCE_LT;
  523. else if (type == "shirt_color")
  524. ret = APPEARANCE_SHIRT;
  525. else if (type == "unknown_chest_color")
  526. ret = APPEARANCE_UCC;
  527. else if (type == "pants_color")
  528. ret = APPEARANCE_PANTS;
  529. else if (type == "unknown_legs_color")
  530. ret = APPEARANCE_ULC;
  531. else if (type == "unknown9")
  532. ret = APPEARANCE_U9;
  533. else if (type == "body_size")
  534. ret = APPEARANCE_BODY_SIZE;
  535. else if (type == "soga_wing_color1")
  536. ret = APPEARANCE_SOGA_WC1;
  537. else if (type == "soga_wing_color2")
  538. ret = APPEARANCE_SOGA_WC2;
  539. else if (type == "soga_shirt_color")
  540. ret = APPEARANCE_SOGA_SHIRT;
  541. else if (type == "soga_unknown_chest_color")
  542. ret = APPEARANCE_SOGA_UCC;
  543. else if (type == "soga_pants_color")
  544. ret = APPEARANCE_SOGA_PANTS;
  545. else if (type == "soga_unknown_legs_color")
  546. ret = APPEARANCE_SOGA_ULC;
  547. else if (type == "soga_unknown13")
  548. ret = APPEARANCE_SOGA_U13;
  549. else if (type == "body_age")
  550. ret = APPEARANCE_BODY_AGE;
  551. return ret;
  552. }
  553. int32 WorldDatabase::LoadAppearances(ZoneServer* zone, Client* client){
  554. Query query, query2;
  555. MYSQL_ROW row;
  556. int32 count = 0, spawn_id = 0, new_spawn_id = 0;
  557. Entity* entity = 0;
  558. if(client)
  559. entity = client->GetPlayer();
  560. map<string, int8> appearance_types;
  561. map<int32, map<int8, EQ2_Color> > appearance_colors;
  562. EQ2_Color color;
  563. color.red = 0;
  564. color.green = 0;
  565. color.blue = 0;
  566. string type;
  567. MYSQL_RES* result = 0;
  568. if(!client)
  569. result = query.RunQuery2(Q_SELECT, "SELECT distinct `type` FROM npc_appearance where length(type) > 0");
  570. else
  571. result = query.RunQuery2(Q_SELECT, "SELECT distinct `type` FROM char_colors where length(type) > 0 and char_id=%u", client->GetCharacterID());
  572. while(result && (row = mysql_fetch_row(result))){
  573. type = string(row[0]);
  574. appearance_types[type] = GetAppearanceType(type);
  575. if(appearance_types[type] == 255)
  576. LogWrite(WORLD__ERROR, 0, "Appearance", "Unknown appearance type '%s' in LoadAppearances.", type.c_str());
  577. }
  578. MYSQL_RES* result2 = 0;
  579. if(!client)
  580. result2 = query2.RunQuery2(Q_SELECT, "SELECT `type`, spawn_id, signed_value, red, green, blue FROM npc_appearance where length(type) > 0 ORDER BY spawn_id");
  581. else
  582. result2 = query2.RunQuery2(Q_SELECT, "SELECT `type`, char_id, signed_value, red, green, blue FROM char_colors where length(type) > 0 and char_id=%u", client->GetCharacterID());
  583. while(result2 && (row = mysql_fetch_row(result2))){
  584. if(!client){
  585. new_spawn_id = atoul(row[1]);
  586. if(spawn_id != new_spawn_id){
  587. entity = zone->GetNPC(new_spawn_id, true);
  588. if(!entity)
  589. continue;
  590. if(spawn_id > 0)
  591. count++;
  592. spawn_id = new_spawn_id;
  593. }
  594. }
  595. if(appearance_types[row[0]] < APPEARANCE_SOGA_EBT){
  596. color.red = atoi(row[3]);
  597. color.green = atoi(row[4]);
  598. color.blue = atoi(row[5]);
  599. }
  600. switch(appearance_types[row[0]]){
  601. case APPEARANCE_SOGA_HFHC:{
  602. entity->features.soga_hair_face_highlight_color = color;
  603. break;
  604. }
  605. case APPEARANCE_SOGA_HTHC:{
  606. entity->features.soga_hair_type_highlight_color = color;
  607. break;
  608. }
  609. case APPEARANCE_SOGA_HFC:{
  610. entity->features.soga_hair_face_color = color;
  611. break;
  612. }
  613. case APPEARANCE_SOGA_HTC:{
  614. entity->features.soga_hair_type_color = color;
  615. break;
  616. }
  617. case APPEARANCE_SOGA_HH:{
  618. entity->features.soga_hair_highlight_color = color;
  619. break;
  620. }
  621. case APPEARANCE_SOGA_HC1:{
  622. entity->features.soga_hair_color1 = color;
  623. break;
  624. }
  625. case APPEARANCE_SOGA_HC2:{
  626. entity->features.soga_hair_color2 = color;
  627. break;
  628. }
  629. case APPEARANCE_SOGA_SC:{
  630. entity->features.soga_skin_color = color;
  631. break;
  632. }
  633. case APPEARANCE_SOGA_EC:{
  634. entity->features.soga_eye_color = color;
  635. break;
  636. }
  637. case APPEARANCE_HTHC:{
  638. entity->features.hair_type_highlight_color = color;
  639. break;
  640. }
  641. case APPEARANCE_HFHC:{
  642. entity->features.hair_face_highlight_color = color;
  643. break;
  644. }
  645. case APPEARANCE_HTC:{
  646. entity->features.hair_type_color = color;
  647. break;
  648. }
  649. case APPEARANCE_HFC:{
  650. entity->features.hair_face_color = color;
  651. break;
  652. }
  653. case APPEARANCE_HH:{
  654. entity->features.hair_highlight_color = color;
  655. break;
  656. }
  657. case APPEARANCE_HC1:{
  658. entity->features.hair_color1 = color;
  659. break;
  660. }
  661. case APPEARANCE_HC2:{
  662. entity->features.hair_color2 = color;
  663. break;
  664. }
  665. case APPEARANCE_WC1:{
  666. entity->features.wing_color1 = color;
  667. break;
  668. }
  669. case APPEARANCE_WC2:{
  670. entity->features.wing_color2 = color;
  671. break;
  672. }
  673. case APPEARANCE_SC:{
  674. entity->features.skin_color = color;
  675. break;
  676. }
  677. case APPEARANCE_EC:{
  678. entity->features.eye_color = color;
  679. break;
  680. }
  681. case APPEARANCE_SOGA_EBT:{
  682. for(int i=0;i<3;i++)
  683. entity->features.soga_eye_brow_type[i] = atoi(row[3+i]);
  684. break;
  685. }
  686. case APPEARANCE_SOGA_CHEEKT:{
  687. for(int i=0;i<3;i++)
  688. entity->features.soga_cheek_type[i] = atoi(row[3+i]);
  689. break;
  690. }
  691. case APPEARANCE_SOGA_NT:{
  692. for(int i=0;i<3;i++)
  693. entity->features.soga_nose_type[i] = atoi(row[3+i]);
  694. break;
  695. }
  696. case APPEARANCE_SOGA_CHINT:{
  697. for(int i=0;i<3;i++)
  698. entity->features.soga_chin_type[i] = atoi(row[3+i]);
  699. break;
  700. }
  701. case APPEARANCE_SOGA_LT:{
  702. for(int i=0;i<3;i++)
  703. entity->features.soga_lip_type[i] = atoi(row[3+i]);
  704. break;
  705. }
  706. case APPEARANCE_SOGA_EART:{
  707. for(int i=0;i<3;i++)
  708. entity->features.soga_ear_type[i] = atoi(row[3+i]);
  709. break;
  710. }
  711. case APPEARANCE_SOGA_EYET:{
  712. for(int i=0;i<3;i++)
  713. entity->features.soga_eye_type[i] = atoi(row[3+i]);
  714. break;
  715. }
  716. case APPEARANCE_EBT:{
  717. for(int i=0;i<3;i++)
  718. entity->features.eye_brow_type[i] = atoi(row[3+i]);
  719. break;
  720. }
  721. case APPEARANCE_CHEEKT:{
  722. for(int i=0;i<3;i++)
  723. entity->features.cheek_type[i] = atoi(row[3+i]);
  724. break;
  725. }
  726. case APPEARANCE_NT:{
  727. for(int i=0;i<3;i++)
  728. entity->features.nose_type[i] = atoi(row[3+i]);
  729. break;
  730. }
  731. case APPEARANCE_CHINT:{
  732. for(int i=0;i<3;i++)
  733. entity->features.chin_type[i] = atoi(row[3+i]);
  734. break;
  735. }
  736. case APPEARANCE_EART:{
  737. for(int i=0;i<3;i++)
  738. entity->features.ear_type[i] = atoi(row[3+i]);
  739. break;
  740. }
  741. case APPEARANCE_EYET:{
  742. for(int i=0;i<3;i++)
  743. entity->features.eye_type[i] = atoi(row[3+i]);
  744. break;
  745. }
  746. case APPEARANCE_LT:{
  747. for(int i=0;i<3;i++)
  748. entity->features.lip_type[i] = atoi(row[3+i]);
  749. break;
  750. }
  751. case APPEARANCE_SHIRT:{
  752. entity->features.shirt_color = color;
  753. break;
  754. }
  755. case APPEARANCE_UCC:{
  756. break;
  757. }
  758. case APPEARANCE_PANTS:{
  759. entity->features.pants_color = color;
  760. break;
  761. }
  762. case APPEARANCE_ULC:{
  763. break;
  764. }
  765. case APPEARANCE_U9:{
  766. break;
  767. }
  768. case APPEARANCE_BODY_SIZE:{
  769. entity->features.body_size = color.red;
  770. break;
  771. }
  772. case APPEARANCE_SOGA_WC1:{
  773. break;
  774. }
  775. case APPEARANCE_SOGA_WC2:{
  776. break;
  777. }
  778. case APPEARANCE_SOGA_SHIRT:{
  779. break;
  780. }
  781. case APPEARANCE_SOGA_UCC:{
  782. break;
  783. }
  784. case APPEARANCE_SOGA_PANTS:{
  785. break;
  786. }
  787. case APPEARANCE_SOGA_ULC:{
  788. break;
  789. }
  790. case APPEARANCE_SOGA_U13:{
  791. break;
  792. }
  793. case APPEARANCE_BODY_AGE: {
  794. entity->features.body_age = color.red;
  795. break;
  796. }
  797. }
  798. }
  799. return count;
  800. }
  801. void WorldDatabase::LoadNPCs(ZoneServer* zone){
  802. Query query;
  803. MYSQL_ROW row;
  804. NPC* npc = 0;
  805. int32 id = 0;
  806. int32 total = 0;
  807. MYSQL_RES* result = query.RunQuery2(Q_SELECT,"SELECT npc.spawn_id, s.name, npc.min_level, npc.max_level, npc.enc_level, s.race, s.model_type, npc.class_, npc.gender, s.command_primary, s.command_secondary, s.show_name, npc.min_group_size, npc.max_group_size, npc.hair_type_id, npc.facial_hair_type_id, npc.wing_type_id, npc.chest_type_id, npc.legs_type_id, npc.soga_hair_type_id, npc.soga_facial_hair_type_id, s.attackable, s.show_level, s.targetable, s.show_command_icon, s.display_hand_icon, s.hp, s.power, s.size, s.collision_radius, npc.action_state, s.visual_state, npc.mood_state, npc.initial_state, npc.activity_status, s.faction_id, s.sub_title, s.merchant_id, s.merchant_type, s.size_offset, npc.attack_type, npc.ai_strategy+0, npc.spell_list_id, npc.secondary_spell_list_id, npc.skill_list_id, npc.secondary_skill_list_id, npc.equipment_list_id, npc.str, npc.sta, npc.wis, npc.intel, npc.agi, npc.heat, npc.cold, npc.magic, npc.mental, npc.divine, npc.disease, npc.poison, npc.aggro_radius, npc.cast_percentage, npc.randomize, npc.soga_model_type, npc.heroic_flag, npc.alignment, npc.elemental, npc.arcane, npc.noxious, s.savagery, s.dissonance, npc.hide_hood, npc.emote_state, s.prefix, s.suffix, s.last_name, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  808. "FROM spawn s\n"
  809. "INNER JOIN spawn_npcs npc\n"
  810. "ON s.id = npc.spawn_id\n"
  811. "INNER JOIN spawn_location_entry le\n"
  812. "ON npc.spawn_id = le.spawn_id\n"
  813. "INNER JOIN spawn_location_placement lp\n"
  814. "ON le.spawn_location_id = lp.spawn_location_id\n"
  815. "WHERE lp.zone_id = %u and (lp.instance_id = 0 or lp.instance_id = %u)\n"
  816. "GROUP BY s.id",
  817. zone->GetZoneID(), zone->GetInstanceID());
  818. while(result && (row = mysql_fetch_row(result))){
  819. /*npc->SetAppearanceID(atoi(row[12]));
  820. AppearanceData* appearance = world.GetNPCAppearance(npc->GetAppearanceID());
  821. if(appearance)
  822. memcpy(&npc->appearance, appearance, sizeof(AppearanceData));
  823. */
  824. int32 npcXpackFlag = atoul(row[75]);
  825. int32 npcHolidayFlag = atoul(row[76]);
  826. if (!CheckExpansionFlags(zone, npcXpackFlag) || !CheckHolidayFlags(zone, npcHolidayFlag))
  827. continue;
  828. id = atoul(row[0]);
  829. if(zone->GetNPC(id, true))
  830. continue;
  831. npc = new NPC();
  832. npc->SetDatabaseID(id);
  833. strcpy(npc->appearance.name, row[1]);
  834. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[9]));
  835. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[10]));
  836. if(primary_command_list){
  837. npc->SetPrimaryCommands(primary_command_list);
  838. npc->primary_command_list_id = atoul(row[9]);
  839. }
  840. if(secondary_command_list){
  841. npc->SetSecondaryCommands(secondary_command_list);
  842. npc->secondary_command_list_id = atoul(row[10]);
  843. }
  844. npc->appearance.min_level = atoi(row[2]);
  845. npc->appearance.max_level = atoi(row[3]);
  846. npc->appearance.level = atoi(row[2]);
  847. npc->appearance.encounter_level = atoi(row[4]);
  848. npc->appearance.race = atoi(row[5]);
  849. //npc->appearance.lua_race_id = atoi(row[75]);
  850. if (atoi(row[74]) > 0) {
  851. int16 xxx = atoi(row[75]);
  852. int8 yyy = 0;
  853. }
  854. npc->appearance.model_type = atoi(row[6]);
  855. npc->appearance.soga_model_type = atoi(row[62]);
  856. npc->appearance.adventure_class = atoi(row[7]);
  857. npc->appearance.gender = atoi(row[8]);
  858. npc->appearance.display_name = atoi(row[11]);
  859. npc->features.hair_type = atoi(row[14]);
  860. npc->features.hair_face_type = atoi(row[15]);
  861. npc->features.wing_type = atoi(row[16]);
  862. npc->features.chest_type = atoi(row[17]);
  863. npc->features.legs_type = atoi(row[18]);
  864. npc->features.soga_hair_type = atoi(row[19]);
  865. npc->features.soga_hair_face_type = atoi(row[20]);
  866. npc->appearance.attackable = atoi(row[21]);
  867. npc->appearance.show_level = atoi(row[22]);
  868. npc->appearance.targetable = atoi(row[23]);
  869. npc->appearance.show_command_icon = atoi(row[24]);
  870. npc->appearance.display_hand_icon = atoi(row[25]);
  871. npc->appearance.hide_hood = atoi(row[70]);
  872. npc->appearance.randomize = atoi(row[61]);
  873. npc->SetTotalHP(atoul(row[26]));
  874. npc->SetTotalPower(atoul(row[27]));
  875. npc->SetHP(npc->GetTotalHP());
  876. npc->SetPower(npc->GetTotalPower());
  877. if(npc->GetTotalHP() == 0){
  878. npc->SetTotalHP(15*npc->GetLevel() + 1);
  879. npc->SetHP(15*npc->GetLevel() + 1);
  880. }
  881. if(npc->GetTotalPower() == 0){
  882. npc->SetTotalPower(15*npc->GetLevel() + 1);
  883. npc->SetPower(15*npc->GetLevel() + 1);
  884. }
  885. npc->size = atoi(row[28]);
  886. npc->appearance.pos.collision_radius = atoi(row[29]);
  887. npc->appearance.action_state = atoi(row[30]);
  888. npc->appearance.visual_state = atoi(row[31]);
  889. npc->appearance.mood_state = atoi(row[32]);
  890. npc->appearance.emote_state = atoi(row[71]);
  891. npc->appearance.pos.state = atoi(row[33]);
  892. npc->appearance.activity_status = atoi(row[34]);
  893. npc->faction_id = atoul(row[35]);
  894. if(row[36]){
  895. if(strlen(row[36]) < sizeof(npc->appearance.sub_title))
  896. strcpy(npc->appearance.sub_title, row[36]);
  897. else
  898. strncpy(npc->appearance.sub_title, row[36], sizeof(npc->appearance.sub_title));
  899. }
  900. npc->SetMerchantID(atoul(row[37]));
  901. npc->SetMerchantType(atoi(row[38]));
  902. npc->SetSizeOffset(atoi(row[39]));
  903. npc->SetAttackType(atoi(row[40]));
  904. npc->SetAIStrategy(atoi(row[41]));
  905. npc->SetPrimarySpellList(atoul(row[42]));
  906. npc->SetSecondarySpellList(atoul(row[43]));
  907. npc->SetPrimarySkillList(atoul(row[44]));
  908. npc->SetSecondarySkillList(atoul(row[45]));
  909. npc->SetEquipmentListID(atoul(row[46]));
  910. InfoStruct* info = npc->GetInfoStruct();
  911. info->str_base = atoi(row[47]);
  912. info->sta_base = atoi(row[48]);
  913. info->wis_base = atoi(row[49]);
  914. info->intel_base = atoi(row[50]);
  915. info->agi_base = atoi(row[51]);
  916. info->heat_base = atoi(row[52]);
  917. info->cold_base = atoi(row[53]);
  918. info->magic_base = atoi(row[54]);
  919. info->mental_base = atoi(row[55]);
  920. info->divine_base = atoi(row[56]);
  921. info->disease_base = atoi(row[57]);
  922. info->poison_base = atoi(row[58]);
  923. info->alignment = atoi(row[64]);
  924. npc->SetAggroRadius(atof(row[59]));
  925. npc->SetCastPercentage(atoi(row[60]));
  926. npc->appearance.heroic_flag = atoi(row[63]);
  927. info->elemental_base = atoi(row[65]);
  928. info->arcane_base = atoi(row[66]);
  929. info->noxious_base = atoi(row[67]);
  930. npc->SetTotalSavagery(atoul(row[68]));
  931. npc->SetTotalDissonance(atoul(row[69]));
  932. npc->SetSavagery(npc->GetTotalSavagery());
  933. npc->SetDissonance(npc->GetTotalDissonance());
  934. if(npc->GetTotalSavagery() == 0){
  935. npc->SetTotalSavagery(15*npc->GetLevel() + 1);
  936. npc->SetSavagery(15*npc->GetLevel() + 1);
  937. }
  938. if(npc->GetTotalDissonance() == 0){
  939. npc->SetTotalDissonance(15*npc->GetLevel() + 1);
  940. npc->SetDissonance(15*npc->GetLevel() + 1);
  941. }
  942. npc->SetPrefixTitle(row[72]);
  943. npc->SetSuffixTitle(row[73]);
  944. npc->SetLastName(row[74]);
  945. // xpack+holiday value handled at top at position 75+76
  946. int8 disableSounds = atoul(row[77]);
  947. npc->SetSoundsDisabled(disableSounds);
  948. npc->SetMerchantLevelRange(atoul(row[78]), atoul(row[79]));
  949. zone->AddNPC(id, npc);
  950. total++;
  951. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC: '%s' (%u)", npc->appearance.name, id);
  952. }
  953. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC(s).", total);
  954. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Spell(s).", LoadNPCSpells(zone));
  955. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Skill(s).", LoadNPCSkills(zone));
  956. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Equipment Piece(s).", LoadNPCEquipment(zone));
  957. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Appearance(s).", LoadAppearances(zone));
  958. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Equipment Appearance(s).", LoadNPCAppearanceEquipmentData(zone));
  959. }
  960. void WorldDatabase::LoadSigns(ZoneServer* zone){
  961. Query query;
  962. MYSQL_ROW row;
  963. Sign* sign = 0;
  964. int32 id = 0;
  965. int32 total = 0;
  966. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT ss.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, ss.widget_id, ss.widget_x, ss.widget_y, ss.widget_z, s.command_primary, s.command_secondary, s.collision_radius, ss.icon, ss.type, ss.title, ss.description, ss.sign_distance, ss.zone_id, ss.zone_x, ss.zone_y, ss.zone_z, ss.zone_heading, ss.include_heading, ss.include_location, s.transport_id, s.size_offset, s.display_hand_icon, s.visual_state, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  967. "FROM spawn s\n"
  968. "INNER JOIN spawn_signs ss\n"
  969. "ON s.id = ss.spawn_id\n"
  970. "INNER JOIN spawn_location_entry le\n"
  971. "ON ss.spawn_id = le.spawn_id\n"
  972. "INNER JOIN spawn_location_placement lp\n"
  973. "ON le.spawn_location_id = lp.spawn_location_id\n"
  974. "WHERE lp.zone_id = %u\n"
  975. "GROUP BY s.id",
  976. zone->GetZoneID());
  977. while(result && (row = mysql_fetch_row(result))){
  978. int32 signXpackFlag = atoul(row[28]);
  979. int32 signHolidayFlag = atoul(row[29]);
  980. if (!CheckExpansionFlags(zone, signXpackFlag) || !CheckHolidayFlags(zone, signHolidayFlag))
  981. continue;
  982. id = atoul(row[0]);
  983. if(zone->GetSign(id, true))
  984. continue;
  985. sign = new Sign();
  986. sign->SetDatabaseID(id);
  987. strcpy(sign->appearance.name, row[1]);
  988. sign->appearance.model_type = atoi(row[2]);
  989. sign->SetSize(atoi(row[3]));
  990. sign->appearance.show_command_icon = atoi(row[4]);
  991. sign->SetWidgetID(atoul(row[5]));
  992. sign->SetWidgetX(atof(row[6]));
  993. sign->SetWidgetY(atof(row[7]));
  994. sign->SetWidgetZ(atof(row[8]));
  995. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[9]));
  996. if(primary_command_list){
  997. sign->SetPrimaryCommands(primary_command_list);
  998. sign->primary_command_list_id = atoul(row[9]);
  999. }
  1000. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[10]));
  1001. if (secondary_command_list){
  1002. sign->SetSecondaryCommands(secondary_command_list);
  1003. sign->secondary_command_list_id = atoul(row[10]);
  1004. }
  1005. sign->appearance.pos.collision_radius = atoi(row[11]);
  1006. sign->SetSignIcon(atoi(row[12]));
  1007. if(strncasecmp(row[13],"Generic", 7) == 0)
  1008. sign->SetSignType(SIGN_TYPE_GENERIC);
  1009. else if(strncasecmp(row[13],"Zone", 4) == 0)
  1010. sign->SetSignType(SIGN_TYPE_ZONE);
  1011. sign->SetSignTitle(row[14]);
  1012. sign->SetSignDescription(row[15]);
  1013. sign->SetSignDistance(atof(row[16]));
  1014. sign->SetSignZoneID(atoul(row[17]));
  1015. sign->SetSignZoneX(atof(row[18]));
  1016. sign->SetSignZoneY(atof(row[19]));
  1017. sign->SetSignZoneZ(atof(row[20]));
  1018. sign->SetSignZoneHeading(atof(row[21]));
  1019. sign->SetIncludeHeading(atoi(row[22]) == 1);
  1020. sign->SetIncludeLocation(atoi(row[23]) == 1);
  1021. sign->SetTransporterID(atoul(row[24]));
  1022. sign->SetSizeOffset(atoi(row[25]));
  1023. sign->appearance.display_hand_icon = atoi(row[26]);
  1024. sign->SetVisualState(atoi(row[27]));
  1025. // xpack+holiday value handled at top at position 28+29
  1026. int8 disableSounds = atoul(row[30]);
  1027. sign->SetSoundsDisabled(disableSounds);
  1028. sign->SetMerchantLevelRange(atoul(row[31]), atoul(row[32]));
  1029. zone->AddSign(id, sign);
  1030. total++;
  1031. LogWrite(SIGN__DEBUG, 5, "Sign", "---Loading Sign: '%s' (%u).", sign->appearance.name, id);
  1032. }
  1033. LogWrite(SIGN__DEBUG, 0, "Sign", "--Loaded %i Sign(s)", total);
  1034. }
  1035. void WorldDatabase::LoadWidgets(ZoneServer* zone){
  1036. Query query;
  1037. MYSQL_ROW row;
  1038. Widget* widget = 0;
  1039. int32 id = 0;
  1040. int32 total = 0;
  1041. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT sw.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, sw.widget_id, sw.widget_x, sw.widget_y, sw.widget_z, s.command_primary, s.command_secondary, s.collision_radius, sw.include_heading, sw.include_location, sw.icon, sw.type, sw.open_heading, sw.open_y, sw.action_spawn_id, sw.open_sound_file, sw.close_sound_file, sw.open_duration, sw.closed_heading, sw.linked_spawn_id, sw.close_y, s.transport_id, s.size_offset, sw.house_id, sw.open_x, sw.open_z, sw.close_x, sw.close_z, s.display_hand_icon, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  1042. "FROM spawn s\n"
  1043. "INNER JOIN spawn_widgets sw\n"
  1044. "ON s.id = sw.spawn_id\n"
  1045. "INNER JOIN spawn_location_entry le\n"
  1046. "ON sw.spawn_id = le.spawn_id\n"
  1047. "INNER JOIN spawn_location_placement lp\n"
  1048. "ON le.spawn_location_id = lp.spawn_location_id\n"
  1049. "WHERE lp.zone_id = %u\n"
  1050. "GROUP BY s.id",
  1051. zone->GetZoneID());
  1052. while(result && (row = mysql_fetch_row(result))){
  1053. int32 widgetXpackFlag = atoul(row[33]);
  1054. int32 widgetHolidayFlag = atoul(row[34]);
  1055. if (!CheckExpansionFlags(zone, widgetXpackFlag) || !CheckHolidayFlags(zone, widgetHolidayFlag))
  1056. continue;
  1057. id = atoul(row[0]);
  1058. if(zone->GetWidget(id, true))
  1059. continue;
  1060. widget = new Widget();
  1061. widget->SetDatabaseID(id);
  1062. strcpy(widget->appearance.name, row[1]);
  1063. widget->appearance.model_type = atoi(row[2]);
  1064. widget->SetSize(atoi(row[3]));
  1065. widget->appearance.show_command_icon = atoi(row[4]);
  1066. if (row[5] == NULL)
  1067. widget->SetWidgetID(0xFFFFFFFF);
  1068. else
  1069. widget->SetWidgetID(atoul(row[5]));
  1070. widget->SetWidgetX(atof(row[6]));
  1071. widget->SetWidgetY(atof(row[7]));
  1072. widget->SetWidgetZ(atof(row[8]));
  1073. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[9]));
  1074. if(primary_command_list){
  1075. widget->SetPrimaryCommands(primary_command_list);
  1076. widget->primary_command_list_id = atoul(row[9]);
  1077. }
  1078. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[10]));
  1079. if (secondary_command_list) {
  1080. widget->SetSecondaryCommands(secondary_command_list);
  1081. widget->secondary_command_list_id = atoul(row[10]);
  1082. }
  1083. widget->appearance.pos.collision_radius = atoi(row[11]);
  1084. widget->SetIncludeHeading(atoi(row[12]) == 1);
  1085. widget->SetIncludeLocation(atoi(row[13]) == 1);
  1086. widget->SetWidgetIcon(atoi(row[14]));
  1087. if (strncasecmp(row[15], "Generic", 7) == 0)
  1088. widget->SetWidgetType(WIDGET_TYPE_GENERIC);
  1089. else if (strncasecmp(row[15], "Door", 4) == 0)
  1090. widget->SetWidgetType(WIDGET_TYPE_DOOR);
  1091. else if (strncasecmp(row[15], "Lift", 4) == 0)
  1092. widget->SetWidgetType(WIDGET_TYPE_LIFT);
  1093. widget->SetOpenHeading(atof(row[16]));
  1094. widget->SetOpenY(atof(row[17]));
  1095. widget->SetActionSpawnID(atoul(row[18]));
  1096. if(row[19] && strlen(row[19]) > 5)
  1097. widget->SetOpenSound(row[19]);
  1098. if(row[20] && strlen(row[20]) > 5)
  1099. widget->SetCloseSound(row[20]);
  1100. widget->SetOpenDuration(atoi(row[21]));
  1101. widget->SetClosedHeading(atof(row[22]));
  1102. widget->SetLinkedSpawnID(atoul(row[23]));
  1103. widget->SetCloseY(atof(row[24]));
  1104. widget->SetTransporterID(atoul(row[25]));
  1105. widget->SetSizeOffset(atoi(row[26]));
  1106. widget->SetHouseID(atoul(row[27]));
  1107. widget->SetOpenX(atof(row[28]));
  1108. widget->SetOpenZ(atof(row[29]));
  1109. widget->SetCloseX(atof(row[30]));
  1110. widget->SetCloseZ(atof(row[31]));
  1111. widget->appearance.display_hand_icon = atoi(row[32]);
  1112. // xpack+holiday value handled at top at position 33+34
  1113. int8 disableSounds = atoul(row[35]);
  1114. widget->SetSoundsDisabled(disableSounds);
  1115. widget->SetMerchantLevelRange(atoul(row[36]), atoul(row[37]));
  1116. zone->AddWidget(id, widget);
  1117. total++;
  1118. LogWrite(WIDGET__DEBUG, 5, "Widget", "---Loading Widget: '%s' (%u).", widget->appearance.name, id);
  1119. }
  1120. LogWrite(WIDGET__DEBUG, 0, "Widget", "--Loaded %i Widget(s)", total);
  1121. }
  1122. void WorldDatabase::LoadObjects(ZoneServer* zone){
  1123. Query query;
  1124. MYSQL_ROW row;
  1125. Object* object = 0;
  1126. int32 id = 0;
  1127. int32 total = 0;
  1128. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT so.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, s.transport_id, s.size_offset, so.device_id, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  1129. "FROM spawn s\n"
  1130. "INNER JOIN spawn_objects so\n"
  1131. "ON s.id = so.spawn_id\n"
  1132. "INNER JOIN spawn_location_entry le\n"
  1133. "ON so.spawn_id = le.spawn_id\n"
  1134. "INNER JOIN spawn_location_placement lp\n"
  1135. "ON le.spawn_location_id = lp.spawn_location_id\n"
  1136. "WHERE lp.zone_id = %u and (lp.instance_id = 0 or lp.instance_id = %u)\n"
  1137. "GROUP BY s.id",
  1138. zone->GetZoneID(), zone->GetInstanceID());
  1139. while(result && (row = mysql_fetch_row(result))){
  1140. int32 objXpackFlag = atoul(row[19]);
  1141. int32 objHolidayFlag = atoul(row[20]);
  1142. if (!CheckExpansionFlags(zone, objXpackFlag) || !CheckHolidayFlags(zone, objHolidayFlag))
  1143. continue;
  1144. id = atoul(row[0]);
  1145. if(zone->GetObject(id, true))
  1146. continue;
  1147. object = new Object();
  1148. object->SetDatabaseID(id);
  1149. strcpy(object->appearance.name, row[1]);
  1150. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[4]));
  1151. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[5]));
  1152. if(primary_command_list){
  1153. object->SetPrimaryCommands(primary_command_list);
  1154. object->primary_command_list_id = atoul(row[4]);
  1155. }
  1156. if(secondary_command_list){
  1157. object->SetSecondaryCommands(secondary_command_list);
  1158. object->secondary_command_list_id = atoul(row[5]);
  1159. }
  1160. object->appearance.race = atoi(row[2]);
  1161. object->appearance.model_type = atoi(row[3]);
  1162. object->appearance.targetable = atoi(row[6]);
  1163. object->size = atoi(row[7]);
  1164. object->appearance.display_name = atoi(row[8]);
  1165. object->appearance.visual_state = atoi(row[9]);
  1166. object->appearance.attackable = atoi(row[10]);
  1167. object->appearance.show_level = atoi(row[11]);
  1168. object->appearance.show_command_icon = atoi(row[12]);
  1169. object->appearance.display_hand_icon = atoi(row[13]);
  1170. object->faction_id = atoul(row[14]);
  1171. object->appearance.pos.collision_radius = atoi(row[15]);
  1172. object->SetTransporterID(atoul(row[16]));
  1173. object->SetSizeOffset(atoi(row[17]));
  1174. object->SetDeviceID(atoi(row[18]));
  1175. // xpack value handled at top at position 19
  1176. int8 disableSounds = atoul(row[21]);
  1177. object->SetSoundsDisabled(disableSounds);
  1178. object->SetMerchantLevelRange(atoul(row[22]), atoul(row[23]));
  1179. zone->AddObject(id, object);
  1180. total++;
  1181. LogWrite(OBJECT__DEBUG, 5, "Object", "---Loading Object: '%s' (%u)", object->appearance.name, id);
  1182. }
  1183. LogWrite(OBJECT__DEBUG, 0, "Object", "--Loaded %i Object(s)", total);
  1184. }
  1185. void WorldDatabase::LoadGroundSpawns(ZoneServer* zone){
  1186. Query query;
  1187. MYSQL_ROW row;
  1188. GroundSpawn* spawn = 0;
  1189. int32 id = 0;
  1190. int32 total = 0;
  1191. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT sg.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, sg.number_harvests, sg.num_attempts_per_harvest, sg.groundspawn_id, sg.collection_skill, s.size_offset, s.expansion_flag, s.holiday_flag, s.disable_sounds\n"
  1192. "FROM spawn s\n"
  1193. "INNER JOIN spawn_ground sg\n"
  1194. "ON s.id = sg.spawn_id\n"
  1195. "INNER JOIN spawn_location_entry le\n"
  1196. "ON sg.spawn_id = le.spawn_id\n"
  1197. "INNER JOIN spawn_location_placement lp\n"
  1198. "ON le.spawn_location_id = lp.spawn_location_id\n"
  1199. "WHERE lp.zone_id = %u\n"
  1200. "GROUP BY s.id",
  1201. zone->GetZoneID());
  1202. while(result && (row = mysql_fetch_row(result))){
  1203. int32 gsXpackFlag = atoul(row[21]);
  1204. int32 gsHolidayFlag = atoul(row[22]);
  1205. if (!CheckExpansionFlags(zone, gsXpackFlag) || !CheckHolidayFlags(zone, gsHolidayFlag))
  1206. continue;
  1207. id = atoul(row[0]);
  1208. if(zone->GetGroundSpawn(id, true))
  1209. continue;
  1210. spawn = new GroundSpawn();
  1211. spawn->SetDatabaseID(id);
  1212. spawn->forceMapCheck = true;
  1213. strcpy(spawn->appearance.name, row[1]);
  1214. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[4]));
  1215. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[5]));
  1216. if(primary_command_list){
  1217. spawn->SetPrimaryCommands(primary_command_list);
  1218. spawn->primary_command_list_id = atoul(row[4]);
  1219. }
  1220. if(secondary_command_list){
  1221. spawn->SetSecondaryCommands(secondary_command_list);
  1222. spawn->secondary_command_list_id = atoul(row[5]);
  1223. }
  1224. spawn->appearance.race = atoi(row[2]);
  1225. spawn->appearance.model_type = atoi(row[3]);
  1226. spawn->appearance.targetable = atoi(row[6]);
  1227. spawn->size = atoi(row[7]);
  1228. spawn->appearance.display_name = atoi(row[8]);
  1229. spawn->appearance.visual_state = atoi(row[9]);
  1230. spawn->appearance.attackable = atoi(row[10]);
  1231. spawn->appearance.show_level = atoi(row[11]);
  1232. spawn->appearance.show_command_icon = atoi(row[12]);
  1233. spawn->appearance.display_hand_icon = atoi(row[13]);
  1234. spawn->faction_id = atoul(row[14]);
  1235. spawn->appearance.pos.collision_radius = atoi(row[15]);
  1236. spawn->SetNumberHarvests(atoi(row[16]));
  1237. spawn->SetAttemptsPerHarvest(atoi(row[17]));
  1238. spawn->SetGroundSpawnEntryID(atoul(row[18]));
  1239. spawn->SetCollectionSkill(row[19]);
  1240. spawn->SetSizeOffset(atoi(row[20]));
  1241. // xpack+holiday value handled at top at position 21+22
  1242. int8 disableSounds = atoul(row[23]);
  1243. spawn->SetSoundsDisabled(disableSounds);
  1244. zone->AddGroundSpawn(id, spawn);
  1245. total++;
  1246. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn: '%s' (%u)", spawn->appearance.name, id);
  1247. }
  1248. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "--Loaded %i GroundSpawn(s)", total);
  1249. }
  1250. void WorldDatabase::LoadGroundSpawnItems(ZoneServer* zone) {
  1251. Query query;
  1252. MYSQL_ROW row;
  1253. int32 total = 0;
  1254. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT groundspawn_id, item_id, is_rare, grid_id FROM groundspawn_items;");
  1255. while(result && (row = mysql_fetch_row(result)))
  1256. {
  1257. zone->AddGroundSpawnItem(atoul(row[0]), atoul(row[1]), atoi(row[2]), atoul(row[3]));
  1258. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn Items: ID: %u\n", atoul(row[0]));
  1259. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---item: %ul, rare: %i, grid: %ul", atoul(row[1]), atoi(row[2]), atoul(row[3]));
  1260. total++;
  1261. }
  1262. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "--Loaded %i GroundSpawn Item%s.", total, total == 1 ? "" : "s");
  1263. }
  1264. void WorldDatabase::LoadGroundSpawnEntries(ZoneServer* zone) {
  1265. Query query;
  1266. MYSQL_ROW row;
  1267. int32 total = 0;
  1268. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT groundspawn_id, min_skill_level, min_adventure_level, bonus_table, harvest1, harvest3, harvest5, harvest_imbue, harvest_rare, harvest10, harvest_coin FROM groundspawns WHERE enabled = 1;");
  1269. while(result && (row = mysql_fetch_row(result)))
  1270. {
  1271. // this is getting ridonkulous...
  1272. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn ID: %u\n" \
  1273. "---min_skill_level: %i, min_adventure_level: %i, bonus_table: %i\n" \
  1274. "---harvest1: %.2f, harvest3: %.2f, harvest5: %.2f\n" \
  1275. "---harvest_imbue: %.2f, harvest_rare: %.2f, harvest10: %.2f\n" \
  1276. "---harvest_coin: %u", atoul(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), atof(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atof(row[9]), atoul(row[10]));
  1277. zone->AddGroundSpawnEntry(atoul(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), atof(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atof(row[9]), atoul(row[10]));
  1278. total++;
  1279. }
  1280. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "--Loaded %i GroundSpawn Entr%s.", total, total == 1 ? "y" : "ies");
  1281. LoadGroundSpawnItems(zone);
  1282. }
  1283. bool WorldDatabase::LoadCharacterStats(int32 id, int32 account_id, Client* client)
  1284. {
  1285. DatabaseResult result;
  1286. if( database_new.Select(&result, "SELECT * FROM character_details WHERE char_id = %i LIMIT 0, 1", id) )
  1287. {
  1288. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading character_details for '%s' (char_id: %u)", client->GetPlayer()->GetName(), id);
  1289. while( result.Next() )
  1290. {
  1291. InfoStruct* info = client->GetPlayer()->GetInfoStruct();
  1292. client->GetPlayer()->SetHP(result.GetSInt32Str("hp"));
  1293. client->GetPlayer()->SetPower(result.GetSInt32Str("power"));
  1294. info->max_concentration = result.GetInt8Str("max_concentration");
  1295. info->attack_base = result.GetInt16Str("attack");
  1296. info->mitigation_base = result.GetInt16Str("mitigation");
  1297. info->avoidance_base = result.GetInt16Str("avoidance");
  1298. info->parry_base = result.GetInt16Str("parry");
  1299. info->deflection_base = result.GetInt16Str("deflection");
  1300. info->block_base = result.GetInt16Str("block");
  1301. info->str_base = result.GetInt16Str("str");
  1302. info->sta_base = result.GetInt16Str("sta");
  1303. info->agi_base = result.GetInt16Str("agi");
  1304. info->wis_base = result.GetInt16Str("wis");
  1305. info->intel_base = result.GetInt16Str("intel");
  1306. // old resist types
  1307. info->heat_base = result.GetInt16Str("heat");
  1308. info->cold_base = result.GetInt16Str("cold");
  1309. info->magic_base = result.GetInt16Str("magic");
  1310. info->mental_base = result.GetInt16Str("mental");
  1311. info->divine_base = result.GetInt16Str("divine");
  1312. info->disease_base = result.GetInt16Str("disease");
  1313. info->poison_base = result.GetInt16Str("poison");
  1314. //
  1315. info->coin_copper = result.GetInt32Str("coin_copper");
  1316. info->coin_silver = result.GetInt32Str("coin_silver");
  1317. info->coin_gold = result.GetInt32Str("coin_gold");
  1318. info->coin_plat = result.GetInt32Str("coin_plat");
  1319. strcpy(info->pet_name, result.GetStringStr("pet_name"));
  1320. strcpy(info->biography, result.GetStringStr("biography"));
  1321. info->status_points = result.GetInt32Str("status_points");
  1322. client->GetPlayer()->GetPlayerInfo()->SetBindZone(result.GetInt32Str("bind_zone_id"));
  1323. client->GetPlayer()->GetPlayerInfo()->SetBindX(result.GetFloatStr("bind_x"));
  1324. client->GetPlayer()->GetPlayerInfo()->SetBindY(result.GetFloatStr("bind_y"));
  1325. client->GetPlayer()->GetPlayerInfo()->SetBindZ(result.GetFloatStr("bind_z"));
  1326. client->GetPlayer()->GetPlayerInfo()->SetBindHeading(result.GetFloatStr("bind_heading"));
  1327. client->GetPlayer()->GetPlayerInfo()->SetHouseZone(result.GetInt32Str("house_zone_id"));
  1328. client->GetPlayer()->SetTotalHP(result.GetSInt32Str("max_hp"));
  1329. client->GetPlayer()->SetTotalPower(result.GetSInt32Str("max_power"));
  1330. client->GetPlayer()->SetAssignedAA(result.GetInt16Str("assigned_aa"));
  1331. client->GetPlayer()->SetUnassignedAA(result.GetInt16Str("unassigned_aa"));
  1332. client->GetPlayer()->SetTradeskillAA(result.GetInt16Str("tradeskill_aa"));
  1333. client->GetPlayer()->SetUnassignedTradeskillAA(result.GetInt16Str("unassigned_tradeskill_aa"));
  1334. client->GetPlayer()->SetPrestigeAA(result.GetInt16Str("prestige_aa"));
  1335. client->GetPlayer()->SetUnassignedPrestigeAA(result.GetInt16Str("unassigned_prestige_aa"));
  1336. client->GetPlayer()->SetTradeskillPrestigeAA(result.GetInt16Str("tradeskill_prestige_aa"));
  1337. client->GetPlayer()->SetUnassignedTradeskillPrestigeAA(result.GetInt16Str("unassigned_tradeskill_prestige_aa"));
  1338. info->xp = result.GetInt32Str("xp");
  1339. info->xp_needed = result.GetInt32Str("xp_needed");
  1340. if(info->xp_needed == 0)
  1341. client->GetPlayer()->SetNeededXP();
  1342. info->xp_debt = result.GetInt32Str("xp_debt");
  1343. info->xp_vitality = result.GetFloatStr("xp_vitality");
  1344. info->ts_xp = result.GetInt32Str("tradeskill_xp");
  1345. info->ts_xp_needed = result.GetInt32Str("tradeskill_xp_needed");
  1346. if (info->ts_xp_needed == 0)
  1347. client->GetPlayer()->SetNeededTSXP();
  1348. info->tradeskill_xp_vitality = result.GetFloatStr("tradeskill_xp_vitality");
  1349. client->GetPlayer()->SetTotalHPBase(client->GetPlayer()->GetTotalHP());
  1350. client->GetPlayer()->SetTotalPowerBase(client->GetPlayer()->GetTotalPower());
  1351. info->bank_coin_copper = result.GetInt32Str("bank_copper");
  1352. info->bank_coin_silver = result.GetInt32Str("bank_silver");
  1353. info->bank_coin_gold = result.GetInt32Str("bank_gold");
  1354. info->bank_coin_plat = result.GetInt32Str("bank_plat");
  1355. client->GetPlayer()->SetCombatVoice(result.GetInt16Str("combat_voice"));
  1356. client->GetPlayer()->SetEmoteVoice(result.GetInt16Str("emote_voice"));
  1357. client->GetPlayer()->SetBiography(result.GetStringStr("biography"));
  1358. client->GetPlayer()->GetInfoStruct()->flags = result.GetInt32Str("flags");
  1359. client->GetPlayer()->GetInfoStruct()->flags2 = result.GetInt32Str("flags2");
  1360. client->GetPlayer()->SetLastName(result.GetStringStr("last_name"));
  1361. // new resist types
  1362. info->elemental_base = result.GetInt16Str("elemental");
  1363. info->arcane_base = result.GetInt16Str("arcane");
  1364. info->noxious_base = result.GetInt16Str("noxious");
  1365. // new savagery and dissonance
  1366. client->GetPlayer()->SetSavagery(result.GetSInt16Str("savagery"));
  1367. client->GetPlayer()->SetDissonance(result.GetSInt16Str("dissonance"));
  1368. client->GetPlayer()->SetTotalSavageryBase(client->GetPlayer()->GetTotalSavagery());
  1369. client->GetPlayer()->SetTotalDissonanceBase(client->GetPlayer()->GetTotalDissonance());
  1370. }
  1371. return true;
  1372. }
  1373. else
  1374. {
  1375. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character_details for '%s' (char_id: %u)", client->GetPlayer()->GetName(), id);
  1376. return false;
  1377. }
  1378. }
  1379. bool WorldDatabase::loadCharacter(const char* ch_name, int32 account_id, Client* client){
  1380. Query query, query4;
  1381. MYSQL_ROW row, row4;
  1382. int32 id = 0;
  1383. query.escaped_name = getEscapeString(ch_name);
  1384. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, current_zone_id, x, y, z, heading, admin_status, race, model_type, class, deity, level, gender, tradeskill_class, tradeskill_level, wing_type, hair_type, chest_type, legs_type, soga_wing_type, soga_hair_type, soga_chest_type, soga_legs_type, 0xFFFFFFFF - crc32(name), facial_hair_type, soga_facial_hair_type,instance_id,last_saved, DATEDIFF(curdate(), created_date) as accage FROM characters where name='%s' and account_id=%i AND deleted = 0", query.escaped_name, account_id);
  1385. // no character found
  1386. if ( result == NULL ) {
  1387. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character for '%s'", ch_name);
  1388. return false;
  1389. }
  1390. if (mysql_num_rows(result) == 1){
  1391. row = mysql_fetch_row(result);
  1392. id = strtoul(row[0], NULL, 0);
  1393. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading character for '%s' (char_id: %u)", ch_name, id);
  1394. client->SetCharacterID(id);
  1395. client->GetPlayer()->SetCharacterID(id);
  1396. client->SetAccountID(account_id);
  1397. client->GetPlayer()->SetName(ch_name);
  1398. client->GetPlayer()->SetX(atof(row[2]));
  1399. client->GetPlayer()->SetY(atof(row[3]));
  1400. client->GetPlayer()->SetZ(atof(row[4]));
  1401. client->GetPlayer()->SetHeading(atof(row[5]));
  1402. client->SetAdminStatus(atoi(row[6]));
  1403. client->GetPlayer()->SetRace(atoi(row[7]));
  1404. client->GetPlayer()->SetModelType(atoi(row[8]));
  1405. client->GetPlayer()->SetAdventureClass(atoi(row[9]));
  1406. client->GetPlayer()->SetDeity(atoi(row[10]));
  1407. client->GetPlayer()->SetLevel(atoi(row[11]));
  1408. client->GetPlayer()->SetGender(atoi(row[12]));
  1409. client->GetPlayer()->SetTradeskillClass(atoi(row[13]));
  1410. client->GetPlayer()->SetTSLevel(atoi(row[14]));
  1411. LogWrite(MISC__TODO, 1, "TODO", "Fix client->GetPlayer()->SetArtLevel(atoi(row[14]));\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  1412. client->GetPlayer()->features.wing_type = atoi(row[15]);
  1413. client->GetPlayer()->features.hair_type = atoi(row[16]);
  1414. client->GetPlayer()->features.chest_type = atoi(row[17]);
  1415. client->GetPlayer()->features.legs_type = atoi(row[18]);
  1416. LogWrite(MISC__TODO, 1, "TODO", "Fix SOGA appearances here\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  1417. client->GetPlayer()->features.wing_type = atoi(row[19]);
  1418. client->GetPlayer()->features.soga_hair_type = atoi(row[20]);
  1419. client->GetPlayer()->features.soga_chest_type = atoi(row[21]);
  1420. client->GetPlayer()->features.soga_legs_type = atoi(row[22]);
  1421. client->SetNameCRC(atoul(row[23]));
  1422. client->GetPlayer()->features.hair_face_type = atoi(row[24]);
  1423. client->GetPlayer()->features.soga_hair_face_type = atoi(row[25]);
  1424. int32 instanceid = atoi(row[26]);
  1425. int32 zoneid = atoul(row[1]);
  1426. /*
  1427. JA Notes on SOGA: I think there are many more settings to add than were commented out here,
  1428. because I can load a SOGA model player, but some features are missing (Barbarian WOAD, Skin tons, etc)
  1429. SOGA chars looked ok in LoginServer screen tho... odd.
  1430. */
  1431. // load character instances here
  1432. if ( LoadCharacterInstances(client) )
  1433. client->UpdateCharacterInstances();
  1434. if ( instanceid > 0 )
  1435. client->SetCurrentZoneByInstanceID(instanceid, zoneid);
  1436. else
  1437. client->SetCurrentZone(zoneid);
  1438. int32 lastsavedtime = atoi(row[27]);
  1439. client->SetLastSavedTimeStamp(lastsavedtime);
  1440. if (row[28])
  1441. client->GetPlayer()->GetPlayerInfo()->SetAccountAge(atoi(row[28]));
  1442. LoadCharacterFriendsIgnoreList(client->GetPlayer());
  1443. MYSQL_RES* result4 = query4.RunQuery2(Q_SELECT, "SELECT `guild_id` FROM `guild_members` WHERE `char_id`=%u", id);
  1444. if (result4 && (row4 = mysql_fetch_row(result4))) {
  1445. Guild* guild = guild_list.GetGuild(atoul(row4[0]));
  1446. if (guild) {
  1447. client->GetPlayer()->SetGuild(guild);
  1448. string subtitle;
  1449. subtitle.append("<").append(guild->GetName()).append(">");
  1450. client->GetPlayer()->SetSubTitle(subtitle.c_str());
  1451. }
  1452. }
  1453. LoadCharacterHistory(id, client->GetPlayer());
  1454. LoadCharacterLUAHistory(id, client->GetPlayer());
  1455. LoadPlayerStatistics(client->GetPlayer(), id);
  1456. LoadPlayerCollections(client->GetPlayer());
  1457. LoadPlayerRecipes(client->GetPlayer());
  1458. //LoadPlayerAchievements(client->GetPlayer());
  1459. LoadPlayerAchievementsUpdates(client->GetPlayer());
  1460. LoadAppearances(client->GetCurrentZone(), client);
  1461. return LoadCharacterStats(id, account_id, client);
  1462. }
  1463. // should not be here...
  1464. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character for '%s'", ch_name);
  1465. return false;
  1466. }
  1467. bool WorldDatabase::InsertCharacterStats(int32 character_id, int8 class_id, int8 race_id){
  1468. Query query1;
  1469. Query query2;
  1470. Query query3;
  1471. Query query4;
  1472. Query query5;
  1473. /* Blank record */
  1474. query1.RunQuery2(Q_INSERT, "INSERT INTO `character_details` (`char_id`) VALUES (%u)", character_id);
  1475. /* Using the class id and race id */
  1476. query2.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = %d AND class_id = %d AND char_id = %u", race_id, class_id, character_id);
  1477. if (query2.GetAffectedRows() > 0)
  1478. return true;
  1479. /* Using the class id and race id = 255 */
  1480. query3.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = 255 AND class_id = %d AND char_id = %u", class_id, character_id);
  1481. if (query3.GetAffectedRows() > 0)
  1482. return true;
  1483. /* Using class id = 255 and the race id */
  1484. query4.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = %d AND class_id = 255 AND char_id = %u", race_id, character_id);
  1485. if (query4.GetAffectedRows() > 0)
  1486. return true;
  1487. /* Using class id = 255 and race id = 255 */
  1488. query5.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = 255 AND class_id = 255 AND char_id = %u", character_id);
  1489. if (query5.GetAffectedRows() > 0)
  1490. return true;
  1491. return false;
  1492. }
  1493. int32 WorldDatabase::GetCharacterTimeStamp(int32 character_id, int32 account_id,bool* char_exists){
  1494. Query query;
  1495. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT unix_timestamp FROM characters where id=%i and account_id=%i",character_id,account_id);
  1496. if(result && mysql_num_rows(result) > 0) {
  1497. MYSQL_ROW row;
  1498. row = mysql_fetch_row(result);
  1499. *char_exists = true;
  1500. return atoi(row[0]); // Return timestamp
  1501. }
  1502. else
  1503. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterTimeStamp query '%s': %s", query.GetQuery(), query.GetError());
  1504. *char_exists = false;
  1505. return 0;
  1506. }
  1507. int32 WorldDatabase::GetCharacterTimeStamp(int32 character_id) {
  1508. Query query;
  1509. MYSQL_ROW row;
  1510. MYSQL_RES *result = query.RunQuery2(Q_SELECT, "SELECT unix_timestamp FROM characters WHERE id=%u", character_id);
  1511. int32 ret = 0;
  1512. if (result && (row = mysql_fetch_row(result)))
  1513. ret = atoul(row[0]);
  1514. return ret;
  1515. }
  1516. bool WorldDatabase::UpdateCharacterTimeStamp(int32 account_id, int32 character_id, int32 timestamp_update){
  1517. Query query;
  1518. string update_charts = string("update characters set unix_timestamp=%i where id=%i and account_id=%i");
  1519. query.RunQuery2(Q_UPDATE, update_charts.c_str(),timestamp_update,character_id,account_id);
  1520. if(!query.GetAffectedRows())
  1521. {
  1522. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateCharacterTimeStamp query '%s': %s", query.GetQuery(), query.GetError());
  1523. return false;
  1524. }
  1525. return true;
  1526. }
  1527. bool WorldDatabase::insertCharacterProperty(Client* client, char* propName, char* propValue) {
  1528. Query query;
  1529. string update_status = string("update charactersProperties set propvalue='%s' where charid=%i and propname='%s'");
  1530. query.RunQuery2(Q_UPDATE, update_status.c_str(), propValue, client->GetCharacterID(), propName);
  1531. if (!query.GetAffectedRows())
  1532. {
  1533. query.RunQuery2(Q_UPDATE, "insert into charactersProperties (charid, propname, propvalue) values(%i, '%s', '%s')", client->GetCharacterID(), propName, propValue);
  1534. if (query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF) {
  1535. LogWrite(WORLD__ERROR, 0, "World", "Error in insertCharacterProperty query '%s': %s", query.GetQuery(), query.GetError());
  1536. return false;
  1537. }
  1538. }
  1539. return true;
  1540. }
  1541. bool WorldDatabase::loadCharacterProperties(Client* client) {
  1542. Query query;
  1543. MYSQL_ROW row;
  1544. int32 id = 0;
  1545. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT propname, propvalue FROM charactersProperties where charid = %i", client->GetCharacterID());
  1546. // no character found
  1547. if (result == NULL) {
  1548. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character properties for '%s'", client->GetPlayer()->GetName());
  1549. return false;
  1550. }
  1551. while (result && (row = mysql_fetch_row(result))) {
  1552. char* prop_name = row[0];
  1553. char* prop_value = row[1];
  1554. if (!prop_name || !prop_value)
  1555. continue;
  1556. if (!stricmp(prop_name, CHAR_PROPERTY_SPEED))
  1557. {
  1558. float new_speed = atof(prop_value);
  1559. client->GetPlayer()->SetSpeed(new_speed,true);
  1560. client->GetPlayer()->SetCharSheetChanged(true);
  1561. }
  1562. else if (!stricmp(prop_name, CHAR_PROPERTY_FLYMODE))
  1563. {
  1564. int8 flymode = atoi(prop_value);
  1565. if (flymode) // avoid fly mode notification unless enabled
  1566. ClientPacketFunctions::SendFlyMode(client, flymode, false);
  1567. }
  1568. else if (!stricmp(prop_name, CHAR_PROPERTY_INVUL))
  1569. {
  1570. int8 invul = atoi(prop_value);
  1571. client->GetPlayer()->SetInvulnerable(invul == 1);
  1572. if (client->GetPlayer()->GetInvulnerable())
  1573. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You are now invulnerable!");
  1574. }
  1575. else if (!stricmp(prop_name, CHAR_PROPERTY_GMVISION))
  1576. {
  1577. int8 val = atoi(prop_value);
  1578. client->GetPlayer()->SetGMVision(val == 1);
  1579. client->GetCurrentZone()->SendAllSpawnsForVisChange(client, false);
  1580. if (val)
  1581. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "GM Vision Enabled!");
  1582. }
  1583. else if (!stricmp(prop_name, CHAR_PROPERTY_LUADEBUG))
  1584. {
  1585. int8 val = atoi(prop_value);
  1586. if (val)
  1587. {
  1588. client->SetLuaDebugClient(true);
  1589. if (lua_interface)
  1590. lua_interface->UpdateDebugClients(client);
  1591. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You will now receive LUA error messages.");
  1592. }
  1593. }
  1594. }
  1595. return true;
  1596. }
  1597. //gets the name FROM the db with the right letters in caps
  1598. string WorldDatabase::GetPlayerName(char* name){
  1599. Query query;
  1600. string ret = "";
  1601. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM characters where name='%s'", getSafeEscapeString(name).c_str());
  1602. if(result && mysql_num_rows(result) > 0) {
  1603. MYSQL_ROW row;
  1604. row = mysql_fetch_row(result);
  1605. if(row[0])
  1606. ret = string(row[0]);
  1607. }
  1608. return ret;
  1609. }
  1610. int32 WorldDatabase::GetCharacterID(const char* name) {
  1611. int32 id = 0;
  1612. Query query;
  1613. if (name) {
  1614. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id` FROM `characters` WHERE `name`='%s'", name);
  1615. if (result && mysql_num_rows(result) > 0) {
  1616. MYSQL_ROW row;
  1617. row = mysql_fetch_row(result);
  1618. id = atoul(row[0]);
  1619. }
  1620. }
  1621. return id;
  1622. }
  1623. int32 WorldDatabase::GetCharacterCurrentZoneID(int32 character_id) {
  1624. int32 id = 0;
  1625. Query query;
  1626. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `current_zone_id` FROM `characters` WHERE `id`=%u", character_id);
  1627. if (result && mysql_num_rows(result) > 0) {
  1628. MYSQL_ROW row;
  1629. row = mysql_fetch_row(result);
  1630. id = atoul(row[0]);
  1631. }
  1632. return id;
  1633. }
  1634. int32 WorldDatabase::GetCharacterAccountID(int32 character_id) {
  1635. int32 id = 0;
  1636. Query query;
  1637. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `account_id` FROM `characters` WHERE `id`=%u", character_id);
  1638. if (result && mysql_num_rows(result) > 0) {
  1639. MYSQL_ROW row;
  1640. row = mysql_fetch_row(result);
  1641. id = atoul(row[0]);
  1642. }
  1643. return id;
  1644. }
  1645. sint16 WorldDatabase::GetHighestCharacterAdminStatus(int32 account_id){
  1646. Query query;
  1647. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(admin_status) FROM characters where account_id=%i ",account_id);
  1648. if(result && mysql_num_rows(result) > 0) {
  1649. MYSQL_ROW row;
  1650. row = mysql_fetch_row(result);
  1651. if ( row[0] != NULL )
  1652. return atoi(row[0]); // Return characters status
  1653. else
  1654. return 0;
  1655. }
  1656. return 0;
  1657. }
  1658. sint16 WorldDatabase::GetLowestCharacterAdminStatus(int32 account_id){
  1659. Query query;
  1660. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT min(admin_status) FROM characters where account_id=%i ",account_id);
  1661. if(result && mysql_num_rows(result) > 0) {
  1662. MYSQL_ROW row;
  1663. row = mysql_fetch_row(result);
  1664. if ( row[0] != NULL )
  1665. return atoi(row[0]); // Return characters status
  1666. else
  1667. return 0;
  1668. }
  1669. return 0;
  1670. }
  1671. sint16 WorldDatabase::GetCharacterAdminStatus(char* character_name){
  1672. Query query;
  1673. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT admin_status FROM characters where name='%s'", getSafeEscapeString(character_name).c_str());
  1674. if(result && mysql_num_rows(result) > 0) {
  1675. MYSQL_ROW row;
  1676. row = mysql_fetch_row(result);
  1677. return atoi(row[0]); // Return characters level
  1678. }
  1679. else
  1680. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterAdminStatus query '%s': %s", query.GetQuery(), query.GetError());
  1681. return -10;
  1682. }
  1683. sint16 WorldDatabase::GetCharacterAdminStatus(int32 account_id , int32 char_id){
  1684. Query query;
  1685. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT admin_status FROM characters where account_id=%i and id=%i",account_id,char_id);
  1686. if(result && mysql_num_rows(result) > 0) {
  1687. MYSQL_ROW row;
  1688. row = mysql_fetch_row(result);
  1689. return atoi(row[0]); // Return characters status
  1690. }
  1691. else{
  1692. Query query2;
  1693. result = query2.RunQuery2(Q_SELECT, "SELECT count(id) FROM characters where account_id=%i and id=%i",account_id,char_id);
  1694. if(result && mysql_num_rows(result) > 0) {
  1695. MYSQL_ROW row;
  1696. row = mysql_fetch_row(result);
  1697. if(atoi(row[0]) == 0) //old character, needs to be deleted FROM login server
  1698. return -10;
  1699. return -8;
  1700. }
  1701. else
  1702. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterAdminStatus query '%s': %s", query.GetQuery(), query.GetError());
  1703. }
  1704. return PLAY_ERROR_PROBLEM;
  1705. }
  1706. bool WorldDatabase::UpdateAdminStatus(char* character_name, sint16 flag){
  1707. Query query;
  1708. string update_status = string("update characters set admin_status=%i where name='%s'");
  1709. query.RunQuery2(Q_UPDATE, update_status.c_str(),flag,character_name);
  1710. if(!query.GetAffectedRows())
  1711. {
  1712. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateAdminStatus query '%s': %s", query.GetQuery(), query.GetError());
  1713. return false;
  1714. }
  1715. return true;
  1716. }
  1717. void WorldDatabase::SaveCharacterFloats(int32 char_id, const char* type, float float1, float float2, float float3){
  1718. Query query;
  1719. string create_char = string("insert into char_colors (char_id, type, red, green, blue, signed_value) values(%i,'%s',%i,%i,%i, 1)");
  1720. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, (sint8)(float1*100), (sint8)(float2*100), (sint8)(float3*100));
  1721. if(query.GetError() && strlen(query.GetError()) > 0){
  1722. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterFloats query '%s': %s", query.GetQuery(), query.GetError());
  1723. }
  1724. }
  1725. void WorldDatabase::SaveCharacterColors(int32 char_id, const char* type, EQ2_Color color){
  1726. Query query;
  1727. string create_char = string("insert into char_colors (char_id, type, red, green, blue) values(%i,'%s',%i,%i,%i)");
  1728. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, color.red, color.green, color.blue);
  1729. if(query.GetError() && strlen(query.GetError()) > 0){
  1730. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterColors query '%s': %s", query.GetQuery(), query.GetError());
  1731. }
  1732. }
  1733. void WorldDatabase::SaveNPCAppearanceEquipment(int32 spawn_id, int8 slot_id, int16 type, int8 red, int8 green, int8 blue, int8 hred, int8 hgreen, int8 hblue){
  1734. Query query;
  1735. string appearance = string("INSERT INTO npc_appearance_equip (spawn_id, slot_id, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue) values (%i, %i, %i, %i, %i, %i, %i, %i, %i) ON DUPLICATE KEY UPDATE equip_type=%i, red=%i, green=%i, blue=%i, highlight_red=%i, highlight_green=%i, highlight_blue=%i");
  1736. query.RunQuery2(Q_INSERT, appearance.c_str(), spawn_id, slot_id, type, red, green, blue, hred, hgreen, hblue, type, red, green, blue, hred, hgreen, hblue);
  1737. if(query.GetError() && strlen(query.GetError()) > 0){
  1738. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveNPCAppearanceEquipment query '%s': %s", query.GetQuery(), query.GetError());
  1739. }
  1740. }
  1741. int32 WorldDatabase::LoadNPCAppearanceEquipmentData(ZoneServer* zone){
  1742. Query query;
  1743. MYSQL_ROW row;
  1744. int32 spawn_id = 0, new_spawn_id = 0, count = 0;
  1745. NPC* npc = 0;
  1746. int8 slot = 0;
  1747. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spawn_id, slot_id, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue FROM npc_appearance_equip ORDER BY spawn_id");
  1748. while(result && (row = mysql_fetch_row(result))){
  1749. new_spawn_id = atoul(row[0]);
  1750. if(new_spawn_id != spawn_id){
  1751. npc = zone->GetNPC(new_spawn_id, true);
  1752. if(!npc)
  1753. continue;
  1754. if(spawn_id > 0)
  1755. count++;
  1756. spawn_id = new_spawn_id;
  1757. }
  1758. slot = atoi(row[1]);
  1759. if(slot < NUM_SLOTS){
  1760. npc->equipment.equip_id[slot] = atoi(row[2]);
  1761. npc->equipment.color[slot].red = atoi(row[3]);
  1762. npc->equipment.color[slot].green = atoi(row[4]);
  1763. npc->equipment.color[slot].blue = atoi(row[5]);
  1764. npc->equipment.highlight[slot].red = atoi(row[6]);
  1765. npc->equipment.highlight[slot].green = atoi(row[7]);
  1766. npc->equipment.highlight[slot].blue = atoi(row[8]);
  1767. }
  1768. }
  1769. if(query.GetError() && strlen(query.GetError()) > 0)
  1770. LogWrite(WORLD__ERROR, 0, "World", "Error in LoadNPCAppearanceEquipmentData query '%s': %s", query.GetQuery(), query.GetError());
  1771. return count;
  1772. }
  1773. int16 WorldDatabase::GetAppearanceID(string name){
  1774. int32 id = 0;
  1775. Query query;
  1776. MYSQL_ROW row;
  1777. query.escaped_name = getEscapeString(name.c_str());
  1778. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT appearance_id FROM appearances where name='%s'", query.escaped_name);
  1779. if(result && mysql_num_rows(result) == 1){
  1780. row = mysql_fetch_row(result);
  1781. id = atoi(row[0]);
  1782. }
  1783. return id;
  1784. }
  1785. vector<int16>* WorldDatabase::GetAppearanceIDsLikeName(string name, bool filtered) {
  1786. vector<int16>* ids = 0;
  1787. Query query;
  1788. MYSQL_ROW row;
  1789. query.escaped_name = getEscapeString(name.c_str());
  1790. MYSQL_RES* result;
  1791. if (filtered)
  1792. result = query.RunQuery2(Q_SELECT, "SELECT `appearance_id` FROM `appearances` WHERE `name` RLIKE '%s' AND `name` NOT RLIKE 'ghost' AND `name` NOT RLIKE 'headless' AND `name` NOT RLIKE 'elemental' AND `name` NOT RLIKE 'test' AND `name` NOT RLIKE 'zombie' AND `name` NOT RLIKE 'vampire'", query.escaped_name);
  1793. else
  1794. result = query.RunQuery2(Q_SELECT, "SELECT `appearance_id` FROM `appearances` WHERE `name` RLIKE '%s' AND `name` NOT RLIKE 'ghost' AND `name`", query.escaped_name);
  1795. while (result && (row = mysql_fetch_row(result))) {
  1796. if (!ids)
  1797. ids = new vector<int16>;
  1798. ids->push_back(atoi(row[0]));
  1799. }
  1800. return ids;
  1801. }
  1802. string WorldDatabase::GetAppearanceName(int16 appearance_id) {
  1803. Query query;
  1804. MYSQL_ROW row;
  1805. string name;
  1806. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `name` FROM `appearances` WHERE `appearance_id`=%u", appearance_id);
  1807. if (result && (row = mysql_fetch_row(result)))
  1808. name = string(row[0]);
  1809. return name;
  1810. }
  1811. void WorldDatabase::UpdateRandomize(int32 spawn_id, sint32 value) {
  1812. Query query;
  1813. query.RunQuery2(Q_UPDATE, "UPDATE `spawn_npcs` SET `randomize`=`randomize` + %i WHERE `spawn_id`=%u", value, spawn_id);
  1814. }
  1815. int32 WorldDatabase::SaveCharacter(PacketStruct* create, int32 loginID){
  1816. Query query;
  1817. int8 race_id = create->getType_int8_ByName("race");
  1818. int8 orig_class_id = create->getType_int8_ByName("class");//Normal server
  1819. int8 class_id = orig_class_id;
  1820. if ( create->GetVersion() <= 546 )
  1821. class_id = 0; //Classic Server Only
  1822. int8 gender_id = create->getType_int8_ByName("gender");
  1823. sint16 auto_admin_status = 0;
  1824. // fetch rules related to setting auto-admin status for server
  1825. bool auto_admin_players = rule_manager.GetGlobalRule(R_World, AutoAdminPlayers)->GetBool();
  1826. bool auto_admin_gm = rule_manager.GetGlobalRule(R_World, AutoAdminGMs)->GetBool();
  1827. /*
  1828. The way I think this is supposed to work :) is if any of your chars are already GM, and AutoAdminGMs rule is true,
  1829. set the new character's admin_status to your highest status for any character active on your loginID.
  1830. - If status > 0, new character > 0
  1831. - If status = 0, new character = 0
  1832. - If status < 0, new character < 0, too... even if auto_admin_gm is true
  1833. If we're not a GM (status = 0) but AutoAdminPlayers rule is true,
  1834. set the new character's admin_status to the default set in AutoAdminStatusValue rule.
  1835. Else, if both rules are False, set everyone to 0 like normal.
  1836. */
  1837. auto_admin_status = GetHighestCharacterAdminStatus(loginID);
  1838. if( auto_admin_status > 0 && auto_admin_gm )
  1839. LogWrite(WORLD__WARNING, 0, "World", "New character '%s' granted GM status (%i) from accountID: %i", create->getType_EQ2_16BitString_ByName("name").data.c_str(), auto_admin_status, loginID);
  1840. else if( auto_admin_players )
  1841. {
  1842. auto_admin_status = rule_manager.GetGlobalRule(R_World, AutoAdminStatusValue)->GetSInt16();
  1843. LogWrite(WORLD__DEBUG, 0, "World", "New character '%s' granted AutoAdminPlayer status: %i", create->getType_EQ2_16BitString_ByName("name").data.c_str(), auto_admin_status);
  1844. }
  1845. else
  1846. auto_admin_status = 0;
  1847. string create_char = string("Insert into characters (account_id, server_id, name, race, class, gender, deity, body_size, body_age, soga_wing_type, soga_chest_type, soga_legs_type, soga_hair_type, soga_model_type, legs_type, chest_type, wing_type, hair_type, model_type, facial_hair_type, soga_facial_hair_type, created_date, last_saved, admin_status) values(%i, %i, '%s', %i, %i, %i, %i, %f, %f, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, now(), unix_timestamp(), %i)");
  1848. query.RunQuery2(Q_INSERT, create_char.c_str(),
  1849. loginID,
  1850. create->getType_int32_ByName("server_id"),
  1851. create->getType_EQ2_16BitString_ByName("name").data.c_str(),
  1852. race_id,
  1853. orig_class_id,
  1854. gender_id,
  1855. create->getType_int8_ByName("deity"),
  1856. create->getType_float_ByName("body_size"),
  1857. create->getType_float_ByName("body_age"),
  1858. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_wing_file").data),
  1859. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_chest_file").data),
  1860. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_legs_file").data),
  1861. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_hair_file").data),
  1862. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_race_file").data),
  1863. GetAppearanceID(create->getType_EQ2_16BitString_ByName("legs_file").data),
  1864. GetAppearanceID(create->getType_EQ2_16BitString_ByName("chest_file").data),
  1865. GetAppearanceID(create->getType_EQ2_16BitString_ByName("wing_file").data),
  1866. GetAppearanceID(create->getType_EQ2_16BitString_ByName("hair_file").data),
  1867. GetAppearanceID(create->getType_EQ2_16BitString_ByName("race_file").data),
  1868. GetAppearanceID(create->getType_EQ2_16BitString_ByName("face_file").data),
  1869. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_face_file").data),
  1870. auto_admin_status);
  1871. if(query.GetError() && strlen(query.GetError()) > 0)
  1872. {
  1873. LogWrite(PLAYER__ERROR, 0, "Player", "Error in SaveCharacter query '%s': %s", query.GetQuery(), query.GetError());
  1874. return 0;
  1875. }
  1876. int32 last_insert_id = query.GetLastInsertedID();
  1877. int32 char_id = last_insert_id;
  1878. UpdateStartingFactions(char_id, create->getType_int8_ByName("starting_zone"));
  1879. UpdateStartingZone(char_id, class_id, race_id, create->getType_int8_ByName("starting_zone"));
  1880. UpdateStartingItems(char_id, class_id, race_id);
  1881. UpdateStartingSkills(char_id, class_id, race_id);
  1882. UpdateStartingSpells(char_id, class_id, race_id);
  1883. UpdateStartingSkillbar(char_id, class_id, race_id);
  1884. UpdateStartingTitles(char_id, class_id, race_id, gender_id);
  1885. InsertCharacterStats(char_id, class_id, race_id);
  1886. AddNewPlayerToServerGuild(loginID, char_id);
  1887. SaveCharacterColors(char_id,"skin_color", create->getType_EQ2_Color_ByName("skin_color"));
  1888. SaveCharacterColors(char_id,"eye_color", create->getType_EQ2_Color_ByName("eye_color"));
  1889. SaveCharacterColors(char_id,"hair_color1", create->getType_EQ2_Color_ByName("hair_color1"));
  1890. SaveCharacterColors(char_id,"hair_color2", create->getType_EQ2_Color_ByName("hair_color2"));
  1891. SaveCharacterColors(char_id,"hair_highlight", create->getType_EQ2_Color_ByName("hair_highlight"));
  1892. SaveCharacterColors(char_id,"hair_type_color", create->getType_EQ2_Color_ByName("hair_type_color"));
  1893. SaveCharacterColors(char_id,"hair_type_highlight_color", create->getType_EQ2_Color_ByName("hair_type_highlight_color"));
  1894. SaveCharacterColors(char_id,"hair_face_color", create->getType_EQ2_Color_ByName("hair_face_color"));
  1895. SaveCharacterColors(char_id,"hair_face_highlight_color", create->getType_EQ2_Color_ByName("hair_face_highlight_color"));
  1896. SaveCharacterColors(char_id,"wing_color1", create->getType_EQ2_Color_ByName("wing_color1"));
  1897. SaveCharacterColors(char_id,"wing_color2", create->getType_EQ2_Color_ByName("wing_color2"));
  1898. SaveCharacterColors(char_id,"shirt_color", create->getType_EQ2_Color_ByName("shirt_color"));
  1899. SaveCharacterColors(char_id,"unknown_chest_color", create->getType_EQ2_Color_ByName("unknown_chest_color"));
  1900. SaveCharacterColors(char_id,"pants_color", create->getType_EQ2_Color_ByName("pants_color"));
  1901. SaveCharacterColors(char_id,"unknown_legs_color", create->getType_EQ2_Color_ByName("unknown_legs_color"));
  1902. SaveCharacterColors(char_id,"unknown9", create->getType_EQ2_Color_ByName("unknown9"));
  1903. SaveCharacterFloats(char_id,"eye_type", create->getType_float_ByName("eyes2",0), create->getType_float_ByName("eyes2",1), create->getType_float_ByName("eyes2",2));
  1904. SaveCharacterFloats(char_id,"ear_type", create->getType_float_ByName("ears",0), create->getType_float_ByName("ears",1), create->getType_float_ByName("ears",2));
  1905. SaveCharacterFloats(char_id,"eye_brow_type", create->getType_float_ByName("eye_brows",0), create->getType_float_ByName("eye_brows",1), create->getType_float_ByName("eye_brows",2));
  1906. SaveCharacterFloats(char_id,"cheek_type", create->getType_float_ByName("cheeks",0), create->getType_float_ByName("cheeks",1), create->getType_float_ByName("cheeks",2));
  1907. SaveCharacterFloats(char_id,"lip_type", create->getType_float_ByName("lips",0), create->getType_float_ByName("lips",1), create->getType_float_ByName("lips",2));
  1908. SaveCharacterFloats(char_id,"chin_type", create->getType_float_ByName("chin",0), create->getType_float_ByName("chin",1), create->getType_float_ByName("chin",2));
  1909. SaveCharacterFloats(char_id,"nose_type", create->getType_float_ByName("nose",0), create->getType_float_ByName("nose",1), create->getType_float_ByName("nose",2));
  1910. SaveCharacterFloats(char_id,"body_size", create->getType_float_ByName("body_size",0), 0, 0);
  1911. SaveCharacterColors(char_id,"soga_skin_color", create->getType_EQ2_Color_ByName("soga_skin_color"));
  1912. SaveCharacterColors(char_id,"soga_eye_color", create->getType_EQ2_Color_ByName("soga_eye_color"));
  1913. SaveCharacterColors(char_id,"soga_hair_color1", create->getType_EQ2_Color_ByName("soga_hair_color1"));
  1914. SaveCharacterColors(char_id,"soga_hair_color2", create->getType_EQ2_Color_ByName("soga_hair_color2"));
  1915. SaveCharacterColors(char_id,"soga_hair_highlight", create->getType_EQ2_Color_ByName("soga_hair_highlight"));
  1916. SaveCharacterColors(char_id,"soga_hair_type_color", create->getType_EQ2_Color_ByName("soga_hair_type_color"));
  1917. SaveCharacterColors(char_id,"soga_hair_type_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_type_highlight_color"));
  1918. SaveCharacterColors(char_id,"soga_hair_face_color", create->getType_EQ2_Color_ByName("soga_hair_face_color"));
  1919. SaveCharacterColors(char_id,"soga_hair_face_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_face_highlight_color"));
  1920. SaveCharacterColors(char_id,"soga_wing_color1", create->getType_EQ2_Color_ByName("soga_wing_color1"));
  1921. SaveCharacterColors(char_id,"soga_wing_color2", create->getType_EQ2_Color_ByName("soga_wing_color2"));
  1922. SaveCharacterColors(char_id,"soga_shirt_color", create->getType_EQ2_Color_ByName("soga_shirt_color"));
  1923. SaveCharacterColors(char_id,"soga_unknown_chest_color", create->getType_EQ2_Color_ByName("soga_unknown_chest_color"));
  1924. SaveCharacterColors(char_id,"soga_pants_color", create->getType_EQ2_Color_ByName("soga_pants_color"));
  1925. SaveCharacterColors(char_id,"soga_unknown_legs_color", create->getType_EQ2_Color_ByName("soga_unknown_legs_color"));
  1926. SaveCharacterColors(char_id,"soga_unknown13", create->getType_EQ2_Color_ByName("soga_unknown13"));
  1927. SaveCharacterFloats(char_id,"soga_eye_type", create->getType_float_ByName("soga_eyes2",0), create->getType_float_ByName("soga_eyes2",1), create->getType_float_ByName("soga_eyes2",2));
  1928. SaveCharacterFloats(char_id,"soga_ear_type", create->getType_float_ByName("soga_ears",0), create->getType_float_ByName("soga_ears",1), create->getType_float_ByName("soga_ears",2));
  1929. SaveCharacterFloats(char_id,"soga_eye_brow_type", create->getType_float_ByName("soga_eye_brows",0), create->getType_float_ByName("soga_eye_brows",1), create->getType_float_ByName("soga_eye_brows",2));
  1930. SaveCharacterFloats(char_id,"soga_cheek_type", create->getType_float_ByName("soga_cheeks",0), create->getType_float_ByName("soga_cheeks",1), create->getType_float_ByName("soga_cheeks",2));
  1931. SaveCharacterFloats(char_id,"soga_lip_type", create->getType_float_ByName("soga_lips",0), create->getType_float_ByName("soga_lips",1), create->getType_float_ByName("soga_lips",2));
  1932. SaveCharacterFloats(char_id,"soga_chin_type", create->getType_float_ByName("soga_chin",0), create->getType_float_ByName("soga_chin",1), create->getType_float_ByName("soga_chin",2));
  1933. SaveCharacterFloats(char_id,"soga_nose_type", create->getType_float_ByName("soga_nose",0), create->getType_float_ByName("soga_nose",1), create->getType_float_ByName("soga_nose",2));
  1934. return char_id;
  1935. }
  1936. int8 WorldDatabase::CheckNameFilter(const char* name) {
  1937. // the minimum 4 is enforced by the client too
  1938. if(!name || strlen(name) < 4 || strlen(name) > 15) // Even 20 char length is long...
  1939. return BADNAMELENGTH_REPLY;
  1940. uchar* checkname = (uchar*)name;
  1941. for (int32 i = 0; i < strlen(name); i++)
  1942. {
  1943. if(!alpha_check(checkname[i]))
  1944. return NAMEINVALID_REPLY;
  1945. }
  1946. Query query;
  1947. LogWrite(WORLD__DEBUG, 0, "World", "Name check on: %s", name);
  1948. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT count(*) FROM characters WHERE name='%s'",name);
  1949. if(result && mysql_num_rows(result) > 0) {
  1950. MYSQL_ROW row;
  1951. row = mysql_fetch_row(result);
  1952. if(row[0] != 0 && atoi(row[0]) > 0)
  1953. return NAMETAKEN_REPLY;
  1954. }
  1955. else
  1956. LogWrite(WORLD__ERROR, 0, "World", "Error in CheckNameFilter (name exist check) (Name query '%s': %s", query.GetQuery(), query.GetError());
  1957. Query query3;
  1958. LogWrite(WORLD__DEBUG, 0, "World", "Name check on: %s (Bots table)", name);
  1959. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT count(*) FROM bots WHERE name='%s'", name);
  1960. if (result3 && mysql_num_rows(result3) > 0) {
  1961. MYSQL_ROW row;
  1962. row = mysql_fetch_row(result3);
  1963. if (row[0] != 0 && atoi(row[0]) > 0)
  1964. return NAMETAKEN_REPLY;
  1965. }
  1966. else
  1967. LogWrite(WORLD__ERROR, 0, "World", "Error in CheckNameFilter (name exist check, bot table) (Name query '%s': %s", query3.GetQuery(), query3.GetError());
  1968. Query query2;
  1969. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT count(*) FROM name_filter WHERE '%s' like name",name);
  1970. if(result2 && mysql_num_rows(result2) > 0) {
  1971. MYSQL_ROW row;
  1972. row = mysql_fetch_row(result2);
  1973. if(row[0] != 0 && atoi(row[0]) > 0)
  1974. return NAMEFILTER_REPLY;
  1975. else if(row[0] != 0 && atoi(row[0]) == 0)
  1976. return CREATESUCCESS_REPLY;
  1977. }
  1978. else
  1979. LogWrite(WORLD__ERROR, 0, "World", "Error in CheckNameFilter (name_filter check) query '%s': %s", query.GetQuery(), query.GetError());
  1980. return UNKNOWNERROR_REPLY;
  1981. }
  1982. char* WorldDatabase::GetCharacterName(int32 character_id){
  1983. LogWrite(WORLD__TRACE, 9, "World", "Enter: %s", __FUNCTION__);
  1984. Query query;
  1985. char* name = 0;
  1986. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM characters where id=%u",character_id);
  1987. if(result && mysql_num_rows(result) > 0) {
  1988. MYSQL_ROW row;
  1989. row = mysql_fetch_row(result);
  1990. if(row[0] && strlen(row[0]) > 0)
  1991. {
  1992. name = new char[strlen(row[0])+1];
  1993. memset(name,0, strlen(row[0])+1);
  1994. strcpy(name, row[0]);
  1995. }
  1996. }
  1997. else
  1998. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterName query '%s': %s", query.GetQuery(), query.GetError());
  1999. LogWrite(WORLD__TRACE, 9, "World", "Exit: %s", __FUNCTION__);
  2000. return name;
  2001. }
  2002. int8 WorldDatabase::GetCharacterLevel(int32 character_id){
  2003. Query query;
  2004. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT level FROM characters where id=%u",character_id);
  2005. if(result && mysql_num_rows(result) > 0) {
  2006. MYSQL_ROW row;
  2007. row = mysql_fetch_row(result);
  2008. return atoi(row[0]); // Return characters level
  2009. }
  2010. else
  2011. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterLevel query '%s': %s", query.GetQuery(), query.GetError());
  2012. return 0;
  2013. }
  2014. int16 WorldDatabase::GetCharacterModelType(int32 character_id){
  2015. Query query;
  2016. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT model_type FROM characters where id=%u",character_id);
  2017. if(result && mysql_num_rows(result) > 0) {
  2018. MYSQL_ROW row;
  2019. row = mysql_fetch_row(result);
  2020. return atoi(row[0]); // Return characters race
  2021. }
  2022. else
  2023. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterModelType query '%s': %s", query.GetQuery(), query.GetError());
  2024. return 0;
  2025. }
  2026. int8 WorldDatabase::GetCharacterClass(int32 character_id){
  2027. Query query;
  2028. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT class FROM characters where id=%u",character_id);
  2029. if(result && mysql_num_rows(result) > 0) {
  2030. MYSQL_ROW row;
  2031. row = mysql_fetch_row(result);
  2032. return atoi(row[0]); // Return characters class
  2033. }
  2034. else
  2035. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterClass query '%s': %s", query.GetQuery(), query.GetError());
  2036. return 0;
  2037. }
  2038. int8 WorldDatabase::GetCharacterGender(int32 character_id){
  2039. Query query;
  2040. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT gender FROM characters where id=%u",character_id);
  2041. if(result && mysql_num_rows(result) > 0) {
  2042. MYSQL_ROW row;
  2043. row = mysql_fetch_row(result);
  2044. return atoi(row[0]); // Return characters gender
  2045. }
  2046. else
  2047. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterGender query '%s': %s", query.GetQuery(), query.GetError());
  2048. return 0;
  2049. }
  2050. void WorldDatabase::DeleteCharacterQuest(int32 quest_id, int32 char_id, bool repeated_quest) {
  2051. if (repeated_quest) {
  2052. if (!database_new.Query("UPDATE `character_quests` SET `given_date` = `completed_date` WHERE `char_id` = %u AND `quest_id` = %u", char_id, quest_id))
  2053. LogWrite(DATABASE__ERROR, 0, "DBNew", "Error (%u) in DeleteCharacterQuest query:\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2054. }
  2055. else {
  2056. if (!database_new.Query("DELETE FROM `character_quests` WHERE `char_id` = %u AND `quest_id` = %u", char_id, quest_id))
  2057. LogWrite(DATABASE__ERROR, 0, "DBNew", "Error (%u) in DeleteCharacterQuest query:\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2058. }
  2059. if (!database_new.Query("DELETE FROM `character_quest_progress` WHERE `char_id` = %u AND `quest_id` = %u", char_id, quest_id))
  2060. LogWrite(DATABASE__ERROR, 0, "DBNew", "Error (%u) in DeleteCharacterQuest query:\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2061. }
  2062. void WorldDatabase::SaveCharacterSkills(Client* client){
  2063. vector<Skill*>* skills = client->GetPlayer()->GetSkills()->GetSaveNeededSkills();
  2064. if(skills){
  2065. Query query;
  2066. if(skills->size() > 0){
  2067. Skill* skill = 0;
  2068. for(int32 i=0;i<skills->size();i++){
  2069. skill = skills->at(i);
  2070. query.AddQueryAsync(client->GetCharacterID(),this,Q_REPLACE, "replace into character_skills (char_id, skill_id, current_val, max_val) values(%u, %u, %i, %i)", client->GetCharacterID(), skill->skill_id, skill->current_val, skill->max_val);
  2071. }
  2072. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2073. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterSkills query '%s': %s", query.GetQuery(), query.GetError());
  2074. }
  2075. safe_delete(skills);
  2076. }
  2077. }
  2078. void WorldDatabase::SaveCharacterQuests(Client* client){
  2079. Query query;
  2080. map<int32, Quest*>::iterator itr;
  2081. master_quest_list.LockQuests(); //prevent reloading until we are done
  2082. client->GetPlayer()->LockQuests(); //prevent all quest modifications until we are done
  2083. map<int32, Quest*>* quests = client->GetPlayer()->GetPlayerQuests();
  2084. for(itr = quests->begin(); itr != quests->end(); itr++){
  2085. if(client->GetCurrentQuestID() == itr->first){
  2086. query.AddQueryAsync(client->GetCharacterID(),this,Q_UPDATE, "update character_quests set current_quest = 0 where char_id = %u", client->GetCharacterID());
  2087. query.AddQueryAsync(client->GetCharacterID(), this,Q_UPDATE, "update character_quests set current_quest = 1 where char_id = %u and quest_id = %u", client->GetCharacterID(), itr->first);
  2088. }
  2089. if(itr->second->GetSaveNeeded()){
  2090. query.AddQueryAsync(client->GetCharacterID(), this,Q_INSERT, "insert ignore into character_quests (char_id, quest_id, given_date, quest_giver) values(%u, %u, now(), %u)", client->GetCharacterID(), itr->first, itr->second->GetQuestGiver());
  2091. query.AddQueryAsync(client->GetCharacterID(), this,Q_UPDATE, "update character_quests set tracked = %i, quest_flags = %u, hidden = %i, complete_count = %u where char_id = %u and quest_id = %u", itr->second->IsTracked() ? 1 : 0, itr->second->GetQuestFlags(), itr->second->IsHidden() ? 1 : 0, itr->second->GetCompleteCount(), client->GetCharacterID(), itr->first);
  2092. SaveCharacterQuestProgress(client, itr->second);
  2093. itr->second->SetSaveNeeded(false);
  2094. }
  2095. }
  2096. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2097. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterQuests query '%s': %s", query.GetQuery(), query.GetError());
  2098. quests = client->GetPlayer()->GetCompletedPlayerQuests();
  2099. for(itr = quests->begin(); itr != quests->end(); itr++){
  2100. if(itr->second->GetSaveNeeded()){
  2101. query.AddQueryAsync(client->GetCharacterID(), this,Q_DELETE, "delete FROM character_quest_progress where char_id = %u and quest_id = %u", client->GetCharacterID(), itr->first);
  2102. /* incase the quest is completed before the quest could be inserted in the PlayerQuests loop, we first try to insert it. If it already exists then we can just update
  2103. * the completed_date */
  2104. query.AddQueryAsync(client->GetCharacterID(), this,Q_INSERT, "INSERT INTO character_quests (char_id, quest_id, quest_giver, current_quest, given_date, completed_date, complete_count) values (%u,%u,%u,0, now(),now(), %u) ON DUPLICATE KEY UPDATE completed_date = now(), complete_count = %u, current_quest = 0", client->GetCharacterID(), itr->first, itr->second->GetQuestGiver(), itr->second->GetCompleteCount(), itr->second->GetCompleteCount());
  2105. itr->second->SetSaveNeeded(false);
  2106. }
  2107. }
  2108. client->GetPlayer()->UnlockQuests();
  2109. master_quest_list.UnlockQuests();
  2110. }
  2111. void WorldDatabase::SaveCharRepeatableQuest(Client* client, int32 quest_id, int16 quest_complete_count) {
  2112. if (!database_new.Query("UPDATE `character_quests` SET `given_date` = now(), complete_count = %u WHERE `char_id` = %u AND `quest_id` = %u", quest_complete_count, client->GetCharacterID(), quest_id))
  2113. LogWrite(DATABASE__ERROR, 0, "DBNew", "DB Error %u\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2114. }
  2115. void WorldDatabase::SaveCharacterQuestProgress(Client* client, Quest* quest){
  2116. Query query;
  2117. vector<QuestStep*>* steps = quest->GetQuestSteps();
  2118. vector<QuestStep*>::iterator itr;
  2119. QuestStep* step = 0;
  2120. if(steps){
  2121. for(itr = steps->begin(); itr != steps->end(); itr++){
  2122. step = *itr;
  2123. if(step && step->GetQuestCurrentQuantity() > 0)
  2124. query.RunQuery2(Q_REPLACE, "replace into character_quest_progress (char_id, quest_id, step_id, progress) values(%u, %u, %u, %i)", client->GetCharacterID(), quest->GetQuestID(), step->GetStepID(), step->GetQuestCurrentQuantity());
  2125. }
  2126. }
  2127. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2128. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterQuestProgress query '%s': %s", query.GetQuery(), query.GetError());
  2129. }
  2130. void WorldDatabase::LoadCharacterQuestProgress(Client* client){
  2131. Query query;
  2132. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT character_quest_progress.quest_id, step_id, progress FROM character_quest_progress, character_quests where character_quest_progress.char_id=%u and character_quest_progress.quest_id = character_quests.quest_id and character_quest_progress.char_id = character_quests.char_id ORDER BY character_quest_progress.quest_id",client->GetCharacterID());
  2133. if(result && mysql_num_rows(result) > 0) {
  2134. MYSQL_ROW row;
  2135. Quest* quest = 0;
  2136. int32 quest_id = 0;
  2137. map<int32, int32>* progress_map = new map<int32, int32>();
  2138. while(result && (row = mysql_fetch_row(result))){
  2139. if(quest_id != atoul(row[0])){
  2140. if(quest_id > 0){
  2141. quest = client->GetPlayer()->GetQuest(quest_id);
  2142. if(quest)
  2143. client->SetPlayerQuest(quest, progress_map);
  2144. }
  2145. quest_id = atoul(row[0]);
  2146. progress_map->clear();
  2147. }
  2148. (*progress_map)[atoul(row[1])] = atoul(row[2]);
  2149. }
  2150. if(progress_map->size() > 0){
  2151. quest = client->GetPlayer()->GetQuest(quest_id);
  2152. if(quest)
  2153. client->SetPlayerQuest(quest, progress_map);
  2154. }
  2155. safe_delete(progress_map);
  2156. }
  2157. else if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2158. LogWrite(WORLD__ERROR, 0, "World", "Error in LoadCharacterQuestProgress query '%s': %s", query.GetQuery(), query.GetError());
  2159. }
  2160. void WorldDatabase::LoadCharacterQuests(Client* client){
  2161. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading Character Quests...");
  2162. Query query;
  2163. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT quest_id, DAY(given_date), MONTH(given_date), YEAR(given_date), DAY(completed_date), MONTH(completed_date), YEAR(completed_date), quest_giver, tracked, quest_flags, hidden, UNIX_TIMESTAMP(given_date), UNIX_TIMESTAMP(completed_date), complete_count FROM character_quests WHERE char_id=%u ORDER BY current_quest", client->GetCharacterID());
  2164. if(result && mysql_num_rows(result) > 0) {
  2165. MYSQL_ROW row;
  2166. Quest* quest = 0;
  2167. while(result && (row = mysql_fetch_row(result))){
  2168. quest = master_quest_list.GetQuest(atoul(row[0]));
  2169. if(quest) {
  2170. LogWrite(PLAYER__DEBUG, 5, "Player", "\tLoading quest_id: %u", atoul(row[0]));
  2171. bool addQuest = true;
  2172. if(row[4] && atoi(row[4]) > 0){
  2173. quest->SetTurnedIn(true);
  2174. if(row[4])
  2175. quest->SetDay(atoi(row[4]));
  2176. if(row[5])
  2177. quest->SetMonth(atoi(row[5]));
  2178. if(row[6] && atoi(row[6]) > 2000)
  2179. quest->SetYear(atoi(row[6]) - 2000);
  2180. client->GetPlayer()->AddCompletedQuest(quest);
  2181. // Added timestamps to quickly compare given and completed dates
  2182. int32 given_timestamp = atoul(row[11]);
  2183. int32 completed_timestamp = atoul(row[12]);
  2184. // If given timestamp is greater then completed then this is a repeatable quest we are working on
  2185. // so get a fresh quest object to add as an active quest
  2186. if (given_timestamp > completed_timestamp)
  2187. quest = master_quest_list.GetQuest(atoul(row[0]));
  2188. else
  2189. addQuest = false;
  2190. quest->SetCompleteCount(atoi(row[13]));
  2191. }
  2192. if (addQuest) {
  2193. if(row[1])
  2194. quest->SetDay(atoi(row[1]));
  2195. if(row[2])
  2196. quest->SetMonth(atoi(row[2]));
  2197. if(row[3] && atoi(row[3]) > 2000)
  2198. quest->SetYear(atoi(row[3]) - 2000);
  2199. quest->SetQuestGiver(atoul(row[7]));
  2200. quest->SetTracked(atoi(row[8]) == 1 ? true : false);
  2201. quest->SetQuestFlags(atoul(row[9]));
  2202. quest->SetHidden(atoi(row[10]) == 1 ? true : false);
  2203. client->AddPlayerQuest(quest, false, false);
  2204. }
  2205. quest->SetSaveNeeded(false);
  2206. // Changed this to call reload with step = 0 for all quests and not
  2207. // just quests with quest flags to allow customized set up if needed
  2208. if (lua_interface)
  2209. lua_interface->CallQuestFunction(quest, "Reload", client->GetPlayer(), 0);
  2210. }
  2211. }
  2212. LoadCharacterQuestProgress(client);
  2213. }
  2214. }
  2215. void WorldDatabase::LoadCharacterFriendsIgnoreList(Player* player) {
  2216. if (player) {
  2217. Query query;
  2218. MYSQL_ROW row;
  2219. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `name`, `type` FROM `character_social` WHERE `char_id`=%u", player->GetCharacterID());
  2220. while (result && (row = mysql_fetch_row(result))) {
  2221. if (strncmp(row[1], "FRIEND", 6) == 0)
  2222. player->AddFriend(row[0], false);
  2223. else
  2224. player->AddIgnore(row[0], false);
  2225. }
  2226. }
  2227. }
  2228. void WorldDatabase::LoadZoneInfo(ZoneServer* zone){
  2229. Query query;
  2230. int32 ruleset_id;
  2231. char* escaped = getEscapeString(zone->GetZoneName());
  2232. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, file, description, underworld, safe_x, safe_y, safe_z, min_status, min_level, max_level, instance_type+0, shutdown_timer, zone_motd, default_reenter_time, default_reset_time, default_lockout_time, force_group_to_zone, safe_heading, xp_modifier, ruleset_id, expansion_id, weather_allowed, sky_file FROM zones where name='%s'",escaped);
  2233. if(result && mysql_num_rows(result) > 0) {
  2234. MYSQL_ROW row;
  2235. row = mysql_fetch_row(result);
  2236. zone->SetZoneName(escaped);
  2237. zone->SetZoneID(strtoul(row[0], NULL, 0));
  2238. zone->SetZoneFile(row[1]);
  2239. zone->SetZoneDescription(row[2]);
  2240. zone->SetUnderWorld(atof(row[3]));
  2241. zone->SetSafeX(atof(row[4]));
  2242. zone->SetSafeY(atof(row[5]));
  2243. zone->SetSafeZ(atof(row[6]));
  2244. zone->SetMinimumStatus(atoi(row[7]));
  2245. zone->SetMinimumLevel(atoi(row[8]));
  2246. zone->SetMaximumLevel(atoi(row[9]));
  2247. int8 type = (atoi(row[10]) == 0) ? 0 : atoi(row[10]) - 1;
  2248. zone->SetInstanceType(type);
  2249. zone->SetShutdownTimer(atoul(row[11]));
  2250. char* zone_motd = row[12];
  2251. if (zone_motd && strlen(zone_motd) > 0)
  2252. zone->SetZoneMOTD(string(zone_motd));
  2253. zone->SetDefaultReenterTime(atoi(row[13]));
  2254. zone->SetDefaultResetTime(atoi(row[14]));
  2255. zone->SetDefaultLockoutTime(atoi(row[15]));
  2256. zone->SetForceGroupZoneOption(atoi(row[16]));
  2257. zone->SetSafeHeading(atof(row[17]));
  2258. zone->SetXPModifier(atof(row[18]));
  2259. if ((ruleset_id = atoul(row[19])) > 0 && !rule_manager.SetZoneRuleSet(zone->GetZoneID(), ruleset_id))
  2260. LogWrite(ZONE__ERROR, 0, "Zones", "Error setting rule set for zone '%s' (%u). A rule set with ID %u does not exist.", zone->GetZoneName(), zone->GetZoneID(), ruleset_id);
  2261. // check data_version to see if client has proper expansion to enter a zone
  2262. zone->SetMinimumVersion(GetMinimumClientVersion(atoi(row[20])));
  2263. zone->SetWeatherAllowed(atoi(row[21]) == 0 ? false : true);
  2264. zone->SetZoneSkyFile(row[22]);
  2265. if (zone->IsInstanceZone())
  2266. {
  2267. if ( zone->GetInstanceID() < 1 )
  2268. zone->SetupInstance(CreateNewInstance(zone->GetZoneID()));
  2269. else
  2270. zone->SetupInstance(zone->GetInstanceID());
  2271. }
  2272. }
  2273. safe_delete_array(escaped);
  2274. }
  2275. void WorldDatabase::LoadZoneInfo(ZoneInfo* zone_info) {
  2276. Query query;
  2277. int32 ruleset_id;
  2278. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name, file, description, underworld, safe_x, safe_y, safe_z, min_status, min_level, max_level, instance_type, shutdown_timer, zone_motd, default_reenter_time, default_reset_time, default_lockout_time, force_group_to_zone, lua_script, xp_modifier, ruleset_id, expansion_id, always_loaded, city_zone, start_zone, zone_type, weather_allowed FROM zones WHERE id = %u", zone_info->id);
  2279. if (result && mysql_num_rows(result) > 0) {
  2280. MYSQL_ROW row;
  2281. row = mysql_fetch_row(result);
  2282. strncpy(zone_info->name, row[0], sizeof(zone_info->name));
  2283. strncpy(zone_info->file, row[1], sizeof(zone_info->file));
  2284. strncpy(zone_info->description, row[2], sizeof(zone_info->description));
  2285. zone_info->underworld = atof(row[3]);
  2286. zone_info->safe_x = atof(row[4]);
  2287. zone_info->safe_y = atof(row[5]);
  2288. zone_info->safe_z = atof(row[6]);
  2289. zone_info->min_status = atoi(row[7]);
  2290. zone_info->min_level = atoi(row[8]);
  2291. zone_info->max_level = atoi(row[9]);
  2292. zone_info->instance_type = (atoi(row[10]) == 0) ? 0 : atoi(row[10]) - 1;
  2293. zone_info->shutdown_timer = atoul(row[11]);
  2294. row[12] == NULL ? strncpy(zone_info->zone_motd, "", sizeof(zone_info->zone_motd)) : strncpy(zone_info->zone_motd, row[12], sizeof(zone_info->zone_motd));
  2295. zone_info->default_reenter_time = atoi(row[13]);
  2296. zone_info->default_reset_time = atoi(row[14]);
  2297. zone_info->default_lockout_time = atoi(row[15]);
  2298. zone_info->force_group_to_zone = atoi(row[16]);
  2299. row[17] == NULL ? strncpy(zone_info->lua_script, "", sizeof(zone_info->lua_script)) : strncpy(zone_info->lua_script, row[17], sizeof(zone_info->lua_script));
  2300. zone_info->xp_modifier = atof(row[18]);
  2301. zone_info->ruleset_id = atoul(row[19]);
  2302. if ((ruleset_id = atoul(row[19])) > 0 && !rule_manager.SetZoneRuleSet(zone_info->id, ruleset_id))
  2303. LogWrite(ZONE__ERROR, 0, "Zones", "Error setting rule set for zone '%s' (%u). A rule set with ID %u does not exist.", zone_info->name, zone_info->id, ruleset_id);
  2304. zone_info->expansion_id = atoi(row[20]);
  2305. zone_info->min_version = GetMinimumClientVersion(zone_info->expansion_id);
  2306. zone_info->always_loaded = atoi(row[21]);
  2307. zone_info->city_zone = atoi(row[22]);
  2308. zone_info->start_zone = atoi(row[23]);
  2309. row[24] == NULL ? strncpy(zone_info->zone_type, "", sizeof(zone_info->zone_type)) : strncpy(zone_info->zone_type, row[24], sizeof(zone_info->zone_type));
  2310. zone_info->weather_allowed = atoi(row[25]);
  2311. }
  2312. }
  2313. void WorldDatabase::SaveZoneInfo(int32 zone_id, const char* field, sint32 value) {
  2314. Query query;
  2315. query.RunQuery2(Q_UPDATE, "UPDATE `zones` SET `%s`=%i WHERE `id`=%u", field, value, zone_id);
  2316. }
  2317. void WorldDatabase::SaveZoneInfo(int32 zone_id, const char* field, float value) {
  2318. Query query;
  2319. query.RunQuery2(Q_UPDATE, "UPDATE `zones` SET `%s`=%f WHERE `id`=%u", field, value, zone_id);
  2320. }
  2321. void WorldDatabase::SaveZoneInfo(int32 zone_id, const char* field, const char* value) {
  2322. Query query;
  2323. query.RunQuery2(Q_UPDATE, "UPDATE `zones` SET `%s`='%s' WHERE `id`=%u", field, const_cast<char*>(getEscapeString(value)), zone_id);
  2324. }
  2325. int32 WorldDatabase::GetZoneID(const char* name) {
  2326. int32 zone_id = 0;
  2327. Query query;
  2328. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id` FROM zones WHERE `name`='%s'", name);
  2329. if (result && mysql_num_rows(result) > 0) {
  2330. MYSQL_ROW row;
  2331. row = mysql_fetch_row(result);
  2332. zone_id = atoi(row[0]);
  2333. }
  2334. return zone_id;
  2335. }
  2336. bool WorldDatabase::GetZoneRequirements(const char* zoneName, sint16* minStatus, int16* minLevel, int16* maxLevel, int16* minVersion) {
  2337. Query query;
  2338. char* escaped = getEscapeString(zoneName);
  2339. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT min_status, min_level, max_level, expansion_id FROM zones where name='%s'",escaped);
  2340. if(result && mysql_num_rows(result) > 0) {
  2341. MYSQL_ROW row;
  2342. row = mysql_fetch_row(result);
  2343. sint16 status = (sint16)atoi(row[0]);
  2344. int16 levelMin = (int16)atoi(row[1]);
  2345. int16 levelMax = (int16)atoi(row[2]);
  2346. int8 expansion_id = (int8)atoi(row[3]);
  2347. *minStatus = status;
  2348. *minLevel = levelMin;
  2349. *maxLevel = levelMax;
  2350. if( expansion_id >= 40 ) // lowest client we support is RoK - exp04
  2351. *minVersion = GetMinimumClientVersion(expansion_id);
  2352. else
  2353. *minVersion = 0;
  2354. safe_delete_array(escaped);
  2355. return true;
  2356. }
  2357. safe_delete_array(escaped);
  2358. return false;
  2359. }
  2360. int16 WorldDatabase::GetMinimumClientVersion(int8 expansion_id)
  2361. {
  2362. /*
  2363. 1 n/a Classic Expansion
  2364. 2 adv01 Bloodline Chronicles
  2365. 3 adv02 Splitpaw Saga
  2366. 10 exp01 Desert of Flames
  2367. 20 exp02 Kingdom of Sky
  2368. 21 adv04 Fallen Dynasty
  2369. 30 exp03 Echoes of Faydwer
  2370. 40 exp04 Rise of Kunark
  2371. 50 exp05 The Shadow Odyssey
  2372. 60 exp06 Sentinel's Fate
  2373. 61 halas Halas Reborn
  2374. 70 exp07 Destiny of Velious
  2375. 80 exp08 Age of Discovery
  2376. */
  2377. int16 minVer = 0;
  2378. // TODO: eventually replace this with reading values from eq2expansions table
  2379. switch(expansion_id)
  2380. {
  2381. case 40: // ROK
  2382. {
  2383. minVer = 843;
  2384. break;
  2385. }
  2386. case 50: // TSO
  2387. {
  2388. minVer = 908;
  2389. break;
  2390. }
  2391. case 60: // SF
  2392. {
  2393. minVer = 1008;
  2394. break;
  2395. }
  2396. case 61: // Halas
  2397. {
  2398. minVer = 1045;
  2399. break;
  2400. }
  2401. case 70: // DoV
  2402. {
  2403. minVer = 1096;
  2404. break;
  2405. }
  2406. case 80: // AoD
  2407. {
  2408. minVer = 1144;
  2409. break;
  2410. }
  2411. case 90: // CoE
  2412. {
  2413. minVer = 1188;
  2414. break;
  2415. }
  2416. }
  2417. return minVer;
  2418. }
  2419. // returns Expansion Name depending on the connected client's data version
  2420. string WorldDatabase::GetExpansionIDByVersion(int16 version)
  2421. {
  2422. /*
  2423. 0 n/a Classic Expansion
  2424. 0 adv01 Bloodline Chronicles
  2425. 0 adv02 Splitpaw Saga
  2426. 0 exp01 Desert of Flames
  2427. 0 exp02 Kingdom of Sky
  2428. 0 adv04 Fallen Dynasty
  2429. 0 exp03 Echoes of Faydwer
  2430. 843 exp04 Rise of Kunark
  2431. 908 exp05 The Shadow Odyssey
  2432. 1008 exp06 Sentinel's Fate
  2433. 1045 halas Halas Reborn
  2434. 1096 exp07 Destiny of Velious
  2435. 1142 exp08 Age of Discovery
  2436. 1188 exp09 Chains of Eternity
  2437. 9999 (and beyond)
  2438. */
  2439. string ret = "";
  2440. if( version >= 9999 )
  2441. ret = "Unknown";
  2442. else if( version >= 1188 )
  2443. ret = "Chains of Eternity";
  2444. else if( version >= 1142 )
  2445. ret = "Age of Discovery";
  2446. else if( version >= 1096 )
  2447. ret = "Destiny of Velious";
  2448. else if( version >= 1045 )
  2449. ret = "Halas Reborn";
  2450. else if( version >= 1008 )
  2451. ret = "Sentinel's Fate";
  2452. else if( version >= 908 )
  2453. ret = "The Shadow Odyssey";
  2454. else if( version >= 843 )
  2455. ret = "Rise of Kunark";
  2456. else
  2457. ret = "Any";
  2458. return ret;
  2459. }
  2460. void WorldDatabase::LoadSpecialZones(){
  2461. Query query;
  2462. ZoneServer* zone = 0;
  2463. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name, always_loaded, city_zone FROM zones where always_loaded = 1 or city_zone = 1");
  2464. if(result && mysql_num_rows(result) > 0) {
  2465. MYSQL_ROW row;
  2466. while(result && (row = mysql_fetch_row(result))){
  2467. zone = new ZoneServer(row[1]);
  2468. LoadZoneInfo(zone);
  2469. zone->Init();
  2470. zone->SetAlwaysLoaded(atoi(row[2]) == 1);
  2471. zone->SetCityZone(atoi(row[3]) == 1);
  2472. }
  2473. }
  2474. }
  2475. bool WorldDatabase::SpawnGroupRemoveAssociation(int32 group1, int32 group2){
  2476. Query query;
  2477. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_group_associations where (group_id1 = %u and group_id2 = %u) or (group_id1 = %u and group_id2 = %u)", group1, group2, group2, group1);
  2478. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2479. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupRemoveSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2480. return false;
  2481. }
  2482. return true;
  2483. }
  2484. bool WorldDatabase::SpawnGroupAddAssociation(int32 group1, int32 group2){
  2485. Query query;
  2486. query.RunQuery2(Q_INSERT, "insert ignore into spawn_location_group_associations (group_id1, group_id2) values(%u, %u)", group1, group2);
  2487. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2488. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupAddAssociation query '%s': %s", query.GetQuery(), query.GetError());
  2489. return false;
  2490. }
  2491. return true;
  2492. }
  2493. bool WorldDatabase::SpawnGroupAddSpawn(Spawn* spawn, int32 group_id){
  2494. Query query;
  2495. query.RunQuery2(Q_INSERT, "insert ignore into spawn_location_group (group_id, placement_id) values(%u, %u)", group_id, spawn->GetSpawnLocationPlacementID());
  2496. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2497. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupAddSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2498. return false;
  2499. }
  2500. return true;
  2501. }
  2502. bool WorldDatabase::SpawnGroupRemoveSpawn(Spawn* spawn, int32 group_id){
  2503. Query query;
  2504. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_group where group_id = %u and placement_id = %u", group_id, spawn->GetSpawnLocationPlacementID());
  2505. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2506. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupRemoveSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2507. return false;
  2508. }
  2509. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT count(group_id) FROM spawn_location_group where group_id = %u", group_id);
  2510. if(result && mysql_num_rows(result) > 0) {
  2511. MYSQL_ROW row;
  2512. if((row = mysql_fetch_row(result))){
  2513. if(atoul(row[0]) == 0)
  2514. DeleteSpawnGroup(group_id);
  2515. }
  2516. }
  2517. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2518. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupRemoveSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2519. return false;
  2520. }
  2521. return true;
  2522. }
  2523. int32 WorldDatabase::CreateSpawnGroup(Spawn* spawn, string name)
  2524. {
  2525. int32 group_id = 0;
  2526. Query query;
  2527. // JA: As of 0.7.1, DB Milestone 2, Content Team needs to use group_id's from Raw Data, so start any manual group_id's > 100,000
  2528. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_group (group_id, placement_id, name) SELECT IF(ISNULL(MAX(group_id))=1, 100000, MAX(group_id)+1), %u, '%s' FROM spawn_location_group", spawn->GetSpawnLocationPlacementID(), getSafeEscapeString(name.c_str()).c_str());
  2529. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2530. return 0;
  2531. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(group_id) FROM spawn_location_group");
  2532. if(result && mysql_num_rows(result) > 0)
  2533. {
  2534. MYSQL_ROW row;
  2535. if((row = mysql_fetch_row(result)))
  2536. {
  2537. if(row[0])
  2538. group_id = atoul(row[0]);
  2539. }
  2540. }
  2541. return group_id;
  2542. }
  2543. void WorldDatabase::DeleteSpawnGroup(int32 id){
  2544. Query query;
  2545. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_group where group_id = %u", id);
  2546. }
  2547. bool WorldDatabase::SetGroupSpawnChance(int32 id, float chance){
  2548. Query query;
  2549. query.RunQuery2(Q_UPDATE, "replace into spawn_location_group_chances (group_id, percentage) values(%u, %f)", id, chance);
  2550. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2551. LogWrite(WORLD__ERROR, 0, "World", "Error in SetGroupSpawnChance query '%s': %s", query.GetQuery(), query.GetError());
  2552. return false;
  2553. }
  2554. return true;
  2555. }
  2556. int32 WorldDatabase::LoadSpawnGroupChances(ZoneServer* zone){
  2557. Query query;
  2558. int32 count = 0;
  2559. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT slgc.group_id, slgc.percentage FROM spawn_location_group_chances slgc, spawn_location_group slg, spawn_location_placement slp where slgc.group_id = slg.group_id and slg.placement_id = slp.id and slp.zone_id = %u", zone->GetZoneID());
  2560. if(result && mysql_num_rows(result) > 0) {
  2561. MYSQL_ROW row;
  2562. int32 group_id = 0;
  2563. float percent = 0;
  2564. while(result && (row = mysql_fetch_row(result))){
  2565. group_id = atoul(row[0]);
  2566. percent = atof(row[1]);
  2567. zone->AddSpawnGroupChance(group_id, percent);
  2568. count++;
  2569. }
  2570. }
  2571. return count;
  2572. }
  2573. int32 WorldDatabase::LoadSpawnLocationGroupAssociations(ZoneServer* zone){
  2574. Query query;
  2575. int32 count = 0;
  2576. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT distinct slga.group_id1, slga.group_id2 FROM spawn_location_group_associations slga, spawn_location_group slg, spawn_location_placement slp where (slg.group_id = slga.group_id1 or slg.group_id = slga.group_id2) and slg.placement_id = slp.id and slp.zone_id = %u", zone->GetZoneID());
  2577. if(result && mysql_num_rows(result) > 0) {
  2578. MYSQL_ROW row;
  2579. while(result && (row = mysql_fetch_row(result))){
  2580. zone->AddSpawnGroupAssociation(atoul(row[0]), atoul(row[1]));
  2581. count++;
  2582. }
  2583. }
  2584. return count;
  2585. }
  2586. int32 WorldDatabase::LoadSpawnLocationGroups(ZoneServer* zone){
  2587. Query query;
  2588. int32 count = 0;
  2589. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT slg.group_id, slg.placement_id, slp.spawn_location_id FROM spawn_location_group slg, spawn_location_placement slp WHERE slg.placement_id = slp.id and slp.zone_id = %u", zone->GetZoneID());
  2590. if(result && mysql_num_rows(result) > 0) {
  2591. MYSQL_ROW row;
  2592. int32 placement_id = 0;
  2593. int32 group_id = 0;
  2594. int32 spawn_location_id = 0;
  2595. while(result && (row = mysql_fetch_row(result))){
  2596. group_id = atoul(row[0]);
  2597. placement_id = atoul(row[1]);
  2598. spawn_location_id = atoul(row[2]);
  2599. zone->AddSpawnGroupLocation(group_id, placement_id, spawn_location_id);
  2600. count++;
  2601. }
  2602. }
  2603. return count;
  2604. }
  2605. int32 WorldDatabase::ProcessSpawnLocations(ZoneServer* zone, const char* sql_query, int8 type){
  2606. int32 number = 0;
  2607. Query query;
  2608. MYSQL_RES* result = query.RunQuery2(Q_SELECT, sql_query, zone->GetZoneID());
  2609. if(result && mysql_num_rows(result) > 0) {
  2610. MYSQL_ROW row;
  2611. int32 spawn_location_id = 0xFFFFFFFF;
  2612. SpawnLocation* spawn_location = 0;
  2613. while(result && (row = mysql_fetch_row(result))){
  2614. if((spawn_location_id == 0xFFFFFFFF) || atoul(row[0]) != spawn_location_id){
  2615. if(spawn_location){
  2616. zone->AddSpawnLocation(spawn_location_id, spawn_location);
  2617. number++;
  2618. }
  2619. spawn_location = new SpawnLocation();
  2620. }
  2621. SpawnEntry* entry = new SpawnEntry;
  2622. spawn_location_id = atoul(row[0]);
  2623. entry->spawn_location_id = spawn_location_id;
  2624. entry->spawn_entry_id = atoul(row[1]);
  2625. entry->spawn_type = type;
  2626. entry->spawn_id = atoul(row[9]);
  2627. entry->spawn_percentage = atof(row[10]);
  2628. entry->respawn = atoul(row[11]);
  2629. entry->expire_time = atoul(row[14]);
  2630. entry->expire_offset = atoul(row[15]);
  2631. spawn_location->x = atof(row[2]);
  2632. spawn_location->y = atof(row[3]);
  2633. spawn_location->z = atof(row[4]);
  2634. spawn_location->x_offset = atof(row[5]);
  2635. spawn_location->y_offset = atof(row[6]);
  2636. spawn_location->z_offset = atof(row[7]);
  2637. spawn_location->heading = atof(row[8]);
  2638. spawn_location->pitch = atof(row[16]);
  2639. spawn_location->roll = atof(row[17]);
  2640. spawn_location->conditional = atoi(row[18]);
  2641. spawn_location->total_percentage += entry->spawn_percentage;
  2642. spawn_location->grid_id = strtoul(row[12], NULL, 0);
  2643. spawn_location->placement_id = strtoul(row[13], NULL, 0);
  2644. spawn_location->AddSpawn(entry);
  2645. }
  2646. if(spawn_location){
  2647. zone->AddSpawnLocation(spawn_location_id, spawn_location);
  2648. number++;
  2649. }
  2650. }
  2651. return number;
  2652. }
  2653. void WorldDatabase::ResetDatabase(){
  2654. Query query;
  2655. query.RunQuery2("delete FROM table_versions where name != 'table_versions'", Q_DELETE);
  2656. }
  2657. void WorldDatabase::EnableConstraints(){
  2658. Query query;
  2659. query.RunQuery2("/*!40101 SET SQL_MODE=@OLD_SQL_MODE */", Q_DBMS);
  2660. query.RunQuery2("/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */", Q_DBMS);
  2661. }
  2662. void WorldDatabase::DisableConstraints(){
  2663. Query query;
  2664. query.RunQuery2("/*!40101 SET NAMES utf8 */", Q_DBMS);
  2665. query.RunQuery2("/*!40101 SET SQL_MODE=''*/", Q_DBMS);
  2666. query.RunQuery2("/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */", Q_DBMS);
  2667. query.RunQuery2("/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */", Q_DBMS);
  2668. }
  2669. void WorldDatabase::LoadSpawns(ZoneServer* zone)
  2670. {
  2671. Query query;
  2672. int32 npcs = 0, objects = 0, widgets = 0, signs = 0, ground_spawns = 0, spawn_groups = 0, spawn_group_associations = 0, spawn_group_chances = 0;
  2673. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter LoadSpawns");
  2674. npcs = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_npcs sn where sn.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_NPC);
  2675. objects = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_objects so where so.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_OBJECT);
  2676. widgets = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_widgets sw where sw.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_WIDGET);
  2677. signs = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_signs ss where ss.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_SIGN);
  2678. ground_spawns = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_ground sg where sg.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_GROUNDSPAWN);
  2679. spawn_groups = LoadSpawnLocationGroups(zone);
  2680. spawn_group_associations = LoadSpawnLocationGroupAssociations(zone);
  2681. spawn_group_chances = LoadSpawnGroupChances(zone);
  2682. LogWrite(SPAWN__INFO, 0, "Spawn", "Loaded for zone '%s' (%u):\n\t%u NPC(s), %u Object(s), %u Widget(s)\n\t%u Sign(s), %u Ground Spawn(s), %u Spawn Group(s)\n\t%u Spawn Group Association(s), %u Spawn Group Chance(s)", zone->GetZoneName(), zone->GetZoneID(), npcs, objects, widgets, signs, ground_spawns, spawn_groups, spawn_group_associations, spawn_group_chances);
  2683. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit LoadSpawns");
  2684. }
  2685. bool WorldDatabase::UpdateSpawnLocationSpawns(Spawn* spawn) {
  2686. Query query;
  2687. query.RunQuery2(Q_UPDATE, "update spawn_location_placement set x=%f, y=%f, z=%f, heading=%f, x_offset=%f, y_offset=%f, z_offset=%f, respawn=%u, expire_timer=%u, expire_offset=%u, grid_id=%u, pitch=%f, roll=%f where id = %u",
  2688. spawn->GetX(), spawn->GetY(), spawn->GetZ(), spawn->GetHeading(), spawn->GetXOffset(), spawn->GetYOffset(), spawn->GetZOffset(), spawn->GetRespawnTime(), spawn->GetExpireTime(), spawn->GetExpireOffsetTime(), spawn->appearance.pos.grid_id, spawn->GetPitch(), spawn->GetRoll(), spawn->GetSpawnLocationPlacementID());
  2689. if (query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF) {
  2690. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateSpawnLocationSpawns query '%s': %s", query.GetQuery(), query.GetError());
  2691. return false;
  2692. }
  2693. return true;
  2694. }
  2695. bool WorldDatabase::UpdateSpawnWidget(int32 widget_id, char* queryString) {
  2696. Query query;
  2697. query.RunQuery2(Q_UPDATE, "update spawn_widgets set %s where widget_id = %u",
  2698. queryString, widget_id);
  2699. if (query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF) {
  2700. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateSpawnWidget query '%s': %s", query.GetQuery(), query.GetError());
  2701. return false;
  2702. }
  2703. return true;
  2704. }
  2705. vector<string>* WorldDatabase::GetSpawnNameList(const char* in_name){
  2706. Query query;
  2707. string names = "";
  2708. vector<string>* ret = 0;
  2709. string name = getSafeEscapeString(in_name);
  2710. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT concat(spawn.id, ', ', name) FROM spawn where name like '%%%s%%'", name.c_str());
  2711. if(result && mysql_num_rows(result) > 0){
  2712. ret = new vector<string>;
  2713. MYSQL_ROW row;
  2714. int8 num = 0;
  2715. while(result && (row = mysql_fetch_row(result))){
  2716. if(num >= 10)
  2717. break;
  2718. ret->push_back(string(row[0]));
  2719. num++;
  2720. }
  2721. char total[60] = {0};
  2722. if(mysql_num_rows(result) > 10)
  2723. sprintf(total, "Total number of results: %u (Limited to 10)", (int32)mysql_num_rows(result));
  2724. else
  2725. sprintf(total, "Total number of results: %u", (int32)mysql_num_rows(result));
  2726. ret->push_back(string(total));
  2727. }
  2728. return ret;
  2729. }
  2730. string WorldDatabase::GetZoneName(char* zone_description){
  2731. string ret;
  2732. Query query;
  2733. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM zones where description = '%s'", getSafeEscapeString(zone_description).c_str());
  2734. if(result && mysql_num_rows(result) > 0){
  2735. MYSQL_ROW row = mysql_fetch_row(result);
  2736. ret = string(row[0]);
  2737. }
  2738. return ret;
  2739. }
  2740. void WorldDatabase::LoadRevivePoints(vector<RevivePoint*>* revive_points, int32 zone_id){
  2741. if(revive_points && revive_points->size() > 0){
  2742. LogWrite(WORLD__ERROR, 0, "World", "Revive points have already been loaded for this zone!");
  2743. return;
  2744. }
  2745. else if(!revive_points || zone_id == 0){
  2746. LogWrite(WORLD__ERROR, 0, "World", "LoadRevivePoints called with null variables!");
  2747. return;
  2748. }
  2749. Query query;
  2750. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT respawn_zone_id, location_name, safe_x, safe_y, safe_z, heading FROM revive_points where zone_id=%u ORDER BY id asc", zone_id);
  2751. if(revive_points && result && mysql_num_rows(result) > 0){
  2752. MYSQL_ROW row;
  2753. int32 id = 0;
  2754. RevivePoint* point = 0;
  2755. while(result && (row=mysql_fetch_row(result))){
  2756. point = new RevivePoint;
  2757. point->id = id;
  2758. point->zone_id = atoul(row[0]);
  2759. point->location_name = string(row[1]);
  2760. point->x = atof(row[2]);
  2761. point->y = atof(row[3]);
  2762. point->z = atof(row[4]);
  2763. point->heading = atof(row[5]);
  2764. revive_points->push_back(point);
  2765. id++;
  2766. }
  2767. }
  2768. }
  2769. int32 WorldDatabase::GetNextSpawnIDInZone(int32 zone_id)
  2770. {
  2771. Query query;
  2772. int32 ret = 0;
  2773. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT MAX(id) FROM spawn where id LIKE '%i____'", zone_id);
  2774. if(result && mysql_num_rows(result) > 0) {
  2775. MYSQL_ROW row;
  2776. row = mysql_fetch_row(result);
  2777. if(row[0])
  2778. ret = atoi(row[0]) + 1;
  2779. }
  2780. if( ret == 0 )
  2781. ret = zone_id * 10000; // there are no spawns for that zone yet, to start with the first ID
  2782. LogWrite(WORLD__DEBUG, 0, "World", "Next Spawn ID for Zone %i: %u", zone_id, ret);
  2783. return ret;
  2784. }
  2785. bool WorldDatabase::SaveSpawnInfo(Spawn* spawn){
  2786. Query query;
  2787. string name = getSafeEscapeString(spawn->GetName());
  2788. string suffix = getSafeEscapeString(spawn->GetSuffixTitle());
  2789. string prefix = getSafeEscapeString(spawn->GetPrefixTitle());
  2790. string last_name = getSafeEscapeString(spawn->GetLastName());
  2791. if(spawn->GetDatabaseID() == 0){
  2792. int8 isInstanceType = (spawn->GetZone()->GetInstanceType() == Instance_Type::PERSONAL_HOUSE_INSTANCE);
  2793. int32 new_spawn_id = GetNextSpawnIDInZone(spawn->GetZone()->GetZoneID());
  2794. query.RunQuery2(Q_INSERT, "insert into spawn (id, name, race, model_type, size, targetable, show_name, command_primary, command_secondary, visual_state, attackable, show_level, show_command_icon, display_hand_icon, faction_id, collision_radius, hp, power, prefix, suffix, last_name, is_instanced_spawn, merchant_min_level, merchant_max_level) values(%u, '%s', %i, %i, %i, %i, %i, %u, %u, %i, %i, %i, %i, %i, %u, %i, %u, %u, '%s', '%s', '%s', %u, %u, %u)",
  2795. new_spawn_id, name.c_str(), spawn->GetRace(), spawn->GetModelType(), spawn->GetSize(), spawn->appearance.targetable, spawn->appearance.display_name, spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, 0, spawn->appearance.pos.collision_radius, spawn->GetTotalHP(), spawn->GetTotalPower(), prefix.c_str(), suffix.c_str(), last_name.c_str(), isInstanceType, spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel());
  2796. if( new_spawn_id > 0 )
  2797. spawn->SetDatabaseID(new_spawn_id); // use the new zone_id range
  2798. else if( query.GetLastInsertedID() > 0 )
  2799. spawn->SetDatabaseID(query.GetLastInsertedID()); // else fall back to last_inserted_id
  2800. else
  2801. return false; // else, hang your head in shame as you are an utter failure
  2802. if(spawn->IsNPC()){
  2803. query.RunQuery2(Q_INSERT, "insert into spawn_npcs (spawn_id, min_level, max_level, enc_level, class_, gender, min_group_size, max_group_size, hair_type_id, facial_hair_type_id, wing_type_id, chest_type_id, legs_type_id, soga_hair_type_id, soga_facial_hair_type_id, soga_model_type, heroic_flag, action_state, mood_state, initial_state, activity_status, hide_hood, emote_state) values(%u, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i)",
  2804. spawn->GetDatabaseID(), spawn->GetLevel(), spawn->GetLevel(), spawn->appearance.encounter_level, spawn->GetAdventureClass(), spawn->GetGender(), 0, 0, ((NPC*)spawn)->features.hair_type, ((NPC*)spawn)->features.hair_face_type,
  2805. ((NPC*)spawn)->features.wing_type, ((NPC*)spawn)->features.chest_type, ((NPC*)spawn)->features.legs_type, ((NPC*)spawn)->features.soga_hair_type, ((NPC*)spawn)->features.soga_hair_face_type, spawn->appearance.soga_model_type, spawn->appearance.heroic_flag, spawn->GetActionState(), spawn->GetMoodState(), spawn->GetInitialState(), spawn->GetActivityStatus(), spawn->appearance.hide_hood, spawn->appearance.emote_state);
  2806. }
  2807. else if(spawn->IsObject()){
  2808. query.RunQuery2(Q_INSERT, "insert into spawn_objects (spawn_id) values(%u)", spawn->GetDatabaseID());
  2809. }
  2810. else if(spawn->IsWidget()){
  2811. Widget* widget = (Widget*)spawn;
  2812. query.RunQuery2(Q_INSERT, "insert into spawn_widgets (spawn_id, widget_id) values(%u, %u)", spawn->GetDatabaseID(), widget->GetWidgetID());
  2813. }
  2814. else if(spawn->IsSign()){
  2815. query.RunQuery2(Q_INSERT, "insert into spawn_signs (spawn_id, description) values(%u, 'change me')", spawn->GetDatabaseID());
  2816. }
  2817. else if (spawn->IsGroundSpawn()) {
  2818. query.RunQuery2(Q_INSERT, "insert into spawn_ground (spawn_id) values(%u)", spawn->GetDatabaseID());
  2819. }
  2820. }
  2821. else{
  2822. if(spawn->IsNPC()){
  2823. query.RunQuery2(Q_UPDATE, "update spawn_npcs, spawn set name='%s', min_level=%i, max_level=%i, enc_level=%i, race=%i, model_type=%i, class_=%i, gender=%i, show_name=%i, attackable=%i, show_level=%i, targetable=%i, show_command_icon=%i, display_hand_icon=%i, hair_type_id=%i, facial_hair_type_id=%i, wing_type_id=%i, chest_type_id=%i, legs_type_id=%i, soga_hair_type_id=%i, soga_facial_hair_type_id=%i, soga_model_type=%i, size=%i, hp=%u, heroic_flag=%i, power=%u, collision_radius=%i, command_primary=%u, command_secondary=%u, visual_state=%i, action_state=%i, mood_state=%i, initial_state=%i, activity_status=%i, alignment=%i, faction_id=%u, hide_hood=%i, emote_state=%i, suffix ='%s', prefix='%s', last_name='%s', merchant_min_level = %u, merchant_max_level = %u where spawn_npcs.spawn_id = spawn.id and spawn.id = %u",
  2824. name.c_str(), spawn->GetLevel(), spawn->GetLevel(), spawn->appearance.encounter_level, spawn->GetRace(), spawn->GetModelType(),
  2825. spawn->GetAdventureClass(), spawn->GetGender(), spawn->appearance.display_name, spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.targetable, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, ((NPC*)spawn)->features.hair_type,
  2826. ((NPC*)spawn)->features.hair_face_type, ((NPC*)spawn)->features.wing_type, ((NPC*)spawn)->features.chest_type, ((NPC*)spawn)->features.legs_type, ((NPC*)spawn)->features.soga_hair_type, ((NPC*)spawn)->features.soga_hair_face_type, spawn->appearance.soga_model_type, spawn->GetSize(),
  2827. spawn->GetTotalHP(), spawn->appearance.heroic_flag, spawn->GetTotalPower(), spawn->GetCollisionRadius(), spawn->GetPrimaryCommandListID(),
  2828. spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->GetActionState(), spawn->GetMoodState(), spawn->GetInitialState(),
  2829. spawn->GetActivityStatus(), ((NPC*)spawn)->GetAlignment(), spawn->GetFactionID(), spawn->appearance.hide_hood, spawn->appearance.emote_state,
  2830. suffix.c_str(), prefix.c_str(), last_name.c_str(), spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), spawn->GetDatabaseID());
  2831. }
  2832. else if(spawn->IsObject()){
  2833. query.RunQuery2(Q_UPDATE, "update spawn_objects, spawn set name='%s', model_type=%i, show_name=%i, targetable=%i, size=%i, command_primary=%u, command_secondary=%u, visual_state=%i, attackable=%i, show_level=%i, show_command_icon=%i, display_hand_icon=%i, collision_radius=%i, hp = %u, power = %u, device_id = %i, merchant_min_level = %u, merchant_max_level = %u where spawn_objects.spawn_id = spawn.id and spawn.id = %u",
  2834. name.c_str(), spawn->GetModelType(), spawn->appearance.display_name, spawn->appearance.targetable, spawn->GetSize(), spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon,
  2835. spawn->GetCollisionRadius(), spawn->GetTotalHP(), spawn->GetTotalPower(), ((Object*)spawn)->GetDeviceID(), spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), spawn->GetDatabaseID());
  2836. }
  2837. else if(spawn->IsWidget()){
  2838. Widget* widget = (Widget*)spawn;
  2839. char* openSound = 0;
  2840. char* closeSound = 0;
  2841. if (widget->GetOpenSound() != NULL) openSound = (char*)widget->GetOpenSound(); else openSound = (char*)string("0").c_str();
  2842. if (widget->GetCloseSound() != NULL) closeSound = (char*)widget->GetCloseSound(); else closeSound = (char*)string("0").c_str();
  2843. query.RunQuery2(Q_UPDATE, "update spawn_widgets, spawn set name='%s', race=%i, model_type=%i, show_name=%i, attackable=%i, show_level=%i, show_command_icon=%i, display_hand_icon=%i, size=%i, hp=%u, power=%u, collision_radius=%i, command_primary=%u, command_secondary=%u, visual_state=%i, faction_id=%u, suffix ='%s', prefix='%s', last_name='%s',widget_id = %u,widget_x = %f,widget_y = %f,widget_z = %f,include_heading = %u,include_location = %u,icon = %u,type='%s',open_heading = %f,closed_heading = %f,open_x = %f,open_y = %f,open_z = %f,action_spawn_id = %u,open_sound_file='%s',close_sound_file='%s',open_duration = %u,close_x = %f,close_y=%f,close_z=%f,linked_spawn_id = %u,house_id = %u, merchant_min_level = %u, merchant_max_level = %u where spawn_widgets.spawn_id = spawn.id and spawn.id = %u",
  2844. name.c_str(), spawn->GetRace(), spawn->GetModelType(), spawn->appearance.display_name, spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, spawn->GetSize(),
  2845. spawn->GetTotalHP(), spawn->GetTotalPower(), spawn->GetCollisionRadius(), spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->GetFactionID(),
  2846. suffix.c_str(), prefix.c_str(), last_name.c_str(), widget->GetWidgetID(), widget->GetX(), widget->GetY(), widget->GetZ(), widget->GetIncludeHeading(), widget->GetIncludeLocation(), widget->GetIconValue(), Widget::GetWidgetTypeNameByTypeID(widget->GetWidgetType()).c_str(),
  2847. widget->GetOpenHeading(), widget->GetClosedHeading(), widget->GetOpenX(), widget->GetOpenY(), widget->GetOpenZ(),
  2848. widget->GetActionSpawnID(), openSound, closeSound,widget->GetOpenDuration(),
  2849. widget->GetCloseX(),widget->GetCloseY(),widget->GetCloseZ(),widget->GetLinkedSpawnID(),widget->GetHouseID(),
  2850. spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), spawn->GetDatabaseID());
  2851. }
  2852. else if(spawn->IsSign()){
  2853. Sign* sign = (Sign*)spawn;
  2854. query.RunQuery2(Q_UPDATE, "update spawn_signs, spawn set name='%s', race=%i, model_type=%i, show_name=%i, attackable=%i, show_level=%i, show_command_icon=%i, display_hand_icon=%i, size=%i, hp=%u, power=%u, collision_radius=%i, command_primary=%u, command_secondary=%u, visual_state=%i, faction_id=%u, suffix ='%s', prefix='%s', last_name='%s', type='%s', zone_id = %u, widget_id = %u, title='%s', widget_x = %f, widget_y = %f, widget_z = %f, icon = %u, description='%s', sign_distance = %f, zone_x = %f, zone_y = %f, zone_z = %f, zone_heading = %f, include_heading = %u, include_location = %u, merchant_min_level = %u, merchant_max_level = %u where spawn_signs.spawn_id = spawn.id and spawn.id = %u",
  2855. name.c_str(), spawn->GetRace(), spawn->GetModelType(), spawn->appearance.display_name, spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, spawn->GetSize(),
  2856. spawn->GetTotalHP(), spawn->GetTotalPower(), spawn->GetCollisionRadius(), spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->GetFactionID(),
  2857. suffix.c_str(), prefix.c_str(), last_name.c_str(), sign->GetSignType(), sign->GetSignZoneID(),
  2858. sign->GetWidgetID(), sign->GetSignTitle(), sign->GetWidgetX(), sign->GetWidgetY(), sign->GetWidgetZ(),
  2859. sign->GetIconValue(), sign->GetSignDescription(), sign->GetSignDistance(), sign->GetSignZoneX(),
  2860. sign->GetSignZoneY(), sign->GetSignZoneZ(), sign->GetSignZoneHeading(), sign->GetIncludeHeading(),
  2861. sign->GetIncludeLocation(), spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), spawn->GetDatabaseID());
  2862. }
  2863. }
  2864. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2865. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in SaveSpawnInfo query '%s': %s", query.GetQuery(), query.GetError());
  2866. return false;
  2867. }
  2868. return true;
  2869. }
  2870. int32 WorldDatabase::SaveCombinedSpawnLocation(ZoneServer* zone, Spawn* in_spawn, const char* name){
  2871. vector<Spawn*>* spawns = in_spawn->GetSpawnGroup();
  2872. uint32 spawnLocationID = 0;
  2873. if(spawns && spawns->size() > 0){
  2874. vector<Spawn*>::iterator itr;
  2875. map<Spawn*, int32>::iterator freq_itr;
  2876. Spawn* spawn = 0;
  2877. map<Spawn*, int32> database_spawns;
  2878. int32 total = 0;
  2879. float x_offset = GetSpawnLocationPlacementOffsetX(in_spawn->GetSpawnLocationID());
  2880. float y_offset = GetSpawnLocationPlacementOffsetY(in_spawn->GetSpawnLocationID());
  2881. float z_offset = GetSpawnLocationPlacementOffsetZ(in_spawn->GetSpawnLocationID());
  2882. int32 spawn_location_id = GetNextSpawnLocation();
  2883. spawnLocationID = spawn_location_id;
  2884. if(!name)
  2885. name = "Combine SpawnGroup Generated";
  2886. if(!CreateNewSpawnLocation(spawn_location_id, name)){
  2887. safe_delete(spawns);
  2888. return 0;
  2889. }
  2890. for(itr = spawns->begin();itr!=spawns->end();itr++){
  2891. spawn = *itr;
  2892. if (spawn) {
  2893. RemoveSpawnFromSpawnLocation(spawn);
  2894. spawn->SetSpawnLocationID(spawn_location_id);
  2895. bool add = true;
  2896. for (freq_itr = database_spawns.begin(); freq_itr != database_spawns.end(); freq_itr++) {
  2897. if (spawn->GetDatabaseID() == freq_itr->first->GetDatabaseID()) {
  2898. freq_itr->second++;
  2899. total++;
  2900. add = false;
  2901. }
  2902. }
  2903. if (add) {
  2904. database_spawns[spawn] = 1;
  2905. total++;
  2906. }
  2907. }
  2908. }
  2909. for(freq_itr = database_spawns.begin(); freq_itr != database_spawns.end(); freq_itr++){
  2910. int8 percent = (freq_itr->second*100)/total;
  2911. if(!SaveSpawnEntry(freq_itr->first, name, percent, x_offset, y_offset, z_offset, freq_itr->first == in_spawn, false)){
  2912. safe_delete(spawns);
  2913. return 0;
  2914. }
  2915. }
  2916. for(itr=spawns->begin();itr!=spawns->end();itr++){
  2917. spawn = *itr;
  2918. zone->RemoveSpawn(false, spawn);
  2919. }
  2920. safe_delete(spawns);
  2921. }
  2922. else{
  2923. safe_delete(spawns);
  2924. return 0;
  2925. }
  2926. return spawnLocationID;
  2927. }
  2928. bool WorldDatabase::SaveSpawnEntry(Spawn* spawn, const char* spawn_location_name, int8 percent, float x_offset, float y_offset, float z_offset, bool save_zonespawn, bool create_spawnlocation){
  2929. Query query;
  2930. Query query2;
  2931. int32 count = 0;
  2932. if(create_spawnlocation){
  2933. count = GetSpawnLocationCount(spawn->GetSpawnLocationID());
  2934. if(count == 0){
  2935. if(!CreateNewSpawnLocation(spawn->GetSpawnLocationID(), spawn_location_name))
  2936. return false;
  2937. }
  2938. }
  2939. query.RunQuery2(Q_INSERT, "insert into spawn_location_entry (spawn_id, spawn_location_id, spawnpercentage) values(%u, %u, %i)", spawn->GetDatabaseID(), spawn->GetSpawnLocationID(), percent);
  2940. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2941. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in SaveSpawnEntry query '%s': %s", query.GetQuery(), query.GetError());
  2942. return false;
  2943. }
  2944. if(save_zonespawn){
  2945. query2.RunQuery2(Q_INSERT, "insert into spawn_location_placement (zone_id, instance_id, spawn_location_id, x, y, z, x_offset, y_offset, z_offset, heading, grid_id) values(%u, %u, %u, %f, %f, %f, %f, %f, %f, %f, %u)", spawn->GetZone()->GetZoneID(), spawn->GetZone()->GetInstanceID(), spawn->GetSpawnLocationID(), spawn->GetX(), spawn->GetY(), spawn->GetZ(),x_offset, y_offset, z_offset, spawn->GetHeading(), spawn->appearance.pos.grid_id);
  2946. if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF){
  2947. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in SaveSpawnEntry query '%s': %s", query2.GetQuery(), query2.GetError());
  2948. return false;
  2949. }
  2950. }
  2951. return true;
  2952. }
  2953. float WorldDatabase::GetSpawnLocationPlacementOffsetX(int32 location_id) {
  2954. Query query;
  2955. MYSQL_ROW row;
  2956. float ret = 0;
  2957. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `x_offset` FROM `spawn_location_placement` WHERE `spawn_location_id`=%u", location_id);
  2958. if (result && (row = mysql_fetch_row(result))) {
  2959. if (row[0])
  2960. ret = atof(row[0]);
  2961. }
  2962. return ret;
  2963. }
  2964. float WorldDatabase::GetSpawnLocationPlacementOffsetY(int32 location_id) {
  2965. Query query;
  2966. MYSQL_ROW row;
  2967. float ret = 0;
  2968. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `y_offset` FROM `spawn_location_placement` WHERE `spawn_location_id`=%u", location_id);
  2969. if (result && (row = mysql_fetch_row(result))) {
  2970. if (row[0])
  2971. ret = atof(row[0]);
  2972. }
  2973. return ret;
  2974. }
  2975. float WorldDatabase::GetSpawnLocationPlacementOffsetZ(int32 location_id) {
  2976. Query query;
  2977. MYSQL_ROW row;
  2978. float ret = 0;
  2979. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `z_offset` FROM `spawn_location_placement` WHERE `spawn_location_id`=%u", location_id);
  2980. if (result && (row = mysql_fetch_row(result))) {
  2981. if (row[0])
  2982. ret = atof(row[0]);
  2983. }
  2984. return ret;
  2985. }
  2986. bool WorldDatabase::CreateNewSpawnLocation(int32 id, const char* name){
  2987. Query query;
  2988. if(!name)
  2989. name = "Unknown Spawn Location Name";
  2990. string str_name = getSafeEscapeString(name);
  2991. query.RunQuery2(Q_INSERT, "insert into spawn_location_name (id, name) values(%u, '%s')", id, str_name.c_str());
  2992. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2993. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in CreateNewSpawnLocation query '%s': %s", query.GetQuery(), query.GetError());
  2994. return false;
  2995. }
  2996. return true;
  2997. }
  2998. int32 WorldDatabase::GetSpawnLocationCount(int32 location, Spawn* spawn){
  2999. Query query;
  3000. int32 ret = 0;
  3001. MYSQL_RES* result = 0;
  3002. if(spawn)
  3003. result = query.RunQuery2(Q_SELECT, "SELECT count(id) FROM spawn_location_entry where spawn_location_id=%u and spawn_id=%u", location, spawn->GetDatabaseID());
  3004. if(result && mysql_num_rows(result) > 0){
  3005. MYSQL_ROW row;
  3006. while(result && (row = mysql_fetch_row(result)) && row[0]){
  3007. ret = strtoul(row[0], NULL, 0);
  3008. }
  3009. }
  3010. return ret;
  3011. }
  3012. int32 WorldDatabase::GetNextSpawnLocation(){
  3013. Query query;
  3014. int32 ret = 0;
  3015. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(id) FROM spawn_location_name");
  3016. if(result && mysql_num_rows(result) > 0){
  3017. MYSQL_ROW row;
  3018. while(result && (row = mysql_fetch_row(result)) && row[0]){
  3019. ret = strtoul(row[0], NULL, 0);
  3020. }
  3021. }
  3022. ret++;
  3023. return ret;
  3024. }
  3025. bool WorldDatabase::RemoveSpawnFromSpawnLocation(Spawn* spawn){
  3026. Query query;
  3027. Query query2;
  3028. int32 count = GetSpawnLocationCount(spawn->GetSpawnLocationID(), spawn);
  3029. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_placement where spawn_location_id=%u", spawn->GetSpawnLocationID());
  3030. if(count == 1)
  3031. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_name where id=%u", spawn->GetSpawnLocationID());
  3032. query2.RunQuery2(Q_DELETE, "delete FROM spawn_location_entry where spawn_id=%u and spawn_location_id = %u", spawn->GetDatabaseID(), spawn->GetSpawnLocationID());
  3033. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  3034. LogWrite(WORLD__ERROR, 0, "World", "Error in RemoveSpawnFromSpawnLocation query '%s': %s", query.GetQuery(), query.GetError());
  3035. return false;
  3036. }
  3037. else if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF){
  3038. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in RemoveSpawnFromSpawnLocation query '%s': %s", query2.GetQuery(), query.GetError());
  3039. return false;
  3040. }
  3041. return true;
  3042. }
  3043. map<int32, string>* WorldDatabase::GetZoneList(const char* name, bool is_admin)
  3044. {
  3045. Query query;
  3046. map<int32, string>* ret = 0;
  3047. string zone_name = getSafeEscapeString(name);
  3048. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `name` FROM zones WHERE `name` RLIKE '%s' %s", zone_name.c_str(), (is_admin)?"":" LIMIT 0,10");
  3049. if(result && mysql_num_rows(result) > 0)
  3050. {
  3051. ret = new map<int32, string>;
  3052. MYSQL_ROW row;
  3053. while(result && (row = mysql_fetch_row(result)))
  3054. {
  3055. zone_name = row[1];
  3056. (*ret)[atoi(row[0])] = zone_name;
  3057. }
  3058. }
  3059. return ret;
  3060. }
  3061. void WorldDatabase::UpdateStartingFactions(int32 char_id, int8 choice){
  3062. Query query;
  3063. query.RunQuery2(Q_INSERT, "insert into character_factions (char_id, faction_id, faction_level) select %u, faction_id, value FROM starting_factions where starting_city=%i", char_id, choice);
  3064. }
  3065. string WorldDatabase::GetStartingZoneName(int8 choice){
  3066. Query query;
  3067. string zone_name = "";
  3068. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM zones where start_zone = %u", choice);
  3069. if(result && mysql_num_rows(result) > 0){
  3070. MYSQL_ROW row;
  3071. while(result && (row = mysql_fetch_row(result))){
  3072. zone_name = string(row[0]);
  3073. }
  3074. }
  3075. return zone_name;
  3076. }
  3077. void WorldDatabase::UpdateStartingZone(int32 char_id, int8 class_id, int8 race_id, int8 choice)
  3078. {
  3079. Query query;
  3080. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default zone for race: %i, class: %i for char_id: %u (choice: %i)", race_id, class_id, char_id, choice);
  3081. // first, check to see if there is a starting_zones record for this race/class/choice combo (now using extended Archetype/BaseClass/Class combos
  3082. MYSQL_RES* result = 0;
  3083. if ( class_id == 0 )
  3084. result = query.RunQuery2(Q_SELECT, "SELECT `name` FROM starting_zones sz, zones z WHERE sz.zone_id = z.id AND class_id = 255 AND race_id IN (%i, 255) AND choice = %u",
  3085. race_id, choice);
  3086. else
  3087. result = query.RunQuery2(Q_SELECT, "SELECT `name` FROM starting_zones sz, zones z WHERE sz.zone_id = z.id AND class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255) AND choice IN (%i, 255)",
  3088. classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id, choice);
  3089. // TODO: verify client version so clients do not crash trying to enter zones they do not own (paks)
  3090. if(result && mysql_num_rows(result) > 0)
  3091. {
  3092. string zone_name = "ERROR";
  3093. MYSQL_ROW row;
  3094. if( result && (row = mysql_fetch_row(result)) )
  3095. zone_name = string(row[0]);
  3096. if (class_id == 0)
  3097. query.RunQuery2(Q_UPDATE, "UPDATE characters c, zones z, starting_zones sz SET c.current_zone_id = z.id, c.x = z.safe_x, c.y = z.safe_y, c.z = z.safe_z, c.starting_city = z.id WHERE z.id = sz.zone_id AND sz.class_id = 255 AND sz.race_id IN (%i, 255) AND sz.choice = %u AND c.id = %u",
  3098. race_id, choice, char_id);
  3099. else
  3100. query.RunQuery2(Q_UPDATE, "UPDATE characters c, zones z, starting_zones sz SET c.current_zone_id = z.id, c.x = z.safe_x, c.y = z.safe_y, c.z = z.safe_z, c.starting_city = %i WHERE z.id = sz.zone_id AND sz.class_id IN (%i, %i, %i, 255) AND sz.race_id IN (%i, 255) AND sz.choice IN (%i, 255) AND c.id = %u",
  3101. choice, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id, choice, char_id);
  3102. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  3103. LogWrite(PLAYER__ERROR, 0, "Player", "Error in UpdateStartingZone custom starting_zones, query: '%s': %s", query.GetQuery(), query.GetError());
  3104. return;
  3105. }
  3106. if(query.GetAffectedRows() > 0)
  3107. {
  3108. LogWrite(PLAYER__DEBUG, 0, "Player", "Setting New Character Starting Zone to '%s' FROM starting_zones table.", zone_name.c_str());
  3109. return;
  3110. }
  3111. }
  3112. else
  3113. {
  3114. // there was no matching starting_zone value, so use default 'choice' starting city
  3115. query.RunQuery2(Q_UPDATE, "UPDATE characters c, zones z SET c.current_zone_id = z.id, c.x = z.safe_x, c.y = z.safe_y, c.z = z.safe_z, c.starting_city = %i WHERE z.start_zone = %i and c.id = %u",
  3116. choice, choice, char_id);
  3117. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3118. {
  3119. LogWrite(PLAYER__ERROR, 0, "Player", "Error in UpdateStartingZone player choice, query: '%s': %s", query.GetQuery(), query.GetError());
  3120. return;
  3121. }
  3122. if(query.GetAffectedRows() > 0)
  3123. {
  3124. LogWrite(PLAYER__DEBUG, 0, "Player", "Setting New Character Starting Zone to '%s' FROM player choice.", GetStartingZoneName(choice).c_str());
  3125. return;
  3126. }
  3127. }
  3128. // if we are here, it's a bad thing. zone tables have no start_city values to match client 'choice', so throw the player into zone according to R_World::DefaultStartingZoneID rule.
  3129. // shout a few warnings so the admin fixes this asap!
  3130. int16 default_zone_id = rule_manager.GetGlobalRule(R_World, DefaultStartingZoneID)->GetInt16();
  3131. LogWrite(WORLD__WARNING, 0, "World", "No Starting City defined for player choice: %i! BAD! BAD! BAD! Defaulting player to zone %i.", choice, default_zone_id);
  3132. query.RunQuery2(Q_UPDATE, "UPDATE characters c, zones z SET c.current_zone_id = z.id, c.x = z.safe_x, c.y = z.safe_y, c.z = z.safe_z, c.heading = z.safe_heading, c.starting_city = 1 WHERE z.id = %i and c.id = %u", default_zone_id, char_id);
  3133. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3134. {
  3135. LogWrite(PLAYER__ERROR, 0, "Player", "Error in UpdateStartingZone default zone %i, query: '%s': %s", default_zone_id, query.GetQuery(), query.GetError());
  3136. return;
  3137. }
  3138. if(query.GetAffectedRows() > 0)
  3139. LogWrite(PLAYER__DEBUG, 0, "Player", "Setting New Character Starting Zone to '%s' due to no valid options!", GetZoneName(1)->c_str());
  3140. return;
  3141. }
  3142. void WorldDatabase::UpdateStartingItems(int32 char_id, int8 class_id, int8 race_id, bool base_class){
  3143. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default items for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3144. Query query;
  3145. Query query2;
  3146. vector<Item*> items;
  3147. vector<Item*> bags;
  3148. map<int32, int8> total_slots;
  3149. map<int32, int8> slots_left;
  3150. map<int8, bool> equip_slots;
  3151. map<Item*, StartingItem> item_list;
  3152. int32 item_id = 0;
  3153. Item* item = 0;
  3154. StartingItem* starting_item = 0;
  3155. //first get a list of the starting items for the character
  3156. MYSQL_RES* result = 0;
  3157. /*if(!base_class)
  3158. result = query2.RunQuery2(Q_SELECT, "SELECT `type`, item_id, creator, condition_, attuned, count FROM starting_items where (class_id=%i and race_id=%i) or (class_id=%i and race_id=255) or (class_id=255 and race_id=%i) or (class_id=255 and race_id=255) ORDER BY id", class_id, race_id, class_id, race_id);
  3159. else
  3160. result = query2.RunQuery2(Q_SELECT, "SELECT `type`, item_id, creator, condition_, attuned, count FROM starting_items where (class_id=%i and race_id=%i) or (class_id=%i and race_id=255) ORDER BY id", class_id, race_id, class_id);*/
  3161. result = query2.RunQuery2(Q_SELECT, "SELECT `type`, item_id, creator, condition_, attuned, count FROM starting_items WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255) ORDER BY id", classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3162. if(result && mysql_num_rows(result) > 0){
  3163. MYSQL_ROW row;
  3164. while(result && (row = mysql_fetch_row(result))){
  3165. item_id = atoul(row[1]);
  3166. item = master_item_list.GetItem(item_id);
  3167. if(item){
  3168. starting_item = &(item_list[item]);
  3169. starting_item->type = (row[0]) ? string(row[0]) : "";
  3170. starting_item->item_id = atoul(row[1]);
  3171. starting_item->creator = (row[2]) ? string(row[2]) : "";
  3172. starting_item->condition = atoi(row[3]);
  3173. starting_item->attuned = atoi(row[4]);
  3174. starting_item->count = atoi(row[5]);
  3175. item = master_item_list.GetItem(starting_item->item_id);
  3176. if(item){
  3177. if(bags.size() < NUM_INV_SLOTS && item->IsBag() && item->details.num_slots > 0)
  3178. bags.push_back(item);
  3179. else
  3180. items.push_back(item);
  3181. }
  3182. }
  3183. }
  3184. }
  3185. slots_left[0] = NUM_INV_SLOTS;
  3186. //next create the bags in the inventory
  3187. for(int8 i=0;i<bags.size();i++){
  3188. item = bags[i];
  3189. query.RunQuery2(Q_INSERT, "insert into character_items (char_id, type, slot, item_id, creator, condition_, attuned, bag_id, count) values(%u, '%s', %i, %u, '%s', %i, %i, %u, %i)",
  3190. char_id, item_list[item].type.c_str(), i, item_list[item].item_id, getSafeEscapeString(item_list[item].creator.c_str()).c_str(), item_list[item].condition, item_list[item].attuned, 0, item_list[item].count);
  3191. slots_left[query.GetLastInsertedID()] = item->details.num_slots;
  3192. total_slots[query.GetLastInsertedID()] = item->details.num_slots;
  3193. slots_left[0]--;
  3194. }
  3195. map<int32, int8>::iterator itr;
  3196. int32 inv_slot = 0;
  3197. int8 slot = 0;
  3198. //finally process the rest of the items, placing them in the first available slot
  3199. for(int32 x=0;x<items.size();x++){
  3200. item = items[x];
  3201. if(item_list[item].type.find("NOT") < 0xFFFFFFFF){ // NOT-EQUIPPED Items
  3202. for(itr = slots_left.begin(); itr != slots_left.end(); itr++){
  3203. if(itr->second > 0){
  3204. if(itr->first == 0 && slots_left.size() > 1) //we want items to go into bags first, then inventory after bags are full
  3205. continue;
  3206. inv_slot = itr->first;
  3207. slot = total_slots[itr->first] - itr->second;
  3208. itr->second--;
  3209. if(itr->second == 0)
  3210. slots_left.erase(itr);
  3211. break;
  3212. }
  3213. }
  3214. query.RunQuery2(Q_INSERT, "insert into character_items (char_id, type, slot, item_id, creator, condition_, attuned, bag_id, count) values(%u, '%s', %i, %u, '%s', %i, %i, %u, %i)",
  3215. char_id, item_list[item].type.c_str(), slot, item_list[item].item_id, getSafeEscapeString(item_list[item].creator.c_str()).c_str(), item_list[item].condition, item_list[item].attuned, inv_slot, item_list[item].count);
  3216. }
  3217. else{ //EQUIPPED Items
  3218. for(int8 i=0;i<item->slot_data.size();i++){
  3219. if(equip_slots.count(item->slot_data[i]) == 0){
  3220. equip_slots[item->slot_data[i]] = true;
  3221. query.RunQuery2(Q_INSERT, "insert into character_items (char_id, type, slot, item_id, creator, condition_, attuned, bag_id, count) values(%u, '%s', %i, %u, '%s', %i, %i, %u, %i)",
  3222. char_id, item_list[item].type.c_str(), item->slot_data[i], item_list[item].item_id, getSafeEscapeString(item_list[item].creator.c_str()).c_str(), item_list[item].condition, item_list[item].attuned, 0, item_list[item].count);
  3223. break;
  3224. }
  3225. }
  3226. }
  3227. }
  3228. }
  3229. void WorldDatabase::UpdateStartingSkills(int32 char_id, int8 class_id, int8 race_id)
  3230. {
  3231. Query query;
  3232. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default skills for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3233. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_skills (char_id, skill_id, current_val, max_val) SELECT %u, skill_id, current_val, max_val FROM starting_skills WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255)",
  3234. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3235. }
  3236. void WorldDatabase::UpdateStartingSpells(int32 char_id, int8 class_id, int8 race_id){
  3237. Query query;
  3238. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default spells for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3239. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_spells (char_id, spell_id, tier, knowledge_slot) SELECT %u, spell_id, tier, knowledge_slot FROM starting_spells WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255)",
  3240. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3241. }
  3242. void WorldDatabase::UpdateStartingSkillbar(int32 char_id, int8 class_id, int8 race_id){
  3243. Query query;
  3244. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default skillbar for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3245. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_skillbar (char_id, type, hotbar, spell_id, slot, text_val) SELECT %u, type, hotbar, spell_id, slot, text_val FROM starting_skillbar WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255)",
  3246. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3247. }
  3248. void WorldDatabase::UpdateStartingTitles(int32 char_id, int8 class_id, int8 race_id, int8 gender_id) {
  3249. Query query;
  3250. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default titles for race: %i, class: %i, gender: %i for char_id: %u", race_id, class_id, gender_id, char_id);
  3251. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_titles (char_id, title_id) SELECT %u, title_id FROM starting_titles WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255) and gender_id IN (%i, 255)",
  3252. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id, gender_id);
  3253. }
  3254. string WorldDatabase::GetZoneDescription(int32 id){
  3255. Query query;
  3256. string ret = "";
  3257. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT description FROM zones where id = %u", id);
  3258. if(result && mysql_num_rows(result) > 0){
  3259. MYSQL_ROW row;
  3260. row = mysql_fetch_row(result);
  3261. ret = string(row[0]);
  3262. }
  3263. return ret;
  3264. }
  3265. string* WorldDatabase::GetZoneName(int32 id){
  3266. string* ret = new string;
  3267. if (zone_names.count(id) > 0){
  3268. ret->assign(zone_names[id]);
  3269. return ret;
  3270. }
  3271. Query query;
  3272. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `name` FROM zones where `id` = %u", id);
  3273. if(result && mysql_num_rows(result) > 0){
  3274. MYSQL_ROW row;
  3275. row = mysql_fetch_row(result);
  3276. zone_names[id] = row[0];
  3277. ret->assign(zone_names[id]);
  3278. }
  3279. return ret;
  3280. }
  3281. bool WorldDatabase::VerifyZone(const char* name){
  3282. Query query;
  3283. char* escaped = getEscapeString(name);
  3284. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM zones where name='%s'",escaped);
  3285. safe_delete_array(escaped);
  3286. if(result && mysql_num_rows(result) > 0)
  3287. return true;
  3288. else
  3289. return false;
  3290. }
  3291. int8 WorldDatabase::GetInstanceTypeByZoneID(int32 zoneID)
  3292. {
  3293. DatabaseResult result;
  3294. int8 ret = 0;
  3295. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Getting instances for zone_id %u", zoneID);
  3296. if( !database_new.Select(&result, "SELECT instance_type+0 FROM zones WHERE id = %u", zoneID) )
  3297. {
  3298. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in GetInstanceTypeByZoneID() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  3299. return ret;
  3300. }
  3301. if( result.GetNumRows() > 0 )
  3302. {
  3303. result.Next();
  3304. ret = (result.GetInt8Str("instance_type+0") == 0) ? 0 : result.GetInt8Str("instance_type+0") - 1;
  3305. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Found instance type %i for zone_id %u", ret, zoneID);
  3306. }
  3307. else
  3308. LogWrite(INSTANCE__DEBUG, 0, "Instance", "No instances found for zone_id %u", zoneID);
  3309. return ret;
  3310. }
  3311. void WorldDatabase::Save(Client* client){
  3312. Query query;
  3313. Player* player = client->GetPlayer();
  3314. if(!player->CheckPlayerInfo())
  3315. return;
  3316. int32 instance_id = 0;
  3317. if ( client->GetCurrentZone ( ) != NULL )
  3318. instance_id = client->GetCurrentZone()->GetInstanceID();
  3319. int32 zone_id = 0;
  3320. if(client->GetCurrentZone())
  3321. zone_id = client->GetCurrentZone()->GetZoneID();
  3322. query.AddQueryAsync(client->GetCharacterID(), this, Q_UPDATE, "update characters set current_zone_id=%u, x=%f, y=%f, z=%f, heading=%f, level=%i,instance_id=%i,last_saved=%i, `class`=%i, `tradeskill_level`=%i, `tradeskill_class`=%i where id = %u", zone_id, player->GetX(), player->GetY(), player->GetZ(), player->GetHeading(), player->GetLevel(), instance_id, client->GetLastSavedTimeStamp(), client->GetPlayer()->GetAdventureClass(), client->GetPlayer()->GetTSLevel(), client->GetPlayer()->GetTradeskillClass(), client->GetCharacterID());
  3323. query.AddQueryAsync(client->GetCharacterID(), this, Q_UPDATE, "update character_details set hp=%u, power=%u, str=%i, sta=%i, agi=%i, wis=%i, intel=%i, heat=%i, cold=%i, magic=%i, mental=%i, divine=%i, disease=%i, poison=%i, coin_copper=%u, coin_silver=%u, coin_gold=%u, coin_plat=%u, max_hp = %u, max_power=%u, xp = %u, xp_needed = %u, xp_debt = %u, xp_vitality = %f, tradeskill_xp = %u, tradeskill_xp_needed = %u, tradeskill_xp_vitality = %f, bank_copper = %u, bank_silver = %u, bank_gold = %u, bank_plat = %u, bind_zone_id=%u, bind_x = %f, bind_y = %f, bind_z = %f, bind_heading = %f, house_zone_id=%u, combat_voice = %i, emote_voice = %i, biography='%s', flags=%u, flags2=%u, last_name='%s' where char_id = %u",
  3324. player->GetHP(), player->GetPower(), player->GetStrBase(), player->GetStaBase(), player->GetAgiBase(), player->GetWisBase(), player->GetIntBase(), player->GetHeatResistanceBase(), player->GetColdResistanceBase(), player->GetMagicResistanceBase(),
  3325. player->GetMentalResistanceBase(), player->GetDivineResistanceBase(), player->GetDiseaseResistanceBase(), player->GetPoisonResistanceBase(), player->GetCoinsCopper(), player->GetCoinsSilver(), player->GetCoinsGold(), player->GetCoinsPlat(), player->GetTotalHPBase(), player->GetTotalPowerBase(), player->GetXP(), player->GetNeededXP(), player->GetXPDebt(), player->GetXPVitality(), player->GetTSXP(), player->GetNeededTSXP(), player->GetTSXPVitality(), player->GetBankCoinsCopper(),
  3326. player->GetBankCoinsSilver(), player->GetBankCoinsGold(), player->GetBankCoinsPlat(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneID(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneX(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneY(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneZ(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneHeading(), client->GetPlayer()->GetPlayerInfo()->GetHouseZoneID(),
  3327. client->GetPlayer()->GetCombatVoice(), client->GetPlayer()->GetEmoteVoice(), getSafeEscapeString(client->GetPlayer()->GetBiography().c_str()).c_str(), player->GetFlags(), player->GetFlags2(), client->GetPlayer()->GetLastName(), client->GetCharacterID());
  3328. map<string, int8>::iterator itr;
  3329. map<string, int8>* friends = player->GetFriends();
  3330. if(friends && friends->size() > 0){
  3331. for(itr = friends->begin(); itr != friends->end(); itr++){
  3332. if(itr->second == 1){
  3333. query.AddQueryAsync(client->GetCharacterID(), this, Q_INSERT, "insert ignore into character_social (char_id, name, type) values(%u, '%s', 'FRIEND')", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3334. itr->second = 0;
  3335. }
  3336. else if(itr->second == 2){
  3337. query.AddQueryAsync(client->GetCharacterID(), this, Q_DELETE, "delete FROM character_social where char_id = %u and name = '%s'", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3338. itr->second = 3;
  3339. }
  3340. }
  3341. }
  3342. map<string, int8>* ignored = player->GetIgnoredPlayers();
  3343. if(ignored && ignored->size() > 0){
  3344. for(itr = ignored->begin(); itr != ignored->end(); itr++){
  3345. if(itr->second == 1){
  3346. query.AddQueryAsync(client->GetCharacterID(), this, Q_INSERT, "insert ignore into character_social (char_id, name, type) values(%u, '%s', 'IGNORE')", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3347. itr->second = 0;
  3348. }
  3349. else if(itr->second == 2){
  3350. query.AddQueryAsync(client->GetCharacterID(), this, Q_DELETE, "delete FROM character_social where char_id = %u and name = '%s'", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3351. itr->second = 3;
  3352. }
  3353. }
  3354. }
  3355. SavePlayerFactions(client);
  3356. SaveCharacterQuests(client);
  3357. SaveCharacterSkills(client);
  3358. SavePlayerSpells(client);
  3359. SavePlayerMail(client);
  3360. SavePlayerCollections(client);
  3361. LogWrite(PLAYER__INFO, 3, "Player", "Player '%s' (%u) data saved.", player->GetName(), player->GetCharacterID());
  3362. }
  3363. void WorldDatabase::LoadEntityCommands(ZoneServer* zone) {
  3364. int32 total = 0;
  3365. int32 id = 0;
  3366. EntityCommand* command = 0;
  3367. DatabaseResult result;
  3368. if (!database_new.Select(&result, "SELECT `command_list_id`, `command_text`, `distance`, `command`, `error_text`, `cast_time`, `spell_visual` FROM `entity_commands` ORDER BY `id`")) {
  3369. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  3370. return;
  3371. }
  3372. while (result.Next()) {
  3373. command = new EntityCommand;
  3374. id = result.GetInt32(0);
  3375. command->name = result.GetString(1);
  3376. command->distance = result.GetFloat(2);
  3377. command->command = result.GetString(3);
  3378. command->error_text = result.GetString(4);
  3379. command->cast_time = result.GetInt16(5);
  3380. command->spell_visual = result.GetInt32(6);
  3381. command->default_allow_list = true;
  3382. zone->SetEntityCommandList(id, command);
  3383. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Command: '%s' (%s)", command->name.c_str(), command->command.c_str());
  3384. total++;
  3385. }
  3386. LogWrite(COMMAND__DEBUG, 0, "Command", "--Loaded %i entity command(s)", total);
  3387. }
  3388. void WorldDatabase::LoadFactionAlliances()
  3389. {
  3390. LogWrite(FACTION__DEBUG, 1, "Faction", "-Loading faction alliances...");
  3391. int32 total = 0;
  3392. int32 fTotal = 0;
  3393. int32 hTotal = 0;
  3394. Query query;
  3395. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT faction_id, friend_faction, hostile_faction FROM faction_alliances");
  3396. if(result && mysql_num_rows(result) > 0)
  3397. {
  3398. MYSQL_ROW row;
  3399. int32 faction_id = 0;
  3400. int32 friendly_id = 0;
  3401. int32 hostile_id = 0;
  3402. while(result && (row = mysql_fetch_row(result)))
  3403. {
  3404. faction_id = atoul(row[0]);
  3405. friendly_id = atoul(row[1]);
  3406. hostile_id = atoul(row[2]);
  3407. if(friendly_id > 0)
  3408. {
  3409. master_faction_list.AddFriendlyFaction(faction_id, friendly_id);
  3410. fTotal++;
  3411. LogWrite(FACTION__DEBUG, 5, "Faction", "---Faction %i is friendly towards %i", faction_id, friendly_id);
  3412. }
  3413. if(hostile_id > 0)
  3414. {
  3415. master_faction_list.AddHostileFaction(faction_id, hostile_id);
  3416. hTotal++;
  3417. LogWrite(FACTION__DEBUG, 5, "Faction", "---Faction %i is hostile towards %i", faction_id, hostile_id);
  3418. }
  3419. total++;
  3420. }
  3421. }
  3422. LogWrite(FACTION__DEBUG, 3, "Faction", "--Loaded %u Alliances: %i friendly, %i hostile", total, fTotal, hTotal);
  3423. }
  3424. bool WorldDatabase::UpdateSpawnScriptData(int32 spawn_id, int32 spawn_location_id, int32 spawnentry_id, const char* name){
  3425. bool ret = false;
  3426. if((spawn_id > 0 || spawn_location_id > 0 || spawnentry_id > 0) && name){
  3427. Query query;
  3428. int32 row_id = 0;
  3429. if(spawn_id > 0){
  3430. query.RunQuery2(Q_DELETE, "DELETE FROM spawn_scripts where spawn_id=%u", spawn_id);
  3431. query.RunQuery2(Q_INSERT, "INSERT into spawn_scripts (spawn_id, lua_script) values(%u, '%s')", spawn_id, getSafeEscapeString(name).c_str());
  3432. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3433. LogWrite(LUA__ERROR, 0, "LUA", "Error in UpdateSpawnScriptData, Query: %s, Error: %s", query.GetQuery(), query.GetError());
  3434. else{
  3435. row_id = query.GetLastInsertedID();
  3436. if(row_id > 0)
  3437. world.AddSpawnScript(row_id, name);
  3438. ret = true;
  3439. }
  3440. }
  3441. else if(spawn_location_id > 0){
  3442. query.RunQuery2(Q_DELETE, "DELETE FROM spawn_scripts where spawn_location_id=%u", spawn_location_id);
  3443. query.RunQuery2(Q_INSERT, "INSERT into spawn_scripts (spawn_location_id, lua_script) values(%u, '%s')", spawn_location_id, getSafeEscapeString(name).c_str());
  3444. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3445. LogWrite(LUA__ERROR, 0, "LUA", "Error in UpdateSpawnScriptData, Query: %s, Error: %s", query.GetQuery(), query.GetError());
  3446. else{
  3447. row_id = query.GetLastInsertedID();
  3448. if(row_id > 0)
  3449. world.AddSpawnLocationScript(row_id, name);
  3450. ret = true;
  3451. }
  3452. }
  3453. else if(spawnentry_id > 0){
  3454. query.RunQuery2(Q_DELETE, "DELETE FROM spawn_scripts where spawnentry_id=%u", spawnentry_id);
  3455. query.RunQuery2(Q_INSERT, "INSERT into spawn_scripts (spawnentry_id, lua_script) values(%u, '%s')", spawnentry_id, getSafeEscapeString(name).c_str());
  3456. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3457. LogWrite(LUA__ERROR, 0, "LUA", "Error in UpdateSpawnScriptData, Query: %s, Error: %s", query.GetQuery(), query.GetError());
  3458. else{
  3459. row_id = query.GetLastInsertedID();
  3460. if(row_id > 0)
  3461. world.AddSpawnEntryScript(row_id, name);
  3462. ret = true;
  3463. }
  3464. }
  3465. }
  3466. return ret;
  3467. }
  3468. void WorldDatabase::LoadSpawnScriptData() {
  3469. int32 total = 0;
  3470. Query query;
  3471. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spawn_id, spawnentry_id, spawn_location_id, lua_script FROM spawn_scripts");
  3472. if(result && mysql_num_rows(result) > 0)
  3473. {
  3474. MYSQL_ROW row;
  3475. int32 spawn_id = 0;
  3476. int32 spawnentry_id = 0;
  3477. int32 spawn_location_id = 0;
  3478. while(result && (row = mysql_fetch_row(result)))
  3479. {
  3480. spawn_id = atoul(row[0]);
  3481. spawnentry_id = atoul(row[1]);
  3482. spawn_location_id = atoul(row[2]);
  3483. string spawn_script = string(row[3]);
  3484. if(spawnentry_id > 0)
  3485. {
  3486. world.AddSpawnEntryScript(spawnentry_id, row[3]);
  3487. total++;
  3488. }
  3489. else if(spawn_location_id > 0)
  3490. {
  3491. world.AddSpawnLocationScript(spawn_location_id, row[3]);
  3492. total++;
  3493. }
  3494. else if(spawn_id > 0)
  3495. {
  3496. world.AddSpawnScript(spawn_id, row[3]);
  3497. total++;
  3498. }
  3499. else {
  3500. if(row[3])
  3501. LogWrite(LUA__ERROR, 0, "LUA", "Invalid Entry in spawn_scripts table for lua_script '%s' (spawn_id, spawnentry_id and spawn_location_id are all 0)", row[3]);
  3502. else
  3503. LogWrite(LUA__ERROR, 0, "LUA", "Invalid Entry in spawn_scripts table.");
  3504. }
  3505. if( spawn_id || spawnentry_id || spawn_location_id ) {
  3506. LogWrite(LUA__DEBUG, 5, "LUA", "SpawnScript %s loaded.", spawn_script.c_str());
  3507. }
  3508. spawn_id = 0;
  3509. spawnentry_id = 0;
  3510. spawn_location_id = 0;
  3511. }
  3512. }
  3513. LogWrite(LUA__DEBUG, 0, "LUA", "\tLoaded %u SpawnScript%s", total, total == 1 ? "" : "s");
  3514. }
  3515. void WorldDatabase::LoadZoneScriptData() {
  3516. Query query;
  3517. int32 total = 0;
  3518. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `lua_script` FROM `zones`");
  3519. if (result && mysql_num_rows(result) > 0) {
  3520. MYSQL_ROW row;
  3521. while(result && (row = mysql_fetch_row(result))) {
  3522. if (row[1]) {
  3523. int32 zone_id = atoul(row[0]);
  3524. string zone_script = string(row[1]);
  3525. if (zone_id > 0 && zone_script.length() > 0) {
  3526. LogWrite(LUA__DEBUG, 5, "LUA", "ZoneScript: %s loaded.", zone_script.c_str());
  3527. world.AddZoneScript(zone_id, zone_script.c_str());
  3528. total++;
  3529. }
  3530. }
  3531. }
  3532. }
  3533. LogWrite(LUA__DEBUG, 0, "LUA", "\tLoaded %u ZoneScript%s", total, total == 1 ? "" : "s");
  3534. }
  3535. int32 WorldDatabase::LoadSpellScriptData() {
  3536. Query query;
  3537. MYSQL_ROW row;
  3538. int32 count;
  3539. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `lua_script` FROM `spells`"); // WHERE is_active = 1
  3540. while (result && (row = mysql_fetch_row(result))) {
  3541. if (row[0] && strlen(row[0]) > 0) {
  3542. if (lua_interface->LoadLuaSpell(row[0]))
  3543. LogWrite(SPELL__DEBUG, 5, "Spells", "SpellScript: %s loaded.", row[0]);
  3544. }
  3545. }
  3546. count = mysql_num_rows(result);
  3547. LogWrite(SPELL__DEBUG, 0, "Spells", "\tLoaded %i SpellScript%s", count, count == 1 ? "" : "s");
  3548. return count;
  3549. }
  3550. void WorldDatabase::LoadFactionList() {
  3551. int32 total = 0;
  3552. Query query;
  3553. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name, type, description, default_level, negative_change, positive_change FROM factions");
  3554. if(result && mysql_num_rows(result) > 0) {
  3555. MYSQL_ROW row;
  3556. Faction* faction = 0;
  3557. while(result && (row = mysql_fetch_row(result))){
  3558. faction = new Faction;
  3559. faction->id = atoul(row[0]);
  3560. faction->name = string(row[1]);
  3561. faction->type = string(row[2]);
  3562. faction->description = string(row[3]);
  3563. faction->default_value = atoi(row[4]);
  3564. faction->negative_change = atoi(row[5]);
  3565. faction->positive_change = atoi(row[6]);
  3566. master_faction_list.AddFaction(faction);
  3567. total++;
  3568. LogWrite(FACTION__DEBUG, 5, "Faction", "---Loading Faction '%s' (%u)", faction->name.c_str(), faction->id);
  3569. }
  3570. }
  3571. LogWrite(FACTION__DEBUG, 3, "Faction", "--Loaded %u Faction%s", total, total == 1 ? "" : "s");
  3572. LoadFactionAlliances();
  3573. }
  3574. void WorldDatabase::SavePlayerFactions(Client* client){
  3575. LogWrite(PLAYER__DEBUG, 3, "Player", "Saving Player Factions...");
  3576. Query query;
  3577. map<int32, sint32>* factions = client->GetPlayer()->GetFactions()->GetFactionValues();
  3578. map<int32, sint32>::iterator itr;
  3579. for(itr = factions->begin(); itr != factions->end(); itr++)
  3580. query.AddQueryAsync(client->GetCharacterID(), this,Q_INSERT, "insert into character_factions (char_id, faction_id, faction_level) values(%u, %u, %i) ON DUPLICATE KEY UPDATE faction_level=%i", client->GetCharacterID(), itr->first, itr->second, itr->second);
  3581. }
  3582. bool WorldDatabase::LoadPlayerFactions(Client* client) {
  3583. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading Player Factions...");
  3584. Query query;
  3585. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT faction_id, faction_level FROM character_factions where char_id=%i", client->GetCharacterID());
  3586. if(result && mysql_num_rows(result) > 0) {
  3587. MYSQL_ROW row;
  3588. while(result && (row = mysql_fetch_row(result))){
  3589. client->GetPlayer()->GetFactions()->SetFactionValue(atoul(row[0]), atol(row[1]));
  3590. }
  3591. }
  3592. if(query.GetErrorNumber())
  3593. return false;
  3594. return true;
  3595. }
  3596. void WorldDatabase::SavePlayerMail(Mail* mail) {
  3597. Query query_update;
  3598. Query query_insert;
  3599. if (mail) {
  3600. query_update.AddQueryAsync(mail->player_to_id, this, Q_UPDATE, "UPDATE `character_mail` SET `already_read`=%u, `coin_copper`=%u, `coin_silver`=%u, `coin_gold`=%u, `coin_plat`=%u WHERE `id`=%u", mail->already_read, mail->coin_copper, mail->coin_silver, mail->coin_gold, mail->coin_plat, mail->mail_id);
  3601. if (query_update.GetAffectedRows() == 0)
  3602. query_insert.AddQueryAsync(mail->player_to_id, this, Q_UPDATE, "INSERT INTO `character_mail` (`player_to_id`, `player_from`, `subject`, `mail_body`, `already_read`, `mail_type`, `coin_copper`, `coin_silver`, `coin_gold`, `coin_plat`, `stack`, `postage_cost`, `attachment_cost`, `char_item_id`, `time_sent`, `expire_time`) VALUES (%u, '%s', '%s', '%s', %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u)", mail->player_to_id, mail->player_from.c_str(), getSafeEscapeString(mail->subject.c_str()).c_str(), getSafeEscapeString(mail->mail_body.c_str()).c_str(), mail->already_read, mail->mail_type, mail->coin_copper, mail->coin_silver, mail->coin_gold, mail->coin_plat, mail->stack, mail->postage_cost, mail->attachment_cost, mail->char_item_id, mail->time_sent, mail->expire_time);
  3603. mail->save_needed = false;
  3604. }
  3605. }
  3606. void WorldDatabase::SavePlayerMail(Client* client) {
  3607. if (client) {
  3608. MutexMap<int32, Mail*>* mail_list = client->GetPlayer()->GetMail();
  3609. MutexMap<int32, Mail*>::iterator itr = mail_list->begin();
  3610. while (itr.Next()) {
  3611. Mail* mail = itr->second;
  3612. if (mail->save_needed)
  3613. SavePlayerMail(mail);
  3614. }
  3615. }
  3616. }
  3617. void WorldDatabase::LoadPlayerMail(Client* client, bool new_only) {
  3618. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading Player Mail...");
  3619. if (client) {
  3620. Query query;
  3621. MYSQL_RES* result;
  3622. if (new_only)
  3623. result = query.RunQuery2(Q_SELECT, "SELECT `id`, `player_to_id`, `player_from`, `subject`, `mail_body`, `already_read`, `mail_type`, `coin_copper`, `coin_silver`, `coin_gold`, `coin_plat`, `stack`, `postage_cost`, `attachment_cost`, `char_item_id`, `time_sent`, `expire_time` FROM `character_mail` WHERE `player_to_id`=%u AND `unread`=1", client->GetCharacterID());
  3624. else
  3625. result = query.RunQuery2(Q_SELECT, "SELECT `id`, `player_to_id`, `player_from`, `subject`, `mail_body`, `already_read`, `mail_type`, `coin_copper`, `coin_silver`, `coin_gold`, `coin_plat`, `stack`, `postage_cost`, `attachment_cost`, `char_item_id`, `time_sent`, `expire_time` FROM `character_mail` WHERE `player_to_id`=%u", client->GetCharacterID());
  3626. if (result && mysql_num_rows(result) > 0) {
  3627. MYSQL_ROW row;
  3628. client->SimpleMessage(CHANNEL_COLOR_MAIL, "You've got mail! :)");
  3629. while (result && (row = mysql_fetch_row(result))) {
  3630. Mail* mail = new Mail;
  3631. mail->mail_id = atoul(row[0]);
  3632. mail->player_to_id = atoul(row[1]);
  3633. mail->player_from = string(row[2]);
  3634. mail->subject = string(row[3]);
  3635. if (row[4])
  3636. mail->mail_body = string(row[4]);
  3637. mail->already_read = atoi(row[5]);
  3638. mail->mail_type = atoi(row[6]);
  3639. mail->coin_copper = atoul(row[7]);
  3640. mail->coin_silver = atoul(row[8]);
  3641. mail->coin_gold = atoul(row[9]);
  3642. mail->coin_plat = atoul(row[10]);
  3643. mail->stack = atoi(row[11]);
  3644. mail->postage_cost = atoul(row[12]);
  3645. mail->attachment_cost = atoul(row[13]);
  3646. mail->char_item_id = atoul(row[14]);
  3647. mail->time_sent = atoul(row[15]);
  3648. mail->expire_time = atoul(row[16]);
  3649. mail->save_needed = false;
  3650. client->GetPlayer()->AddMail(mail);
  3651. LogWrite(PLAYER__DEBUG, 5, "Player", "Loaded Mail ID %i, to: %i, from: %s", atoul(row[0]), atoul(row[1]), string(row[2]).c_str());
  3652. }
  3653. }
  3654. }
  3655. }
  3656. void WorldDatabase::DeletePlayerMail(Mail* mail) {
  3657. Query query;
  3658. if (mail)
  3659. query.RunQuery2(Q_DELETE, "DELETE FROM `character_mail` WHERE `id`=%u", mail->mail_id);
  3660. LogWrite(PLAYER__DEBUG, 0, "Player", "Delete Player Mail...");
  3661. }
  3662. vector<int32>* WorldDatabase::GetAllPlayerIDs() {
  3663. Query query;
  3664. vector<int32>* ids = 0;
  3665. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id` FROM `characters`");
  3666. MYSQL_ROW row;
  3667. while (result && (row = mysql_fetch_row(result))) {
  3668. if (ids == 0)
  3669. ids = new vector<int32>;
  3670. ids->push_back(atoul(row[0]));
  3671. }
  3672. return ids;
  3673. }
  3674. void WorldDatabase::GetPetNames(ZoneServer* zone)
  3675. {
  3676. DatabaseResult result;
  3677. int32 total = 0;
  3678. if( database_new.Select(&result, "SELECT pet_name FROM spawn_pet_names") )
  3679. {
  3680. while(result.Next())
  3681. {
  3682. zone->pet_names.push_back(result.GetStringStr("pet_name"));
  3683. total++;
  3684. LogWrite(PET__DEBUG, 5, "Pet", "---Loading Pet Name: '%s'", result.GetStringStr("pet_name"));
  3685. }
  3686. LogWrite(PET__DEBUG, 0, "Pet", "--Loaded %u Pet Names", total);
  3687. }
  3688. }
  3689. int32 WorldDatabase::CheckTableVersions(char* tablename) {
  3690. Query query;
  3691. char* escaped_name = getEscapeString(tablename);
  3692. LogWrite(INIT__PATCHER_DEBUG, 1, "Patcher", "\tChecking current version for table: %s", tablename);
  3693. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT version FROM table_versions WHERE name='%s';", escaped_name);
  3694. int32 ret_version = 0;
  3695. if(result && mysql_num_rows(result) > 0)
  3696. {
  3697. MYSQL_ROW row;
  3698. row = mysql_fetch_row(result);
  3699. ret_version = atoul(row[0]);
  3700. }
  3701. safe_delete_array(escaped_name);
  3702. return ret_version;
  3703. }
  3704. bool WorldDatabase::RunDatabaseQueries(TableQuery* queries, bool output_result, bool data){
  3705. for(int16 i=0;i<queries->num_queries;i++){
  3706. Query query;
  3707. if(string(queries->GetQuery(i)).length() > 5)
  3708. query.RunQuery2(string(queries->GetQuery(i)), Q_UPDATE);
  3709. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3710. {
  3711. if(output_result)
  3712. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "FAILED!");
  3713. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Error in updating tables query '%s': %s", query.GetQuery(), query.GetError());
  3714. return false;
  3715. }
  3716. }
  3717. if(output_result)
  3718. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "SUCCESS!");
  3719. if(data)
  3720. UpdateDataTableVersion(queries->tablename, queries->latest_version);
  3721. else
  3722. UpdateTableVersion(queries->tablename, queries->latest_version);
  3723. return true;
  3724. }
  3725. void WorldDatabase::UpdateDataTableVersion(char* name, int32 version){
  3726. Query query;
  3727. char* escaped_name = getEscapeString(name);
  3728. query.RunQuery2(Q_UPDATE, "update table_versions set download_version=%u where name='%s'", version, escaped_name);
  3729. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3730. {
  3731. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Error in updating version table query '%s': %s", query.GetQuery(), query.GetError());
  3732. }
  3733. safe_delete_array(escaped_name);
  3734. }
  3735. void WorldDatabase::UpdateTableVersion(char* name, int32 version){
  3736. Query query;
  3737. char* escaped_name = getEscapeString(name);
  3738. query.RunQuery2(Q_UPDATE, "INSERT into table_versions (name, version) values('%s', %u) ON DUPLICATE KEY UPDATE version=%u", escaped_name, version, version);
  3739. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3740. {
  3741. LogWrite(WORLD__ERROR, 0, "Patcher", "Error in updating version table query '%s': %s", query.GetQuery(), query.GetError());
  3742. }
  3743. safe_delete_array(escaped_name);
  3744. }
  3745. bool WorldDatabase::CheckVersionTable() {
  3746. Query query;
  3747. // todo: suppress SQL errors while this command is running, because ERROR is normal on a new DB creation (Zcoretri)
  3748. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SHOW COLUMNS FROM table_versions");
  3749. if(result && mysql_num_rows(result) > 0) {
  3750. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--DB Schema exists! Checking for updates...");
  3751. return true;
  3752. }
  3753. else {
  3754. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Version Table NOT Found! Creating...");
  3755. Query query2;
  3756. query2.RunQuery2(Q_UPDATE, "CREATE TABLE `table_versions` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(64) NOT NULL DEFAULT '',`version` INT(10) UNSIGNED NOT NULL DEFAULT '0',`download_version` INT(10) UNSIGNED NOT NULL DEFAULT '0',PRIMARY KEY (`id`),UNIQUE KEY `UniqueName` (`name`)) ENGINE=INNODB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;");
  3757. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Setting table_version = 1...");
  3758. Query query3;
  3759. query3.RunQuery2(Q_INSERT, "INSERT INTO table_versions (name, version) VALUES ('table_versions', 1)");
  3760. }
  3761. return false;
  3762. }
  3763. sint32 WorldDatabase::GetLatestDataTableVersion(char* name){
  3764. Query query;
  3765. char* escaped_name = getEscapeString(name);
  3766. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT download_version FROM table_versions where name='%s'", escaped_name);
  3767. sint32 ret_version = 0;
  3768. if(result && mysql_num_rows(result) > 0) {
  3769. MYSQL_ROW row;
  3770. row = mysql_fetch_row(result);
  3771. ret_version = atol(row[0]);
  3772. }
  3773. safe_delete_array(escaped_name);
  3774. return ret_version;
  3775. }
  3776. int32 WorldDatabase::GetMaxHotBarID(){
  3777. Query query;
  3778. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(id) FROM character_skillbar");
  3779. int32 ret = 0;
  3780. if(result && mysql_num_rows(result) > 0) {
  3781. MYSQL_ROW row;
  3782. row = mysql_fetch_row(result);
  3783. if(row && row[0])
  3784. ret = strtoul(row[0], NULL, 0);
  3785. }
  3786. return ret;
  3787. }
  3788. void WorldDatabase::SaveQuickBar(int32 char_id, vector<QuickBarItem*>* quickbar_items){
  3789. vector<QuickBarItem*>::iterator itr;
  3790. QuickBarItem* qbi = 0;
  3791. for(itr = quickbar_items->begin(); itr != quickbar_items->end(); itr++){
  3792. qbi = *itr;
  3793. if(!qbi)
  3794. continue;
  3795. if(qbi->deleted == false){
  3796. Query query;
  3797. if(qbi->text.size > 0){
  3798. query.AddQueryAsync(char_id, this, Q_REPLACE, "replace into character_skillbar (id, hotbar, slot, char_id, spell_id, type, text_val, tier) values(%u, %u, %u, %u, %u, %i, '%s', %i)",
  3799. qbi->unique_id, qbi->hotbar, qbi->slot, char_id, qbi->id, qbi->type, getSafeEscapeString(qbi->text.data.c_str()).c_str(), qbi->tier);
  3800. }
  3801. else{
  3802. query.AddQueryAsync(char_id, this, Q_REPLACE, "replace into character_skillbar (id, hotbar, slot, char_id, spell_id, type, text_val, tier) values(%u, %u, %u, %u, %u, %i, 'Unused', %i)",
  3803. qbi->unique_id, qbi->hotbar, qbi->slot, char_id, qbi->id, qbi->type, qbi->tier);
  3804. }
  3805. }
  3806. else{
  3807. Query query;
  3808. query.AddQueryAsync(char_id, this, Q_DELETE, "delete FROM character_skillbar where hotbar=%u and slot=%u and char_id=%u", qbi->hotbar, qbi->slot, char_id);
  3809. }
  3810. }
  3811. }
  3812. map<int32, vector<LevelArray*> >* WorldDatabase::LoadSpellClasses(){
  3813. map<int32, vector<LevelArray*> >* ret = new map<int32, vector<LevelArray*> >();
  3814. Query query;
  3815. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spell_id, adventure_class_id, tradeskill_class_id, level FROM spell_classes");
  3816. MYSQL_ROW row;
  3817. LevelArray* level = 0;
  3818. while(result && (row = mysql_fetch_row(result))){
  3819. level = new LevelArray();
  3820. level->adventure_class = atoi(row[1]);
  3821. level->tradeskill_class = atoi(row[2]);
  3822. level->spell_level = atoi(row[3]);
  3823. (*ret)[atoul(row[0])].push_back(level);
  3824. }
  3825. return ret;
  3826. }
  3827. void WorldDatabase::LoadTraits(){
  3828. Query query;
  3829. MYSQL_ROW row;
  3830. TraitData* trait;
  3831. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `spell_id`, `level`, `class_req`, `race_req`, `isTrait`,`isInate`, `isFocusEffect`, `isTraining`,`tier`, `group` FROM spell_traits");
  3832. while (result && (row = mysql_fetch_row(result))){
  3833. trait = new TraitData;
  3834. int8 i = 0;
  3835. trait->spellID = strtoul(row[0], NULL, 0);
  3836. trait->level = atoi(row[(++i)]);
  3837. trait->classReq = atoi(row[(++i)]);
  3838. trait->raceReq = atoi(row[(++i)]);
  3839. trait->isTrait = (atoi(row[(++i)]) == 0) ? false : true;
  3840. trait->isInate = (atoi(row[(++i)]) == 0) ? false : true;
  3841. trait->isFocusEffect = (atoi(row[(++i)]) == 0) ? false : true;
  3842. trait->isTraining = (atoi(row[(++i)]) == 0) ? false : true;
  3843. trait->tier = atoi(row[(++i)]);
  3844. trait->group = atoi(row[(++i)]);
  3845. master_trait_list.AddTrait(trait);
  3846. }
  3847. LogWrite(SPELL__INFO, 0, "Traits", "Loaded %u Trait(s)", master_trait_list.Size());
  3848. }
  3849. void WorldDatabase::LoadSpells()
  3850. {
  3851. DatabaseResult result;
  3852. Spell *spell;
  3853. SpellData *data;
  3854. int32 t_now = Timer::GetUnixTimeStamp();
  3855. int32 total = 0;
  3856. map<int32, vector<LevelArray*> >* level_data = LoadSpellClasses();
  3857. if( !database_new.Select(&result, "SELECT s.`id`, ts.spell_id, ts.index, `name`, `description`, `type`, `class_skill`, `mastery_skill`, `tier`, `is_aa`,`hp_req`, `power_req`,`power_by_level`, `cast_time`, `recast`, `radius`, `max_aoe_targets`, `req_concentration`, `range`, `duration1`, `duration2`, `resistibility`, `hp_upkeep`, `power_upkeep`, `duration_until_cancel`, `target_type`, `recovery`, `power_req_percent`, `hp_req_percent`, `icon`, `icon_heroic_op`, `icon_backdrop`, `success_message`, `fade_message`, `cast_type`, `lua_script`, `call_frequency`, `interruptable`, `spell_visual`, `effect_message`, `min_range`, `can_effect_raid`, `affect_only_group_members`, `hit_bonus`, `display_spell_tier`, `friendly_spell`, `group_spell`, `spell_book_type`, spell_type+0, s.is_active, savagery_req, savagery_req_percent, savagery_upkeep, dissonance_req, dissonance_req_percent, dissonance_upkeep, linked_timer_id, det_type, incurable, control_effect_type, cast_while_moving, casting_flags, persist_through_death, not_maintained, savage_bar, savage_bar_slot, soe_spell_crc "
  3858. "FROM (spells s, spell_tiers st) "
  3859. "LEFT JOIN spell_ts_ability_index ts "
  3860. "ON s.`id` = ts.spell_id "
  3861. "WHERE s.id = st.spell_id AND s.is_active = 1 "
  3862. "ORDER BY s.`id`, `tier`") )
  3863. {
  3864. // error
  3865. }
  3866. else
  3867. {
  3868. while( result.Next() )
  3869. {
  3870. data = new SpellData;
  3871. int32 spell_id = result.GetInt32Str("id");
  3872. string spell_name = result.GetStringStr("name");
  3873. /* General Spell info */
  3874. data->id = spell_id;
  3875. data->soe_spell_crc = result.GetInt32Str("soe_spell_crc");
  3876. data->tier = result.GetInt8Str("tier");
  3877. data->ts_loc_index = result.GetInt8Str("index");
  3878. data->name.data = spell_name.c_str();
  3879. data->name.size = data->name.data.length();
  3880. data->description.data = result.GetStringStr("description");
  3881. data->description.size = data->description.data.length();
  3882. data->icon = result.GetSInt16Str("icon");
  3883. data->icon_heroic_op = result.GetInt16Str("icon_heroic_op");
  3884. data->icon_backdrop = result.GetInt16Str("icon_backdrop");
  3885. data->spell_visual = result.GetInt32Str("spell_visual");
  3886. data->type = result.GetInt16Str("type");
  3887. data->target_type = result.GetInt8Str("target_type");
  3888. data->cast_type = result.GetInt8Str("cast_type");
  3889. data->spell_book_type = result.GetInt32Str("spell_book_type");
  3890. data->det_type = result.GetInt8Str("det_type");
  3891. data->incurable = (result.GetInt8Str("incurable") == 1);
  3892. data->control_effect_type = result.GetInt8Str("control_effect_type");
  3893. data->casting_flags = result.GetInt32Str("casting_flags");
  3894. data->savage_bar = result.GetInt8Str("savage_bar");
  3895. data->savage_bar_slot = result.GetInt8Str("savage_bar_slot");
  3896. data->spell_type = result.IsNullStr("spell_type+0") ? 0 : result.GetInt8Str("spell_type+0");
  3897. /* Toggles */
  3898. data->interruptable = ( result.GetInt8Str("interruptable") == 1);
  3899. data->duration_until_cancel = ( result.GetInt8Str("duration_until_cancel") == 1);
  3900. data->can_effect_raid = result.GetInt8Str("can_effect_raid");
  3901. data->affect_only_group_members = result.GetInt8Str("affect_only_group_members");
  3902. data->display_spell_tier = result.GetInt8Str("display_spell_tier");
  3903. data->friendly_spell = result.GetInt8Str("friendly_spell");
  3904. data->group_spell = result.GetInt8Str("group_spell");
  3905. data->is_active = result.GetInt8Str("is_active");
  3906. data->persist_though_death = ( result.GetInt8Str("persist_through_death") == 1);
  3907. data->cast_while_moving = ( result.GetInt8Str("cast_while_moving") == 1);
  3908. data->not_maintained = ( result.GetInt8Str("not_maintained") == 1);
  3909. data->is_aa = (result.GetInt8Str("is_aa") == 1);
  3910. /* Skill Requirements */
  3911. data->class_skill = result.GetInt32Str("class_skill");
  3912. data->mastery_skill = result.GetInt32Str("mastery_skill");
  3913. // no min_class_skill_req?
  3914. /* Cost */
  3915. data->req_concentration = result.GetInt16Str("req_concentration");
  3916. data->hp_req = result.GetInt16Str("hp_req");
  3917. data->hp_upkeep = result.GetInt16Str("hp_upkeep");
  3918. data->hp_req_percent = result.GetInt8Str("hp_req_percent");
  3919. data->power_req = result.GetFloatStr("power_req");
  3920. data->power_by_level = ( result.GetInt8Str("power_by_level") == 0)? false : true;
  3921. data->power_upkeep = result.GetInt16Str("power_upkeep");
  3922. data->power_req_percent = result.GetInt8Str("power_req_percent");
  3923. data->savagery_req = result.GetInt16Str("savagery_req");
  3924. data->savagery_upkeep = result.GetInt16Str("savagery_upkeep");
  3925. data->savagery_req_percent = result.GetInt8Str("savagery_req_percent");
  3926. data->dissonance_req = result.GetInt16Str("dissonance_req");
  3927. data->dissonance_upkeep = result.GetInt16Str("dissonance_upkeep");
  3928. data->dissonance_req_percent = result.GetInt8Str("dissonance_req_percent");
  3929. /* Spell Parameters */
  3930. data->call_frequency = result.GetInt32Str("call_frequency");
  3931. data->cast_time = result.GetInt16Str("cast_time");
  3932. data->duration1 = result.GetInt32Str("duration1");
  3933. data->duration2 = result.GetInt32Str("duration2");
  3934. data->hit_bonus = result.GetFloatStr("hit_bonus");
  3935. data->max_aoe_targets = result.GetInt16Str("max_aoe_targets");
  3936. data->min_range = result.GetFloatStr("min_range");
  3937. data->radius = result.GetFloatStr("radius");
  3938. data->range = result.GetFloatStr("range");
  3939. data->recast = result.GetFloatStr("recast");
  3940. data->recovery = result.GetFloatStr("recovery");
  3941. data->resistibility = result.GetFloatStr("resistibility");
  3942. data->linked_timer = result.GetInt32Str("linked_timer_id");
  3943. /* Cast Messaging */
  3944. string message = result.GetStringStr("success_message");
  3945. if( message.length() > 0 )
  3946. data->success_message = message;
  3947. message = result.GetStringStr("fade_message");
  3948. if( message.length() > 0 )
  3949. data->fade_message = string(message);
  3950. message = result.GetStringStr("effect_message");
  3951. if( message.length() > 0 )
  3952. data->effect_message = string(message);
  3953. string lua_script = result.GetStringStr("lua_script");
  3954. if( lua_script.length() > 0 )
  3955. data->lua_script = string(lua_script);
  3956. /* Load spell level data */
  3957. spell = new Spell(data);
  3958. if(level_data && level_data->count(data->id) > 0)
  3959. {
  3960. vector<LevelArray*>* level_array = &((*level_data)[data->id]);
  3961. for(int8 i=0; i<level_array->size(); i++)
  3962. {
  3963. spell->AddSpellLevel(level_array->at(i)->adventure_class, level_array->at(i)->tradeskill_class, level_array->at(i)->spell_level*10);
  3964. }
  3965. }
  3966. /* Add spell to master list */
  3967. master_spell_list.AddSpell(data->id, data->tier, spell);
  3968. total++;
  3969. if( lua_script.length() > 0 )
  3970. LogWrite(SPELL__DEBUG, 5, "Spells", "\t%i. %s (Tier: %i) - '%s'", spell_id, spell_name.c_str(), data->tier, lua_script.c_str());
  3971. else if(data->is_active)
  3972. LogWrite(SPELL__WARNING, 1, "Spells", "\tSpell %s (%u, Tier: %i) set 'Active', but missing LUAScript", spell_name.c_str(), spell_id, data->tier);
  3973. } // end while
  3974. } // end else
  3975. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Spell Effects...");
  3976. LoadSpellEffects();
  3977. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Spell LUA Data...");
  3978. LoadSpellLuaData();
  3979. if(lua_interface)
  3980. {
  3981. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Spells Scripts...");
  3982. LoadSpellScriptData();
  3983. }
  3984. if (level_data) {
  3985. map<int32, vector<LevelArray*> >::iterator map_itr;
  3986. vector<LevelArray*>::iterator level_itr;
  3987. for(map_itr = level_data->begin(); map_itr != level_data->end(); map_itr++)
  3988. {
  3989. for(level_itr = map_itr->second.begin(); level_itr != map_itr->second.end(); level_itr++)
  3990. {
  3991. safe_delete(*level_itr);
  3992. }
  3993. }
  3994. }
  3995. safe_delete(level_data);
  3996. LogWrite(SPELL__INFO, 0, "Spells", "Loaded %u Spell%s (took %u seconds)", total, total == 1 ? "" : "s", Timer::GetUnixTimeStamp() - t_now);
  3997. }
  3998. void WorldDatabase::LoadSpellLuaData(){
  3999. Spell *spell;
  4000. Query query;
  4001. MYSQL_ROW row;
  4002. int32 total = 0;
  4003. MYSQL_RES *result = query.RunQuery2(Q_SELECT, "SELECT `spell_id`,`tier`,`value_type`,`value`,`value2`,`dynamic_helper` "
  4004. "FROM `spell_data` "
  4005. "ORDER BY `index_field`");
  4006. while (result && (row = mysql_fetch_row(result))) {
  4007. if ((spell = master_spell_list.GetSpell(atoul(row[0]), atoi(row[1]))) && row[2] && row[3] && row[4] && row[4]) {
  4008. LogWrite(SPELL__DEBUG, 5, "Spells", "\tLoading Spell LUA Data for spell_id: %u", atoul(row[0]));
  4009. if (!strcmp(row[2], "INT"))
  4010. spell->AddSpellLuaDataInt(atoi(row[3]), atoi(row[4]), string(row[5]));
  4011. else if (!strcmp(row[2], "FLOAT"))
  4012. spell->AddSpellLuaDataFloat(atof(row[3]), atof(row[4]),string(row[5]));
  4013. else if (!strcmp(row[2], "BOOL"))
  4014. spell->AddSpellLuaDataBool(!(strncasecmp(row[3], "true", 4)), string(row[5]));
  4015. else if (!strcmp(row[2], "STRING"))
  4016. spell->AddSpellLuaDataString(string(row[3]), string(row[4]), string(row[5]));
  4017. else
  4018. LogWrite(SPELL__ERROR, 0, "Spells", "Invalid Lua Spell data '%s' for Spell ID: %u", row[2], spell->GetSpellID());
  4019. total++;
  4020. }
  4021. }
  4022. LogWrite(SPELL__DEBUG, 0, "Spells", "\tLoaded %i Spell LUA Data entr%s.", total, total == 1 ? "y" : "ies");
  4023. }
  4024. void WorldDatabase::LoadSpellEffects() {
  4025. Spell *spell;
  4026. Query query;
  4027. MYSQL_ROW row;
  4028. int32 total = 0;
  4029. MYSQL_RES *result = query.RunQuery2(Q_SELECT, "SELECT `spell_id`,`tier`,`percentage`,`bullet`,`description` "
  4030. "FROM `spell_display_effects` "
  4031. "ORDER BY `spell_id`,`id` ASC");
  4032. while (result && (row = mysql_fetch_row(result))) {
  4033. if ((spell = master_spell_list.GetSpell(atoul(row[0]), atoi(row[1]))) && row[4]) {
  4034. LogWrite(SPELL__DEBUG, 5, "Spells", "\tLoading Spell Effects for spell_id: %u", atoul(row[0]));
  4035. spell->AddSpellEffect(atoi(row[2]), atoi(row[3]), row[4]);
  4036. total++;
  4037. }
  4038. }
  4039. LogWrite(SPELL__DEBUG, 0, "Spells", "\tLoaded %u Spell Effect%s.", total, total == 1 ? "" : "s");
  4040. }
  4041. int32 WorldDatabase::LoadPlayerSkillbar(Client* client){
  4042. client->GetPlayer()->ClearQuickbarItems();
  4043. Query query;
  4044. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, type, spell_id, slot, text_val, hotbar, tier FROM character_skillbar where char_id = %u", client->GetCharacterID());
  4045. MYSQL_ROW row;
  4046. int32 count = 0;
  4047. while(result && (row = mysql_fetch_row(result))){
  4048. count++;
  4049. int8 tier = atoi(row[6]);
  4050. Spell* spell = master_spell_list.GetSpell(atoul(row[2]), tier);
  4051. if(spell)
  4052. client->GetPlayer()->AddQuickbarItem(atoul(row[5]), atoul(row[3]), atoul(row[1]), spell->GetSpellIcon(), spell->GetSpellIconBackdrop(), spell->GetSpellID(), spell->GetSpellTier(), atoul(row[0]), row[4], false);
  4053. else if(atoul(row[1]) == QUICKBAR_MACRO)
  4054. client->GetPlayer()->AddQuickbarItem(atoul(row[5]), atoul(row[3]), atoul(row[1]), client->GetPlayer()->macro_icons[atoul(row[2])], 0xFFFF, atoul(row[2]), 0, atoul(row[0]), row[4], false);
  4055. else
  4056. client->GetPlayer()->AddQuickbarItem(atoul(row[5]), atoul(row[3]), atoul(row[1]), 0, 0, atoul(row[2]), 0, atoul(row[0]), row[4], false);
  4057. }
  4058. return count;
  4059. }
  4060. bool WorldDatabase::DeleteCharacter(int32 account_id, int32 character_id){
  4061. Guild *guild;
  4062. if((guild = guild_list.GetGuild(GetGuildIDByCharacterID(character_id))))
  4063. guild->RemoveGuildMember(character_id);
  4064. Query query;
  4065. query.RunQuery2(Q_DELETE, "DELETE FROM characters WHERE id=%u AND account_id=%u", character_id, account_id);
  4066. if(!query.GetAffectedRows())
  4067. {
  4068. //No error just in case ppl try doing stupid stuff
  4069. return false;
  4070. }
  4071. else{ //successfull, so the character did exist with that character_id and account_id
  4072. // new DB constraints should handle all these deletes, and more... commenting out for now
  4073. /*query.RunQuery2(Q_DELETE, "delete FROM character_details where char_id=%u", character_id);
  4074. query.RunQuery2(Q_DELETE, "delete FROM character_factions where char_id=%u", character_id);
  4075. query.RunQuery2(Q_DELETE, "delete FROM character_items where char_id=%u", character_id);
  4076. query.RunQuery2(Q_DELETE, "delete FROM character_skillbar where char_id=%u", character_id);
  4077. query.RunQuery2(Q_DELETE, "delete FROM character_skills where char_id=%u", character_id);
  4078. query.RunQuery2(Q_DELETE, "delete FROM character_spells where char_id=%u", character_id);
  4079. query.RunQuery2(Q_DELETE, "delete FROM char_colors where char_id=%u", character_id);*/
  4080. }
  4081. return true;
  4082. }
  4083. void WorldDatabase::DeleteCharacterSpell(int32 character_id, int32 spell_id) {
  4084. if (character_id > 0 && spell_id > 0) {
  4085. Query query;
  4086. query.RunQuery2(Q_DELETE, "DELETE FROM character_spells WHERE char_id=%u AND spell_id=%u", character_id, spell_id);
  4087. }
  4088. }
  4089. bool WorldDatabase::GetItemResultsToClient (Client* client, const char* varSearch, int maxResults) {
  4090. Query query;
  4091. MYSQL_ROW row;
  4092. int results = 0;
  4093. if( maxResults > 10 && client->GetAdminStatus ( ) < 100 )
  4094. maxResults = 10;
  4095. else if( maxResults > 20 )
  4096. maxResults = 20;
  4097. client->Message(CHANNEL_COLOR_YELLOW, "Item Search Results");
  4098. client->Message(CHANNEL_COLOR_YELLOW, "ResultNum) [ItemID] ItemName");
  4099. string itemsearch_query = string("SELECT id, name FROM items where name like '%%%s%%' limit %i");
  4100. MYSQL_RES* result = query.RunQuery2(Q_SELECT, itemsearch_query.c_str(),getSafeEscapeString(varSearch).c_str(),maxResults);
  4101. while(result && (row = mysql_fetch_row(result))){
  4102. results++;
  4103. client->Message(CHANNEL_COLOR_YELLOW, "%i) [%s] %s",results,row[0],row[1]);
  4104. }
  4105. if(results == 0)
  4106. {
  4107. client->Message(CHANNEL_COLOR_YELLOW, "No Items Found.");
  4108. return false;
  4109. }
  4110. client->Message(CHANNEL_COLOR_YELLOW, "%i Items Found.",results);
  4111. return true;
  4112. }
  4113. void WorldDatabase::SaveWorldTime(WorldTime* time){
  4114. Query query;
  4115. query.RunQuery2(Q_REPLACE, "replace into variables (variable_name, variable_value) values('gametime', '%i/%i/%i %i:%i')", time->month, time->day, time->year, time->hour, time->minute);
  4116. }
  4117. void WorldDatabase::SaveBugReport(const char* category, const char* subcategory, const char* causes_crash, const char* reproducible, const char* summary, const char* description, const char* version, const char* player, int32 account_id, const char* spawn_name, int32 spawn_id, int32 zone_id){
  4118. Query query;
  4119. int32 dbVersion = rule_manager.GetGlobalRule(R_World, DatabaseVersion)->GetInt32();
  4120. string bug_report = string("insert into bugs (category, subcategory, causes_crash, reproducible, summary, description, version, player, account_id, spawn_name, spawn_id, zone_id, dbversion, worldversion) values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %u, '%s', %u, %u, %u, '%s')");
  4121. query.RunQuery2(Q_INSERT, bug_report.c_str(), getSafeEscapeString(category).c_str(), getSafeEscapeString(subcategory).c_str(),
  4122. getSafeEscapeString(causes_crash).c_str(), getSafeEscapeString(reproducible).c_str(), getSafeEscapeString(summary).c_str(),
  4123. getSafeEscapeString(description).c_str(), getSafeEscapeString(version).c_str(), getSafeEscapeString(player).c_str(), account_id,
  4124. getSafeEscapeString(spawn_name).c_str(), spawn_id, zone_id, dbVersion, CURRENT_VERSION);
  4125. FixBugReport();
  4126. FixBugReport();
  4127. FixBugReport();
  4128. }
  4129. void WorldDatabase::FixBugReport(){
  4130. Query query;
  4131. string bug_report = string("update bugs set description = REPLACE(description,SUBSTRING(description,INSTR(description,'%'), 3),char(CONV(SUBSTRING(description,INSTR(description,'%')+1, 2), 16, 10))), summary = REPLACE(summary,SUBSTRING(summary,INSTR(summary,'%'), 3),char(CONV(SUBSTRING(summary,INSTR(summary,'%')+1, 2), 16, 10)))");
  4132. query.RunQuery2(bug_report.c_str(), Q_UPDATE);
  4133. }
  4134. int32 WorldDatabase::LoadQuests(){
  4135. Query query;
  4136. MYSQL_ROW row;
  4137. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `quest_id`, `name`, `type`, `zone`, `level`, `enc_level`, `description`, `lua_script`, `completed_text`, `spawn_id` FROM `quests`");
  4138. Quest* quest = 0;
  4139. char* name = 0;
  4140. char* type = 0;
  4141. char* zone = 0;
  4142. int8 level = 0;
  4143. int8 enc_level = 0;
  4144. char* description = 0;
  4145. char* script = 0;
  4146. int32 total = 0;
  4147. int32 id = 0;
  4148. char* completed_description = 0;
  4149. int32 return_npc_id = 0;
  4150. if(result){
  4151. while(result && (row = mysql_fetch_row(result))){
  4152. id = atoul(row[0]);
  4153. name = row[1];
  4154. type = row[2];
  4155. zone = row[3];
  4156. level = atoul(row[4]);
  4157. enc_level = atoul(row[5]);
  4158. description = row[6];
  4159. script = row[7];
  4160. completed_description = row[8];
  4161. return_npc_id = atoi(row[9]);
  4162. if(lua_interface) {
  4163. quest = lua_interface->LoadQuest(id, name, type, zone, level, description, script);
  4164. }
  4165. if(quest){
  4166. LogWrite(QUEST__DEBUG, 5, "Quests", "\tLoading Quest: '%s' (%u)", name, id);
  4167. LoadQuestDetails(quest);
  4168. string compDescription;
  4169. if (completed_description == NULL)
  4170. {
  4171. compDescription = string("Missing! Notify Developer");
  4172. LogWrite(QUEST__WARNING, 5, "Quests", "\tLoading Quest MISSING completed_text in quests table for: '%s' (%u)", name, id);
  4173. }
  4174. else
  4175. compDescription = string(completed_description);
  4176. quest->SetCompletedDescription(string(compDescription));
  4177. quest->SetQuestReturnNPC(return_npc_id);
  4178. quest->SetEncounterLevel(enc_level);
  4179. total++;
  4180. master_quest_list.AddQuest(id, quest);
  4181. }
  4182. }
  4183. }
  4184. LogWrite(QUEST__DEBUG, 0, "Quest", "\tLoaded %i Quest(s)", total);
  4185. return total;
  4186. }
  4187. void WorldDatabase::LoadQuestDetails(Quest* quest) {
  4188. Query query;
  4189. MYSQL_ROW row;
  4190. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `type`, `subtype`, `value`, `faction_id`, `quantity` FROM `quest_details` WHERE `quest_id`=%u", quest->GetQuestID());
  4191. string type;
  4192. string subtype;
  4193. sint64 value;
  4194. int32 faction_id;
  4195. int32 quantity;
  4196. while (result && (row = mysql_fetch_row(result))) {
  4197. type = string(row[0]);
  4198. subtype = string(row[1]);
  4199. value = atoi(row[2]);
  4200. faction_id = atoi(row[3]);
  4201. quantity = atoi(row[4]);
  4202. LogWrite(QUEST__DEBUG, 5, "Quests", "\t- Type: %s, SubType: %s, Val: %u, Faction: %u, Qty: %u", type.c_str(), subtype.c_str(), value, faction_id, quantity);
  4203. if (type == "Prereq") {
  4204. if (subtype == "Class")
  4205. quest->AddPrereqClass(value);
  4206. else if (subtype == "Faction")
  4207. quest->AddPrereqFaction(faction_id, value);
  4208. else if (subtype == "Item") {
  4209. Item* master_item = master_item_list.GetItem(value);
  4210. if (master_item) {
  4211. Item* item = new Item(master_item);
  4212. quest->AddPrereqItem(item);
  4213. }
  4214. }
  4215. else if (subtype == "AdvLevel")
  4216. quest->SetPrereqLevel(value);
  4217. else if (subtype == "Quest")
  4218. quest->AddPrereqQuest(value);
  4219. else if (subtype == "Race")
  4220. quest->AddPrereqRace(value);
  4221. else if (subtype == "TSClass")
  4222. quest->AddPrereqTradeskillClass(value);
  4223. else if (subtype == "TSLevel")
  4224. quest->SetPrereqTSLevel(value);
  4225. else if (subtype == "MaxTSLevel")
  4226. quest->SetPrereqMaxTSLevel(value);
  4227. else if (subtype == "MaxAdvLevel")
  4228. quest->SetPrereqMaxLevel(value);
  4229. }
  4230. else if (type == "Reward") {
  4231. if (subtype == "Item") {
  4232. Item* master_item = master_item_list.GetItem(value);
  4233. if (master_item) {
  4234. Item* item = new Item(master_item);
  4235. item->details.count = quantity;
  4236. quest->AddRewardItem(item);
  4237. }
  4238. }
  4239. else if (subtype == "Selectable") {
  4240. Item* master_item = master_item_list.GetItem(value);
  4241. if (master_item) {
  4242. Item* item = new Item(master_item);
  4243. item->details.count = quantity;
  4244. quest->AddSelectableRewardItem(item);
  4245. }
  4246. }
  4247. else if (subtype == "Coin") {
  4248. int32 copper = 0;
  4249. int32 silver = 0;
  4250. int32 gold = 0;
  4251. int32 plat = 0;
  4252. if (value >= 1000000) {
  4253. plat = value / 1000000;
  4254. value -= 1000000 * plat;
  4255. }
  4256. if (value >= 10000) {
  4257. gold = value / 10000;
  4258. value -= 10000 * gold;
  4259. }
  4260. if (value >= 100) {
  4261. silver = value / 100;
  4262. value -= 100 * silver;
  4263. }
  4264. if (value > 0)
  4265. copper = value;
  4266. quest->AddRewardCoins(copper, silver, gold, plat);
  4267. }
  4268. else if (subtype == "MaxCoin") {
  4269. quest->AddRewardCoinsMax(value);
  4270. }
  4271. else if (subtype == "Faction")
  4272. quest->AddRewardFaction(faction_id, value);
  4273. else if (subtype == "Experience")
  4274. quest->SetRewardXP(value);
  4275. else if (subtype == "TSExperience")
  4276. quest->SetRewardTSXP(value);
  4277. }
  4278. }
  4279. }
  4280. void WorldDatabase::LoadMerchantInformation() {
  4281. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tClearing Merchant Inventory...");
  4282. world.DeleteMerchantItems();
  4283. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tLoading Merchant Inventory...");
  4284. LoadMerchantInventory();
  4285. Query query;
  4286. MYSQL_ROW row;
  4287. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT merchant_id, inventory_id FROM merchants ORDER BY merchant_id");
  4288. int32 total = 0;
  4289. int32 last_merchant_id = 0;
  4290. int32 id = 0;
  4291. MerchantInfo* merchant = 0;
  4292. if(result) {
  4293. while(result && (row = mysql_fetch_row(result))) {
  4294. id = atoul(row[0]);
  4295. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "\tMerchantID: %u, InventoryID: %u", id, atoul(row[1]));
  4296. if(id != last_merchant_id) {
  4297. if(merchant)
  4298. world.AddMerchantInfo(last_merchant_id, merchant);
  4299. merchant = new MerchantInfo;
  4300. last_merchant_id = id;
  4301. total++;
  4302. }
  4303. merchant->inventory_ids.push_back(atoul(row[1]));
  4304. }
  4305. if(merchant)
  4306. world.AddMerchantInfo(last_merchant_id, merchant);
  4307. }
  4308. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tLoaded %i Merchant List(s)", total);
  4309. }
  4310. void WorldDatabase::LoadMerchantInventory(){
  4311. Query query;
  4312. MYSQL_ROW row;
  4313. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT inventory_id, item_id, quantity, price_item_id, price_item_qty, price_item2_id, price_item2_qty, price_status, price_coins, price_stationcash FROM merchant_inventory ORDER BY inventory_id");
  4314. int32 total = 0;
  4315. int32 id;
  4316. if(result) {
  4317. while(result && (row = mysql_fetch_row(result))) {
  4318. MerchantItemInfo ItemInfo;
  4319. id = atoul(row[0]);
  4320. ItemInfo.item_id = atoul(row[1]);
  4321. ItemInfo.quantity = atoi(row[2]);
  4322. ItemInfo.price_item_id = atoul(row[3]);
  4323. ItemInfo.price_item_qty = atoi(row[4]);
  4324. ItemInfo.price_item2_id = atoul(row[5]);
  4325. ItemInfo.price_item2_qty = atoi(row[6]);
  4326. ItemInfo.price_status = atoul(row[7]);
  4327. ItemInfo.price_coins = atoul(row[8]);
  4328. ItemInfo.price_stationcash = atoul(row[9]);
  4329. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "\tInventoryID: %u, ItemID: %u, Qty: %u", id, ItemInfo.item_id, ItemInfo.quantity);
  4330. world.AddMerchantItem(id, ItemInfo);
  4331. total++;
  4332. }
  4333. }
  4334. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tLoaded %i Merchant Inventory Item(s)", total);
  4335. }
  4336. string WorldDatabase::GetMerchantDescription(int32 merchant_id) {
  4337. Query query;
  4338. MYSQL_ROW row;
  4339. string description;
  4340. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `description` FROM `merchants` WHERE `merchant_id`=%u", merchant_id);
  4341. if (result && (row = mysql_fetch_row(result)))
  4342. description = string(row[0]);
  4343. return description;
  4344. }
  4345. void WorldDatabase::LoadTransporters(ZoneServer* zone){
  4346. int32 total = 0;
  4347. zone->DeleteGlobalTransporters();
  4348. Query query;
  4349. MYSQL_ROW row;
  4350. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT transport_id, transport_type, display_name, message, destination_zone_id, destination_x, destination_y, destination_z, destination_heading, trigger_location_zone_id, trigger_location_x, trigger_location_y, trigger_location_z, trigger_radius, cost, id, min_level, max_level, quest_req, quest_step_req, quest_completed, map_x, map_y, expansion_flag, holiday_flag, min_client_version, max_client_version, flight_path_id, mount_id, mount_red_color, mount_green_color, mount_blue_color FROM transporters ORDER BY transport_id");
  4351. if(result){
  4352. while(result && (row = mysql_fetch_row(result))){
  4353. LogWrite(TRANSPORT__DEBUG, 5, "Transport", "---Loading Transporter ID: %u, transport_type: %s", row[0], row[1]);
  4354. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---display_name: %s, message: %s", row[2], row[3]);
  4355. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---destination_zone_id: %s", row[4]);
  4356. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---destination_x: %s, destination_y: %s, destination_z: %s, destination_heading: %s", row[5], row[6], row[7], row[8]);
  4357. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---trigger_location_zone_id: %s", row[9]);
  4358. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---trigger_location_x: %s, trigger_location_y: %s, trigger_location_z: %s", row[10], row[11], row[12], row[13]);
  4359. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---trigger_radius: %s, cost: %s, id: %s", row[14], row[15]);
  4360. string name = "";
  4361. if(row[2])
  4362. name = string(row[2]);
  4363. string message = "";
  4364. if(row[3])
  4365. message = string(row[3]);
  4366. if(row[1] && strcmp(row[1], "Zone") == 0)
  4367. zone->AddTransporter(atoul(row[0]), TRANSPORT_TYPE_ZONE, name, message, atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]), atoi(row[16]), atoi(row[17]), atoul(row[18]), atoi(row[19]), atoul(row[20]), atoul(row[21]), atoul(row[22]), atoul(row[23]), atoul(row[24]), atoul(row[25]), atoul(row[26]), atoul(row[27]), atoul(row[28]), atoul(row[29]), atoul(row[30]), atoul(row[31]));
  4368. else if (row[1] && strcmp(row[1], "Flight") == 0)
  4369. zone->AddTransporter(atoul(row[0]), TRANSPORT_TYPE_FLIGHT, name, message, atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]), atoi(row[16]), atoi(row[17]), atoul(row[18]), atoi(row[19]), atoul(row[20]), atoul(row[21]), atoul(row[22]), atoul(row[23]), atoul(row[24]), atoul(row[25]), atoul(row[26]), atoul(row[27]), atoul(row[28]), atoul(row[29]), atoul(row[30]), atoul(row[31]));
  4370. else if(row[1] && strcmp(row[1], "Location") == 0)
  4371. zone->AddLocationTransporter(atoul(row[9]), message, atof(row[10]), atof(row[11]), atof(row[12]), atof(row[13]), atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]));
  4372. else
  4373. zone->AddTransporter(atoul(row[0]), TRANSPORT_TYPE_GENERIC, "", message, atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]), atoi(row[16]), atoi(row[17]), atoul(row[18]), atoi(row[19]), atoul(row[20]), atoul(row[21]), atoul(row[22]), atoul(row[23]), atoul(row[24]), atoul(row[25]), atoul(row[26]), atoul(row[27]), atoul(row[28]), atoul(row[29]), atoul(row[30]), atoul(row[31]));
  4374. total++;
  4375. }
  4376. }
  4377. LogWrite(TRANSPORT__DEBUG, 0, "Transport", "--Loaded %i Transporter(s)", total);
  4378. LoadTransportMaps(zone);
  4379. }
  4380. void WorldDatabase::LoadFogInit(string zone, PacketStruct* packet)
  4381. {
  4382. LogWrite(WORLD__TRACE, 9, "World", "Enter: %s", __FUNCTION__);
  4383. if(!packet || zone.length() == 0)
  4384. return;
  4385. Query query;
  4386. MYSQL_ROW row;
  4387. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT highest, lowest, zone_name, explored_map_name, unexplored_map_name, bounds1_x, bounds1_z, bounds2_x, bounds2_z, bounds3_x, bounds3_z, bounds4_x, bounds4_z, explored_key, unexplored_key, map_id FROM map_data where zone_name like '%s%%'", getSafeEscapeString(&zone).c_str());
  4388. if(result){
  4389. int count = mysql_num_rows(result);
  4390. int i=0;
  4391. int64 explored_key;
  4392. int64 unexplored_key;
  4393. packet->setArrayLengthByName("num_maps", count);
  4394. while(result && (row = mysql_fetch_row(result))){
  4395. packet->setDataByName("highest_z", atof(row[0]));
  4396. packet->setDataByName("lowest_z", atof(row[1]));
  4397. packet->setDataByName("map_id", atoul(row[15]));
  4398. packet->setArrayDataByName("unknown7", 1600, i);
  4399. packet->setArrayDataByName("unknown8", 1200, i);
  4400. packet->setArrayDataByName("zone_name", row[2], i);
  4401. packet->setArrayDataByName("explored_map_name", row[3], i);
  4402. packet->setArrayDataByName("unexplored_map_name", row[4], i);
  4403. packet->setArrayDataByName("map_bounds1_x", atof(row[5]), i);
  4404. packet->setArrayDataByName("map_bounds1_z", atof(row[6]), i);
  4405. packet->setArrayDataByName("map_bounds2_x", atof(row[7]), i);
  4406. packet->setArrayDataByName("map_bounds2_z", atof(row[8]), i);
  4407. packet->setArrayDataByName("map_bounds3_x", atof(row[9]), i);
  4408. packet->setArrayDataByName("map_bounds3_z", atof(row[10]), i);
  4409. packet->setArrayDataByName("map_bounds4_x", atof(row[11]), i);
  4410. packet->setArrayDataByName("map_bounds4_z", atof(row[12]), i);
  4411. #ifdef WIN32
  4412. explored_key = _strtoui64(row[13], NULL, 10);
  4413. unexplored_key = _strtoui64(row[14], NULL, 10);
  4414. #else
  4415. explored_key = strtoull(row[13], 0, 10);
  4416. unexplored_key = strtoull(row[14], 0, 10);
  4417. #endif
  4418. packet->setArrayDataByName("explored_key", explored_key, i);
  4419. packet->setArrayDataByName("unexplored_key", unexplored_key, i);
  4420. i++;
  4421. }
  4422. }
  4423. LogWrite(WORLD__TRACE, 9, "World", "Exit: %s", __FUNCTION__);
  4424. }
  4425. string WorldDatabase::GetColumnNames(char* name){
  4426. Query query;
  4427. MYSQL_ROW row;
  4428. string columns = "";
  4429. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "show columns FROM %s", name);
  4430. if(result && mysql_num_rows(result) > 0){
  4431. int16 i = 0;
  4432. while((row = mysql_fetch_row(result))){
  4433. if(strcmp(row[0], "table_data_version") != 0){
  4434. if(i>0)
  4435. columns.append(",");
  4436. columns.append(row[0]);
  4437. i++;
  4438. }
  4439. }
  4440. }
  4441. columns.append("");
  4442. return columns;
  4443. }
  4444. void WorldDatabase::ToggleCharacterOnline() {
  4445. Query query;
  4446. query.RunQuery2(Q_UPDATE, "UPDATE characters SET is_online = 0;");
  4447. }
  4448. void WorldDatabase::ToggleCharacterOnline(Client* client, int8 toggle) {
  4449. if (client) {
  4450. Query query;
  4451. Player* player = client->GetPlayer();
  4452. //if(!player->CheckPlayerInfo())
  4453. // return;
  4454. if (player)
  4455. {
  4456. LogWrite(PLAYER__DEBUG, 0, "Player", "Toggling Character %s", toggle ? "ONLINE!" : "OFFLINE!");
  4457. query.RunQuery2(Q_UPDATE, "UPDATE characters SET is_online=%i WHERE id = %u;", toggle, client->GetCharacterID());
  4458. }
  4459. }
  4460. }
  4461. void WorldDatabase::LoadPlayerStatistics(Player* player, int32 char_id) {
  4462. Query query;
  4463. MYSQL_ROW row;
  4464. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT stat_id, stat_value, stat_date FROM statistics WHERE char_id=%i", char_id);
  4465. while (result && (row = mysql_fetch_row(result))) {
  4466. int32 stat_id = atoi(row[0]);
  4467. sint32 stat_value = atoi(row[1]);
  4468. int32 stat_date = atoi(row[2]);
  4469. player->AddPlayerStatistic(stat_id, stat_value, stat_date);
  4470. }
  4471. }
  4472. void WorldDatabase::WritePlayerStatistic(Player *player, Statistic* stat) {
  4473. if (player && player->GetCharacterID() > 0 && stat) {
  4474. Query query;
  4475. query.RunQuery2(Q_INSERT, "INSERT INTO statistics (char_id, guild_id, stat_id, stat_value, stat_date) VALUES(%i, %i, %i, %i, %i) ON DUPLICATE KEY UPDATE stat_value = %i, stat_date = %i;",
  4476. player->GetCharacterID(), 0, stat->stat_id, stat->stat_value, stat->stat_date,
  4477. stat->stat_value, stat->stat_date);
  4478. }
  4479. }
  4480. void WorldDatabase::LoadServerStatistics()
  4481. {
  4482. Query query;
  4483. MYSQL_ROW row;
  4484. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT stat_id, stat_value, stat_date FROM statistics WHERE char_id=0 AND guild_id=0");
  4485. while (result && (row = mysql_fetch_row(result))) {
  4486. int32 stat_id = atoi(row[0]);
  4487. sint32 stat_value = atoi(row[1]);
  4488. int32 stat_date = atoi(row[2]);
  4489. world.AddServerStatistic(stat_id, stat_value, stat_date);
  4490. LogWrite(INIT__DEBUG, 5, "Stats", "Loading Stat ID %i, value: %i", stat_id, stat_value);
  4491. }
  4492. }
  4493. void WorldDatabase::WriteServerStatistic(Statistic* stat) {
  4494. if (stat) {
  4495. Query query;
  4496. query.RunQuery2(Q_INSERT, "INSERT INTO statistics (char_id, guild_id, stat_id, stat_value, stat_date) VALUES(0, 0, %i, %i, %i) ON DUPLICATE KEY UPDATE stat_value = %i, stat_date = %i;",
  4497. stat->stat_id, stat->stat_value, stat->stat_date,
  4498. stat->stat_value, stat->stat_date);
  4499. }
  4500. }
  4501. void WorldDatabase::WriteServerStatistic(int32 stat_id, sint32 stat_value) {
  4502. Query query;
  4503. query.RunQuery2(Q_INSERT, "INSERT INTO statistics (char_id, guild_id, stat_id, stat_value, stat_date) VALUES(0, 0, %i, %i, %i) ON DUPLICATE KEY UPDATE stat_value = %i, stat_date = %i;",
  4504. stat_id, stat_value, Timer::GetUnixTimeStamp(),
  4505. stat_value, Timer::GetUnixTimeStamp());
  4506. }
  4507. void WorldDatabase::WriteServerStatisticsNeededQueries() {
  4508. Query query1, query2, query3;
  4509. MYSQL_ROW row1, row2, row3;
  4510. // Number of unique accounts
  4511. MYSQL_RES* result1 = query1.RunQuery2(Q_SELECT, "SELECT COUNT(DISTINCT account_id) FROM characters");
  4512. if (result1 && (row1 = mysql_fetch_row(result1)) && row1[0] != NULL)
  4513. WriteServerStatistic(STAT_SERVER_NUM_ACCOUNTS, atoi(row1[0]));
  4514. else
  4515. WriteServerStatistic(STAT_SERVER_NUM_ACCOUNTS, 0);
  4516. // Number of characters
  4517. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT COUNT(id) FROM characters");
  4518. if (result2 && (row2 = mysql_fetch_row(result2)) && row2[0] != NULL)
  4519. WriteServerStatistic(STAT_SERVER_NUM_CHARACTERS, atoi(row2[0]));
  4520. else
  4521. WriteServerStatistic(STAT_SERVER_NUM_CHARACTERS, 0);
  4522. // Average character level
  4523. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT ROUND(AVG(level)) FROM characters");
  4524. if (result3 && (row3 = mysql_fetch_row(result3)) && row3[0] != NULL)
  4525. WriteServerStatistic(STAT_SERVER_AVG_CHAR_LEVEL, atoi(row3[0]));
  4526. else
  4527. WriteServerStatistic(STAT_SERVER_AVG_CHAR_LEVEL, 0);
  4528. }
  4529. map<int32,int32>* WorldDatabase::GetInstanceRemovedSpawns(int32 instance_id, int8 type)
  4530. {
  4531. DatabaseResult result;
  4532. map<int32,int32>* ret = NULL;
  4533. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  4534. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Loading removed spawns for instance_id: %u, spawn_type: %i", instance_id, type);
  4535. if( !database_new.Select(&result, "SELECT spawn_location_entry_id, respawn_time FROM instance_spawns_removed WHERE instance_id = %i AND spawn_type = %i", instance_id, type) )
  4536. {
  4537. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in GetInstanceRemovedSpawns() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4538. return ret;
  4539. }
  4540. else
  4541. {
  4542. if( result.GetNumRows() > 0 )
  4543. {
  4544. ret = new map<int32,int32>;
  4545. while( result.Next() )
  4546. {
  4547. int32 spawn_location_entry_id = result.GetInt32Str("spawn_location_entry_id");
  4548. /*
  4549. respawnTime == 0 - never respawn
  4550. respawnTime = 1 - spawn now
  4551. respawnTime > 1 (continue timer)
  4552. */
  4553. int32 respawntime = result.GetInt32Str("respawn_time");
  4554. LogWrite(INSTANCE__DEBUG, 5, "Instance", "Found spawn point: %u, respawn time: %i", spawn_location_entry_id, respawntime);
  4555. ret->insert(make_pair(spawn_location_entry_id, respawntime));
  4556. }
  4557. }
  4558. else
  4559. LogWrite(INSTANCE__DEBUG, 0, "Instance", "No removed spawns found for instance_id: %u, spawn_type: %i", instance_id, type);
  4560. }
  4561. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  4562. return ret;
  4563. }
  4564. bool WorldDatabase::CheckVectorForValue(vector<int32>* vector, int32 value) {
  4565. if ( vector != NULL )
  4566. {
  4567. for(int32 i=0;i<vector->size();i++)
  4568. {
  4569. int32 compare = vector->at(i);
  4570. if ( compare == value )
  4571. return true;
  4572. }
  4573. }
  4574. return false;
  4575. }
  4576. int32 WorldDatabase::CheckSpawnRemoveInfo(map<int32,int32>* inmap, int32 spawn_location_entry_id)
  4577. {
  4578. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  4579. map<int32, int32>::iterator iter;
  4580. if ( inmap != NULL )
  4581. {
  4582. for(iter=inmap->begin();iter!=inmap->end();iter++)
  4583. {
  4584. if ( iter->first == spawn_location_entry_id )
  4585. return (int32)iter->second;
  4586. }
  4587. }
  4588. return 1;
  4589. }
  4590. int32 WorldDatabase::AddCharacterInstance(int32 char_id, int32 instance_id, string zone_name, int8 instance_type, int32 last_success, int32 last_failure, int32 success_lockout, int32 failure_lockout)
  4591. {
  4592. int32 ret = 0;
  4593. if( !database_new.Query("INSERT INTO character_instances (char_id, instance_id, instance_zone_name, instance_type, last_success_timestamp, last_failure_timestamp, success_lockout_time, failure_lockout_time) VALUES (%u, %u, '%s', %i, %u, %u, %u, %u) ", char_id, instance_id, database_new.EscapeStr(zone_name).c_str(), instance_type, last_success, last_failure, success_lockout, failure_lockout) )
  4594. {
  4595. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in AddCharacterInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4596. return 0;
  4597. }
  4598. ret = database_new.LastInsertID();
  4599. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Adding character %u to instance: %u", char_id, instance_id);
  4600. //LogWrite(INSTANCE__DEBUG, 1, "Instance", "-- Reenter: %u, Reset: %u, Lockout: %u", grant_reenter_time_left, grant_reset_time_left, lockout_time);
  4601. return ret;
  4602. }
  4603. bool WorldDatabase::UpdateCharacterInstanceTimers(int32 char_id, int32 instance_id, int32 lockout_time, int32 reset_time, int32 reenter_time )
  4604. {
  4605. if ( lockout_time > 0 && reset_time > 0 && reenter_time > 0 )
  4606. database_new.Query("UPDATE character_instances SET lockout_time = %i, grant_reset_time_left = %i, grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i", lockout_time, reset_time, reenter_time, char_id, instance_id);
  4607. else if ( lockout_time > 0 && reset_time > 0 )
  4608. database_new.Query("UPDATE character_instances SET lockout_time = %i, grant_reset_time_left = %i WHERE char_id = %i AND instance_id = %i", lockout_time, reset_time, char_id, instance_id);
  4609. else if ( reset_time > 0 && reenter_time > 0 )
  4610. database_new.Query("UPDATE character_instances SET grant_reset_time_left = %i, grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i",reset_time, reenter_time, char_id, instance_id);
  4611. else if ( lockout_time > 0 && reenter_time > 0 )
  4612. database_new.Query("UPDATE character_instances SET lockout_time = %i, grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i", lockout_time, reenter_time, char_id, instance_id);
  4613. else if ( lockout_time > 0 )
  4614. database_new.Query("UPDATE character_instances SET lockout_time = %i WHERE char_id = %i AND instance_id = %i", lockout_time, char_id, instance_id);
  4615. else if ( reset_time > 0 )
  4616. database_new.Query("UPDATE character_instances SET grant_reset_time_left = %i WHERE char_id = %i AND instance_id = %i", reset_time, char_id, instance_id);
  4617. else if ( reenter_time > 0 )
  4618. database_new.Query("UPDATE character_instances SET grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i", reenter_time, char_id, instance_id);
  4619. if( database_new.GetError() )
  4620. {
  4621. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in UpdateCharacterInstanceTimers() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4622. return false;
  4623. }
  4624. else
  4625. {
  4626. if ( database_new.AffectedRows() > 0 )
  4627. {
  4628. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Updating instance timers for character %u to instance: %u", char_id, instance_id);
  4629. LogWrite(INSTANCE__DEBUG, 1, "Instance", "-- Reenter: %u, Reset: %u, Lockout: %u", reenter_time, reset_time, lockout_time);
  4630. return true;
  4631. }
  4632. else
  4633. return false;
  4634. }
  4635. }
  4636. bool WorldDatabase::UpdateCharacterInstance(int32 char_id, string zone_name, int32 instance_id, int8 type, int32 timestamp) {
  4637. // type = 1 = success timestamp
  4638. // type = 2 = failure timestamp
  4639. if (instance_id > 0) {
  4640. if (type == 1) {
  4641. database_new.Query("UPDATE character_instances SET instance_id = %u, last_success_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", instance_id, timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  4642. }
  4643. else if (type == 2) {
  4644. database_new.Query("UPDATE character_instances SET instance_id = %u, last_failure_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", instance_id, timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  4645. }
  4646. else {
  4647. database_new.Query("UPDATE character_instances SET instance_id = %u WHERE char_id = %u AND instance_zone_name = '%s'", instance_id, char_id, database_new.EscapeStr(zone_name).c_str());
  4648. }
  4649. }
  4650. else {
  4651. if (type == 1) {
  4652. database_new.Query("UPDATE character_instances SET last_success_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  4653. }
  4654. else if (type == 2) {
  4655. database_new.Query("UPDATE character_instances SET last_failure_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  4656. }
  4657. }
  4658. if (database_new.GetError()) {
  4659. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in UpdateCharacterInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4660. return false;
  4661. }
  4662. return true;
  4663. }
  4664. bool WorldDatabase::VerifyInstanceID(int32 char_id, int32 instance_id) {
  4665. DatabaseResult result;
  4666. database_new.Select(&result, "SELECT COUNT(id) as num_instances FROM instances WHERE id = %u", instance_id);
  4667. if (result.Next() && result.GetInt32Str("num_instances") == 0) {
  4668. DeleteCharacterFromInstance(char_id, instance_id);
  4669. return false;
  4670. }
  4671. return true;
  4672. }
  4673. bool WorldDatabase::UpdateInstancedSpawnRemoved(int32 spawn_location_entry_id, int32 spawn_type, int32 respawn_time, int32 instance_id )
  4674. {
  4675. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Updating removed spawns for instance_id: %u", instance_id);
  4676. LogWrite(INSTANCE__DEBUG, 1, "Instance", "-- Spawn Location Entry ID: %u, Type: %u, Respawn: %u", spawn_location_entry_id, spawn_type, respawn_time);
  4677. if( !database_new.Query("UPDATE instance_spawns_removed SET respawn_time = %i WHERE spawn_location_entry_id = %i AND spawn_type = %i AND instance_id = %i", respawn_time, spawn_location_entry_id, spawn_type, instance_id) )
  4678. {
  4679. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in UpdateInstancedSpawnRemoved() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4680. return false;
  4681. }
  4682. if ( database_new.AffectedRows() > 0 )
  4683. {
  4684. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Updated removed spawns for instance_id: %u", instance_id);
  4685. return true;
  4686. }
  4687. else
  4688. return false;
  4689. }
  4690. int32 WorldDatabase::CreateNewInstance(int32 zone_id)
  4691. {
  4692. int32 ret = 0;
  4693. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Creating new instance for zone: %u ", zone_id);
  4694. if( !database_new.Query("INSERT INTO instances (zone_id) VALUES (%u)", zone_id) )
  4695. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in CreateNewInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4696. else
  4697. ret = database_new.LastInsertID();
  4698. if( ret > 0 )
  4699. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Created new instance_id %u for zone: %u ", ret, zone_id);
  4700. return ret;
  4701. }
  4702. int32 WorldDatabase::CreateInstanceSpawnRemoved(int32 spawn_location_entry_id, int32 spawn_type, int32 respawn_time, int32 instance_id )
  4703. {
  4704. int32 ret = 0;
  4705. LogWrite(INSTANCE__DEBUG, 3, "Instance", "Creating new instance spawn removed entries for instance_id: %u ", instance_id);
  4706. LogWrite(INSTANCE__DEBUG, 5, "Instance", "-- Spawn Location Entry ID: %u, Type: %u, Respawn: %u", spawn_location_entry_id, spawn_type, respawn_time);
  4707. if( !database_new.Query("INSERT INTO instance_spawns_removed (spawn_location_entry_id, spawn_type, instance_id, respawn_time) values(%u, %u, %u, %u)", spawn_location_entry_id, spawn_type, instance_id, respawn_time) )
  4708. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in CreateInstanceSpawnRemoved() query '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4709. else
  4710. ret = database_new.LastInsertID();
  4711. // potentially spammy, if it calls for every spawn added. Set to level 3 or 5?
  4712. if( ret > 0 )
  4713. LogWrite(INSTANCE__DEBUG, 5, "Instance", "Created new spawn removed entry: %u for instance_id %u", ret, instance_id);
  4714. return ret;
  4715. }
  4716. bool WorldDatabase::DeleteInstance(int32 instance_id)
  4717. {
  4718. if( !database_new.Query("DELETE FROM instances WHERE id = %u", instance_id) )
  4719. {
  4720. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4721. return false;
  4722. }
  4723. /* JA: should not need this delete with FK/Constraints
  4724. if( !database_new.Query("DELETE FROM instance_spawns_removed WHERE instance_id = %u", instance_id) )
  4725. {
  4726. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4727. return false;
  4728. }
  4729. */
  4730. // Remove the instance from the character_instances table
  4731. database_new.Query("UPDATE `character_instances` SET `instance_id` = 0 WHERE `instance_id` = %u", instance_id);
  4732. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Deleted instance_id %u", instance_id);
  4733. return true;
  4734. }
  4735. bool WorldDatabase::DeleteInstanceSpawnRemoved(int32 instance_id, int32 spawn_location_entry_id)
  4736. {
  4737. if( !database_new.Query("DELETE FROM instance_spawns_removed WHERE instance_id = %u AND spawn_location_entry_id = %u", instance_id, spawn_location_entry_id) )
  4738. {
  4739. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteInstanceSpawnRemoved() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4740. return false;
  4741. }
  4742. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Deleted removed spawn: %u for instance_id %u", spawn_location_entry_id, instance_id);
  4743. return true;
  4744. }
  4745. bool WorldDatabase::DeleteCharacterFromInstance(int32 char_id, int32 instance_id)
  4746. {
  4747. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Delete character %u from instance_id %u.", char_id, instance_id);
  4748. if( !database_new.Query("UPDATE `character_instances` SET `instance_id` = 0 WHERE `instance_id` = %u AND `char_id` = %u", instance_id, char_id) )
  4749. {
  4750. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteCharacterFromInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  4751. return false;
  4752. }
  4753. if ( database_new.AffectedRows() == 0 ) // didn't find an instance to delete
  4754. {
  4755. LogWrite(INSTANCE__DEBUG, 1, "Instance", "No instance_id %u for character %u to delete.", instance_id, char_id);
  4756. return false;
  4757. }
  4758. else
  4759. {
  4760. // delete entire instance if the last player has left
  4761. DatabaseResult result;
  4762. database_new.Select(&result, "SELECT count(id) as num_instances FROM character_instances where instance_id = %u",instance_id);
  4763. if(result.Next() && result.GetInt32Str("num_instances") == 0)
  4764. {
  4765. LogWrite(INSTANCE__DEBUG, 0, "Instance", "No characters in instance: Delete instance_id %u.", instance_id);
  4766. DeleteInstance(instance_id);
  4767. }
  4768. }
  4769. return true;
  4770. }
  4771. bool WorldDatabase::LoadCharacterInstances(Client* client)
  4772. {
  4773. DatabaseResult result;
  4774. DatabaseResult result2;
  4775. bool addedInstance = false;
  4776. database_new.Select(&result, "SELECT `id`, `instance_id`, `instance_zone_name`, `instance_type`, `last_success_timestamp`, `last_failure_timestamp`, `success_lockout_time`, `failure_lockout_time` FROM `character_instances` WHERE `char_id` = %u", client->GetCharacterID());
  4777. if( result.GetNumRows() > 0 )
  4778. {
  4779. while( result.Next() )
  4780. {
  4781. int32 zone_id = 0;
  4782. int32 instance_id = result.GetInt32Str("instance_id");
  4783. // If `instance_id` is greater then 0 then get the zone id with it, else get the zone id from the zone name
  4784. if (instance_id != 0) {
  4785. if (database_new.Select(&result2, "SELECT `zone_id` FROM `instances` WHERE `id` = %u", instance_id)) {
  4786. if (result2.Next())
  4787. zone_id = result2.GetInt32Str("zone_id");
  4788. }
  4789. }
  4790. if (zone_id == 0)
  4791. zone_id = GetZoneID(result.GetStringStr("instance_zone_name"));
  4792. client->GetPlayer()->GetCharacterInstances()->AddInstance(
  4793. result.GetInt32Str("id"),
  4794. instance_id,
  4795. result.GetInt32Str("last_success_timestamp"),
  4796. result.GetInt32Str("last_failure_timestamp"),
  4797. result.GetInt32Str("success_lockout_time"),
  4798. result.GetInt32Str("failure_lockout_time"),
  4799. zone_id,
  4800. result.GetInt8Str("instance_type"),
  4801. string(result.GetStringStr("instance_zone_name"))
  4802. );
  4803. addedInstance = true;
  4804. }
  4805. }
  4806. return addedInstance;
  4807. }
  4808. void WorldDatabase::UpdateLoginEquipment()
  4809. {
  4810. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Updating `character_items` CRC in %s", __FUNCTION__);
  4811. database_new.Query("UPDATE character_items SET login_checksum = CRC32(CRC32(type) + CRC32(slot) + CRC32(item_id)) WHERE `type` = 'EQUIPPED' AND ( slot <= 8 OR slot = 19 )");
  4812. }
  4813. MutexMap<int32, LoginEquipmentUpdate>* WorldDatabase::GetEquipmentUpdates()
  4814. {
  4815. DatabaseResult result;
  4816. MutexMap<int32, LoginEquipmentUpdate>* ret = 0;
  4817. LoginEquipmentUpdate update;
  4818. int32 count = 0;
  4819. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Looking for Login Appearance Updates...");
  4820. // TODO: Someday store the equipment colors in character_items, for custom colorization of gear (?)
  4821. if( database_new.Select(&result, "SELECT ci.id, ci.char_id, ia.equip_type, ia.red, green, ia.blue, ia.highlight_red, ia.highlight_green, ia.highlight_blue, ci.slot FROM characters c JOIN character_items ci ON c.id = ci.char_id JOIN item_appearances ia ON ci.item_id = ia.item_id WHERE c.deleted = 0 AND ci.type = 'EQUIPPED' AND ( ci.slot <= 8 OR ci.slot = 19 ) AND ci.login_checksum <> CRC32(CRC32(`ci`.`type`) + CRC32(ci.slot) + CRC32(ci.item_id)) ORDER BY ci.char_id, ci.slot") )
  4822. {
  4823. while( result.Next() )
  4824. {
  4825. LogWrite(INIT__LOGIN_DEBUG, 5, "Login", "Found update for char_id %i!", result.GetInt32Str("char_id"));
  4826. if(!ret)
  4827. ret = new MutexMap<int32, LoginEquipmentUpdate>();
  4828. update.world_char_id = result.GetInt32Str("char_id");
  4829. update.equip_type = result.GetInt16Str("equip_type");
  4830. update.red = result.GetInt8Str("red");
  4831. update.green = result.GetInt8Str("green");
  4832. update.blue = result.GetInt8Str("blue");
  4833. update.highlight_red = result.GetInt8Str("highlight_red");
  4834. update.highlight_green = result.GetInt8Str("highlight_green");
  4835. update.highlight_blue = result.GetInt8Str("highlight_blue");
  4836. update.slot = result.GetInt8Str("slot");
  4837. ret->Put(result.GetInt32Str("id"), update);
  4838. count++;
  4839. }
  4840. }
  4841. if(count)
  4842. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Found %i Login Appearance Update%s...", count, count == 1 ? "" : "s");
  4843. return ret;
  4844. }
  4845. MutexMap<int32, LoginEquipmentUpdate>* WorldDatabase::GetEquipmentUpdates(int32 char_id)
  4846. {
  4847. DatabaseResult result;
  4848. MutexMap<int32, LoginEquipmentUpdate>* ret = 0;
  4849. LoginEquipmentUpdate update;
  4850. int32 count = 0;
  4851. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Looking for Login Appearance Updates for char_id: %u", char_id);
  4852. // TODO: Someday store the equipment colors in character_items, for custom colorization of gear (?)
  4853. if( database_new.Select(&result, "SELECT ci.id, ci.char_id, ia.equip_type, ia.red, green, ia.blue, ia.highlight_red, ia.highlight_green, ia.highlight_blue, ci.slot FROM characters c JOIN character_items ci ON c.id = ci.char_id JOIN item_appearances ia ON ci.item_id = ia.item_id WHERE c.deleted = 0 AND ci.type = 'EQUIPPED' AND ( ci.slot <= 8 OR ci.slot = 19 ) AND ci.login_checksum <> CRC32(CRC32(ci.type) + CRC32(ci.slot) + CRC32(ci.item_id)) AND ci.char_id = %u ORDER BY ci.slot", char_id) )
  4854. {
  4855. while( result.Next() )
  4856. {
  4857. LogWrite(INIT__LOGIN_DEBUG, 5, "Login", "Found update for char_id %i!", result.GetInt32Str("char_id"));
  4858. if(!ret)
  4859. ret = new MutexMap<int32, LoginEquipmentUpdate>();
  4860. update.world_char_id = char_id;
  4861. update.equip_type = result.GetInt16Str("equip_type");
  4862. update.red = result.GetInt8Str("red");
  4863. update.green = result.GetInt8Str("green");
  4864. update.blue = result.GetInt8Str("blue");
  4865. update.highlight_red = result.GetInt8Str("highlight_red");
  4866. update.highlight_green = result.GetInt8Str("highlight_green");
  4867. update.highlight_blue = result.GetInt8Str("highlight_blue");
  4868. update.slot = result.GetInt8Str("slot");
  4869. ret->Put(result.GetInt32Str("id"), update);
  4870. count++;
  4871. }
  4872. }
  4873. if(count)
  4874. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Found %i Login Appearance Update%s...", count, count == 1 ? "" : "s");
  4875. return ret;
  4876. }
  4877. void WorldDatabase::UpdateLoginZones() {
  4878. Query query;
  4879. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Updating `zones` CRC in %s", __FUNCTION__);
  4880. query.RunQuery2("UPDATE zones SET login_checksum = CRC32(CRC32(id) + CRC32(`name`) + CRC32(`file`) + CRC32(description))", Q_UPDATE);
  4881. }
  4882. MutexMap<int32, LoginZoneUpdate>* WorldDatabase::GetZoneUpdates() {
  4883. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Looking for Login Zone Updates...");
  4884. MutexMap<int32, LoginZoneUpdate>* ret = 0;
  4885. LoginZoneUpdate update;
  4886. Query query;
  4887. MYSQL_ROW row;
  4888. int32 count = 0;
  4889. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name, description FROM zones where login_checksum != crc32(crc32(id) + crc32(name) + crc32(file) + crc32(description))");
  4890. while(result && (row = mysql_fetch_row(result))) {
  4891. if(row[0] && row[1]) {
  4892. LogWrite(INIT__LOGIN_DEBUG, 5, "Login", "Found update for zone_id %i!", atoi(row[0]));
  4893. if(!ret)
  4894. ret = new MutexMap<int32, LoginZoneUpdate>();
  4895. update.name = string(row[1]);
  4896. if(row[2])
  4897. update.description = string(row[2]);
  4898. else
  4899. update.description = "";
  4900. ret->Put(atoi(row[0]), update);
  4901. count++;
  4902. }
  4903. }
  4904. if(count)
  4905. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Found %i Login Zone Update%s...", count, count == 1 ? "" : "s");
  4906. return ret;
  4907. }
  4908. void WorldDatabase::LoadLocationGrids(ZoneServer* zone) {
  4909. if (zone) {
  4910. Query query;
  4911. MYSQL_ROW row;
  4912. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `grid_id`, `name`, `include_y`, `discovery` FROM `locations` WHERE `zone_id`=%u", zone->GetZoneID());
  4913. while (result && (row = mysql_fetch_row(result))) {
  4914. LocationGrid* grid = new LocationGrid;
  4915. grid->id = atoul(row[0]);
  4916. grid->grid_id = atoul(row[1]);
  4917. grid->name = string(row[2]);
  4918. grid->include_y = (atoi(row[3]) == 1);
  4919. grid->discovery = (atoi(row[4]) == 1);
  4920. if (LoadLocationGridLocations(grid))
  4921. zone->AddLocationGrid(grid);
  4922. else
  4923. safe_delete(grid);
  4924. }
  4925. }
  4926. }
  4927. bool WorldDatabase::LoadLocationGridLocations(LocationGrid* grid) {
  4928. bool ret = false;
  4929. if (grid) {
  4930. Query query;
  4931. MYSQL_ROW row;
  4932. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `x`, `y`, `z` FROM `location_details` WHERE `location_id`=%u", grid->id);
  4933. if (result->row_count >= 3) {
  4934. while (result && (row = mysql_fetch_row(result))) {
  4935. Location* location = new Location;
  4936. location->id = atoul(row[0]);
  4937. location->x = atof(row[1]);
  4938. location->y = atof(row[2]);
  4939. location->z = atof(row[3]);
  4940. grid->locations.Add(location);
  4941. }
  4942. ret = true;
  4943. }
  4944. else
  4945. LogWrite(WORLD__ERROR, 0, "World", "Grid '%s' only has %u location(s). A minimum of 3 is needed.", grid->name.c_str(), result->row_count);
  4946. }
  4947. return ret;
  4948. }
  4949. int32 WorldDatabase::CreateLocation(int32 zone_id, int32 grid_id, const char* name, bool include_y) {
  4950. int32 ret = 0;
  4951. if (name && strlen(name) > 0) {
  4952. Query query;
  4953. query.RunQuery2(Q_INSERT, "INSERT INTO `locations` (`zone_id`, `grid_id`, `name`, `include_y`) VALUES (%u, %u, '%s', %u)", zone_id, grid_id, name, include_y == true ? 1 : 0);
  4954. ret = query.GetLastInsertedID();
  4955. }
  4956. return ret;
  4957. }
  4958. bool WorldDatabase::AddLocationPoint(int32 location_id, float x, float y, float z) {
  4959. bool ret = false;
  4960. if (LocationExists(location_id)) {
  4961. Query query;
  4962. query.RunQuery2(Q_INSERT, "INSERT INTO `location_details` (`location_id`, `x`, `y`, `z`) VALUES (%u, %f, %f, %f)", location_id, x, y, z);
  4963. ret = true;
  4964. }
  4965. return ret;
  4966. }
  4967. bool WorldDatabase::DeleteLocation(int32 location_id) {
  4968. bool ret = false;
  4969. if (LocationExists(location_id)) {
  4970. Query query;
  4971. query.RunQuery2(Q_DELETE, "DELETE FROM `locations` WHERE `id`=%u", location_id);
  4972. ret = true;
  4973. }
  4974. return ret;
  4975. }
  4976. bool WorldDatabase::DeleteLocationPoint(int32 location_point_id) {
  4977. Query query;
  4978. query.RunQuery2(Q_DELETE, "DELETE FROM `location_details` WHERE `id`=%u", location_point_id);
  4979. return true;
  4980. }
  4981. void WorldDatabase::ListLocations(Client* client) {
  4982. if (client) {
  4983. Query query;
  4984. MYSQL_ROW row;
  4985. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Listing all locations:");
  4986. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `zone_id`, `grid_id`, `name` FROM `locations`");
  4987. while (result && (row = mysql_fetch_row(result))) {
  4988. int32 id = atoul(row[0]);
  4989. int32 zone_id = atoul(row[1]);
  4990. int32 grid_id = atoul(row[2]);
  4991. const char* name = row[3];
  4992. client->Message(CHANNEL_COLOR_YELLOW, "%u) Zone ID: %u Grid ID:%u Name: '%s'", id, zone_id, grid_id, name);
  4993. }
  4994. }
  4995. }
  4996. void WorldDatabase::ListLocationPoints(Client* client, int32 location_id) {
  4997. if (client) {
  4998. if (LocationExists(location_id)) {
  4999. Query query;
  5000. client->Message(CHANNEL_COLOR_YELLOW, "Listing all points for location ID %u:", location_id);
  5001. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `x`, `y`, `z` FROM `location_details` WHERE `location_id`=%u", location_id);
  5002. MYSQL_ROW row;
  5003. while (result && (row = mysql_fetch_row(result))) {
  5004. int32 id = atoul(row[0]);
  5005. float x = atof(row[1]);
  5006. float y = atof(row[2]);
  5007. float z = atof(row[3]);
  5008. client->Message(CHANNEL_COLOR_YELLOW, "%u) (%f, %f, %f)", id, x, y, z);
  5009. }
  5010. }
  5011. else
  5012. client->Message(CHANNEL_COLOR_YELLOW, "A location with ID %u does not exist", location_id);
  5013. }
  5014. }
  5015. bool WorldDatabase::LocationExists(int32 location_id) {
  5016. bool ret = false;
  5017. Query query;
  5018. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT COUNT(id) FROM `locations` WHERE `id`=%u", location_id);
  5019. MYSQL_ROW row;
  5020. if (result && (row = mysql_fetch_row(result))) {
  5021. if (atoul(row[0]) > 0)
  5022. ret = true;
  5023. }
  5024. return ret;
  5025. }
  5026. bool WorldDatabase::GetTableVersions(vector<TableVersion*>* table_versions) {
  5027. DatabaseResult result;
  5028. TableVersion *table_version;
  5029. bool success;
  5030. //don't treat 1146 (table not found) as an error since the patch server will create it
  5031. database_new.SetIgnoredErrno(1146);
  5032. success = database_new.Select(&result, "SELECT `name`,`version`,`download_version`\n"
  5033. "FROM `table_versions`\n");
  5034. database_new.RemoveIgnoredErrno(1146);
  5035. if (!success)
  5036. return false;
  5037. while (result.Next()) {
  5038. table_version = (TableVersion *)malloc(sizeof(*table_version));
  5039. table_version->name_len = (unsigned int)strlcpy(table_version->name, result.GetString(0), sizeof(table_version->name));
  5040. table_version->version = result.GetInt32(1);
  5041. table_version->data_version = result.GetInt32(2);
  5042. table_versions->push_back(table_version);
  5043. }
  5044. return true;
  5045. }
  5046. bool WorldDatabase::QueriesFromFile(const char * file) {
  5047. return database_new.QueriesFromFile(file);
  5048. }
  5049. bool WorldDatabase::CheckBannedIPs(const char* loginIP)
  5050. {
  5051. // til you build the table, all IPs are allowed
  5052. return false;
  5053. }
  5054. void WorldDatabase::LoadTitles(){
  5055. int32 index = 0;
  5056. Query query;
  5057. MYSQL_ROW row;
  5058. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, title, prefix FROM titles");
  5059. if(result && mysql_num_rows(result) > 0){
  5060. Title* title = 0;
  5061. while(result && (row = mysql_fetch_row(result))){
  5062. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading Title '%s' (%u), Prefix: %i, Index: %u", row[1], atoul(row[0]), atoi(row[2]), index);
  5063. title = new Title;
  5064. title->SetID(index);
  5065. title->SetName(row[1]);
  5066. title->SetPrefix(atoi(row[2]));
  5067. master_titles_list.AddTitle(title);
  5068. index++;
  5069. }
  5070. }
  5071. LogWrite(WORLD__DEBUG, 0, "World", "\tLoaded %u Title%s", index, index == 1 ? "" : "s");
  5072. }
  5073. int32 WorldDatabase::LoadCharacterTitles(int32 char_id, Player *player){
  5074. LogWrite(WORLD__DEBUG, 0, "World", "Loading Titles for player '%s'...", player->GetName());
  5075. Query query;
  5076. MYSQL_ROW row;
  5077. int32 index = 0;
  5078. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT title_id, title, prefix FROM character_titles, titles WHERE character_titles.title_id = titles.id AND character_titles.char_id = %u", char_id);
  5079. if(result && mysql_num_rows(result) > 0){
  5080. while(result && (row = mysql_fetch_row(result))){
  5081. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading Title ID: %u, Title: '%s' Index: %u", atoul(row[0]), row[1], index);
  5082. player->AddTitle(index, row[1], atoi(row[2]));
  5083. index++;
  5084. }
  5085. }
  5086. return index;
  5087. }
  5088. sint16 WorldDatabase::GetCharPrefixIndex(int32 char_id, Player *player){
  5089. LogWrite(PLAYER__DEBUG, 0, "Player", "Getting current title index for player '%s'...", player->GetName());
  5090. Query query;
  5091. MYSQL_ROW row;
  5092. sint16 ret = -1;
  5093. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT prefix_title FROM character_details WHERE char_id = %u", char_id);
  5094. if(result && mysql_num_rows(result) > 0)
  5095. while(result && (row = mysql_fetch_row(result))){
  5096. ret = atoi(row[0]);
  5097. LogWrite(PLAYER__DEBUG, 5, "Player", "\tPrefix Index: %i", ret);
  5098. }
  5099. return ret;
  5100. }
  5101. sint16 WorldDatabase::GetCharSuffixIndex(int32 char_id, Player *player){
  5102. LogWrite(PLAYER__DEBUG, 0, "Player", "Getting current title index for player '%s'...", player->GetName());
  5103. Query query;
  5104. MYSQL_ROW row;
  5105. sint16 ret = -1;
  5106. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT suffix_title FROM character_details WHERE char_id = %u", char_id);
  5107. if(result && mysql_num_rows(result) > 0)
  5108. while(result && (row = mysql_fetch_row(result))){
  5109. ret = atoi(row[0]);
  5110. LogWrite(PLAYER__DEBUG, 5, "Player", "\tSuffix Index: %i", ret);
  5111. }
  5112. return ret;
  5113. }
  5114. void WorldDatabase::SaveCharPrefixIndex(sint16 index, int32 char_id, Client *client){
  5115. Query query;
  5116. query.RunQuery2(Q_UPDATE, "UPDATE character_details SET prefix_title = %i WHERE char_id = %u", index, client->GetCharacterID());
  5117. LogWrite(PLAYER__DEBUG, 0, "Player", "Saving Prefix Index %i for player '%s'...", index, client->GetPlayer()->GetName());
  5118. }
  5119. void WorldDatabase::SaveCharSuffixIndex(sint16 index, int32 char_id, Client *client){
  5120. Query query;
  5121. query.RunQuery2(Q_SELECT, "UPDATE character_details SET suffix_title = %i WHERE char_id = %u", index, client->GetCharacterID());
  5122. LogWrite(PLAYER__DEBUG, 0, "Player", "Saving Suffix Index %i for player '%s'...", index, client->GetPlayer()->GetName());
  5123. }
  5124. void WorldDatabase::LoadLanguages()
  5125. {
  5126. int32 count = 0;
  5127. Query query;
  5128. MYSQL_ROW row;
  5129. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, language FROM languages");
  5130. if(result && mysql_num_rows(result) > 0)
  5131. {
  5132. Language* language = 0;
  5133. while(result && (row = mysql_fetch_row(result)))
  5134. {
  5135. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading language '%s' , ID: %u", row[1], atoul(row[0]));
  5136. language = new Language;
  5137. language->SetID(atoul(row[0]));
  5138. language->SetName(row[1]);
  5139. master_languages_list.AddLanguage(language);
  5140. count++;
  5141. }
  5142. }
  5143. LogWrite(WORLD__DEBUG, 0, "World", "\tLoaded %u Language%s", count, count == 1 ? "" : "s");
  5144. }
  5145. int32 WorldDatabase::LoadCharacterLanguages(int32 char_id, Player *player)
  5146. {
  5147. LogWrite(WORLD__DEBUG, 0, "World", "Loading Languages for player '%s'...", player->GetName());
  5148. Query query;
  5149. MYSQL_ROW row;
  5150. int32 count = 0;
  5151. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT language_id, language FROM character_languages, languages WHERE character_languages.language_id = languages.id AND character_languages.char_id = %u", char_id);
  5152. if(result && mysql_num_rows(result) > 0)
  5153. {
  5154. while(result && (row = mysql_fetch_row(result)))
  5155. {
  5156. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading Language ID: %u, Language: '%s'", atoul(row[0]), row[1]);
  5157. player->AddLanguage(atoul(row[0]), row[1]);
  5158. count++;
  5159. }
  5160. }
  5161. return count;
  5162. }
  5163. int16 WorldDatabase::GetCharacterCurrentLang(int32 char_id, Player *player)
  5164. {
  5165. LogWrite(PLAYER__DEBUG, 0, "Player", "Getting current language for player '%s'...", player->GetName());
  5166. Query query;
  5167. MYSQL_ROW row;
  5168. int16 ret = 0;
  5169. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT current_language FROM character_details WHERE char_id = %u", char_id);
  5170. if(result && mysql_num_rows(result) > 0)
  5171. while(result && (row = mysql_fetch_row(result)))
  5172. {
  5173. ret = atoi(row[0]);
  5174. LogWrite(PLAYER__DEBUG, 5, "Player", "\tLanguage ID: %i", ret);
  5175. }
  5176. return ret;
  5177. }
  5178. void WorldDatabase::SaveCharacterCurrentLang(int32 id, int32 char_id, Client *client)
  5179. {
  5180. Query query;
  5181. query.RunQuery2(Q_UPDATE, "UPDATE character_details SET current_language = %i WHERE char_id = %u", id, char_id);
  5182. LogWrite(PLAYER__DEBUG, 3, "Player", "Saving current language ID %i for player '%s'...", id, client->GetPlayer()->GetName());
  5183. }
  5184. void WorldDatabase::SaveCharacterLang(int32 char_id, int32 lang_id) {
  5185. if (!database_new.Query("INSERT INTO character_languages (char_id, language_id) VALUES (%u, %u)", char_id, lang_id))
  5186. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5187. }
  5188. // JA - this is not used yet, lots more to consider for storing player history
  5189. void WorldDatabase::LoadCharacterHistory(int32 char_id, Player *player)
  5190. {
  5191. DatabaseResult result;
  5192. // Use -1 on type and subtype to turn the enum into an int and make it a 0 index
  5193. if (!database_new.Select(&result, "SELECT type-1, subtype-1, value, value2, location, event_id, event_date FROM character_history WHERE char_id = %u", char_id)) {
  5194. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5195. return;
  5196. }
  5197. while (result.Next()) {
  5198. int8 type = result.GetInt8(0);
  5199. int8 subtype = result.GetInt8(1);
  5200. HistoryData* hd = new HistoryData;
  5201. hd->Value = result.GetInt32(2);
  5202. hd->Value2 = result.GetInt32(3);
  5203. strcpy(hd->Location, result.GetString(4));
  5204. // skipped event id as use for it has not been determined yet
  5205. hd->EventDate = result.GetInt32(6);
  5206. player->LoadPlayerHistory(type, subtype, hd);
  5207. }
  5208. }
  5209. void WorldDatabase::LoadSpellErrors() {
  5210. Query query;
  5211. MYSQL_ROW row;
  5212. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `version`, `error_index`, `value` FROM `spell_error_versions`");
  5213. if (result && mysql_num_rows(result) > 0) {
  5214. while ((row = mysql_fetch_row(result))) {
  5215. master_spell_list.AddSpellError(atoi(row[0]), atoi(row[1]), atoi(row[2]));
  5216. }
  5217. }
  5218. }
  5219. void WorldDatabase::SaveCharacterHistory(Player* player, int8 type, int8 subtype, int32 value, int32 value2, char* location, int32 event_date) {
  5220. string str_type;
  5221. string str_subtype;
  5222. switch (type) {
  5223. case HISTORY_TYPE_NONE:
  5224. str_type = "None";
  5225. break;
  5226. case HISTORY_TYPE_DEATH:
  5227. str_type = "Death";
  5228. break;
  5229. case HISTORY_TYPE_DISCOVERY:
  5230. str_type = "Discovery";
  5231. break;
  5232. case HISTORY_TYPE_XP:
  5233. str_type = "XP";
  5234. break;
  5235. default:
  5236. LogWrite(PLAYER__ERROR, 0, "Player", "WorldDatabase::SaveCharacterHistory() - Invalid history type given (%i) with subtype (%i), character history NOT saved.", type, subtype);
  5237. return;
  5238. }
  5239. switch (subtype) {
  5240. case HISTORY_SUBTYPE_NONE:
  5241. str_subtype = "None";
  5242. break;
  5243. case HISTORY_SUBTYPE_ADVENTURE:
  5244. str_subtype = "Adventure";
  5245. break;
  5246. case HISTORY_SUBTYPE_TRADESKILL:
  5247. str_subtype = "Tradeskill";
  5248. break;
  5249. case HISTORY_SUBTYPE_QUEST:
  5250. str_subtype = "Quest";
  5251. break;
  5252. case HISTORY_SUBTYPE_AA:
  5253. str_subtype = "AA";
  5254. break;
  5255. case HISTORY_SUBTYPE_ITEM:
  5256. str_subtype = "Item";
  5257. break;
  5258. case HISTORY_SUBTYPE_LOCATION:
  5259. str_subtype = "Location";
  5260. break;
  5261. default:
  5262. LogWrite(PLAYER__ERROR, 0, "Player", "WorldDatabase::SaveCharacterHistory() - Invalid history sub type given (%i) with type (%i), character history NOT saved.", subtype, type);
  5263. return;
  5264. }
  5265. LogWrite(PLAYER__INFO, 1, "Player", "Saving character history, type = %s (%i) subtype = %s (%i)", (char*)str_type.c_str(), type, (char*)str_subtype.c_str(), subtype);
  5266. Query query;
  5267. query.AddQueryAsync(player->GetCharacterID(), this, Q_REPLACE, "replace into character_history (char_id, type, subtype, value, value2, location, event_date) values (%u, '%s', '%s', %i, %i, '%s', %u)",
  5268. player->GetCharacterID(), str_type.c_str(), str_subtype.c_str(), value, value2, location, event_date);
  5269. }
  5270. void WorldDatabase::LoadTransportMaps(ZoneServer* zone) {
  5271. int32 total = 0;
  5272. LogWrite(TRANSPORT__DEBUG, 0, "Transport", "-Loading Transporter Maps...");
  5273. zone->DeleteTransporterMaps();
  5274. Query query;
  5275. MYSQL_ROW row;
  5276. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `transport_id`, `map_name` FROM `transport_maps`");
  5277. if(result) {
  5278. while(result && (row = mysql_fetch_row(result))){
  5279. zone->AddTransportMap(atoul(row[0]), string(row[1]));
  5280. total++;
  5281. }
  5282. }
  5283. LogWrite(TRANSPORT__DEBUG, 0, "Transport", "--Loaded %i Transporter Maps", total);
  5284. }
  5285. bool WorldDatabase::LoadSign(ZoneServer* zone, int32 spawn_id) {
  5286. Sign* sign = 0;
  5287. int32 id = 0;
  5288. DatabaseResult result;
  5289. database_new.Select(&result, "SELECT ss.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, ss.widget_id, ss.widget_x, ss.widget_y, ss.widget_z, s.command_primary, s.command_secondary, s.collision_radius, ss.icon, ss.type, ss.title, ss.description, ss.sign_distance, ss.zone_id, ss.zone_x, ss.zone_y, ss.zone_z, ss.zone_heading, ss.include_heading, ss.include_location, s.transport_id, s.size_offset, s.display_hand_icon, s.visual_state, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  5290. "FROM spawn s\n"
  5291. "INNER JOIN spawn_signs ss\n"
  5292. "ON ss.spawn_id = s.id\n"
  5293. "WHERE s.id = %u\n",
  5294. spawn_id);
  5295. if (result.GetNumRows() > 0 && result.Next()) {
  5296. id = result.GetInt32(0);
  5297. sign = new Sign();
  5298. sign->SetDatabaseID(id);
  5299. strcpy(sign->appearance.name, result.GetString(1));
  5300. sign->appearance.model_type = result.GetInt16(2);
  5301. sign->SetSize(result.GetInt16(3));
  5302. sign->appearance.show_command_icon = result.GetInt8(4);
  5303. sign->SetWidgetID(result.GetInt32(5));
  5304. sign->SetWidgetX(result.GetFloat(6));
  5305. sign->SetWidgetY(result.GetFloat(7));
  5306. sign->SetWidgetZ(result.GetFloat(8));
  5307. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(9));
  5308. if(primary_command_list){
  5309. sign->SetPrimaryCommands(primary_command_list);
  5310. sign->primary_command_list_id = result.GetInt32(9);
  5311. }
  5312. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(10));
  5313. if (secondary_command_list) {
  5314. sign->SetSecondaryCommands(secondary_command_list);
  5315. sign->secondary_command_list_id = result.GetInt32(10);
  5316. }
  5317. sign->appearance.pos.collision_radius = result.GetInt16(11);
  5318. sign->SetSignIcon(result.GetInt8(12));
  5319. if(strncasecmp(result.GetString(13), "Generic", 7) == 0)
  5320. sign->SetSignType(SIGN_TYPE_GENERIC);
  5321. else if(strncasecmp(result.GetString(13), "Zone", 4) == 0)
  5322. sign->SetSignType(SIGN_TYPE_ZONE);
  5323. sign->SetSignTitle(result.GetString(14));
  5324. sign->SetSignDescription(result.GetString(15));
  5325. sign->SetSignDistance(result.GetFloat(16));
  5326. sign->SetSignZoneID(result.GetInt32(17));
  5327. sign->SetSignZoneX(result.GetFloat(18));
  5328. sign->SetSignZoneY(result.GetFloat(19));
  5329. sign->SetSignZoneZ(result.GetFloat(20));
  5330. sign->SetSignZoneHeading(result.GetFloat(21));
  5331. sign->SetIncludeHeading(result.GetInt8(22) == 1);
  5332. sign->SetIncludeLocation(result.GetInt8(23) == 1);
  5333. sign->SetTransporterID(result.GetInt32(24));
  5334. sign->SetSizeOffset(result.GetInt8(25));
  5335. sign->appearance.display_hand_icon = result.GetInt8(26);
  5336. sign->SetVisualState(result.GetInt16(27));
  5337. sign->SetSoundsDisabled(result.GetInt8(28));
  5338. sign->SetMerchantLevelRange(result.GetInt32(29), result.GetInt32(30));
  5339. zone->AddSign(id, sign);
  5340. LogWrite(SIGN__DEBUG, 0, "Sign", "Loaded Sign: '%s' (%u).", sign->appearance.name, spawn_id);
  5341. return true;
  5342. }
  5343. LogWrite(SIGN__DEBUG, 0, "Sign", "Unable to find a sign for spawn id of %u", spawn_id);
  5344. return false;
  5345. }
  5346. bool WorldDatabase::LoadWidget(ZoneServer* zone, int32 spawn_id) {
  5347. Widget* widget = 0;
  5348. int32 id = 0;
  5349. DatabaseResult result;
  5350. database_new.Select(&result, "SELECT sw.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, sw.widget_id, sw.widget_x, sw.widget_y, sw.widget_z, s.command_primary, s.command_secondary, s.collision_radius, sw.include_heading, sw.include_location, sw.icon, sw.type, sw.open_heading, sw.open_y, sw.action_spawn_id, sw.open_sound_file, sw.close_sound_file, sw.open_duration, sw.closed_heading, sw.linked_spawn_id, sw.close_y, s.transport_id, s.size_offset, sw.house_id, sw.open_x, sw.open_z, sw.close_x, sw.close_z, s.display_hand_icon, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  5351. "FROM spawn s\n"
  5352. "INNER JOIN spawn_widgets sw\n"
  5353. "ON sw.spawn_id = s.id\n"
  5354. "WHERE s.id = %u",
  5355. spawn_id);
  5356. if (result.GetNumRows() > 0 && result.Next()) {
  5357. id = result.GetInt32(0);
  5358. widget = new Widget();
  5359. widget->SetDatabaseID(id);
  5360. strcpy(widget->appearance.name, result.GetString(1));
  5361. widget->appearance.model_type = result.GetInt16(2);
  5362. widget->SetSize(result.GetInt16(3));
  5363. widget->appearance.show_command_icon = result.GetInt8(4);
  5364. widget->SetWidgetID(result.GetInt32(5));
  5365. widget->SetWidgetX(result.GetFloat(6));
  5366. widget->SetWidgetY(result.GetFloat(7));
  5367. widget->SetWidgetZ(result.GetFloat(8));
  5368. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(9));
  5369. if(primary_command_list){
  5370. widget->SetPrimaryCommands(primary_command_list);
  5371. widget->primary_command_list_id = result.GetInt32(9);
  5372. }
  5373. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(10));
  5374. if (secondary_command_list) {
  5375. widget->SetSecondaryCommands(secondary_command_list);
  5376. widget->secondary_command_list_id = result.GetInt32(10);
  5377. }
  5378. widget->appearance.pos.collision_radius = result.GetInt16(11);
  5379. widget->SetIncludeHeading(result.GetInt8(12) == 1);
  5380. widget->SetIncludeLocation(result.GetInt8(13) == 1);
  5381. widget->SetWidgetIcon(result.GetInt8(14));
  5382. if(strncasecmp(result.GetString(15),"Generic", 7) == 0)
  5383. widget->SetWidgetType(WIDGET_TYPE_GENERIC);
  5384. else if(strncasecmp(result.GetString(15),"Door", 4) == 0)
  5385. widget->SetWidgetType(WIDGET_TYPE_DOOR);
  5386. widget->SetOpenHeading(result.GetFloat(16));
  5387. widget->SetOpenY(result.GetFloat(17));
  5388. widget->SetActionSpawnID(result.GetInt32(18));
  5389. if(!result.IsNull(19) && strlen(result.GetString(19)) > 5)
  5390. widget->SetOpenSound(result.GetString(19));
  5391. if(!result.IsNull(20) && strlen(result.GetString(20)) > 5)
  5392. widget->SetCloseSound(result.GetString(20));
  5393. widget->SetOpenDuration(result.GetInt16(21));
  5394. widget->SetClosedHeading(result.GetFloat(22));
  5395. widget->SetLinkedSpawnID(result.GetInt32(23));
  5396. widget->SetCloseY(result.GetFloat(24));
  5397. widget->SetTransporterID(result.GetInt32(25));
  5398. widget->SetSizeOffset(result.GetInt8(26));
  5399. widget->SetHouseID(result.GetInt32(27));
  5400. widget->SetOpenX(result.GetFloat(28));
  5401. widget->SetOpenZ(result.GetFloat(29));
  5402. widget->SetCloseX(result.GetFloat(30));
  5403. widget->SetCloseZ(result.GetFloat(31));
  5404. widget->appearance.display_hand_icon = result.GetInt8(32);
  5405. widget->SetSoundsDisabled(result.GetInt8(33));
  5406. widget->SetMerchantLevelRange(result.GetInt32(34), result.GetInt32(35));
  5407. zone->AddWidget(id, widget);
  5408. LogWrite(WIDGET__DEBUG, 0, "Widget", "Loaded Widget: '%s' (%u).", widget->appearance.name, spawn_id);
  5409. return true;
  5410. }
  5411. LogWrite(WIDGET__DEBUG, 0, "Widget", "Unable to find a widget for spawn id of %u", spawn_id);
  5412. return false;
  5413. }
  5414. bool WorldDatabase::LoadObject(ZoneServer* zone, int32 spawn_id) {
  5415. Object* object = 0;
  5416. int32 id = 0;
  5417. DatabaseResult result;
  5418. database_new.Select(&result, "SELECT so.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, s.transport_id, s.size_offset, so.device_id, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  5419. "FROM spawn s\n"
  5420. "INNER JOIN spawn_objects so\n"
  5421. "ON so.spawn_id = s.id\n"
  5422. "WHERE s.id = %u",
  5423. spawn_id);
  5424. if (result.GetNumRows() > 0 && result.Next()) {
  5425. id = result.GetInt32(0);
  5426. object = new Object();
  5427. object->SetDatabaseID(id);
  5428. strcpy(object->appearance.name, result.GetString(1));
  5429. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(4));
  5430. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(5));
  5431. if(primary_command_list){
  5432. object->SetPrimaryCommands(primary_command_list);
  5433. object->primary_command_list_id = result.GetInt32(4);
  5434. }
  5435. if(secondary_command_list){
  5436. object->SetSecondaryCommands(secondary_command_list);
  5437. object->secondary_command_list_id = result.GetInt32(5);
  5438. }
  5439. object->appearance.race = result.GetInt8(2);
  5440. object->appearance.model_type = result.GetInt16(3);
  5441. object->appearance.targetable = result.GetInt8(6);
  5442. object->size = result.GetInt16(7);
  5443. object->appearance.display_name = result.GetInt8(8);
  5444. object->appearance.visual_state = result.GetInt16(9);
  5445. object->appearance.attackable = result.GetInt8(10);
  5446. object->appearance.show_level = result.GetInt8(11);
  5447. object->appearance.show_command_icon = result.GetInt8(12);
  5448. object->appearance.display_hand_icon = result.GetInt8(13);
  5449. object->faction_id = result.GetInt32(14);
  5450. object->appearance.pos.collision_radius = result.GetInt16(15);
  5451. object->SetTransporterID(result.GetInt32(16));
  5452. object->SetSizeOffset(result.GetInt8(17));
  5453. object->SetDeviceID(result.GetInt8(18));
  5454. object->SetSoundsDisabled(result.GetInt8(19));
  5455. object->SetMerchantLevelRange(result.GetInt32(20), result.GetInt32(21));
  5456. zone->AddObject(id, object);
  5457. LogWrite(OBJECT__DEBUG, 0, "Object", "Loaded Object: '%s' (%u).", object->appearance.name, spawn_id);
  5458. return true;
  5459. }
  5460. LogWrite(OBJECT__DEBUG, 0, "Object", "Unable to find an object for spawn id of %u", spawn_id);
  5461. return false;
  5462. }
  5463. bool WorldDatabase::LoadGroundSpawn(ZoneServer* zone, int32 spawn_id) {
  5464. GroundSpawn* spawn = 0;
  5465. int32 id = 0;
  5466. DatabaseResult result;
  5467. database_new.Select(&result, "SELECT sg.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, sg.number_harvests, sg.num_attempts_per_harvest, sg.groundspawn_id, sg.collection_skill, s.size_offset\n"
  5468. "FROM spawn s\n"
  5469. "INNER JOIN spawn_ground sg\n"
  5470. "ON sg.spawn_id = s.id\n"
  5471. "WHERE s.id = %u",
  5472. spawn_id);
  5473. if (result.GetNumRows() > 0 && result.Next()) {
  5474. id = result.GetInt32(0);
  5475. spawn = new GroundSpawn();
  5476. spawn->SetDatabaseID(id);
  5477. strcpy(spawn->appearance.name, result.GetString(1));
  5478. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(4));
  5479. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(5));
  5480. if(primary_command_list){
  5481. spawn->SetPrimaryCommands(primary_command_list);
  5482. spawn->primary_command_list_id = result.GetInt32(4);
  5483. }
  5484. if(secondary_command_list){
  5485. spawn->SetSecondaryCommands(secondary_command_list);
  5486. spawn->secondary_command_list_id = result.GetInt32(5);
  5487. }
  5488. spawn->appearance.race = result.GetInt8(2);
  5489. spawn->appearance.model_type = result.GetInt16(3);
  5490. spawn->appearance.targetable = result.GetInt8(6);
  5491. spawn->size = result.GetInt16(7);
  5492. spawn->appearance.display_name = result.GetInt8(8);
  5493. spawn->appearance.visual_state = result.GetInt16(9);
  5494. spawn->appearance.attackable = result.GetInt8(10);
  5495. spawn->appearance.show_level = result.GetInt8(11);
  5496. spawn->appearance.show_command_icon = result.GetInt8(12);
  5497. spawn->appearance.display_hand_icon = result.GetInt8(13);
  5498. spawn->faction_id = result.GetInt32(14);
  5499. spawn->appearance.pos.collision_radius = result.GetInt16(15);
  5500. spawn->SetNumberHarvests(result.GetInt8(16));
  5501. spawn->SetAttemptsPerHarvest(result.GetInt8(17));
  5502. spawn->SetGroundSpawnEntryID(result.GetInt32(18));
  5503. spawn->SetCollectionSkill(result.GetString(19));
  5504. spawn->SetSizeOffset(result.GetInt8(20));
  5505. zone->AddGroundSpawn(id, spawn);
  5506. if (!zone->GetGroundSpawnEntries(spawn->GetGroundSpawnEntryID()))
  5507. LoadGroundSpawnEntry(zone, spawn->GetGroundSpawnEntryID());
  5508. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "Loaded Ground Spawn: '%s' (%u).", spawn->appearance.name, spawn_id);
  5509. return true;
  5510. }
  5511. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "Unable to find a ground spawn for spawn id of %u", spawn_id);
  5512. return false;
  5513. }
  5514. void WorldDatabase::LoadGroundSpawnItems(ZoneServer* zone, int32 entry_id) {
  5515. DatabaseResult result;
  5516. database_new.Select(&result, "SELECT item_id, is_rare, grid_id\n"
  5517. "FROM groundspawn_items\n"
  5518. "WHERE groundspawn_id = %u",
  5519. entry_id);
  5520. if (result.GetNumRows() > 0 && result.Next()) {
  5521. zone->AddGroundSpawnItem(entry_id, result.GetInt32(0), result.GetInt8(1), result.GetInt32(2));
  5522. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn Items: ID: %u\n", entry_id);
  5523. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---item: %ul, rare: %i, grid: %ul", result.GetInt32(0), result.GetInt8(1), result.GetInt32(2));
  5524. }
  5525. }
  5526. void WorldDatabase::LoadGroundSpawnEntry(ZoneServer* zone, int32 entry_id) {
  5527. DatabaseResult result;
  5528. database_new.Select(&result, "SELECT min_skill_level, min_adventure_level, bonus_table, harvest1, harvest3, harvest5, harvest_imbue, harvest_rare, harvest10, harvest_coin\n"
  5529. "FROM groundspawns\n"
  5530. "WHERE enabled = 1 AND groundspawn_id = %u",
  5531. entry_id);
  5532. if (result.GetNumRows() > 0 && result.Next()) {
  5533. // this is getting ridonkulous...
  5534. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn ID: %u\n" \
  5535. "---min_skill_level: %i, min_adventure_level: %i, bonus_table: %i\n" \
  5536. "---harvest1: %.2f, harvest3: %.2f, harvest5: %.2f\n" \
  5537. "---harvest_imbue: %.2f, harvest_rare: %.2f, harvest10: %.2f\n" \
  5538. "---harvest_coin: %u", entry_id, result.GetInt16(0), result.GetInt16(1), result.GetInt8(2), result.GetFloat(3), result.GetFloat(4), result.GetFloat(5), result.GetFloat(6), result.GetFloat(7), result.GetFloat(8), result.GetInt32(9));
  5539. zone->AddGroundSpawnEntry(entry_id, result.GetInt16(0), result.GetInt16(1), result.GetInt8(2), result.GetFloat(3), result.GetFloat(4), result.GetFloat(5), result.GetFloat(6), result.GetFloat(7), result.GetFloat(8), result.GetInt32(9));
  5540. LoadGroundSpawnItems(zone, entry_id);
  5541. }
  5542. }
  5543. bool WorldDatabase::LoadNPC(ZoneServer* zone, int32 spawn_id) {
  5544. NPC* npc = nullptr;
  5545. int32 id = 0;
  5546. DatabaseResult result;
  5547. database_new.Select(&result, "SELECT npc.spawn_id, s.name, npc.min_level, npc.max_level, npc.enc_level, s.race, s.model_type, npc.class_, npc.gender, s.command_primary, s.command_secondary, s.show_name, npc.min_group_size, npc.max_group_size, npc.hair_type_id, npc.facial_hair_type_id, npc.wing_type_id, npc.chest_type_id, npc.legs_type_id, npc.soga_hair_type_id, npc.soga_facial_hair_type_id, s.attackable, s.show_level, s.targetable, s.show_command_icon, s.display_hand_icon, s.hp, s.power, s.size, s.collision_radius, npc.action_state, s.visual_state, npc.mood_state, npc.initial_state, npc.activity_status, s.faction_id, s.sub_title, s.merchant_id, s.merchant_type, s.size_offset, npc.attack_type, npc.ai_strategy+0, npc.spell_list_id, npc.secondary_spell_list_id, npc.skill_list_id, npc.secondary_skill_list_id, npc.equipment_list_id, npc.str, npc.sta, npc.wis, npc.intel, npc.agi, npc.heat, npc.cold, npc.magic, npc.mental, npc.divine, npc.disease, npc.poison, npc.aggro_radius, npc.cast_percentage, npc.randomize, npc.soga_model_type, npc.heroic_flag, npc.alignment, npc.elemental, npc.arcane, npc.noxious, s.savagery, s.dissonance, npc.hide_hood, npc.emote_state, s.prefix, s.suffix, s.last_name, s.disable_sounds, s.merchant_min_level, s.merchant_max_level\n"
  5548. "FROM spawn s\n"
  5549. "INNER JOIN spawn_npcs npc\n"
  5550. "ON npc.spawn_id = s.id\n"
  5551. "WHERE s.id = %u",
  5552. spawn_id);
  5553. if (result.GetNumRows() > 0 && result.Next()) {
  5554. id = result.GetInt32(0);
  5555. npc = new NPC();
  5556. npc->SetDatabaseID(id);
  5557. strcpy(npc->appearance.name, result.GetString(1));
  5558. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(9));
  5559. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(10));
  5560. if(primary_command_list){
  5561. npc->SetPrimaryCommands(primary_command_list);
  5562. npc->primary_command_list_id = result.GetInt32(9);
  5563. }
  5564. if(secondary_command_list){
  5565. npc->SetSecondaryCommands(secondary_command_list);
  5566. npc->secondary_command_list_id = result.GetInt32(10);
  5567. }
  5568. npc->appearance.min_level = result.GetInt8(2);
  5569. npc->appearance.max_level = result.GetInt8(3);
  5570. npc->appearance.level = result.GetInt8(2);
  5571. npc->appearance.encounter_level = result.GetInt8(4);
  5572. npc->appearance.race = result.GetInt8(5);
  5573. //npc->appearance.lua_race_id = result.GetInt16(74);
  5574. npc->appearance.model_type = result.GetInt16(6);
  5575. npc->appearance.soga_model_type = result.GetInt16(62);
  5576. npc->appearance.adventure_class = result.GetInt8(7);
  5577. npc->appearance.gender = result.GetInt8(8);
  5578. npc->appearance.display_name = result.GetInt8(11);
  5579. npc->features.hair_type = result.GetInt16(14);
  5580. npc->features.hair_face_type = result.GetInt16(15);
  5581. npc->features.wing_type = result.GetInt16(16);
  5582. npc->features.chest_type = result.GetInt16(17);
  5583. npc->features.legs_type = result.GetInt16(18);
  5584. npc->features.soga_hair_type = result.GetInt16(19);
  5585. npc->features.soga_hair_face_type = result.GetInt16(20);
  5586. npc->appearance.attackable = result.GetInt8(21);
  5587. npc->appearance.show_level = result.GetInt8(22);
  5588. npc->appearance.targetable = result.GetInt8(23);
  5589. npc->appearance.show_command_icon = result.GetInt8(24);
  5590. npc->appearance.display_hand_icon = result.GetInt8(25);
  5591. npc->appearance.hide_hood = result.GetInt8(70);
  5592. npc->appearance.randomize = result.GetInt32(61);
  5593. npc->SetTotalHP(result.GetInt32(26));
  5594. npc->SetTotalPower(result.GetInt32(27));
  5595. npc->SetHP(npc->GetTotalHP());
  5596. npc->SetPower(npc->GetTotalPower());
  5597. if(npc->GetTotalHP() == 0){
  5598. npc->SetTotalHP(15*npc->GetLevel() + 1);
  5599. npc->SetHP(15*npc->GetLevel() + 1);
  5600. }
  5601. if(npc->GetTotalPower() == 0){
  5602. npc->SetTotalPower(15*npc->GetLevel() + 1);
  5603. npc->SetPower(15*npc->GetLevel() + 1);
  5604. }
  5605. npc->size = result.GetInt16(28);
  5606. npc->appearance.pos.collision_radius = result.GetInt16(29);
  5607. npc->appearance.action_state = result.GetInt16(30);
  5608. npc->appearance.visual_state = result.GetInt16(31);
  5609. npc->appearance.mood_state = result.GetInt16(32);
  5610. npc->appearance.emote_state = result.GetInt16(71);
  5611. npc->appearance.pos.state = result.GetInt16(33);
  5612. npc->appearance.activity_status = result.GetInt16(34);
  5613. npc->faction_id = result.GetInt32(35);
  5614. if(!result.IsNull(36)){
  5615. if(strlen(result.GetString(36)) < sizeof(npc->appearance.sub_title))
  5616. strcpy(npc->appearance.sub_title, result.GetString(36));
  5617. else
  5618. strncpy(npc->appearance.sub_title, result.GetString(36), sizeof(npc->appearance.sub_title));
  5619. }
  5620. npc->SetMerchantID(result.GetInt32(37));
  5621. npc->SetMerchantType(result.GetInt8(38));
  5622. npc->SetSizeOffset(result.GetInt8(39));
  5623. npc->SetAttackType(result.GetInt8(40));
  5624. npc->SetAIStrategy(result.GetInt8(41));
  5625. npc->SetPrimarySpellList(result.GetInt32(42));
  5626. npc->SetSecondarySpellList(result.GetInt32(43));
  5627. npc->SetPrimarySkillList(result.GetInt32(44));
  5628. npc->SetSecondarySkillList(result.GetInt32(45));
  5629. npc->SetEquipmentListID(result.GetInt32(46));
  5630. InfoStruct* info = npc->GetInfoStruct();
  5631. info->str_base = result.GetInt16(47);
  5632. info->sta_base = result.GetInt16(48);
  5633. info->wis_base = result.GetInt16(49);
  5634. info->intel_base = result.GetInt16(50);
  5635. info->agi_base = result.GetInt16(51);
  5636. info->heat_base = result.GetInt16(52);
  5637. info->cold_base = result.GetInt16(53);
  5638. info->magic_base = result.GetInt16(54);
  5639. info->mental_base = result.GetInt16(55);
  5640. info->divine_base = result.GetInt16(56);
  5641. info->disease_base = result.GetInt16(57);
  5642. info->poison_base = result.GetInt16(58);
  5643. info->alignment = result.GetInt8(64);
  5644. npc->SetAggroRadius(result.GetFloat(59));
  5645. npc->SetCastPercentage(result.GetInt8(60));
  5646. npc->appearance.heroic_flag = result.GetInt8(63);
  5647. info->elemental_base = result.GetInt16(65);
  5648. info->arcane_base = result.GetInt16(66);
  5649. info->noxious_base = result.GetInt16(67);
  5650. npc->SetTotalSavagery(result.GetInt32(68));
  5651. npc->SetTotalDissonance(result.GetInt32(69));
  5652. npc->SetSavagery(npc->GetTotalSavagery());
  5653. npc->SetDissonance(npc->GetTotalDissonance());
  5654. if(npc->GetTotalSavagery() == 0){
  5655. npc->SetTotalSavagery(15*npc->GetLevel() + 1);
  5656. npc->SetSavagery(15*npc->GetLevel() + 1);
  5657. }
  5658. if(npc->GetTotalDissonance() == 0){
  5659. npc->SetTotalDissonance(15*npc->GetLevel() + 1);
  5660. npc->SetDissonance(15*npc->GetLevel() + 1);
  5661. }
  5662. npc->SetPrefixTitle(result.GetString(72));
  5663. npc->SetSuffixTitle(result.GetString(73));
  5664. npc->SetLastName(result.GetString(74));
  5665. npc->SetSoundsDisabled(result.GetInt8(75));
  5666. npc->SetMerchantLevelRange(result.GetInt32(76), result.GetInt32(77));
  5667. zone->AddNPC(id, npc);
  5668. //skipped spells/skills/equipment as it is all loaded, the following rely on a spawn to load
  5669. LoadAppearance(zone, spawn_id);
  5670. LoadNPCAppearanceEquipmentData(zone, spawn_id);
  5671. LogWrite(NPC__DEBUG, 0, "NPC", "Loaded NPC: '%s' (%u).", npc->appearance.name, spawn_id);
  5672. return true;
  5673. }
  5674. LogWrite(NPC__DEBUG, 0, "NPC", "Unable to find a npc for spawn id of %u", spawn_id);
  5675. return false;
  5676. }
  5677. void WorldDatabase::LoadAppearance(ZoneServer* zone, int32 spawn_id) {
  5678. Entity* entity = zone->GetNPC(spawn_id);
  5679. if (!entity)
  5680. return;
  5681. DatabaseResult result, result2;
  5682. map<string, int8> appearance_types;
  5683. map<int32, map<int8, EQ2_Color> > appearance_colors;
  5684. EQ2_Color color;
  5685. color.red = 0;
  5686. color.green = 0;
  5687. color.blue = 0;
  5688. string type;
  5689. database_new.Select(&result2, "SELECT distinct `type`\n"
  5690. "FROM npc_appearance\n"
  5691. "WHERE length(`type`) > 0 AND `spawn_id` = %u",
  5692. spawn_id);
  5693. while(result2.Next()) {
  5694. type = string(result2.GetString(0));
  5695. appearance_types[type] = GetAppearanceType(type);
  5696. if(appearance_types[type] == 255)
  5697. LogWrite(WORLD__ERROR, 0, "Appearance", "Unknown appearance type '%s' in LoadAppearances.", type.c_str());
  5698. }
  5699. database_new.Select(&result, "SELECT `type`, `signed_value`, `red`, `green`, `blue`\n"
  5700. "FROM npc_appearance\n"
  5701. "WHERE length(`type`) > 0 AND `spawn_id` = %u",
  5702. spawn_id);
  5703. while(result.Next()) {
  5704. if(appearance_types[result.GetString(0)] < APPEARANCE_SOGA_EBT){
  5705. color.red = result.GetInt8(2);
  5706. color.green = result.GetInt8(3);
  5707. color.blue = result.GetInt8(4);
  5708. }
  5709. switch(appearance_types[result.GetString(0)]){
  5710. case APPEARANCE_SOGA_HFHC:{
  5711. entity->features.soga_hair_face_highlight_color = color;
  5712. break;
  5713. }
  5714. case APPEARANCE_SOGA_HTHC:{
  5715. entity->features.soga_hair_type_highlight_color = color;
  5716. break;
  5717. }
  5718. case APPEARANCE_SOGA_HFC:{
  5719. entity->features.soga_hair_face_color = color;
  5720. break;
  5721. }
  5722. case APPEARANCE_SOGA_HTC:{
  5723. entity->features.soga_hair_type_color = color;
  5724. break;
  5725. }
  5726. case APPEARANCE_SOGA_HH:{
  5727. entity->features.soga_hair_highlight_color = color;
  5728. break;
  5729. }
  5730. case APPEARANCE_SOGA_HC1:{
  5731. entity->features.soga_hair_color1 = color;
  5732. break;
  5733. }
  5734. case APPEARANCE_SOGA_HC2:{
  5735. entity->features.soga_hair_color2 = color;
  5736. break;
  5737. }
  5738. case APPEARANCE_SOGA_SC:{
  5739. entity->features.soga_skin_color = color;
  5740. break;
  5741. }
  5742. case APPEARANCE_SOGA_EC:{
  5743. entity->features.soga_eye_color = color;
  5744. break;
  5745. }
  5746. case APPEARANCE_HTHC:{
  5747. entity->features.hair_type_highlight_color = color;
  5748. break;
  5749. }
  5750. case APPEARANCE_HFHC:{
  5751. entity->features.hair_face_highlight_color = color;
  5752. break;
  5753. }
  5754. case APPEARANCE_HTC:{
  5755. entity->features.hair_type_color = color;
  5756. break;
  5757. }
  5758. case APPEARANCE_HFC:{
  5759. entity->features.hair_face_color = color;
  5760. break;
  5761. }
  5762. case APPEARANCE_HH:{
  5763. entity->features.hair_highlight_color = color;
  5764. break;
  5765. }
  5766. case APPEARANCE_HC1:{
  5767. entity->features.hair_color1 = color;
  5768. break;
  5769. }
  5770. case APPEARANCE_HC2:{
  5771. entity->features.hair_color2 = color;
  5772. break;
  5773. }
  5774. case APPEARANCE_WC1:{
  5775. entity->features.wing_color1 = color;
  5776. break;
  5777. }
  5778. case APPEARANCE_WC2:{
  5779. entity->features.wing_color2 = color;
  5780. break;
  5781. }
  5782. case APPEARANCE_SC:{
  5783. entity->features.skin_color = color;
  5784. break;
  5785. }
  5786. case APPEARANCE_EC:{
  5787. entity->features.eye_color = color;
  5788. break;
  5789. }
  5790. case APPEARANCE_SOGA_EBT:{
  5791. for(int i=0;i<3;i++)
  5792. entity->features.soga_eye_brow_type[i] = result.GetInt8(2+i);
  5793. break;
  5794. }
  5795. case APPEARANCE_SOGA_CHEEKT:{
  5796. for(int i=0;i<3;i++)
  5797. entity->features.soga_cheek_type[i] = result.GetInt8(2+i);
  5798. break;
  5799. }
  5800. case APPEARANCE_SOGA_NT:{
  5801. for(int i=0;i<3;i++)
  5802. entity->features.soga_nose_type[i] = result.GetInt8(2+i);
  5803. break;
  5804. }
  5805. case APPEARANCE_SOGA_CHINT:{
  5806. for(int i=0;i<3;i++)
  5807. entity->features.soga_chin_type[i] = result.GetInt8(2+i);
  5808. break;
  5809. }
  5810. case APPEARANCE_SOGA_LT:{
  5811. for(int i=0;i<3;i++)
  5812. entity->features.soga_lip_type[i] = result.GetInt8(2+i);
  5813. break;
  5814. }
  5815. case APPEARANCE_SOGA_EART:{
  5816. for(int i=0;i<3;i++)
  5817. entity->features.soga_ear_type[i] = result.GetInt8(2+i);
  5818. break;
  5819. }
  5820. case APPEARANCE_SOGA_EYET:{
  5821. for(int i=0;i<3;i++)
  5822. entity->features.soga_eye_type[i] = result.GetInt8(2+i);
  5823. break;
  5824. }
  5825. case APPEARANCE_EBT:{
  5826. for(int i=0;i<3;i++)
  5827. entity->features.eye_brow_type[i] = result.GetInt8(2+i);
  5828. break;
  5829. }
  5830. case APPEARANCE_CHEEKT:{
  5831. for(int i=0;i<3;i++)
  5832. entity->features.cheek_type[i] = result.GetInt8(2+i);
  5833. break;
  5834. }
  5835. case APPEARANCE_NT:{
  5836. for(int i=0;i<3;i++)
  5837. entity->features.nose_type[i] = result.GetInt8(2+i);
  5838. break;
  5839. }
  5840. case APPEARANCE_CHINT:{
  5841. for(int i=0;i<3;i++)
  5842. entity->features.chin_type[i] = result.GetInt8(2+i);
  5843. break;
  5844. }
  5845. case APPEARANCE_EART:{
  5846. for(int i=0;i<3;i++)
  5847. entity->features.ear_type[i] = result.GetInt8(2+i);
  5848. break;
  5849. }
  5850. case APPEARANCE_EYET:{
  5851. for(int i=0;i<3;i++)
  5852. entity->features.eye_type[i] = result.GetInt8(2+i);
  5853. break;
  5854. }
  5855. case APPEARANCE_LT:{
  5856. for(int i=0;i<3;i++)
  5857. entity->features.lip_type[i] = result.GetInt8(2+i);
  5858. break;
  5859. }
  5860. case APPEARANCE_SHIRT:{
  5861. entity->features.shirt_color = color;
  5862. break;
  5863. }
  5864. case APPEARANCE_UCC:{
  5865. break;
  5866. }
  5867. case APPEARANCE_PANTS:{
  5868. entity->features.pants_color = color;
  5869. break;
  5870. }
  5871. case APPEARANCE_ULC:{
  5872. break;
  5873. }
  5874. case APPEARANCE_U9:{
  5875. break;
  5876. }
  5877. case APPEARANCE_BODY_SIZE:{
  5878. entity->features.body_size = color.red;
  5879. break;
  5880. }
  5881. case APPEARANCE_SOGA_WC1:{
  5882. break;
  5883. }
  5884. case APPEARANCE_SOGA_WC2:{
  5885. break;
  5886. }
  5887. case APPEARANCE_SOGA_SHIRT:{
  5888. break;
  5889. }
  5890. case APPEARANCE_SOGA_UCC:{
  5891. break;
  5892. }
  5893. case APPEARANCE_SOGA_PANTS:{
  5894. break;
  5895. }
  5896. case APPEARANCE_SOGA_ULC:{
  5897. break;
  5898. }
  5899. case APPEARANCE_SOGA_U13:{
  5900. break;
  5901. }
  5902. }
  5903. }
  5904. }
  5905. void WorldDatabase::LoadNPCAppearanceEquipmentData(ZoneServer* zone, int32 spawn_id) {
  5906. NPC* npc = zone->GetNPC(spawn_id);
  5907. if(!npc) {
  5908. LogWrite(NPC__ERROR, 0, "NPC", "Unable to get a valid npc (%u) in %s", spawn_id, __FUNCTION__);
  5909. return;
  5910. }
  5911. DatabaseResult result;
  5912. int8 slot = 0;
  5913. if (!database_new.Select(&result, "SELECT slot_id, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue\n"
  5914. "FROM npc_appearance_equip\n"
  5915. "WHERE spawn_id = %u\n",
  5916. spawn_id))
  5917. {
  5918. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5919. return;
  5920. }
  5921. while (result.Next()) {
  5922. slot = result.GetInt8(0);
  5923. if(slot < NUM_SLOTS) {
  5924. npc->equipment.equip_id[slot] = result.GetInt16(1);
  5925. npc->equipment.color[slot].red = result.GetInt8(2);
  5926. npc->equipment.color[slot].green = result.GetInt8(3);
  5927. npc->equipment.color[slot].blue = result.GetInt8(4);
  5928. npc->equipment.highlight[slot].red = result.GetInt8(5);
  5929. npc->equipment.highlight[slot].green = result.GetInt8(6);
  5930. npc->equipment.highlight[slot].blue = result.GetInt8(7);
  5931. }
  5932. }
  5933. }
  5934. void WorldDatabase::SaveCharacterPicture(int32 characterID, int8 type, uchar* picture, int32 picture_size) {
  5935. stringstream ss_hex;
  5936. stringstream ss_query;
  5937. ss_hex.flags(ios::hex);
  5938. for (int32 i = 0; i < picture_size; i++)
  5939. ss_hex << setfill('0') << setw(2) << (int32)picture[i];
  5940. ss_query << "INSERT INTO `character_pictures` (`char_id`, `pic_type`, `picture`) VALUES (" << characterID << ", " << (int32)type << ", '" << ss_hex.str() << "') ON DUPLICATE KEY UPDATE `picture` = '" << ss_hex.str() << "'";
  5941. if (!database_new.Query(ss_query.str().c_str()))
  5942. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5943. }
  5944. void WorldDatabase::LoadZoneFlightPaths(ZoneServer* zone) {
  5945. DatabaseResult result;
  5946. int32 total = 0;
  5947. if (!database_new.Select(&result, "SELECT id, speed, flying, early_dismount FROM flight_paths WHERE zone_id = %u", zone->GetZoneID())) {
  5948. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5949. return;
  5950. }
  5951. while (result.Next()) {
  5952. FlightPathInfo* info = new FlightPathInfo;
  5953. int32 id = result.GetInt32(0);
  5954. info->speed = result.GetFloat(1);
  5955. info->flying = result.GetInt8(2) == 1 ? true : false;
  5956. info->dismount = result.GetInt8(3) == 1 ? true : false;
  5957. zone->AddFlightPath(id, info);
  5958. total++;
  5959. }
  5960. LogWrite(ZONE__DEBUG, 0, "Zone", "Loaded %u flight paths for %s", total, zone->GetZoneDescription());
  5961. LoadZoneFlightPathLocations(zone);
  5962. }
  5963. void WorldDatabase::LoadZoneFlightPathLocations(ZoneServer* zone) {
  5964. DatabaseResult result;
  5965. int32 total = 0;
  5966. if (!database_new.Select(&result, "SELECT loc.flight_path, loc.x, loc.y, loc.z FROM flight_paths_locations loc\n"
  5967. "INNER JOIN flight_paths path\n"
  5968. "ON loc.flight_path = path.id\n"
  5969. "WHERE path.zone_id = %u\n"
  5970. "ORDER BY loc.id",
  5971. zone->GetZoneID()))
  5972. {
  5973. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5974. return;
  5975. }
  5976. while (result.Next()) {
  5977. FlightPathLocation* loc = new FlightPathLocation;
  5978. int32 id = result.GetInt32(0);
  5979. loc->X = result.GetFloat(1);
  5980. loc->Y = result.GetFloat(2);
  5981. loc->Z = result.GetFloat(3);
  5982. zone->AddFlightPathLocation(id, loc);
  5983. total++;
  5984. }
  5985. LogWrite(ZONE__DEBUG, 0, "Zone", "Loaded %u flight path locations for %s", total, zone->GetZoneDescription());
  5986. }
  5987. void WorldDatabase::SaveCharacterLUAHistory(Player* player, int32 event_id, int32 value, int32 value2) {
  5988. Query query;
  5989. query.AddQueryAsync(player->GetCharacterID(), this, Q_REPLACE, "REPLACE INTO character_lua_history(char_id, event_id, value, value2) VALUES(% u, % u, % u, % u)", player->GetCharacterID(), event_id, value, value2);
  5990. // if (!database_new.Query("REPLACE INTO character_lua_history (char_id, event_id, value, value2) VALUES (%u, %u, %u, %u)", player->GetCharacterID(), event_id, value, value2))
  5991. // LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5992. }
  5993. void WorldDatabase::LoadCharacterLUAHistory(int32 char_id, Player* player) {
  5994. DatabaseResult result;
  5995. int32 total = 0;
  5996. if (!database_new.Select(&result, "SELECT event_id, value, value2 FROM character_lua_history WHERE char_id = %u", char_id)) {
  5997. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5998. return;
  5999. }
  6000. while (result.Next()) {
  6001. int32 id = result.GetInt32(0);
  6002. LUAHistory* hd = new LUAHistory;
  6003. hd->Value = result.GetInt32(1);
  6004. hd->Value2 = result.GetInt32(2);
  6005. hd->SaveNeeded = false;
  6006. player->LoadLUAHistory(id, hd);
  6007. total++;
  6008. }
  6009. LogWrite(PLAYER__DEBUG, 0, "Player", "Loaded %u LUA history for %s", total, player->GetName());
  6010. }
  6011. void WorldDatabase::FindSpell(Client* client, char* findString)
  6012. {
  6013. DatabaseResult result;
  6014. if (!database_new.Select(&result, "SELECT s.`id`, ts.spell_id, ts.index, `name`, `tier` "
  6015. "FROM (spells s, spell_tiers st) "
  6016. "LEFT JOIN spell_ts_ability_index ts "
  6017. "ON s.`id` = ts.spell_id "
  6018. "WHERE s.id = st.spell_id and s.name like '%%%s%%' AND s.is_active = 1 "
  6019. "ORDER BY s.`id`, `tier` limit 50", findString))
  6020. {
  6021. // error
  6022. }
  6023. else
  6024. {
  6025. client->Message(CHANNEL_COLOR_YELLOW, "SpellID (SpellTier): SpellName for %s", findString);
  6026. while (result.Next())
  6027. {
  6028. int32 spell_id = result.GetInt32Str("id");
  6029. string spell_name = result.GetStringStr("name");
  6030. int8 tier = result.GetInt8Str("tier");
  6031. client->Message(CHANNEL_COLOR_YELLOW, "%i (%i): %s", spell_id, tier, spell_name.c_str());
  6032. }
  6033. client->Message(CHANNEL_COLOR_YELLOW, "End Spell Results for %s", findString);
  6034. }
  6035. }
  6036. void WorldDatabase::LoadChestTraps() {
  6037. chest_trap_list.Clear();
  6038. int32 index = 0;
  6039. Query query;
  6040. MYSQL_ROW row;
  6041. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, applicable_zone_id, chest_min_difficulty, chest_max_difficulty, spell_id, spell_tier FROM chest_traps");
  6042. if (result && mysql_num_rows(result) > 0) {
  6043. Title* title = 0;
  6044. while (result && (row = mysql_fetch_row(result))) {
  6045. int32 dbid = atoul(row[0]);
  6046. sint32 applicable_zone_id = atoi(row[1]);
  6047. int32 mindifficulty = atoul(row[2]);
  6048. int32 maxdifficulty = atoul(row[3]);
  6049. int32 spellid = atoul(row[4]);
  6050. int32 tier = atoul(row[5]);
  6051. ChestTrap* trap = new ChestTrap(dbid,applicable_zone_id,mindifficulty,maxdifficulty,spellid,tier);
  6052. chest_trap_list.AddChestTrap(trap);
  6053. }
  6054. }
  6055. }
  6056. bool WorldDatabase::CheckExpansionFlags(ZoneServer* zone, int32 spawnXpackFlag)
  6057. {
  6058. if (spawnXpackFlag == 0)
  6059. return true;
  6060. int32 globalXpackFlag = rule_manager.GetGlobalRule(R_Expansion, GlobalExpansionFlag)->GetInt32();
  6061. int32 zoneXpackFlag = zone->GetExpansionFlag();
  6062. // zone expansion flag takes priority
  6063. if (zoneXpackFlag > 0 && (spawnXpackFlag & zoneXpackFlag) == 0)
  6064. return false;
  6065. // zone expansion flag fails, then if global expansion flag set, we see if that bit operand doesn't match, skip mob then
  6066. else if (zoneXpackFlag == 0 && globalXpackFlag > 0 && (spawnXpackFlag & globalXpackFlag) == 0)
  6067. return false;
  6068. return true;
  6069. }
  6070. bool WorldDatabase::CheckHolidayFlags(ZoneServer* zone, int32 spawnHolidayFlag)
  6071. {
  6072. if (spawnHolidayFlag == 0)
  6073. return true;
  6074. int32 globalHolidayFlag = rule_manager.GetGlobalRule(R_Expansion, GlobalHolidayFlag)->GetInt32();
  6075. int32 zoneHolidayFlag = zone->GetHolidayFlag();
  6076. // zone holiday flag takes priority
  6077. if (zoneHolidayFlag > 0 && (spawnHolidayFlag & zoneHolidayFlag) == 0)
  6078. return false;
  6079. // zone holiday flag fails, then if global expansion flag set, we see if that bit operand doesn't match, skip mob then
  6080. else if (zoneHolidayFlag == 0 && globalHolidayFlag > 0 && (spawnHolidayFlag & globalHolidayFlag) == 0)
  6081. return false;
  6082. return true;
  6083. }
  6084. void WorldDatabase::GetHouseSpawnInstanceData(ZoneServer* zone, Spawn* spawn)
  6085. {
  6086. if (!spawn)
  6087. return;
  6088. if (zone->house_object_database_lookup.count(spawn->GetModelType()) < 1)
  6089. zone->house_object_database_lookup.Put(spawn->GetModelType(), spawn->GetDatabaseID());
  6090. DatabaseResult result;
  6091. database_new.Select(&result, "SELECT pickup_item_id, pickup_unique_item_id\n"
  6092. " FROM spawn_instance_data\n"
  6093. " WHERE spawn_id = %u and spawn_location_id = %u",
  6094. spawn->GetDatabaseID(),spawn->GetSpawnLocationID());
  6095. if (result.GetNumRows() > 0 && result.Next()) {
  6096. spawn->SetPickupItemID(result.GetInt32(0));
  6097. spawn->SetPickupUniqueItemID(result.GetInt32(1));
  6098. if (spawn->GetZone() != nullptr && spawn->GetZone()->zonemap != nullptr && spawn->GetZone()->zonemap->IsMapLoaded())
  6099. {
  6100. int32 newGrid = spawn->GetZone()->Grid->GetGridID(spawn);
  6101. spawn->SetPos(&(spawn->appearance.pos.grid_id), newGrid);
  6102. }
  6103. }
  6104. }
  6105. int32 WorldDatabase::FindHouseInstanceSpawn(Spawn* spawn)
  6106. {
  6107. DatabaseResult result;
  6108. database_new.Select(&result, "SELECT id\n"
  6109. " FROM spawn\n"
  6110. " WHERE model_type = %u and is_instanced_spawn=1 limit 1",
  6111. spawn->GetModelType());
  6112. if (result.GetNumRows() > 0 && result.Next()) {
  6113. return result.GetInt32(0);
  6114. }
  6115. return 0;
  6116. }