WorldDatabase.cpp 268 KB

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