WorldDatabase.cpp 275 KB

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