WorldDatabase.cpp 362 KB

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