WorldDatabase.cpp 301 KB

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