WorldDatabase.cpp 354 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <math.h>
  17. #include <iostream>
  18. #include <sstream>
  19. #include <iomanip>
  20. #include <ios>
  21. #include <assert.h>
  22. #include <boost/algorithm/string/predicate.hpp>
  23. #include <boost/algorithm/string.hpp>
  24. #include "WorldDatabase.h"
  25. #include "../common/debug.h"
  26. #include "../common/packet_dump.h"
  27. #include "../common/GlobalHeaders.h"
  28. #include "Items/Items.h"
  29. #include "Factions.h"
  30. #include "World.h"
  31. #include "Variables.h"
  32. #include "VisualStates.h"
  33. #include "Appearances.h"
  34. #include "Skills.h"
  35. #include "Quests.h"
  36. #include "LuaInterface.h"
  37. #include "classes.h"
  38. #include "../common/Log.h"
  39. #include "Rules/Rules.h"
  40. #include "Titles.h"
  41. #include "Languages.h"
  42. #include "Traits/Traits.h"
  43. #include "ClientPacketFunctions.h"
  44. #include "Zone/ChestTrap.h"
  45. #include "../common/version.h"
  46. #include "SpellProcess.h"
  47. #include "races.h"
  48. extern Classes classes;
  49. extern Commands commands;
  50. extern MasterTitlesList master_titles_list;
  51. extern MasterItemList master_item_list;
  52. extern MasterSpellList master_spell_list;
  53. extern MasterTraitList master_trait_list;
  54. extern MasterFactionList master_faction_list;
  55. extern World world;
  56. extern Variables variables;
  57. extern VisualStates visual_states;
  58. extern Appearances master_appearance_list;
  59. extern MasterSkillList master_skill_list;
  60. extern MasterQuestList master_quest_list;
  61. extern LuaInterface* lua_interface;
  62. extern ZoneList zone_list;
  63. extern GuildList guild_list;
  64. extern MasterCollectionList master_collection_list;
  65. extern RuleManager rule_manager;
  66. extern MasterLanguagesList master_languages_list;
  67. extern ChestTrapList chest_trap_list;
  68. //devn00b: Fix for linux builds since we dont use stricmp we use strcasecmp
  69. #if defined(__GNUC__)
  70. #define stricmp strcasecmp
  71. #define strnicmp strncasecmp
  72. #include <sys/stat.h>
  73. #endif
  74. WorldDatabase::WorldDatabase(){
  75. }
  76. WorldDatabase::~WorldDatabase(){
  77. }
  78. bool WorldDatabase::ConnectNewDatabase() {
  79. /*
  80. TESTS
  81. database_new.Connect();
  82. DatabaseResult result;
  83. database_new.Select(&result, "select name from characters where id=1");
  84. if (result.Next()) {
  85. printf("'%s'\n", result.GetStringStr("name"));
  86. printf("'%s'\n", result.GetStringStr("nameBAD"));
  87. printf("'%s'\n", result.GetString(3));
  88. }
  89. return true;
  90. */
  91. return database_new.Connect();
  92. }
  93. void WorldDatabase::PingNewDB()
  94. {
  95. database_new.PingNewDB();
  96. }
  97. void WorldDatabase::DeleteBuyBack(int32 char_id, int32 item_id, int16 quantity, int32 price) {
  98. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "Deleting Buyback - Player: %u, Item ID: %u, Qty: %i, Price: %u", char_id, item_id, quantity, price);
  99. Query query;
  100. query.RunQuery2(Q_DELETE, "DELETE FROM character_buyback WHERE char_id = %u AND item_id = %u AND quantity = %i AND price = %u", char_id, item_id, quantity, price);
  101. }
  102. void WorldDatabase::LoadBuyBacks(Client* client) {
  103. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "Loading Buyback - Player: %u", client->GetCharacterID());
  104. Query query;
  105. MYSQL_ROW row;
  106. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, item_id, quantity, price FROM character_buyback where char_id = %u ORDER BY id desc limit 10", client->GetCharacterID());
  107. int8 count = 0;
  108. int32 last_id = 0;
  109. if(result)
  110. {
  111. while(result && (row = mysql_fetch_row(result)))
  112. {
  113. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "AddBuyBack: item: %u, qty: %i, price: %u", atoul(row[1]), atoi(row[2]), atoul(row[3]));
  114. last_id = atoul(row[0]);
  115. client->AddBuyBack(last_id, atoul(row[1]), atoi(row[2]), atoul(row[3]), false);
  116. count++;
  117. }
  118. if(count >= 10)
  119. {
  120. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "Deleting excess Buyback from Player: %u", client->GetCharacterID());
  121. Query query2;
  122. query2.RunQuery2(Q_DELETE, "DELETE FROM character_buyback WHERE char_id = %u AND id < %u", client->GetCharacterID(), last_id);
  123. }
  124. }
  125. }
  126. void WorldDatabase::SaveBuyBacks(Client* client)
  127. {
  128. LogWrite(MERCHANT__DEBUG, 3, "Merchant", "Saving Buybacks - Player: %u", client->GetCharacterID());
  129. deque<BuyBackItem*>* buybacks = client->GetBuyBacks();
  130. if(buybacks && buybacks->size() > 0)
  131. {
  132. BuyBackItem* item = 0;
  133. deque<BuyBackItem*>::iterator itr;
  134. for(itr = buybacks->begin(); itr != buybacks->end(); itr++)
  135. {
  136. item = *itr;
  137. if(item && item->save_needed)
  138. {
  139. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "SaveBuyBack: char: %u, item: %u, qty: %i, price: %u", client->GetCharacterID(), item->item_id, item->quantity, item->price);
  140. SaveBuyBack(client->GetCharacterID(), item->item_id, item->quantity, item->price);
  141. item->save_needed = false;
  142. }
  143. }
  144. }
  145. }
  146. void WorldDatabase::SaveBuyBack(int32 char_id, int32 item_id, int16 quantity, int32 price)
  147. {
  148. LogWrite(MERCHANT__DEBUG, 3, "Merchant", "Saving Buyback - Player: %u, Item ID: %u, Qty: %i, Price: %u", char_id, item_id, quantity, price);
  149. Query query;
  150. string insert = string("INSERT INTO character_buyback (char_id, item_id, quantity, price) VALUES (%u, %u, %i, %u) ");
  151. query.AddQueryAsync(char_id, this, Q_INSERT, insert.c_str(), char_id, item_id, quantity, price);
  152. }
  153. int32 WorldDatabase::LoadCharacterSpells(int32 char_id, Player* player)
  154. {
  155. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Character Spells for player %s...", player->GetName());
  156. Query query;
  157. MYSQL_ROW row;
  158. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spell_id, tier, knowledge_slot, spell_book_type, linked_timer_id FROM character_spells, spells where character_spells.spell_id = spells.id and character_spells.char_id = %u ORDER BY spell_id, tier desc", char_id);
  159. int32 old_spell_id = 0;
  160. int32 new_spell_id = 0;
  161. int32 count = 0;
  162. if(result && mysql_num_rows(result) >0)
  163. {
  164. while(result && (row = mysql_fetch_row(result)))
  165. {
  166. count++;
  167. new_spell_id = atoul(row[0]);
  168. if(new_spell_id == old_spell_id)
  169. continue;
  170. old_spell_id = new_spell_id;
  171. LogWrite(SPELL__DEBUG, 5, "Spells", "\tLoading SpellID: %u, tier: %i, slot: %i, type: %u linked_timer_id: %u", new_spell_id, atoi(row[1]), atoi(row[2]), atoul(row[3]), atoul(row[4]));
  172. int8 tier = atoi(row[1]);
  173. if (player->HasSpell(new_spell_id, tier, true))
  174. continue;
  175. player->AddSpellBookEntry(new_spell_id, tier, atoi(row[2]), atoul(row[3]), atoul(row[4]));
  176. }
  177. }
  178. return count;
  179. }
  180. void WorldDatabase::SavePlayerSpells(Client* client)
  181. {
  182. if(!client || client->GetCharacterID() < 1)
  183. return;
  184. LogWrite(SPELL__DEBUG, 3, "Spells", "Saving Spell(s) for Player: '%s'", client->GetPlayer()->GetName());
  185. vector<SpellBookEntry*>* spells = client->GetPlayer()->GetSpellsSaveNeeded();
  186. if(spells)
  187. {
  188. vector<SpellBookEntry*>::iterator itr;
  189. SpellBookEntry* spell = 0;
  190. for(itr = spells->begin(); itr != spells->end(); itr++)
  191. {
  192. spell = *itr;
  193. Query query;
  194. LogWrite(SPELL__DEBUG, 5, "Spells", "\tSaving SpellID: %u, tier: %i, slot: %i", spell->spell_id, spell->tier, spell->slot);
  195. query.AddQueryAsync(client->GetCharacterID(), this, Q_INSERT, "INSERT INTO character_spells (char_id, spell_id, tier) SELECT %u, %u, %i ON DUPLICATE KEY UPDATE tier = %i",
  196. client->GetPlayer()->GetCharacterID(), spell->spell_id, spell->tier, spell->tier);
  197. spell->save_needed = false;
  198. }
  199. safe_delete(spells);
  200. }
  201. }
  202. int32 WorldDatabase::LoadCharacterSkills(int32 char_id, Player* player)
  203. {
  204. Query query;
  205. MYSQL_ROW row;
  206. int32 count = 0;
  207. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT skill_id, current_val, max_val FROM character_skills, skills where character_skills.skill_id = skills.id and character_skills.char_id = %u", char_id);
  208. if(result && mysql_num_rows(result) >0)
  209. {
  210. while(result && (row = mysql_fetch_row(result)))
  211. {
  212. count++;
  213. LogWrite(SKILL__DEBUG, 5, "Skills", "Loading SkillID: %u, cur_val: %i, max_val: %l", strtoul(row[0], NULL, 0), atoi(row[1]), atoi(row[2]));
  214. player->AddSkill(strtoul(row[0], NULL, 0), atoi(row[1]), atoi(row[2]));
  215. }
  216. }
  217. return count;
  218. }
  219. void WorldDatabase::DeleteCharacterSkill(int32 char_id, Skill* skill)
  220. {
  221. if (char_id > 0 && skill)
  222. {
  223. LogWrite(SKILL__DEBUG, 0, "Skills", "Deleting Skill '%s' (%u) from char_id: %u", skill->name.data.c_str(), skill->skill_id, char_id);
  224. Query query;
  225. query.RunQuery2(Q_DELETE, "DELETE FROM `character_skills` WHERE `char_id`=%u AND `skill_id`=%u", char_id, skill->skill_id);
  226. }
  227. }
  228. int32 WorldDatabase::LoadSkills()
  229. {
  230. int32 total = 0;
  231. Query query;
  232. MYSQL_ROW row;
  233. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, short_name, name, description, skill_type, display FROM skills");
  234. if(result)
  235. {
  236. if (mysql_num_rows(result) >0)
  237. {
  238. Skill* skill = 0;
  239. while(result && (row = mysql_fetch_row(result)))
  240. {
  241. skill = new Skill();
  242. skill->skill_id = strtoul(row[0], NULL, 0);
  243. skill->short_name.data = string(row[1]);
  244. skill->short_name.size = skill->short_name.data.length();
  245. skill->name.data = string(row[2]);
  246. skill->name.size = skill->name.data.length();
  247. skill->description.data = string(row[3]);
  248. skill->description.size = skill->description.data.length();
  249. skill->skill_type = strtoul(row[4], NULL, 0);
  250. //these two need to be converted to the correct numbers
  251. if(skill->skill_type == 13)
  252. skill->skill_type = SKILL_TYPE_LANGUAGE;
  253. else if(skill->skill_type == 12)
  254. skill->skill_type = SKILL_TYPE_GENERAL;
  255. skill->display = atoi(row[5]);
  256. master_skill_list.AddSkill(skill);
  257. total++;
  258. LogWrite(SKILL__DEBUG, 5, "Skill", "---Loading Skill: %s (%u)", skill->name.data.c_str(), skill->skill_id);
  259. LogWrite(SKILL__DEBUG, 7, "Skill", "---short_name: %s, type: %i, display: %i", skill->short_name.data.c_str(), skill->skill_type, skill->display);
  260. }
  261. }
  262. }
  263. LogWrite(SKILL__DEBUG, 3, "Skill", "--Loaded %u Skill(s)", total);
  264. return total;
  265. }
  266. map<int8, vector<MacroData*> >* WorldDatabase::LoadCharacterMacros(int32 char_id)
  267. {
  268. Query query;
  269. MYSQL_ROW row;
  270. int32 total = 0;
  271. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT macro_number, macro_name, macro_icon, macro_text FROM character_macros where char_id = %u ORDER BY macro_number, id", char_id);
  272. if(result && mysql_num_rows(result) >0)
  273. {
  274. map<int8, vector<MacroData*> >* macros = new map<int8, vector<MacroData*> >;
  275. while(result && (row = mysql_fetch_row(result)))
  276. {
  277. MacroData* data = new MacroData;
  278. data->name = row[1];
  279. data->icon = atoi(row[2]);
  280. data->text = row[3];
  281. (*macros)[atoi(row[0])].push_back(data);
  282. total++;
  283. LogWrite(PLAYER__DEBUG, 5, "Player", "\tLoading macro: %i. %s for player: %u", atoi(row[0]), row[1], char_id);
  284. }
  285. LogWrite(PLAYER__DEBUG, 0, "Player", "\tLoaded %u macro%s", total, total == 1 ? "" : "s");
  286. return macros;
  287. }
  288. return 0;
  289. }
  290. void WorldDatabase::UpdateCharacterMacro(int32 char_id, int8 number, const char* name, int16 icon, vector<string>* updates)
  291. {
  292. LogWrite(PLAYER__DEBUG, 0, "Player", "Update player id %u macro: %i", char_id, number);
  293. Query query;
  294. Query query2;
  295. query.RunQuery2(Q_DELETE, "delete FROM character_macros where char_id = %u and macro_number = %i", char_id, number);
  296. if(name && updates && updates->size() > 0)
  297. {
  298. for(int8 i=0;i<updates->size();i++)
  299. {
  300. query2.RunQuery2(Q_INSERT, "insert into character_macros (char_id, macro_number, macro_name, macro_icon, macro_text) values(%u, %i, '%s', %i, '%s')", char_id, number, getSafeEscapeString(name).c_str(), icon, getSafeEscapeString(updates->at(i).c_str()).c_str());
  301. LogWrite(PLAYER__DEBUG, 5, "Player", "\tAdding macro: %s, %s (Player: %u)", name, updates->at(i).c_str(), char_id);
  302. }
  303. }
  304. }
  305. //we use our timestamp just in case db is on another server, otherwise times might be off
  306. void WorldDatabase::UpdateVitality(int32 timestamp, float amount){
  307. Query query, query2, query3;
  308. LogWrite(PLAYER__DEBUG, 3, "Player", "Reset Vitality > 100: %f", amount);
  309. query.RunQuery2(Q_UPDATE, "update character_details set xp_vitality=100 where (xp_vitality + %f) > 100", amount);
  310. LogWrite(PLAYER__DEBUG, 3, "Player", "Update Vitality <= 100: %f", amount);
  311. query2.RunQuery2(Q_UPDATE, "update character_details set xp_vitality=(xp_vitality+%f) where (xp_vitality + %f) <= 100", amount, amount);
  312. LogWrite(PLAYER__DEBUG, 3, "Player", "Update Vitality Timer: %u", timestamp);
  313. query3.RunQuery2(Q_UPDATE, "update variables set variable_value=%u where variable_name='vitalitytimer'", timestamp);
  314. }
  315. void WorldDatabase::SaveVariable(const char* name, const char* value, const char* comment){
  316. LogWrite(WORLD__DEBUG, 0, "Variables", "Saving Variable: %s = %s", name, value);
  317. Query query;
  318. if(comment){
  319. query.RunQuery2(Q_REPLACE, "replace into variables (variable_name, variable_value, comment) values('%s', '%s', '%s')",
  320. getSafeEscapeString(name).c_str(), getSafeEscapeString(value).c_str(), getSafeEscapeString(comment).c_str());
  321. }
  322. else{
  323. query.RunQuery2(Q_REPLACE, "replace into variables (variable_name, variable_value) values('%s', '%s')",
  324. getSafeEscapeString(name).c_str(), getSafeEscapeString(value).c_str());
  325. }
  326. }
  327. void WorldDatabase::LoadGlobalVariables(){
  328. variables.ClearVariables ( );
  329. int32 total = 0;
  330. Query query;
  331. MYSQL_ROW row;
  332. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT variable_name, variable_value, comment FROM variables");
  333. while(result && (row = mysql_fetch_row(result)))
  334. {
  335. Variable* newVar = new Variable(row[0], row[1], row[2]);
  336. variables.AddVariable(newVar);
  337. total++;
  338. LogWrite(WORLD__DEBUG, 5, "World", "---Loading variable: '%s' = '%s'", row[0], row[1]);
  339. }
  340. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u variables", total);
  341. }
  342. void WorldDatabase::LoadAppearanceMasterList()
  343. {
  344. DatabaseResult result;
  345. int32 total = 0;
  346. int32 appearance_id;
  347. int16 appearance_version;
  348. master_appearance_list.ClearAppearances();
  349. database_new.Select(&result, "SELECT appearance_id, `name`, min_client_version FROM appearances ORDER BY appearance_id");
  350. while( result.Next() )
  351. {
  352. appearance_id = result.GetInt32Str("appearance_id");
  353. const char *appearance_name = result.GetStringStr("name");
  354. appearance_version = result.GetInt16Str("min_client_version");
  355. Appearance* a = new Appearance(appearance_id, appearance_name, appearance_version);
  356. master_appearance_list.InsertAppearance(a);
  357. total++;
  358. LogWrite(WORLD__DEBUG, 5, "World", "---Loading appearances: '%s' (%i)", appearance_name, appearance_id);
  359. }
  360. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u appearances", total);
  361. }
  362. void WorldDatabase::LoadVisualStates()
  363. {
  364. visual_states.Reset();
  365. int32 total = 0;
  366. Query query;
  367. Query query2;
  368. Query query3;
  369. MYSQL_ROW row;
  370. MYSQL_ROW row2;
  371. MYSQL_ROW row3;
  372. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT visual_state_id, name FROM visual_states");
  373. while(result && (row = mysql_fetch_row(result)))
  374. {
  375. VisualState* vs = new VisualState(atoi(row[0]), row[1]);
  376. visual_states.InsertVisualState(vs);
  377. total++;
  378. LogWrite(WORLD__DEBUG, 5, "World", "---Loading visual state: '%s' (%i)", row[1], atoi(row[0]));
  379. }
  380. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u visual states", total);
  381. total = 0;
  382. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT name, visual_state_id, message, targeted_message, min_version_range, max_version_range FROM emotes");
  383. while(result2 && (row2 = mysql_fetch_row(result2)))
  384. {
  385. EmoteVersionRange* range = 0;
  386. if ((range = visual_states.FindEmoteRange(string(row2[0]))) == NULL)
  387. {
  388. range = new EmoteVersionRange(row2[0]);
  389. visual_states.InsertEmoteRange(range);
  390. }
  391. range->AddVersionRange(atoul(row2[4]),atoul(row2[5]), row2[0], atoul(row2[1]), row2[2], row2[3]);
  392. total++;
  393. LogWrite(WORLD__DEBUG, 5, "World", "---Loading emote state: '%s' (%i)", row2[0], atoul(row2[0]));
  394. }
  395. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u emote state(s)", total);
  396. total = 0;
  397. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT name, spell_visual_id, alternate_spell_visual, min_version_range, max_version_range FROM spell_visuals");
  398. while(result3 && (row3 = mysql_fetch_row(result3)))
  399. {
  400. EmoteVersionRange* range = 0;
  401. if ((range = visual_states.FindSpellVisualRange(string(row3[0]))) == NULL)
  402. {
  403. range = new EmoteVersionRange(row3[0]);
  404. visual_states.InsertSpellVisualRange(range, atoul(row3[1]));
  405. }
  406. range->AddVersionRange(atoul(row3[3]),atoul(row3[4]), row3[0], atoul(row3[1]), row3[2]);
  407. total++;
  408. LogWrite(WORLD__DEBUG, 5, "World", "---Loading spell visual state: '%s' (%u)", row3[1], atoul(row3[0]));
  409. }
  410. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u spell visual state(s)", total);
  411. }
  412. void WorldDatabase::LoadSubCommandList()
  413. {
  414. int32 total = 0;
  415. Query query;
  416. MYSQL_ROW row;
  417. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT command, subcommand, handler, required_status FROM commands where length(subcommand) > 0 ORDER BY handler asc");
  418. while(result && (row = mysql_fetch_row(result)))
  419. {
  420. commands.GetRemoteCommands()->CheckAddSubCommand(string(row[0]), EQ2_RemoteCommandString(row[1], (int32)strtoul(row[2], NULL, 0), atoi(row[3])));
  421. total++;
  422. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Command: '%s', sub '%s', handler, %u status %i", row[0], row[1], atoul(row[2]), atoi(row[3]));
  423. }
  424. LogWrite(COMMAND__DEBUG, 3, "Command", "--Loaded %i Subcommand(s)", total);
  425. }
  426. void WorldDatabase::LoadCommandList()
  427. {
  428. Query query;
  429. MYSQL_ROW row;
  430. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT command, handler, required_status FROM commands where length(subcommand) = 0 ORDER BY handler asc");
  431. int16 index = 0;
  432. while(result && (row = mysql_fetch_row(result)))
  433. {
  434. int32 handler = strtoul(row[1], NULL, 0);
  435. while(handler>index && handler != 999)
  436. {
  437. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Remote Commands: handler %u, index %u", handler, index);
  438. commands.GetRemoteCommands()->addZero();
  439. index++;
  440. }
  441. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Commands: handler %u, index %u", handler, index);
  442. commands.GetRemoteCommands()->addCommand(EQ2_RemoteCommandString(row[0], handler, atoi(row[2])));
  443. index++;
  444. }
  445. LogWrite(COMMAND__DEBUG, 3, "Command", "--Loaded %i Command%s", index, index > 0 ? "s" : "");
  446. LoadSubCommandList();
  447. }
  448. int32 WorldDatabase::LoadNPCSpells(){
  449. Query query;
  450. MYSQL_ROW row;
  451. int32 count = 0;
  452. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spell_list_id, spell_id, spell_tier, on_spawn_cast, on_aggro_cast, required_hp_ratio FROM spawn_npc_spells where spell_list_id > 0");
  453. while(result && (row = mysql_fetch_row(result))){
  454. if(!row[0] || !row[1] || !row[2] || !row[3] || !row[4] || !row[5]) {
  455. LogWrite(NPC__ERROR, 0, "NPC", "---Loading NPC Spell List: %u, found NULL values in entry, SKIP!", row[0] ? atoul(row[0]) : 0);
  456. }
  457. else {
  458. world.AddNPCSpell(atoul(row[0]), atoul(row[1]), atoi(row[2]), atoul(row[3]), atoul(row[4]), atoi(row[5]));
  459. count++;
  460. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC Spell List: %u, spell id: %u, tier: %i", atoul(row[0]), atoul(row[1]), atoi(row[2]));
  461. }
  462. }
  463. return count;
  464. }
  465. int32 WorldDatabase::LoadNPCSkills(ZoneServer* zone){
  466. Query query;
  467. MYSQL_ROW row;
  468. int32 count = 0;
  469. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT skill_list_id, skill_id, starting_value FROM spawn_npc_skills");
  470. while(result && (row = mysql_fetch_row(result))){
  471. zone->AddNPCSkill(atoul(row[0]), atoul(row[1]), atoi(row[2]));
  472. count++;
  473. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC Skill List: %u, skill id: %u, value: %i", atoul(row[0]), atoul(row[1]), atoi(row[2]));
  474. }
  475. return count;
  476. }
  477. int32 WorldDatabase::LoadNPCEquipment(ZoneServer* zone){
  478. Query query;
  479. MYSQL_ROW row;
  480. int32 count = 0;
  481. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT equipment_list_id, item_id FROM spawn_npc_equipment");
  482. while(result && (row = mysql_fetch_row(result))){
  483. zone->AddNPCEquipment(atoul(row[0]), atoul(row[1]));
  484. count++;
  485. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC Equipment List: %u, item: %u", atoul(row[0]), atoul(row[1]));
  486. }
  487. return count;
  488. }
  489. int8 WorldDatabase::GetAppearanceType(string type){
  490. int8 ret = 255;
  491. if (type == "soga_hair_face_highlight_color")
  492. ret = APPEARANCE_SOGA_HFHC;
  493. else if (type == "soga_hair_type_highlight_color")
  494. ret = APPEARANCE_SOGA_HTHC;
  495. else if (type == "soga_hair_face_color")
  496. ret = APPEARANCE_SOGA_HFC;
  497. else if (type == "soga_hair_type_color")
  498. ret = APPEARANCE_SOGA_HTC;
  499. else if (type == "soga_hair_highlight")
  500. ret = APPEARANCE_SOGA_HH;
  501. else if (type == "soga_hair_color1")
  502. ret = APPEARANCE_SOGA_HC1;
  503. else if (type == "soga_hair_color2")
  504. ret = APPEARANCE_SOGA_HC2;
  505. else if (type == "hair_type_color")
  506. ret = APPEARANCE_HTC;
  507. else if (type == "soga_skin_color")
  508. ret = APPEARANCE_SOGA_SC;
  509. else if (type == "soga_eye_color")
  510. ret = APPEARANCE_SOGA_EC;
  511. else if (type == "hair_type_highlight_color")
  512. ret = APPEARANCE_HTHC;
  513. else if (type == "hair_face_highlight_color")
  514. ret = APPEARANCE_HFHC;
  515. else if (type == "hair_face_color")
  516. ret = APPEARANCE_HFC;
  517. else if (type == "hair_highlight")
  518. ret = APPEARANCE_HH;
  519. else if (type == "hair_color1")
  520. ret = APPEARANCE_HC1;
  521. else if (type == "wing_color1")
  522. ret = APPEARANCE_WC1;
  523. else if (type == "hair_color2")
  524. ret = APPEARANCE_HC2;
  525. else if (type == "wing_color2")
  526. ret = APPEARANCE_WC2;
  527. else if (type == "skin_color")
  528. ret = APPEARANCE_SC;
  529. else if (type == "eye_color")
  530. ret = APPEARANCE_EC;
  531. else if (type == "soga_eye_brow_type")
  532. ret = APPEARANCE_SOGA_EBT;
  533. else if (type == "soga_cheek_type")
  534. ret = APPEARANCE_SOGA_CHEEKT;
  535. else if (type == "soga_nose_type")
  536. ret = APPEARANCE_SOGA_NT;
  537. else if (type == "soga_chin_type")
  538. ret = APPEARANCE_SOGA_CHINT;
  539. else if (type == "soga_lip_type")
  540. ret = APPEARANCE_SOGA_LT;
  541. else if (type == "eye_brow_type")
  542. ret = APPEARANCE_EBT;
  543. else if (type == "soga_ear_type")
  544. ret = APPEARANCE_SOGA_EART;
  545. else if (type == "soga_eye_type")
  546. ret = APPEARANCE_SOGA_EYET;
  547. else if (type == "cheek_type")
  548. ret = APPEARANCE_CHEEKT;
  549. else if (type == "nose_type")
  550. ret = APPEARANCE_NT;
  551. else if (type == "chin_type")
  552. ret = APPEARANCE_CHINT;
  553. else if (type == "ear_type")
  554. ret = APPEARANCE_EART;
  555. else if (type == "eye_type")
  556. ret = APPEARANCE_EYET;
  557. else if (type == "lip_type")
  558. ret = APPEARANCE_LT;
  559. else if (type == "shirt_color")
  560. ret = APPEARANCE_SHIRT;
  561. else if (type == "unknown_chest_color")
  562. ret = APPEARANCE_UCC;
  563. else if (type == "pants_color")
  564. ret = APPEARANCE_PANTS;
  565. else if (type == "unknown_legs_color")
  566. ret = APPEARANCE_ULC;
  567. else if (type == "unknown9")
  568. ret = APPEARANCE_U9;
  569. else if (type == "body_size")
  570. ret = APPEARANCE_BODY_SIZE;
  571. else if (type == "soga_wing_color1")
  572. ret = APPEARANCE_SOGA_WC1;
  573. else if (type == "soga_wing_color2")
  574. ret = APPEARANCE_SOGA_WC2;
  575. else if (type == "soga_shirt_color")
  576. ret = APPEARANCE_SOGA_SHIRT;
  577. else if (type == "soga_unknown_chest_color")
  578. ret = APPEARANCE_SOGA_UCC;
  579. else if (type == "soga_pants_color")
  580. ret = APPEARANCE_SOGA_PANTS;
  581. else if (type == "soga_unknown_legs_color")
  582. ret = APPEARANCE_SOGA_ULC;
  583. else if (type == "soga_unknown13")
  584. ret = APPEARANCE_SOGA_U13;
  585. else if (type == "body_age")
  586. ret = APPEARANCE_BODY_AGE;
  587. else if (type == "model_color")
  588. ret = APPEARANCE_MC;
  589. else if (type == "soga_model_color")
  590. ret = APPEARANCE_SMC;
  591. else if (type == "soga_body_size")
  592. ret = APPEARANCE_SBS;
  593. else if (type == "soga_body_age")
  594. ret = APPEARANCE_SBA;
  595. return ret;
  596. }
  597. int32 WorldDatabase::LoadAppearances(ZoneServer* zone, Client* client){
  598. Query query, query2;
  599. MYSQL_ROW row;
  600. int32 count = 0, spawn_id = 0, new_spawn_id = 0;
  601. Entity* entity = 0;
  602. if(client)
  603. entity = client->GetPlayer();
  604. map<string, int8> appearance_types;
  605. map<int32, map<int8, EQ2_Color> > appearance_colors;
  606. EQ2_Color color;
  607. color.red = 0;
  608. color.green = 0;
  609. color.blue = 0;
  610. string type;
  611. MYSQL_RES* result = 0;
  612. if(!client)
  613. result = query.RunQuery2(Q_SELECT, "SELECT distinct `type` FROM npc_appearance where length(type) > 0");
  614. else
  615. result = query.RunQuery2(Q_SELECT, "SELECT distinct `type` FROM char_colors where length(type) > 0 and char_id=%u", client->GetCharacterID());
  616. while(result && (row = mysql_fetch_row(result))){
  617. type = string(row[0]);
  618. appearance_types[type] = GetAppearanceType(type);
  619. if(appearance_types[type] == 255)
  620. LogWrite(WORLD__ERROR, 0, "Appearance", "Unknown appearance type '%s' in LoadAppearances.", type.c_str());
  621. }
  622. MYSQL_RES* result2 = 0;
  623. if(!client)
  624. result2 = query2.RunQuery2(Q_SELECT, "SELECT `type`, spawn_id, signed_value, red, green, blue FROM npc_appearance where length(type) > 0 ORDER BY spawn_id");
  625. else
  626. result2 = query2.RunQuery2(Q_SELECT, "SELECT `type`, char_id, signed_value, red, green, blue FROM char_colors where length(type) > 0 and char_id=%u", client->GetCharacterID());
  627. while(result2 && (row = mysql_fetch_row(result2))){
  628. if(!client){
  629. new_spawn_id = atoul(row[1]);
  630. if(spawn_id != new_spawn_id){
  631. entity = zone->GetNPC(new_spawn_id, true);
  632. if(!entity)
  633. continue;
  634. if(spawn_id > 0)
  635. count++;
  636. spawn_id = new_spawn_id;
  637. }
  638. }
  639. if(appearance_types[row[0]] < APPEARANCE_SOGA_EBT){
  640. color.red = atoi(row[3]);
  641. color.green = atoi(row[4]);
  642. color.blue = atoi(row[5]);
  643. }
  644. switch(appearance_types[row[0]]){
  645. case APPEARANCE_SOGA_HFHC:{
  646. entity->features.soga_hair_face_highlight_color = color;
  647. break;
  648. }
  649. case APPEARANCE_SOGA_HTHC:{
  650. entity->features.soga_hair_type_highlight_color = color;
  651. break;
  652. }
  653. case APPEARANCE_SOGA_HFC:{
  654. entity->features.soga_hair_face_color = color;
  655. break;
  656. }
  657. case APPEARANCE_SOGA_HTC:{
  658. entity->features.soga_hair_type_color = color;
  659. break;
  660. }
  661. case APPEARANCE_SOGA_HH:{
  662. entity->features.soga_hair_highlight_color = color;
  663. break;
  664. }
  665. case APPEARANCE_SOGA_HC1:{
  666. entity->features.soga_hair_color1 = color;
  667. break;
  668. }
  669. case APPEARANCE_SOGA_HC2:{
  670. entity->features.soga_hair_color2 = color;
  671. break;
  672. }
  673. case APPEARANCE_SOGA_SC:{
  674. entity->features.soga_skin_color = color;
  675. break;
  676. }
  677. case APPEARANCE_SOGA_EC:{
  678. entity->features.soga_eye_color = color;
  679. break;
  680. }
  681. case APPEARANCE_HTHC:{
  682. entity->features.hair_type_highlight_color = color;
  683. break;
  684. }
  685. case APPEARANCE_HFHC:{
  686. entity->features.hair_face_highlight_color = color;
  687. break;
  688. }
  689. case APPEARANCE_HTC:{
  690. entity->features.hair_type_color = color;
  691. break;
  692. }
  693. case APPEARANCE_HFC:{
  694. entity->features.hair_face_color = color;
  695. break;
  696. }
  697. case APPEARANCE_HH:{
  698. entity->features.hair_highlight_color = color;
  699. break;
  700. }
  701. case APPEARANCE_HC1:{
  702. entity->features.hair_color1 = color;
  703. break;
  704. }
  705. case APPEARANCE_HC2:{
  706. entity->features.hair_color2 = color;
  707. break;
  708. }
  709. case APPEARANCE_WC1:{
  710. entity->features.wing_color1 = color;
  711. break;
  712. }
  713. case APPEARANCE_WC2:{
  714. entity->features.wing_color2 = color;
  715. break;
  716. }
  717. case APPEARANCE_SC:{
  718. entity->features.skin_color = color;
  719. break;
  720. }
  721. case APPEARANCE_EC:{
  722. entity->features.eye_color = color;
  723. break;
  724. }
  725. case APPEARANCE_SOGA_EBT:{
  726. for(int i=0;i<3;i++)
  727. entity->features.soga_eye_brow_type[i] = atoi(row[3+i]);
  728. break;
  729. }
  730. case APPEARANCE_SOGA_CHEEKT:{
  731. for(int i=0;i<3;i++)
  732. entity->features.soga_cheek_type[i] = atoi(row[3+i]);
  733. break;
  734. }
  735. case APPEARANCE_SOGA_NT:{
  736. for(int i=0;i<3;i++)
  737. entity->features.soga_nose_type[i] = atoi(row[3+i]);
  738. break;
  739. }
  740. case APPEARANCE_SOGA_CHINT:{
  741. for(int i=0;i<3;i++)
  742. entity->features.soga_chin_type[i] = atoi(row[3+i]);
  743. break;
  744. }
  745. case APPEARANCE_SOGA_LT:{
  746. for(int i=0;i<3;i++)
  747. entity->features.soga_lip_type[i] = atoi(row[3+i]);
  748. break;
  749. }
  750. case APPEARANCE_SOGA_EART:{
  751. for(int i=0;i<3;i++)
  752. entity->features.soga_ear_type[i] = atoi(row[3+i]);
  753. break;
  754. }
  755. case APPEARANCE_SOGA_EYET:{
  756. for(int i=0;i<3;i++)
  757. entity->features.soga_eye_type[i] = atoi(row[3+i]);
  758. break;
  759. }
  760. case APPEARANCE_EBT:{
  761. for(int i=0;i<3;i++)
  762. entity->features.eye_brow_type[i] = atoi(row[3+i]);
  763. break;
  764. }
  765. case APPEARANCE_CHEEKT:{
  766. for(int i=0;i<3;i++)
  767. entity->features.cheek_type[i] = atoi(row[3+i]);
  768. break;
  769. }
  770. case APPEARANCE_NT:{
  771. for(int i=0;i<3;i++)
  772. entity->features.nose_type[i] = atoi(row[3+i]);
  773. break;
  774. }
  775. case APPEARANCE_CHINT:{
  776. for(int i=0;i<3;i++)
  777. entity->features.chin_type[i] = atoi(row[3+i]);
  778. break;
  779. }
  780. case APPEARANCE_EART:{
  781. for(int i=0;i<3;i++)
  782. entity->features.ear_type[i] = atoi(row[3+i]);
  783. break;
  784. }
  785. case APPEARANCE_EYET:{
  786. for(int i=0;i<3;i++)
  787. entity->features.eye_type[i] = atoi(row[3+i]);
  788. break;
  789. }
  790. case APPEARANCE_LT:{
  791. for(int i=0;i<3;i++)
  792. entity->features.lip_type[i] = atoi(row[3+i]);
  793. break;
  794. }
  795. case APPEARANCE_SHIRT:{
  796. entity->features.shirt_color = color;
  797. break;
  798. }
  799. case APPEARANCE_UCC:{
  800. break;
  801. }
  802. case APPEARANCE_PANTS:{
  803. entity->features.pants_color = color;
  804. break;
  805. }
  806. case APPEARANCE_ULC:{
  807. break;
  808. }
  809. case APPEARANCE_U9:{
  810. break;
  811. }
  812. case APPEARANCE_BODY_SIZE:{
  813. entity->features.body_size = color.red;
  814. break;
  815. }
  816. case APPEARANCE_SOGA_WC1:{
  817. break;
  818. }
  819. case APPEARANCE_SOGA_WC2:{
  820. break;
  821. }
  822. case APPEARANCE_SOGA_SHIRT:{
  823. break;
  824. }
  825. case APPEARANCE_SOGA_UCC:{
  826. break;
  827. }
  828. case APPEARANCE_SOGA_PANTS:{
  829. break;
  830. }
  831. case APPEARANCE_SOGA_ULC:{
  832. break;
  833. }
  834. case APPEARANCE_SOGA_U13:{
  835. break;
  836. }
  837. case APPEARANCE_BODY_AGE: {
  838. entity->features.body_age = color.red;
  839. break;
  840. }
  841. case APPEARANCE_MC:{
  842. entity->features.model_color = color;
  843. break;
  844. }
  845. case APPEARANCE_SMC:{
  846. entity->features.soga_model_color = color;
  847. break;
  848. }
  849. case APPEARANCE_SBS: {
  850. entity->features.soga_body_size = color.red;
  851. break;
  852. }
  853. case APPEARANCE_SBA: {
  854. entity->features.soga_body_age = color.red;
  855. break;
  856. }
  857. }
  858. entity->info_changed = true;
  859. }
  860. return count;
  861. }
  862. void WorldDatabase::LoadNPCs(ZoneServer* zone){
  863. Query query;
  864. MYSQL_ROW row;
  865. NPC* npc = 0;
  866. int32 id = 0;
  867. int32 total = 0;
  868. MYSQL_RES* result = query.RunQuery2(Q_SELECT,"SELECT npc.spawn_id, s.name, npc.min_level, npc.max_level, npc.enc_level, s.race, s.model_type, npc.class_, npc.gender, s.command_primary, s.command_secondary, s.show_name, npc.min_group_size, npc.max_group_size, npc.hair_type_id, npc.facial_hair_type_id, npc.wing_type_id, npc.chest_type_id, npc.legs_type_id, npc.soga_hair_type_id, npc.soga_facial_hair_type_id, s.attackable, s.show_level, s.targetable, s.show_command_icon, s.display_hand_icon, s.hp, s.power, s.size, s.collision_radius, npc.action_state, s.visual_state, npc.mood_state, npc.initial_state, npc.activity_status, s.faction_id, s.sub_title, s.merchant_id, s.merchant_type, s.size_offset, npc.attack_type, npc.ai_strategy+0, npc.spell_list_id, npc.secondary_spell_list_id, npc.skill_list_id, npc.secondary_skill_list_id, npc.equipment_list_id, npc.str, npc.sta, npc.wis, npc.intel, npc.agi, npc.heat, npc.cold, npc.magic, npc.mental, npc.divine, npc.disease, npc.poison, npc.aggro_radius, npc.cast_percentage, npc.randomize, npc.soga_model_type, npc.heroic_flag, npc.alignment, npc.elemental, npc.arcane, npc.noxious, s.savagery, s.dissonance, npc.hide_hood, npc.emote_state, s.prefix, s.suffix, s.last_name, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, npc.water_type, npc.flying_type, s.loot_tier, s.loot_drop_type, npc.scared_by_strong_players, npc.action_state_str\n"
  869. "FROM spawn s\n"
  870. "INNER JOIN spawn_npcs npc\n"
  871. "ON s.id = npc.spawn_id\n"
  872. "INNER JOIN spawn_location_entry le\n"
  873. "ON npc.spawn_id = le.spawn_id\n"
  874. "INNER JOIN spawn_location_placement lp\n"
  875. "ON le.spawn_location_id = lp.spawn_location_id\n"
  876. "WHERE lp.zone_id = %u and (lp.instance_id = 0 or lp.instance_id = %u)\n"
  877. "GROUP BY s.id",
  878. zone->GetZoneID(), zone->GetInstanceID());
  879. while(result && (row = mysql_fetch_row(result))){
  880. /*npc->SetAppearanceID(atoi(row[12]));
  881. AppearanceData* appearance = world.GetNPCAppearance(npc->GetAppearanceID());
  882. if(appearance)
  883. memcpy(&npc->appearance, appearance, sizeof(AppearanceData));
  884. */
  885. int32 npcXpackFlag = atoul(row[75]);
  886. int32 npcHolidayFlag = atoul(row[76]);
  887. id = atoul(row[0]);
  888. if(zone->GetNPC(id, true))
  889. continue;
  890. npc = new NPC();
  891. if (!CheckExpansionFlags(zone, npcXpackFlag) || !CheckHolidayFlags(zone, npcHolidayFlag))
  892. npc->SetOmittedByDBFlag(true);
  893. npc->SetDatabaseID(id);
  894. strcpy(npc->appearance.name, row[1]);
  895. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[9]));
  896. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[10]));
  897. if(primary_command_list){
  898. npc->SetPrimaryCommands(primary_command_list);
  899. npc->primary_command_list_id = atoul(row[9]);
  900. }
  901. if(secondary_command_list){
  902. npc->SetSecondaryCommands(secondary_command_list);
  903. npc->secondary_command_list_id = atoul(row[10]);
  904. }
  905. npc->appearance.min_level = atoi(row[2]);
  906. npc->appearance.max_level = atoi(row[3]);
  907. npc->appearance.level = atoi(row[2]);
  908. npc->appearance.difficulty = atoi(row[4]);
  909. npc->appearance.race = atoi(row[5]);
  910. npc->appearance.model_type = atoi(row[6]);
  911. npc->appearance.soga_model_type = atoi(row[62]);
  912. npc->appearance.adventure_class = atoi(row[7]);
  913. npc->appearance.gender = atoi(row[8]);
  914. npc->appearance.display_name = atoi(row[11]);
  915. npc->features.hair_type = atoi(row[14]);
  916. npc->features.hair_face_type = atoi(row[15]);
  917. npc->features.wing_type = atoi(row[16]);
  918. npc->features.chest_type = atoi(row[17]);
  919. npc->features.legs_type = atoi(row[18]);
  920. npc->features.soga_hair_type = atoi(row[19]);
  921. npc->features.soga_hair_face_type = atoi(row[20]);
  922. npc->appearance.attackable = atoi(row[21]);
  923. npc->appearance.show_level = atoi(row[22]);
  924. npc->appearance.targetable = atoi(row[23]);
  925. npc->appearance.show_command_icon = atoi(row[24]);
  926. npc->appearance.display_hand_icon = atoi(row[25]);
  927. npc->appearance.hide_hood = atoi(row[70]);
  928. npc->appearance.randomize = atoi(row[61]);
  929. npc->SetTotalHP(atoul(row[26]));
  930. npc->SetTotalHPBaseInstance(atoul(row[26]));
  931. npc->SetTotalPower(atoul(row[27]));
  932. npc->SetTotalPowerBaseInstance(atoul(row[27]));
  933. npc->SetHP(npc->GetTotalHP());
  934. npc->SetPower(npc->GetTotalPower());
  935. if(npc->GetTotalHP() == 0){
  936. npc->SetTotalHP(15*npc->GetLevel() + 1);
  937. npc->SetHP(15*npc->GetLevel() + 1);
  938. }
  939. if(npc->GetTotalPower() == 0){
  940. npc->SetTotalPower(15*npc->GetLevel() + 1);
  941. npc->SetPower(15*npc->GetLevel() + 1);
  942. }
  943. npc->size = atoi(row[28]);
  944. npc->appearance.pos.collision_radius = atoi(row[29]);
  945. npc->appearance.action_state = atoi(row[30]);
  946. npc->appearance.visual_state = atoi(row[31]);
  947. npc->appearance.mood_state = atoi(row[32]);
  948. npc->appearance.emote_state = atoi(row[71]);
  949. npc->appearance.pos.state = atoi(row[33]);
  950. npc->appearance.activity_status = atoi(row[34]);
  951. npc->faction_id = atoul(row[35]);
  952. if(row[36]){
  953. std::string sub_title = std::string(row[36]);
  954. if(strncmp(row[36],"<Collector>", 11) == 0) {
  955. npc->SetCollector(true);
  956. }
  957. if(strlen(row[36]) < sizeof(npc->appearance.sub_title))
  958. strcpy(npc->appearance.sub_title, row[36]);
  959. else
  960. strncpy(npc->appearance.sub_title, row[36], sizeof(npc->appearance.sub_title));
  961. }
  962. npc->SetMerchantID(atoul(row[37]));
  963. npc->SetMerchantType(atoi(row[38]));
  964. npc->SetSizeOffset(atoi(row[39]));
  965. npc->SetAIStrategy(atoi(row[41]));
  966. npc->SetPrimarySpellList(atoul(row[42]));
  967. npc->SetSecondarySpellList(atoul(row[43]));
  968. npc->SetPrimarySkillList(atoul(row[44]));
  969. npc->SetSecondarySkillList(atoul(row[45]));
  970. npc->SetEquipmentListID(atoul(row[46]));
  971. InfoStruct* info = npc->GetInfoStruct();
  972. info->set_attack_type(atoi(row[40]));
  973. info->set_str_base(atoi(row[47]));
  974. info->set_sta_base(atoi(row[48]));
  975. info->set_wis_base(atoi(row[49]));
  976. info->set_intel_base(atoi(row[50]));
  977. info->set_agi_base(atoi(row[51]));
  978. info->set_heat_base(atoi(row[52]));
  979. info->set_cold_base(atoi(row[53]));
  980. info->set_magic_base(atoi(row[54]));
  981. info->set_mental_base(atoi(row[55]));
  982. info->set_divine_base(atoi(row[56]));
  983. info->set_disease_base(atoi(row[57]));
  984. info->set_poison_base(atoi(row[58]));
  985. info->set_alignment(atoi(row[64]));
  986. npc->SetAggroRadius(atof(row[59]));
  987. npc->SetCastPercentage(atoi(row[60]));
  988. npc->appearance.heroic_flag = atoi(row[63]);
  989. info->set_elemental_base(atoi(row[65]));
  990. info->set_arcane_base(atoi(row[66]));
  991. info->set_noxious_base(atoi(row[67]));
  992. npc->SetTotalSavagery(atoul(row[68]));
  993. npc->SetTotalDissonance(atoul(row[69]));
  994. npc->SetSavagery(npc->GetTotalSavagery());
  995. npc->SetDissonance(npc->GetTotalDissonance());
  996. if(npc->GetTotalSavagery() == 0){
  997. npc->SetTotalSavagery(15*npc->GetLevel() + 1);
  998. npc->SetSavagery(15*npc->GetLevel() + 1);
  999. }
  1000. if(npc->GetTotalDissonance() == 0){
  1001. npc->SetTotalDissonance(15*npc->GetLevel() + 1);
  1002. npc->SetDissonance(15*npc->GetLevel() + 1);
  1003. }
  1004. npc->SetPrefixTitle(row[72]);
  1005. npc->SetSuffixTitle(row[73]);
  1006. npc->SetLastName(row[74]);
  1007. // xpack+holiday value handled at top at position 75+76
  1008. int8 disableSounds = atoul(row[77]);
  1009. npc->SetSoundsDisabled(disableSounds);
  1010. npc->SetMerchantLevelRange(atoul(row[78]), atoul(row[79]));
  1011. npc->SetAAXPRewards(atoul(row[80]));
  1012. info->set_water_type(atoul(row[81]));
  1013. info->set_flying_type(atoul(row[82]));
  1014. npc->SetLootTier(atoul(row[83]));
  1015. npc->SetLootDropType(atoul(row[84]));
  1016. npc->SetScaredByStrongPlayers(atoul(row[85]));
  1017. if(row[86]){
  1018. std::string action_state_str = std::string(row[86]);
  1019. npc->GetInfoStruct()->set_action_state(action_state_str);
  1020. }
  1021. zone->AddNPC(id, npc);
  1022. total++;
  1023. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading NPC: '%s' (%u)", npc->appearance.name, id);
  1024. }
  1025. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC(s).", total);
  1026. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Skill(s).", LoadNPCSkills(zone));
  1027. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Equipment Piece(s).", LoadNPCEquipment(zone));
  1028. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Appearance(s).", LoadAppearances(zone));
  1029. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i NPC Equipment Appearance(s).", LoadNPCAppearanceEquipmentData(zone));
  1030. }
  1031. void WorldDatabase::LoadSpiritShards(ZoneServer* zone){
  1032. Query query;
  1033. MYSQL_ROW row;
  1034. int32 id = 0;
  1035. int32 total = 0;
  1036. MYSQL_RES* result = query.RunQuery2(Q_SELECT,"SELECT timestamp, name, level, race, gender, adventure_class, model_type, soga_model_type, hair_type, hair_face_type, wing_type, chest_type, legs_type, soga_hair_type, soga_hair_face_type, hide_hood, size, collision_radius, action_state, visual_state, mood_state, emote_state, pos_state, activity_status, sub_title, prefix_title, suffix_title, lastname, x, y, z, heading, gridid, id, charid\n"
  1037. "FROM character_spirit_shards\n"
  1038. "WHERE zoneid = %u and (instanceid = 0 or instanceid = %u)",
  1039. zone->GetZoneID(), zone->GetInstanceID());
  1040. while(result && (row = mysql_fetch_row(result))){
  1041. /*npc->SetAppearanceID(atoi(row[12]));
  1042. AppearanceData* appearance = world.GetNPCAppearance(npc->GetAppearanceID());
  1043. if(appearance)
  1044. memcpy(&npc->appearance, appearance, sizeof(AppearanceData));
  1045. */
  1046. sint64 timestamp = 0;
  1047. #ifdef WIN32
  1048. timestamp = _strtoui64(row[0], NULL, 10);
  1049. #else
  1050. timestamp = strtoull(row[0], 0, 10);
  1051. #endif
  1052. if(!row[1])
  1053. continue;
  1054. NPC* shard = new NPC();
  1055. shard->SetShardCreatedTimestamp(timestamp);
  1056. strcpy(shard->appearance.name, row[1]);
  1057. shard->appearance.level = atoul(row[2]);
  1058. shard->appearance.race = atoul(row[3]);
  1059. shard->appearance.gender = atoul(row[4]);
  1060. shard->appearance.adventure_class = atoul(row[5]);
  1061. shard->appearance.model_type = atoul(row[6]);
  1062. shard->appearance.soga_model_type = atoul(row[7]);
  1063. shard->appearance.display_name = 1;
  1064. shard->features.hair_type = atoul(row[8]);
  1065. shard->features.hair_face_type = atoul(row[9]);
  1066. shard->features.wing_type = atoul(row[10]);
  1067. shard->features.chest_type = atoul(row[11]);
  1068. shard->features.legs_type = atoul(row[12]);
  1069. shard->features.soga_hair_type = atoul(row[13]);
  1070. shard->features.soga_hair_face_type = atoul(row[14]);
  1071. shard->appearance.attackable = 0;
  1072. shard->appearance.show_level = 1;
  1073. shard->appearance.targetable = 1;
  1074. shard->appearance.show_command_icon = 1;
  1075. shard->appearance.display_hand_icon = 0;
  1076. shard->appearance.hide_hood = atoul(row[15]);
  1077. shard->size = atoul(row[16]);
  1078. shard->appearance.pos.collision_radius = atoul(row[17]);
  1079. shard->appearance.action_state = atoul(row[18]);
  1080. shard->appearance.visual_state = atoul(row[19]); // ghostly look
  1081. shard->appearance.mood_state = atoul(row[20]);
  1082. shard->appearance.emote_state = atoul(row[21]);
  1083. shard->appearance.pos.state = atoul(row[22]);
  1084. shard->appearance.activity_status = atoul(row[23]);
  1085. if(row[24])
  1086. strncpy(shard->appearance.sub_title, row[24], sizeof(shard->appearance.sub_title));
  1087. if(row[25])
  1088. shard->SetPrefixTitle(row[25]);
  1089. if(row[26])
  1090. shard->SetSuffixTitle(row[26]);
  1091. if(row[27])
  1092. shard->SetLastName(row[27]);
  1093. shard->SetX(atof(row[28]));
  1094. shard->SetY(atof(row[29]));
  1095. shard->SetZ(atof(row[30]));
  1096. shard->SetHeading(atof(row[31]));
  1097. shard->SetSpawnOrigX(shard->GetX());
  1098. shard->SetSpawnOrigY(shard->GetY());
  1099. shard->SetSpawnOrigZ(shard->GetZ());
  1100. shard->SetSpawnOrigHeading(shard->GetHeading());
  1101. shard->SetLocation(atoul(row[32]));
  1102. shard->SetShardID(atoul(row[33]));
  1103. shard->SetShardCharID(atoul(row[34]));
  1104. shard->SetAlive(false);
  1105. const char* script = rule_manager.GetGlobalRule(R_Combat, SpiritShardSpawnScript)->GetString();
  1106. if(script)
  1107. {
  1108. shard->SetSpawnScript(script);
  1109. zone->CallSpawnScript(shard, SPAWN_SCRIPT_PRESPAWN);
  1110. }
  1111. zone->AddSpawn(shard);
  1112. if(script)
  1113. zone->CallSpawnScript(shard, SPAWN_SCRIPT_SPAWN);
  1114. total++;
  1115. LogWrite(NPC__DEBUG, 5, "NPC", "---Loading Player Spirit Shard: '%s' (%u)", shard->appearance.name, id);
  1116. }
  1117. LogWrite(NPC__INFO, 0, "NPC", "--Loaded %i Spirit Shard(s).", total);
  1118. }
  1119. void WorldDatabase::LoadSigns(ZoneServer* zone){
  1120. Query query;
  1121. MYSQL_ROW row;
  1122. Sign* sign = 0;
  1123. int32 id = 0;
  1124. int32 total = 0;
  1125. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT ss.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, ss.widget_id, ss.widget_x, ss.widget_y, ss.widget_z, s.command_primary, s.command_secondary, s.collision_radius, ss.icon, ss.type, ss.title, ss.description, ss.sign_distance, ss.zone_id, ss.zone_x, ss.zone_y, ss.zone_z, ss.zone_heading, ss.include_heading, ss.include_location, s.transport_id, s.size_offset, s.display_hand_icon, s.visual_state, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, s.loot_tier, s.loot_drop_type, ss.language\n"
  1126. "FROM spawn s\n"
  1127. "INNER JOIN spawn_signs ss\n"
  1128. "ON s.id = ss.spawn_id\n"
  1129. "INNER JOIN spawn_location_entry le\n"
  1130. "ON ss.spawn_id = le.spawn_id\n"
  1131. "INNER JOIN spawn_location_placement lp\n"
  1132. "ON le.spawn_location_id = lp.spawn_location_id\n"
  1133. "WHERE lp.zone_id = %u\n"
  1134. "GROUP BY s.id",
  1135. zone->GetZoneID());
  1136. while(result && (row = mysql_fetch_row(result))){
  1137. int32 signXpackFlag = atoul(row[28]);
  1138. int32 signHolidayFlag = atoul(row[29]);
  1139. id = atoul(row[0]);
  1140. if(zone->GetSign(id, true))
  1141. continue;
  1142. sign = new Sign();
  1143. if (!CheckExpansionFlags(zone, signXpackFlag) || !CheckHolidayFlags(zone, signHolidayFlag))
  1144. sign->SetOmittedByDBFlag(true);
  1145. sign->SetDatabaseID(id);
  1146. strcpy(sign->appearance.name, row[1]);
  1147. sign->appearance.model_type = atoi(row[2]);
  1148. sign->SetSize(atoi(row[3]));
  1149. sign->appearance.show_command_icon = atoi(row[4]);
  1150. sign->SetWidgetID(atoul(row[5]));
  1151. sign->SetWidgetX(atof(row[6]));
  1152. sign->SetWidgetY(atof(row[7]));
  1153. sign->SetWidgetZ(atof(row[8]));
  1154. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[9]));
  1155. if(primary_command_list){
  1156. sign->SetPrimaryCommands(primary_command_list);
  1157. sign->primary_command_list_id = atoul(row[9]);
  1158. }
  1159. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[10]));
  1160. if (secondary_command_list){
  1161. sign->SetSecondaryCommands(secondary_command_list);
  1162. sign->secondary_command_list_id = atoul(row[10]);
  1163. }
  1164. sign->appearance.pos.collision_radius = atoi(row[11]);
  1165. sign->SetSignIcon(atoi(row[12]));
  1166. if(strncasecmp(row[13],"Generic", 7) == 0)
  1167. sign->SetSignType(SIGN_TYPE_GENERIC);
  1168. else if(strncasecmp(row[13],"Zone", 4) == 0)
  1169. sign->SetSignType(SIGN_TYPE_ZONE);
  1170. sign->SetSignTitle(row[14]);
  1171. sign->SetSignDescription(row[15]);
  1172. sign->SetSignDistance(atof(row[16]));
  1173. sign->SetSignZoneID(atoul(row[17]));
  1174. sign->SetSignZoneX(atof(row[18]));
  1175. sign->SetSignZoneY(atof(row[19]));
  1176. sign->SetSignZoneZ(atof(row[20]));
  1177. sign->SetSignZoneHeading(atof(row[21]));
  1178. sign->SetIncludeHeading(atoi(row[22]) == 1);
  1179. sign->SetIncludeLocation(atoi(row[23]) == 1);
  1180. sign->SetTransporterID(atoul(row[24]));
  1181. sign->SetSizeOffset(atoi(row[25]));
  1182. sign->appearance.display_hand_icon = atoi(row[26]);
  1183. sign->SetVisualState(atoi(row[27]));
  1184. // xpack+holiday value handled at top at position 28+29
  1185. int8 disableSounds = atoul(row[30]);
  1186. sign->SetSoundsDisabled(disableSounds);
  1187. sign->SetMerchantLevelRange(atoul(row[31]), atoul(row[32]));
  1188. sign->SetAAXPRewards(atoul(row[33]));
  1189. sign->SetLootTier(atoul(row[34]));
  1190. sign->SetLootDropType(atoul(row[35]));
  1191. sign->SetLanguage(atoul(row[36]));
  1192. zone->AddSign(id, sign);
  1193. total++;
  1194. LogWrite(SIGN__DEBUG, 5, "Sign", "---Loading Sign: '%s' (%u).", sign->appearance.name, id);
  1195. }
  1196. LogWrite(SIGN__DEBUG, 0, "Sign", "--Loaded %i Sign(s)", total);
  1197. }
  1198. void WorldDatabase::LoadWidgets(ZoneServer* zone){
  1199. Query query;
  1200. MYSQL_ROW row;
  1201. Widget* widget = 0;
  1202. int32 id = 0;
  1203. int32 total = 0;
  1204. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT sw.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, sw.widget_id, sw.widget_x, sw.widget_y, sw.widget_z, s.command_primary, s.command_secondary, s.collision_radius, sw.include_heading, sw.include_location, sw.icon, sw.type, sw.open_heading, sw.open_y, sw.action_spawn_id, sw.open_sound_file, sw.close_sound_file, sw.open_duration, sw.closed_heading, sw.linked_spawn_id, sw.close_y, s.transport_id, s.size_offset, sw.house_id, sw.open_x, sw.open_z, sw.close_x, sw.close_z, s.display_hand_icon, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, s.loot_tier, s.loot_drop_type\n"
  1205. "FROM spawn s\n"
  1206. "INNER JOIN spawn_widgets sw\n"
  1207. "ON s.id = sw.spawn_id\n"
  1208. "INNER JOIN spawn_location_entry le\n"
  1209. "ON sw.spawn_id = le.spawn_id\n"
  1210. "INNER JOIN spawn_location_placement lp\n"
  1211. "ON le.spawn_location_id = lp.spawn_location_id\n"
  1212. "WHERE lp.zone_id = %u\n"
  1213. "GROUP BY s.id",
  1214. zone->GetZoneID());
  1215. while(result && (row = mysql_fetch_row(result))){
  1216. int32 widgetXpackFlag = atoul(row[33]);
  1217. int32 widgetHolidayFlag = atoul(row[34]);
  1218. id = atoul(row[0]);
  1219. if(zone->GetWidget(id, true))
  1220. continue;
  1221. widget = new Widget();
  1222. if (!CheckExpansionFlags(zone, widgetXpackFlag) || !CheckHolidayFlags(zone, widgetHolidayFlag))
  1223. widget->SetOmittedByDBFlag(true);
  1224. widget->SetDatabaseID(id);
  1225. strcpy(widget->appearance.name, row[1]);
  1226. widget->appearance.model_type = atoi(row[2]);
  1227. widget->SetSize(atoi(row[3]));
  1228. widget->appearance.show_command_icon = atoi(row[4]);
  1229. if (row[5] == NULL)
  1230. widget->SetWidgetID(0xFFFFFFFF);
  1231. else
  1232. widget->SetWidgetID(atoul(row[5]));
  1233. widget->SetWidgetX(atof(row[6]));
  1234. widget->SetWidgetY(atof(row[7]));
  1235. widget->SetWidgetZ(atof(row[8]));
  1236. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[9]));
  1237. if(primary_command_list){
  1238. widget->SetPrimaryCommands(primary_command_list);
  1239. widget->primary_command_list_id = atoul(row[9]);
  1240. }
  1241. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[10]));
  1242. if (secondary_command_list) {
  1243. widget->SetSecondaryCommands(secondary_command_list);
  1244. widget->secondary_command_list_id = atoul(row[10]);
  1245. }
  1246. widget->appearance.pos.collision_radius = atoi(row[11]);
  1247. widget->SetIncludeHeading(atoi(row[12]) == 1);
  1248. widget->SetIncludeLocation(atoi(row[13]) == 1);
  1249. widget->SetWidgetIcon(atoi(row[14]));
  1250. if (strncasecmp(row[15], "Generic", 7) == 0)
  1251. widget->SetWidgetType(WIDGET_TYPE_GENERIC);
  1252. else if (strncasecmp(row[15], "Door", 4) == 0)
  1253. widget->SetWidgetType(WIDGET_TYPE_DOOR);
  1254. else if (strncasecmp(row[15], "Lift", 4) == 0)
  1255. widget->SetWidgetType(WIDGET_TYPE_LIFT);
  1256. widget->SetOpenHeading(atof(row[16]));
  1257. widget->SetOpenY(atof(row[17]));
  1258. widget->SetActionSpawnID(atoul(row[18]));
  1259. if(row[19] && strlen(row[19]) > 5)
  1260. widget->SetOpenSound(row[19]);
  1261. if(row[20] && strlen(row[20]) > 5)
  1262. widget->SetCloseSound(row[20]);
  1263. widget->SetOpenDuration(atoi(row[21]));
  1264. widget->SetClosedHeading(atof(row[22]));
  1265. widget->SetLinkedSpawnID(atoul(row[23]));
  1266. widget->SetCloseY(atof(row[24]));
  1267. widget->SetTransporterID(atoul(row[25]));
  1268. widget->SetSizeOffset(atoi(row[26]));
  1269. widget->SetHouseID(atoul(row[27]));
  1270. widget->SetOpenX(atof(row[28]));
  1271. widget->SetOpenZ(atof(row[29]));
  1272. widget->SetCloseX(atof(row[30]));
  1273. widget->SetCloseZ(atof(row[31]));
  1274. widget->appearance.display_hand_icon = atoi(row[32]);
  1275. // xpack+holiday value handled at top at position 33+34
  1276. int8 disableSounds = atoul(row[35]);
  1277. widget->SetSoundsDisabled(disableSounds);
  1278. widget->SetMerchantLevelRange(atoul(row[36]), atoul(row[37]));
  1279. widget->SetAAXPRewards(atoul(row[38]));
  1280. widget->SetLootTier(atoul(row[39]));
  1281. widget->SetLootDropType(atoul(row[40]));
  1282. zone->AddWidget(id, widget);
  1283. total++;
  1284. LogWrite(WIDGET__DEBUG, 5, "Widget", "---Loading Widget: '%s' (%u).", widget->appearance.name, id);
  1285. }
  1286. LogWrite(WIDGET__DEBUG, 0, "Widget", "--Loaded %i Widget(s)", total);
  1287. }
  1288. void WorldDatabase::LoadObjects(ZoneServer* zone){
  1289. Query query;
  1290. MYSQL_ROW row;
  1291. Object* object = 0;
  1292. int32 id = 0;
  1293. int32 total = 0;
  1294. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT so.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, s.transport_id, s.size_offset, so.device_id, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, s.loot_tier, s.loot_drop_type\n"
  1295. "FROM spawn s\n"
  1296. "INNER JOIN spawn_objects so\n"
  1297. "ON s.id = so.spawn_id\n"
  1298. "INNER JOIN spawn_location_entry le\n"
  1299. "ON so.spawn_id = le.spawn_id\n"
  1300. "INNER JOIN spawn_location_placement lp\n"
  1301. "ON le.spawn_location_id = lp.spawn_location_id\n"
  1302. "WHERE lp.zone_id = %u and (lp.instance_id = 0 or lp.instance_id = %u)\n"
  1303. "GROUP BY s.id",
  1304. zone->GetZoneID(), zone->GetInstanceID());
  1305. while(result && (row = mysql_fetch_row(result))){
  1306. int32 objXpackFlag = atoul(row[19]);
  1307. int32 objHolidayFlag = atoul(row[20]);
  1308. id = atoul(row[0]);
  1309. if(zone->GetObject(id, true))
  1310. continue;
  1311. object = new Object();
  1312. if (!CheckExpansionFlags(zone, objXpackFlag) || !CheckHolidayFlags(zone, objHolidayFlag))
  1313. object->SetOmittedByDBFlag(true);
  1314. object->SetDatabaseID(id);
  1315. strcpy(object->appearance.name, row[1]);
  1316. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[4]));
  1317. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[5]));
  1318. if(primary_command_list){
  1319. object->SetPrimaryCommands(primary_command_list);
  1320. object->primary_command_list_id = atoul(row[4]);
  1321. }
  1322. if(secondary_command_list){
  1323. object->SetSecondaryCommands(secondary_command_list);
  1324. object->secondary_command_list_id = atoul(row[5]);
  1325. }
  1326. object->appearance.race = atoi(row[2]);
  1327. object->appearance.model_type = atoi(row[3]);
  1328. object->appearance.targetable = atoi(row[6]);
  1329. object->size = atoi(row[7]);
  1330. object->appearance.display_name = atoi(row[8]);
  1331. object->appearance.visual_state = atoi(row[9]);
  1332. object->appearance.attackable = atoi(row[10]);
  1333. object->appearance.show_level = atoi(row[11]);
  1334. object->appearance.show_command_icon = atoi(row[12]);
  1335. object->appearance.display_hand_icon = atoi(row[13]);
  1336. object->faction_id = atoul(row[14]);
  1337. object->appearance.pos.collision_radius = atoi(row[15]);
  1338. object->SetTransporterID(atoul(row[16]));
  1339. object->SetSizeOffset(atoi(row[17]));
  1340. object->SetDeviceID(atoi(row[18]));
  1341. // xpack value handled at top at position 19
  1342. int8 disableSounds = atoul(row[21]);
  1343. object->SetSoundsDisabled(disableSounds);
  1344. object->SetMerchantLevelRange(atoul(row[22]), atoul(row[23]));
  1345. object->SetAAXPRewards(atoul(row[24]));
  1346. object->SetLootTier(atoul(row[25]));
  1347. object->SetLootDropType(atoul(row[26]));
  1348. zone->AddObject(id, object);
  1349. total++;
  1350. LogWrite(OBJECT__DEBUG, 5, "Object", "---Loading Object: '%s' (%u)", object->appearance.name, id);
  1351. }
  1352. LogWrite(OBJECT__DEBUG, 0, "Object", "--Loaded %i Object(s)", total);
  1353. }
  1354. void WorldDatabase::LoadGroundSpawns(ZoneServer* zone){
  1355. Query query;
  1356. MYSQL_ROW row;
  1357. GroundSpawn* spawn = 0;
  1358. int32 id = 0;
  1359. int32 total = 0;
  1360. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT sg.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, sg.number_harvests, sg.num_attempts_per_harvest, sg.groundspawn_id, sg.collection_skill, s.size_offset, s.expansion_flag, s.holiday_flag, s.disable_sounds, s.aaxp_rewards, s.loot_tier, s.loot_drop_type\n"
  1361. "FROM spawn s\n"
  1362. "INNER JOIN spawn_ground sg\n"
  1363. "ON s.id = sg.spawn_id\n"
  1364. "INNER JOIN spawn_location_entry le\n"
  1365. "ON sg.spawn_id = le.spawn_id\n"
  1366. "INNER JOIN spawn_location_placement lp\n"
  1367. "ON le.spawn_location_id = lp.spawn_location_id\n"
  1368. "WHERE lp.zone_id = %u\n"
  1369. "GROUP BY s.id",
  1370. zone->GetZoneID());
  1371. while(result && (row = mysql_fetch_row(result))){
  1372. int32 gsXpackFlag = atoul(row[21]);
  1373. int32 gsHolidayFlag = atoul(row[22]);
  1374. id = atoul(row[0]);
  1375. if(zone->GetGroundSpawn(id, true))
  1376. continue;
  1377. spawn = new GroundSpawn();
  1378. if (!CheckExpansionFlags(zone, gsXpackFlag) || !CheckHolidayFlags(zone, gsHolidayFlag))
  1379. spawn->SetOmittedByDBFlag(true);
  1380. spawn->SetDatabaseID(id);
  1381. spawn->forceMapCheck = true;
  1382. strcpy(spawn->appearance.name, row[1]);
  1383. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(atoul(row[4]));
  1384. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(atoul(row[5]));
  1385. if(primary_command_list){
  1386. spawn->SetPrimaryCommands(primary_command_list);
  1387. spawn->primary_command_list_id = atoul(row[4]);
  1388. }
  1389. if(secondary_command_list){
  1390. spawn->SetSecondaryCommands(secondary_command_list);
  1391. spawn->secondary_command_list_id = atoul(row[5]);
  1392. }
  1393. spawn->appearance.race = atoi(row[2]);
  1394. spawn->appearance.model_type = atoi(row[3]);
  1395. spawn->appearance.targetable = atoi(row[6]);
  1396. spawn->size = atoi(row[7]);
  1397. spawn->appearance.display_name = atoi(row[8]);
  1398. spawn->appearance.visual_state = atoi(row[9]);
  1399. spawn->appearance.attackable = atoi(row[10]);
  1400. spawn->appearance.show_level = atoi(row[11]);
  1401. spawn->appearance.show_command_icon = atoi(row[12]);
  1402. spawn->appearance.display_hand_icon = atoi(row[13]);
  1403. spawn->faction_id = atoul(row[14]);
  1404. spawn->appearance.pos.collision_radius = atoi(row[15]);
  1405. spawn->SetNumberHarvests(atoi(row[16]));
  1406. spawn->SetAttemptsPerHarvest(atoi(row[17]));
  1407. spawn->SetGroundSpawnEntryID(atoul(row[18]));
  1408. spawn->SetCollectionSkill(row[19]);
  1409. spawn->SetSizeOffset(atoi(row[20]));
  1410. // xpack+holiday value handled at top at position 21+22
  1411. int8 disableSounds = atoul(row[23]);
  1412. spawn->SetSoundsDisabled(disableSounds);
  1413. spawn->SetAAXPRewards(atoul(row[24]));
  1414. spawn->SetLootTier(atoul(row[25]));
  1415. spawn->SetLootDropType(atoul(row[26]));
  1416. zone->AddGroundSpawn(id, spawn);
  1417. total++;
  1418. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn: '%s' (%u)", spawn->appearance.name, id);
  1419. }
  1420. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "--Loaded %i GroundSpawn(s)", total);
  1421. }
  1422. void WorldDatabase::LoadGroundSpawnItems(ZoneServer* zone) {
  1423. Query query;
  1424. MYSQL_ROW row;
  1425. int32 total = 0;
  1426. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT groundspawn_id, item_id, is_rare, grid_id FROM groundspawn_items;");
  1427. while(result && (row = mysql_fetch_row(result)))
  1428. {
  1429. zone->AddGroundSpawnItem(atoul(row[0]), atoul(row[1]), atoi(row[2]), atoul(row[3]));
  1430. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn Items: ID: %u\n", atoul(row[0]));
  1431. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---item: %ul, rare: %i, grid: %ul", atoul(row[1]), atoi(row[2]), atoul(row[3]));
  1432. total++;
  1433. }
  1434. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "--Loaded %i GroundSpawn Item%s.", total, total == 1 ? "" : "s");
  1435. }
  1436. void WorldDatabase::LoadGroundSpawnEntries(ZoneServer* zone) {
  1437. Query query;
  1438. MYSQL_ROW row;
  1439. int32 total = 0;
  1440. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT groundspawn_id, min_skill_level, min_adventure_level, bonus_table, harvest1, harvest3, harvest5, harvest_imbue, harvest_rare, harvest10, harvest_coin FROM groundspawns WHERE enabled = 1;");
  1441. while(result && (row = mysql_fetch_row(result)))
  1442. {
  1443. // this is getting ridonkulous...
  1444. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn ID: %u\n" \
  1445. "---min_skill_level: %i, min_adventure_level: %i, bonus_table: %i\n" \
  1446. "---harvest1: %.2f, harvest3: %.2f, harvest5: %.2f\n" \
  1447. "---harvest_imbue: %.2f, harvest_rare: %.2f, harvest10: %.2f\n" \
  1448. "---harvest_coin: %u", atoul(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), atof(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atof(row[9]), atoul(row[10]));
  1449. zone->AddGroundSpawnEntry(atoul(row[0]), atoi(row[1]), atoi(row[2]), atoi(row[3]), atof(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atof(row[9]), atoul(row[10]));
  1450. total++;
  1451. }
  1452. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "--Loaded %i GroundSpawn Entr%s.", total, total == 1 ? "y" : "ies");
  1453. LoadGroundSpawnItems(zone);
  1454. }
  1455. bool WorldDatabase::LoadCharacterStats(int32 id, int32 account_id, Client* client)
  1456. {
  1457. DatabaseResult result;
  1458. if( database_new.Select(&result, "SELECT * FROM character_details WHERE char_id = %i LIMIT 0, 1", id) )
  1459. {
  1460. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading character_details for '%s' (char_id: %u)", client->GetPlayer()->GetName(), id);
  1461. while( result.Next() )
  1462. {
  1463. InfoStruct* info = client->GetPlayer()->GetInfoStruct();
  1464. // we need stats assigned before we try to assign hp/power
  1465. info->set_str_base(result.GetInt16Str("str"));
  1466. info->set_sta_base(result.GetInt16Str("sta"));
  1467. info->set_agi_base(result.GetInt16Str("agi"));
  1468. info->set_wis_base(result.GetInt16Str("wis"));
  1469. info->set_intel_base(result.GetInt16Str("intel"));
  1470. info->set_sta(info->get_sta_base());
  1471. info->set_agi(info->get_agi_base());
  1472. info->set_str(info->get_str_base());
  1473. info->set_wis(info->get_wis_base());
  1474. info->set_intel(info->get_intel_base());
  1475. // must have totals up top before we set the current 'hp' / 'power'
  1476. client->GetPlayer()->CalculatePlayerHPPower();
  1477. client->GetPlayer()->SetHP(result.GetSInt32Str("hp"));
  1478. client->GetPlayer()->SetPower(result.GetSInt32Str("power"));
  1479. info->set_max_concentration_base(result.GetInt8Str("max_concentration"));
  1480. if (info->get_max_concentration_base() == 0)
  1481. info->set_max_concentration_base(5);
  1482. info->set_attack_base(result.GetInt16Str("attack"));
  1483. info->set_mitigation_base(result.GetInt16Str("mitigation"));
  1484. info->set_avoidance_base(result.GetInt16Str("avoidance"));
  1485. info->set_parry_base(result.GetInt16Str("parry"));
  1486. info->set_deflection_base(result.GetInt16Str("deflection"));
  1487. info->set_block_base(result.GetInt16Str("block"));
  1488. // old resist types
  1489. info->set_heat_base(result.GetInt16Str("heat"));
  1490. info->set_cold_base(result.GetInt16Str("cold"));
  1491. info->set_magic_base(result.GetInt16Str("magic"));
  1492. info->set_mental_base(result.GetInt16Str("mental"));
  1493. info->set_divine_base(result.GetInt16Str("divine"));
  1494. info->set_disease_base(result.GetInt16Str("disease"));
  1495. info->set_poison_base(result.GetInt16Str("poison"));
  1496. //
  1497. info->set_coin_copper(result.GetInt32Str("coin_copper"));
  1498. info->set_coin_silver(result.GetInt32Str("coin_silver"));
  1499. info->set_coin_gold(result.GetInt32Str("coin_gold"));
  1500. info->set_coin_plat(result.GetInt32Str("coin_plat"));
  1501. const char* bio = result.GetStringStr("biography");
  1502. if(bio && strlen(bio) > 0)
  1503. info->set_biography(std::string(bio));
  1504. info->set_status_points(result.GetInt32Str("status_points"));
  1505. client->GetPlayer()->GetPlayerInfo()->SetBindZone(result.GetInt32Str("bind_zone_id"));
  1506. client->GetPlayer()->GetPlayerInfo()->SetBindX(result.GetFloatStr("bind_x"));
  1507. client->GetPlayer()->GetPlayerInfo()->SetBindY(result.GetFloatStr("bind_y"));
  1508. client->GetPlayer()->GetPlayerInfo()->SetBindZ(result.GetFloatStr("bind_z"));
  1509. client->GetPlayer()->GetPlayerInfo()->SetBindHeading(result.GetFloatStr("bind_heading"));
  1510. client->GetPlayer()->GetPlayerInfo()->SetHouseZone(result.GetInt32Str("house_zone_id"));
  1511. client->GetPlayer()->SetAssignedAA(result.GetInt16Str("assigned_aa"));
  1512. client->GetPlayer()->SetUnassignedAA(result.GetInt16Str("unassigned_aa"));
  1513. client->GetPlayer()->SetTradeskillAA(result.GetInt16Str("tradeskill_aa"));
  1514. client->GetPlayer()->SetUnassignedTradeskillAA(result.GetInt16Str("unassigned_tradeskill_aa"));
  1515. client->GetPlayer()->SetPrestigeAA(result.GetInt16Str("prestige_aa"));
  1516. client->GetPlayer()->SetUnassignedPrestigeAA(result.GetInt16Str("unassigned_prestige_aa"));
  1517. client->GetPlayer()->SetTradeskillPrestigeAA(result.GetInt16Str("tradeskill_prestige_aa"));
  1518. client->GetPlayer()->SetUnassignedTradeskillPrestigeAA(result.GetInt16Str("unassigned_tradeskill_prestige_aa"));
  1519. info->set_xp(result.GetInt32Str("xp"));
  1520. info->set_xp_needed(result.GetInt32Str("xp_needed"));
  1521. if(info->get_xp_needed()== 0)
  1522. client->GetPlayer()->SetNeededXP();
  1523. info->set_xp_debt(result.GetFloatStr("xp_debt"));
  1524. info->set_xp_vitality(result.GetFloatStr("xp_vitality"));
  1525. info->set_ts_xp(result.GetInt32Str("tradeskill_xp"));
  1526. info->set_ts_xp_needed(result.GetInt32Str("tradeskill_xp_needed"));
  1527. if (info->get_ts_xp_needed() == 0)
  1528. client->GetPlayer()->SetNeededTSXP();
  1529. info->set_tradeskill_xp_vitality(result.GetFloatStr("tradeskill_xp_vitality"));
  1530. info->set_bank_coin_copper(result.GetInt32Str("bank_copper"));
  1531. info->set_bank_coin_silver(result.GetInt32Str("bank_silver"));
  1532. info->set_bank_coin_gold(result.GetInt32Str("bank_gold"));
  1533. info->set_bank_coin_plat(result.GetInt32Str("bank_plat"));
  1534. client->GetPlayer()->SetCombatVoice(result.GetInt16Str("combat_voice"));
  1535. client->GetPlayer()->SetEmoteVoice(result.GetInt16Str("emote_voice"));
  1536. client->GetPlayer()->SetBiography(result.GetStringStr("biography"));
  1537. client->GetPlayer()->GetInfoStruct()->set_flags(result.GetInt32Str("flags"));
  1538. client->GetPlayer()->GetInfoStruct()->set_flags2(result.GetInt32Str("flags2"));
  1539. client->GetPlayer()->SetLastName(result.GetStringStr("last_name"));
  1540. // new resist types
  1541. info->set_elemental_base(result.GetInt16Str("elemental"));
  1542. info->set_arcane_base(result.GetInt16Str("arcane"));
  1543. info->set_noxious_base(result.GetInt16Str("noxious"));
  1544. // new savagery and dissonance
  1545. client->GetPlayer()->SetSavagery(result.GetSInt16Str("savagery"));
  1546. client->GetPlayer()->SetDissonance(result.GetSInt16Str("dissonance"));
  1547. client->GetPlayer()->SetTotalSavageryBase(client->GetPlayer()->GetTotalSavagery());
  1548. client->GetPlayer()->SetTotalDissonanceBase(client->GetPlayer()->GetTotalDissonance());
  1549. client->GetPlayer()->SetCurrentLanguage(result.GetInt32Str("current_language"));
  1550. std::string petName = result.GetStringStr("pet_name");
  1551. boost::algorithm::to_lower(petName);
  1552. if(petName != "no pet") { // some reason we default to "no pet", the code handles an empty string as setting a random pet name when its summoned
  1553. client->GetPlayer()->GetInfoStruct()->set_pet_name(result.GetStringStr("pet_name"));
  1554. }
  1555. }
  1556. return true;
  1557. }
  1558. else
  1559. {
  1560. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character_details for '%s' (char_id: %u)", client->GetPlayer()->GetName(), id);
  1561. return false;
  1562. }
  1563. }
  1564. bool WorldDatabase::loadCharacter(const char* ch_name, int32 account_id, Client* client){
  1565. Query query, query4;
  1566. MYSQL_ROW row, row4;
  1567. int32 id = 0;
  1568. query.escaped_name = getEscapeString(ch_name);
  1569. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, current_zone_id, x, y, z, heading, admin_status, race, model_type, class, deity, level, gender, tradeskill_class, tradeskill_level, wing_type, hair_type, chest_type, legs_type, soga_wing_type, soga_hair_type, soga_chest_type, soga_legs_type, 0xFFFFFFFF - crc32(name), facial_hair_type, soga_facial_hair_type, instance_id, group_id, last_saved, DATEDIFF(curdate(), created_date) as accage, alignment, first_world_login FROM characters where name='%s' and account_id=%i AND deleted = 0", query.escaped_name, account_id);
  1570. // no character found
  1571. if ( result == NULL ) {
  1572. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character for '%s'", ch_name);
  1573. return false;
  1574. }
  1575. if (mysql_num_rows(result) == 1){
  1576. row = mysql_fetch_row(result);
  1577. id = strtoul(row[0], NULL, 0);
  1578. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading character for '%s' (char_id: %u)", ch_name, id);
  1579. client->SetCharacterID(id);
  1580. client->GetPlayer()->SetCharacterID(id);
  1581. client->SetAccountID(account_id);
  1582. client->GetPlayer()->SetName(ch_name);
  1583. client->GetPlayer()->SetX(atof(row[2]));
  1584. client->GetPlayer()->SetY(atof(row[3]));
  1585. client->GetPlayer()->SetZ(atof(row[4]));
  1586. client->GetPlayer()->SetHeading(atof(row[5]));
  1587. client->SetAdminStatus(atoi(row[6]));
  1588. client->GetPlayer()->SetRace(atoi(row[7]));
  1589. client->GetPlayer()->SetModelType(atoi(row[8]));
  1590. client->GetPlayer()->SetAdventureClass(atoi(row[9]));
  1591. client->GetPlayer()->SetDeity(atoi(row[10]));
  1592. client->GetPlayer()->SetLevel(atoi(row[11]));
  1593. client->GetPlayer()->SetGender(atoi(row[12]));
  1594. client->GetPlayer()->SetTradeskillClass(atoi(row[13]));
  1595. client->GetPlayer()->SetTSLevel(atoi(row[14]));
  1596. client->GetPlayer()->features.wing_type = atoi(row[15]);
  1597. client->GetPlayer()->features.hair_type = atoi(row[16]);
  1598. client->GetPlayer()->features.chest_type = atoi(row[17]);
  1599. client->GetPlayer()->features.legs_type = atoi(row[18]);
  1600. client->GetPlayer()->features.wing_type = atoi(row[19]);
  1601. client->GetPlayer()->features.soga_hair_type = atoi(row[20]);
  1602. client->GetPlayer()->features.soga_chest_type = atoi(row[21]);
  1603. client->GetPlayer()->features.soga_legs_type = atoi(row[22]);
  1604. client->SetNameCRC(atoul(row[23]));
  1605. client->GetPlayer()->features.hair_face_type = atoi(row[24]);
  1606. client->GetPlayer()->features.soga_hair_face_type = atoi(row[25]);
  1607. int32 instanceid = atoi(row[26]);
  1608. int32 groupid = atoi(row[27]);
  1609. client->SetRejoinGroupID(groupid);
  1610. int32 zoneid = atoul(row[1]);
  1611. /*
  1612. JA Notes on SOGA: I think there are many more settings to add than were commented out here,
  1613. because I can load a SOGA model player, but some features are missing (Barbarian WOAD, Skin tons, etc)
  1614. SOGA chars looked ok in LoginServer screen tho... odd.
  1615. */
  1616. // load character instances here
  1617. if ( LoadCharacterInstances(client) )
  1618. client->UpdateCharacterInstances();
  1619. if ( instanceid > 0 )
  1620. client->SetCurrentZoneByInstanceID(instanceid, zoneid);
  1621. else
  1622. client->SetCurrentZone(zoneid);
  1623. int32 lastsavedtime = atoi(row[28]);
  1624. client->SetLastSavedTimeStamp(lastsavedtime);
  1625. if (row[29])
  1626. client->GetPlayer()->GetPlayerInfo()->SetAccountAge(atoi(row[29]));
  1627. client->GetPlayer()->GetInfoStruct()->set_alignment(atoi(row[30]));
  1628. client->GetPlayer()->GetInfoStruct()->set_first_world_login(atoi(row[31]));
  1629. LoadCharacterFriendsIgnoreList(client->GetPlayer());
  1630. MYSQL_RES* result4 = query4.RunQuery2(Q_SELECT, "SELECT `guild_id` FROM `guild_members` WHERE `char_id`=%u", id);
  1631. if (result4 && (row4 = mysql_fetch_row(result4))) {
  1632. Guild* guild = guild_list.GetGuild(atoul(row4[0]));
  1633. if (guild) {
  1634. client->GetPlayer()->SetGuild(guild);
  1635. string subtitle;
  1636. subtitle.append("<").append(guild->GetName()).append(">");
  1637. client->GetPlayer()->SetSubTitle(subtitle.c_str());
  1638. }
  1639. }
  1640. LoadCharacterHistory(id, client->GetPlayer());
  1641. LoadCharacterLUAHistory(id, client->GetPlayer());
  1642. LoadPlayerStatistics(client->GetPlayer(), id);
  1643. LoadPlayerCollections(client->GetPlayer());
  1644. LoadPlayerRecipes(client->GetPlayer());
  1645. //LoadPlayerAchievements(client->GetPlayer());
  1646. LoadPlayerAchievementsUpdates(client->GetPlayer());
  1647. LoadAppearances(client->GetCurrentZone(), client);
  1648. return LoadCharacterStats(id, account_id, client);
  1649. }
  1650. // should not be here...
  1651. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character for '%s'", ch_name);
  1652. return false;
  1653. }
  1654. void WorldDatabase::LoadCharacterQuestRewards(Client* client) {
  1655. Query query;
  1656. MYSQL_ROW row;
  1657. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT indexed, quest_id, is_temporary, is_collection, has_displayed, tmp_coin, tmp_status, description FROM character_quest_rewards where char_id = %u ORDER BY indexed asc", client->GetCharacterID());
  1658. int8 count = 0;
  1659. if(result)
  1660. {
  1661. while(result && (row = mysql_fetch_row(result)))
  1662. {
  1663. int32 index = atoul(row[0]);
  1664. int32 quest_id = atoul(row[1]);
  1665. bool is_temporary = atoul(row[2]);
  1666. bool is_collection = atoul(row[3]);
  1667. bool has_displayed = atoul(row[4]);
  1668. int64 tmp_coin = 0;
  1669. #ifdef WIN32
  1670. tmp_coin = _strtoui64(row[5], NULL, 10);
  1671. #else
  1672. tmp_coin = strtoull(row[5], 0, 10);
  1673. #endif
  1674. int32 tmp_status = atoul(row[6]);
  1675. std::string description = std::string("");
  1676. if(row[7]) {
  1677. std::string description = std::string(row[7]);
  1678. }
  1679. if(is_collection) {
  1680. map<int32, Collection*>* collections = client->GetPlayer()->GetCollectionList()->GetCollections();
  1681. map<int32, Collection*>::iterator itr;
  1682. Collection* collection = 0;
  1683. for (itr = collections->begin(); itr != collections->end(); itr++) {
  1684. collection = itr->second;
  1685. if (collection->GetIsReadyToTurnIn()) {
  1686. client->GetPlayer()->SetPendingCollectionReward(collection);
  1687. break;
  1688. }
  1689. }
  1690. }
  1691. if(is_temporary) {
  1692. LoadCharacterQuestTemporaryRewards(client, quest_id);
  1693. }
  1694. client->QueueQuestReward(quest_id, is_temporary, is_collection, has_displayed, tmp_coin, tmp_status, description, true, index);
  1695. count++;
  1696. }
  1697. }
  1698. if(count) {
  1699. client->SetQuestUpdateState(true);
  1700. }
  1701. }
  1702. void WorldDatabase::LoadCharacterQuestTemporaryRewards(Client* client, int32 quest_id) {
  1703. Query query;
  1704. MYSQL_ROW row;
  1705. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT item_id, count FROM character_quest_temporary_rewards where char_id = %u and quest_id = %u", client->GetCharacterID(), quest_id);
  1706. int8 count = 0;
  1707. if(result)
  1708. {
  1709. while(result && (row = mysql_fetch_row(result)))
  1710. {
  1711. int32 item_id = atoul(row[0]);
  1712. int16 item_count = atoul(row[1]);
  1713. client->GetPlayer()->AddQuestTemporaryReward(quest_id, item_id, item_count);
  1714. }
  1715. }
  1716. }
  1717. bool WorldDatabase::InsertCharacterStats(int32 character_id, int8 class_id, int8 race_id){
  1718. Query query1;
  1719. Query query2;
  1720. Query query3;
  1721. Query query4;
  1722. Query query5;
  1723. /* Blank record */
  1724. query1.RunQuery2(Q_INSERT, "INSERT INTO `character_details` (`char_id`) VALUES (%u)", character_id);
  1725. /* Using the class id and race id */
  1726. query2.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = %d AND class_id = %d AND char_id = %u", race_id, class_id, character_id);
  1727. if (query2.GetAffectedRows() > 0)
  1728. return true;
  1729. /* Using the class id and race id = 255 */
  1730. query3.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = 255 AND class_id = %d AND char_id = %u", class_id, character_id);
  1731. if (query3.GetAffectedRows() > 0)
  1732. return true;
  1733. /* Using class id = 255 and the race id */
  1734. query4.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = %d AND class_id = 255 AND char_id = %u", race_id, character_id);
  1735. if (query4.GetAffectedRows() > 0)
  1736. return true;
  1737. /* Using class id = 255 and race id = 255 */
  1738. query5.RunQuery2(Q_UPDATE, "UPDATE character_details c, starting_details s SET c.max_hp = s.max_hp, c.hp = s.max_hp, c.max_power = s.max_power, c.power = s.max_power, c.str = s.str, c.sta = s.sta, c.agi = s.agi, c.wis = s.wis, c.intel = s.intel,c.heat = s.heat, c.cold = s.cold, c.magic = s.magic, c.mental = s.mental, c.divine = s.divine, c.disease = s.disease, c.poison = s.poison, c.coin_copper = s.coin_copper, c.coin_silver = s.coin_silver, c.coin_gold = s.coin_gold, c.coin_plat = s.coin_plat, c.status_points = s.status_points WHERE s.race_id = 255 AND class_id = 255 AND char_id = %u", character_id);
  1739. if (query5.GetAffectedRows() > 0)
  1740. return true;
  1741. return false;
  1742. }
  1743. int32 WorldDatabase::GetCharacterTimeStamp(int32 character_id, int32 account_id,bool* char_exists){
  1744. Query query;
  1745. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT unix_timestamp FROM characters where id=%i and account_id=%i",character_id,account_id);
  1746. if(result && mysql_num_rows(result) > 0) {
  1747. MYSQL_ROW row;
  1748. row = mysql_fetch_row(result);
  1749. *char_exists = true;
  1750. return atoi(row[0]); // Return timestamp
  1751. }
  1752. else
  1753. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterTimeStamp query '%s': %s", query.GetQuery(), query.GetError());
  1754. *char_exists = false;
  1755. return 0;
  1756. }
  1757. int32 WorldDatabase::GetCharacterTimeStamp(int32 character_id) {
  1758. Query query;
  1759. MYSQL_ROW row;
  1760. MYSQL_RES *result = query.RunQuery2(Q_SELECT, "SELECT unix_timestamp FROM characters WHERE id=%u", character_id);
  1761. int32 ret = 0;
  1762. if (result && (row = mysql_fetch_row(result)))
  1763. ret = atoul(row[0]);
  1764. return ret;
  1765. }
  1766. bool WorldDatabase::UpdateCharacterTimeStamp(int32 account_id, int32 character_id, int32 timestamp_update){
  1767. Query query;
  1768. string update_charts = string("update characters set unix_timestamp=%i where id=%i and account_id=%i");
  1769. query.RunQuery2(Q_UPDATE, update_charts.c_str(),timestamp_update,character_id,account_id);
  1770. if(!query.GetAffectedRows())
  1771. {
  1772. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateCharacterTimeStamp query '%s': %s", query.GetQuery(), query.GetError());
  1773. return false;
  1774. }
  1775. return true;
  1776. }
  1777. bool WorldDatabase::insertCharacterProperty(Client* client, char* propName, char* propValue) {
  1778. Query query, query2;
  1779. string update_status = string("update character_properties set propvalue='%s' where charid=%i and propname='%s'");
  1780. query.RunQuery2(Q_UPDATE, update_status.c_str(), propValue, client->GetCharacterID(), propName);
  1781. if (!query.GetAffectedRows())
  1782. {
  1783. query2.RunQuery2(Q_UPDATE, "insert into character_properties (charid, propname, propvalue) values(%i, '%s', '%s')", client->GetCharacterID(), propName, propValue);
  1784. if (query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF) {
  1785. LogWrite(WORLD__ERROR, 0, "World", "Error in insertCharacterProperty query '%s': %s", query.GetQuery(), query.GetError());
  1786. return false;
  1787. }
  1788. }
  1789. return true;
  1790. }
  1791. bool WorldDatabase::loadCharacterProperties(Client* client) {
  1792. Query query;
  1793. MYSQL_ROW row;
  1794. int32 id = 0;
  1795. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT propname, propvalue FROM character_properties where charid = %i", client->GetCharacterID());
  1796. // no character found
  1797. if (result == NULL) {
  1798. LogWrite(PLAYER__ERROR, 0, "Player", "Error loading character properties for '%s'", client->GetPlayer()->GetName());
  1799. return false;
  1800. }
  1801. while (result && (row = mysql_fetch_row(result))) {
  1802. char* prop_name = row[0];
  1803. char* prop_value = row[1];
  1804. if (!prop_name || !prop_value)
  1805. continue;
  1806. if (!stricmp(prop_name, CHAR_PROPERTY_SPEED))
  1807. {
  1808. float new_speed = atof(prop_value);
  1809. client->GetPlayer()->SetSpeed(new_speed, true);
  1810. client->GetPlayer()->SetCharSheetChanged(true);
  1811. }
  1812. else if (!stricmp(prop_name, CHAR_PROPERTY_FLYMODE))
  1813. {
  1814. int8 flymode = atoul(prop_value);
  1815. if (flymode) // avoid fly mode notification unless enabled
  1816. ClientPacketFunctions::SendFlyMode(client, flymode, false);
  1817. }
  1818. else if (!stricmp(prop_name, CHAR_PROPERTY_INVUL))
  1819. {
  1820. int8 invul = atoul(prop_value);
  1821. client->GetPlayer()->SetInvulnerable(invul == 1);
  1822. if (client->GetPlayer()->GetInvulnerable())
  1823. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You are now invulnerable!");
  1824. }
  1825. else if (!stricmp(prop_name, CHAR_PROPERTY_GMVISION))
  1826. {
  1827. int8 val = atoul(prop_value);
  1828. client->GetPlayer()->SetGMVision(val == 1);
  1829. client->GetCurrentZone()->SendAllSpawnsForVisChange(client, false);
  1830. if (val)
  1831. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "GM Vision Enabled!");
  1832. }
  1833. else if (!stricmp(prop_name, CHAR_PROPERTY_REGIONDEBUG))
  1834. {
  1835. int8 val = atoul(prop_value);
  1836. client->SetRegionDebug(val == 1);
  1837. if (val)
  1838. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Region Debug Enabled!");
  1839. }
  1840. else if (!stricmp(prop_name, CHAR_PROPERTY_LUADEBUG))
  1841. {
  1842. int8 val = atoul(prop_value);
  1843. if (val)
  1844. {
  1845. client->SetLuaDebugClient(true);
  1846. if (lua_interface)
  1847. lua_interface->UpdateDebugClients(client);
  1848. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "You will now receive LUA error messages.");
  1849. }
  1850. }
  1851. else if (!stricmp(prop_name, CHAR_PROPERTY_GROUPLOOTMETHOD))
  1852. {
  1853. int8 val = atoul(prop_value);
  1854. client->GetPlayer()->GetInfoStruct()->set_group_loot_method(val);
  1855. }
  1856. else if (!stricmp(prop_name, CHAR_PROPERTY_GROUPLOOTITEMRARITY))
  1857. {
  1858. int8 val = atoul(prop_value);
  1859. client->GetPlayer()->GetInfoStruct()->set_group_loot_items_rarity(val);
  1860. }
  1861. else if (!stricmp(prop_name, CHAR_PROPERTY_GROUPAUTOSPLIT))
  1862. {
  1863. int8 val = atoul(prop_value);
  1864. client->GetPlayer()->GetInfoStruct()->set_group_auto_split(val);
  1865. }
  1866. else if (!stricmp(prop_name, CHAR_PROPERTY_GROUPDEFAULTYELL))
  1867. {
  1868. int8 val = atoul(prop_value);
  1869. client->GetPlayer()->GetInfoStruct()->set_group_default_yell(val);
  1870. }
  1871. else if (!stricmp(prop_name, CHAR_PROPERTY_GROUPAUTOLOCK))
  1872. {
  1873. int8 val = atoul(prop_value);
  1874. client->GetPlayer()->GetInfoStruct()->set_group_autolock(val);
  1875. }
  1876. else if (!stricmp(prop_name, CHAR_PROPERTY_GROUPSOLOAUTOLOCK))
  1877. {
  1878. int8 val = atoul(prop_value);
  1879. client->GetPlayer()->GetInfoStruct()->set_group_solo_autolock(val);
  1880. }
  1881. else if (!stricmp(prop_name, CHAR_PROPERTY_AUTOLOOTMETHOD))
  1882. {
  1883. int8 val = atoul(prop_value);
  1884. client->GetPlayer()->GetInfoStruct()->set_group_auto_loot_method(val);
  1885. }
  1886. else if (!stricmp(prop_name, CHAR_PROPERTY_GROUPLOCKMETHOD))
  1887. {
  1888. int8 val = atoul(prop_value);
  1889. client->GetPlayer()->GetInfoStruct()->set_group_lock_method(val);
  1890. }
  1891. else if (!stricmp(prop_name, CHAR_PROPERTY_ASSISTAUTOATTACK))
  1892. {
  1893. int8 val = atoul(prop_value);
  1894. client->GetPlayer()->GetInfoStruct()->set_assist_auto_attack(val);
  1895. }
  1896. else if (!stricmp(prop_name, CHAR_PROPERTY_SETACTIVEFOOD))
  1897. {
  1898. int32 val = atoul(prop_value);
  1899. client->GetPlayer()->SetActiveFoodUniqueID(val, false);
  1900. }
  1901. else if (!stricmp(prop_name, CHAR_PROPERTY_SETACTIVEDRINK))
  1902. {
  1903. int32 val = atoul(prop_value);
  1904. client->GetPlayer()->SetActiveDrinkUniqueID(val, false);
  1905. }
  1906. }
  1907. return true;
  1908. }
  1909. //gets the name FROM the db with the right letters in caps
  1910. string WorldDatabase::GetPlayerName(char* name){
  1911. Query query;
  1912. string ret = "";
  1913. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM characters where name='%s'", getSafeEscapeString(name).c_str());
  1914. if(result && mysql_num_rows(result) > 0) {
  1915. MYSQL_ROW row;
  1916. row = mysql_fetch_row(result);
  1917. if(row[0])
  1918. ret = string(row[0]);
  1919. }
  1920. return ret;
  1921. }
  1922. int32 WorldDatabase::GetCharacterID(const char* name) {
  1923. int32 id = 0;
  1924. Query query;
  1925. if (name) {
  1926. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id` FROM `characters` WHERE `name`='%s'", name);
  1927. if (result && mysql_num_rows(result) > 0) {
  1928. MYSQL_ROW row;
  1929. row = mysql_fetch_row(result);
  1930. id = atoul(row[0]);
  1931. }
  1932. }
  1933. return id;
  1934. }
  1935. int32 WorldDatabase::GetCharacterCurrentZoneID(int32 character_id) {
  1936. int32 id = 0;
  1937. Query query;
  1938. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `current_zone_id` FROM `characters` WHERE `id`=%u", character_id);
  1939. if (result && mysql_num_rows(result) > 0) {
  1940. MYSQL_ROW row;
  1941. row = mysql_fetch_row(result);
  1942. id = atoul(row[0]);
  1943. }
  1944. return id;
  1945. }
  1946. int32 WorldDatabase::GetCharacterAccountID(int32 character_id) {
  1947. int32 id = 0;
  1948. Query query;
  1949. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `account_id` FROM `characters` WHERE `id`=%u", character_id);
  1950. if (result && mysql_num_rows(result) > 0) {
  1951. MYSQL_ROW row;
  1952. row = mysql_fetch_row(result);
  1953. id = atoul(row[0]);
  1954. }
  1955. return id;
  1956. }
  1957. sint16 WorldDatabase::GetHighestCharacterAdminStatus(int32 account_id){
  1958. Query query;
  1959. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(admin_status) FROM characters where account_id=%i ",account_id);
  1960. if(result && mysql_num_rows(result) > 0) {
  1961. MYSQL_ROW row;
  1962. row = mysql_fetch_row(result);
  1963. if ( row[0] != NULL )
  1964. return atoi(row[0]); // Return characters status
  1965. else
  1966. return 0;
  1967. }
  1968. return 0;
  1969. }
  1970. sint16 WorldDatabase::GetLowestCharacterAdminStatus(int32 account_id){
  1971. Query query;
  1972. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT min(admin_status) FROM characters where account_id=%i ",account_id);
  1973. if(result && mysql_num_rows(result) > 0) {
  1974. MYSQL_ROW row;
  1975. row = mysql_fetch_row(result);
  1976. if ( row[0] != NULL )
  1977. return atoi(row[0]); // Return characters status
  1978. else
  1979. return 0;
  1980. }
  1981. return 0;
  1982. }
  1983. sint16 WorldDatabase::GetCharacterAdminStatus(char* character_name){
  1984. Query query;
  1985. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT admin_status FROM characters where name='%s'", getSafeEscapeString(character_name).c_str());
  1986. if(result && mysql_num_rows(result) > 0) {
  1987. MYSQL_ROW row;
  1988. row = mysql_fetch_row(result);
  1989. return atoi(row[0]); // Return characters level
  1990. }
  1991. else
  1992. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterAdminStatus query '%s': %s", query.GetQuery(), query.GetError());
  1993. return -10;
  1994. }
  1995. sint16 WorldDatabase::GetCharacterAdminStatus(int32 account_id , int32 char_id){
  1996. Query query;
  1997. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT admin_status FROM characters where account_id=%i and id=%i",account_id,char_id);
  1998. if(result && mysql_num_rows(result) > 0) {
  1999. MYSQL_ROW row;
  2000. row = mysql_fetch_row(result);
  2001. return atoi(row[0]); // Return characters status
  2002. }
  2003. else{
  2004. Query query2;
  2005. result = query2.RunQuery2(Q_SELECT, "SELECT count(id) FROM characters where account_id=%i and id=%i",account_id,char_id);
  2006. if(result && mysql_num_rows(result) > 0) {
  2007. MYSQL_ROW row;
  2008. row = mysql_fetch_row(result);
  2009. if(atoi(row[0]) == 0) //old character, needs to be deleted FROM login server
  2010. return -10;
  2011. return -8;
  2012. }
  2013. else
  2014. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterAdminStatus query '%s': %s", query.GetQuery(), query.GetError());
  2015. }
  2016. return PLAY_ERROR_PROBLEM;
  2017. }
  2018. bool WorldDatabase::UpdateAdminStatus(char* character_name, sint16 flag){
  2019. Query query;
  2020. string update_status = string("update characters set admin_status=%i where name='%s'");
  2021. query.RunQuery2(Q_UPDATE, update_status.c_str(),flag,character_name);
  2022. if(!query.GetAffectedRows())
  2023. {
  2024. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateAdminStatus query '%s': %s", query.GetQuery(), query.GetError());
  2025. return false;
  2026. }
  2027. return true;
  2028. }
  2029. void WorldDatabase::SaveCharacterFloats(int32 char_id, const char* type, float float1, float float2, float float3, float multiplier){
  2030. Query query;
  2031. string create_char = string("insert into char_colors (char_id, type, red, green, blue, signed_value) values(%i,'%s',%i,%i,%i, 1)");
  2032. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, (sint8)(float1*multiplier), (sint8)(float2*multiplier), (sint8)(float3*multiplier));
  2033. if(query.GetError() && strlen(query.GetError()) > 0){
  2034. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterFloats query '%s': %s", query.GetQuery(), query.GetError());
  2035. }
  2036. }
  2037. void WorldDatabase::SaveCharacterColors(int32 char_id, const char* type, EQ2_Color color){
  2038. Query query;
  2039. string create_char = string("insert into char_colors (char_id, type, red, green, blue) values(%i,'%s',%i,%i,%i)");
  2040. query.RunQuery2(Q_INSERT, create_char.c_str(), char_id, type, color.red, color.green, color.blue);
  2041. if(query.GetError() && strlen(query.GetError()) > 0){
  2042. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterColors query '%s': %s", query.GetQuery(), query.GetError());
  2043. }
  2044. }
  2045. void WorldDatabase::SaveNPCAppearanceEquipment(int32 spawn_id, int8 slot_id, int16 type, int8 red, int8 green, int8 blue, int8 hred, int8 hgreen, int8 hblue){
  2046. Query query;
  2047. string appearance = string("INSERT INTO npc_appearance_equip (spawn_id, slot_id, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue) values (%i, %i, %i, %i, %i, %i, %i, %i, %i) ON DUPLICATE KEY UPDATE equip_type=%i, red=%i, green=%i, blue=%i, highlight_red=%i, highlight_green=%i, highlight_blue=%i");
  2048. query.RunQuery2(Q_INSERT, appearance.c_str(), spawn_id, slot_id, type, red, green, blue, hred, hgreen, hblue, type, red, green, blue, hred, hgreen, hblue);
  2049. if(query.GetError() && strlen(query.GetError()) > 0){
  2050. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveNPCAppearanceEquipment query '%s': %s", query.GetQuery(), query.GetError());
  2051. }
  2052. }
  2053. int32 WorldDatabase::LoadNPCAppearanceEquipmentData(ZoneServer* zone){
  2054. Query query;
  2055. MYSQL_ROW row;
  2056. int32 spawn_id = 0, new_spawn_id = 0, count = 0;
  2057. NPC* npc = 0;
  2058. int8 slot = 0;
  2059. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spawn_id, slot_id, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue FROM npc_appearance_equip ORDER BY spawn_id");
  2060. while(result && (row = mysql_fetch_row(result))){
  2061. new_spawn_id = atoul(row[0]);
  2062. if(new_spawn_id != spawn_id){
  2063. npc = zone->GetNPC(new_spawn_id, true);
  2064. if(!npc)
  2065. continue;
  2066. if(spawn_id > 0)
  2067. count++;
  2068. spawn_id = new_spawn_id;
  2069. }
  2070. slot = atoul(row[1]);
  2071. if(slot < NUM_SLOTS){
  2072. npc->SetEquipment(slot, atoul(row[2]), atoul(row[3]), atoul(row[4]), atoul(row[5]), atoul(row[6]), atoul(row[7]), atoul(row[8]));
  2073. }
  2074. }
  2075. if(query.GetError() && strlen(query.GetError()) > 0)
  2076. LogWrite(WORLD__ERROR, 0, "World", "Error in LoadNPCAppearanceEquipmentData query '%s': %s", query.GetQuery(), query.GetError());
  2077. return count;
  2078. }
  2079. int16 WorldDatabase::GetAppearanceID(string name){
  2080. int32 id = 0;
  2081. Query query;
  2082. MYSQL_ROW row;
  2083. query.escaped_name = getEscapeString(name.c_str());
  2084. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT appearance_id FROM appearances where name='%s'", query.escaped_name);
  2085. if(result && mysql_num_rows(result) == 1){
  2086. row = mysql_fetch_row(result);
  2087. id = atoi(row[0]);
  2088. }
  2089. return id;
  2090. }
  2091. vector<int16>* WorldDatabase::GetAppearanceIDsLikeName(string name, bool filtered) {
  2092. vector<int16>* ids = 0;
  2093. Query query;
  2094. MYSQL_ROW row;
  2095. query.escaped_name = getEscapeString(name.c_str());
  2096. MYSQL_RES* result;
  2097. if (filtered)
  2098. result = query.RunQuery2(Q_SELECT, "SELECT `appearance_id` FROM `appearances` WHERE `name` RLIKE '%s' AND `name` NOT RLIKE 'ghost' AND `name` NOT RLIKE 'headless' AND `name` NOT RLIKE 'elemental' AND `name` NOT RLIKE 'test' AND `name` NOT RLIKE 'zombie' AND `name` NOT RLIKE 'vampire'", query.escaped_name);
  2099. else
  2100. result = query.RunQuery2(Q_SELECT, "SELECT `appearance_id` FROM `appearances` WHERE `name` RLIKE '%s' AND `name` NOT RLIKE 'ghost' AND `name`", query.escaped_name);
  2101. while (result && (row = mysql_fetch_row(result))) {
  2102. if (!ids)
  2103. ids = new vector<int16>;
  2104. ids->push_back(atoi(row[0]));
  2105. }
  2106. return ids;
  2107. }
  2108. string WorldDatabase::GetAppearanceName(int16 appearance_id) {
  2109. Query query;
  2110. MYSQL_ROW row;
  2111. string name;
  2112. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `name` FROM `appearances` WHERE `appearance_id`=%u", appearance_id);
  2113. if (result && (row = mysql_fetch_row(result)))
  2114. name = string(row[0]);
  2115. return name;
  2116. }
  2117. void WorldDatabase::UpdateRandomize(int32 spawn_id, sint32 value) {
  2118. Query query;
  2119. query.RunQuery2(Q_UPDATE, "UPDATE `spawn_npcs` SET `randomize`=`randomize` + %i WHERE `spawn_id`=%u", value, spawn_id);
  2120. }
  2121. int32 WorldDatabase::SaveCharacter(PacketStruct* create, int32 loginID){
  2122. Query query;
  2123. int8 race_id = create->getType_int8_ByName("race");
  2124. int8 orig_class_id = create->getType_int8_ByName("class");//Normal server
  2125. int8 class_id = orig_class_id;
  2126. if ( create->GetVersion() <= 546 ) {
  2127. class_id = 0; //Classic Server Only
  2128. }
  2129. create->PrintPacket();
  2130. int8 gender_id = create->getType_int8_ByName("gender");
  2131. sint16 auto_admin_status = 0;
  2132. // fetch rules related to setting auto-admin status for server
  2133. bool auto_admin_players = rule_manager.GetGlobalRule(R_World, AutoAdminPlayers)->GetBool();
  2134. bool auto_admin_gm = rule_manager.GetGlobalRule(R_World, AutoAdminGMs)->GetBool();
  2135. /*
  2136. The way I think this is supposed to work :) is if any of your chars are already GM, and AutoAdminGMs rule is true,
  2137. set the new character's admin_status to your highest status for any character active on your loginID.
  2138. - If status > 0, new character > 0
  2139. - If status = 0, new character = 0
  2140. - If status < 0, new character < 0, too... even if auto_admin_gm is true
  2141. If we're not a GM (status = 0) but AutoAdminPlayers rule is true,
  2142. set the new character's admin_status to the default set in AutoAdminStatusValue rule.
  2143. Else, if both rules are False, set everyone to 0 like normal.
  2144. */
  2145. auto_admin_status = GetHighestCharacterAdminStatus(loginID);
  2146. if( auto_admin_status > 0 && auto_admin_gm ) {
  2147. LogWrite(WORLD__WARNING, 0, "World", "New character '%s' granted GM status (%i) from accountID: %i", create->getType_EQ2_16BitString_ByName("name").data.c_str(), auto_admin_status, loginID);
  2148. }
  2149. else if( auto_admin_players )
  2150. {
  2151. auto_admin_status = rule_manager.GetGlobalRule(R_World, AutoAdminStatusValue)->GetSInt16();
  2152. LogWrite(WORLD__DEBUG, 0, "World", "New character '%s' granted AutoAdminPlayer status: %i", create->getType_EQ2_16BitString_ByName("name").data.c_str(), auto_admin_status);
  2153. }
  2154. else {
  2155. auto_admin_status = 0;
  2156. }
  2157. string create_char = string("Insert into characters (account_id, server_id, name, race, class, gender, deity, body_size, body_age, soga_wing_type, soga_chest_type, soga_legs_type, soga_hair_type, soga_model_type, legs_type, chest_type, wing_type, hair_type, model_type, facial_hair_type, soga_facial_hair_type, created_date, last_saved, admin_status, first_world_login) values(%i, %i, '%s', %i, %i, %i, %i, %f, %f, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, now(), unix_timestamp(), %i, 1)");
  2158. query.RunQuery2(Q_INSERT, create_char.c_str(),
  2159. loginID,
  2160. create->getType_int32_ByName("server_id"),
  2161. create->getType_EQ2_16BitString_ByName("name").data.c_str(),
  2162. race_id,
  2163. class_id,
  2164. gender_id,
  2165. create->getType_int8_ByName("deity"),
  2166. create->getType_float_ByName("body_size"),
  2167. create->getType_float_ByName("body_age"),
  2168. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_wing_file").data),
  2169. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_chest_file").data),
  2170. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_legs_file").data),
  2171. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_hair_file").data),
  2172. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_race_file").data),
  2173. GetAppearanceID(create->getType_EQ2_16BitString_ByName("legs_file").data),
  2174. GetAppearanceID(create->getType_EQ2_16BitString_ByName("chest_file").data),
  2175. GetAppearanceID(create->getType_EQ2_16BitString_ByName("wing_file").data),
  2176. GetAppearanceID(create->getType_EQ2_16BitString_ByName("hair_file").data),
  2177. GetAppearanceID(create->getType_EQ2_16BitString_ByName("race_file").data),
  2178. GetAppearanceID(create->getType_EQ2_16BitString_ByName("face_file").data),
  2179. GetAppearanceID(create->getType_EQ2_16BitString_ByName("soga_face_file").data),
  2180. auto_admin_status);
  2181. if(query.GetError() && strlen(query.GetError()) > 0)
  2182. {
  2183. LogWrite(PLAYER__ERROR, 0, "Player", "Error in SaveCharacter query '%s': %s", query.GetQuery(), query.GetError());
  2184. return 0;
  2185. }
  2186. int32 last_insert_id = query.GetLastInsertedID();
  2187. int32 char_id = last_insert_id;
  2188. UpdateStartingFactions(char_id, create->getType_int8_ByName("starting_zone"));
  2189. UpdateStartingZone(char_id, class_id, race_id, create);
  2190. UpdateStartingItems(char_id, class_id, race_id);
  2191. UpdateStartingSkills(char_id, class_id, race_id);
  2192. UpdateStartingSpells(char_id, class_id, race_id);
  2193. UpdateStartingSkillbar(char_id, class_id, race_id);
  2194. UpdateStartingTitles(char_id, class_id, race_id, gender_id);
  2195. InsertCharacterStats(char_id, class_id, race_id);
  2196. UpdateStartingLanguage(char_id, race_id, create->getType_int8_ByName("starting_zone"));
  2197. LoadClaimItems(char_id); //insert claim items, from claim_items to the character_ version.
  2198. AddNewPlayerToServerGuild(loginID, char_id);
  2199. if (create->GetVersion() <= 561) {
  2200. float classic_multiplier = 250.0f;
  2201. SaveCharacterFloats(char_id, "skin_color", create->getType_float_ByName("skin_color", 0), create->getType_float_ByName("skin_color", 1), create->getType_float_ByName("skin_color", 2), classic_multiplier);
  2202. SaveCharacterFloats(char_id, "eye_color", create->getType_float_ByName("eye_color", 0), create->getType_float_ByName("eye_color", 1), create->getType_float_ByName("eye_color", 2), classic_multiplier);
  2203. SaveCharacterFloats(char_id, "hair_color1", create->getType_float_ByName("hair_color1", 0), create->getType_float_ByName("hair_color1", 1), create->getType_float_ByName("hair_color1", 2), classic_multiplier);
  2204. SaveCharacterFloats(char_id, "hair_color2", create->getType_float_ByName("hair_color2", 0), create->getType_float_ByName("hair_color2", 1), create->getType_float_ByName("hair_color2", 2), classic_multiplier);
  2205. SaveCharacterFloats(char_id, "hair_highlight", create->getType_float_ByName("hair_highlight", 0), create->getType_float_ByName("hair_highlight", 1), create->getType_float_ByName("hair_highlight", 2), classic_multiplier);
  2206. SaveCharacterFloats(char_id, "hair_type_color", create->getType_float_ByName("hair_type_color", 0), create->getType_float_ByName("hair_type_color", 1), create->getType_float_ByName("hair_type_color", 2), classic_multiplier);
  2207. SaveCharacterFloats(char_id, "hair_type_highlight_color", create->getType_float_ByName("hair_type_highlight_color", 0), create->getType_float_ByName("hair_type_highlight_color", 1), create->getType_float_ByName("hair_type_highlight_color", 2), classic_multiplier);
  2208. SaveCharacterFloats(char_id, "hair_type_color", create->getType_float_ByName("hair_type_color", 0), create->getType_float_ByName("hair_type_color", 1), create->getType_float_ByName("hair_type_color", 2), classic_multiplier);
  2209. SaveCharacterFloats(char_id, "hair_type_highlight_color", create->getType_float_ByName("hair_type_highlight_color", 0), create->getType_float_ByName("hair_type_highlight_color", 1), create->getType_float_ByName("hair_type_highlight_color", 2), classic_multiplier);
  2210. SaveCharacterFloats(char_id, "hair_face_color", create->getType_float_ByName("hair_face_color", 0), create->getType_float_ByName("hair_face_color", 1), create->getType_float_ByName("hair_face_color", 2), classic_multiplier);
  2211. SaveCharacterFloats(char_id, "hair_face_highlight_color", create->getType_float_ByName("hair_face_highlight_color", 0), create->getType_float_ByName("hair_face_highlight_color", 1), create->getType_float_ByName("hair_face_highlight_color", 2), classic_multiplier);
  2212. SaveCharacterFloats(char_id, "shirt_color", create->getType_float_ByName("shirt_color", 0), create->getType_float_ByName("shirt_color", 1), create->getType_float_ByName("shirt_color", 2), classic_multiplier);
  2213. SaveCharacterFloats(char_id, "unknown_chest_color", create->getType_float_ByName("unknown_chest_color", 0), create->getType_float_ByName("unknown_chest_color", 1), create->getType_float_ByName("unknown_chest_color", 2), classic_multiplier);
  2214. SaveCharacterFloats(char_id, "pants_color", create->getType_float_ByName("pants_color", 0), create->getType_float_ByName("pants_color", 1), create->getType_float_ByName("pants_color", 2), classic_multiplier);
  2215. SaveCharacterFloats(char_id, "unknown_legs_color", create->getType_float_ByName("unknown_legs_color", 0), create->getType_float_ByName("unknown_legs_color", 1), create->getType_float_ByName("unknown_legs_color", 2), classic_multiplier);
  2216. SaveCharacterFloats(char_id, "unknown9", create->getType_float_ByName("unknown9", 0), create->getType_float_ByName("unknown9", 1), create->getType_float_ByName("unknown9", 2), classic_multiplier);
  2217. }
  2218. else {
  2219. SaveCharacterColors(char_id, "skin_color", create->getType_EQ2_Color_ByName("skin_color"));
  2220. SaveCharacterColors(char_id, "model_color", create->getType_EQ2_Color_ByName("model_color"));
  2221. SaveCharacterColors(char_id, "eye_color", create->getType_EQ2_Color_ByName("eye_color"));
  2222. SaveCharacterColors(char_id, "hair_color1", create->getType_EQ2_Color_ByName("hair_color1"));
  2223. SaveCharacterColors(char_id, "hair_color2", create->getType_EQ2_Color_ByName("hair_color2"));
  2224. SaveCharacterColors(char_id, "hair_highlight", create->getType_EQ2_Color_ByName("hair_highlight"));
  2225. SaveCharacterColors(char_id, "hair_type_color", create->getType_EQ2_Color_ByName("hair_type_color"));
  2226. SaveCharacterColors(char_id, "hair_type_highlight_color", create->getType_EQ2_Color_ByName("hair_type_highlight_color"));
  2227. SaveCharacterColors(char_id, "hair_face_color", create->getType_EQ2_Color_ByName("hair_face_color"));
  2228. SaveCharacterColors(char_id, "hair_face_highlight_color", create->getType_EQ2_Color_ByName("hair_face_highlight_color"));
  2229. SaveCharacterColors(char_id, "wing_color1", create->getType_EQ2_Color_ByName("wing_color1"));
  2230. SaveCharacterColors(char_id, "wing_color2", create->getType_EQ2_Color_ByName("wing_color2"));
  2231. SaveCharacterColors(char_id, "shirt_color", create->getType_EQ2_Color_ByName("shirt_color"));
  2232. SaveCharacterColors(char_id, "unknown_chest_color", create->getType_EQ2_Color_ByName("unknown_chest_color"));
  2233. SaveCharacterColors(char_id, "pants_color", create->getType_EQ2_Color_ByName("pants_color"));
  2234. SaveCharacterColors(char_id, "unknown_legs_color", create->getType_EQ2_Color_ByName("unknown_legs_color"));
  2235. SaveCharacterColors(char_id, "unknown9", create->getType_EQ2_Color_ByName("unknown9"));
  2236. SaveCharacterColors(char_id, "soga_skin_color", create->getType_EQ2_Color_ByName("soga_skin_color"));
  2237. SaveCharacterColors(char_id, "soga_model_color", create->getType_EQ2_Color_ByName("soga_model_color"));
  2238. SaveCharacterColors(char_id, "soga_eye_color", create->getType_EQ2_Color_ByName("soga_eye_color"));
  2239. SaveCharacterColors(char_id, "soga_hair_color1", create->getType_EQ2_Color_ByName("soga_hair_color1"));
  2240. SaveCharacterColors(char_id, "soga_hair_color2", create->getType_EQ2_Color_ByName("soga_hair_color2"));
  2241. SaveCharacterColors(char_id, "soga_hair_highlight", create->getType_EQ2_Color_ByName("soga_hair_highlight"));
  2242. SaveCharacterColors(char_id, "soga_hair_type_color", create->getType_EQ2_Color_ByName("soga_hair_type_color"));
  2243. SaveCharacterColors(char_id, "soga_hair_type_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_type_highlight_color"));
  2244. SaveCharacterColors(char_id, "soga_hair_face_color", create->getType_EQ2_Color_ByName("soga_hair_face_color"));
  2245. SaveCharacterColors(char_id, "soga_hair_face_highlight_color", create->getType_EQ2_Color_ByName("soga_hair_face_highlight_color"));
  2246. SaveCharacterColors(char_id, "soga_wing_color1", create->getType_EQ2_Color_ByName("soga_wing_color1"));
  2247. SaveCharacterColors(char_id, "soga_wing_color2", create->getType_EQ2_Color_ByName("soga_wing_color2"));
  2248. SaveCharacterColors(char_id, "soga_shirt_color", create->getType_EQ2_Color_ByName("soga_shirt_color"));
  2249. SaveCharacterColors(char_id, "soga_unknown_chest_color", create->getType_EQ2_Color_ByName("soga_unknown_chest_color"));
  2250. SaveCharacterColors(char_id, "soga_pants_color", create->getType_EQ2_Color_ByName("soga_pants_color"));
  2251. SaveCharacterColors(char_id, "soga_unknown_legs_color", create->getType_EQ2_Color_ByName("soga_unknown_legs_color"));
  2252. SaveCharacterColors(char_id, "soga_unknown13", create->getType_EQ2_Color_ByName("soga_unknown13"));
  2253. SaveCharacterFloats(char_id, "soga_eye_type", create->getType_float_ByName("soga_eyes2", 0), create->getType_float_ByName("soga_eyes2", 1), create->getType_float_ByName("soga_eyes2", 2));
  2254. SaveCharacterFloats(char_id, "soga_ear_type", create->getType_float_ByName("soga_ears", 0), create->getType_float_ByName("soga_ears", 1), create->getType_float_ByName("soga_ears", 2));
  2255. SaveCharacterFloats(char_id, "soga_eye_brow_type", create->getType_float_ByName("soga_eye_brows", 0), create->getType_float_ByName("soga_eye_brows", 1), create->getType_float_ByName("soga_eye_brows", 2));
  2256. SaveCharacterFloats(char_id, "soga_cheek_type", create->getType_float_ByName("soga_cheeks", 0), create->getType_float_ByName("soga_cheeks", 1), create->getType_float_ByName("soga_cheeks", 2));
  2257. SaveCharacterFloats(char_id, "soga_lip_type", create->getType_float_ByName("soga_lips", 0), create->getType_float_ByName("soga_lips", 1), create->getType_float_ByName("soga_lips", 2));
  2258. SaveCharacterFloats(char_id, "soga_chin_type", create->getType_float_ByName("soga_chin", 0), create->getType_float_ByName("soga_chin", 1), create->getType_float_ByName("soga_chin", 2));
  2259. SaveCharacterFloats(char_id, "soga_nose_type", create->getType_float_ByName("soga_nose", 0), create->getType_float_ByName("soga_nose", 1), create->getType_float_ByName("soga_nose", 2));
  2260. }
  2261. SaveCharacterFloats(char_id, "eye_type", create->getType_float_ByName("eyes2", 0), create->getType_float_ByName("eyes2", 1), create->getType_float_ByName("eyes2", 2));
  2262. SaveCharacterFloats(char_id, "ear_type", create->getType_float_ByName("ears", 0), create->getType_float_ByName("ears", 1), create->getType_float_ByName("ears", 2));
  2263. SaveCharacterFloats(char_id, "eye_brow_type", create->getType_float_ByName("eye_brows", 0), create->getType_float_ByName("eye_brows", 1), create->getType_float_ByName("eye_brows", 2));
  2264. SaveCharacterFloats(char_id, "cheek_type", create->getType_float_ByName("cheeks", 0), create->getType_float_ByName("cheeks", 1), create->getType_float_ByName("cheeks", 2));
  2265. SaveCharacterFloats(char_id, "lip_type", create->getType_float_ByName("lips", 0), create->getType_float_ByName("lips", 1), create->getType_float_ByName("lips", 2));
  2266. SaveCharacterFloats(char_id, "chin_type", create->getType_float_ByName("chin", 0), create->getType_float_ByName("chin", 1), create->getType_float_ByName("chin", 2));
  2267. SaveCharacterFloats(char_id, "nose_type", create->getType_float_ByName("nose", 0), create->getType_float_ByName("nose", 1), create->getType_float_ByName("nose", 2));
  2268. SaveCharacterFloats(char_id, "body_size", create->getType_float_ByName("body_size", 0), 0, 0);
  2269. return char_id;
  2270. }
  2271. int8 WorldDatabase::CheckNameFilter(const char* name, int8 min_length, int8 max_length) {
  2272. // the minimum 4 is enforced by the client too
  2273. if(!name || strlen(name) < min_length || strlen(name) > max_length) // Even 20 char length is long...
  2274. return BADNAMELENGTH_REPLY;
  2275. uchar* checkname = (uchar*)name;
  2276. for (int32 i = 0; i < strlen(name); i++)
  2277. {
  2278. if(!alpha_check(checkname[i]))
  2279. return NAMEINVALID_REPLY;
  2280. }
  2281. Query query;
  2282. LogWrite(WORLD__DEBUG, 0, "World", "Name check on: %s", name);
  2283. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT count(*) FROM characters WHERE name='%s'",name);
  2284. if(result && mysql_num_rows(result) > 0) {
  2285. MYSQL_ROW row;
  2286. row = mysql_fetch_row(result);
  2287. if(row[0] != 0 && atoi(row[0]) > 0)
  2288. return NAMETAKEN_REPLY;
  2289. }
  2290. else
  2291. LogWrite(WORLD__ERROR, 0, "World", "Error in CheckNameFilter (name exist check) (Name query '%s': %s", query.GetQuery(), query.GetError());
  2292. Query query3;
  2293. LogWrite(WORLD__DEBUG, 0, "World", "Name check on: %s (Bots table)", name);
  2294. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT count(*) FROM bots WHERE name='%s'", name);
  2295. if (result3 && mysql_num_rows(result3) > 0) {
  2296. MYSQL_ROW row;
  2297. row = mysql_fetch_row(result3);
  2298. if (row[0] != 0 && atoi(row[0]) > 0)
  2299. return NAMETAKEN_REPLY;
  2300. }
  2301. else
  2302. LogWrite(WORLD__ERROR, 0, "World", "Error in CheckNameFilter (name exist check, bot table) (Name query '%s': %s", query3.GetQuery(), query3.GetError());
  2303. Query query2;
  2304. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT count(*) FROM name_filter WHERE '%s' like name",name);
  2305. if(result2 && mysql_num_rows(result2) > 0) {
  2306. MYSQL_ROW row;
  2307. row = mysql_fetch_row(result2);
  2308. if(row[0] != 0 && atoi(row[0]) > 0)
  2309. return NAMEFILTER_REPLY;
  2310. else if(row[0] != 0 && atoi(row[0]) == 0)
  2311. return CREATESUCCESS_REPLY;
  2312. }
  2313. else
  2314. LogWrite(WORLD__ERROR, 0, "World", "Error in CheckNameFilter (name_filter check) query '%s': %s", query.GetQuery(), query.GetError());
  2315. return UNKNOWNERROR_REPLY;
  2316. }
  2317. char* WorldDatabase::GetCharacterName(int32 character_id){
  2318. LogWrite(WORLD__TRACE, 9, "World", "Enter: %s", __FUNCTION__);
  2319. Query query;
  2320. char* name = 0;
  2321. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM characters where id=%u",character_id);
  2322. if(result && mysql_num_rows(result) > 0) {
  2323. MYSQL_ROW row;
  2324. row = mysql_fetch_row(result);
  2325. if(row[0] && strlen(row[0]) > 0)
  2326. {
  2327. name = new char[strlen(row[0])+1];
  2328. memset(name,0, strlen(row[0])+1);
  2329. strcpy(name, row[0]);
  2330. }
  2331. }
  2332. else
  2333. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterName query '%s': %s", query.GetQuery(), query.GetError());
  2334. LogWrite(WORLD__TRACE, 9, "World", "Exit: %s", __FUNCTION__);
  2335. return name;
  2336. }
  2337. int8 WorldDatabase::GetCharacterLevel(int32 character_id){
  2338. Query query;
  2339. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT level FROM characters where id=%u",character_id);
  2340. if(result && mysql_num_rows(result) > 0) {
  2341. MYSQL_ROW row;
  2342. row = mysql_fetch_row(result);
  2343. return atoi(row[0]); // Return characters level
  2344. }
  2345. else
  2346. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterLevel query '%s': %s", query.GetQuery(), query.GetError());
  2347. return 0;
  2348. }
  2349. int16 WorldDatabase::GetCharacterModelType(int32 character_id){
  2350. Query query;
  2351. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT model_type FROM characters where id=%u",character_id);
  2352. if(result && mysql_num_rows(result) > 0) {
  2353. MYSQL_ROW row;
  2354. row = mysql_fetch_row(result);
  2355. return atoi(row[0]); // Return characters race
  2356. }
  2357. else
  2358. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterModelType query '%s': %s", query.GetQuery(), query.GetError());
  2359. return 0;
  2360. }
  2361. int8 WorldDatabase::GetCharacterClass(int32 character_id){
  2362. Query query;
  2363. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT class FROM characters where id=%u",character_id);
  2364. if(result && mysql_num_rows(result) > 0) {
  2365. MYSQL_ROW row;
  2366. row = mysql_fetch_row(result);
  2367. return atoi(row[0]); // Return characters class
  2368. }
  2369. else
  2370. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterClass query '%s': %s", query.GetQuery(), query.GetError());
  2371. return 0;
  2372. }
  2373. int8 WorldDatabase::GetCharacterGender(int32 character_id){
  2374. Query query;
  2375. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT gender FROM characters where id=%u",character_id);
  2376. if(result && mysql_num_rows(result) > 0) {
  2377. MYSQL_ROW row;
  2378. row = mysql_fetch_row(result);
  2379. return atoi(row[0]); // Return characters gender
  2380. }
  2381. else
  2382. LogWrite(WORLD__ERROR, 0, "World", "Error in GetCharacterGender query '%s': %s", query.GetQuery(), query.GetError());
  2383. return 0;
  2384. }
  2385. void WorldDatabase::DeleteCharacterQuest(int32 quest_id, int32 char_id, bool repeated_quest) {
  2386. if (repeated_quest) {
  2387. if (!database_new.Query("UPDATE `character_quests` SET `given_date` = `completed_date` WHERE `char_id` = %u AND `quest_id` = %u", char_id, quest_id))
  2388. LogWrite(DATABASE__ERROR, 0, "DBNew", "Error (%u) in DeleteCharacterQuest query:\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2389. }
  2390. else {
  2391. if (!database_new.Query("DELETE FROM `character_quests` WHERE `char_id` = %u AND `quest_id` = %u", char_id, quest_id))
  2392. LogWrite(DATABASE__ERROR, 0, "DBNew", "Error (%u) in DeleteCharacterQuest query:\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2393. }
  2394. if (!database_new.Query("DELETE FROM `character_quest_progress` WHERE `char_id` = %u AND `quest_id` = %u", char_id, quest_id))
  2395. LogWrite(DATABASE__ERROR, 0, "DBNew", "Error (%u) in DeleteCharacterQuest query:\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2396. }
  2397. void WorldDatabase::SaveCharacterSkills(Client* client){
  2398. vector<Skill*>* skills = client->GetPlayer()->GetSkills()->GetSaveNeededSkills();
  2399. if(skills){
  2400. Query query;
  2401. if(skills->size() > 0){
  2402. Skill* skill = 0;
  2403. for(int32 i=0;i<skills->size();i++){
  2404. skill = skills->at(i);
  2405. query.AddQueryAsync(client->GetCharacterID(),this,Q_REPLACE, "replace into character_skills (char_id, skill_id, current_val, max_val) values(%u, %u, %i, %i)", client->GetCharacterID(), skill->skill_id, skill->current_val, skill->max_val);
  2406. }
  2407. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2408. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterSkills query '%s': %s", query.GetQuery(), query.GetError());
  2409. }
  2410. safe_delete(skills);
  2411. }
  2412. }
  2413. void WorldDatabase::SaveCharacterQuests(Client* client){
  2414. Query query;
  2415. map<int32, Quest*>::iterator itr;
  2416. master_quest_list.LockQuests(); //prevent reloading until we are done
  2417. client->GetPlayer()->MPlayerQuests.writelock(__FUNCTION__, __LINE__); //prevent all quest modifications until we are done
  2418. map<int32, Quest*>* quests = client->GetPlayer()->GetPlayerQuests();
  2419. for(itr = quests->begin(); itr != quests->end(); itr++){
  2420. if(client->GetCurrentQuestID() == itr->first){
  2421. query.AddQueryAsync(client->GetCharacterID(),this,Q_UPDATE, "update character_quests set current_quest = 0 where char_id = %u", client->GetCharacterID());
  2422. query.AddQueryAsync(client->GetCharacterID(), this,Q_UPDATE, "update character_quests set current_quest = 1 where char_id = %u and quest_id = %u", client->GetCharacterID(), itr->first);
  2423. }
  2424. if(itr->second && itr->second->GetSaveNeeded()){
  2425. query.AddQueryAsync(client->GetCharacterID(), this,Q_INSERT, "insert ignore into character_quests (char_id, quest_id, given_date, quest_giver) values(%u, %u, now(), %u)", client->GetCharacterID(), itr->first, itr->second->GetQuestGiver());
  2426. query.AddQueryAsync(client->GetCharacterID(), this,Q_UPDATE, "update character_quests set tracked = %i, quest_flags = %u, hidden = %i, complete_count = %u where char_id = %u and quest_id = %u", itr->second->IsTracked() ? 1 : 0, itr->second->GetQuestFlags(), itr->second->IsHidden() ? 1 : 0, itr->second->GetCompleteCount(), client->GetCharacterID(), itr->first);
  2427. SaveCharacterQuestProgress(client, itr->second);
  2428. itr->second->SetSaveNeeded(false);
  2429. }
  2430. }
  2431. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2432. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterQuests query '%s': %s", query.GetQuery(), query.GetError());
  2433. quests = client->GetPlayer()->GetCompletedPlayerQuests();
  2434. for(itr = quests->begin(); itr != quests->end(); itr++){
  2435. if(itr->second && itr->second->GetSaveNeeded()){
  2436. query.AddQueryAsync(client->GetCharacterID(), this,Q_DELETE, "delete FROM character_quest_progress where char_id = %u and quest_id = %u", client->GetCharacterID(), itr->first);
  2437. /* incase the quest is completed before the quest could be inserted in the PlayerQuests loop, we first try to insert it. If it already exists then we can just update
  2438. * the completed_date */
  2439. query.AddQueryAsync(client->GetCharacterID(), this,Q_INSERT, "INSERT INTO character_quests (char_id, quest_id, quest_giver, current_quest, given_date, completed_date, complete_count) values (%u,%u,%u,0, now(),now(), %u) ON DUPLICATE KEY UPDATE completed_date = now(), complete_count = %u, current_quest = 0", client->GetCharacterID(), itr->first, itr->second->GetQuestGiver(), itr->second->GetCompleteCount(), itr->second->GetCompleteCount());
  2440. itr->second->SetSaveNeeded(false);
  2441. }
  2442. }
  2443. client->GetPlayer()->MPlayerQuests.releasewritelock(__FUNCTION__, __LINE__);
  2444. master_quest_list.UnlockQuests();
  2445. }
  2446. void WorldDatabase::SaveCharRepeatableQuest(Client* client, int32 quest_id, int16 quest_complete_count) {
  2447. if (!database_new.Query("UPDATE `character_quests` SET `given_date` = now(), complete_count = %u WHERE `char_id` = %u AND `quest_id` = %u", quest_complete_count, client->GetCharacterID(), quest_id))
  2448. LogWrite(DATABASE__ERROR, 0, "DBNew", "DB Error %u\n%s", database_new.GetError(), database_new.GetErrorMsg());
  2449. }
  2450. void WorldDatabase::SaveCharacterQuestProgress(Client* client, Quest* quest){
  2451. vector<QuestStep*>* steps = quest->GetQuestSteps();
  2452. vector<QuestStep*>::iterator itr;
  2453. QuestStep* step = 0;
  2454. quest->MQuestSteps.readlock(__FUNCTION__, __LINE__);
  2455. if(steps){
  2456. for(itr = steps->begin(); itr != steps->end(); itr++){
  2457. step = *itr;
  2458. if(step && step->GetQuestCurrentQuantity() > 0) {
  2459. Query query;
  2460. query.RunQuery2(Q_REPLACE, "replace into character_quest_progress (char_id, quest_id, step_id, progress) values(%u, %u, %u, %i)", client->GetCharacterID(), quest->GetQuestID(), step->GetStepID(), step->GetQuestCurrentQuantity());
  2461. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2462. LogWrite(WORLD__ERROR, 0, "World", "Error in SaveCharacterQuestProgress query '%s': %s", query.GetQuery(), query.GetError());
  2463. }
  2464. }
  2465. }
  2466. quest->MQuestSteps.releasereadlock(__FUNCTION__, __LINE__);
  2467. }
  2468. void WorldDatabase::LoadCharacterQuestProgress(Client* client){
  2469. Query query;
  2470. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT character_quest_progress.quest_id, step_id, progress FROM character_quest_progress, character_quests where character_quest_progress.char_id=%u and character_quest_progress.quest_id = character_quests.quest_id and character_quest_progress.char_id = character_quests.char_id ORDER BY character_quest_progress.quest_id",client->GetCharacterID());
  2471. if(result && mysql_num_rows(result) > 0) {
  2472. MYSQL_ROW row;
  2473. Quest* quest = 0;
  2474. int32 quest_id = 0;
  2475. map<int32, int32>* progress_map = new map<int32, int32>();
  2476. while(result && (row = mysql_fetch_row(result))){
  2477. if(quest_id != atoul(row[0])){
  2478. if(quest_id > 0){
  2479. quest = client->GetPlayer()->GetQuest(quest_id);
  2480. if(quest)
  2481. client->SetPlayerQuest(quest, progress_map);
  2482. }
  2483. quest_id = atoul(row[0]);
  2484. progress_map->clear();
  2485. }
  2486. (*progress_map)[atoul(row[1])] = atoul(row[2]);
  2487. }
  2488. if(progress_map->size() > 0){
  2489. quest = client->GetPlayer()->GetQuest(quest_id);
  2490. if(quest)
  2491. client->SetPlayerQuest(quest, progress_map);
  2492. }
  2493. safe_delete(progress_map);
  2494. }
  2495. else if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2496. LogWrite(WORLD__ERROR, 0, "World", "Error in LoadCharacterQuestProgress query '%s': %s", query.GetQuery(), query.GetError());
  2497. }
  2498. void WorldDatabase::LoadCharacterQuests(Client* client){
  2499. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading Character Quests...");
  2500. Query query;
  2501. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT quest_id, DAY(given_date), MONTH(given_date), YEAR(given_date), DAY(completed_date), MONTH(completed_date), YEAR(completed_date), quest_giver, tracked, quest_flags, hidden, UNIX_TIMESTAMP(given_date), UNIX_TIMESTAMP(completed_date), complete_count FROM character_quests WHERE char_id=%u ORDER BY current_quest", client->GetCharacterID());
  2502. if(result && mysql_num_rows(result) > 0) {
  2503. MYSQL_ROW row;
  2504. Quest* quest = 0;
  2505. while(result && (row = mysql_fetch_row(result))){
  2506. quest = master_quest_list.GetQuest(atoul(row[0]));
  2507. if(quest) {
  2508. LogWrite(PLAYER__DEBUG, 5, "Player", "\tLoading quest_id: %u", atoul(row[0]));
  2509. bool addQuest = true;
  2510. if(row[4] && atoi(row[4]) > 0){
  2511. quest->SetTurnedIn(true);
  2512. if(row[4])
  2513. quest->SetDay(atoi(row[4]));
  2514. if(row[5])
  2515. quest->SetMonth(atoi(row[5]));
  2516. if(row[6] && atoi(row[6]) > 2000)
  2517. quest->SetYear(atoi(row[6]) - 2000);
  2518. client->GetPlayer()->AddCompletedQuest(quest);
  2519. // Added timestamps to quickly compare given and completed dates
  2520. int32 given_timestamp = atoul(row[11]);
  2521. int32 completed_timestamp = atoul(row[12]);
  2522. // If given timestamp is greater then completed then this is a repeatable quest we are working on
  2523. // so get a fresh quest object to add as an active quest
  2524. if (given_timestamp > completed_timestamp)
  2525. {
  2526. lua_interface->SetLuaUserDataStale(quest);
  2527. safe_delete(quest);
  2528. quest = master_quest_list.GetQuest(atoul(row[0]));
  2529. }
  2530. else
  2531. addQuest = false;
  2532. quest->SetCompleteCount(atoi(row[13]));
  2533. }
  2534. if (addQuest) {
  2535. if(row[1])
  2536. quest->SetDay(atoi(row[1]));
  2537. if(row[2])
  2538. quest->SetMonth(atoi(row[2]));
  2539. if(row[3] && atoi(row[3]) > 2000)
  2540. quest->SetYear(atoi(row[3]) - 2000);
  2541. quest->SetQuestGiver(atoul(row[7]));
  2542. quest->SetTracked(atoi(row[8]) == 1 ? true : false);
  2543. quest->SetQuestFlags(atoul(row[9]));
  2544. quest->SetHidden(atoi(row[10]) == 1 ? true : false);
  2545. client->AddPlayerQuest(quest, false, false);
  2546. }
  2547. quest->SetSaveNeeded(false);
  2548. // Changed this to call reload with step = 0 for all quests and not
  2549. // just quests with quest flags to allow customized set up if needed
  2550. if (lua_interface)
  2551. lua_interface->CallQuestFunction(quest, "Reload", client->GetPlayer(), 0);
  2552. }
  2553. }
  2554. LoadCharacterQuestProgress(client);
  2555. }
  2556. }
  2557. void WorldDatabase::LoadCharacterFriendsIgnoreList(Player* player) {
  2558. if (player) {
  2559. Query query;
  2560. MYSQL_ROW row;
  2561. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `name`, `type` FROM `character_social` WHERE `char_id`=%u", player->GetCharacterID());
  2562. while (result && (row = mysql_fetch_row(result))) {
  2563. if (strncmp(row[1], "FRIEND", 6) == 0)
  2564. player->AddFriend(row[0], false);
  2565. else
  2566. player->AddIgnore(row[0], false);
  2567. }
  2568. }
  2569. }
  2570. void WorldDatabase::LoadZoneInfo(ZoneServer* zone){
  2571. Query query;
  2572. int32 ruleset_id;
  2573. char* escaped = getEscapeString(zone->GetZoneName());
  2574. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, file, description, underworld, safe_x, safe_y, safe_z, min_status, min_level, max_level, instance_type+0, shutdown_timer, zone_motd, default_reenter_time, default_reset_time, default_lockout_time, force_group_to_zone, safe_heading, xp_modifier, ruleset_id, expansion_id, weather_allowed, sky_file, can_bind, can_gate, city_zone, can_evac FROM zones where name='%s'",escaped);
  2575. if(result && mysql_num_rows(result) > 0) {
  2576. MYSQL_ROW row;
  2577. row = mysql_fetch_row(result);
  2578. zone->SetZoneName(escaped);
  2579. zone->SetZoneID(strtoul(row[0], NULL, 0));
  2580. zone->SetZoneFile(row[1]);
  2581. zone->SetZoneDescription(row[2]);
  2582. zone->SetUnderWorld(atof(row[3]));
  2583. zone->SetSafeX(atof(row[4]));
  2584. zone->SetSafeY(atof(row[5]));
  2585. zone->SetSafeZ(atof(row[6]));
  2586. zone->SetMinimumStatus(atoi(row[7]));
  2587. zone->SetMinimumLevel(atoi(row[8]));
  2588. zone->SetMaximumLevel(atoi(row[9]));
  2589. int8 type = (atoi(row[10]) == 0) ? 0 : atoi(row[10]) - 1;
  2590. zone->SetInstanceType(type);
  2591. zone->SetShutdownTimer(atoul(row[11]));
  2592. char* zone_motd = row[12];
  2593. if (zone_motd && strlen(zone_motd) > 0)
  2594. zone->SetZoneMOTD(string(zone_motd));
  2595. zone->SetDefaultReenterTime(atoi(row[13]));
  2596. zone->SetDefaultResetTime(atoi(row[14]));
  2597. zone->SetDefaultLockoutTime(atoi(row[15]));
  2598. zone->SetForceGroupZoneOption(atoi(row[16]));
  2599. zone->SetSafeHeading(atof(row[17]));
  2600. zone->SetXPModifier(atof(row[18]));
  2601. if ((ruleset_id = atoul(row[19])) > 0 && !rule_manager.SetZoneRuleSet(zone->GetZoneID(), ruleset_id))
  2602. LogWrite(ZONE__ERROR, 0, "Zones", "Error setting rule set for zone '%s' (%u). A rule set with ID %u does not exist.", zone->GetZoneName(), zone->GetZoneID(), ruleset_id);
  2603. // check data_version to see if client has proper expansion to enter a zone
  2604. zone->SetMinimumVersion(GetMinimumClientVersion(atoi(row[20])));
  2605. zone->SetWeatherAllowed(atoi(row[21]) == 0 ? false : true);
  2606. zone->SetZoneSkyFile(row[22]);
  2607. if (zone->IsInstanceZone())
  2608. {
  2609. if ( zone->GetInstanceID() < 1 )
  2610. zone->SetupInstance(CreateNewInstance(zone->GetZoneID()));
  2611. else
  2612. zone->SetupInstance(zone->GetInstanceID());
  2613. }
  2614. zone->SetCanBind(atoul(row[23]));
  2615. zone->SetCanGate(atoul(row[24]));
  2616. zone->SetCityZone(atoi(row[25]));
  2617. zone->SetCanEvac(atoul(row[26]));
  2618. }
  2619. safe_delete_array(escaped);
  2620. }
  2621. void WorldDatabase::LoadZoneInfo(ZoneInfo* zone_info) {
  2622. Query query;
  2623. int32 ruleset_id;
  2624. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name, file, description, underworld, safe_x, safe_y, safe_z, min_status, min_level, max_level, instance_type, shutdown_timer, zone_motd, default_reenter_time, default_reset_time, default_lockout_time, force_group_to_zone, lua_script, xp_modifier, ruleset_id, expansion_id, always_loaded, city_zone, start_zone, zone_type, weather_allowed, sky_file FROM zones WHERE id = %u", zone_info->id);
  2625. if (result && mysql_num_rows(result) > 0) {
  2626. MYSQL_ROW row;
  2627. row = mysql_fetch_row(result);
  2628. strncpy(zone_info->name, row[0], sizeof(zone_info->name));
  2629. strncpy(zone_info->file, row[1], sizeof(zone_info->file));
  2630. strncpy(zone_info->description, row[2], sizeof(zone_info->description));
  2631. zone_info->underworld = atof(row[3]);
  2632. zone_info->safe_x = atof(row[4]);
  2633. zone_info->safe_y = atof(row[5]);
  2634. zone_info->safe_z = atof(row[6]);
  2635. zone_info->min_status = atoi(row[7]);
  2636. zone_info->min_level = atoi(row[8]);
  2637. zone_info->max_level = atoi(row[9]);
  2638. zone_info->instance_type = (atoi(row[10]) == 0) ? 0 : atoi(row[10]) - 1;
  2639. zone_info->shutdown_timer = atoul(row[11]);
  2640. row[12] == NULL ? strncpy(zone_info->zone_motd, "", sizeof(zone_info->zone_motd)) : strncpy(zone_info->zone_motd, row[12], sizeof(zone_info->zone_motd));
  2641. zone_info->default_reenter_time = atoi(row[13]);
  2642. zone_info->default_reset_time = atoi(row[14]);
  2643. zone_info->default_lockout_time = atoi(row[15]);
  2644. zone_info->force_group_to_zone = atoi(row[16]);
  2645. row[17] == NULL ? strncpy(zone_info->lua_script, "", sizeof(zone_info->lua_script)) : strncpy(zone_info->lua_script, row[17], sizeof(zone_info->lua_script));
  2646. zone_info->xp_modifier = atof(row[18]);
  2647. zone_info->ruleset_id = atoul(row[19]);
  2648. if ((ruleset_id = atoul(row[19])) > 0 && !rule_manager.SetZoneRuleSet(zone_info->id, ruleset_id))
  2649. LogWrite(ZONE__ERROR, 0, "Zones", "Error setting rule set for zone '%s' (%u). A rule set with ID %u does not exist.", zone_info->name, zone_info->id, ruleset_id);
  2650. zone_info->expansion_id = atoi(row[20]);
  2651. zone_info->min_version = GetMinimumClientVersion(zone_info->expansion_id);
  2652. zone_info->always_loaded = atoi(row[21]);
  2653. zone_info->city_zone = atoi(row[22]);
  2654. zone_info->start_zone = atoi(row[23]);
  2655. row[24] == NULL ? strncpy(zone_info->zone_type, "", sizeof(zone_info->zone_type)) : strncpy(zone_info->zone_type, row[24], sizeof(zone_info->zone_type));
  2656. zone_info->weather_allowed = atoi(row[25]);
  2657. strncpy(zone_info->sky_file, row[26], sizeof(zone_info->sky_file));
  2658. }
  2659. }
  2660. void WorldDatabase::SaveZoneInfo(int32 zone_id, const char* field, sint32 value) {
  2661. Query query;
  2662. query.RunQuery2(Q_UPDATE, "UPDATE `zones` SET `%s`=%i WHERE `id`=%u", field, value, zone_id);
  2663. }
  2664. void WorldDatabase::SaveZoneInfo(int32 zone_id, const char* field, float value) {
  2665. Query query;
  2666. query.RunQuery2(Q_UPDATE, "UPDATE `zones` SET `%s`=%f WHERE `id`=%u", field, value, zone_id);
  2667. }
  2668. void WorldDatabase::SaveZoneInfo(int32 zone_id, const char* field, const char* value) {
  2669. Query query;
  2670. query.RunQuery2(Q_UPDATE, "UPDATE `zones` SET `%s`='%s' WHERE `id`=%u", field, const_cast<char*>(getEscapeString(value)), zone_id);
  2671. }
  2672. int32 WorldDatabase::GetZoneID(const char* name) {
  2673. int32 zone_id = 0;
  2674. Query query;
  2675. char* escaped = getEscapeString(name);
  2676. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id` FROM zones WHERE `name`=\"%s\"", escaped);
  2677. if (result && mysql_num_rows(result) > 0) {
  2678. MYSQL_ROW row;
  2679. row = mysql_fetch_row(result);
  2680. zone_id = atoi(row[0]);
  2681. }
  2682. safe_delete_array(escaped);
  2683. return zone_id;
  2684. }
  2685. bool WorldDatabase::GetZoneRequirements(const char* zoneName, sint16* minStatus, int16* minLevel, int16* maxLevel, int16* minVersion) {
  2686. Query query;
  2687. char* escaped = getEscapeString(zoneName);
  2688. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT min_status, min_level, max_level, expansion_id FROM zones where name='%s'",escaped);
  2689. if(result && mysql_num_rows(result) > 0) {
  2690. MYSQL_ROW row;
  2691. row = mysql_fetch_row(result);
  2692. sint16 status = (sint16)atoi(row[0]);
  2693. int16 levelMin = (int16)atoi(row[1]);
  2694. int16 levelMax = (int16)atoi(row[2]);
  2695. int8 expansion_id = (int8)atoi(row[3]);
  2696. *minStatus = status;
  2697. *minLevel = levelMin;
  2698. *maxLevel = levelMax;
  2699. if( expansion_id >= 40 ) // lowest client we support is RoK - exp04
  2700. *minVersion = GetMinimumClientVersion(expansion_id);
  2701. else
  2702. *minVersion = 0;
  2703. safe_delete_array(escaped);
  2704. return true;
  2705. }
  2706. safe_delete_array(escaped);
  2707. return false;
  2708. }
  2709. int16 WorldDatabase::GetMinimumClientVersion(int8 expansion_id)
  2710. {
  2711. /*
  2712. 1 n/a Classic Expansion
  2713. 2 adv01 Bloodline Chronicles
  2714. 3 adv02 Splitpaw Saga
  2715. 10 exp01 Desert of Flames
  2716. 20 exp02 Kingdom of Sky
  2717. 21 adv04 Fallen Dynasty
  2718. 30 exp03 Echoes of Faydwer
  2719. 40 exp04 Rise of Kunark
  2720. 50 exp05 The Shadow Odyssey
  2721. 60 exp06 Sentinel's Fate
  2722. 61 halas Halas Reborn
  2723. 70 exp07 Destiny of Velious
  2724. 80 exp08 Age of Discovery
  2725. */
  2726. int16 minVer = 0;
  2727. // TODO: eventually replace this with reading values from eq2expansions table
  2728. switch(expansion_id)
  2729. {
  2730. case 40: // ROK
  2731. {
  2732. minVer = 843;
  2733. break;
  2734. }
  2735. case 50: // TSO
  2736. {
  2737. minVer = 908;
  2738. break;
  2739. }
  2740. case 60: // SF
  2741. {
  2742. minVer = 1008;
  2743. break;
  2744. }
  2745. case 61: // Halas
  2746. {
  2747. minVer = 1045;
  2748. break;
  2749. }
  2750. case 70: // DoV
  2751. {
  2752. minVer = 1096;
  2753. break;
  2754. }
  2755. case 80: // AoD
  2756. {
  2757. minVer = 1144;
  2758. break;
  2759. }
  2760. case 90: // CoE
  2761. {
  2762. minVer = 1188;
  2763. break;
  2764. }
  2765. }
  2766. return minVer;
  2767. }
  2768. // returns Expansion Name depending on the connected client's data version
  2769. string WorldDatabase::GetExpansionIDByVersion(int16 version)
  2770. {
  2771. /*
  2772. 0 n/a Classic Expansion
  2773. 0 adv01 Bloodline Chronicles
  2774. 0 adv02 Splitpaw Saga
  2775. 0 exp01 Desert of Flames
  2776. 0 exp02 Kingdom of Sky
  2777. 0 adv04 Fallen Dynasty
  2778. 0 exp03 Echoes of Faydwer
  2779. 843 exp04 Rise of Kunark
  2780. 908 exp05 The Shadow Odyssey
  2781. 1008 exp06 Sentinel's Fate
  2782. 1045 halas Halas Reborn
  2783. 1096 exp07 Destiny of Velious
  2784. 1142 exp08 Age of Discovery
  2785. 1188 exp09 Chains of Eternity
  2786. 9999 (and beyond)
  2787. */
  2788. string ret = "";
  2789. if( version >= 9999 )
  2790. ret = "Unknown";
  2791. else if( version >= 1188 )
  2792. ret = "Chains of Eternity";
  2793. else if( version >= 1142 )
  2794. ret = "Age of Discovery";
  2795. else if( version >= 1096 )
  2796. ret = "Destiny of Velious";
  2797. else if( version >= 1045 )
  2798. ret = "Halas Reborn";
  2799. else if( version >= 1008 )
  2800. ret = "Sentinel's Fate";
  2801. else if( version >= 908 )
  2802. ret = "The Shadow Odyssey";
  2803. else if( version >= 843 )
  2804. ret = "Rise of Kunark";
  2805. else
  2806. ret = "Any";
  2807. return ret;
  2808. }
  2809. void WorldDatabase::LoadSpecialZones(){
  2810. Query query;
  2811. ZoneServer* zone = 0;
  2812. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name, always_loaded FROM zones where always_loaded = 1");
  2813. if(result && mysql_num_rows(result) > 0) {
  2814. MYSQL_ROW row;
  2815. while(result && (row = mysql_fetch_row(result))){
  2816. zone = new ZoneServer(row[1]);
  2817. LoadZoneInfo(zone);
  2818. zone->Init();
  2819. zone->SetAlwaysLoaded(atoi(row[2]) == 1);
  2820. }
  2821. }
  2822. }
  2823. bool WorldDatabase::SpawnGroupRemoveAssociation(int32 group1, int32 group2){
  2824. Query query;
  2825. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_group_associations where (group_id1 = %u and group_id2 = %u) or (group_id1 = %u and group_id2 = %u)", group1, group2, group2, group1);
  2826. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2827. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupRemoveSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2828. return false;
  2829. }
  2830. return true;
  2831. }
  2832. bool WorldDatabase::SpawnGroupAddAssociation(int32 group1, int32 group2){
  2833. Query query;
  2834. query.RunQuery2(Q_INSERT, "insert ignore into spawn_location_group_associations (group_id1, group_id2) values(%u, %u)", group1, group2);
  2835. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2836. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupAddAssociation query '%s': %s", query.GetQuery(), query.GetError());
  2837. return false;
  2838. }
  2839. return true;
  2840. }
  2841. bool WorldDatabase::SpawnGroupAddSpawn(Spawn* spawn, int32 group_id){
  2842. Query query;
  2843. query.RunQuery2(Q_INSERT, "insert ignore into spawn_location_group (group_id, placement_id) values(%u, %u)", group_id, spawn->GetSpawnLocationPlacementID());
  2844. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2845. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupAddSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2846. return false;
  2847. }
  2848. return true;
  2849. }
  2850. bool WorldDatabase::SpawnGroupRemoveSpawn(Spawn* spawn, int32 group_id){
  2851. Query query;
  2852. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_group where group_id = %u and placement_id = %u", group_id, spawn->GetSpawnLocationPlacementID());
  2853. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2854. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupRemoveSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2855. return false;
  2856. }
  2857. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT count(group_id) FROM spawn_location_group where group_id = %u", group_id);
  2858. if(result && mysql_num_rows(result) > 0) {
  2859. MYSQL_ROW row;
  2860. if((row = mysql_fetch_row(result))){
  2861. if(atoul(row[0]) == 0)
  2862. DeleteSpawnGroup(group_id);
  2863. }
  2864. }
  2865. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2866. LogWrite(WORLD__ERROR, 0, "World", "Error in SpawnGroupRemoveSpawn query '%s': %s", query.GetQuery(), query.GetError());
  2867. return false;
  2868. }
  2869. return true;
  2870. }
  2871. int32 WorldDatabase::CreateSpawnGroup(Spawn* spawn, string name)
  2872. {
  2873. int32 group_id = 0;
  2874. Query query;
  2875. // JA: As of 0.7.1, DB Milestone 2, Content Team needs to use group_id's from Raw Data, so start any manual group_id's > 100,000
  2876. query.RunQuery2(Q_INSERT, "INSERT INTO spawn_location_group (group_id, placement_id, name) SELECT IF(ISNULL(MAX(group_id))=1, 100000, MAX(group_id)+1), %u, '%s' FROM spawn_location_group", spawn->GetSpawnLocationPlacementID(), getSafeEscapeString(name.c_str()).c_str());
  2877. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  2878. return 0;
  2879. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(group_id) FROM spawn_location_group");
  2880. if(result && mysql_num_rows(result) > 0)
  2881. {
  2882. MYSQL_ROW row;
  2883. if((row = mysql_fetch_row(result)))
  2884. {
  2885. if(row[0])
  2886. group_id = atoul(row[0]);
  2887. }
  2888. }
  2889. return group_id;
  2890. }
  2891. void WorldDatabase::DeleteSpawnGroup(int32 id){
  2892. Query query;
  2893. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_group where group_id = %u", id);
  2894. }
  2895. bool WorldDatabase::SetGroupSpawnChance(int32 id, float chance){
  2896. Query query;
  2897. query.RunQuery2(Q_UPDATE, "replace into spawn_location_group_chances (group_id, percentage) values(%u, %f)", id, chance);
  2898. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  2899. LogWrite(WORLD__ERROR, 0, "World", "Error in SetGroupSpawnChance query '%s': %s", query.GetQuery(), query.GetError());
  2900. return false;
  2901. }
  2902. return true;
  2903. }
  2904. int32 WorldDatabase::LoadSpawnGroupChances(ZoneServer* zone){
  2905. Query query;
  2906. int32 count = 0;
  2907. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT slgc.group_id, slgc.percentage FROM spawn_location_group_chances slgc, spawn_location_group slg, spawn_location_placement slp where slgc.group_id = slg.group_id and slg.placement_id = slp.id and slp.zone_id = %u", zone->GetZoneID());
  2908. if(result && mysql_num_rows(result) > 0) {
  2909. MYSQL_ROW row;
  2910. int32 group_id = 0;
  2911. float percent = 0;
  2912. while(result && (row = mysql_fetch_row(result))){
  2913. group_id = atoul(row[0]);
  2914. percent = atof(row[1]);
  2915. zone->AddSpawnGroupChance(group_id, percent);
  2916. count++;
  2917. }
  2918. }
  2919. return count;
  2920. }
  2921. int32 WorldDatabase::LoadSpawnLocationGroupAssociations(ZoneServer* zone){
  2922. Query query;
  2923. int32 count = 0;
  2924. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT distinct slga.group_id1, slga.group_id2 FROM spawn_location_group_associations slga, spawn_location_group slg, spawn_location_placement slp where (slg.group_id = slga.group_id1 or slg.group_id = slga.group_id2) and slg.placement_id = slp.id and slp.zone_id = %u", zone->GetZoneID());
  2925. if(result && mysql_num_rows(result) > 0) {
  2926. MYSQL_ROW row;
  2927. while(result && (row = mysql_fetch_row(result))){
  2928. zone->AddSpawnGroupAssociation(atoul(row[0]), atoul(row[1]));
  2929. count++;
  2930. }
  2931. }
  2932. return count;
  2933. }
  2934. int32 WorldDatabase::LoadSpawnLocationGroups(ZoneServer* zone){
  2935. Query query;
  2936. int32 count = 0;
  2937. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT slg.group_id, slg.placement_id, slp.spawn_location_id FROM spawn_location_group slg, spawn_location_placement slp WHERE slg.placement_id = slp.id and slp.zone_id = %u", zone->GetZoneID());
  2938. if(result && mysql_num_rows(result) > 0) {
  2939. MYSQL_ROW row;
  2940. int32 placement_id = 0;
  2941. int32 group_id = 0;
  2942. int32 spawn_location_id = 0;
  2943. while(result && (row = mysql_fetch_row(result))){
  2944. group_id = atoul(row[0]);
  2945. placement_id = atoul(row[1]);
  2946. spawn_location_id = atoul(row[2]);
  2947. zone->AddSpawnGroupLocation(group_id, placement_id, spawn_location_id);
  2948. count++;
  2949. }
  2950. }
  2951. return count;
  2952. }
  2953. int32 WorldDatabase::ProcessSpawnLocations(ZoneServer* zone, const char* sql_query, int8 type){
  2954. int32 number = 0;
  2955. Query query;
  2956. MYSQL_RES* result = query.RunQuery2(Q_SELECT, sql_query, zone->GetZoneID());
  2957. if(result && mysql_num_rows(result) > 0) {
  2958. MYSQL_ROW row;
  2959. int32 spawn_location_id = 0xFFFFFFFF;
  2960. SpawnLocation* spawn_location = 0;
  2961. while(result && (row = mysql_fetch_row(result))){
  2962. if((spawn_location_id == 0xFFFFFFFF) || atoul(row[0]) != spawn_location_id){
  2963. if(spawn_location){
  2964. zone->AddSpawnLocation(spawn_location_id, spawn_location);
  2965. number++;
  2966. }
  2967. spawn_location = new SpawnLocation();
  2968. }
  2969. SpawnEntry* entry = new SpawnEntry;
  2970. spawn_location_id = atoul(row[0]);
  2971. entry->spawn_location_id = spawn_location_id;
  2972. entry->spawn_entry_id = atoul(row[1]);
  2973. entry->spawn_type = type;
  2974. entry->spawn_id = atoul(row[9]);
  2975. entry->spawn_percentage = atof(row[10]);
  2976. entry->respawn = atoul(row[11]);
  2977. entry->expire_time = atoul(row[14]);
  2978. entry->expire_offset = atoul(row[15]);
  2979. //devn00b add stat overrides. Just a slight increase in size. Used in ZoneServer::AddNPCSpawn.
  2980. entry->lvl_override = atoul(row[19]);
  2981. entry->hp_override = atoul(row[20]);
  2982. entry->mp_override = atoul(row[21]);
  2983. entry->str_override = atoul(row[22]);
  2984. entry->sta_override = atoul(row[23]);
  2985. entry->wis_override = atoul(row[24]);
  2986. entry->int_override = atoul(row[25]);
  2987. entry->agi_override = atoul(row[26]);
  2988. entry->heat_override = atoul(row[27]);
  2989. entry->cold_override = atoul(row[28]);
  2990. entry->magic_override = atoul(row[29]);
  2991. entry->mental_override = atoul(row[30]);
  2992. entry->divine_override = atoul(row[31]);
  2993. entry->disease_override = atoul(row[32]);
  2994. entry->poison_override = atoul(row[33]);
  2995. entry->difficulty_override = atoul(row[34]);
  2996. spawn_location->x = atof(row[2]);
  2997. spawn_location->y = atof(row[3]);
  2998. spawn_location->z = atof(row[4]);
  2999. spawn_location->x_offset = atof(row[5]);
  3000. spawn_location->y_offset = atof(row[6]);
  3001. spawn_location->z_offset = atof(row[7]);
  3002. spawn_location->heading = atof(row[8]);
  3003. spawn_location->pitch = atof(row[16]);
  3004. spawn_location->roll = atof(row[17]);
  3005. spawn_location->conditional = atoi(row[18]);
  3006. spawn_location->total_percentage += entry->spawn_percentage;
  3007. spawn_location->grid_id = strtoul(row[12], NULL, 0);
  3008. spawn_location->placement_id = strtoul(row[13], NULL, 0);
  3009. spawn_location->AddSpawn(entry);
  3010. }
  3011. if(spawn_location){
  3012. zone->AddSpawnLocation(spawn_location_id, spawn_location);
  3013. number++;
  3014. }
  3015. }
  3016. return number;
  3017. }
  3018. void WorldDatabase::ResetDatabase(){
  3019. Query query;
  3020. query.RunQuery2("delete FROM table_versions where name != 'table_versions'", Q_DELETE);
  3021. }
  3022. void WorldDatabase::EnableConstraints(){
  3023. Query query;
  3024. query.RunQuery2("/*!40101 SET SQL_MODE=@OLD_SQL_MODE */", Q_DBMS);
  3025. query.RunQuery2("/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */", Q_DBMS);
  3026. }
  3027. void WorldDatabase::DisableConstraints(){
  3028. Query query;
  3029. query.RunQuery2("/*!40101 SET NAMES utf8 */", Q_DBMS);
  3030. query.RunQuery2("/*!40101 SET SQL_MODE=''*/", Q_DBMS);
  3031. query.RunQuery2("/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */", Q_DBMS);
  3032. query.RunQuery2("/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */", Q_DBMS);
  3033. }
  3034. void WorldDatabase::LoadSpawns(ZoneServer* zone)
  3035. {
  3036. Query query;
  3037. int32 npcs = 0, objects = 0, widgets = 0, signs = 0, ground_spawns = 0, spawn_groups = 0, spawn_group_associations = 0, spawn_group_chances = 0;
  3038. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter LoadSpawns");
  3039. npcs = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition, slp.lvl_override, slp.hp_override, slp.mp_override, slp.str_override, slp.sta_override, slp.wis_override, slp.int_override, slp.agi_override, slp.heat_override, slp.cold_override, slp.magic_override, slp.mental_override, slp.divine_override, slp.disease_override, slp.poison_override, difficulty_override FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_npcs sn where sn.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_NPC);
  3040. objects = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition, slp.lvl_override, slp.hp_override, slp.mp_override, slp.str_override, slp.sta_override, slp.wis_override, slp.int_override, slp.agi_override, slp.heat_override, slp.cold_override, slp.magic_override, slp.mental_override, slp.divine_override, slp.disease_override, slp.poison_override, difficulty_override FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_objects so where so.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_OBJECT);
  3041. widgets = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition, slp.lvl_override, slp.hp_override, slp.mp_override, slp.str_override, slp.sta_override, slp.wis_override, slp.int_override, slp.agi_override, slp.heat_override, slp.cold_override, slp.magic_override, slp.mental_override, slp.divine_override, slp.disease_override, slp.poison_override, difficulty_override FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_widgets sw where sw.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_WIDGET);
  3042. signs = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition, slp.lvl_override, slp.hp_override, slp.mp_override, slp.str_override, slp.sta_override, slp.wis_override, slp.int_override, slp.agi_override, slp.heat_override, slp.cold_override, slp.magic_override, slp.mental_override, slp.divine_override, slp.disease_override, slp.poison_override, difficulty_override FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_signs ss where ss.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_SIGN);
  3043. ground_spawns = ProcessSpawnLocations(zone, "SELECT sln.id, sle.id, slp.x, slp.y, slp.z, slp.x_offset, slp.y_offset, slp.z_offset, slp.heading, sle.spawn_id, sle.spawnpercentage, slp.respawn, slp.grid_id, slp.id, slp.expire_timer, slp.expire_offset, slp.pitch, slp.roll, sle.condition, slp.lvl_override, slp.hp_override, slp.mp_override, slp.str_override, slp.sta_override, slp.wis_override, slp.int_override, slp.agi_override, slp.heat_override, slp.cold_override, slp.magic_override, slp.mental_override, slp.divine_override, slp.disease_override, slp.poison_override, difficulty_override FROM spawn_location_placement slp, spawn_location_name sln, spawn_location_entry sle, spawn_ground sg where sg.spawn_id = sle.spawn_id and sln.id = sle.spawn_location_id and sln.id = slp.spawn_location_id and slp.zone_id=%i ORDER BY sln.id, sle.id", SPAWN_ENTRY_TYPE_GROUNDSPAWN);
  3044. spawn_groups = LoadSpawnLocationGroups(zone);
  3045. spawn_group_associations = LoadSpawnLocationGroupAssociations(zone);
  3046. spawn_group_chances = LoadSpawnGroupChances(zone);
  3047. LogWrite(SPAWN__INFO, 0, "Spawn", "Loaded for zone '%s' (%u):\n\t%u NPC(s), %u Object(s), %u Widget(s)\n\t%u Sign(s), %u Ground Spawn(s), %u Spawn Group(s)\n\t%u Spawn Group Association(s), %u Spawn Group Chance(s)", zone->GetZoneName(), zone->GetZoneID(), npcs, objects, widgets, signs, ground_spawns, spawn_groups, spawn_group_associations, spawn_group_chances);
  3048. LogWrite(SPAWN__TRACE, 0, "Spawn", "Exit LoadSpawns");
  3049. }
  3050. bool WorldDatabase::UpdateSpawnLocationSpawns(Spawn* spawn) {
  3051. Query query;
  3052. query.RunQuery2(Q_UPDATE, "update spawn_location_placement set x=%f, y=%f, z=%f, heading=%f, x_offset=%f, y_offset=%f, z_offset=%f, respawn=%u, expire_timer=%u, expire_offset=%u, grid_id=%u, pitch=%f, roll=%f where id = %u",
  3053. spawn->GetX(), spawn->GetY(), spawn->GetZ(), spawn->GetHeading(), spawn->GetXOffset(), spawn->GetYOffset(), spawn->GetZOffset(), spawn->GetRespawnTime(), spawn->GetExpireTime(), spawn->GetExpireOffsetTime(), spawn->GetLocation(), spawn->GetPitch(), spawn->GetRoll(), spawn->GetSpawnLocationPlacementID());
  3054. if (query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF) {
  3055. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateSpawnLocationSpawns query '%s': %s", query.GetQuery(), query.GetError());
  3056. return false;
  3057. }
  3058. return true;
  3059. }
  3060. bool WorldDatabase::UpdateSpawnWidget(int32 widget_id, char* queryString) {
  3061. Query query;
  3062. query.RunQuery2(Q_UPDATE, "update spawn_widgets set %s where widget_id = %u",
  3063. queryString, widget_id);
  3064. if (query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF) {
  3065. LogWrite(WORLD__ERROR, 0, "World", "Error in UpdateSpawnWidget query '%s': %s", query.GetQuery(), query.GetError());
  3066. return false;
  3067. }
  3068. return true;
  3069. }
  3070. vector<string>* WorldDatabase::GetSpawnNameList(const char* in_name){
  3071. Query query;
  3072. string names = "";
  3073. vector<string>* ret = 0;
  3074. string name = getSafeEscapeString(in_name);
  3075. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT concat(spawn.id, ', ', name) FROM spawn where name like '%%%s%%'", name.c_str());
  3076. if(result && mysql_num_rows(result) > 0){
  3077. ret = new vector<string>;
  3078. MYSQL_ROW row;
  3079. int8 num = 0;
  3080. while(result && (row = mysql_fetch_row(result))){
  3081. if(num >= 10)
  3082. break;
  3083. ret->push_back(string(row[0]));
  3084. num++;
  3085. }
  3086. char total[60] = {0};
  3087. if(mysql_num_rows(result) > 10)
  3088. sprintf(total, "Total number of results: %u (Limited to 10)", (int32)mysql_num_rows(result));
  3089. else
  3090. sprintf(total, "Total number of results: %u", (int32)mysql_num_rows(result));
  3091. ret->push_back(string(total));
  3092. }
  3093. return ret;
  3094. }
  3095. string WorldDatabase::GetZoneName(char* zone_description){
  3096. string ret;
  3097. Query query;
  3098. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM zones where description = '%s'", getSafeEscapeString(zone_description).c_str());
  3099. if(result && mysql_num_rows(result) > 0){
  3100. MYSQL_ROW row = mysql_fetch_row(result);
  3101. ret = string(row[0]);
  3102. }
  3103. return ret;
  3104. }
  3105. void WorldDatabase::LoadRevivePoints(vector<RevivePoint*>* revive_points, int32 zone_id){
  3106. if(revive_points && revive_points->size() > 0){
  3107. LogWrite(WORLD__ERROR, 0, "World", "Revive points have already been loaded for this zone!");
  3108. return;
  3109. }
  3110. else if(!revive_points || zone_id == 0){
  3111. LogWrite(WORLD__ERROR, 0, "World", "LoadRevivePoints called with null variables!");
  3112. return;
  3113. }
  3114. Query query;
  3115. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT respawn_zone_id, location_name, safe_x, safe_y, safe_z, heading, always_included FROM revive_points where zone_id=%u ORDER BY id asc", zone_id);
  3116. if(revive_points && result && mysql_num_rows(result) > 0){
  3117. MYSQL_ROW row;
  3118. int32 id = 0;
  3119. RevivePoint* point = 0;
  3120. while(result && (row=mysql_fetch_row(result))){
  3121. point = new RevivePoint;
  3122. point->id = id;
  3123. point->zone_id = atoul(row[0]);
  3124. point->location_name = string(row[1]);
  3125. point->x = atof(row[2]);
  3126. point->y = atof(row[3]);
  3127. point->z = atof(row[4]);
  3128. point->heading = atof(row[5]);
  3129. point->always_included = atoul(row[6]);
  3130. revive_points->push_back(point);
  3131. id++;
  3132. }
  3133. }
  3134. }
  3135. int32 WorldDatabase::GetNextSpawnIDInZone(int32 zone_id)
  3136. {
  3137. Query query;
  3138. int32 ret = 0;
  3139. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT MAX(id) FROM spawn where id LIKE '%i____'", zone_id);
  3140. if(result && mysql_num_rows(result) > 0) {
  3141. MYSQL_ROW row;
  3142. row = mysql_fetch_row(result);
  3143. if(row[0])
  3144. ret = atoi(row[0]) + 1;
  3145. }
  3146. if( ret == 0 )
  3147. ret = zone_id * 10000; // there are no spawns for that zone yet, to start with the first ID
  3148. LogWrite(WORLD__DEBUG, 0, "World", "Next Spawn ID for Zone %i: %u", zone_id, ret);
  3149. return ret;
  3150. }
  3151. bool WorldDatabase::SaveSpawnInfo(Spawn* spawn){
  3152. Query query;
  3153. string name = getSafeEscapeString(spawn->GetName());
  3154. string suffix = getSafeEscapeString(spawn->GetSuffixTitle());
  3155. string prefix = getSafeEscapeString(spawn->GetPrefixTitle());
  3156. string last_name = getSafeEscapeString(spawn->GetLastName());
  3157. if(spawn->GetDatabaseID() == 0){
  3158. int8 isInstanceType = (spawn->GetZone()->GetInstanceType() == Instance_Type::PERSONAL_HOUSE_INSTANCE);
  3159. int32 new_spawn_id = GetNextSpawnIDInZone(spawn->GetZone()->GetZoneID());
  3160. query.RunQuery2(Q_INSERT, "insert into spawn (id, name, race, model_type, size, targetable, show_name, command_primary, command_secondary, visual_state, attackable, show_level, show_command_icon, display_hand_icon, faction_id, collision_radius, hp, power, prefix, suffix, last_name, is_instanced_spawn, merchant_min_level, merchant_max_level) values(%u, '%s', %i, %i, %i, %i, %i, %u, %u, %i, %i, %i, %i, %i, %u, %i, %u, %u, '%s', '%s', '%s', %u, %u, %u)",
  3161. new_spawn_id, name.c_str(), spawn->GetRace(), spawn->GetModelType(), spawn->GetSize(), spawn->appearance.targetable, spawn->appearance.display_name, spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, 0, spawn->appearance.pos.collision_radius, spawn->GetTotalHP(), spawn->GetTotalPower(), prefix.c_str(), suffix.c_str(), last_name.c_str(), isInstanceType, spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel());
  3162. if( new_spawn_id > 0 )
  3163. spawn->SetDatabaseID(new_spawn_id); // use the new zone_id range
  3164. else if( query.GetLastInsertedID() > 0 )
  3165. spawn->SetDatabaseID(query.GetLastInsertedID()); // else fall back to last_inserted_id
  3166. else
  3167. return false; // else, hang your head in shame as you are an utter failure
  3168. if(spawn->IsNPC()){
  3169. query.RunQuery2(Q_INSERT, "insert into spawn_npcs (spawn_id, min_level, max_level, enc_level, class_, gender, min_group_size, max_group_size, hair_type_id, facial_hair_type_id, wing_type_id, chest_type_id, legs_type_id, soga_hair_type_id, soga_facial_hair_type_id, soga_model_type, heroic_flag, action_state, mood_state, initial_state, activity_status, hide_hood, emote_state) values(%u, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i)",
  3170. spawn->GetDatabaseID(), spawn->GetLevel(), spawn->GetLevel(), spawn->GetDifficulty(), spawn->GetAdventureClass(), spawn->GetGender(), 0, 0, ((NPC*)spawn)->features.hair_type, ((NPC*)spawn)->features.hair_face_type,
  3171. ((NPC*)spawn)->features.wing_type, ((NPC*)spawn)->features.chest_type, ((NPC*)spawn)->features.legs_type, ((NPC*)spawn)->features.soga_hair_type, ((NPC*)spawn)->features.soga_hair_face_type, spawn->appearance.soga_model_type, spawn->appearance.heroic_flag, spawn->GetActionState(), spawn->GetMoodState(), spawn->GetInitialState(), spawn->GetActivityStatus(), spawn->appearance.hide_hood, spawn->appearance.emote_state);
  3172. }
  3173. else if(spawn->IsObject()){
  3174. query.RunQuery2(Q_INSERT, "insert into spawn_objects (spawn_id) values(%u)", spawn->GetDatabaseID());
  3175. }
  3176. else if(spawn->IsWidget()){
  3177. Widget* widget = (Widget*)spawn;
  3178. query.RunQuery2(Q_INSERT, "insert into spawn_widgets (spawn_id, widget_id) values(%u, %u)", spawn->GetDatabaseID(), widget->GetWidgetID());
  3179. }
  3180. else if(spawn->IsSign()){
  3181. query.RunQuery2(Q_INSERT, "insert into spawn_signs (spawn_id, description) values(%u, 'change me')", spawn->GetDatabaseID());
  3182. }
  3183. else if (spawn->IsGroundSpawn()) {
  3184. query.RunQuery2(Q_INSERT, "insert into spawn_ground (spawn_id) values(%u)", spawn->GetDatabaseID());
  3185. }
  3186. }
  3187. else{
  3188. if(spawn->IsNPC()){
  3189. query.RunQuery2(Q_UPDATE, "update spawn_npcs, spawn set name='%s', min_level=%i, max_level=%i, enc_level=%i, race=%i, model_type=%i, class_=%i, gender=%i, show_name=%i, attackable=%i, show_level=%i, targetable=%i, show_command_icon=%i, display_hand_icon=%i, hair_type_id=%i, facial_hair_type_id=%i, wing_type_id=%i, chest_type_id=%i, legs_type_id=%i, soga_hair_type_id=%i, soga_facial_hair_type_id=%i, soga_model_type=%i, size=%i, hp=%u, heroic_flag=%i, power=%u, collision_radius=%i, command_primary=%u, command_secondary=%u, visual_state=%i, action_state=%i, mood_state=%i, initial_state=%i, activity_status=%i, alignment=%i, faction_id=%u, hide_hood=%i, emote_state=%i, suffix ='%s', prefix='%s', last_name='%s', merchant_min_level = %u, merchant_max_level = %u where spawn_npcs.spawn_id = spawn.id and spawn.id = %u",
  3190. name.c_str(), spawn->GetLevel(), spawn->GetLevel(), spawn->GetDifficulty(), spawn->GetRace(), spawn->GetModelType(),
  3191. spawn->GetAdventureClass(), spawn->GetGender(), spawn->appearance.display_name, spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.targetable, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, ((NPC*)spawn)->features.hair_type,
  3192. ((NPC*)spawn)->features.hair_face_type, ((NPC*)spawn)->features.wing_type, ((NPC*)spawn)->features.chest_type, ((NPC*)spawn)->features.legs_type, ((NPC*)spawn)->features.soga_hair_type, ((NPC*)spawn)->features.soga_hair_face_type, spawn->appearance.soga_model_type, spawn->GetSize(),
  3193. spawn->GetTotalHPBase(), spawn->appearance.heroic_flag, spawn->GetTotalPowerBase(), spawn->GetCollisionRadius(), spawn->GetPrimaryCommandListID(),
  3194. spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->GetActionState(), spawn->GetMoodState(), spawn->GetInitialState(),
  3195. spawn->GetActivityStatus(), ((NPC*)spawn)->GetAlignment(), spawn->GetFactionID(), spawn->appearance.hide_hood, spawn->appearance.emote_state,
  3196. suffix.c_str(), prefix.c_str(), last_name.c_str(), spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), spawn->GetDatabaseID());
  3197. }
  3198. else if(spawn->IsObject()){
  3199. query.RunQuery2(Q_UPDATE, "update spawn_objects, spawn set name='%s', model_type=%i, show_name=%i, targetable=%i, size=%i, command_primary=%u, command_secondary=%u, visual_state=%i, attackable=%i, show_level=%i, show_command_icon=%i, display_hand_icon=%i, collision_radius=%i, hp = %u, power = %u, device_id = %i, merchant_min_level = %u, merchant_max_level = %u where spawn_objects.spawn_id = spawn.id and spawn.id = %u",
  3200. name.c_str(), spawn->GetModelType(), spawn->appearance.display_name, spawn->appearance.targetable, spawn->GetSize(), spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon,
  3201. spawn->GetCollisionRadius(), spawn->GetTotalHP(), spawn->GetTotalPower(), ((Object*)spawn)->GetDeviceID(), spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), spawn->GetDatabaseID());
  3202. }
  3203. else if(spawn->IsWidget()){
  3204. Widget* widget = (Widget*)spawn;
  3205. char* openSound = 0;
  3206. char* closeSound = 0;
  3207. if (widget->GetOpenSound() != NULL) openSound = (char*)widget->GetOpenSound(); else openSound = (char*)string("0").c_str();
  3208. if (widget->GetCloseSound() != NULL) closeSound = (char*)widget->GetCloseSound(); else closeSound = (char*)string("0").c_str();
  3209. query.RunQuery2(Q_UPDATE, "update spawn_widgets, spawn set name='%s', race=%i, model_type=%i, show_name=%i, attackable=%i, show_level=%i, show_command_icon=%i, display_hand_icon=%i, size=%i, hp=%u, power=%u, collision_radius=%i, command_primary=%u, command_secondary=%u, visual_state=%i, faction_id=%u, suffix ='%s', prefix='%s', last_name='%s',widget_id = %u,widget_x = %f,widget_y = %f,widget_z = %f,include_heading = %u,include_location = %u,icon = %u,type='%s',open_heading = %f,closed_heading = %f,open_x = %f,open_y = %f,open_z = %f,action_spawn_id = %u,open_sound_file='%s',close_sound_file='%s',open_duration = %u,close_x = %f,close_y=%f,close_z=%f,linked_spawn_id = %u,house_id = %u, merchant_min_level = %u, merchant_max_level = %u where spawn_widgets.spawn_id = spawn.id and spawn.id = %u",
  3210. name.c_str(), spawn->GetRace(), spawn->GetModelType(), spawn->appearance.display_name, spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, spawn->GetSize(),
  3211. spawn->GetTotalHP(), spawn->GetTotalPower(), spawn->GetCollisionRadius(), spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->GetFactionID(),
  3212. suffix.c_str(), prefix.c_str(), last_name.c_str(), widget->GetWidgetID(), widget->GetX(), widget->GetY(), widget->GetZ(), widget->GetIncludeHeading(), widget->GetIncludeLocation(), widget->GetIconValue(), Widget::GetWidgetTypeNameByTypeID(widget->GetWidgetType()).c_str(),
  3213. widget->GetOpenHeading(), widget->GetClosedHeading(), widget->GetOpenX(), widget->GetOpenY(), widget->GetOpenZ(),
  3214. widget->GetActionSpawnID(), openSound, closeSound,widget->GetOpenDuration(),
  3215. widget->GetCloseX(),widget->GetCloseY(),widget->GetCloseZ(),widget->GetLinkedSpawnID(),widget->GetHouseID(),
  3216. spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), spawn->GetDatabaseID());
  3217. }
  3218. else if(spawn->IsSign()){
  3219. Sign* sign = (Sign*)spawn;
  3220. query.RunQuery2(Q_UPDATE, "update spawn_signs, spawn set name='%s', race=%i, model_type=%i, show_name=%i, attackable=%i, show_level=%i, show_command_icon=%i, display_hand_icon=%i, size=%i, hp=%u, power=%u, collision_radius=%i, command_primary=%u, command_secondary=%u, visual_state=%i, faction_id=%u, suffix ='%s', prefix='%s', last_name='%s', `type`='%s', zone_id = %u, widget_id = %u, title='%s', widget_x = %f, widget_y = %f, widget_z = %f, icon = %u, description='%s', sign_distance = %f, zone_x = %f, zone_y = %f, zone_z = %f, zone_heading = %f, include_heading = %u, include_location = %u, merchant_min_level = %u, merchant_max_level = %u, language = %u where spawn_signs.spawn_id = spawn.id and spawn.id = %u",
  3221. name.c_str(), spawn->GetRace(), spawn->GetModelType(), spawn->appearance.display_name, spawn->appearance.attackable, spawn->appearance.show_level, spawn->appearance.show_command_icon, spawn->appearance.display_hand_icon, spawn->GetSize(),
  3222. spawn->GetTotalHP(), spawn->GetTotalPower(), spawn->GetCollisionRadius(), spawn->GetPrimaryCommandListID(), spawn->GetSecondaryCommandListID(), spawn->GetVisualState(), spawn->GetFactionID(),
  3223. suffix.c_str(), prefix.c_str(), last_name.c_str(), sign->GetSignType() == SIGN_TYPE_GENERIC ? "Generic" : "Zone", sign->GetSignZoneID(),
  3224. sign->GetWidgetID(), sign->GetSignTitle(), sign->GetWidgetX(), sign->GetWidgetY(), sign->GetWidgetZ(),
  3225. sign->GetIconValue(), sign->GetSignDescription(), sign->GetSignDistance(), sign->GetSignZoneX(),
  3226. sign->GetSignZoneY(), sign->GetSignZoneZ(), sign->GetSignZoneHeading(), sign->GetIncludeHeading(),
  3227. sign->GetIncludeLocation(), spawn->GetMerchantMinLevel(), spawn->GetMerchantMaxLevel(), sign->GetLanguage(), spawn->GetDatabaseID());
  3228. }
  3229. }
  3230. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  3231. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in SaveSpawnInfo query '%s': %s", query.GetQuery(), query.GetError());
  3232. return false;
  3233. }
  3234. return true;
  3235. }
  3236. int32 WorldDatabase::SaveCombinedSpawnLocation(ZoneServer* zone, Spawn* in_spawn, const char* name){
  3237. vector<Spawn*>* spawns = in_spawn->GetSpawnGroup();
  3238. uint32 spawnLocationID = 0;
  3239. if(spawns && spawns->size() > 0){
  3240. vector<Spawn*>::iterator itr;
  3241. map<Spawn*, int32>::iterator freq_itr;
  3242. Spawn* spawn = 0;
  3243. map<Spawn*, int32> database_spawns;
  3244. int32 total = 0;
  3245. float x_offset = GetSpawnLocationPlacementOffsetX(in_spawn->GetSpawnLocationID());
  3246. float y_offset = GetSpawnLocationPlacementOffsetY(in_spawn->GetSpawnLocationID());
  3247. float z_offset = GetSpawnLocationPlacementOffsetZ(in_spawn->GetSpawnLocationID());
  3248. int32 spawn_location_id = GetNextSpawnLocation();
  3249. spawnLocationID = spawn_location_id;
  3250. if(!name)
  3251. name = "Combine SpawnGroup Generated";
  3252. if(!CreateNewSpawnLocation(spawn_location_id, name)){
  3253. safe_delete(spawns);
  3254. return 0;
  3255. }
  3256. for(itr = spawns->begin();itr!=spawns->end();itr++){
  3257. spawn = *itr;
  3258. if (spawn) {
  3259. RemoveSpawnFromSpawnLocation(spawn);
  3260. spawn->SetSpawnLocationID(spawn_location_id);
  3261. bool add = true;
  3262. for (freq_itr = database_spawns.begin(); freq_itr != database_spawns.end(); freq_itr++) {
  3263. if (spawn->GetDatabaseID() == freq_itr->first->GetDatabaseID()) {
  3264. freq_itr->second++;
  3265. total++;
  3266. add = false;
  3267. }
  3268. }
  3269. if (add) {
  3270. database_spawns[spawn] = 1;
  3271. total++;
  3272. }
  3273. }
  3274. }
  3275. for(freq_itr = database_spawns.begin(); freq_itr != database_spawns.end(); freq_itr++){
  3276. int8 percent = (freq_itr->second*100)/total;
  3277. if(!SaveSpawnEntry(freq_itr->first, name, percent, x_offset, y_offset, z_offset, freq_itr->first == in_spawn, false)){
  3278. safe_delete(spawns);
  3279. return 0;
  3280. }
  3281. }
  3282. for(itr=spawns->begin();itr!=spawns->end();itr++){
  3283. spawn = *itr;
  3284. zone->RemoveSpawn(spawn, true, true, true, true, true);
  3285. }
  3286. safe_delete(spawns);
  3287. }
  3288. else{
  3289. safe_delete(spawns);
  3290. return 0;
  3291. }
  3292. return spawnLocationID;
  3293. }
  3294. bool WorldDatabase::SaveSpawnEntry(Spawn* spawn, const char* spawn_location_name, int8 percent, float x_offset, float y_offset, float z_offset, bool save_zonespawn, bool create_spawnlocation){
  3295. Query query;
  3296. Query query2;
  3297. int32 count = 0;
  3298. if(create_spawnlocation){
  3299. count = GetSpawnLocationCount(spawn->GetSpawnLocationID());
  3300. if(count == 0){
  3301. if(!CreateNewSpawnLocation(spawn->GetSpawnLocationID(), spawn_location_name))
  3302. return false;
  3303. }
  3304. }
  3305. query.RunQuery2(Q_INSERT, "insert into spawn_location_entry (spawn_id, spawn_location_id, spawnpercentage) values(%u, %u, %i)", spawn->GetDatabaseID(), spawn->GetSpawnLocationID(), percent);
  3306. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  3307. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in SaveSpawnEntry query '%s': %s", query.GetQuery(), query.GetError());
  3308. return false;
  3309. }
  3310. if(save_zonespawn){
  3311. query2.RunQuery2(Q_INSERT, "insert into spawn_location_placement (zone_id, instance_id, spawn_location_id, x, y, z, x_offset, y_offset, z_offset, heading, grid_id) values(%u, %u, %u, %f, %f, %f, %f, %f, %f, %f, %u)", spawn->GetZone()->GetZoneID(), spawn->GetZone()->GetInstanceID(), spawn->GetSpawnLocationID(), spawn->GetX(), spawn->GetY(), spawn->GetZ(),x_offset, y_offset, z_offset, spawn->GetHeading(), spawn->GetLocation());
  3312. if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF){
  3313. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in SaveSpawnEntry query '%s': %s", query2.GetQuery(), query2.GetError());
  3314. return false;
  3315. }
  3316. spawn->SetSpawnLocationPlacementID(query2.GetLastInsertedID());
  3317. }
  3318. return true;
  3319. }
  3320. float WorldDatabase::GetSpawnLocationPlacementOffsetX(int32 location_id) {
  3321. Query query;
  3322. MYSQL_ROW row;
  3323. float ret = 0;
  3324. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `x_offset` FROM `spawn_location_placement` WHERE `spawn_location_id`=%u", location_id);
  3325. if (result && (row = mysql_fetch_row(result))) {
  3326. if (row[0])
  3327. ret = atof(row[0]);
  3328. }
  3329. return ret;
  3330. }
  3331. float WorldDatabase::GetSpawnLocationPlacementOffsetY(int32 location_id) {
  3332. Query query;
  3333. MYSQL_ROW row;
  3334. float ret = 0;
  3335. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `y_offset` FROM `spawn_location_placement` WHERE `spawn_location_id`=%u", location_id);
  3336. if (result && (row = mysql_fetch_row(result))) {
  3337. if (row[0])
  3338. ret = atof(row[0]);
  3339. }
  3340. return ret;
  3341. }
  3342. float WorldDatabase::GetSpawnLocationPlacementOffsetZ(int32 location_id) {
  3343. Query query;
  3344. MYSQL_ROW row;
  3345. float ret = 0;
  3346. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `z_offset` FROM `spawn_location_placement` WHERE `spawn_location_id`=%u", location_id);
  3347. if (result && (row = mysql_fetch_row(result))) {
  3348. if (row[0])
  3349. ret = atof(row[0]);
  3350. }
  3351. return ret;
  3352. }
  3353. bool WorldDatabase::CreateNewSpawnLocation(int32 id, const char* name){
  3354. Query query;
  3355. if(!name)
  3356. name = "Unknown Spawn Location Name";
  3357. string str_name = getSafeEscapeString(name);
  3358. query.RunQuery2(Q_INSERT, "insert into spawn_location_name (id, name) values(%u, '%s')", id, str_name.c_str());
  3359. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  3360. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in CreateNewSpawnLocation query '%s': %s", query.GetQuery(), query.GetError());
  3361. return false;
  3362. }
  3363. return true;
  3364. }
  3365. int32 WorldDatabase::GetSpawnLocationCount(int32 location, Spawn* spawn){
  3366. Query query;
  3367. int32 ret = 0;
  3368. MYSQL_RES* result = 0;
  3369. if(spawn)
  3370. result = query.RunQuery2(Q_SELECT, "SELECT count(id) FROM spawn_location_entry where spawn_location_id=%u and spawn_id=%u", location, spawn->GetDatabaseID());
  3371. else
  3372. result = query.RunQuery2(Q_SELECT, "SELECT count(id) FROM spawn_location_entry where spawn_location_id=%u", location);
  3373. if(result && mysql_num_rows(result) > 0){
  3374. MYSQL_ROW row;
  3375. while(result && (row = mysql_fetch_row(result)) && row[0]){
  3376. ret = strtoul(row[0], NULL, 0);
  3377. }
  3378. }
  3379. return ret;
  3380. }
  3381. int32 WorldDatabase::GetNextSpawnLocation(){
  3382. Query query;
  3383. int32 ret = 0;
  3384. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(id) FROM spawn_location_name");
  3385. if(result && mysql_num_rows(result) > 0){
  3386. MYSQL_ROW row;
  3387. while(result && (row = mysql_fetch_row(result)) && row[0]){
  3388. ret = strtoul(row[0], NULL, 0);
  3389. }
  3390. }
  3391. ret++;
  3392. return ret;
  3393. }
  3394. bool WorldDatabase::RemoveSpawnFromSpawnLocation(Spawn* spawn){
  3395. Query query;
  3396. Query query2;
  3397. int32 count = GetSpawnLocationCount(spawn->GetSpawnLocationID(), spawn);
  3398. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_placement where spawn_location_id=%u", spawn->GetSpawnLocationID());
  3399. if(count == 1)
  3400. query.RunQuery2(Q_DELETE, "delete FROM spawn_location_name where id=%u", spawn->GetSpawnLocationID());
  3401. query2.RunQuery2(Q_DELETE, "delete FROM spawn_location_entry where spawn_id=%u and spawn_location_id = %u", spawn->GetDatabaseID(), spawn->GetSpawnLocationID());
  3402. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  3403. LogWrite(WORLD__ERROR, 0, "World", "Error in RemoveSpawnFromSpawnLocation query '%s': %s", query.GetQuery(), query.GetError());
  3404. return false;
  3405. }
  3406. else if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF){
  3407. LogWrite(SPAWN__ERROR, 0, "Spawn", "Error in RemoveSpawnFromSpawnLocation query '%s': %s", query2.GetQuery(), query.GetError());
  3408. return false;
  3409. }
  3410. return true;
  3411. }
  3412. map<int32, string>* WorldDatabase::GetZoneList(const char* name, bool is_admin)
  3413. {
  3414. Query query;
  3415. map<int32, string>* ret = 0;
  3416. string zone_name = getSafeEscapeString(name);
  3417. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `name` FROM zones WHERE `name` RLIKE '%s' %s", zone_name.c_str(), (is_admin)?"":" LIMIT 0,10");
  3418. if(result && mysql_num_rows(result) > 0)
  3419. {
  3420. ret = new map<int32, string>;
  3421. MYSQL_ROW row;
  3422. while(result && (row = mysql_fetch_row(result)))
  3423. {
  3424. zone_name = row[1];
  3425. (*ret)[atoi(row[0])] = zone_name;
  3426. }
  3427. }
  3428. return ret;
  3429. }
  3430. void WorldDatabase::UpdateStartingFactions(int32 char_id, int8 choice){
  3431. Query query;
  3432. query.RunQuery2(Q_INSERT, "insert into character_factions (char_id, faction_id, faction_level) select %u, faction_id, value FROM starting_factions where starting_city=%i", char_id, choice);
  3433. }
  3434. string WorldDatabase::GetStartingZoneName(int8 choice){
  3435. Query query;
  3436. string zone_name = "";
  3437. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM zones where start_zone = %u", choice);
  3438. if(result && mysql_num_rows(result) > 0){
  3439. MYSQL_ROW row;
  3440. while(result && (row = mysql_fetch_row(result))){
  3441. zone_name = string(row[0]);
  3442. }
  3443. }
  3444. return zone_name;
  3445. }
  3446. void WorldDatabase::UpdateStartingZone(int32 char_id, int8 class_id, int8 race_id, PacketStruct* create)
  3447. {
  3448. Query query,query2;
  3449. int32 packetVersion = create->GetVersion();
  3450. int8 choice = create->getType_int8_ByName("starting_zone"); // 0 = far journey, 1 = isle of refuge
  3451. int8 deity = create->getType_int8_ByName("deity"); // aka 'alignment' for early DOF, 0 = evil, 1 = good
  3452. int32 startingZoneRuleFlag = rule_manager.GetGlobalRule(R_World, StartingZoneRuleFlag)->GetInt32();
  3453. bool enforceRacialAlignment = rule_manager.GetGlobalRule(R_World, EnforceRacialAlignment)->GetBool();
  3454. if((startingZoneRuleFlag == 1 || startingZoneRuleFlag == 2) && packetVersion > 561)
  3455. {
  3456. LogWrite(PLAYER__INFO, 0, "Player", "Starting zone rule flag %u override choice %u to deity value of 0", startingZoneRuleFlag, choice);
  3457. choice = 0;
  3458. }
  3459. LogWrite(PLAYER__INFO, 0, "Player", "Adding default zone for race: %i, class: %i for char_id: %u (choice: %i), deity(alignment): %u, version: %u.", race_id, class_id, char_id, choice, deity, packetVersion);
  3460. // first, check to see if there is a starting_zones record for this race/class/choice combo (now using extended Archetype/BaseClass/Class combos
  3461. MYSQL_RES* result = 0;
  3462. string whereRuleFlag("");
  3463. if(startingZoneRuleFlag > 0)
  3464. whereRuleFlag = string(" AND ruleflag & " + std::to_string(startingZoneRuleFlag));
  3465. string syntaxSelect("SELECT z.name, sz.zone_id, z.safe_x, z.safe_y, z.safe_z, sz.x, sz.y, sz.z, sz.heading, sz.is_instance, z.city_zone, sz.start_alignment FROM");
  3466. if ( class_id == 0 )
  3467. result = query.RunQuery2(Q_SELECT, "%s starting_zones sz, zones z WHERE sz.zone_id = z.id AND class_id = 255 AND race_id IN (%i, 255) AND deity IN (%i, 255) AND choice = %u AND (min_version = 0 or min_version <= %u) AND (max_version = 0 or max_version >= %u)%s",
  3468. syntaxSelect.c_str(), race_id, deity, choice, packetVersion, packetVersion, whereRuleFlag.c_str());
  3469. else
  3470. result = query.RunQuery2(Q_SELECT, "%s starting_zones sz, zones z WHERE sz.zone_id = z.id AND class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255) AND deity IN (%i, 255) AND choice IN (%i, 255) AND (min_version = 0 or min_version <= %u) AND (max_version = 0 or max_version >= %u)%s",
  3471. syntaxSelect.c_str(), classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id, deity, choice, packetVersion, packetVersion, whereRuleFlag.c_str());
  3472. if(result && mysql_num_rows(result) > 0)
  3473. {
  3474. string zone_name = "ERROR";
  3475. MYSQL_ROW row;
  3476. bool zoneSet = false;
  3477. float safeX = 0.0f, safeY = 0.0f, safeZ = 0.0f, x = 0.0f, y = 0.0f, z = 0.0f, heading = 0.0f;
  3478. int8 is_instance = 0;
  3479. int32 zone_id = 0;
  3480. int32 instance_id = 0;
  3481. int8 starting_city = 0;
  3482. sint8 start_alignment = 0;
  3483. if( result && (row = mysql_fetch_row(result)) )
  3484. {
  3485. int8 i=0;
  3486. zoneSet = true;
  3487. zone_name = string(row[i++]);
  3488. zone_id = atoul(row[i++]);
  3489. safeX = atof(row[i++]);
  3490. safeY = atof(row[i++]);
  3491. safeZ = atof(row[i++]);
  3492. x = atof(row[i++]);
  3493. y = atof(row[i++]);
  3494. z = atof(row[i++]);
  3495. if ( x == -999999.0f && y == -999999.0f && z == -999999.0f)
  3496. {
  3497. x = safeX;
  3498. y = safeY;
  3499. z = safeZ;
  3500. }
  3501. heading = atof(row[i++]);
  3502. if(heading == -999999.0f )
  3503. heading = 0.0f;
  3504. is_instance = atoul(row[i++]);
  3505. starting_city = atoul(row[i++]);
  3506. start_alignment = atoi(row[i++]);
  3507. }
  3508. // all EQ2 clients hard-code these restrictions in some form (later clients added Neutral instead of good/evil only)
  3509. // start with good races only
  3510. if(enforceRacialAlignment && (race_id == DWARF || race_id == FROGLOK || race_id == HALFLING ||
  3511. race_id == HIGH_ELF || race_id == WOOD_ELF || race_id == FAE)) {
  3512. start_alignment = ALIGNMENT_GOOD; // always should good
  3513. }
  3514. // we drop into this case because it is a special check compared to the straigt good/evil checks on top and below
  3515. else if(start_alignment == ALIGNMENT_EVIL && deity == ALIGNMENT_GOOD) {
  3516. if (enforceRacialAlignment && (race_id == AERAKYN || race_id == RATONGA || race_id == BARBARIAN || race_id == ERUDITE ||
  3517. race_id == HUMAN || race_id == VAMPIRE || race_id == HALF_ELF || race_id == GNOME || race_id == KERRA)) {
  3518. if(zone_id == 21 || zone_id == 25 || zone_id == 26 || zone_id == 27) { // far journey zones
  3519. start_alignment = deity; // inheriting the clients alignment of 'good' due to the fact we start in far journey and it is a shared instance right now (farjourneyfreeport)
  3520. }
  3521. // otherwise we use the starting zone alignment since these particular races can be evil which will be set in start_alignment of 0 within starting_zones (neriak, freeport, etc.)
  3522. }
  3523. else if(enforceRacialAlignment) {
  3524. LogWrite(WORLD__WARNING, 0, "World", "Starting alignment seems unexpected, zone id %u, race id %u, deity(alignment) choice: %u, start alignment: %i", zone_id, race_id, deity, start_alignment);
  3525. }
  3526. }
  3527. // anyone else is simply evil with the enforcement check
  3528. else if(enforceRacialAlignment && (race_id != DWARF && race_id != FROGLOK && race_id != HALFLING &&
  3529. race_id != HIGH_ELF && race_id != WOOD_ELF && race_id != FAE)) {
  3530. start_alignment = ALIGNMENT_EVIL;
  3531. }
  3532. if(is_instance) // should only be true if we get a result
  3533. {
  3534. // this will force a pre-load
  3535. ZoneServer* instance_zone = zone_list.GetByInstanceID(0, zone_id);
  3536. if (instance_zone) {
  3537. instance_id = CreateNewInstance(zone_id);
  3538. AddCharacterInstance(char_id, instance_id, string(instance_zone->GetZoneName()), instance_zone->GetInstanceType(), Timer::GetUnixTimeStamp(), 0, instance_zone->GetDefaultLockoutTime(), instance_zone->GetDefaultReenterTime());
  3539. // make sure we inherit the instance id setup in the AddCharacterInstance
  3540. instance_zone->SetupInstance(instance_id);
  3541. }
  3542. }
  3543. query2.RunQuery2(Q_UPDATE, "UPDATE characters SET current_zone_id = %u, x = %f, y = %f, z = %f, heading = %f, starting_city = %i, instance_id = %u, alignment = %u WHERE id = %u",
  3544. zone_id, x, y, z, heading, starting_city, instance_id, start_alignment, char_id);
  3545. if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF){
  3546. LogWrite(PLAYER__ERROR, 0, "Player", "Error in UpdateStartingZone custom starting_zones, query: '%s': %s", query2.GetQuery(), query2.GetError());
  3547. return;
  3548. }
  3549. if(query2.GetAffectedRows() > 0)
  3550. {
  3551. LogWrite(PLAYER__INFO, 0, "Player", "Setting New Character Starting Zone to '%s' with location %f, %f, %f and heading %f FROM starting_zones table.", zone_name.c_str(), x, y, z, heading);
  3552. return;
  3553. }
  3554. }
  3555. else
  3556. {
  3557. // there was no matching starting_zone value, so use default 'choice' starting city
  3558. query2.RunQuery2(Q_UPDATE, "UPDATE characters c, zones z SET c.current_zone_id = z.id, c.x = z.safe_x, c.y = z.safe_y, c.z = z.safe_z, c.starting_city = %i WHERE z.start_zone = %i and c.id = %u",
  3559. choice, choice, char_id);
  3560. if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF)
  3561. {
  3562. LogWrite(PLAYER__ERROR, 0, "Player", "Error in UpdateStartingZone player choice, query: '%s': %s", query2.GetQuery(), query2.GetError());
  3563. return;
  3564. }
  3565. if(query2.GetAffectedRows() > 0)
  3566. {
  3567. LogWrite(PLAYER__DEBUG, 0, "Player", "Setting New Character Starting Zone to '%s' FROM player choice.", GetStartingZoneName(choice).c_str());
  3568. return;
  3569. }
  3570. }
  3571. // if we are here, it's a bad thing. zone tables have no start_city values to match client 'choice', so throw the player into zone according to R_World::DefaultStartingZoneID rule.
  3572. // shout a few warnings so the admin fixes this asap!
  3573. int16 default_zone_id = rule_manager.GetGlobalRule(R_World, DefaultStartingZoneID)->GetInt16();
  3574. LogWrite(WORLD__WARNING, 0, "World", "No Starting City defined for player choice: %i! BAD! BAD! BAD! Defaulting player to zone %i.", choice, default_zone_id);
  3575. query.RunQuery2(Q_UPDATE, "UPDATE characters c, zones z SET c.current_zone_id = z.id, c.x = z.safe_x, c.y = z.safe_y, c.z = z.safe_z, c.heading = z.safe_heading, c.starting_city = 1 WHERE z.id = %i and c.id = %u", default_zone_id, char_id);
  3576. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  3577. {
  3578. LogWrite(PLAYER__ERROR, 0, "Player", "Error in UpdateStartingZone default zone %i, query: '%s': %s", default_zone_id, query.GetQuery(), query.GetError());
  3579. return;
  3580. }
  3581. if (query.GetAffectedRows() > 0) {
  3582. string zone_name = GetZoneName(1);
  3583. if(zone_name.length() > 0)
  3584. LogWrite(PLAYER__DEBUG, 0, "Player", "Setting New Character Starting Zone to '%s' due to no valid options!", zone_name.c_str());
  3585. else
  3586. LogWrite(PLAYER__DEBUG, 0, "Player", "Unable to set New Character Starting Zone due to no valid options!");
  3587. }
  3588. return;
  3589. }
  3590. void WorldDatabase::UpdateStartingItems(int32 char_id, int8 class_id, int8 race_id, bool base_class){
  3591. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default items for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3592. Query query;
  3593. Query query2;
  3594. vector<Item*> items;
  3595. vector<Item*> bags;
  3596. map<int32, int8> total_slots;
  3597. map<int32, int8> slots_left;
  3598. map<int8, bool> equip_slots;
  3599. map<Item*, StartingItem> item_list;
  3600. int32 item_id = 0;
  3601. Item* item = 0;
  3602. StartingItem* starting_item = 0;
  3603. //first get a list of the starting items for the character
  3604. MYSQL_RES* result = 0;
  3605. /*if(!base_class)
  3606. result = query2.RunQuery2(Q_SELECT, "SELECT `type`, item_id, creator, condition_, attuned, count FROM starting_items where (class_id=%i and race_id=%i) or (class_id=%i and race_id=255) or (class_id=255 and race_id=%i) or (class_id=255 and race_id=255) ORDER BY id", class_id, race_id, class_id, race_id);
  3607. else
  3608. result = query2.RunQuery2(Q_SELECT, "SELECT `type`, item_id, creator, condition_, attuned, count FROM starting_items where (class_id=%i and race_id=%i) or (class_id=%i and race_id=255) ORDER BY id", class_id, race_id, class_id);*/
  3609. result = query2.RunQuery2(Q_SELECT, "SELECT `type`, item_id, creator, condition_, attuned, count FROM starting_items WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255) ORDER BY id", classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3610. if(result && mysql_num_rows(result) > 0){
  3611. MYSQL_ROW row;
  3612. while(result && (row = mysql_fetch_row(result))){
  3613. item_id = atoul(row[1]);
  3614. item = master_item_list.GetItem(item_id);
  3615. if(item){
  3616. starting_item = &(item_list[item]);
  3617. starting_item->type = (row[0]) ? string(row[0]) : "";
  3618. starting_item->item_id = atoul(row[1]);
  3619. starting_item->creator = (row[2]) ? string(row[2]) : "";
  3620. starting_item->condition = atoi(row[3]);
  3621. starting_item->attuned = atoi(row[4]);
  3622. starting_item->count = atoi(row[5]);
  3623. item = master_item_list.GetItem(starting_item->item_id);
  3624. if(item){
  3625. if(bags.size() < NUM_INV_SLOTS && item->IsBag() && item->details.num_slots > 0)
  3626. bags.push_back(item);
  3627. else
  3628. items.push_back(item);
  3629. }
  3630. }
  3631. }
  3632. }
  3633. slots_left[0] = NUM_INV_SLOTS;
  3634. //next create the bags in the inventory
  3635. for(int8 i=0;i<bags.size();i++){
  3636. item = bags[i];
  3637. query.RunQuery2(Q_INSERT, "insert into character_items (char_id, type, slot, item_id, creator, condition_, attuned, bag_id, count) values(%u, '%s', %i, %u, '%s', %i, %i, %u, %i)",
  3638. char_id, item_list[item].type.c_str(), i, item_list[item].item_id, getSafeEscapeString(item_list[item].creator.c_str()).c_str(), item_list[item].condition, item_list[item].attuned, 0, item_list[item].count);
  3639. slots_left[query.GetLastInsertedID()] = item->details.num_slots;
  3640. total_slots[query.GetLastInsertedID()] = item->details.num_slots;
  3641. slots_left[0]--;
  3642. }
  3643. map<int32, int8>::iterator itr;
  3644. int32 inv_slot = 0;
  3645. int8 slot = 0;
  3646. //finally process the rest of the items, placing them in the first available slot
  3647. for(int32 x=0;x<items.size();x++){
  3648. item = items[x];
  3649. if(item_list[item].type.find("NOT") < 0xFFFFFFFF){ // NOT-EQUIPPED Items
  3650. for(itr = slots_left.begin(); itr != slots_left.end(); itr++){
  3651. if(itr->second > 0){
  3652. if(itr->first == 0 && slots_left.size() > 1) //we want items to go into bags first, then inventory after bags are full
  3653. continue;
  3654. inv_slot = itr->first;
  3655. slot = total_slots[itr->first] - itr->second;
  3656. itr->second--;
  3657. if(itr->second == 0)
  3658. slots_left.erase(itr);
  3659. break;
  3660. }
  3661. }
  3662. query.RunQuery2(Q_INSERT, "insert into character_items (char_id, type, slot, item_id, creator, condition_, attuned, bag_id, count) values(%u, '%s', %i, %u, '%s', %i, %i, %u, %i)",
  3663. char_id, item_list[item].type.c_str(), slot, item_list[item].item_id, getSafeEscapeString(item_list[item].creator.c_str()).c_str(), item_list[item].condition, item_list[item].attuned, inv_slot, item_list[item].count);
  3664. }
  3665. else{ //EQUIPPED Items
  3666. for(int8 i=0;i<item->slot_data.size();i++){
  3667. if(equip_slots.count(item->slot_data[i]) == 0){
  3668. equip_slots[item->slot_data[i]] = true;
  3669. query.RunQuery2(Q_INSERT, "insert into character_items (char_id, type, slot, item_id, creator, condition_, attuned, bag_id, count) values(%u, '%s', %i, %u, '%s', %i, %i, %u, %i)",
  3670. char_id, item_list[item].type.c_str(), item->slot_data[i], item_list[item].item_id, getSafeEscapeString(item_list[item].creator.c_str()).c_str(), item_list[item].condition, item_list[item].attuned, 0, item_list[item].count);
  3671. break;
  3672. }
  3673. }
  3674. }
  3675. }
  3676. }
  3677. void WorldDatabase::UpdateStartingSkills(int32 char_id, int8 class_id, int8 race_id)
  3678. {
  3679. Query query;
  3680. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default skills for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3681. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_skills (char_id, skill_id, current_val, max_val) SELECT %u, skill_id, current_val, max_val FROM starting_skills WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255)",
  3682. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3683. }
  3684. void WorldDatabase::UpdateStartingSpells(int32 char_id, int8 class_id, int8 race_id){
  3685. Query query;
  3686. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default spells for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3687. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_spells (char_id, spell_id, tier, knowledge_slot) SELECT %u, spell_id, tier, knowledge_slot FROM starting_spells WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255)",
  3688. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3689. }
  3690. void WorldDatabase::UpdateStartingSkillbar(int32 char_id, int8 class_id, int8 race_id){
  3691. Query query;
  3692. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default skillbar for race: %i, class: %i for char_id: %u", race_id, class_id, char_id);
  3693. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_skillbar (char_id, type, hotbar, spell_id, slot, text_val) SELECT %u, type, hotbar, spell_id, slot, text_val FROM starting_skillbar WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255)",
  3694. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id);
  3695. }
  3696. void WorldDatabase::UpdateStartingTitles(int32 char_id, int8 class_id, int8 race_id, int8 gender_id) {
  3697. Query query;
  3698. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding default titles for race: %i, class: %i, gender: %i for char_id: %u", race_id, class_id, gender_id, char_id);
  3699. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_titles (char_id, title_id) SELECT %u, title_id FROM starting_titles WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255) and gender_id IN (%i, 255)",
  3700. char_id, classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id, gender_id);
  3701. }
  3702. string WorldDatabase::GetZoneDescription(int32 id){
  3703. Query query;
  3704. string ret = "";
  3705. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT description FROM zones where id = %u", id);
  3706. if(result && mysql_num_rows(result) > 0){
  3707. MYSQL_ROW row;
  3708. row = mysql_fetch_row(result);
  3709. ret = string(row[0]);
  3710. }
  3711. return ret;
  3712. }
  3713. string WorldDatabase::GetZoneName(int32 id){
  3714. if (zone_names.count(id) > 0){
  3715. return zone_names[id];
  3716. }
  3717. Query query;
  3718. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `name` FROM zones where `id` = %u", id);
  3719. if(result && mysql_num_rows(result) > 0){
  3720. MYSQL_ROW row;
  3721. row = mysql_fetch_row(result);
  3722. zone_names[id] = row[0];
  3723. return zone_names[id];
  3724. }
  3725. return string("");
  3726. }
  3727. bool WorldDatabase::VerifyZone(const char* name){
  3728. Query query;
  3729. char* escaped = getEscapeString(name);
  3730. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT name FROM zones where name='%s'",escaped);
  3731. safe_delete_array(escaped);
  3732. if(result && mysql_num_rows(result) > 0)
  3733. return true;
  3734. else
  3735. return false;
  3736. }
  3737. int8 WorldDatabase::GetInstanceTypeByZoneID(int32 zoneID)
  3738. {
  3739. std::map<int32, int8>::iterator itr = zone_instance_types.find(zoneID);
  3740. if(itr != zone_instance_types.end())
  3741. return itr->second;
  3742. DatabaseResult result;
  3743. int8 ret = 0;
  3744. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Getting instances for zone_id %u", zoneID);
  3745. if( !database_new.Select(&result, "SELECT instance_type+0 FROM zones WHERE id = %u", zoneID) )
  3746. {
  3747. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in GetInstanceTypeByZoneID() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  3748. return ret;
  3749. }
  3750. if( result.GetNumRows() > 0 )
  3751. {
  3752. result.Next();
  3753. ret = (result.GetInt8Str("instance_type+0") == 0) ? 0 : result.GetInt8Str("instance_type+0") - 1;
  3754. zone_instance_types.insert(make_pair(zoneID, ret));
  3755. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Found instance type %i for zone_id %u", ret, zoneID);
  3756. }
  3757. else
  3758. LogWrite(INSTANCE__DEBUG, 0, "Instance", "No instances found for zone_id %u", zoneID);
  3759. return ret;
  3760. }
  3761. void WorldDatabase::Save(Client* client){
  3762. Query query;
  3763. Player* player = client->GetPlayer();
  3764. if(!player->CheckPlayerInfo())
  3765. return;
  3766. int32 instance_id = 0;
  3767. if ( client->GetCurrentZone ( ) != NULL )
  3768. instance_id = client->GetCurrentZone()->GetInstanceID();
  3769. int32 zone_id = 0;
  3770. if(client->GetCurrentZone())
  3771. zone_id = client->GetCurrentZone()->GetZoneID();
  3772. query.AddQueryAsync(client->GetCharacterID(), this, Q_UPDATE, "update characters set current_zone_id=%u, x=%f, y=%f, z=%f, heading=%f, level=%i,instance_id=%i,last_saved=%i, `class`=%i, `tradeskill_level`=%i, `tradeskill_class`=%i, `group_id`=%u, deity = %u, alignment = %u where id = %u", zone_id, player->GetX(), player->GetY(), player->GetZ(), player->GetHeading(), player->GetLevel(), instance_id, client->GetLastSavedTimeStamp(), client->GetPlayer()->GetAdventureClass(), client->GetPlayer()->GetTSLevel(), client->GetPlayer()->GetTradeskillClass(), client->GetPlayer()->GetGroupMemberInfo() ? client->GetPlayer()->GetGroupMemberInfo()->group_id : client->GetRejoinGroupID(), client->GetPlayer()->GetDeity(), client->GetPlayer()->GetInfoStruct()->get_alignment(), client->GetCharacterID());
  3773. query.AddQueryAsync(client->GetCharacterID(), this, Q_UPDATE, "update character_details set hp=%u, power=%u, str=%i, sta=%i, agi=%i, wis=%i, intel=%i, heat=%i, cold=%i, magic=%i, mental=%i, divine=%i, disease=%i, poison=%i, coin_copper=%u, coin_silver=%u, coin_gold=%u, coin_plat=%u, max_hp = %u, max_power=%u, xp = %u, xp_needed = %u, xp_debt = %f, xp_vitality = %f, tradeskill_xp = %u, tradeskill_xp_needed = %u, tradeskill_xp_vitality = %f, bank_copper = %u, bank_silver = %u, bank_gold = %u, bank_plat = %u, status_points = %u, bind_zone_id=%u, bind_x = %f, bind_y = %f, bind_z = %f, bind_heading = %f, house_zone_id=%u, combat_voice = %i, emote_voice = %i, biography='%s', flags=%u, flags2=%u, last_name='%s', assigned_aa = %i, unassigned_aa = %i, tradeskill_aa = %i, unassigned_tradeskill_aa = %i, prestige_aa = %i, unassigned_prestige_aa = %i, tradeskill_prestige_aa = %i, unassigned_tradeskill_prestige_aa = %i, pet_name = '%s' where char_id = %u",
  3774. player->GetHP(), player->GetPower(), player->GetStrBase(), player->GetStaBase(), player->GetAgiBase(), player->GetWisBase(), player->GetIntBase(), player->GetHeatResistanceBase(), player->GetColdResistanceBase(), player->GetMagicResistanceBase(),
  3775. player->GetMentalResistanceBase(), player->GetDivineResistanceBase(), player->GetDiseaseResistanceBase(), player->GetPoisonResistanceBase(), player->GetCoinsCopper(), player->GetCoinsSilver(), player->GetCoinsGold(), player->GetCoinsPlat(), player->GetTotalHPBase(), player->GetTotalPowerBase(), player->GetXP(), player->GetNeededXP(), player->GetXPDebt(), player->GetXPVitality(), player->GetTSXP(), player->GetNeededTSXP(), player->GetTSXPVitality(), player->GetBankCoinsCopper(),
  3776. player->GetBankCoinsSilver(), player->GetBankCoinsGold(), player->GetBankCoinsPlat(), player->GetStatusPoints(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneID(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneX(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneY(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneZ(), client->GetPlayer()->GetPlayerInfo()->GetBindZoneHeading(), client->GetPlayer()->GetPlayerInfo()->GetHouseZoneID(),
  3777. client->GetPlayer()->GetCombatVoice(), client->GetPlayer()->GetEmoteVoice(), getSafeEscapeString(client->GetPlayer()->GetBiography().c_str()).c_str(), player->GetFlags(), player->GetFlags2(), client->GetPlayer()->GetLastName(),
  3778. client->GetPlayer()->GetAssignedAA(), client->GetPlayer()->GetUnassignedAA(), client->GetPlayer()->GetTradeskillAA(), client->GetPlayer()->GetUnassignedTradeskillAA(), client->GetPlayer()->GetPrestigeAA(),
  3779. client->GetPlayer()->GetUnassignedPretigeAA(), client->GetPlayer()->GetTradeskillPrestigeAA(), client->GetPlayer()->GetUnassignedTradeskillPrestigeAA(), client->GetPlayer()->GetInfoStruct()->get_pet_name().c_str(), client->GetCharacterID());
  3780. map<string, int8>::iterator itr;
  3781. map<string, int8>* friends = player->GetFriends();
  3782. if(friends && friends->size() > 0){
  3783. for(itr = friends->begin(); itr != friends->end(); itr++){
  3784. if(itr->second == 1){
  3785. query.AddQueryAsync(client->GetCharacterID(), this, Q_INSERT, "insert ignore into character_social (char_id, name, type) values(%u, '%s', 'FRIEND')", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3786. itr->second = 0;
  3787. }
  3788. else if(itr->second == 2){
  3789. query.AddQueryAsync(client->GetCharacterID(), this, Q_DELETE, "delete FROM character_social where char_id = %u and name = '%s'", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3790. itr->second = 3;
  3791. }
  3792. }
  3793. }
  3794. map<string, int8>* ignored = player->GetIgnoredPlayers();
  3795. if(ignored && ignored->size() > 0){
  3796. for(itr = ignored->begin(); itr != ignored->end(); itr++){
  3797. if(itr->second == 1){
  3798. query.AddQueryAsync(client->GetCharacterID(), this, Q_INSERT, "insert ignore into character_social (char_id, name, type) values(%u, '%s', 'IGNORE')", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3799. itr->second = 0;
  3800. }
  3801. else if(itr->second == 2){
  3802. query.AddQueryAsync(client->GetCharacterID(), this, Q_DELETE, "delete FROM character_social where char_id = %u and name = '%s'", client->GetCharacterID(), getSafeEscapeString(itr->first.c_str()).c_str());
  3803. itr->second = 3;
  3804. }
  3805. }
  3806. }
  3807. SavePlayerFactions(client);
  3808. SaveCharacterQuests(client);
  3809. SaveCharacterSkills(client);
  3810. SavePlayerSpells(client);
  3811. SavePlayerMail(client);
  3812. SavePlayerCollections(client);
  3813. client->SaveQuestRewardData();
  3814. LogWrite(PLAYER__INFO, 3, "Player", "Player '%s' (%u) data saved.", player->GetName(), player->GetCharacterID());
  3815. }
  3816. void WorldDatabase::LoadEntityCommands(ZoneServer* zone) {
  3817. int32 total = 0;
  3818. int32 id = 0;
  3819. EntityCommand* command = 0;
  3820. DatabaseResult result;
  3821. if (!database_new.Select(&result, "SELECT `command_list_id`, `command_text`, `distance`, `command`, `error_text`, `cast_time`, `spell_visual` FROM `entity_commands` ORDER BY `id`")) {
  3822. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  3823. return;
  3824. }
  3825. while (result.Next()) {
  3826. command = new EntityCommand;
  3827. id = result.GetInt32(0);
  3828. command->name = result.GetString(1);
  3829. command->distance = result.GetFloat(2);
  3830. command->command = result.GetString(3);
  3831. command->error_text = result.GetString(4);
  3832. command->cast_time = result.GetInt16(5);
  3833. command->spell_visual = result.GetInt32(6);
  3834. command->default_allow_list = true;
  3835. zone->SetEntityCommandList(id, command);
  3836. LogWrite(COMMAND__DEBUG, 5, "Command", "---Loading Command: '%s' (%s)", command->name.c_str(), command->command.c_str());
  3837. total++;
  3838. }
  3839. LogWrite(COMMAND__DEBUG, 0, "Command", "--Loaded %i entity command(s)", total);
  3840. }
  3841. void WorldDatabase::LoadFactionAlliances()
  3842. {
  3843. LogWrite(FACTION__DEBUG, 1, "Faction", "-Loading faction alliances...");
  3844. int32 total = 0;
  3845. int32 fTotal = 0;
  3846. int32 hTotal = 0;
  3847. Query query;
  3848. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT faction_id, friend_faction, hostile_faction FROM faction_alliances");
  3849. if(result && mysql_num_rows(result) > 0)
  3850. {
  3851. MYSQL_ROW row;
  3852. int32 faction_id = 0;
  3853. int32 friendly_id = 0;
  3854. int32 hostile_id = 0;
  3855. while(result && (row = mysql_fetch_row(result)))
  3856. {
  3857. faction_id = atoul(row[0]);
  3858. friendly_id = atoul(row[1]);
  3859. hostile_id = atoul(row[2]);
  3860. if(friendly_id > 0)
  3861. {
  3862. master_faction_list.AddFriendlyFaction(faction_id, friendly_id);
  3863. fTotal++;
  3864. LogWrite(FACTION__DEBUG, 5, "Faction", "---Faction %i is friendly towards %i", faction_id, friendly_id);
  3865. }
  3866. if(hostile_id > 0)
  3867. {
  3868. master_faction_list.AddHostileFaction(faction_id, hostile_id);
  3869. hTotal++;
  3870. LogWrite(FACTION__DEBUG, 5, "Faction", "---Faction %i is hostile towards %i", faction_id, hostile_id);
  3871. }
  3872. total++;
  3873. }
  3874. }
  3875. LogWrite(FACTION__DEBUG, 3, "Faction", "--Loaded %u Alliances: %i friendly, %i hostile", total, fTotal, hTotal);
  3876. }
  3877. bool WorldDatabase::UpdateSpawnScriptData(int32 spawn_id, int32 spawn_location_id, int32 spawnentry_id, const char* name){
  3878. bool ret = false;
  3879. if((spawn_id > 0 || spawn_location_id > 0 || spawnentry_id > 0) && name){
  3880. Query query, query2;
  3881. int32 row_id = 0;
  3882. if(spawn_id > 0){
  3883. query.RunQuery2(Q_DELETE, "DELETE FROM spawn_scripts where spawn_id=%u", spawn_id);
  3884. query2.RunQuery2(Q_INSERT, "INSERT into spawn_scripts (spawn_id, lua_script) values(%u, '%s')", spawn_id, getSafeEscapeString(name).c_str());
  3885. if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF)
  3886. LogWrite(LUA__ERROR, 0, "LUA", "Error in UpdateSpawnScriptData, Query: %s, Error: %s", query2.GetQuery(), query2.GetError());
  3887. else{
  3888. row_id = query2.GetLastInsertedID();
  3889. if(row_id > 0)
  3890. world.AddSpawnScript(row_id, name);
  3891. ret = true;
  3892. }
  3893. }
  3894. else if(spawn_location_id > 0){
  3895. query.RunQuery2(Q_DELETE, "DELETE FROM spawn_scripts where spawn_location_id=%u", spawn_location_id);
  3896. query2.RunQuery2(Q_INSERT, "INSERT into spawn_scripts (spawn_location_id, lua_script) values(%u, '%s')", spawn_location_id, getSafeEscapeString(name).c_str());
  3897. if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF)
  3898. LogWrite(LUA__ERROR, 0, "LUA", "Error in UpdateSpawnScriptData, Query: %s, Error: %s", query2.GetQuery(), query2.GetError());
  3899. else{
  3900. row_id = query2.GetLastInsertedID();
  3901. if(row_id > 0)
  3902. world.AddSpawnLocationScript(row_id, name);
  3903. ret = true;
  3904. }
  3905. }
  3906. else if(spawnentry_id > 0){
  3907. query.RunQuery2(Q_DELETE, "DELETE FROM spawn_scripts where spawnentry_id=%u", spawnentry_id);
  3908. query2.RunQuery2(Q_INSERT, "INSERT into spawn_scripts (spawnentry_id, lua_script) values(%u, '%s')", spawnentry_id, getSafeEscapeString(name).c_str());
  3909. if(query2.GetErrorNumber() && query2.GetError() && query2.GetErrorNumber() < 0xFFFFFFFF)
  3910. LogWrite(LUA__ERROR, 0, "LUA", "Error in UpdateSpawnScriptData, Query: %s, Error: %s", query2.GetQuery(), query2.GetError());
  3911. else{
  3912. row_id = query2.GetLastInsertedID();
  3913. if(row_id > 0)
  3914. world.AddSpawnEntryScript(row_id, name);
  3915. ret = true;
  3916. }
  3917. }
  3918. }
  3919. return ret;
  3920. }
  3921. void WorldDatabase::LoadSpawnScriptData() {
  3922. int32 total = 0;
  3923. Query query;
  3924. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spawn_id, spawnentry_id, spawn_location_id, lua_script FROM spawn_scripts");
  3925. if(result && mysql_num_rows(result) > 0)
  3926. {
  3927. MYSQL_ROW row;
  3928. int32 spawn_id = 0;
  3929. int32 spawnentry_id = 0;
  3930. int32 spawn_location_id = 0;
  3931. while(result && (row = mysql_fetch_row(result)))
  3932. {
  3933. spawn_id = atoul(row[0]);
  3934. spawnentry_id = atoul(row[1]);
  3935. spawn_location_id = atoul(row[2]);
  3936. string spawn_script = string(row[3]);
  3937. if(spawnentry_id > 0)
  3938. {
  3939. world.AddSpawnEntryScript(spawnentry_id, row[3]);
  3940. total++;
  3941. }
  3942. else if(spawn_location_id > 0)
  3943. {
  3944. world.AddSpawnLocationScript(spawn_location_id, row[3]);
  3945. total++;
  3946. }
  3947. else if(spawn_id > 0)
  3948. {
  3949. world.AddSpawnScript(spawn_id, row[3]);
  3950. total++;
  3951. }
  3952. else {
  3953. if(row[3])
  3954. LogWrite(LUA__ERROR, 0, "LUA", "Invalid Entry in spawn_scripts table for lua_script '%s' (spawn_id, spawnentry_id and spawn_location_id are all 0)", row[3]);
  3955. else
  3956. LogWrite(LUA__ERROR, 0, "LUA", "Invalid Entry in spawn_scripts table.");
  3957. }
  3958. spawn_id = 0;
  3959. spawnentry_id = 0;
  3960. spawn_location_id = 0;
  3961. }
  3962. }
  3963. LogWrite(LUA__DEBUG, 0, "LUA", "\tLoaded %u SpawnScript%s", total, total == 1 ? "" : "s");
  3964. }
  3965. void WorldDatabase::LoadZoneScriptData() {
  3966. Query query;
  3967. int32 total = 0;
  3968. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `lua_script` FROM `zones`");
  3969. if (result && mysql_num_rows(result) > 0) {
  3970. MYSQL_ROW row;
  3971. while(result && (row = mysql_fetch_row(result))) {
  3972. if (row[1]) {
  3973. int32 zone_id = atoul(row[0]);
  3974. string zone_script = string(row[1]);
  3975. if (zone_id > 0 && zone_script.length() > 0) {
  3976. LogWrite(LUA__DEBUG, 5, "LUA", "ZoneScript: %s loaded.", zone_script.c_str());
  3977. world.AddZoneScript(zone_id, zone_script.c_str());
  3978. total++;
  3979. }
  3980. else if (zone_id > 0) {
  3981. string tmpScript;
  3982. tmpScript.append("ZoneScripts/");
  3983. string zonename = GetZoneName(zone_id);
  3984. tmpScript.append(zonename);
  3985. tmpScript.append(".lua");
  3986. struct stat buffer;
  3987. bool fileExists = (stat(tmpScript.c_str(), &buffer) == 0);
  3988. if (fileExists)
  3989. {
  3990. LogWrite(LUA__INFO, 0, "LUA", "No zonescript file described in the database, overriding with ZoneScript %s for Zone ID %u", (char*)tmpScript.c_str(), zone_id);
  3991. world.AddZoneScript(zone_id, tmpScript.c_str());
  3992. }
  3993. }
  3994. }
  3995. }
  3996. }
  3997. LogWrite(LUA__DEBUG, 0, "LUA", "\tLoaded %u ZoneScript%s", total, total == 1 ? "" : "s");
  3998. }
  3999. int32 WorldDatabase::LoadSpellScriptData() {
  4000. Query query;
  4001. MYSQL_ROW row;
  4002. int32 count;
  4003. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `lua_script` FROM `spells`"); // WHERE is_active = 1
  4004. while (result && (row = mysql_fetch_row(result))) {
  4005. if (row[0] && strlen(row[0]) > 0) {
  4006. if (lua_interface->LoadLuaSpell(row[0]))
  4007. LogWrite(SPELL__DEBUG, 5, "Spells", "SpellScript: %s loaded.", row[0]);
  4008. }
  4009. }
  4010. count = mysql_num_rows(result);
  4011. LogWrite(SPELL__DEBUG, 0, "Spells", "\tLoaded %i SpellScript%s", count, count == 1 ? "" : "s");
  4012. return count;
  4013. }
  4014. void WorldDatabase::LoadFactionList() {
  4015. int32 total = 0;
  4016. Query query;
  4017. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name, type, description, default_level, negative_change, positive_change FROM factions");
  4018. if(result && mysql_num_rows(result) > 0) {
  4019. MYSQL_ROW row;
  4020. Faction* faction = 0;
  4021. while(result && (row = mysql_fetch_row(result))){
  4022. faction = new Faction;
  4023. faction->id = atoul(row[0]);
  4024. faction->name = string(row[1]);
  4025. faction->type = string(row[2]);
  4026. faction->description = string(row[3]);
  4027. faction->default_value = atoi(row[4]);
  4028. faction->negative_change = atoi(row[5]);
  4029. faction->positive_change = atoi(row[6]);
  4030. master_faction_list.AddFaction(faction);
  4031. total++;
  4032. LogWrite(FACTION__DEBUG, 5, "Faction", "---Loading Faction '%s' (%u)", faction->name.c_str(), faction->id);
  4033. }
  4034. }
  4035. LogWrite(FACTION__DEBUG, 3, "Faction", "--Loaded %u Faction%s", total, total == 1 ? "" : "s");
  4036. LoadFactionAlliances();
  4037. }
  4038. void WorldDatabase::SavePlayerFactions(Client* client){
  4039. LogWrite(PLAYER__DEBUG, 3, "Player", "Saving Player Factions...");
  4040. Query query;
  4041. map<int32, sint32>* factions = client->GetPlayer()->GetFactions()->GetFactionValues();
  4042. map<int32, sint32>::iterator itr;
  4043. for(itr = factions->begin(); itr != factions->end(); itr++)
  4044. query.AddQueryAsync(client->GetCharacterID(), this,Q_INSERT, "insert into character_factions (char_id, faction_id, faction_level) values(%u, %u, %i) ON DUPLICATE KEY UPDATE faction_level=%i", client->GetCharacterID(), itr->first, itr->second, itr->second);
  4045. }
  4046. bool WorldDatabase::LoadPlayerFactions(Client* client) {
  4047. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading Player Factions...");
  4048. Query query;
  4049. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT faction_id, faction_level FROM character_factions where char_id=%i", client->GetCharacterID());
  4050. if(result && mysql_num_rows(result) > 0) {
  4051. MYSQL_ROW row;
  4052. while(result && (row = mysql_fetch_row(result))){
  4053. client->GetPlayer()->GetFactions()->SetFactionValue(atoul(row[0]), atol(row[1]));
  4054. }
  4055. }
  4056. if(query.GetErrorNumber())
  4057. return false;
  4058. return true;
  4059. }
  4060. void WorldDatabase::SavePlayerMail(Mail* mail) {
  4061. Query query_update;
  4062. Query query_insert;
  4063. if (mail) {
  4064. if(mail->mail_id > 0)
  4065. query_update.RunQuery2(Q_UPDATE, "UPDATE `character_mail` SET `already_read`=%u, `coin_copper`=%u, `coin_silver`=%u, `coin_gold`=%u, `coin_plat`=%u, `char_item_id`=%u, `stack`=%u WHERE `id`=%u", mail->already_read, mail->coin_copper, mail->coin_silver, mail->coin_gold, mail->coin_plat, mail->char_item_id, mail->stack, mail->mail_id);
  4066. else if (mail->mail_id == 0 || query_update.GetAffectedRows() == 0)
  4067. {
  4068. query_insert.RunQuery2(Q_INSERT, "INSERT INTO `character_mail` (`player_to_id`, `player_from`, `subject`, `mail_body`, `already_read`, `mail_type`, `coin_copper`, `coin_silver`, `coin_gold`, `coin_plat`, `stack`, `postage_cost`, `attachment_cost`, `char_item_id`, `time_sent`, `expire_time`) VALUES (%u, '%s', '%s', '%s', %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u)", mail->player_to_id, mail->player_from.c_str(), getSafeEscapeString(mail->subject.c_str()).c_str(), getSafeEscapeString(mail->mail_body.c_str()).c_str(), mail->already_read, mail->mail_type, mail->coin_copper, mail->coin_silver, mail->coin_gold, mail->coin_plat, mail->stack, mail->postage_cost, mail->attachment_cost, mail->char_item_id, mail->time_sent, mail->expire_time);
  4069. if(!mail->mail_id)
  4070. mail->mail_id = query_insert.GetLastInsertedID();
  4071. }
  4072. mail->save_needed = false;
  4073. }
  4074. }
  4075. void WorldDatabase::SavePlayerMail(Client* client) {
  4076. if (client) {
  4077. MutexMap<int32, Mail*>* mail_list = client->GetPlayer()->GetMail();
  4078. MutexMap<int32, Mail*>::iterator itr = mail_list->begin();
  4079. while (itr.Next()) {
  4080. Mail* mail = itr->second;
  4081. if (mail->save_needed)
  4082. SavePlayerMail(mail);
  4083. }
  4084. }
  4085. }
  4086. void WorldDatabase::LoadPlayerMail(Client* client, bool new_only) {
  4087. LogWrite(PLAYER__DEBUG, 0, "Player", "Loading Player Mail...");
  4088. if (client) {
  4089. Query query;
  4090. MYSQL_RES* result;
  4091. if (new_only)
  4092. result = query.RunQuery2(Q_SELECT, "SELECT `id`, `player_to_id`, `player_from`, `subject`, `mail_body`, `already_read`, `mail_type`, `coin_copper`, `coin_silver`, `coin_gold`, `coin_plat`, `stack`, `postage_cost`, `attachment_cost`, `char_item_id`, `time_sent`, `expire_time` FROM `character_mail` WHERE `player_to_id`=%u AND `unread`=1", client->GetCharacterID());
  4093. else
  4094. result = query.RunQuery2(Q_SELECT, "SELECT `id`, `player_to_id`, `player_from`, `subject`, `mail_body`, `already_read`, `mail_type`, `coin_copper`, `coin_silver`, `coin_gold`, `coin_plat`, `stack`, `postage_cost`, `attachment_cost`, `char_item_id`, `time_sent`, `expire_time` FROM `character_mail` WHERE `player_to_id`=%u", client->GetCharacterID());
  4095. if (result && mysql_num_rows(result) > 0) {
  4096. MYSQL_ROW row;
  4097. bool hasMail = false;
  4098. while (result && (row = mysql_fetch_row(result))) {
  4099. int32 time_sent = atoul(row[15]);
  4100. if ( time_sent > Timer::GetUnixTimeStamp() )
  4101. continue; // should have not been received yet
  4102. hasMail = true;
  4103. Mail* mail = new Mail;
  4104. mail->mail_id = atoul(row[0]);
  4105. mail->player_to_id = atoul(row[1]);
  4106. mail->player_from = string(row[2]);
  4107. mail->subject = string(row[3]);
  4108. if (row[4])
  4109. mail->mail_body = string(row[4]);
  4110. mail->already_read = atoi(row[5]);
  4111. mail->mail_type = atoi(row[6]);
  4112. mail->coin_copper = atoul(row[7]);
  4113. mail->coin_silver = atoul(row[8]);
  4114. mail->coin_gold = atoul(row[9]);
  4115. mail->coin_plat = atoul(row[10]);
  4116. mail->stack = atoi(row[11]);
  4117. mail->postage_cost = atoul(row[12]);
  4118. mail->attachment_cost = atoul(row[13]);
  4119. mail->char_item_id = atoul(row[14]);
  4120. mail->time_sent = time_sent;
  4121. mail->expire_time = atoul(row[16]);
  4122. mail->save_needed = false;
  4123. client->GetPlayer()->AddMail(mail);
  4124. LogWrite(PLAYER__DEBUG, 5, "Player", "Loaded Mail ID %i, to: %i, from: %s", atoul(row[0]), atoul(row[1]), string(row[2]).c_str());
  4125. }
  4126. if(hasMail)
  4127. client->SimpleMessage(CHANNEL_NARRATIVE, "You've got mail! :)");
  4128. }
  4129. }
  4130. }
  4131. void WorldDatabase::DeletePlayerMail(Mail* mail) {
  4132. Query query;
  4133. if (mail)
  4134. query.RunQuery2(Q_DELETE, "DELETE FROM `character_mail` WHERE `id`=%u", mail->mail_id);
  4135. LogWrite(PLAYER__DEBUG, 0, "Player", "Delete Player Mail...");
  4136. }
  4137. vector<int32>* WorldDatabase::GetAllPlayerIDs() {
  4138. Query query;
  4139. vector<int32>* ids = 0;
  4140. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id` FROM `characters`");
  4141. MYSQL_ROW row;
  4142. while (result && (row = mysql_fetch_row(result))) {
  4143. if (ids == 0)
  4144. ids = new vector<int32>;
  4145. ids->push_back(atoul(row[0]));
  4146. }
  4147. return ids;
  4148. }
  4149. void WorldDatabase::GetPetNames(ZoneServer* zone)
  4150. {
  4151. DatabaseResult result;
  4152. int32 total = 0;
  4153. if( database_new.Select(&result, "SELECT pet_name FROM spawn_pet_names") )
  4154. {
  4155. while(result.Next())
  4156. {
  4157. zone->pet_names.push_back(result.GetStringStr("pet_name"));
  4158. total++;
  4159. LogWrite(PET__DEBUG, 5, "Pet", "---Loading Pet Name: '%s'", result.GetStringStr("pet_name"));
  4160. }
  4161. LogWrite(PET__DEBUG, 0, "Pet", "--Loaded %u Pet Names", total);
  4162. }
  4163. }
  4164. int32 WorldDatabase::GetMaxHotBarID(){
  4165. Query query;
  4166. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT max(id) FROM character_skillbar");
  4167. int32 ret = 0;
  4168. if(result && mysql_num_rows(result) > 0) {
  4169. MYSQL_ROW row;
  4170. row = mysql_fetch_row(result);
  4171. if(row && row[0])
  4172. ret = strtoul(row[0], NULL, 0);
  4173. }
  4174. return ret;
  4175. }
  4176. void WorldDatabase::SaveQuickBar(int32 char_id, vector<QuickBarItem*>* quickbar_items){
  4177. vector<QuickBarItem*>::iterator itr;
  4178. QuickBarItem* qbi = 0;
  4179. for(itr = quickbar_items->begin(); itr != quickbar_items->end(); itr++){
  4180. qbi = *itr;
  4181. if(!qbi)
  4182. continue;
  4183. if(qbi->deleted == false){
  4184. Query query;
  4185. if(qbi->text.size > 0){
  4186. query.AddQueryAsync(char_id, this, Q_REPLACE, "replace into character_skillbar (id, hotbar, slot, char_id, spell_id, type, text_val, tier) values(%u, %u, %u, %u, %u, %i, '%s', %i)",
  4187. qbi->unique_id, qbi->hotbar, qbi->slot, char_id, qbi->id, qbi->type, getSafeEscapeString(qbi->text.data.c_str()).c_str(), qbi->tier);
  4188. }
  4189. else{
  4190. query.AddQueryAsync(char_id, this, Q_REPLACE, "replace into character_skillbar (id, hotbar, slot, char_id, spell_id, type, text_val, tier) values(%u, %u, %u, %u, %u, %i, 'Unused', %i)",
  4191. qbi->unique_id, qbi->hotbar, qbi->slot, char_id, qbi->id, qbi->type, qbi->tier);
  4192. }
  4193. }
  4194. else{
  4195. Query query;
  4196. query.AddQueryAsync(char_id, this, Q_DELETE, "delete FROM character_skillbar where hotbar=%u and slot=%u and char_id=%u", qbi->hotbar, qbi->slot, char_id);
  4197. }
  4198. }
  4199. }
  4200. map<int32, vector<LevelArray*> >* WorldDatabase::LoadSpellClasses(){
  4201. map<int32, vector<LevelArray*> >* ret = new map<int32, vector<LevelArray*> >();
  4202. Query query;
  4203. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT spell_id, adventure_class_id, tradeskill_class_id, level FROM spell_classes");
  4204. MYSQL_ROW row;
  4205. LevelArray* level = 0;
  4206. while(result && (row = mysql_fetch_row(result))){
  4207. level = new LevelArray();
  4208. level->adventure_class = atoi(row[1]);
  4209. level->tradeskill_class = atoi(row[2]);
  4210. level->spell_level = atoi(row[3]);
  4211. (*ret)[atoul(row[0])].push_back(level);
  4212. }
  4213. return ret;
  4214. }
  4215. void WorldDatabase::LoadTraits(){
  4216. Query query;
  4217. MYSQL_ROW row;
  4218. TraitData* trait;
  4219. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `spell_id`, `level`, `class_req`, `race_req`, `isTrait`,`isInate`, `isFocusEffect`, `isTraining`,`tier`, `group`, `item_id` FROM spell_traits where is_active = 1");
  4220. while (result && (row = mysql_fetch_row(result))){
  4221. trait = new TraitData;
  4222. int8 i = 0;
  4223. trait->spellID = strtoul(row[0], NULL, 0);
  4224. trait->level = atoi(row[(++i)]);
  4225. trait->classReq = atoi(row[(++i)]);
  4226. trait->raceReq = atoi(row[(++i)]);
  4227. trait->isTrait = (atoi(row[(++i)]) == 0) ? false : true;
  4228. trait->isInate = (atoi(row[(++i)]) == 0) ? false : true;
  4229. trait->isFocusEffect = (atoi(row[(++i)]) == 0) ? false : true;
  4230. trait->isTraining = (atoi(row[(++i)]) == 0) ? false : true;
  4231. trait->tier = atoi(row[(++i)]);
  4232. trait->group = atoi(row[(++i)]);
  4233. trait->item_id = atoul(row[(++i)]);
  4234. master_trait_list.AddTrait(trait);
  4235. }
  4236. LogWrite(SPELL__INFO, 0, "Traits", "Loaded %u Trait(s)", master_trait_list.Size());
  4237. }
  4238. void WorldDatabase::LoadSpells()
  4239. {
  4240. DatabaseResult result;
  4241. Spell *spell;
  4242. SpellData *data;
  4243. int32 t_now = Timer::GetUnixTimeStamp();
  4244. int32 total = 0;
  4245. map<int32, vector<LevelArray*> >* level_data = LoadSpellClasses();
  4246. if( !database_new.Select(&result, "SELECT s.`id`, ts.spell_id, ts.index, `name`, `description`, `type`, `class_skill`, `min_class_skill_req`, `mastery_skill`, `tier`, `is_aa`,`hp_req`, `power_req`,`power_by_level`, `cast_time`, `recast`, `radius`, `max_aoe_targets`, `req_concentration`, `range`, `duration1`, `duration2`, `resistibility`, `hp_upkeep`, `power_upkeep`, `duration_until_cancel`, `target_type`, `recovery`, `power_req_percent`, `hp_req_percent`, `icon`, `icon_heroic_op`, `icon_backdrop`, `success_message`, `fade_message`, `fade_message_others`, `cast_type`, `lua_script`, `call_frequency`, `interruptable`, `spell_visual`, `effect_message`, `min_range`, `can_effect_raid`, `affect_only_group_members`, `hit_bonus`, `display_spell_tier`, `friendly_spell`, `group_spell`, `spell_book_type`, spell_type+0, s.is_active, savagery_req, savagery_req_percent, savagery_upkeep, dissonance_req, dissonance_req_percent, dissonance_upkeep, linked_timer_id, det_type, incurable, control_effect_type, cast_while_moving, casting_flags, persist_through_death, not_maintained, savage_bar, savage_bar_slot, soe_spell_crc, 0xffffffff-CRC32(s.`name`) as 'spell_name_crc', type_group_spell_id, can_fizzle, given_by "
  4247. "FROM (spells s, spell_tiers st) "
  4248. "LEFT JOIN spell_ts_ability_index ts "
  4249. "ON s.`id` = ts.spell_id "
  4250. "WHERE s.id = st.spell_id AND s.is_active = 1 "
  4251. "ORDER BY s.`id`, `tier`") )
  4252. {
  4253. // error
  4254. }
  4255. else
  4256. {
  4257. while( result.Next() )
  4258. {
  4259. data = new SpellData;
  4260. int32 spell_id = result.GetInt32Str("id");
  4261. string spell_name = result.GetStringStr("name");
  4262. /* General Spell info */
  4263. data->id = spell_id;
  4264. data->inherited_spell_id = spell_id;
  4265. data->soe_spell_crc = result.GetInt32Str("soe_spell_crc");
  4266. data->tier = result.GetInt8Str("tier");
  4267. data->ts_loc_index = result.GetInt8Str("index");
  4268. data->name.data = spell_name.c_str();
  4269. data->name.size = data->name.data.length();
  4270. data->description.data = result.GetStringStr("description");
  4271. data->description.size = data->description.data.length();
  4272. data->icon = result.GetSInt16Str("icon");
  4273. data->icon_heroic_op = result.GetInt16Str("icon_heroic_op");
  4274. data->icon_backdrop = result.GetInt16Str("icon_backdrop");
  4275. data->spell_visual = result.GetInt32Str("spell_visual");
  4276. data->type = result.GetInt16Str("type");
  4277. data->target_type = result.GetInt8Str("target_type");
  4278. data->cast_type = result.GetInt8Str("cast_type");
  4279. data->spell_book_type = result.GetInt32Str("spell_book_type");
  4280. data->det_type = result.GetInt8Str("det_type");
  4281. data->incurable = (result.GetInt8Str("incurable") == 1);
  4282. data->control_effect_type = result.GetInt8Str("control_effect_type");
  4283. data->casting_flags = result.GetInt32Str("casting_flags");
  4284. data->savage_bar = result.GetInt8Str("savage_bar");
  4285. data->savage_bar_slot = result.GetInt8Str("savage_bar_slot");
  4286. data->spell_type = result.IsNullStr("spell_type+0") ? 0 : result.GetInt8Str("spell_type+0");
  4287. /* Toggles */
  4288. data->interruptable = ( result.GetInt8Str("interruptable") == 1);
  4289. data->duration_until_cancel = ( result.GetInt8Str("duration_until_cancel") == 1);
  4290. data->can_effect_raid = result.GetInt8Str("can_effect_raid");
  4291. data->affect_only_group_members = result.GetInt8Str("affect_only_group_members");
  4292. data->display_spell_tier = result.GetInt8Str("display_spell_tier");
  4293. data->friendly_spell = result.GetInt8Str("friendly_spell");
  4294. data->group_spell = result.GetInt8Str("group_spell");
  4295. data->is_active = result.GetInt8Str("is_active");
  4296. data->persist_through_death = ( result.GetInt8Str("persist_through_death") == 1);
  4297. data->cast_while_moving = ( result.GetInt8Str("cast_while_moving") == 1);
  4298. data->not_maintained = ( result.GetInt8Str("not_maintained") == 1);
  4299. data->is_aa = (result.GetInt8Str("is_aa") == 1);
  4300. /* Skill Requirements */
  4301. data->class_skill = result.GetInt32Str("class_skill");
  4302. data->min_class_skill_req = result.GetInt16Str("min_class_skill_req");
  4303. data->mastery_skill = result.GetInt32Str("mastery_skill");
  4304. /* Cost */
  4305. data->req_concentration = result.GetInt16Str("req_concentration");
  4306. data->hp_req = result.GetInt16Str("hp_req");
  4307. data->hp_upkeep = result.GetInt16Str("hp_upkeep");
  4308. data->hp_req_percent = result.GetInt8Str("hp_req_percent");
  4309. data->power_req = result.GetFloatStr("power_req");
  4310. data->power_by_level = ( result.GetInt8Str("power_by_level") == 0)? false : true;
  4311. data->power_upkeep = result.GetInt16Str("power_upkeep");
  4312. data->power_req_percent = result.GetInt8Str("power_req_percent");
  4313. data->savagery_req = result.GetInt16Str("savagery_req");
  4314. data->savagery_upkeep = result.GetInt16Str("savagery_upkeep");
  4315. data->savagery_req_percent = result.GetInt8Str("savagery_req_percent");
  4316. data->dissonance_req = result.GetInt16Str("dissonance_req");
  4317. data->dissonance_upkeep = result.GetInt16Str("dissonance_upkeep");
  4318. data->dissonance_req_percent = result.GetInt8Str("dissonance_req_percent");
  4319. /* Spell Parameters */
  4320. data->call_frequency = result.GetInt32Str("call_frequency");
  4321. data->orig_cast_time = result.GetInt16Str("cast_time");
  4322. data->cast_time = result.GetInt16Str("cast_time");
  4323. data->duration1 = result.GetInt32Str("duration1");
  4324. data->duration2 = result.GetInt32Str("duration2");
  4325. data->hit_bonus = result.GetFloatStr("hit_bonus");
  4326. data->max_aoe_targets = result.GetInt16Str("max_aoe_targets");
  4327. data->min_range = result.GetFloatStr("min_range");
  4328. data->radius = result.GetFloatStr("radius");
  4329. data->range = result.GetFloatStr("range");
  4330. data->recast = result.GetFloatStr("recast");
  4331. data->recovery = result.GetFloatStr("recovery");
  4332. data->resistibility = result.GetFloatStr("resistibility");
  4333. data->linked_timer = result.GetInt32Str("linked_timer_id");
  4334. data->spell_name_crc = result.GetInt32Str("spell_name_crc");
  4335. data->type_group_spell_id = result.GetSInt32Str("type_group_spell_id");
  4336. data->can_fizzle = ( result.GetInt8Str("can_fizzle") == 1);
  4337. string given_by = result.GetStringStr("given_by");
  4338. data->given_by.data = given_by.c_str();
  4339. data->given_by.size = data->given_by.data.length();
  4340. std::string givenType(given_by);
  4341. boost::algorithm::to_lower(givenType);
  4342. if(givenType == "unset" || givenType.length() < 1) {
  4343. data->given_by_type == GivenByType::GivenBy_Unset;
  4344. }
  4345. else if(givenType == "tradeskillclass") {
  4346. data->given_by_type = GivenByType::GivenBy_TradeskillClass;
  4347. }
  4348. else if(givenType == "spellscroll") {
  4349. data->given_by_type = GivenByType::GivenBy_SpellScroll;
  4350. }
  4351. else if(givenType == "alternateadvancement") {
  4352. data->given_by_type = GivenByType::GivenBy_AltAdvancement;
  4353. }
  4354. else if(givenType == "race") {
  4355. data->given_by_type = GivenByType::GivenBy_Race;
  4356. }
  4357. else if(givenType == "racialinnate") {
  4358. data->given_by_type = GivenByType::GivenBy_RacialInnate;
  4359. }
  4360. else if(givenType == "racialtradition") {
  4361. data->given_by_type = GivenByType::GivenBy_RacialTradition;
  4362. }
  4363. else if(givenType == "class") {
  4364. data->given_by_type = GivenByType::GivenBy_Class;
  4365. }
  4366. else if(givenType == "charactertrait") {
  4367. data->given_by_type = GivenByType::GivenBy_CharacterTrait;
  4368. }
  4369. else if(givenType == "focusabilities") {
  4370. data->given_by_type = GivenByType::GivenBy_FocusAbility;
  4371. }
  4372. else if(givenType == "classtraining") {
  4373. data->given_by_type = GivenByType::GivenBy_ClassTraining;
  4374. }
  4375. else if(givenType == "warderspell") {
  4376. data->given_by_type = GivenByType::GivenBy_WarderSpell;
  4377. }
  4378. else {
  4379. data->given_by_type = GivenByType::GivenBy_Unset;
  4380. }
  4381. /* Cast Messaging */
  4382. string message = result.GetStringStr("success_message");
  4383. if( message.length() > 0 )
  4384. data->success_message = message;
  4385. message = result.GetStringStr("fade_message");
  4386. if( message.length() > 0 )
  4387. data->fade_message = string(message);
  4388. message = result.GetStringStr("fade_message_others");
  4389. if (message.length() > 0)
  4390. data->fade_message_others = string(message);
  4391. message = result.GetStringStr("effect_message");
  4392. if( message.length() > 0 )
  4393. data->effect_message = string(message);
  4394. string lua_script = result.GetStringStr("lua_script");
  4395. if( lua_script.length() > 0 )
  4396. data->lua_script = string(lua_script);
  4397. /* Load spell level data */
  4398. spell = new Spell(data);
  4399. if(level_data && level_data->count(data->id) > 0)
  4400. {
  4401. vector<LevelArray*>* level_array = &((*level_data)[data->id]);
  4402. for(int8 i=0; i<level_array->size(); i++)
  4403. {
  4404. spell->AddSpellLevel(level_array->at(i)->adventure_class, level_array->at(i)->tradeskill_class, level_array->at(i)->spell_level*10);
  4405. }
  4406. }
  4407. /* Add spell to master list */
  4408. master_spell_list.AddSpell(data->id, data->tier, spell);
  4409. total++;
  4410. if( lua_script.length() > 0 )
  4411. LogWrite(SPELL__DEBUG, 5, "Spells", "\t%i. %s (Tier: %i) - '%s'", spell_id, spell_name.c_str(), data->tier, lua_script.c_str());
  4412. else if(data->is_active)
  4413. LogWrite(SPELL__WARNING, 1, "Spells", "\tSpell %s (%u, Tier: %i) set 'Active', but missing LUAScript", spell_name.c_str(), spell_id, data->tier);
  4414. } // end while
  4415. } // end else
  4416. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Spell Effects...");
  4417. LoadSpellEffects();
  4418. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Spell LUA Data...");
  4419. LoadSpellLuaData();
  4420. if(lua_interface)
  4421. {
  4422. LogWrite(SPELL__DEBUG, 0, "Spells", "Loading Spells Scripts...");
  4423. LoadSpellScriptData();
  4424. }
  4425. if (level_data) {
  4426. map<int32, vector<LevelArray*> >::iterator map_itr;
  4427. vector<LevelArray*>::iterator level_itr;
  4428. for(map_itr = level_data->begin(); map_itr != level_data->end(); map_itr++)
  4429. {
  4430. for(level_itr = map_itr->second.begin(); level_itr != map_itr->second.end(); level_itr++)
  4431. {
  4432. safe_delete(*level_itr);
  4433. }
  4434. }
  4435. }
  4436. safe_delete(level_data);
  4437. LogWrite(SPELL__INFO, 0, "Spells", "Loaded %u Spell%s (took %u seconds)", total, total == 1 ? "" : "s", Timer::GetUnixTimeStamp() - t_now);
  4438. }
  4439. void WorldDatabase::LoadSpellLuaData(){
  4440. Spell *spell;
  4441. Query query;
  4442. MYSQL_ROW row;
  4443. int32 total = 0;
  4444. MYSQL_RES *result = query.RunQuery2(Q_SELECT, "SELECT `spell_id`,`tier`,`value_type`,`value`,`value2`,`dynamic_helper` "
  4445. "FROM `spell_data` "
  4446. "ORDER BY `index_field`");
  4447. while (result && (row = mysql_fetch_row(result))) {
  4448. if ((spell = master_spell_list.GetSpell(atoul(row[0]), atoi(row[1]))) && row[2] && row[3] && row[4] && row[4]) {
  4449. LogWrite(SPELL__DEBUG, 5, "Spells", "\tLoading Spell LUA Data for spell_id: %u", atoul(row[0]));
  4450. if (!strcmp(row[2], "INT"))
  4451. spell->AddSpellLuaDataInt(atoi(row[3]), atoi(row[4]), string(row[5]));
  4452. else if (!strcmp(row[2], "FLOAT"))
  4453. spell->AddSpellLuaDataFloat(atof(row[3]), atof(row[4]),string(row[5]));
  4454. else if (!strcmp(row[2], "BOOL"))
  4455. spell->AddSpellLuaDataBool(!(strncasecmp(row[3], "true", 4)), string(row[5]));
  4456. else if (!strcmp(row[2], "STRING"))
  4457. spell->AddSpellLuaDataString(string(row[3]), string(row[4]), string(row[5]));
  4458. else
  4459. LogWrite(SPELL__ERROR, 0, "Spells", "Invalid Lua Spell data '%s' for Spell ID: %u", row[2], spell->GetSpellID());
  4460. total++;
  4461. }
  4462. }
  4463. LogWrite(SPELL__DEBUG, 0, "Spells", "\tLoaded %i Spell LUA Data entr%s.", total, total == 1 ? "y" : "ies");
  4464. }
  4465. void WorldDatabase::LoadSpellEffects() {
  4466. Spell *spell;
  4467. Query query;
  4468. MYSQL_ROW row;
  4469. int32 total = 0;
  4470. MYSQL_RES *result = query.RunQuery2(Q_SELECT, "SELECT `spell_id`,`tier`,`percentage`,`bullet`,`description` "
  4471. "FROM `spell_display_effects` "
  4472. "ORDER BY `spell_id`,`id` ASC");
  4473. while (result && (row = mysql_fetch_row(result))) {
  4474. if ((spell = master_spell_list.GetSpell(atoul(row[0]), atoi(row[1]))) && row[4]) {
  4475. LogWrite(SPELL__DEBUG, 5, "Spells", "\tLoading Spell Effects for spell_id: %u", atoul(row[0]));
  4476. spell->AddSpellEffect(atoi(row[2]), atoi(row[3]), row[4]);
  4477. total++;
  4478. }
  4479. }
  4480. LogWrite(SPELL__DEBUG, 0, "Spells", "\tLoaded %u Spell Effect%s.", total, total == 1 ? "" : "s");
  4481. }
  4482. int32 WorldDatabase::LoadPlayerSkillbar(Client* client){
  4483. client->GetPlayer()->ClearQuickbarItems();
  4484. Query query;
  4485. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, type, spell_id, slot, text_val, hotbar, tier FROM character_skillbar where char_id = %u", client->GetCharacterID());
  4486. MYSQL_ROW row;
  4487. int32 count = 0;
  4488. while(result && (row = mysql_fetch_row(result))){
  4489. count++;
  4490. int8 tier = atoi(row[6]);
  4491. Spell* spell = master_spell_list.GetSpell(atoul(row[2]), tier);
  4492. if(spell)
  4493. client->GetPlayer()->AddQuickbarItem(atoul(row[5]), atoul(row[3]), atoul(row[1]), spell->GetSpellIcon(), spell->GetSpellIconBackdrop(), spell->GetSpellID(), spell->GetSpellTier(), atoul(row[0]), row[4], false);
  4494. else if(atoul(row[1]) == QUICKBAR_MACRO)
  4495. client->GetPlayer()->AddQuickbarItem(atoul(row[5]), atoul(row[3]), atoul(row[1]), client->GetPlayer()->macro_icons[atoul(row[2])], 0xFFFF, atoul(row[2]), 0, atoul(row[0]), row[4], false);
  4496. else
  4497. client->GetPlayer()->AddQuickbarItem(atoul(row[5]), atoul(row[3]), atoul(row[1]), 0, 0, atoul(row[2]), 0, atoul(row[0]), row[4], false);
  4498. }
  4499. return count;
  4500. }
  4501. bool WorldDatabase::DeleteCharacter(int32 account_id, int32 character_id){
  4502. Guild *guild;
  4503. if((guild = guild_list.GetGuild(GetGuildIDByCharacterID(character_id))))
  4504. guild->RemoveGuildMember(character_id);
  4505. Query query;
  4506. //devn00b: Update this whole thing we were missing many tables. This is ugly but swapped 99% of the delete to async to lighten server load.
  4507. //Do character delete immediately.
  4508. query.RunQuery2(Q_DELETE, "DELETE FROM characters WHERE id=%u AND account_id=%u", character_id, account_id);
  4509. //if no character then we shouldn't bother with the rest.
  4510. if (!query.GetAffectedRows())
  4511. {
  4512. //No error just in case ppl try doing stupid stuff
  4513. return false;
  4514. }
  4515. //Async
  4516. query.AddQueryAsync(character_id, this, Q_DELETE, "DELETE FROM character_languages WHERE char_id=%u", character_id);
  4517. query.AddQueryAsync(character_id, this, Q_DELETE, "DELETE FROM character_quest_rewards WHERE char_id=%u", character_id);
  4518. query.AddQueryAsync(character_id, this, Q_DELETE, "DELETE FROM character_quest_temporary_rewards WHERE char_id=%u", character_id);
  4519. query.AddQueryAsync(character_id, this, Q_DELETE, "DELETE FROM character_claim_items where char_id=%u", character_id);
  4520. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from charactersproperties where charid = %u", character_id);
  4521. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_aa where char_id = %u", character_id);
  4522. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_achievements where char_id = %u", character_id);
  4523. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_achievements_items where char_id = %u", character_id);
  4524. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_buyback where char_id = %u", character_id);
  4525. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_collections where char_id = %u", character_id);
  4526. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_collection_items where char_id = %u", character_id);
  4527. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_details where char_id = %u", character_id);
  4528. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_factions where char_id = %u", character_id);
  4529. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_history where char_id = %u", character_id);
  4530. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_houses where char_id = %u", character_id);
  4531. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_instances where char_id = %u", character_id);
  4532. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_items where char_id = %u", character_id);
  4533. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_items_group_members where character_id = %u", character_id);
  4534. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_lua_history where char_id = %u", character_id);
  4535. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_macros where char_id = %u", character_id);
  4536. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_pictures where char_id = %u", character_id);
  4537. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_properties where charid = %u", character_id);
  4538. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_quests where char_id = %u", character_id);
  4539. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_quest_progress where char_id = %u", character_id);
  4540. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_recipes where char_id = %u", character_id);
  4541. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_recipe_books where char_id = %u", character_id);
  4542. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_skillbar where char_id = %u", character_id);
  4543. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_skills where char_id = %u", character_id);
  4544. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_social where char_id = %u", character_id);
  4545. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_spells where char_id = %u", character_id);
  4546. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_spell_effects where caster_char_id = %u or target_char_id = %u", character_id, character_id);
  4547. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_spell_effect_targets where caster_char_id = %u or target_char_id = %u", character_id, character_id);
  4548. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_spirit_shards where charid = %u", character_id);
  4549. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from character_titles where char_id = %u", character_id);
  4550. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from char_colors where char_id = %u", character_id);
  4551. query.AddQueryAsync(character_id, this, Q_DELETE, "delete from statistics where char_id = %u", character_id);
  4552. return true;
  4553. }
  4554. void WorldDatabase::DeleteCharacterSpell(int32 character_id, int32 spell_id) {
  4555. if (character_id > 0 && spell_id > 0) {
  4556. Query query;
  4557. query.RunQuery2(Q_DELETE, "DELETE FROM character_spells WHERE char_id=%u AND spell_id=%u", character_id, spell_id);
  4558. }
  4559. }
  4560. bool WorldDatabase::GetItemResultsToClient (Client* client, const char* varSearch, int maxResults) {
  4561. Query query;
  4562. MYSQL_ROW row;
  4563. int results = 0;
  4564. if( maxResults > 10 && client->GetAdminStatus ( ) < 100 )
  4565. maxResults = 10;
  4566. else if( maxResults > 20 )
  4567. maxResults = 20;
  4568. client->Message(CHANNEL_COLOR_YELLOW, "Item Search Results");
  4569. client->Message(CHANNEL_COLOR_YELLOW, "ResultNum) [ItemID] ItemName");
  4570. string itemsearch_query = string("SELECT id, name FROM items where name like '%%%s%%' limit %i");
  4571. MYSQL_RES* result = query.RunQuery2(Q_SELECT, itemsearch_query.c_str(),getSafeEscapeString(varSearch).c_str(),maxResults);
  4572. while(result && (row = mysql_fetch_row(result))){
  4573. results++;
  4574. client->Message(CHANNEL_COLOR_YELLOW, "%i) [%s] %s",results,row[0],row[1]);
  4575. }
  4576. if(results == 0)
  4577. {
  4578. client->Message(CHANNEL_COLOR_YELLOW, "No Items Found.");
  4579. return false;
  4580. }
  4581. client->Message(CHANNEL_COLOR_YELLOW, "%i Items Found.",results);
  4582. return true;
  4583. }
  4584. void WorldDatabase::SaveWorldTime(WorldTime* time){
  4585. Query query;
  4586. query.RunQuery2(Q_REPLACE, "replace into variables (variable_name, variable_value) values('gametime', '%i/%i/%i %i:%i')", time->month, time->day, time->year, time->hour, time->minute);
  4587. }
  4588. void WorldDatabase::SaveBugReport(const char* category, const char* subcategory, const char* causes_crash, const char* reproducible, const char* summary, const char* description, const char* version, const char* player, int32 account_id, const char* spawn_name, int32 spawn_id, int32 zone_id){
  4589. Query query;
  4590. int32 dbVersion = rule_manager.GetGlobalRule(R_World, DatabaseVersion)->GetInt32();
  4591. string bug_report = string("insert into bugs (category, subcategory, causes_crash, reproducible, summary, description, version, player, account_id, spawn_name, spawn_id, zone_id, dbversion, worldversion) values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %u, '%s', %u, %u, %u, '%s')");
  4592. query.RunQuery2(Q_INSERT, bug_report.c_str(), getSafeEscapeString(category).c_str(), getSafeEscapeString(subcategory).c_str(),
  4593. getSafeEscapeString(causes_crash).c_str(), getSafeEscapeString(reproducible).c_str(), getSafeEscapeString(summary).c_str(),
  4594. getSafeEscapeString(description).c_str(), getSafeEscapeString(version).c_str(), getSafeEscapeString(player).c_str(), account_id,
  4595. getSafeEscapeString(spawn_name).c_str(), spawn_id, zone_id, dbVersion, CURRENT_VERSION);
  4596. FixBugReport();
  4597. FixBugReport();
  4598. FixBugReport();
  4599. }
  4600. void WorldDatabase::FixBugReport(){
  4601. Query query;
  4602. string bug_report = string("update bugs set description = REPLACE(description,SUBSTRING(description,INSTR(description,'%'), 3),char(CONV(SUBSTRING(description,INSTR(description,'%')+1, 2), 16, 10))), summary = REPLACE(summary,SUBSTRING(summary,INSTR(summary,'%'), 3),char(CONV(SUBSTRING(summary,INSTR(summary,'%')+1, 2), 16, 10)))");
  4603. query.RunQuery2(bug_report.c_str(), Q_UPDATE);
  4604. }
  4605. int32 WorldDatabase::LoadQuests(){
  4606. Query query;
  4607. MYSQL_ROW row;
  4608. std::string querystr = std::string("SELECT `quest_id`, `name`, `type`, `zone`, `level`, `enc_level`, `description`, `lua_script`, `completed_text`, `spawn_id`, `shareable_flag`, `deleteable` FROM `quests`");
  4609. MYSQL_RES* result = query.RunQuery2(Q_SELECT, querystr.c_str());
  4610. Quest* quest = 0;
  4611. char* name = 0;
  4612. char* type = 0;
  4613. char* zone = 0;
  4614. int8 level = 0;
  4615. int8 enc_level = 0;
  4616. char* description = 0;
  4617. char* script = 0;
  4618. int32 total = 0;
  4619. int32 id = 0;
  4620. char* completed_description = 0;
  4621. int32 return_npc_id = 0;
  4622. if(result){
  4623. while(result && (row = mysql_fetch_row(result))){
  4624. id = atoul(row[0]);
  4625. name = row[1];
  4626. type = row[2];
  4627. zone = row[3];
  4628. level = atoul(row[4]);
  4629. enc_level = atoul(row[5]);
  4630. description = row[6];
  4631. script = row[7];
  4632. completed_description = row[8];
  4633. return_npc_id = atoi(row[9]);
  4634. if(lua_interface) {
  4635. quest = lua_interface->LoadQuest(id, name, type, zone, level, description, script);
  4636. }
  4637. if(quest){
  4638. LogWrite(QUEST__DEBUG, 5, "Quests", "\tLoading Quest: '%s' (%u)", name, id);
  4639. LoadQuestDetails(quest);
  4640. string compDescription;
  4641. if (completed_description == NULL)
  4642. {
  4643. compDescription = string("Missing! Notify Developer");
  4644. LogWrite(QUEST__WARNING, 5, "Quests", "\tLoading Quest MISSING completed_text in quests table for: '%s' (%u)", name, id);
  4645. }
  4646. else
  4647. compDescription = string(completed_description);
  4648. quest->SetCompletedDescription(string(compDescription));
  4649. quest->SetQuestReturnNPC(return_npc_id);
  4650. quest->SetEncounterLevel(enc_level);
  4651. quest->SetQuestShareableFlag(atoul(row[10]));
  4652. quest->SetCanDeleteQuest(atoul(row[11]));
  4653. total++;
  4654. master_quest_list.AddQuest(id, quest);
  4655. }
  4656. else
  4657. {
  4658. LogWrite(QUEST__ERROR, 5, "Quests", "\tFAILED LOADING QUEST: '%s' (%u), check that script file exists/permissions correct: %s", name, id, (script && strlen(script)) ? script : "Not Set, Missing!");
  4659. }
  4660. }
  4661. }
  4662. LogWrite(QUEST__DEBUG, 0, "Quest", "\tLoaded %i Quest(s)", total);
  4663. return total;
  4664. }
  4665. void WorldDatabase::LoadQuestDetails(Quest* quest) {
  4666. Query query;
  4667. MYSQL_ROW row;
  4668. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `type`, `subtype`, `value`, `faction_id`, `quantity` FROM `quest_details` WHERE `quest_id`=%u", quest->GetQuestID());
  4669. string type;
  4670. string subtype;
  4671. sint64 value;
  4672. int32 faction_id;
  4673. int32 quantity;
  4674. while (result && (row = mysql_fetch_row(result))) {
  4675. type = string(row[0]);
  4676. subtype = string(row[1]);
  4677. value = atoi(row[2]);
  4678. faction_id = atoi(row[3]);
  4679. quantity = atoi(row[4]);
  4680. LogWrite(QUEST__DEBUG, 5, "Quests", "\t- Type: %s, SubType: %s, Val: %u, Faction: %u, Qty: %u", type.c_str(), subtype.c_str(), value, faction_id, quantity);
  4681. if (type == "Prereq") {
  4682. if (subtype == "Class")
  4683. quest->AddPrereqClass(value);
  4684. else if (subtype == "Faction")
  4685. quest->AddPrereqFaction(faction_id, value);
  4686. else if (subtype == "Item") {
  4687. Item* master_item = master_item_list.GetItem(value);
  4688. if (master_item) {
  4689. Item* item = new Item(master_item);
  4690. quest->AddPrereqItem(item);
  4691. }
  4692. }
  4693. else if (subtype == "AdvLevel")
  4694. quest->SetPrereqLevel(value);
  4695. else if (subtype == "Quest")
  4696. quest->AddPrereqQuest(value);
  4697. else if (subtype == "Race")
  4698. quest->AddPrereqRace(value);
  4699. else if (subtype == "TSClass")
  4700. quest->AddPrereqTradeskillClass(value);
  4701. else if (subtype == "TSLevel")
  4702. quest->SetPrereqTSLevel(value);
  4703. else if (subtype == "MaxTSLevel")
  4704. quest->SetPrereqMaxTSLevel(value);
  4705. else if (subtype == "MaxAdvLevel")
  4706. quest->SetPrereqMaxLevel(value);
  4707. }
  4708. else if (type == "Reward") {
  4709. if (subtype == "Item") {
  4710. Item* master_item = master_item_list.GetItem(value);
  4711. if (master_item) {
  4712. Item* item = new Item(master_item);
  4713. item->details.count = quantity;
  4714. quest->AddRewardItem(item);
  4715. }
  4716. }
  4717. else if (subtype == "Selectable") {
  4718. Item* master_item = master_item_list.GetItem(value);
  4719. if (master_item) {
  4720. Item* item = new Item(master_item);
  4721. item->details.count = quantity;
  4722. quest->AddSelectableRewardItem(item);
  4723. }
  4724. }
  4725. else if (subtype == "Coin") {
  4726. int32 copper = 0;
  4727. int32 silver = 0;
  4728. int32 gold = 0;
  4729. int32 plat = 0;
  4730. if (value >= 1000000) {
  4731. plat = value / 1000000;
  4732. value -= 1000000 * plat;
  4733. }
  4734. if (value >= 10000) {
  4735. gold = value / 10000;
  4736. value -= 10000 * gold;
  4737. }
  4738. if (value >= 100) {
  4739. silver = value / 100;
  4740. value -= 100 * silver;
  4741. }
  4742. if (value > 0)
  4743. copper = value;
  4744. quest->AddRewardCoins(copper, silver, gold, plat);
  4745. }
  4746. else if (subtype == "MaxCoin") {
  4747. quest->AddRewardCoinsMax(value);
  4748. }
  4749. else if (subtype == "Faction")
  4750. quest->AddRewardFaction(faction_id, value);
  4751. else if (subtype == "Experience")
  4752. quest->SetRewardXP(value);
  4753. else if (subtype == "TSExperience")
  4754. quest->SetRewardTSXP(value);
  4755. }
  4756. }
  4757. }
  4758. void WorldDatabase::LoadMerchantInformation() {
  4759. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tClearing Merchant Inventory...");
  4760. world.DeleteMerchantItems();
  4761. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tLoading Merchant Inventory...");
  4762. LoadMerchantInventory();
  4763. Query query;
  4764. MYSQL_ROW row;
  4765. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT merchant_id, inventory_id FROM merchants ORDER BY merchant_id");
  4766. int32 total = 0;
  4767. int32 last_merchant_id = 0;
  4768. int32 id = 0;
  4769. MerchantInfo* merchant = 0;
  4770. if(result) {
  4771. while(result && (row = mysql_fetch_row(result))) {
  4772. id = atoul(row[0]);
  4773. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "\tMerchantID: %u, InventoryID: %u", id, atoul(row[1]));
  4774. if(id != last_merchant_id) {
  4775. if(merchant)
  4776. world.AddMerchantInfo(last_merchant_id, merchant);
  4777. merchant = new MerchantInfo;
  4778. last_merchant_id = id;
  4779. total++;
  4780. }
  4781. merchant->inventory_ids.push_back(atoul(row[1]));
  4782. }
  4783. if(merchant)
  4784. world.AddMerchantInfo(last_merchant_id, merchant);
  4785. }
  4786. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tLoaded %i Merchant List(s)", total);
  4787. }
  4788. void WorldDatabase::LoadMerchantInventory(){
  4789. Query query;
  4790. MYSQL_ROW row;
  4791. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT inventory_id, item_id, quantity, price_item_id, price_item_qty, price_item2_id, price_item2_qty, price_status, price_coins, price_stationcash FROM merchant_inventory ORDER BY inventory_id");
  4792. int32 total = 0;
  4793. int32 id;
  4794. if(result) {
  4795. while(result && (row = mysql_fetch_row(result))) {
  4796. MerchantItemInfo ItemInfo;
  4797. id = atoul(row[0]);
  4798. ItemInfo.item_id = atoul(row[1]);
  4799. ItemInfo.quantity = atoi(row[2]);
  4800. ItemInfo.price_item_id = atoul(row[3]);
  4801. ItemInfo.price_item_qty = atoi(row[4]);
  4802. ItemInfo.price_item2_id = atoul(row[5]);
  4803. ItemInfo.price_item2_qty = atoi(row[6]);
  4804. ItemInfo.price_status = atoul(row[7]);
  4805. ItemInfo.price_coins = atoul(row[8]);
  4806. ItemInfo.price_stationcash = atoul(row[9]);
  4807. LogWrite(MERCHANT__DEBUG, 5, "Merchant", "\tInventoryID: %u, ItemID: %u, Qty: %u", id, ItemInfo.item_id, ItemInfo.quantity);
  4808. world.AddMerchantItem(id, ItemInfo);
  4809. total++;
  4810. }
  4811. }
  4812. LogWrite(MERCHANT__DEBUG, 0, "Merchant", "\tLoaded %i Merchant Inventory Item(s)", total);
  4813. }
  4814. string WorldDatabase::GetMerchantDescription(int32 merchant_id) {
  4815. Query query;
  4816. MYSQL_ROW row;
  4817. string description;
  4818. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `description` FROM `merchants` WHERE `merchant_id`=%u", merchant_id);
  4819. if (result && (row = mysql_fetch_row(result)))
  4820. description = string(row[0]);
  4821. return description;
  4822. }
  4823. void WorldDatabase::LoadTransporters(ZoneServer* zone){
  4824. int32 total = 0;
  4825. zone->DeleteGlobalTransporters();
  4826. Query query;
  4827. MYSQL_ROW row;
  4828. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT transport_id, transport_type, display_name, message, destination_zone_id, destination_x, destination_y, destination_z, destination_heading, trigger_location_zone_id, trigger_location_x, trigger_location_y, trigger_location_z, trigger_radius, cost, id, min_level, max_level, quest_req, quest_step_req, quest_completed, map_x, map_y, expansion_flag, holiday_flag, min_client_version, max_client_version, flight_path_id, mount_id, mount_red_color, mount_green_color, mount_blue_color FROM transporters ORDER BY transport_id");
  4829. if(result){
  4830. while(result && (row = mysql_fetch_row(result))){
  4831. LogWrite(TRANSPORT__DEBUG, 5, "Transport", "---Loading Transporter ID: %u, transport_type: %s", row[0], row[1]);
  4832. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---display_name: %s, message: %s", row[2], row[3]);
  4833. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---destination_zone_id: %s", row[4]);
  4834. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---destination_x: %s, destination_y: %s, destination_z: %s, destination_heading: %s", row[5], row[6], row[7], row[8]);
  4835. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---trigger_location_zone_id: %s", row[9]);
  4836. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---trigger_location_x: %s, trigger_location_y: %s, trigger_location_z: %s", row[10], row[11], row[12], row[13]);
  4837. LogWrite(TRANSPORT__DEBUG, 7, "Transport", "---trigger_radius: %s, cost: %s, id: %s", row[14], row[15]);
  4838. string name = "";
  4839. if(row[2])
  4840. name = string(row[2]);
  4841. string message = "";
  4842. if(row[3])
  4843. message = string(row[3]);
  4844. if(row[1] && strcmp(row[1], "Zone") == 0)
  4845. zone->AddTransporter(atoul(row[0]), TRANSPORT_TYPE_ZONE, name, message, atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]), atoi(row[16]), atoi(row[17]), atoul(row[18]), atoi(row[19]), atoul(row[20]), atoul(row[21]), atoul(row[22]), atoul(row[23]), atoul(row[24]), atoul(row[25]), atoul(row[26]), atoul(row[27]), atoul(row[28]), atoul(row[29]), atoul(row[30]), atoul(row[31]));
  4846. else if (row[1] && strcmp(row[1], "Flight") == 0)
  4847. zone->AddTransporter(atoul(row[0]), TRANSPORT_TYPE_FLIGHT, name, message, atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]), atoi(row[16]), atoi(row[17]), atoul(row[18]), atoi(row[19]), atoul(row[20]), atoul(row[21]), atoul(row[22]), atoul(row[23]), atoul(row[24]), atoul(row[25]), atoul(row[26]), atoul(row[27]), atoul(row[28]), atoul(row[29]), atoul(row[30]), atoul(row[31]));
  4848. else if(row[1] && strcmp(row[1], "Location") == 0)
  4849. zone->AddLocationTransporter(atoul(row[9]), message, atof(row[10]), atof(row[11]), atof(row[12]), atof(row[13]), atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]));
  4850. else
  4851. zone->AddTransporter(atoul(row[0]), TRANSPORT_TYPE_GENERIC, "", message, atoul(row[4]), atof(row[5]), atof(row[6]), atof(row[7]), atof(row[8]), atoul(row[14]), atoul(row[15]), atoi(row[16]), atoi(row[17]), atoul(row[18]), atoi(row[19]), atoul(row[20]), atoul(row[21]), atoul(row[22]), atoul(row[23]), atoul(row[24]), atoul(row[25]), atoul(row[26]), atoul(row[27]), atoul(row[28]), atoul(row[29]), atoul(row[30]), atoul(row[31]));
  4852. total++;
  4853. }
  4854. }
  4855. LogWrite(TRANSPORT__DEBUG, 0, "Transport", "--Loaded %i Transporter(s)", total);
  4856. LoadTransportMaps(zone);
  4857. }
  4858. void WorldDatabase::LoadFogInit(string zone, PacketStruct* packet)
  4859. {
  4860. LogWrite(WORLD__TRACE, 9, "World", "Enter: %s", __FUNCTION__);
  4861. if(!packet || zone.length() == 0)
  4862. return;
  4863. Query query;
  4864. MYSQL_ROW row;
  4865. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT highest, lowest, zone_name, explored_map_name, unexplored_map_name, bounds1_x, bounds1_z, bounds2_x, bounds2_z, bounds3_x, bounds3_z, bounds4_x, bounds4_z, explored_key, unexplored_key, map_id FROM map_data where zone_name like '%s%%'", getSafeEscapeString(&zone).c_str());
  4866. if(result){
  4867. int count = mysql_num_rows(result);
  4868. int i=0;
  4869. int64 explored_key;
  4870. int64 unexplored_key;
  4871. packet->setArrayLengthByName("num_maps", count);
  4872. while(result && (row = mysql_fetch_row(result))){
  4873. packet->setDataByName("highest_z", atof(row[0]));
  4874. packet->setDataByName("lowest_z", atof(row[1]));
  4875. packet->setDataByName("map_id", atoul(row[15]));
  4876. packet->setArrayDataByName("unknown7", 1600, i);
  4877. packet->setArrayDataByName("unknown8", 1200, i);
  4878. packet->setArrayDataByName("zone_name", row[2], i);
  4879. packet->setArrayDataByName("explored_map_name", row[3], i);
  4880. packet->setArrayDataByName("unexplored_map_name", row[4], i);
  4881. packet->setArrayDataByName("map_bounds1_x", atof(row[5]), i);
  4882. packet->setArrayDataByName("map_bounds1_z", atof(row[6]), i);
  4883. packet->setArrayDataByName("map_bounds2_x", atof(row[7]), i);
  4884. packet->setArrayDataByName("map_bounds2_z", atof(row[8]), i);
  4885. packet->setArrayDataByName("map_bounds3_x", atof(row[9]), i);
  4886. packet->setArrayDataByName("map_bounds3_z", atof(row[10]), i);
  4887. packet->setArrayDataByName("map_bounds4_x", atof(row[11]), i);
  4888. packet->setArrayDataByName("map_bounds4_z", atof(row[12]), i);
  4889. #ifdef WIN32
  4890. explored_key = _strtoui64(row[13], NULL, 10);
  4891. unexplored_key = _strtoui64(row[14], NULL, 10);
  4892. #else
  4893. explored_key = strtoull(row[13], 0, 10);
  4894. unexplored_key = strtoull(row[14], 0, 10);
  4895. #endif
  4896. packet->setArrayDataByName("explored_key", explored_key, i);
  4897. packet->setArrayDataByName("unexplored_key", unexplored_key, i);
  4898. i++;
  4899. }
  4900. }
  4901. LogWrite(WORLD__TRACE, 9, "World", "Exit: %s", __FUNCTION__);
  4902. }
  4903. string WorldDatabase::GetColumnNames(char* name){
  4904. Query query;
  4905. MYSQL_ROW row;
  4906. string columns = "";
  4907. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "show columns FROM %s", name);
  4908. if(result && mysql_num_rows(result) > 0){
  4909. int16 i = 0;
  4910. while((row = mysql_fetch_row(result))){
  4911. if(strcmp(row[0], "table_data_version") != 0){
  4912. if(i>0)
  4913. columns.append(",");
  4914. columns.append(row[0]);
  4915. i++;
  4916. }
  4917. }
  4918. }
  4919. columns.append("");
  4920. return columns;
  4921. }
  4922. void WorldDatabase::ToggleCharacterOnline() {
  4923. Query query;
  4924. query.RunQuery2(Q_UPDATE, "UPDATE characters SET is_online = 0;");
  4925. }
  4926. void WorldDatabase::ToggleCharacterOnline(Client* client, int8 toggle) {
  4927. if (client) {
  4928. Query query;
  4929. Player* player = client->GetPlayer();
  4930. //if(!player->CheckPlayerInfo())
  4931. // return;
  4932. if (player)
  4933. {
  4934. LogWrite(PLAYER__DEBUG, 0, "Player", "Toggling Character %s", toggle ? "ONLINE!" : "OFFLINE!");
  4935. query.RunQuery2(Q_UPDATE, "UPDATE characters SET is_online=%i WHERE id = %u;", toggle, client->GetCharacterID());
  4936. }
  4937. }
  4938. }
  4939. void WorldDatabase::LoadPlayerStatistics(Player* player, int32 char_id) {
  4940. Query query;
  4941. MYSQL_ROW row;
  4942. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT stat_id, stat_value, stat_date FROM statistics WHERE char_id=%i", char_id);
  4943. while (result && (row = mysql_fetch_row(result))) {
  4944. int32 stat_id = atoi(row[0]);
  4945. sint32 stat_value = atoi(row[1]);
  4946. int32 stat_date = atoi(row[2]);
  4947. player->AddPlayerStatistic(stat_id, stat_value, stat_date);
  4948. }
  4949. }
  4950. void WorldDatabase::WritePlayerStatistic(Player *player, Statistic* stat) {
  4951. if (player && player->GetCharacterID() > 0 && stat) {
  4952. Query query;
  4953. query.RunQuery2(Q_INSERT, "INSERT INTO statistics (char_id, guild_id, stat_id, stat_value, stat_date) VALUES(%i, %i, %i, %i, %i) ON DUPLICATE KEY UPDATE stat_value = %i, stat_date = %i;",
  4954. player->GetCharacterID(), 0, stat->stat_id, stat->stat_value, stat->stat_date,
  4955. stat->stat_value, stat->stat_date);
  4956. }
  4957. }
  4958. void WorldDatabase::LoadServerStatistics()
  4959. {
  4960. Query query;
  4961. MYSQL_ROW row;
  4962. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT stat_id, stat_value, stat_date FROM statistics WHERE char_id=0 AND guild_id=0");
  4963. while (result && (row = mysql_fetch_row(result))) {
  4964. int32 stat_id = atoi(row[0]);
  4965. sint32 stat_value = atoi(row[1]);
  4966. int32 stat_date = atoi(row[2]);
  4967. world.AddServerStatistic(stat_id, stat_value, stat_date);
  4968. LogWrite(INIT__DEBUG, 5, "Stats", "Loading Stat ID %i, value: %i", stat_id, stat_value);
  4969. }
  4970. }
  4971. void WorldDatabase::WriteServerStatistic(Statistic* stat) {
  4972. if (stat) {
  4973. Query query;
  4974. query.RunQuery2(Q_INSERT, "INSERT INTO statistics (char_id, guild_id, stat_id, stat_value, stat_date) VALUES(0, 0, %i, %i, %i) ON DUPLICATE KEY UPDATE stat_value = %i, stat_date = %i;",
  4975. stat->stat_id, stat->stat_value, stat->stat_date,
  4976. stat->stat_value, stat->stat_date);
  4977. }
  4978. }
  4979. void WorldDatabase::WriteServerStatistic(int32 stat_id, sint32 stat_value) {
  4980. Query query;
  4981. query.RunQuery2(Q_INSERT, "INSERT INTO statistics (char_id, guild_id, stat_id, stat_value, stat_date) VALUES(0, 0, %i, %i, %i) ON DUPLICATE KEY UPDATE stat_value = %i, stat_date = %i;",
  4982. stat_id, stat_value, Timer::GetUnixTimeStamp(),
  4983. stat_value, Timer::GetUnixTimeStamp());
  4984. }
  4985. void WorldDatabase::WriteServerStatisticsNeededQueries() {
  4986. Query query1, query2, query3;
  4987. MYSQL_ROW row1, row2, row3;
  4988. // Number of unique accounts
  4989. MYSQL_RES* result1 = query1.RunQuery2(Q_SELECT, "SELECT COUNT(DISTINCT account_id) FROM characters");
  4990. if (result1 && (row1 = mysql_fetch_row(result1)) && row1[0] != NULL)
  4991. WriteServerStatistic(STAT_SERVER_NUM_ACCOUNTS, atoi(row1[0]));
  4992. else
  4993. WriteServerStatistic(STAT_SERVER_NUM_ACCOUNTS, 0);
  4994. // Number of characters
  4995. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT COUNT(id) FROM characters");
  4996. if (result2 && (row2 = mysql_fetch_row(result2)) && row2[0] != NULL)
  4997. WriteServerStatistic(STAT_SERVER_NUM_CHARACTERS, atoi(row2[0]));
  4998. else
  4999. WriteServerStatistic(STAT_SERVER_NUM_CHARACTERS, 0);
  5000. // Average character level
  5001. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT ROUND(AVG(level)) FROM characters");
  5002. if (result3 && (row3 = mysql_fetch_row(result3)) && row3[0] != NULL)
  5003. WriteServerStatistic(STAT_SERVER_AVG_CHAR_LEVEL, atoi(row3[0]));
  5004. else
  5005. WriteServerStatistic(STAT_SERVER_AVG_CHAR_LEVEL, 0);
  5006. }
  5007. map<int32,int32>* WorldDatabase::GetInstanceRemovedSpawns(int32 instance_id, int8 type)
  5008. {
  5009. DatabaseResult result;
  5010. map<int32,int32>* ret = NULL;
  5011. LogWrite(SPAWN__TRACE, 1, "Spawn", "Enter %s", __FUNCTION__);
  5012. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Loading removed spawns for instance_id: %u, spawn_type: %i", instance_id, type);
  5013. if( !database_new.Select(&result, "SELECT spawn_location_entry_id, respawn_time FROM instance_spawns_removed WHERE instance_id = %i AND spawn_type = %i", instance_id, type) )
  5014. {
  5015. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in GetInstanceRemovedSpawns() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5016. return ret;
  5017. }
  5018. else
  5019. {
  5020. if( result.GetNumRows() > 0 )
  5021. {
  5022. ret = new map<int32,int32>;
  5023. while( result.Next() )
  5024. {
  5025. int32 spawn_location_entry_id = result.GetInt32Str("spawn_location_entry_id");
  5026. /*
  5027. respawnTime == 0 - never respawn
  5028. respawnTime = 1 - spawn now
  5029. respawnTime > 1 (continue timer)
  5030. */
  5031. int32 respawntime = result.GetInt32Str("respawn_time");
  5032. LogWrite(INSTANCE__DEBUG, 5, "Instance", "Found spawn point: %u, respawn time: %i", spawn_location_entry_id, respawntime);
  5033. ret->insert(make_pair(spawn_location_entry_id, respawntime));
  5034. }
  5035. }
  5036. else
  5037. LogWrite(INSTANCE__DEBUG, 0, "Instance", "No removed spawns found for instance_id: %u, spawn_type: %i", instance_id, type);
  5038. }
  5039. LogWrite(SPAWN__TRACE, 1, "Spawn", "Exit %s", __FUNCTION__);
  5040. return ret;
  5041. }
  5042. bool WorldDatabase::CheckVectorForValue(vector<int32>* vector, int32 value) {
  5043. if ( vector != NULL )
  5044. {
  5045. for(int32 i=0;i<vector->size();i++)
  5046. {
  5047. int32 compare = vector->at(i);
  5048. if ( compare == value )
  5049. return true;
  5050. }
  5051. }
  5052. return false;
  5053. }
  5054. int32 WorldDatabase::CheckSpawnRemoveInfo(map<int32,int32>* inmap, int32 spawn_location_entry_id)
  5055. {
  5056. LogWrite(SPAWN__TRACE, 0, "Spawn", "Enter %s", __FUNCTION__);
  5057. map<int32, int32>::iterator iter;
  5058. if ( inmap != NULL )
  5059. {
  5060. for(iter=inmap->begin();iter!=inmap->end();iter++)
  5061. {
  5062. if ( iter->first == spawn_location_entry_id )
  5063. return (int32)iter->second;
  5064. }
  5065. }
  5066. return 1;
  5067. }
  5068. int32 WorldDatabase::AddCharacterInstance(int32 char_id, int32 instance_id, string zone_name, int8 instance_type, int32 last_success, int32 last_failure, int32 success_lockout, int32 failure_lockout)
  5069. {
  5070. int32 ret = 0;
  5071. if( !database_new.Query("INSERT INTO character_instances (char_id, instance_id, instance_zone_name, instance_type, last_success_timestamp, last_failure_timestamp, success_lockout_time, failure_lockout_time) VALUES (%u, %u, '%s', %i, %u, %u, %u, %u) ", char_id, instance_id, database_new.EscapeStr(zone_name).c_str(), instance_type, last_success, last_failure, success_lockout, failure_lockout) )
  5072. {
  5073. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in AddCharacterInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5074. return 0;
  5075. }
  5076. ret = database_new.LastInsertID();
  5077. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Adding character %u to instance: %u", char_id, instance_id);
  5078. //LogWrite(INSTANCE__DEBUG, 1, "Instance", "-- Reenter: %u, Reset: %u, Lockout: %u", grant_reenter_time_left, grant_reset_time_left, lockout_time);
  5079. return ret;
  5080. }
  5081. bool WorldDatabase::UpdateCharacterInstanceTimers(int32 char_id, int32 instance_id, int32 lockout_time, int32 reset_time, int32 reenter_time )
  5082. {
  5083. if ( lockout_time > 0 && reset_time > 0 && reenter_time > 0 )
  5084. database_new.Query("UPDATE character_instances SET lockout_time = %i, grant_reset_time_left = %i, grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i", lockout_time, reset_time, reenter_time, char_id, instance_id);
  5085. else if ( lockout_time > 0 && reset_time > 0 )
  5086. database_new.Query("UPDATE character_instances SET lockout_time = %i, grant_reset_time_left = %i WHERE char_id = %i AND instance_id = %i", lockout_time, reset_time, char_id, instance_id);
  5087. else if ( reset_time > 0 && reenter_time > 0 )
  5088. database_new.Query("UPDATE character_instances SET grant_reset_time_left = %i, grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i",reset_time, reenter_time, char_id, instance_id);
  5089. else if ( lockout_time > 0 && reenter_time > 0 )
  5090. database_new.Query("UPDATE character_instances SET lockout_time = %i, grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i", lockout_time, reenter_time, char_id, instance_id);
  5091. else if ( lockout_time > 0 )
  5092. database_new.Query("UPDATE character_instances SET lockout_time = %i WHERE char_id = %i AND instance_id = %i", lockout_time, char_id, instance_id);
  5093. else if ( reset_time > 0 )
  5094. database_new.Query("UPDATE character_instances SET grant_reset_time_left = %i WHERE char_id = %i AND instance_id = %i", reset_time, char_id, instance_id);
  5095. else if ( reenter_time > 0 )
  5096. database_new.Query("UPDATE character_instances SET grant_reenter_time_left = %i WHERE char_id = %i AND instance_id = %i", reenter_time, char_id, instance_id);
  5097. if( database_new.GetError() )
  5098. {
  5099. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in UpdateCharacterInstanceTimers() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5100. return false;
  5101. }
  5102. else
  5103. {
  5104. if ( database_new.AffectedRows() > 0 )
  5105. {
  5106. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Updating instance timers for character %u to instance: %u", char_id, instance_id);
  5107. LogWrite(INSTANCE__DEBUG, 1, "Instance", "-- Reenter: %u, Reset: %u, Lockout: %u", reenter_time, reset_time, lockout_time);
  5108. return true;
  5109. }
  5110. else
  5111. return false;
  5112. }
  5113. }
  5114. bool WorldDatabase::UpdateCharacterInstance(int32 char_id, string zone_name, int32 instance_id, int8 type, int32 timestamp) {
  5115. // type = 1 = success timestamp
  5116. // type = 2 = failure timestamp
  5117. if (instance_id > 0) {
  5118. if (type == 1) {
  5119. database_new.Query("UPDATE character_instances SET instance_id = %u, last_success_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", instance_id, timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  5120. }
  5121. else if (type == 2) {
  5122. database_new.Query("UPDATE character_instances SET instance_id = %u, last_failure_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", instance_id, timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  5123. }
  5124. else {
  5125. database_new.Query("UPDATE character_instances SET instance_id = %u WHERE char_id = %u AND instance_zone_name = '%s'", instance_id, char_id, database_new.EscapeStr(zone_name).c_str());
  5126. }
  5127. }
  5128. else {
  5129. if (type == 1) {
  5130. database_new.Query("UPDATE character_instances SET last_success_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  5131. }
  5132. else if (type == 2) {
  5133. database_new.Query("UPDATE character_instances SET last_failure_timestamp = %u WHERE char_id = %u AND instance_zone_name = '%s'", timestamp, char_id, database_new.EscapeStr(zone_name).c_str());
  5134. }
  5135. }
  5136. if (database_new.GetError()) {
  5137. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in UpdateCharacterInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5138. return false;
  5139. }
  5140. return true;
  5141. }
  5142. bool WorldDatabase::VerifyInstanceID(int32 char_id, int32 instance_id) {
  5143. DatabaseResult result;
  5144. database_new.Select(&result, "SELECT COUNT(id) as num_instances FROM instances WHERE id = %u", instance_id);
  5145. if (result.Next() && result.GetInt32Str("num_instances") == 0) {
  5146. DeleteCharacterFromInstance(char_id, instance_id);
  5147. return false;
  5148. }
  5149. return true;
  5150. }
  5151. bool WorldDatabase::UpdateInstancedSpawnRemoved(int32 spawn_location_entry_id, int32 spawn_type, int32 respawn_time, int32 instance_id )
  5152. {
  5153. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Updating removed spawns for instance_id: %u", instance_id);
  5154. LogWrite(INSTANCE__DEBUG, 1, "Instance", "-- Spawn Location Entry ID: %u, Type: %u, Respawn: %u", spawn_location_entry_id, spawn_type, respawn_time);
  5155. if( !database_new.Query("UPDATE instance_spawns_removed SET respawn_time = %i WHERE spawn_location_entry_id = %i AND spawn_type = %i AND instance_id = %i", respawn_time, spawn_location_entry_id, spawn_type, instance_id) )
  5156. {
  5157. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in UpdateInstancedSpawnRemoved() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5158. return false;
  5159. }
  5160. if ( database_new.AffectedRows() > 0 )
  5161. {
  5162. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Updated removed spawns for instance_id: %u", instance_id);
  5163. return true;
  5164. }
  5165. else
  5166. return false;
  5167. }
  5168. int32 WorldDatabase::CreateNewInstance(int32 zone_id)
  5169. {
  5170. int32 ret = 0;
  5171. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Creating new instance for zone: %u ", zone_id);
  5172. if( !database_new.Query("INSERT INTO instances (zone_id) VALUES (%u)", zone_id) )
  5173. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in CreateNewInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5174. else
  5175. ret = database_new.LastInsertID();
  5176. if( ret > 0 )
  5177. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Created new instance_id %u for zone: %u ", ret, zone_id);
  5178. return ret;
  5179. }
  5180. int32 WorldDatabase::CreateInstanceSpawnRemoved(int32 spawn_location_entry_id, int32 spawn_type, int32 respawn_time, int32 instance_id )
  5181. {
  5182. int32 ret = 0;
  5183. LogWrite(INSTANCE__DEBUG, 3, "Instance", "Creating new instance spawn removed entries for instance_id: %u ", instance_id);
  5184. LogWrite(INSTANCE__DEBUG, 5, "Instance", "-- Spawn Location Entry ID: %u, Type: %u, Respawn: %u", spawn_location_entry_id, spawn_type, respawn_time);
  5185. if( !database_new.Query("INSERT INTO instance_spawns_removed (spawn_location_entry_id, spawn_type, instance_id, respawn_time) values(%u, %u, %u, %u)", spawn_location_entry_id, spawn_type, instance_id, respawn_time) )
  5186. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in CreateInstanceSpawnRemoved() query '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5187. else
  5188. ret = database_new.LastInsertID();
  5189. // potentially spammy, if it calls for every spawn added. Set to level 3 or 5?
  5190. if( ret > 0 )
  5191. LogWrite(INSTANCE__DEBUG, 5, "Instance", "Created new spawn removed entry: %u for instance_id %u", ret, instance_id);
  5192. return ret;
  5193. }
  5194. bool WorldDatabase::DeleteInstance(int32 instance_id)
  5195. {
  5196. if( !database_new.Query("DELETE FROM instances WHERE id = %u", instance_id) )
  5197. {
  5198. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5199. return false;
  5200. }
  5201. /* JA: should not need this delete with FK/Constraints
  5202. if( !database_new.Query("DELETE FROM instance_spawns_removed WHERE instance_id = %u", instance_id) )
  5203. {
  5204. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5205. return false;
  5206. }
  5207. */
  5208. // Remove the instance from the character_instances table
  5209. database_new.Query("UPDATE `character_instances` SET `instance_id` = 0 WHERE `instance_id` = %u", instance_id);
  5210. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Deleted instance_id %u", instance_id);
  5211. return true;
  5212. }
  5213. bool WorldDatabase::DeleteInstanceSpawnRemoved(int32 instance_id, int32 spawn_location_entry_id)
  5214. {
  5215. if( !database_new.Query("DELETE FROM instance_spawns_removed WHERE instance_id = %u AND spawn_location_entry_id = %u", instance_id, spawn_location_entry_id) )
  5216. {
  5217. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteInstanceSpawnRemoved() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5218. return false;
  5219. }
  5220. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Deleted removed spawn: %u for instance_id %u", spawn_location_entry_id, instance_id);
  5221. return true;
  5222. }
  5223. bool WorldDatabase::DeleteCharacterFromInstance(int32 char_id, int32 instance_id)
  5224. {
  5225. LogWrite(INSTANCE__DEBUG, 0, "Instance", "Delete character %u from instance_id %u.", char_id, instance_id);
  5226. if( !database_new.Query("UPDATE `character_instances` SET `instance_id` = 0 WHERE `instance_id` = %u AND `char_id` = %u", instance_id, char_id) )
  5227. {
  5228. LogWrite(INSTANCE__ERROR, 0, "Instance", "Error in DeleteCharacterFromInstance() '%s': %i", database_new.GetErrorMsg(), database_new.GetError());
  5229. return false;
  5230. }
  5231. if ( database_new.AffectedRows() == 0 ) // didn't find an instance to delete
  5232. {
  5233. LogWrite(INSTANCE__DEBUG, 1, "Instance", "No instance_id %u for character %u to delete.", instance_id, char_id);
  5234. return false;
  5235. }
  5236. else
  5237. {
  5238. // delete entire instance if the last player has left
  5239. DatabaseResult result;
  5240. database_new.Select(&result, "SELECT count(id) as num_instances FROM character_instances where instance_id = %u",instance_id);
  5241. if(result.Next() && result.GetInt32Str("num_instances") == 0)
  5242. {
  5243. LogWrite(INSTANCE__DEBUG, 0, "Instance", "No characters in instance: Delete instance_id %u.", instance_id);
  5244. DeleteInstance(instance_id);
  5245. }
  5246. }
  5247. return true;
  5248. }
  5249. bool WorldDatabase::LoadCharacterInstances(Client* client)
  5250. {
  5251. DatabaseResult result;
  5252. DatabaseResult result2;
  5253. bool addedInstance = false;
  5254. database_new.Select(&result, "SELECT `id`, `instance_id`, `instance_zone_name`, `instance_type`, `last_success_timestamp`, `last_failure_timestamp`, `success_lockout_time`, `failure_lockout_time` FROM `character_instances` WHERE `char_id` = %u", client->GetCharacterID());
  5255. if( result.GetNumRows() > 0 )
  5256. {
  5257. while( result.Next() )
  5258. {
  5259. int32 zone_id = 0;
  5260. int32 instance_id = result.GetInt32Str("instance_id");
  5261. // If `instance_id` is greater then 0 then get the zone id with it, else get the zone id from the zone name
  5262. if (instance_id != 0) {
  5263. if (database_new.Select(&result2, "SELECT `zone_id` FROM `instances` WHERE `id` = %u", instance_id)) {
  5264. if (result2.Next())
  5265. zone_id = result2.GetInt32Str("zone_id");
  5266. }
  5267. }
  5268. if (zone_id == 0)
  5269. zone_id = GetZoneID(result.GetStringStr("instance_zone_name"));
  5270. client->GetPlayer()->GetCharacterInstances()->AddInstance(
  5271. result.GetInt32Str("id"),
  5272. instance_id,
  5273. result.GetInt32Str("last_success_timestamp"),
  5274. result.GetInt32Str("last_failure_timestamp"),
  5275. result.GetInt32Str("success_lockout_time"),
  5276. result.GetInt32Str("failure_lockout_time"),
  5277. zone_id,
  5278. result.GetInt8Str("instance_type"),
  5279. string(result.GetStringStr("instance_zone_name"))
  5280. );
  5281. addedInstance = true;
  5282. }
  5283. }
  5284. return addedInstance;
  5285. }
  5286. void WorldDatabase::UpdateLoginEquipment()
  5287. {
  5288. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Updating `character_items` CRC in %s", __FUNCTION__);
  5289. database_new.Query("UPDATE character_items SET login_checksum = CRC32(CRC32(type) + CRC32(slot) + CRC32(item_id)) WHERE `type` = 'EQUIPPED' AND ( slot <= 8 OR slot = 19 )");
  5290. }
  5291. MutexMap<int32, LoginEquipmentUpdate>* WorldDatabase::GetEquipmentUpdates()
  5292. {
  5293. DatabaseResult result;
  5294. MutexMap<int32, LoginEquipmentUpdate>* ret = 0;
  5295. LoginEquipmentUpdate update;
  5296. int32 count = 0;
  5297. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Looking for Login Appearance Updates...");
  5298. // TODO: Someday store the equipment colors in character_items, for custom colorization of gear (?)
  5299. if( database_new.Select(&result, "SELECT ci.id, ci.char_id, ia.equip_type, ia.red, ia.green, ia.blue, ia.highlight_red, ia.highlight_green, ia.highlight_blue, ci.slot FROM characters c JOIN character_items ci ON c.id = ci.char_id JOIN item_appearances ia ON ci.item_id = ia.item_id WHERE c.deleted = 0 AND ci.type = 'EQUIPPED' AND ( ci.slot <= 8 OR ci.slot = 19 ) AND ci.login_checksum <> CRC32(CRC32(`ci`.`type`) + CRC32(ci.slot) + CRC32(ci.item_id)) ORDER BY ci.char_id, ci.slot") )
  5300. {
  5301. while( result.Next() )
  5302. {
  5303. LogWrite(INIT__LOGIN_DEBUG, 5, "Login", "Found update for char_id %i!", result.GetInt32Str("char_id"));
  5304. if(!ret)
  5305. ret = new MutexMap<int32, LoginEquipmentUpdate>();
  5306. update.world_char_id = result.GetInt32Str("char_id");
  5307. update.equip_type = result.GetInt16Str("equip_type");
  5308. update.red = result.GetInt8Str("red");
  5309. update.green = result.GetInt8Str("green");
  5310. update.blue = result.GetInt8Str("blue");
  5311. update.highlight_red = result.GetInt8Str("highlight_red");
  5312. update.highlight_green = result.GetInt8Str("highlight_green");
  5313. update.highlight_blue = result.GetInt8Str("highlight_blue");
  5314. update.slot = result.GetInt8Str("slot");
  5315. ret->Put(result.GetInt32Str("id"), update);
  5316. count++;
  5317. }
  5318. }
  5319. if(count)
  5320. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Found %i Login Appearance Update%s...", count, count == 1 ? "" : "s");
  5321. return ret;
  5322. }
  5323. MutexMap<int32, LoginEquipmentUpdate>* WorldDatabase::GetEquipmentUpdates(int32 char_id)
  5324. {
  5325. DatabaseResult result;
  5326. MutexMap<int32, LoginEquipmentUpdate>* ret = 0;
  5327. LoginEquipmentUpdate update;
  5328. int32 count = 0;
  5329. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Looking for Login Appearance Updates for char_id: %u", char_id);
  5330. // TODO: Someday store the equipment colors in character_items, for custom colorization of gear (?)
  5331. if( database_new.Select(&result, "SELECT ci.id, ci.char_id, ia.equip_type, ia.red, green, ia.blue, ia.highlight_red, ia.highlight_green, ia.highlight_blue, ci.slot FROM characters c JOIN character_items ci ON c.id = ci.char_id JOIN item_appearances ia ON ci.item_id = ia.item_id WHERE c.deleted = 0 AND ci.type = 'EQUIPPED' AND ( ci.slot <= 8 OR ci.slot = 19 ) AND ci.login_checksum <> CRC32(CRC32(ci.type) + CRC32(ci.slot) + CRC32(ci.item_id)) AND ci.char_id = %u ORDER BY ci.slot", char_id) )
  5332. {
  5333. while( result.Next() )
  5334. {
  5335. LogWrite(INIT__LOGIN_DEBUG, 5, "Login", "Found update for char_id %i!", result.GetInt32Str("char_id"));
  5336. if(!ret)
  5337. ret = new MutexMap<int32, LoginEquipmentUpdate>();
  5338. update.world_char_id = char_id;
  5339. update.equip_type = result.GetInt16Str("equip_type");
  5340. update.red = result.GetInt8Str("red");
  5341. update.green = result.GetInt8Str("green");
  5342. update.blue = result.GetInt8Str("blue");
  5343. update.highlight_red = result.GetInt8Str("highlight_red");
  5344. update.highlight_green = result.GetInt8Str("highlight_green");
  5345. update.highlight_blue = result.GetInt8Str("highlight_blue");
  5346. update.slot = result.GetInt8Str("slot");
  5347. ret->Put(result.GetInt32Str("id"), update);
  5348. count++;
  5349. }
  5350. }
  5351. if(count)
  5352. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Found %i Login Appearance Update%s...", count, count == 1 ? "" : "s");
  5353. return ret;
  5354. }
  5355. void WorldDatabase::UpdateLoginZones() {
  5356. Query query;
  5357. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Updating `zones` CRC in %s", __FUNCTION__);
  5358. query.RunQuery2("UPDATE zones SET login_checksum = CRC32(CRC32(id) + CRC32(`name`) + CRC32(`file`) + CRC32(description))", Q_UPDATE);
  5359. }
  5360. MutexMap<int32, LoginZoneUpdate>* WorldDatabase::GetZoneUpdates() {
  5361. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Looking for Login Zone Updates...");
  5362. MutexMap<int32, LoginZoneUpdate>* ret = 0;
  5363. LoginZoneUpdate update;
  5364. Query query;
  5365. MYSQL_ROW row;
  5366. int32 count = 0;
  5367. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, name, description FROM zones where login_checksum != crc32(crc32(id) + crc32(name) + crc32(file) + crc32(description))");
  5368. while(result && (row = mysql_fetch_row(result))) {
  5369. if(row[0] && row[1]) {
  5370. LogWrite(INIT__LOGIN_DEBUG, 5, "Login", "Found update for zone_id %i!", atoi(row[0]));
  5371. if(!ret)
  5372. ret = new MutexMap<int32, LoginZoneUpdate>();
  5373. update.name = string(row[1]);
  5374. if(row[2])
  5375. update.description = string(row[2]);
  5376. else
  5377. update.description = "";
  5378. ret->Put(atoi(row[0]), update);
  5379. count++;
  5380. }
  5381. }
  5382. if(count)
  5383. LogWrite(INIT__LOGIN_DEBUG, 0, "Login", "Found %i Login Zone Update%s...", count, count == 1 ? "" : "s");
  5384. return ret;
  5385. }
  5386. void WorldDatabase::LoadLocationGrids(ZoneServer* zone) {
  5387. if (zone) {
  5388. Query query;
  5389. MYSQL_ROW row;
  5390. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `grid_id`, `name`, `include_y`, `discovery` FROM `locations` WHERE `zone_id`=%u", zone->GetZoneID());
  5391. while (result && (row = mysql_fetch_row(result))) {
  5392. LocationGrid* grid = new LocationGrid;
  5393. grid->id = atoul(row[0]);
  5394. grid->grid_id = atoul(row[1]);
  5395. grid->name = string(row[2]);
  5396. grid->include_y = (atoi(row[3]) == 1);
  5397. grid->discovery = (atoi(row[4]) == 1);
  5398. if (LoadLocationGridLocations(grid))
  5399. zone->AddLocationGrid(grid);
  5400. else
  5401. safe_delete(grid);
  5402. }
  5403. }
  5404. }
  5405. bool WorldDatabase::LoadLocationGridLocations(LocationGrid* grid) {
  5406. bool ret = false;
  5407. if (grid) {
  5408. Query query;
  5409. int row_count = 0;
  5410. MYSQL_ROW row;
  5411. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `x`, `y`, `z` FROM `location_details` WHERE `location_id`=%u", grid->id);
  5412. if (result) {
  5413. while (result && (row = mysql_fetch_row(result))) {
  5414. row_count++;
  5415. Location* location = new Location;
  5416. location->id = atoul(row[0]);
  5417. location->x = atof(row[1]);
  5418. location->y = atof(row[2]);
  5419. location->z = atof(row[3]);
  5420. grid->locations.Add(location);
  5421. }
  5422. ret = true;
  5423. }
  5424. if(row_count > 0 && row_count < 3)
  5425. LogWrite(WORLD__WARNING, 0, "World", "Grid '%s' only has %u location(s). A minimum of 3 is needed for a proper location based grid.", grid->name.c_str(), row_count);
  5426. }
  5427. return ret;
  5428. }
  5429. int32 WorldDatabase::CreateLocation(int32 zone_id, int32 grid_id, const char* name, bool include_y) {
  5430. int32 ret = 0;
  5431. if (name && strlen(name) > 0) {
  5432. Query query;
  5433. query.RunQuery2(Q_INSERT, "INSERT INTO `locations` (`zone_id`, `grid_id`, `name`, `include_y`) VALUES (%u, %u, '%s', %u)", zone_id, grid_id, name, include_y == true ? 1 : 0);
  5434. ret = query.GetLastInsertedID();
  5435. }
  5436. return ret;
  5437. }
  5438. bool WorldDatabase::AddLocationPoint(int32 location_id, float x, float y, float z) {
  5439. bool ret = false;
  5440. if (LocationExists(location_id)) {
  5441. Query query;
  5442. query.RunQuery2(Q_INSERT, "INSERT INTO `location_details` (`location_id`, `x`, `y`, `z`) VALUES (%u, %f, %f, %f)", location_id, x, y, z);
  5443. ret = true;
  5444. }
  5445. return ret;
  5446. }
  5447. bool WorldDatabase::DeleteLocation(int32 location_id) {
  5448. bool ret = false;
  5449. if (LocationExists(location_id)) {
  5450. Query query;
  5451. query.RunQuery2(Q_DELETE, "DELETE FROM `locations` WHERE `id`=%u", location_id);
  5452. ret = true;
  5453. }
  5454. return ret;
  5455. }
  5456. bool WorldDatabase::DeleteLocationPoint(int32 location_point_id) {
  5457. Query query;
  5458. query.RunQuery2(Q_DELETE, "DELETE FROM `location_details` WHERE `id`=%u", location_point_id);
  5459. return true;
  5460. }
  5461. void WorldDatabase::ListLocations(Client* client) {
  5462. if (client) {
  5463. Query query;
  5464. MYSQL_ROW row;
  5465. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Listing all locations:");
  5466. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `zone_id`, `grid_id`, `name` FROM `locations`");
  5467. while (result && (row = mysql_fetch_row(result))) {
  5468. int32 id = atoul(row[0]);
  5469. int32 zone_id = atoul(row[1]);
  5470. int32 grid_id = atoul(row[2]);
  5471. const char* name = row[3];
  5472. client->Message(CHANNEL_COLOR_YELLOW, "%u) Zone ID: %u Grid ID:%u Name: '%s'", id, zone_id, grid_id, name);
  5473. }
  5474. }
  5475. }
  5476. void WorldDatabase::ListLocationPoints(Client* client, int32 location_id) {
  5477. if (client) {
  5478. if (LocationExists(location_id)) {
  5479. Query query;
  5480. client->Message(CHANNEL_COLOR_YELLOW, "Listing all points for location ID %u:", location_id);
  5481. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `id`, `x`, `y`, `z` FROM `location_details` WHERE `location_id`=%u", location_id);
  5482. MYSQL_ROW row;
  5483. while (result && (row = mysql_fetch_row(result))) {
  5484. int32 id = atoul(row[0]);
  5485. float x = atof(row[1]);
  5486. float y = atof(row[2]);
  5487. float z = atof(row[3]);
  5488. client->Message(CHANNEL_COLOR_YELLOW, "%u) (%f, %f, %f)", id, x, y, z);
  5489. }
  5490. }
  5491. else
  5492. client->Message(CHANNEL_COLOR_YELLOW, "A location with ID %u does not exist", location_id);
  5493. }
  5494. }
  5495. bool WorldDatabase::LocationExists(int32 location_id) {
  5496. bool ret = false;
  5497. Query query;
  5498. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT COUNT(id) FROM `locations` WHERE `id`=%u", location_id);
  5499. MYSQL_ROW row;
  5500. if (result && (row = mysql_fetch_row(result))) {
  5501. if (atoul(row[0]) > 0)
  5502. ret = true;
  5503. }
  5504. return ret;
  5505. }
  5506. bool WorldDatabase::QueriesFromFile(const char * file) {
  5507. return database_new.QueriesFromFile(file);
  5508. }
  5509. bool WorldDatabase::CheckBannedIPs(const char* loginIP)
  5510. {
  5511. // til you build the table, all IPs are allowed
  5512. return false;
  5513. }
  5514. sint32 WorldDatabase::AddMasterTitle(const char* titleName, int8 isPrefix)
  5515. {
  5516. if(titleName == nullptr || strlen(titleName) < 1)
  5517. {
  5518. LogWrite(DATABASE__ERROR, 0, "DBNew", "AddMasterTitle called with missing titleName");
  5519. return -1;
  5520. }
  5521. Query query;
  5522. Title* title = master_titles_list.GetTitleByName(titleName);
  5523. if(title)
  5524. return title->GetID();
  5525. query.RunQuery2(Q_INSERT, "INSERT INTO titles set title='%s', prefix=%u", titleName, isPrefix);
  5526. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  5527. LogWrite(DATABASE__ERROR, 0, "Database", "Error in AddMasterTitle query '%s': %s", query.GetQuery(), query.GetError());
  5528. return false;
  5529. }
  5530. int32 last_insert_id = query.GetLastInsertedID();
  5531. if(last_insert_id > 0)
  5532. {
  5533. title = new Title;
  5534. title->SetID(last_insert_id);
  5535. title->SetName(titleName);
  5536. title->SetPrefix(isPrefix);
  5537. master_titles_list.AddTitle(title);
  5538. return (sint32)last_insert_id;
  5539. }
  5540. return -1;
  5541. }
  5542. void WorldDatabase::LoadTitles(){
  5543. Query query;
  5544. MYSQL_ROW row;
  5545. int32 count = 0;
  5546. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, title, prefix FROM titles");
  5547. if(result && mysql_num_rows(result) > 0){
  5548. Title* title = 0;
  5549. while(result && (row = mysql_fetch_row(result))){
  5550. sint32 idx = atoi(row[0]);
  5551. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading Title '%s' (%u), Prefix: %i, Index: %u", row[1], idx, atoi(row[2]), count);
  5552. title = new Title;
  5553. title->SetID(idx);
  5554. title->SetName(row[1]);
  5555. title->SetPrefix(atoi(row[2]));
  5556. master_titles_list.AddTitle(title);
  5557. count++;
  5558. }
  5559. }
  5560. LogWrite(WORLD__DEBUG, 0, "World", "\tLoaded %u Title%s", count, count == 1 ? "" : "s");
  5561. }
  5562. sint32 WorldDatabase::LoadCharacterTitles(int32 char_id, Player *player){
  5563. LogWrite(WORLD__DEBUG, 0, "World", "Loading Titles for player '%s'...", player->GetName());
  5564. Query query;
  5565. MYSQL_ROW row;
  5566. sint32 index = 0;
  5567. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT title_id, title, prefix FROM character_titles, titles WHERE character_titles.title_id = titles.id AND character_titles.char_id = %u", char_id);
  5568. if(result && mysql_num_rows(result) > 0){
  5569. while(result && (row = mysql_fetch_row(result))){
  5570. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading Title ID: %u, Title: '%s' Index: %u", atoul(row[0]), row[1], index);
  5571. player->AddTitle(index, row[1], atoi(row[2]));
  5572. index++;
  5573. }
  5574. }
  5575. return index;
  5576. }
  5577. sint32 WorldDatabase::GetCharPrefixIndex(int32 char_id, Player *player){
  5578. LogWrite(PLAYER__DEBUG, 0, "Player", "Getting current title index for player '%s'...", player->GetName());
  5579. Query query;
  5580. MYSQL_ROW row;
  5581. sint32 ret = 0;
  5582. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT prefix_title FROM character_details WHERE char_id = %u", char_id);
  5583. if(result && mysql_num_rows(result) > 0)
  5584. while(result && (row = mysql_fetch_row(result))){
  5585. ret = atoi(row[0]);
  5586. LogWrite(PLAYER__DEBUG, 5, "Player", "\tPrefix Index: %i", ret);
  5587. }
  5588. return ret;
  5589. }
  5590. sint32 WorldDatabase::GetCharSuffixIndex(int32 char_id, Player *player){
  5591. LogWrite(PLAYER__DEBUG, 0, "Player", "Getting current title index for player '%s'...", player->GetName());
  5592. Query query;
  5593. MYSQL_ROW row;
  5594. sint32 ret = 0;
  5595. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT suffix_title FROM character_details WHERE char_id = %u", char_id);
  5596. if(result && mysql_num_rows(result) > 0)
  5597. while(result && (row = mysql_fetch_row(result))){
  5598. ret = atoi(row[0]);
  5599. LogWrite(PLAYER__DEBUG, 5, "Player", "\tSuffix Index: %i", ret);
  5600. }
  5601. return ret;
  5602. }
  5603. void WorldDatabase::SaveCharPrefixIndex(sint32 index, int32 char_id){
  5604. Query query;
  5605. query.RunQuery2(Q_UPDATE, "UPDATE character_details SET prefix_title = %i WHERE char_id = %u", index, char_id);
  5606. LogWrite(PLAYER__DEBUG, 0, "Player", "Saving Prefix Index %i for character id '%u'...", index, char_id);
  5607. }
  5608. void WorldDatabase::SaveCharSuffixIndex(sint32 index, int32 char_id){
  5609. Query query;
  5610. query.RunQuery2(Q_SELECT, "UPDATE character_details SET suffix_title = %i WHERE char_id = %u", index, char_id);
  5611. LogWrite(PLAYER__DEBUG, 0, "Player", "Saving Suffix Index %i for character id %u...", index, char_id);
  5612. }
  5613. sint32 WorldDatabase::AddCharacterTitle(sint32 index, int32 char_id, Spawn* player) {
  5614. if(!player || !player->IsPlayer())
  5615. {
  5616. LogWrite(DATABASE__ERROR, 0, "DBNew", "AddCharacterTitle spawn is not a player: %s", player ? player->GetName() : "Unset");
  5617. return -1;
  5618. }
  5619. Title* title = master_titles_list.GetTitle(index);
  5620. if(!title)
  5621. {
  5622. LogWrite(DATABASE__ERROR, 0, "DBNew", "AddCharacterTitle title index %u missing from master_titles_list for player: %s (%u)", index, player ? player->GetName() : "Unset", char_id);
  5623. return -1;
  5624. }
  5625. Query query;
  5626. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding titles for char_id: %u, index: %i", char_id, index);
  5627. query.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_titles (char_id, title_id) VALUES (%u, %i)", char_id, index);
  5628. sint32 curIndex = (sint32)((Player*)player)->GetPlayerTitles()->Size();
  5629. ((Player*)player)->AddTitle(curIndex++, title->GetName(), title->GetPrefix(), title->GetSaveNeeded());
  5630. return curIndex;
  5631. }
  5632. void WorldDatabase::LoadLanguages()
  5633. {
  5634. int32 count = 0;
  5635. Query query;
  5636. MYSQL_ROW row;
  5637. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, language FROM languages");
  5638. if(result && mysql_num_rows(result) > 0)
  5639. {
  5640. Language* language = 0;
  5641. while(result && (row = mysql_fetch_row(result)))
  5642. {
  5643. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading language '%s' , ID: %u", row[1], atoul(row[0]));
  5644. language = new Language;
  5645. language->SetID(atoul(row[0]));
  5646. language->SetName(row[1]);
  5647. master_languages_list.AddLanguage(language);
  5648. count++;
  5649. }
  5650. }
  5651. LogWrite(WORLD__DEBUG, 0, "World", "\tLoaded %u Language%s", count, count == 1 ? "" : "s");
  5652. }
  5653. int32 WorldDatabase::LoadCharacterLanguages(int32 char_id, Player *player)
  5654. {
  5655. LogWrite(WORLD__DEBUG, 0, "World", "Loading Languages for player '%s'...", player->GetName());
  5656. Query query;
  5657. MYSQL_ROW row;
  5658. int32 count = 0;
  5659. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT language_id, language FROM character_languages, languages WHERE character_languages.language_id = languages.id AND character_languages.char_id = %u", char_id);
  5660. if(result && mysql_num_rows(result) > 0)
  5661. {
  5662. while(result && (row = mysql_fetch_row(result)))
  5663. {
  5664. LogWrite(WORLD__DEBUG, 5, "World", "\tLoading Language ID: %u, Language: '%s'", atoul(row[0]), row[1]);
  5665. player->AddLanguage(atoul(row[0]), row[1]);
  5666. count++;
  5667. }
  5668. }
  5669. return count;
  5670. }
  5671. int16 WorldDatabase::GetCharacterCurrentLang(int32 char_id, Player *player)
  5672. {
  5673. LogWrite(PLAYER__DEBUG, 0, "Player", "Getting current language for player '%s'...", player->GetName());
  5674. Query query;
  5675. MYSQL_ROW row;
  5676. int16 ret = 0;
  5677. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT current_language FROM character_details WHERE char_id = %u", char_id);
  5678. if(result && mysql_num_rows(result) > 0)
  5679. while(result && (row = mysql_fetch_row(result)))
  5680. {
  5681. ret = atoi(row[0]);
  5682. LogWrite(PLAYER__DEBUG, 5, "Player", "\tLanguage ID: %i", ret);
  5683. }
  5684. return ret;
  5685. }
  5686. void WorldDatabase::SaveCharacterCurrentLang(int32 id, int32 char_id, Client *client)
  5687. {
  5688. Query query;
  5689. query.RunQuery2(Q_UPDATE, "UPDATE character_details SET current_language = %i WHERE char_id = %u", id, char_id);
  5690. LogWrite(PLAYER__DEBUG, 3, "Player", "Saving current language ID %i for player '%s'...", id, client->GetPlayer()->GetName());
  5691. }
  5692. void WorldDatabase::SaveCharacterLang(int32 char_id, int32 lang_id) {
  5693. if (!database_new.Query("INSERT INTO character_languages (char_id, language_id) VALUES (%u, %u)", char_id, lang_id))
  5694. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5695. }
  5696. // JA - this is not used yet, lots more to consider for storing player history
  5697. void WorldDatabase::LoadCharacterHistory(int32 char_id, Player *player)
  5698. {
  5699. DatabaseResult result;
  5700. // Use -1 on type and subtype to turn the enum into an int and make it a 0 index
  5701. if (!database_new.Select(&result, "SELECT type-1, subtype-1, value, value2, location, event_id, event_date FROM character_history WHERE char_id = %u", char_id)) {
  5702. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  5703. return;
  5704. }
  5705. while (result.Next()) {
  5706. int8 type = result.GetInt8(0);
  5707. int8 subtype = result.GetInt8(1);
  5708. HistoryData* hd = new HistoryData;
  5709. hd->Value = result.GetInt32(2);
  5710. hd->Value2 = result.GetInt32(3);
  5711. strcpy(hd->Location, result.GetString(4));
  5712. // skipped event id as use for it has not been determined yet
  5713. hd->EventDate = result.GetInt32(6);
  5714. hd->needs_save = false;
  5715. player->LoadPlayerHistory(type, subtype, hd);
  5716. }
  5717. }
  5718. void WorldDatabase::LoadSpellErrors() {
  5719. Query query;
  5720. MYSQL_ROW row;
  5721. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `version`, `error_index`, `value` FROM `spell_error_versions`");
  5722. if (result && mysql_num_rows(result) > 0) {
  5723. while ((row = mysql_fetch_row(result))) {
  5724. master_spell_list.AddSpellError(atoi(row[0]), atoi(row[1]), atoi(row[2]));
  5725. }
  5726. }
  5727. }
  5728. void WorldDatabase::SaveCharacterHistory(Player* player, int8 type, int8 subtype, int32 value, int32 value2, char* location, int32 event_date) {
  5729. string str_type("");
  5730. string str_subtype("");
  5731. switch (type) {
  5732. case HISTORY_TYPE_NONE:
  5733. str_type = "None";
  5734. break;
  5735. case HISTORY_TYPE_DEATH:
  5736. str_type = "Death";
  5737. break;
  5738. case HISTORY_TYPE_DISCOVERY:
  5739. str_type = "Discovery";
  5740. break;
  5741. case HISTORY_TYPE_XP:
  5742. str_type = "XP";
  5743. break;
  5744. default:
  5745. LogWrite(PLAYER__ERROR, 0, "Player", "WorldDatabase::SaveCharacterHistory() - Invalid history type given (%i) with subtype (%i), character history NOT saved.", type, subtype);
  5746. return;
  5747. }
  5748. switch (subtype) {
  5749. case HISTORY_SUBTYPE_NONE:
  5750. str_subtype = "None";
  5751. break;
  5752. case HISTORY_SUBTYPE_ADVENTURE:
  5753. str_subtype = "Adventure";
  5754. break;
  5755. case HISTORY_SUBTYPE_TRADESKILL:
  5756. str_subtype = "Tradeskill";
  5757. break;
  5758. case HISTORY_SUBTYPE_QUEST:
  5759. str_subtype = "Quest";
  5760. break;
  5761. case HISTORY_SUBTYPE_AA:
  5762. str_subtype = "AA";
  5763. break;
  5764. case HISTORY_SUBTYPE_ITEM:
  5765. str_subtype = "Item";
  5766. break;
  5767. case HISTORY_SUBTYPE_LOCATION:
  5768. str_subtype = "Location";
  5769. break;
  5770. default:
  5771. LogWrite(PLAYER__ERROR, 0, "Player", "WorldDatabase::SaveCharacterHistory() - Invalid history sub type given (%i) with type (%i), character history NOT saved.", subtype, type);
  5772. return;
  5773. }
  5774. LogWrite(PLAYER__DEBUG, 1, "Player", "Saving character history, type = %s (%i) subtype = %s (%i)", (char*)str_type.c_str(), type, (char*)str_subtype.c_str(), subtype);
  5775. Query query;
  5776. query.AddQueryAsync(player->GetCharacterID(), this, Q_REPLACE, "replace into character_history (char_id, type, subtype, value, value2, location, event_date) values (%u, '%s', '%s', %i, %i, '%s', %u)",
  5777. player->GetCharacterID(), str_type.c_str(), str_subtype.c_str(), value, value2, getSafeEscapeString(location).c_str(), event_date);
  5778. }
  5779. void WorldDatabase::LoadTransportMaps(ZoneServer* zone) {
  5780. int32 total = 0;
  5781. LogWrite(TRANSPORT__DEBUG, 0, "Transport", "-Loading Transporter Maps...");
  5782. zone->DeleteTransporterMaps();
  5783. Query query;
  5784. MYSQL_ROW row;
  5785. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT `transport_id`, `map_name` FROM `transport_maps`");
  5786. if(result) {
  5787. while(result && (row = mysql_fetch_row(result))){
  5788. zone->AddTransportMap(atoul(row[0]), string(row[1]));
  5789. total++;
  5790. }
  5791. }
  5792. LogWrite(TRANSPORT__DEBUG, 0, "Transport", "--Loaded %i Transporter Maps", total);
  5793. }
  5794. bool WorldDatabase::LoadSign(ZoneServer* zone, int32 spawn_id) {
  5795. Sign* sign = 0;
  5796. int32 id = 0;
  5797. DatabaseResult result;
  5798. database_new.Select(&result, "SELECT ss.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, ss.widget_id, ss.widget_x, ss.widget_y, ss.widget_z, s.command_primary, s.command_secondary, s.collision_radius, ss.icon, ss.type, ss.title, ss.description, ss.sign_distance, ss.zone_id, ss.zone_x, ss.zone_y, ss.zone_z, ss.zone_heading, ss.include_heading, ss.include_location, s.transport_id, s.size_offset, s.display_hand_icon, s.visual_state, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, s.loot_tier, s.loot_drop_type, ss.language\n"
  5799. "FROM spawn s\n"
  5800. "INNER JOIN spawn_signs ss\n"
  5801. "ON ss.spawn_id = s.id\n"
  5802. "WHERE s.id = %u\n",
  5803. spawn_id);
  5804. if (result.GetNumRows() > 0 && result.Next()) {
  5805. id = result.GetInt32(0);
  5806. sign = new Sign();
  5807. sign->SetDatabaseID(id);
  5808. strcpy(sign->appearance.name, result.GetString(1));
  5809. sign->appearance.model_type = result.GetInt16(2);
  5810. sign->SetSize(result.GetInt16(3));
  5811. sign->appearance.show_command_icon = result.GetInt8(4);
  5812. sign->SetWidgetID(result.GetInt32(5));
  5813. sign->SetWidgetX(result.GetFloat(6));
  5814. sign->SetWidgetY(result.GetFloat(7));
  5815. sign->SetWidgetZ(result.GetFloat(8));
  5816. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(9));
  5817. if(primary_command_list){
  5818. sign->SetPrimaryCommands(primary_command_list);
  5819. sign->primary_command_list_id = result.GetInt32(9);
  5820. }
  5821. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(10));
  5822. if (secondary_command_list) {
  5823. sign->SetSecondaryCommands(secondary_command_list);
  5824. sign->secondary_command_list_id = result.GetInt32(10);
  5825. }
  5826. sign->appearance.pos.collision_radius = result.GetInt16(11);
  5827. sign->SetSignIcon(result.GetInt8(12));
  5828. if(strncasecmp(result.GetString(13), "Generic", 7) == 0)
  5829. sign->SetSignType(SIGN_TYPE_GENERIC);
  5830. else if(strncasecmp(result.GetString(13), "Zone", 4) == 0)
  5831. sign->SetSignType(SIGN_TYPE_ZONE);
  5832. sign->SetSignTitle(result.GetString(14));
  5833. sign->SetSignDescription(result.GetString(15));
  5834. sign->SetSignDistance(result.GetFloat(16));
  5835. sign->SetSignZoneID(result.GetInt32(17));
  5836. sign->SetSignZoneX(result.GetFloat(18));
  5837. sign->SetSignZoneY(result.GetFloat(19));
  5838. sign->SetSignZoneZ(result.GetFloat(20));
  5839. sign->SetSignZoneHeading(result.GetFloat(21));
  5840. sign->SetIncludeHeading(result.GetInt8(22) == 1);
  5841. sign->SetIncludeLocation(result.GetInt8(23) == 1);
  5842. sign->SetTransporterID(result.GetInt32(24));
  5843. sign->SetSizeOffset(result.GetInt8(25));
  5844. sign->appearance.display_hand_icon = result.GetInt8(26);
  5845. sign->SetVisualState(result.GetInt16(27));
  5846. sign->SetSoundsDisabled(result.GetInt8(28));
  5847. sign->SetMerchantLevelRange(result.GetInt32(29), result.GetInt32(30));
  5848. sign->SetAAXPRewards(result.GetInt32(31));
  5849. sign->SetLootTier(result.GetInt32(32));
  5850. sign->SetLootDropType(result.GetInt32(33));
  5851. sign->SetLanguage(result.GetInt8(34));
  5852. zone->AddSign(id, sign);
  5853. LogWrite(SIGN__DEBUG, 0, "Sign", "Loaded Sign: '%s' (%u).", sign->appearance.name, spawn_id);
  5854. return true;
  5855. }
  5856. LogWrite(SIGN__DEBUG, 0, "Sign", "Unable to find a sign for spawn id of %u", spawn_id);
  5857. return false;
  5858. }
  5859. bool WorldDatabase::LoadWidget(ZoneServer* zone, int32 spawn_id) {
  5860. Widget* widget = 0;
  5861. int32 id = 0;
  5862. DatabaseResult result;
  5863. database_new.Select(&result, "SELECT sw.spawn_id, s.name, s.model_type, s.size, s.show_command_icon, sw.widget_id, sw.widget_x, sw.widget_y, sw.widget_z, s.command_primary, s.command_secondary, s.collision_radius, sw.include_heading, sw.include_location, sw.icon, sw.type, sw.open_heading, sw.open_y, sw.action_spawn_id, sw.open_sound_file, sw.close_sound_file, sw.open_duration, sw.closed_heading, sw.linked_spawn_id, sw.close_y, s.transport_id, s.size_offset, sw.house_id, sw.open_x, sw.open_z, sw.close_x, sw.close_z, s.display_hand_icon, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, s.loot_tier, s.loot_drop_type\n"
  5864. "FROM spawn s\n"
  5865. "INNER JOIN spawn_widgets sw\n"
  5866. "ON sw.spawn_id = s.id\n"
  5867. "WHERE s.id = %u",
  5868. spawn_id);
  5869. if (result.GetNumRows() > 0 && result.Next()) {
  5870. id = result.GetInt32(0);
  5871. widget = new Widget();
  5872. widget->SetDatabaseID(id);
  5873. strcpy(widget->appearance.name, result.GetString(1));
  5874. widget->appearance.model_type = result.GetInt16(2);
  5875. widget->SetSize(result.GetInt16(3));
  5876. widget->appearance.show_command_icon = result.GetInt8(4);
  5877. widget->SetWidgetID(result.GetInt32(5));
  5878. widget->SetWidgetX(result.GetFloat(6));
  5879. widget->SetWidgetY(result.GetFloat(7));
  5880. widget->SetWidgetZ(result.GetFloat(8));
  5881. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(9));
  5882. if(primary_command_list){
  5883. widget->SetPrimaryCommands(primary_command_list);
  5884. widget->primary_command_list_id = result.GetInt32(9);
  5885. }
  5886. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(10));
  5887. if (secondary_command_list) {
  5888. widget->SetSecondaryCommands(secondary_command_list);
  5889. widget->secondary_command_list_id = result.GetInt32(10);
  5890. }
  5891. widget->appearance.pos.collision_radius = result.GetInt16(11);
  5892. widget->SetIncludeHeading(result.GetInt8(12) == 1);
  5893. widget->SetIncludeLocation(result.GetInt8(13) == 1);
  5894. widget->SetWidgetIcon(result.GetInt8(14));
  5895. if(strncasecmp(result.GetString(15),"Generic", 7) == 0)
  5896. widget->SetWidgetType(WIDGET_TYPE_GENERIC);
  5897. else if(strncasecmp(result.GetString(15),"Door", 4) == 0)
  5898. widget->SetWidgetType(WIDGET_TYPE_DOOR);
  5899. widget->SetOpenHeading(result.GetFloat(16));
  5900. widget->SetOpenY(result.GetFloat(17));
  5901. widget->SetActionSpawnID(result.GetInt32(18));
  5902. if(!result.IsNull(19) && strlen(result.GetString(19)) > 5)
  5903. widget->SetOpenSound(result.GetString(19));
  5904. if(!result.IsNull(20) && strlen(result.GetString(20)) > 5)
  5905. widget->SetCloseSound(result.GetString(20));
  5906. widget->SetOpenDuration(result.GetInt16(21));
  5907. widget->SetClosedHeading(result.GetFloat(22));
  5908. widget->SetLinkedSpawnID(result.GetInt32(23));
  5909. widget->SetCloseY(result.GetFloat(24));
  5910. widget->SetTransporterID(result.GetInt32(25));
  5911. widget->SetSizeOffset(result.GetInt8(26));
  5912. widget->SetHouseID(result.GetInt32(27));
  5913. widget->SetOpenX(result.GetFloat(28));
  5914. widget->SetOpenZ(result.GetFloat(29));
  5915. widget->SetCloseX(result.GetFloat(30));
  5916. widget->SetCloseZ(result.GetFloat(31));
  5917. widget->appearance.display_hand_icon = result.GetInt8(32);
  5918. widget->SetSoundsDisabled(result.GetInt8(33));
  5919. widget->SetMerchantLevelRange(result.GetInt32(34), result.GetInt32(35));
  5920. widget->SetAAXPRewards(result.GetInt32(36));
  5921. widget->SetLootTier(result.GetInt32(37));
  5922. widget->SetLootDropType(result.GetInt32(38));
  5923. zone->AddWidget(id, widget);
  5924. LogWrite(WIDGET__DEBUG, 0, "Widget", "Loaded Widget: '%s' (%u).", widget->appearance.name, spawn_id);
  5925. return true;
  5926. }
  5927. LogWrite(WIDGET__DEBUG, 0, "Widget", "Unable to find a widget for spawn id of %u", spawn_id);
  5928. return false;
  5929. }
  5930. bool WorldDatabase::LoadObject(ZoneServer* zone, int32 spawn_id) {
  5931. Object* object = 0;
  5932. int32 id = 0;
  5933. DatabaseResult result;
  5934. database_new.Select(&result, "SELECT so.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, s.transport_id, s.size_offset, so.device_id, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, s.loot_tier, s.loot_drop_type\n"
  5935. "FROM spawn s\n"
  5936. "INNER JOIN spawn_objects so\n"
  5937. "ON so.spawn_id = s.id\n"
  5938. "WHERE s.id = %u",
  5939. spawn_id);
  5940. if (result.GetNumRows() > 0 && result.Next()) {
  5941. id = result.GetInt32(0);
  5942. object = new Object();
  5943. object->SetDatabaseID(id);
  5944. strcpy(object->appearance.name, result.GetString(1));
  5945. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(4));
  5946. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(5));
  5947. if(primary_command_list){
  5948. object->SetPrimaryCommands(primary_command_list);
  5949. object->primary_command_list_id = result.GetInt32(4);
  5950. }
  5951. if(secondary_command_list){
  5952. object->SetSecondaryCommands(secondary_command_list);
  5953. object->secondary_command_list_id = result.GetInt32(5);
  5954. }
  5955. object->appearance.race = result.GetInt8(2);
  5956. object->appearance.model_type = result.GetInt16(3);
  5957. object->appearance.targetable = result.GetInt8(6);
  5958. object->size = result.GetInt16(7);
  5959. object->appearance.display_name = result.GetInt8(8);
  5960. object->appearance.visual_state = result.GetInt16(9);
  5961. object->appearance.attackable = result.GetInt8(10);
  5962. object->appearance.show_level = result.GetInt8(11);
  5963. object->appearance.show_command_icon = result.GetInt8(12);
  5964. object->appearance.display_hand_icon = result.GetInt8(13);
  5965. object->faction_id = result.GetInt32(14);
  5966. object->appearance.pos.collision_radius = result.GetInt16(15);
  5967. object->SetTransporterID(result.GetInt32(16));
  5968. object->SetSizeOffset(result.GetInt8(17));
  5969. object->SetDeviceID(result.GetInt8(18));
  5970. object->SetSoundsDisabled(result.GetInt8(19));
  5971. object->SetMerchantLevelRange(result.GetInt32(20), result.GetInt32(21));
  5972. object->SetAAXPRewards(result.GetInt32(22));
  5973. object->SetLootTier(result.GetInt32(23));
  5974. object->SetLootDropType(result.GetInt32(24));
  5975. zone->AddObject(id, object);
  5976. LogWrite(OBJECT__DEBUG, 0, "Object", "Loaded Object: '%s' (%u).", object->appearance.name, spawn_id);
  5977. return true;
  5978. }
  5979. LogWrite(OBJECT__DEBUG, 0, "Object", "Unable to find an object for spawn id of %u", spawn_id);
  5980. return false;
  5981. }
  5982. bool WorldDatabase::LoadGroundSpawn(ZoneServer* zone, int32 spawn_id) {
  5983. GroundSpawn* spawn = 0;
  5984. int32 id = 0;
  5985. DatabaseResult result;
  5986. database_new.Select(&result, "SELECT sg.spawn_id, s.name, s.race, s.model_type, s.command_primary, s.command_secondary, s.targetable, s.size, s.show_name, s.visual_state, s.attackable, s.show_level, s.show_command_icon, s.display_hand_icon, s.faction_id, s.collision_radius, sg.number_harvests, sg.num_attempts_per_harvest, sg.groundspawn_id, sg.collection_skill, s.size_offset, s.disable_sounds, s.aaxp_rewards, s.loot_tier, s.loot_drop_type, sg.randomize_heading\n"
  5987. "FROM spawn s\n"
  5988. "INNER JOIN spawn_ground sg\n"
  5989. "ON sg.spawn_id = s.id\n"
  5990. "WHERE s.id = %u",
  5991. spawn_id);
  5992. if (result.GetNumRows() > 0 && result.Next()) {
  5993. id = result.GetInt32(0);
  5994. spawn = new GroundSpawn();
  5995. spawn->SetDatabaseID(id);
  5996. strcpy(spawn->appearance.name, result.GetString(1));
  5997. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(4));
  5998. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(5));
  5999. if(primary_command_list){
  6000. spawn->SetPrimaryCommands(primary_command_list);
  6001. spawn->primary_command_list_id = result.GetInt32(4);
  6002. }
  6003. if(secondary_command_list){
  6004. spawn->SetSecondaryCommands(secondary_command_list);
  6005. spawn->secondary_command_list_id = result.GetInt32(5);
  6006. }
  6007. spawn->appearance.race = result.GetInt8(2);
  6008. spawn->appearance.model_type = result.GetInt16(3);
  6009. spawn->appearance.targetable = result.GetInt8(6);
  6010. spawn->size = result.GetInt16(7);
  6011. spawn->appearance.display_name = result.GetInt8(8);
  6012. spawn->appearance.visual_state = result.GetInt16(9);
  6013. spawn->appearance.attackable = result.GetInt8(10);
  6014. spawn->appearance.show_level = result.GetInt8(11);
  6015. spawn->appearance.show_command_icon = result.GetInt8(12);
  6016. spawn->appearance.display_hand_icon = result.GetInt8(13);
  6017. spawn->faction_id = result.GetInt32(14);
  6018. spawn->appearance.pos.collision_radius = result.GetInt16(15);
  6019. spawn->SetNumberHarvests(result.GetInt8(16));
  6020. spawn->SetAttemptsPerHarvest(result.GetInt8(17));
  6021. spawn->SetGroundSpawnEntryID(result.GetInt32(18));
  6022. spawn->SetCollectionSkill(result.GetString(19));
  6023. spawn->SetSizeOffset(result.GetInt8(20));
  6024. spawn->SetSoundsDisabled(result.GetInt8(21));
  6025. spawn->SetAAXPRewards(result.GetInt32(22));
  6026. spawn->SetLootTier(result.GetInt32(23));
  6027. spawn->SetLootDropType(result.GetInt32(24));
  6028. spawn->SetRandomizeHeading(result.GetInt32(25));
  6029. zone->AddGroundSpawn(id, spawn);
  6030. if (!zone->GetGroundSpawnEntries(spawn->GetGroundSpawnEntryID()))
  6031. LoadGroundSpawnEntry(zone, spawn->GetGroundSpawnEntryID());
  6032. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "Loaded Ground Spawn: '%s' (%u).", spawn->appearance.name, spawn_id);
  6033. return true;
  6034. }
  6035. LogWrite(GROUNDSPAWN__DEBUG, 0, "GSpawn", "Unable to find a ground spawn for spawn id of %u", spawn_id);
  6036. return false;
  6037. }
  6038. void WorldDatabase::LoadGroundSpawnItems(ZoneServer* zone, int32 entry_id) {
  6039. DatabaseResult result;
  6040. database_new.Select(&result, "SELECT item_id, is_rare, grid_id\n"
  6041. "FROM groundspawn_items\n"
  6042. "WHERE groundspawn_id = %u",
  6043. entry_id);
  6044. if (result.GetNumRows() > 0 && result.Next()) {
  6045. zone->AddGroundSpawnItem(entry_id, result.GetInt32(0), result.GetInt8(1), result.GetInt32(2));
  6046. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn Items: ID: %u\n", entry_id);
  6047. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---item: %ul, rare: %i, grid: %ul", result.GetInt32(0), result.GetInt8(1), result.GetInt32(2));
  6048. }
  6049. }
  6050. void WorldDatabase::LoadGroundSpawnEntry(ZoneServer* zone, int32 entry_id) {
  6051. DatabaseResult result;
  6052. database_new.Select(&result, "SELECT min_skill_level, min_adventure_level, bonus_table, harvest1, harvest3, harvest5, harvest_imbue, harvest_rare, harvest10, harvest_coin\n"
  6053. "FROM groundspawns\n"
  6054. "WHERE enabled = 1 AND groundspawn_id = %u",
  6055. entry_id);
  6056. if (result.GetNumRows() > 0 && result.Next()) {
  6057. // this is getting ridonkulous...
  6058. LogWrite(GROUNDSPAWN__DEBUG, 5, "GSpawn", "---Loading GroundSpawn ID: %u\n" \
  6059. "---min_skill_level: %i, min_adventure_level: %i, bonus_table: %i\n" \
  6060. "---harvest1: %.2f, harvest3: %.2f, harvest5: %.2f\n" \
  6061. "---harvest_imbue: %.2f, harvest_rare: %.2f, harvest10: %.2f\n" \
  6062. "---harvest_coin: %u", entry_id, result.GetInt16(0), result.GetInt16(1), result.GetInt8(2), result.GetFloat(3), result.GetFloat(4), result.GetFloat(5), result.GetFloat(6), result.GetFloat(7), result.GetFloat(8), result.GetInt32(9));
  6063. zone->AddGroundSpawnEntry(entry_id, result.GetInt16(0), result.GetInt16(1), result.GetInt8(2), result.GetFloat(3), result.GetFloat(4), result.GetFloat(5), result.GetFloat(6), result.GetFloat(7), result.GetFloat(8), result.GetInt32(9));
  6064. LoadGroundSpawnItems(zone, entry_id);
  6065. }
  6066. }
  6067. bool WorldDatabase::LoadNPC(ZoneServer* zone, int32 spawn_id) {
  6068. NPC* npc = nullptr;
  6069. int32 id = 0;
  6070. DatabaseResult result;
  6071. database_new.Select(&result, "SELECT npc.spawn_id, s.name, npc.min_level, npc.max_level, npc.enc_level, s.race, s.model_type, npc.class_, npc.gender, s.command_primary, s.command_secondary, s.show_name, npc.min_group_size, npc.max_group_size, npc.hair_type_id, npc.facial_hair_type_id, npc.wing_type_id, npc.chest_type_id, npc.legs_type_id, npc.soga_hair_type_id, npc.soga_facial_hair_type_id, s.attackable, s.show_level, s.targetable, s.show_command_icon, s.display_hand_icon, s.hp, s.power, s.size, s.collision_radius, npc.action_state, s.visual_state, npc.mood_state, npc.initial_state, npc.activity_status, s.faction_id, s.sub_title, s.merchant_id, s.merchant_type, s.size_offset, npc.attack_type, npc.ai_strategy+0, npc.spell_list_id, npc.secondary_spell_list_id, npc.skill_list_id, npc.secondary_skill_list_id, npc.equipment_list_id, npc.str, npc.sta, npc.wis, npc.intel, npc.agi, npc.heat, npc.cold, npc.magic, npc.mental, npc.divine, npc.disease, npc.poison, npc.aggro_radius, npc.cast_percentage, npc.randomize, npc.soga_model_type, npc.heroic_flag, npc.alignment, npc.elemental, npc.arcane, npc.noxious, s.savagery, s.dissonance, npc.hide_hood, npc.emote_state, s.prefix, s.suffix, s.last_name, s.disable_sounds, s.merchant_min_level, s.merchant_max_level, s.aaxp_rewards, s.loot_tier, s.loot_drop_type, npc.scared_by_strong_players, npc.action_state_str\n"
  6072. "FROM spawn s\n"
  6073. "INNER JOIN spawn_npcs npc\n"
  6074. "ON npc.spawn_id = s.id\n"
  6075. "WHERE s.id = %u",
  6076. spawn_id);
  6077. if (result.GetNumRows() > 0 && result.Next()) {
  6078. id = result.GetInt32(0);
  6079. npc = new NPC();
  6080. npc->SetDatabaseID(id);
  6081. strcpy(npc->appearance.name, result.GetString(1));
  6082. vector<EntityCommand*>* primary_command_list = zone->GetEntityCommandList(result.GetInt32(9));
  6083. vector<EntityCommand*>* secondary_command_list = zone->GetEntityCommandList(result.GetInt32(10));
  6084. if(primary_command_list){
  6085. npc->SetPrimaryCommands(primary_command_list);
  6086. npc->primary_command_list_id = result.GetInt32(9);
  6087. }
  6088. if(secondary_command_list){
  6089. npc->SetSecondaryCommands(secondary_command_list);
  6090. npc->secondary_command_list_id = result.GetInt32(10);
  6091. }
  6092. npc->appearance.min_level = result.GetInt8(2);
  6093. npc->appearance.max_level = result.GetInt8(3);
  6094. npc->appearance.level = result.GetInt8(2);
  6095. npc->appearance.difficulty = result.GetInt8(4);
  6096. npc->appearance.race = result.GetInt8(5);
  6097. npc->appearance.model_type = result.GetInt16(6);
  6098. npc->appearance.soga_model_type = result.GetInt16(62);
  6099. npc->appearance.adventure_class = result.GetInt8(7);
  6100. npc->appearance.gender = result.GetInt8(8);
  6101. npc->appearance.display_name = result.GetInt8(11);
  6102. npc->features.hair_type = result.GetInt16(14);
  6103. npc->features.hair_face_type = result.GetInt16(15);
  6104. npc->features.wing_type = result.GetInt16(16);
  6105. npc->features.chest_type = result.GetInt16(17);
  6106. npc->features.legs_type = result.GetInt16(18);
  6107. npc->features.soga_hair_type = result.GetInt16(19);
  6108. npc->features.soga_hair_face_type = result.GetInt16(20);
  6109. npc->appearance.attackable = result.GetInt8(21);
  6110. npc->appearance.show_level = result.GetInt8(22);
  6111. npc->appearance.targetable = result.GetInt8(23);
  6112. npc->appearance.show_command_icon = result.GetInt8(24);
  6113. npc->appearance.display_hand_icon = result.GetInt8(25);
  6114. npc->appearance.hide_hood = result.GetInt8(70);
  6115. npc->appearance.randomize = result.GetInt32(61);
  6116. npc->SetTotalHP(result.GetInt32(26));
  6117. npc->SetTotalPower(result.GetInt32(27));
  6118. npc->SetHP(npc->GetTotalHP());
  6119. npc->SetPower(npc->GetTotalPower());
  6120. if(npc->GetTotalHP() == 0){
  6121. npc->SetTotalHP(15*npc->GetLevel() + 1);
  6122. npc->SetHP(15*npc->GetLevel() + 1);
  6123. }
  6124. if(npc->GetTotalPower() == 0){
  6125. npc->SetTotalPower(15*npc->GetLevel() + 1);
  6126. npc->SetPower(15*npc->GetLevel() + 1);
  6127. }
  6128. npc->size = result.GetInt16(28);
  6129. npc->appearance.pos.collision_radius = result.GetInt16(29);
  6130. npc->appearance.action_state = result.GetInt16(30);
  6131. npc->appearance.visual_state = result.GetInt16(31);
  6132. npc->appearance.mood_state = result.GetInt16(32);
  6133. npc->appearance.emote_state = result.GetInt16(71);
  6134. npc->appearance.pos.state = result.GetInt16(33);
  6135. npc->appearance.activity_status = result.GetInt16(34);
  6136. npc->faction_id = result.GetInt32(35);
  6137. if(!result.IsNull(36)){
  6138. std::string sub_title = std::string(result.GetString(36));
  6139. if(sub_title.find("Collector") != std::string::npos) {
  6140. npc->SetCollector(true);
  6141. }
  6142. if(strlen(result.GetString(36)) < sizeof(npc->appearance.sub_title))
  6143. strcpy(npc->appearance.sub_title, result.GetString(36));
  6144. else
  6145. strncpy(npc->appearance.sub_title, result.GetString(36), sizeof(npc->appearance.sub_title));
  6146. }
  6147. npc->SetMerchantID(result.GetInt32(37));
  6148. npc->SetMerchantType(result.GetInt8(38));
  6149. npc->SetSizeOffset(result.GetInt8(39));
  6150. npc->SetAIStrategy(result.GetInt8(41));
  6151. npc->SetPrimarySpellList(result.GetInt32(42));
  6152. npc->SetSecondarySpellList(result.GetInt32(43));
  6153. npc->SetPrimarySkillList(result.GetInt32(44));
  6154. npc->SetSecondarySkillList(result.GetInt32(45));
  6155. npc->SetEquipmentListID(result.GetInt32(46));
  6156. InfoStruct* info = npc->GetInfoStruct();
  6157. info->set_attack_type(result.GetInt8(40));
  6158. info->set_str_base(result.GetInt16(47));
  6159. info->set_sta_base(result.GetInt16(48));
  6160. info->set_wis_base(result.GetInt16(49));
  6161. info->set_intel_base(result.GetInt16(50));
  6162. info->set_agi_base(result.GetInt16(51));
  6163. info->set_heat_base(result.GetInt16(52));
  6164. info->set_cold_base(result.GetInt16(53));
  6165. info->set_magic_base(result.GetInt16(54));
  6166. info->set_mental_base(result.GetInt16(55));
  6167. info->set_divine_base(result.GetInt16(56));
  6168. info->set_disease_base(result.GetInt16(57));
  6169. info->set_poison_base(result.GetInt16(58));
  6170. info->set_alignment(result.GetSInt8(64));
  6171. npc->SetAggroRadius(result.GetFloat(59));
  6172. npc->SetCastPercentage(result.GetInt8(60));
  6173. npc->appearance.heroic_flag = result.GetInt8(63);
  6174. info->set_elemental_base(result.GetInt16(65));
  6175. info->set_arcane_base(result.GetInt16(66));
  6176. info->set_noxious_base(result.GetInt16(67));
  6177. npc->SetTotalSavagery(result.GetInt32(68));
  6178. npc->SetTotalDissonance(result.GetInt32(69));
  6179. npc->SetSavagery(npc->GetTotalSavagery());
  6180. npc->SetDissonance(npc->GetTotalDissonance());
  6181. if(npc->GetTotalSavagery() == 0){
  6182. npc->SetTotalSavagery(15*npc->GetLevel() + 1);
  6183. npc->SetSavagery(15*npc->GetLevel() + 1);
  6184. }
  6185. if(npc->GetTotalDissonance() == 0){
  6186. npc->SetTotalDissonance(15*npc->GetLevel() + 1);
  6187. npc->SetDissonance(15*npc->GetLevel() + 1);
  6188. }
  6189. npc->SetPrefixTitle(result.GetString(72));
  6190. npc->SetSuffixTitle(result.GetString(73));
  6191. npc->SetLastName(result.GetString(74));
  6192. npc->SetSoundsDisabled(result.GetInt8(75));
  6193. npc->SetMerchantLevelRange(result.GetInt32(76), result.GetInt32(77));
  6194. npc->SetAAXPRewards(result.GetInt32(78));
  6195. npc->SetLootTier(result.GetInt32(79));
  6196. npc->SetLootDropType(result.GetInt32(80));
  6197. npc->SetScaredByStrongPlayers(result.GetInt32(81));
  6198. if(!result.IsNull(82)){
  6199. std::string action_state_str = std::string(result.GetString(82));
  6200. npc->GetInfoStruct()->set_action_state(action_state_str);
  6201. }
  6202. zone->AddNPC(id, npc);
  6203. //skipped spells/skills/equipment as it is all loaded, the following rely on a spawn to load
  6204. LoadAppearance(zone, spawn_id);
  6205. LoadNPCAppearanceEquipmentData(zone, spawn_id);
  6206. LogWrite(NPC__DEBUG, 0, "NPC", "Loaded NPC: '%s' (%u).", npc->appearance.name, spawn_id);
  6207. return true;
  6208. }
  6209. LogWrite(NPC__DEBUG, 0, "NPC", "Unable to find a npc for spawn id of %u", spawn_id);
  6210. return false;
  6211. }
  6212. void WorldDatabase::LoadAppearance(ZoneServer* zone, int32 spawn_id) {
  6213. Entity* entity = zone->GetNPC(spawn_id);
  6214. if (!entity)
  6215. return;
  6216. DatabaseResult result, result2;
  6217. map<string, int8> appearance_types;
  6218. map<int32, map<int8, EQ2_Color> > appearance_colors;
  6219. EQ2_Color color;
  6220. color.red = 0;
  6221. color.green = 0;
  6222. color.blue = 0;
  6223. string type;
  6224. database_new.Select(&result2, "SELECT distinct `type`\n"
  6225. "FROM npc_appearance\n"
  6226. "WHERE length(`type`) > 0 AND `spawn_id` = %u",
  6227. spawn_id);
  6228. while(result2.Next()) {
  6229. type = string(result2.GetString(0));
  6230. appearance_types[type] = GetAppearanceType(type);
  6231. if(appearance_types[type] == 255)
  6232. LogWrite(WORLD__ERROR, 0, "Appearance", "Unknown appearance type '%s' in LoadAppearances.", type.c_str());
  6233. }
  6234. database_new.Select(&result, "SELECT `type`, `signed_value`, `red`, `green`, `blue`\n"
  6235. "FROM npc_appearance\n"
  6236. "WHERE length(`type`) > 0 AND `spawn_id` = %u",
  6237. spawn_id);
  6238. while(result.Next()) {
  6239. if(appearance_types[result.GetString(0)] < APPEARANCE_SOGA_EBT){
  6240. color.red = result.GetInt8(2);
  6241. color.green = result.GetInt8(3);
  6242. color.blue = result.GetInt8(4);
  6243. }
  6244. switch(appearance_types[result.GetString(0)]){
  6245. case APPEARANCE_SOGA_HFHC:{
  6246. entity->features.soga_hair_face_highlight_color = color;
  6247. break;
  6248. }
  6249. case APPEARANCE_SOGA_HTHC:{
  6250. entity->features.soga_hair_type_highlight_color = color;
  6251. break;
  6252. }
  6253. case APPEARANCE_SOGA_HFC:{
  6254. entity->features.soga_hair_face_color = color;
  6255. break;
  6256. }
  6257. case APPEARANCE_SOGA_HTC:{
  6258. entity->features.soga_hair_type_color = color;
  6259. break;
  6260. }
  6261. case APPEARANCE_SOGA_HH:{
  6262. entity->features.soga_hair_highlight_color = color;
  6263. break;
  6264. }
  6265. case APPEARANCE_SOGA_HC1:{
  6266. entity->features.soga_hair_color1 = color;
  6267. break;
  6268. }
  6269. case APPEARANCE_SOGA_HC2:{
  6270. entity->features.soga_hair_color2 = color;
  6271. break;
  6272. }
  6273. case APPEARANCE_SOGA_SC:{
  6274. entity->features.soga_skin_color = color;
  6275. break;
  6276. }
  6277. case APPEARANCE_SOGA_EC:{
  6278. entity->features.soga_eye_color = color;
  6279. break;
  6280. }
  6281. case APPEARANCE_HTHC:{
  6282. entity->features.hair_type_highlight_color = color;
  6283. break;
  6284. }
  6285. case APPEARANCE_HFHC:{
  6286. entity->features.hair_face_highlight_color = color;
  6287. break;
  6288. }
  6289. case APPEARANCE_HTC:{
  6290. entity->features.hair_type_color = color;
  6291. break;
  6292. }
  6293. case APPEARANCE_HFC:{
  6294. entity->features.hair_face_color = color;
  6295. break;
  6296. }
  6297. case APPEARANCE_HH:{
  6298. entity->features.hair_highlight_color = color;
  6299. break;
  6300. }
  6301. case APPEARANCE_HC1:{
  6302. entity->features.hair_color1 = color;
  6303. break;
  6304. }
  6305. case APPEARANCE_HC2:{
  6306. entity->features.hair_color2 = color;
  6307. break;
  6308. }
  6309. case APPEARANCE_WC1:{
  6310. entity->features.wing_color1 = color;
  6311. break;
  6312. }
  6313. case APPEARANCE_WC2:{
  6314. entity->features.wing_color2 = color;
  6315. break;
  6316. }
  6317. case APPEARANCE_SC:{
  6318. entity->features.skin_color = color;
  6319. break;
  6320. }
  6321. case APPEARANCE_EC:{
  6322. entity->features.eye_color = color;
  6323. break;
  6324. }
  6325. case APPEARANCE_SOGA_EBT:{
  6326. for(int i=0;i<3;i++)
  6327. entity->features.soga_eye_brow_type[i] = result.GetInt8(2+i);
  6328. break;
  6329. }
  6330. case APPEARANCE_SOGA_CHEEKT:{
  6331. for(int i=0;i<3;i++)
  6332. entity->features.soga_cheek_type[i] = result.GetInt8(2+i);
  6333. break;
  6334. }
  6335. case APPEARANCE_SOGA_NT:{
  6336. for(int i=0;i<3;i++)
  6337. entity->features.soga_nose_type[i] = result.GetInt8(2+i);
  6338. break;
  6339. }
  6340. case APPEARANCE_SOGA_CHINT:{
  6341. for(int i=0;i<3;i++)
  6342. entity->features.soga_chin_type[i] = result.GetInt8(2+i);
  6343. break;
  6344. }
  6345. case APPEARANCE_SOGA_LT:{
  6346. for(int i=0;i<3;i++)
  6347. entity->features.soga_lip_type[i] = result.GetInt8(2+i);
  6348. break;
  6349. }
  6350. case APPEARANCE_SOGA_EART:{
  6351. for(int i=0;i<3;i++)
  6352. entity->features.soga_ear_type[i] = result.GetInt8(2+i);
  6353. break;
  6354. }
  6355. case APPEARANCE_SOGA_EYET:{
  6356. for(int i=0;i<3;i++)
  6357. entity->features.soga_eye_type[i] = result.GetInt8(2+i);
  6358. break;
  6359. }
  6360. case APPEARANCE_EBT:{
  6361. for(int i=0;i<3;i++)
  6362. entity->features.eye_brow_type[i] = result.GetInt8(2+i);
  6363. break;
  6364. }
  6365. case APPEARANCE_CHEEKT:{
  6366. for(int i=0;i<3;i++)
  6367. entity->features.cheek_type[i] = result.GetInt8(2+i);
  6368. break;
  6369. }
  6370. case APPEARANCE_NT:{
  6371. for(int i=0;i<3;i++)
  6372. entity->features.nose_type[i] = result.GetInt8(2+i);
  6373. break;
  6374. }
  6375. case APPEARANCE_CHINT:{
  6376. for(int i=0;i<3;i++)
  6377. entity->features.chin_type[i] = result.GetInt8(2+i);
  6378. break;
  6379. }
  6380. case APPEARANCE_EART:{
  6381. for(int i=0;i<3;i++)
  6382. entity->features.ear_type[i] = result.GetInt8(2+i);
  6383. break;
  6384. }
  6385. case APPEARANCE_EYET:{
  6386. for(int i=0;i<3;i++)
  6387. entity->features.eye_type[i] = result.GetInt8(2+i);
  6388. break;
  6389. }
  6390. case APPEARANCE_LT:{
  6391. for(int i=0;i<3;i++)
  6392. entity->features.lip_type[i] = result.GetInt8(2+i);
  6393. break;
  6394. }
  6395. case APPEARANCE_SHIRT:{
  6396. entity->features.shirt_color = color;
  6397. break;
  6398. }
  6399. case APPEARANCE_UCC:{
  6400. break;
  6401. }
  6402. case APPEARANCE_PANTS:{
  6403. entity->features.pants_color = color;
  6404. break;
  6405. }
  6406. case APPEARANCE_ULC:{
  6407. break;
  6408. }
  6409. case APPEARANCE_U9:{
  6410. break;
  6411. }
  6412. case APPEARANCE_BODY_SIZE:{
  6413. entity->features.body_size = color.red;
  6414. break;
  6415. }
  6416. case APPEARANCE_SOGA_WC1:{
  6417. break;
  6418. }
  6419. case APPEARANCE_SOGA_WC2:{
  6420. break;
  6421. }
  6422. case APPEARANCE_SOGA_SHIRT:{
  6423. break;
  6424. }
  6425. case APPEARANCE_SOGA_UCC:{
  6426. break;
  6427. }
  6428. case APPEARANCE_SOGA_PANTS:{
  6429. break;
  6430. }
  6431. case APPEARANCE_SOGA_ULC:{
  6432. break;
  6433. }
  6434. case APPEARANCE_SOGA_U13:{
  6435. break;
  6436. }
  6437. case APPEARANCE_BODY_AGE: {
  6438. entity->features.body_age = color.red;
  6439. break;
  6440. }
  6441. case APPEARANCE_MC:{
  6442. entity->features.model_color = color;
  6443. break;
  6444. }
  6445. case APPEARANCE_SMC:{
  6446. entity->features.soga_model_color = color;
  6447. break;
  6448. }
  6449. case APPEARANCE_SBS: {
  6450. entity->features.soga_body_size = color.red;
  6451. break;
  6452. }
  6453. case APPEARANCE_SBA: {
  6454. entity->features.soga_body_age = color.red;
  6455. break;
  6456. }
  6457. }
  6458. }
  6459. entity->info_changed = true;
  6460. }
  6461. void WorldDatabase::LoadNPCAppearanceEquipmentData(ZoneServer* zone, int32 spawn_id) {
  6462. NPC* npc = zone->GetNPC(spawn_id);
  6463. if(!npc) {
  6464. LogWrite(NPC__ERROR, 0, "NPC", "Unable to get a valid npc (%u) in %s", spawn_id, __FUNCTION__);
  6465. return;
  6466. }
  6467. DatabaseResult result;
  6468. int8 slot = 0;
  6469. if (!database_new.Select(&result, "SELECT slot_id, equip_type, red, green, blue, highlight_red, highlight_green, highlight_blue\n"
  6470. "FROM npc_appearance_equip\n"
  6471. "WHERE spawn_id = %u\n",
  6472. spawn_id))
  6473. {
  6474. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  6475. return;
  6476. }
  6477. while (result.Next()) {
  6478. slot = result.GetInt8(0);
  6479. if(slot < NUM_SLOTS) {
  6480. npc->SetEquipment(slot, result.GetInt16(1), result.GetInt8(2), result.GetInt8(3), result.GetInt8(4), result.GetInt8(5), result.GetInt8(6), result.GetInt8(7));
  6481. }
  6482. }
  6483. }
  6484. void WorldDatabase::SaveCharacterPicture(int32 characterID, int8 type, uchar* picture, int32 picture_size) {
  6485. stringstream ss_hex;
  6486. stringstream ss_query;
  6487. ss_hex.flags(ios::hex);
  6488. for (int32 i = 0; i < picture_size; i++)
  6489. ss_hex << setfill('0') << setw(2) << (int32)picture[i];
  6490. ss_query << "INSERT INTO `character_pictures` (`char_id`, `pic_type`, `picture`) VALUES (" << characterID << ", " << (int32)type << ", '" << ss_hex.str() << "') ON DUPLICATE KEY UPDATE `picture` = '" << ss_hex.str() << "'";
  6491. Query query;
  6492. query.RunQuery2(ss_query.str(), Q_REPLACE);
  6493. if (query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF)
  6494. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error: in SaveCharacterPicture! Error Message: ", query.GetError());
  6495. }
  6496. void WorldDatabase::LoadZoneFlightPaths(ZoneServer* zone) {
  6497. DatabaseResult result;
  6498. int32 total = 0;
  6499. if (!database_new.Select(&result, "SELECT id, speed, flying, early_dismount FROM flight_paths WHERE zone_id = %u", zone->GetZoneID())) {
  6500. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  6501. return;
  6502. }
  6503. while (result.Next()) {
  6504. FlightPathInfo* info = new FlightPathInfo;
  6505. int32 id = result.GetInt32(0);
  6506. info->speed = result.GetFloat(1);
  6507. info->flying = result.GetInt8(2) == 1 ? true : false;
  6508. info->dismount = result.GetInt8(3) == 1 ? true : false;
  6509. zone->AddFlightPath(id, info);
  6510. total++;
  6511. }
  6512. LogWrite(ZONE__DEBUG, 0, "Zone", "Loaded %u flight paths for %s", total, zone->GetZoneDescription());
  6513. LoadZoneFlightPathLocations(zone);
  6514. }
  6515. void WorldDatabase::LoadZoneFlightPathLocations(ZoneServer* zone) {
  6516. DatabaseResult result;
  6517. int32 total = 0;
  6518. if (!database_new.Select(&result, "SELECT loc.flight_path, loc.x, loc.y, loc.z FROM flight_paths_locations loc\n"
  6519. "INNER JOIN flight_paths path\n"
  6520. "ON loc.flight_path = path.id\n"
  6521. "WHERE path.zone_id = %u\n"
  6522. "ORDER BY loc.id",
  6523. zone->GetZoneID()))
  6524. {
  6525. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  6526. return;
  6527. }
  6528. while (result.Next()) {
  6529. FlightPathLocation* loc = new FlightPathLocation;
  6530. int32 id = result.GetInt32(0);
  6531. loc->X = result.GetFloat(1);
  6532. loc->Y = result.GetFloat(2);
  6533. loc->Z = result.GetFloat(3);
  6534. zone->AddFlightPathLocation(id, loc);
  6535. total++;
  6536. }
  6537. LogWrite(ZONE__DEBUG, 0, "Zone", "Loaded %u flight path locations for %s", total, zone->GetZoneDescription());
  6538. }
  6539. void WorldDatabase::SaveCharacterLUAHistory(Player* player, int32 event_id, int32 value, int32 value2) {
  6540. Query query;
  6541. query.AddQueryAsync(player->GetCharacterID(), this, Q_REPLACE, "REPLACE INTO character_lua_history(char_id, event_id, value, value2) VALUES(% u, % u, % u, % u)", player->GetCharacterID(), event_id, value, value2);
  6542. // if (!database_new.Query("REPLACE INTO character_lua_history (char_id, event_id, value, value2) VALUES (%u, %u, %u, %u)", player->GetCharacterID(), event_id, value, value2))
  6543. // LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  6544. }
  6545. void WorldDatabase::LoadCharacterLUAHistory(int32 char_id, Player* player) {
  6546. DatabaseResult result;
  6547. int32 total = 0;
  6548. if (!database_new.Select(&result, "SELECT event_id, value, value2 FROM character_lua_history WHERE char_id = %u", char_id)) {
  6549. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  6550. return;
  6551. }
  6552. while (result.Next()) {
  6553. int32 id = result.GetInt32(0);
  6554. LUAHistory* hd = new LUAHistory;
  6555. hd->Value = result.GetInt32(1);
  6556. hd->Value2 = result.GetInt32(2);
  6557. hd->SaveNeeded = false;
  6558. player->LoadLUAHistory(id, hd);
  6559. total++;
  6560. }
  6561. LogWrite(PLAYER__DEBUG, 0, "Player", "Loaded %u LUA history for %s", total, player->GetName());
  6562. }
  6563. void WorldDatabase::FindSpell(Client* client, char* findString)
  6564. {
  6565. char* find_escaped = getEscapeString(findString);
  6566. DatabaseResult result;
  6567. if (!database_new.Select(&result, "SELECT s.`id`, ts.spell_id, ts.index, `name`, `tier` "
  6568. "FROM (spells s, spell_tiers st) "
  6569. "LEFT JOIN spell_ts_ability_index ts "
  6570. "ON s.`id` = ts.spell_id "
  6571. "WHERE s.id = st.spell_id and s.name like '%%%s%%' AND s.is_active = 1 "
  6572. "ORDER BY s.`id`, `tier` limit 50", find_escaped))
  6573. {
  6574. // error
  6575. }
  6576. else
  6577. {
  6578. client->Message(CHANNEL_COLOR_YELLOW, "SpellID (SpellTier): SpellName for %s", findString);
  6579. while (result.Next())
  6580. {
  6581. int32 spell_id = result.GetInt32Str("id");
  6582. string spell_name = result.GetStringStr("name");
  6583. int8 tier = result.GetInt8Str("tier");
  6584. client->Message(CHANNEL_COLOR_YELLOW, "%i (%i): %s", spell_id, tier, spell_name.c_str());
  6585. }
  6586. client->Message(CHANNEL_COLOR_YELLOW, "End Spell Results for %s", find_escaped);
  6587. }
  6588. safe_delete_array(find_escaped);
  6589. }
  6590. void WorldDatabase::LoadChestTraps() {
  6591. chest_trap_list.Clear();
  6592. int32 index = 0;
  6593. Query query;
  6594. MYSQL_ROW row;
  6595. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, applicable_zone_id, chest_min_difficulty, chest_max_difficulty, spell_id, spell_tier FROM chest_traps");
  6596. if (result && mysql_num_rows(result) > 0) {
  6597. Title* title = 0;
  6598. while (result && (row = mysql_fetch_row(result))) {
  6599. int32 dbid = atoul(row[0]);
  6600. sint32 applicable_zone_id = atoi(row[1]);
  6601. int32 mindifficulty = atoul(row[2]);
  6602. int32 maxdifficulty = atoul(row[3]);
  6603. int32 spellid = atoul(row[4]);
  6604. int32 tier = atoul(row[5]);
  6605. ChestTrap* trap = new ChestTrap(dbid,applicable_zone_id,mindifficulty,maxdifficulty,spellid,tier);
  6606. chest_trap_list.AddChestTrap(trap);
  6607. }
  6608. }
  6609. }
  6610. bool WorldDatabase::CheckExpansionFlags(ZoneServer* zone, int32 spawnXpackFlag)
  6611. {
  6612. if (spawnXpackFlag == 0)
  6613. return true;
  6614. int32 globalXpackFlag = rule_manager.GetGlobalRule(R_Expansion, GlobalExpansionFlag)->GetInt32();
  6615. int32 zoneXpackFlag = zone->GetExpansionFlag();
  6616. // zone expansion flag takes priority
  6617. if (zoneXpackFlag > 0 && (spawnXpackFlag & zoneXpackFlag) == 0)
  6618. return false;
  6619. // zone expansion flag fails, then if global expansion flag set, we see if that bit operand doesn't match, skip mob then
  6620. else if (zoneXpackFlag == 0 && globalXpackFlag > 0 && (spawnXpackFlag & globalXpackFlag) == 0)
  6621. return false;
  6622. return true;
  6623. }
  6624. bool WorldDatabase::CheckHolidayFlags(ZoneServer* zone, int32 spawnHolidayFlag)
  6625. {
  6626. if (spawnHolidayFlag == 0)
  6627. return true;
  6628. int32 globalHolidayFlag = rule_manager.GetGlobalRule(R_Expansion, GlobalHolidayFlag)->GetInt32();
  6629. int32 zoneHolidayFlag = zone->GetHolidayFlag();
  6630. // zone holiday flag takes priority
  6631. if (zoneHolidayFlag > 0 && (spawnHolidayFlag & zoneHolidayFlag) == 0)
  6632. return false;
  6633. // zone holiday flag fails, then if global expansion flag set, we see if that bit operand doesn't match, skip mob then
  6634. else if (zoneHolidayFlag == 0 && globalHolidayFlag > 0 && (spawnHolidayFlag & globalHolidayFlag) == 0)
  6635. return false;
  6636. return true;
  6637. }
  6638. void WorldDatabase::GetHouseSpawnInstanceData(ZoneServer* zone, Spawn* spawn)
  6639. {
  6640. if (!spawn)
  6641. return;
  6642. if (zone->house_object_database_lookup.count(spawn->GetModelType()) < 1)
  6643. zone->house_object_database_lookup.Put(spawn->GetModelType(), spawn->GetDatabaseID());
  6644. DatabaseResult result;
  6645. database_new.Select(&result, "SELECT pickup_item_id, pickup_unique_item_id\n"
  6646. " FROM spawn_instance_data\n"
  6647. " WHERE spawn_id = %u and spawn_location_id = %u",
  6648. spawn->GetDatabaseID(),spawn->GetSpawnLocationID());
  6649. if (result.GetNumRows() > 0 && result.Next()) {
  6650. spawn->SetPickupItemID(result.GetInt32(0));
  6651. spawn->SetPickupUniqueItemID(result.GetInt32(1));
  6652. if (spawn->GetZone() != nullptr && spawn->GetMap() != nullptr && spawn->GetMap()->IsMapLoaded())
  6653. {
  6654. auto loc = glm::vec3(spawn->GetX(),spawn->GetZ(),spawn->GetY());
  6655. uint32 GridID = 0;
  6656. float new_z = spawn->FindBestZ(loc, nullptr, &GridID);
  6657. spawn->SetPos(&(spawn->appearance.pos.grid_id), GridID);
  6658. }
  6659. }
  6660. }
  6661. int32 WorldDatabase::FindHouseInstanceSpawn(Spawn* spawn)
  6662. {
  6663. DatabaseResult result;
  6664. database_new.Select(&result, "SELECT id\n"
  6665. " FROM spawn\n"
  6666. " WHERE model_type = %u and is_instanced_spawn=1 limit 1",
  6667. spawn->GetModelType());
  6668. if (result.GetNumRows() > 0 && result.Next()) {
  6669. return result.GetInt32(0);
  6670. }
  6671. return 0;
  6672. }
  6673. void WorldDatabase::LoadStartingSkills(World* world)
  6674. {
  6675. world->MStartingLists.writelock();
  6676. int32 total = 0;
  6677. Query query;
  6678. MYSQL_ROW row;
  6679. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT race_id, class_id, skill_id, current_val, max_val, progress FROM starting_skills");
  6680. if (result)
  6681. {
  6682. if (mysql_num_rows(result) > 0)
  6683. {
  6684. Skill* skill = 0;
  6685. while (result && (row = mysql_fetch_row(result)))
  6686. {
  6687. StartingSkill skill;
  6688. skill.header.race_id = atoul(row[0]);
  6689. skill.header.class_id = atoul(row[1]);
  6690. skill.skill_id = atoul(row[2]);
  6691. skill.current_val = atoul(row[3]);
  6692. skill.max_val = atoul(row[4]);
  6693. if (!world->starting_skills.count(skill.header.race_id))
  6694. {
  6695. multimap<int8, StartingSkill>* skills = new multimap<int8, StartingSkill>();
  6696. skills->insert(make_pair(skill.header.class_id, skill));
  6697. world->starting_skills.insert(make_pair(skill.header.race_id, skills));
  6698. }
  6699. else
  6700. {
  6701. multimap<int8, multimap<int8, StartingSkill>*>::const_iterator skills = world->starting_skills.find(skill.header.race_id);
  6702. skills->second->insert(make_pair(skill.header.class_id, skill));
  6703. }
  6704. total++;
  6705. }
  6706. }
  6707. }
  6708. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u Starting Skill(s)", total);
  6709. world->MStartingLists.releasewritelock();
  6710. }
  6711. void WorldDatabase::LoadVoiceOvers(World* world)
  6712. {
  6713. int32 total = 0;
  6714. Query query;
  6715. MYSQL_ROW row;
  6716. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT type_id, id, indexed, mp3_string, text_string, emote_string, key1, key2, garbled, garble_link_id FROM voiceovers");
  6717. if (result)
  6718. {
  6719. if (mysql_num_rows(result) > 0)
  6720. {
  6721. Skill* skill = 0;
  6722. while (result && (row = mysql_fetch_row(result)))
  6723. {
  6724. VoiceOverStruct vos;
  6725. vos.mp3_string = std::string(row[3]);
  6726. vos.text_string = std::string(row[4]);
  6727. vos.emote_string = std::string(row[5]);
  6728. vos.key1 = atoul(row[6]);
  6729. vos.key2 = atoul(row[7]);
  6730. vos.is_garbled = atoul(row[8]);
  6731. vos.garble_link_id = atoul(row[9]);
  6732. int8 type = atoul(row[0]);
  6733. int32 id = atoul(row[1]);
  6734. int16 index = atoul(row[2]);
  6735. world->AddVoiceOver(type, id, index, &vos);
  6736. total++;
  6737. }
  6738. }
  6739. }
  6740. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u Voiceover(s)", total);
  6741. }
  6742. void WorldDatabase::LoadStartingSpells(World* world)
  6743. {
  6744. world->MStartingLists.writelock();
  6745. int32 total = 0;
  6746. Query query;
  6747. MYSQL_ROW row;
  6748. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT race_id, class_id, spell_id, tier, knowledge_slot FROM starting_spells");
  6749. if (result)
  6750. {
  6751. if (mysql_num_rows(result) > 0)
  6752. {
  6753. Skill* skill = 0;
  6754. while (result && (row = mysql_fetch_row(result)))
  6755. {
  6756. StartingSpell spell;
  6757. spell.header.race_id = atoul(row[0]);
  6758. spell.header.class_id = atoul(row[1]);
  6759. spell.spell_id = atoul(row[2]);
  6760. spell.tier = atoul(row[3]);
  6761. spell.knowledge_slot = atoul(row[4]);
  6762. if (!world->starting_spells.count(spell.header.race_id))
  6763. {
  6764. multimap<int8, StartingSpell>* spells = new multimap<int8, StartingSpell>();
  6765. spells->insert(make_pair(spell.header.class_id, spell));
  6766. world->starting_spells.insert(make_pair(spell.header.race_id, spells));
  6767. }
  6768. else
  6769. {
  6770. multimap<int8, multimap<int8, StartingSpell>*>::iterator spells = world->starting_spells.find(spell.header.race_id);
  6771. spells->second->insert(make_pair(spell.header.class_id, spell));
  6772. }
  6773. total++;
  6774. }
  6775. }
  6776. }
  6777. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u Starting Spell(s)", total);
  6778. world->MStartingLists.releasewritelock();
  6779. }
  6780. bool WorldDatabase::DeleteSpiritShard(int32 id){
  6781. Query query;
  6782. query.RunQuery2(Q_DELETE, "delete FROM character_spirit_shards where id=%u",id);
  6783. if(query.GetErrorNumber() && query.GetError() && query.GetErrorNumber() < 0xFFFFFFFF){
  6784. LogWrite(WORLD__ERROR, 0, "World", "Error in DeleteSpiritShard query '%s': %s", query.GetQuery(), query.GetError());
  6785. return false;
  6786. }
  6787. return true;
  6788. }
  6789. int32 WorldDatabase::CreateSpiritShard(const char* name, int32 level, int8 race, int8 gender, int8 adventure_class,
  6790. int16 model_type, int16 soga_model_type, int16 hair_type, int16 hair_face_type, int16 wing_type,
  6791. int16 chest_type, int16 legs_type, int16 soga_hair_type, int16 soga_hair_face_type, int8 hide_hood,
  6792. int16 size, int16 collision_radius, int16 action_state, int16 visual_state, int16 mood_state, int16 emote_state,
  6793. int16 pos_state, int16 activity_status, char* sub_title, char* prefix_title, char* suffix_title, char* lastname,
  6794. float x, float y, float z, float heading, int32 gridid, int32 charid, int32 zoneid, int32 instanceid)
  6795. {
  6796. LogWrite(WORLD__INFO, 3, "World", "Saving Spirit Shard %s %u", name, charid);
  6797. Query query;
  6798. char* name_escaped = getEscapeString(name);
  6799. if(!sub_title)
  6800. sub_title = "";
  6801. char* subtitle_escaped = getEscapeString(sub_title);
  6802. char* prefix_escaped = getEscapeString(prefix_title);
  6803. char* suffix_escaped = getEscapeString(suffix_title);
  6804. char* lastname_escaped = getEscapeString(lastname);
  6805. string insert = string("INSERT INTO character_spirit_shards (name, level, race, gender, adventure_class, model_type, soga_model_type, hair_type, hair_face_type, wing_type, chest_type, legs_type, soga_hair_type, soga_hair_face_type, hide_hood, size, collision_radius, action_state, visual_state, mood_state, emote_state, pos_state, activity_status, sub_title, prefix_title, suffix_title, lastname, x, y, z, heading, gridid, charid, zoneid, instanceid) VALUES ('%s', %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, '%s', '%s', '%s', '%s', %f, %f, %f, %f, %u, %u, %u, %u)");
  6806. query.RunQuery2(Q_INSERT, insert.c_str(), name_escaped, level, race, gender, adventure_class, model_type, soga_model_type,
  6807. hair_type, hair_face_type, wing_type, chest_type, legs_type, soga_hair_type,
  6808. soga_hair_face_type, hide_hood, size, collision_radius, action_state, visual_state,
  6809. mood_state, emote_state, pos_state, activity_status, subtitle_escaped, prefix_escaped, suffix_escaped,
  6810. lastname_escaped, x, y, z, heading, gridid, charid, zoneid, instanceid);
  6811. safe_delete_array(name_escaped);
  6812. safe_delete_array(subtitle_escaped);
  6813. safe_delete_array(prefix_escaped);
  6814. safe_delete_array(suffix_escaped);
  6815. safe_delete_array(lastname_escaped);
  6816. return query.GetLastInsertedID();
  6817. }
  6818. void WorldDatabase::LoadCharacterSpellEffects(int32 char_id, Client* client, int8 db_spell_type)
  6819. {
  6820. SpellProcess* spellProcess = client->GetCurrentZone()->GetSpellProcess();
  6821. Player* player = client->GetPlayer();
  6822. if(!spellProcess)
  6823. return;
  6824. DatabaseResult result;
  6825. multimap<LuaSpell*, Entity*> restoreSpells;
  6826. // Use -1 on type and subtype to turn the enum into an int and make it a 0 index
  6827. if (!database_new.Select(&result, "SELECT name, caster_char_id, target_char_id, target_type, spell_id, effect_slot, slot_pos, icon, icon_backdrop, conc_used, tier, total_time, expire_timestamp, lua_file, custom_spell, damage_remaining, effect_bitmask, num_triggers, had_triggers, cancel_after_triggers, crit, last_spellattack_hit, interrupted, resisted, has_damaged, custom_function FROM character_spell_effects WHERE charid = %u and db_effect_type = %u", char_id, db_spell_type)) {
  6828. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  6829. return;
  6830. }
  6831. InfoStruct* info = player->GetInfoStruct();
  6832. while (result.Next()) {
  6833. //result.GetInt8Str
  6834. char spell_name[60];
  6835. strncpy(spell_name, result.GetStringStr("name"), 60);
  6836. int32 caster_char_id = result.GetInt32Str("caster_char_id");
  6837. int32 target_char_id = result.GetInt32Str("target_char_id");
  6838. int8 target_type = result.GetInt8Str("target_type");
  6839. int32 spell_id = result.GetInt32Str("spell_id");
  6840. int32 effect_slot = result.GetInt32Str("effect_slot");
  6841. int32 slot_pos = result.GetInt32Str("slot_pos");
  6842. int16 icon = result.GetInt32Str("icon");
  6843. int16 icon_backdrop = result.GetInt32Str("icon_backdrop");
  6844. int8 conc_used = result.GetInt32Str("conc_used");
  6845. int8 tier = result.GetInt32Str("tier");
  6846. float total_time = result.GetFloatStr("total_time");
  6847. int32 expire_timestamp = result.GetInt32Str("expire_timestamp");
  6848. string lua_file (result.GetStringStr("lua_file"));
  6849. int8 custom_spell = result.GetInt32Str("custom_spell");
  6850. int32 damage_remaining = result.GetInt32Str("damage_remaining");
  6851. int32 effect_bitmask = result.GetInt32Str("effect_bitmask");
  6852. int16 num_triggers = result.GetInt32Str("num_triggers");
  6853. int8 had_triggers = result.GetInt32Str("had_triggers");
  6854. int8 cancel_after_triggers = result.GetInt32Str("cancel_after_triggers");
  6855. int8 crit = result.GetInt32Str("crit");
  6856. int8 last_spellattack_hit = result.GetInt32Str("last_spellattack_hit");
  6857. int8 interrupted = result.GetInt32Str("interrupted");
  6858. int8 resisted = result.GetInt32Str("resisted");
  6859. int8 has_damaged = result.GetInt32Str("has_damaged");
  6860. std::string custom_function = std::string(result.GetStringStr("custom_function"));
  6861. LuaSpell* lua_spell = 0;
  6862. if(custom_spell)
  6863. {
  6864. if((lua_spell = lua_interface->GetSpell(lua_file.c_str())) == nullptr)
  6865. {
  6866. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: GetSpell(%u, %u, '%s'), custom lua script not loaded, when attempting to load.", spell_id, tier, lua_file.c_str());
  6867. lua_interface->LoadLuaSpell(lua_file);
  6868. }
  6869. }
  6870. Spell* spell = master_spell_list.GetSpell(spell_id, tier);
  6871. if(!spell)
  6872. {
  6873. LogWrite(LUA__ERROR, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: GetSpell(%u, %u), spell could not be found!", spell_id, tier);
  6874. spell = master_spell_list.GetSpell(spell_id, 0);
  6875. if(spell)
  6876. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: GetSpell(%u, %u), identified tier 0 as replacement since the GetSpell failed!", spell_id, tier);
  6877. else
  6878. continue;
  6879. }
  6880. bool isMaintained = false;
  6881. bool isExistingLuaSpell = false;
  6882. MaintainedEffects* effect = nullptr;
  6883. Client* tmpCaster = nullptr;
  6884. if(caster_char_id == player->GetCharacterID() && (target_char_id == 0xFFFFFFFF || target_char_id == player->GetCharacterID()) && (effect = player->GetMaintainedSpell(spell_id)) != nullptr)
  6885. {
  6886. safe_delete(lua_spell);
  6887. lua_spell = effect->spell;
  6888. if(lua_spell)
  6889. spell = lua_spell->spell;
  6890. isMaintained = true;
  6891. isExistingLuaSpell = true;
  6892. }
  6893. else if ( caster_char_id != player->GetCharacterID() && (tmpCaster = zone_list.GetClientByCharID(caster_char_id)) != nullptr
  6894. && tmpCaster->GetPlayer() && (effect = tmpCaster->GetPlayer()->GetMaintainedSpell(spell_id)) != nullptr)
  6895. {
  6896. if(effect->spell && effect->spell_id == spell_id)
  6897. {
  6898. safe_delete(lua_spell);
  6899. if(tmpCaster->GetCurrentZone() == player->GetZone())
  6900. spellProcess->AddLuaSpellTarget(effect->spell, client->GetPlayer()->GetID());
  6901. lua_spell = effect->spell;
  6902. spell = effect->spell->spell;
  6903. isExistingLuaSpell = true;
  6904. isMaintained = true;
  6905. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: GetSpell(%u, %u, '%s'), effect spell id %u maintained spell recovered from %s", spell_id, tier, spell_name, effect ? effect->spell_id : 0, (tmpCaster && tmpCaster->GetPlayer()) ? tmpCaster->GetPlayer()->GetName() : "?");
  6906. }
  6907. else
  6908. {
  6909. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: GetSpell(%u, %u, '%s'), something went wrong loading another characters maintained spell. Effect has spell id %u", spell_id, tier, lua_file.c_str(), effect ? effect->spell_id : 0);
  6910. safe_delete(lua_spell);
  6911. continue;
  6912. }
  6913. }
  6914. else if(custom_spell && lua_spell)
  6915. {
  6916. lua_spell->spell = new Spell(spell);
  6917. lua_interface->AddCustomSpell(lua_spell);
  6918. }
  6919. else if(db_spell_type == DB_TYPE_MAINTAINEDEFFECTS)
  6920. {
  6921. safe_delete(lua_spell);
  6922. if(!target_char_id)
  6923. continue;
  6924. lua_spell = lua_interface->GetSpell(spell->GetSpellData()->lua_script.c_str());
  6925. if(lua_spell)
  6926. lua_spell->spell = spell;
  6927. }
  6928. if(!lua_spell)
  6929. {
  6930. LogWrite(LUA__ERROR, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: GetSpell(%u, %u, '%s'), lua_spell FAILED, when attempting to load.", spell_id, tier, lua_file.c_str());
  6931. continue;
  6932. }
  6933. SpellScriptTimer* timer = nullptr;
  6934. if(!isExistingLuaSpell && expire_timestamp != 0xFFFFFFFF && custom_function.size() > 0)
  6935. {
  6936. timer = new SpellScriptTimer;
  6937. timer->caster = 0;
  6938. timer->deleteWhenDone = false;
  6939. timer->target = 0;
  6940. timer->time = expire_timestamp;
  6941. timer->customFunction = string(custom_function); // TODO
  6942. timer->spell = lua_spell;
  6943. timer->caster = (caster_char_id == player->GetCharacterID()) ? player->GetID() : 0;
  6944. if(target_char_id == 0xFFFFFFFF && player->HasPet())
  6945. timer->target = player->GetPet()->GetID();
  6946. else
  6947. timer->target = (target_char_id == player->GetCharacterID()) ? player->GetID() : 0;
  6948. if(!timer->target && target_char_id)
  6949. {
  6950. Client* tmpClient = zone_list.GetClientByCharID(target_char_id);
  6951. if(tmpClient && tmpClient->GetPlayer() && tmpClient->GetPlayer()->GetZone() == player->GetZone())
  6952. timer->target = tmpClient->GetPlayer()->GetID();
  6953. }
  6954. }
  6955. if(!isExistingLuaSpell)
  6956. {
  6957. lua_spell->crit = crit;
  6958. lua_spell->damage_remaining = damage_remaining;
  6959. lua_spell->effect_bitmask = effect_bitmask;
  6960. lua_spell->had_dmg_remaining = (damage_remaining>0) ? true : false;
  6961. lua_spell->had_triggers = had_triggers;
  6962. lua_spell->initial_caster_char_id = caster_char_id;
  6963. lua_spell->initial_target = (target_char_id == player->GetCharacterID()) ? player->GetID() : 0;
  6964. lua_spell->initial_target_char_id = target_char_id;
  6965. lua_spell->interrupted = interrupted;
  6966. lua_spell->last_spellattack_hit = last_spellattack_hit;
  6967. lua_spell->num_triggers = num_triggers;
  6968. lua_spell->has_damaged = has_damaged;
  6969. lua_spell->is_damage_spell = has_damaged;
  6970. }
  6971. if(lua_spell->initial_target == 0 && target_char_id == 0xFFFFFFFF && player->HasPet())
  6972. {
  6973. lua_spell->initial_target = player->GetPet()->GetID();
  6974. lua_spell->initial_target_char_id = target_char_id;
  6975. }
  6976. //lua_spell->num_calls ??
  6977. //if(target_char_id == player->GetCharacterID())
  6978. // lua_spell->targets.push_back(player->GetID());
  6979. if(db_spell_type == DB_TYPE_SPELLEFFECTS)
  6980. {
  6981. if (caster_char_id != player->GetCharacterID() && lua_spell->spell->GetSpellData()->group_spell && lua_spell->spell->GetSpellData()->friendly_spell)
  6982. {
  6983. if(!isExistingLuaSpell)
  6984. safe_delete(lua_spell);
  6985. continue;
  6986. }
  6987. player->MSpellEffects.writelock();
  6988. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: %s lua_spell caster %s (%u), caster char id: %u.", lua_spell->spell->GetName(), lua_spell->caster ? lua_spell->caster->GetName() : "", lua_spell->caster ? lua_spell->caster->GetID() : 0, caster_char_id);
  6989. if(lua_spell->caster && (rule_manager.GetGlobalRule(R_Spells,EnableCrossZoneTargetBuffs)->GetInt8() || (!rule_manager.GetGlobalRule(R_Spells,EnableCrossZoneTargetBuffs)->GetInt8() && lua_spell->caster->GetZone() == player->GetZone())))
  6990. info->spell_effects[effect_slot].caster = lua_spell->caster;
  6991. else if(caster_char_id != player->GetCharacterID())
  6992. {
  6993. Client* tmpCaster = zone_list.GetClientByCharID(caster_char_id);
  6994. if(tmpCaster)
  6995. {
  6996. if((rule_manager.GetGlobalRule(R_Spells,EnableCrossZoneTargetBuffs)->GetInt8() || (!rule_manager.GetGlobalRule(R_Spells,EnableCrossZoneTargetBuffs)->GetInt8() && lua_spell->caster->GetZone() == player->GetZone())))
  6997. {
  6998. info->spell_effects[effect_slot].caster = tmpCaster->GetPlayer();
  6999. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: %s lua_spell caster %s (%u), caster char id: %u, found player %s.", lua_spell->spell->GetName(), lua_spell->caster ? lua_spell->caster->GetName() : "", lua_spell->caster ? lua_spell->caster->GetID() : 0, caster_char_id, tmpCaster->GetPlayer()->GetName());
  7000. }
  7001. else
  7002. {
  7003. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: %s lua_spell caster %s (%u), caster char id: %u, found player %s, SKIPPED due to R_Spells, EnableCrossZoneTargetBuffs.", lua_spell->spell->GetName(), lua_spell->caster ? lua_spell->caster->GetName() : "", lua_spell->caster ? lua_spell->caster->GetID() : 0, caster_char_id, tmpCaster->GetPlayer()->GetName());
  7004. if(!isExistingLuaSpell)
  7005. {
  7006. safe_delete(lua_spell);
  7007. }
  7008. else
  7009. {
  7010. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  7011. lua_spell->char_id_targets.insert(make_pair(player->GetCharacterID(),0));
  7012. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  7013. }
  7014. player->MSpellEffects.releasewritelock();
  7015. continue;
  7016. }
  7017. }
  7018. }
  7019. else if(caster_char_id == player->GetCharacterID())
  7020. info->spell_effects[effect_slot].caster = player;
  7021. else
  7022. {
  7023. LogWrite(LUA__ERROR, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: %s lua_spell caster %s (%u), caster char id: %u, failed to find caster will delete: %u", lua_spell->spell->GetName(), lua_spell->caster ? lua_spell->caster->GetName() : "", lua_spell->caster ? lua_spell->caster->GetID() : 0, caster_char_id, isExistingLuaSpell);
  7024. if(!isExistingLuaSpell)
  7025. safe_delete(lua_spell);
  7026. continue;
  7027. }
  7028. if(spell->GetSpellData()->duration_until_cancel)
  7029. info->spell_effects[effect_slot].expire_timestamp = 0xFFFFFFFF;
  7030. else
  7031. info->spell_effects[effect_slot].expire_timestamp = Timer::GetCurrentTime2() + expire_timestamp;
  7032. info->spell_effects[effect_slot].icon = icon;
  7033. info->spell_effects[effect_slot].icon_backdrop = icon_backdrop;
  7034. info->spell_effects[effect_slot].spell_id = spell_id;
  7035. info->spell_effects[effect_slot].tier = tier;
  7036. info->spell_effects[effect_slot].total_time = total_time;
  7037. info->spell_effects[effect_slot].spell = lua_spell;
  7038. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  7039. multimap<int32,int8>::iterator entries;
  7040. while((entries = lua_spell->char_id_targets.find(player->GetCharacterID())) != lua_spell->char_id_targets.end())
  7041. {
  7042. lua_spell->char_id_targets.erase(entries);
  7043. }
  7044. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  7045. lua_spell->slot_pos = slot_pos;
  7046. if(!isExistingLuaSpell)
  7047. lua_spell->caster = player; // TODO: get actual player
  7048. player->MSpellEffects.releasewritelock();
  7049. if(!isMaintained)
  7050. spellProcess->ProcessSpell(lua_spell, true, "cast", timer);
  7051. else
  7052. {
  7053. // track target id when caster isnt in zone somehow
  7054. }
  7055. }
  7056. else if ( db_spell_type == DB_TYPE_MAINTAINEDEFFECTS )
  7057. {
  7058. player->MMaintainedSpells.writelock();
  7059. DatabaseResult targets;
  7060. // Use -1 on type and subtype to turn the enum into an int and make it a 0 index
  7061. if (database_new.Select(&targets, "SELECT target_char_id, target_type, db_effect_type, spell_id from character_spell_effect_targets where caster_char_id = %u and effect_slot = %u and slot_pos = %u", char_id, effect_slot, slot_pos)) {
  7062. while (targets.Next()) {
  7063. int32 target_char = targets.GetInt32Str("target_char_id");
  7064. int8 maintained_target_type = targets.GetInt32Str("target_type");
  7065. int32 in_spell_id = targets.GetInt32Str("spell_id");
  7066. if(spell_id != in_spell_id)
  7067. continue;
  7068. int32 idToAdd = 0;
  7069. if(target_char == 0xFFFFFFFF)
  7070. {
  7071. if( player->HasPet() )
  7072. {
  7073. restoreSpells.insert(make_pair(lua_spell, player->GetPet()));
  7074. // target added via restoreSpells
  7075. }
  7076. }
  7077. else
  7078. {
  7079. Client* client2 = zone_list.GetClientByCharID(target_char);
  7080. if(client2 && client2->GetPlayer() && client2->GetCurrentZone() == client->GetCurrentZone())
  7081. {
  7082. if(maintained_target_type > 0)
  7083. {
  7084. if(client != client2)
  7085. {
  7086. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  7087. if(client2->GetPlayer()->GetPet() && maintained_target_type == PET_TYPE_COMBAT)
  7088. {
  7089. restoreSpells.insert(make_pair(lua_spell, client2->GetPlayer()->GetPet()));
  7090. // target added via restoreSpells
  7091. }
  7092. if(client2->GetPlayer()->GetCharmedPet() && maintained_target_type == PET_TYPE_CHARMED)
  7093. {
  7094. restoreSpells.insert(make_pair(lua_spell, client2->GetPlayer()->GetCharmedPet()));
  7095. // target added via restoreSpells
  7096. }
  7097. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  7098. }
  7099. } // end of pet clause
  7100. else if(client != client2) // maintained type must be 0, so client
  7101. restoreSpells.insert(make_pair(lua_spell, client2->GetPlayer()));
  7102. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  7103. multimap<int32,int8>::iterator entries;
  7104. for(entries = lua_spell->char_id_targets.begin(); entries != lua_spell->char_id_targets.end(); entries++)
  7105. {
  7106. int32 ent_char_id = entries->first;
  7107. int8 ent_target_type = entries->second;
  7108. if(ent_char_id == target_char && ent_target_type == maintained_target_type)
  7109. entries = lua_spell->char_id_targets.erase(entries);
  7110. }
  7111. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  7112. }
  7113. else
  7114. {
  7115. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  7116. lua_spell->char_id_targets.insert(make_pair(target_char,maintained_target_type));
  7117. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  7118. }
  7119. }
  7120. }
  7121. }
  7122. Client* tmpClient = 0;
  7123. int32 targetID = 0;
  7124. if(target_char_id == 0xFFFFFFFF && player->HasPet())
  7125. targetID = player->GetPet()->GetID();
  7126. else if(target_char_id == player->GetCharacterID())
  7127. {
  7128. targetID = player->GetID();
  7129. tmpClient = player->GetClient();
  7130. }
  7131. else if((tmpClient = zone_list.GetClientByCharID(target_char_id)) != nullptr && tmpClient->GetPlayer())
  7132. targetID = tmpClient->GetPlayer()->GetID();
  7133. info->maintained_effects[effect_slot].conc_used = conc_used;
  7134. strncpy(info->maintained_effects[effect_slot].name, spell_name, 60);
  7135. info->maintained_effects[effect_slot].slot_pos = slot_pos;
  7136. info->maintained_effects[effect_slot].target = targetID;
  7137. info->maintained_effects[effect_slot].target_type = target_type;
  7138. if(spell->GetSpellData()->duration_until_cancel)
  7139. info->maintained_effects[effect_slot].expire_timestamp = 0xFFFFFFFF;
  7140. else
  7141. info->maintained_effects[effect_slot].expire_timestamp = Timer::GetCurrentTime2() + expire_timestamp;
  7142. info->maintained_effects[effect_slot].icon = icon;
  7143. info->maintained_effects[effect_slot].icon_backdrop = icon_backdrop;
  7144. info->maintained_effects[effect_slot].spell_id = spell_id;
  7145. info->maintained_effects[effect_slot].tier = tier;
  7146. info->maintained_effects[effect_slot].total_time = total_time;
  7147. info->maintained_effects[effect_slot].spell = lua_spell;
  7148. if(!isExistingLuaSpell)
  7149. lua_spell->caster = player;
  7150. player->MMaintainedSpells.releasewritelock();
  7151. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: %s process spell caster %s (%u), caster char id: %u, target id %u (%s).", lua_spell->spell->GetName(), lua_spell->caster ? lua_spell->caster->GetName() : "",
  7152. lua_spell->caster ? lua_spell->caster->GetID() : 0, caster_char_id, targetID, tmpClient ? tmpClient->GetPlayer()->GetName() : "");
  7153. if(tmpClient && lua_spell->initial_target_char_id == tmpClient->GetCharacterID())
  7154. {
  7155. lua_spell->initial_target = tmpClient->GetPlayer()->GetID();
  7156. spellProcess->AddLuaSpellTarget(lua_spell, lua_spell->initial_target, false);
  7157. }
  7158. spellProcess->ProcessSpell(lua_spell, true, "cast", timer);
  7159. }
  7160. if(!isExistingLuaSpell && expire_timestamp != 0xFFFFFFFF && !isMaintained)
  7161. {
  7162. lua_spell->timer.SetTimer(expire_timestamp);
  7163. lua_spell->timer.SetAtTrigger(lua_spell->spell->GetSpellData()->duration1 * 100);
  7164. lua_spell->timer.Start();
  7165. }
  7166. if(target_char_id == player->GetCharacterID() && lua_spell->spell->GetSpellData()->det_type)
  7167. player->AddDetrimentalSpell(lua_spell, expire_timestamp);
  7168. if(!isExistingLuaSpell)
  7169. {
  7170. if(timer)
  7171. spellProcess->AddSpellScriptTimer(timer);
  7172. lua_spell->num_calls = 1;
  7173. lua_spell->restored = true;
  7174. }
  7175. if(!lua_spell->resisted && (lua_spell->spell->GetSpellDuration() > 0 || lua_spell->spell->GetSpellData()->duration_until_cancel))
  7176. spellProcess->AddActiveSpell(lua_spell);
  7177. if ( db_spell_type == DB_TYPE_MAINTAINEDEFFECTS )
  7178. {
  7179. // restore concentration on zoning/reloading characters maintained effects
  7180. spellProcess->AddConcentration(lua_spell);
  7181. if (num_triggers > 0)
  7182. ClientPacketFunctions::SendMaintainedExamineUpdate(client, slot_pos, num_triggers, 0);
  7183. if (damage_remaining > 0)
  7184. ClientPacketFunctions::SendMaintainedExamineUpdate(client, slot_pos, damage_remaining, 1);
  7185. }
  7186. }
  7187. if(db_spell_type == DB_TYPE_SPELLEFFECTS)
  7188. {
  7189. // if the cross_zone_target_buff option is disabled then we check for all possible targets within the current zone
  7190. // if cross_zone_target_buff is enabled, we only check to restore pets, the players get restored by their own spell effects (we don't directly track the pets, indirectly we do through the player casting and their targets)
  7191. int8 cross_zone_target_buff = rule_manager.GetGlobalRule(R_Spells,EnableCrossZoneTargetBuffs)->GetInt8();
  7192. DatabaseResult targets;
  7193. if (
  7194. (!cross_zone_target_buff && database_new.Select(&targets, "SELECT caster_char_id, target_type, spell_id from character_spell_effect_targets where target_char_id = %u", player->GetCharacterID())) ||
  7195. (database_new.Select(&targets, "SELECT caster_char_id, target_type, spell_id from character_spell_effect_targets where target_char_id = %u and target_type > 0", player->GetCharacterID())))
  7196. {
  7197. while (targets.Next()) {
  7198. int32 caster_char_id = targets.GetInt32Str("caster_char_id");
  7199. int8 prev_target_type = targets.GetInt32Str("target_type");
  7200. int32 in_spell_id = targets.GetInt32Str("spell_id");
  7201. Client* tmpCaster = nullptr;
  7202. MaintainedEffects* effect = nullptr;
  7203. if (caster_char_id != player->GetCharacterID() && (tmpCaster = zone_list.GetClientByCharID(caster_char_id)) != nullptr && (cross_zone_target_buff ||
  7204. tmpCaster->GetCurrentZone() == player->GetZone()) && tmpCaster->GetPlayer() && (effect = tmpCaster->GetPlayer()->GetMaintainedSpell(in_spell_id)) != nullptr)
  7205. {
  7206. if(prev_target_type > 0)
  7207. {
  7208. if(player->HasPet())
  7209. {
  7210. if(player->GetPet() && prev_target_type == PET_TYPE_COMBAT)
  7211. restoreSpells.insert(make_pair(effect->spell, player->GetPet()));
  7212. if(player->GetCharmedPet() && prev_target_type == PET_TYPE_CHARMED)
  7213. restoreSpells.insert(make_pair(effect->spell, player->GetCharmedPet()));
  7214. }
  7215. }
  7216. else if(!player->GetSpellEffect(effect->spell_id, tmpCaster->GetPlayer()))
  7217. {
  7218. if(effect->spell->initial_target_char_id == player->GetCharacterID())
  7219. effect->spell->initial_target = player->GetID();
  7220. restoreSpells.insert(make_pair(effect->spell, player));
  7221. }
  7222. }
  7223. }
  7224. }
  7225. }
  7226. multimap<LuaSpell*, Entity*>::const_iterator itr;
  7227. for (itr = restoreSpells.begin(); itr != restoreSpells.end(); itr++)
  7228. {
  7229. LuaSpell* tmpSpell = itr->first;
  7230. Entity* target = itr->second;
  7231. if(!target)
  7232. {
  7233. target = client->GetPlayer()->GetPet();
  7234. if(!target)
  7235. continue;
  7236. }
  7237. Entity* caster = tmpSpell->caster;
  7238. if(!caster)
  7239. caster = client->GetPlayer();
  7240. int32 group_id_target = target->group_id;
  7241. if(target->IsPet() && target->GetOwner())
  7242. group_id_target = target->GetOwner()->group_id;
  7243. if(caster != target && caster->GetPet() != target &&
  7244. tmpSpell->spell->GetSpellData()->group_spell && tmpSpell->spell->GetSpellData()->friendly_spell && (caster->group_id == 0 || group_id_target == 0 || caster->group_id != group_id_target))
  7245. {
  7246. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: %s player no longer grouped with %s to reload bonuses for spell %s (target_groupid: %u, caster_groupid: %u).", target->GetName(), caster ? caster->GetName() : "?", tmpSpell->spell->GetName(), group_id_target, caster->group_id);
  7247. continue;
  7248. }
  7249. LogWrite(LUA__WARNING, 0, "LUA", "WorldDatabase::LoadCharacterSpellEffects: %s using caster %s to reload bonuses for spell %s.", player->GetName(), caster ? caster->GetName() : "?", tmpSpell->spell->GetName());
  7250. spellProcess->AddLuaSpellTarget(tmpSpell, target->GetID());
  7251. target->AddSpellEffect(tmpSpell, tmpSpell->timer.GetRemainingTime() != 0 ? tmpSpell->timer.GetRemainingTime() : 0);
  7252. vector<BonusValues*>* sb_list = caster->GetAllSpellBonuses(tmpSpell);
  7253. for (int32 x = 0; x < sb_list->size(); x++) {
  7254. BonusValues* bv = sb_list->at(x);
  7255. target->AddSpellBonus(tmpSpell, bv->type, bv->value, bv->class_req, bv->race_req, bv->faction_req);
  7256. }
  7257. sb_list->clear();
  7258. safe_delete(sb_list);
  7259. // look for a skill bonus on the caster's spell
  7260. if(caster->IsPlayer())
  7261. {
  7262. SkillBonus* sb = ((Player*)caster)->GetSkillBonus(tmpSpell->spell->GetSpellID());
  7263. if (sb) {
  7264. map<int32, SkillBonusValue*>::iterator itr_skills;
  7265. for (itr_skills = sb->skills.begin(); itr_skills != sb->skills.end(); itr_skills++)
  7266. target->AddSkillBonus(sb->spell_id, (*itr_skills).second->skill_id, (*itr_skills).second->value);
  7267. }
  7268. }
  7269. }
  7270. }
  7271. //devn00b: We need to handle non-found factions so the subtraction/addition works on 1st kill. Need to find a better way to handle this, but for now..
  7272. bool WorldDatabase::VerifyFactionID(int32 char_id, int32 faction_id) {
  7273. DatabaseResult result;
  7274. database_new.Select(&result, "SELECT COUNT(id) as faction_exists from character_factions where faction_id=%u and char_id=%u", faction_id, char_id);
  7275. if (result.Next() && result.GetInt32Str("faction_exists") == 0)
  7276. return false;
  7277. return true;
  7278. }
  7279. //using int/32 for starting city, should be large enough to support future zones (LOL). TODO: Will probably need more support bolted on, to support PVP and such.
  7280. void WorldDatabase::UpdateStartingLanguage(int32 char_id, uint8 race_id, int32 starting_city)
  7281. {
  7282. int8 rule = rule_manager.GetGlobalRule(R_World, StartingZoneLanguages)->GetInt8();
  7283. //this should never need to be done but lets make sure we got all the stuff we need. char_id at min since used for both.
  7284. if(!char_id || (rule == 0 && !race_id) || (rule == 1 && !starting_city))
  7285. return;
  7286. std::string query_string = "SELECT language_id FROM starting_languages ";
  7287. //check the rule, and that they gave us a race, if so use default entries to match live.
  7288. if(rule == 0 && race_id >= 0) {
  7289. query_string.append("WHERE race=" + std::to_string(race_id));
  7290. }
  7291. //if we have a starting city supplied, and the rule is set to use it, deal with it
  7292. else if(rule == 1) {
  7293. query_string.append("WHERE starting_city=" + std::to_string(starting_city) + " or (starting_city=0 and race_id=" + std::to_string(race_id) + ")");
  7294. }
  7295. Query query;
  7296. MYSQL_ROW row;
  7297. MYSQL_RES* result = query.RunQuery2(Q_SELECT,query_string.c_str());
  7298. LogWrite(PLAYER__DEBUG, 0, "Player", "Adding Languages for starting_city: %i based on rule R_World:StartingZoneLanguages value %u", starting_city, rule);
  7299. if (result) {
  7300. if (mysql_num_rows(result) > 0) {
  7301. while (result && (row = mysql_fetch_row(result))){
  7302. //add custom languages to the character_languages db.
  7303. Query query2;
  7304. query2.RunQuery2(Q_INSERT, "INSERT IGNORE INTO character_languages (char_id, language_id) VALUES (%u,%u)",char_id, atoul(row[0]));
  7305. }
  7306. }
  7307. }
  7308. }
  7309. //devn00b: load the items the server has into a character_ db for easy access. Should be done on char create.
  7310. void WorldDatabase::LoadClaimItems(int32 char_id)
  7311. {
  7312. if (!char_id) {
  7313. LogWrite(WORLD__DEBUG, 3, "World", "-- There was an error in LoadClaimItems (missing char_id)");
  7314. return;
  7315. }
  7316. int16 total = 0;
  7317. Query query;
  7318. MYSQL_ROW row;
  7319. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT id, item_id, max_claim, one_per_char, veteran_reward_time from claim_items");
  7320. if (result)
  7321. {
  7322. if (mysql_num_rows(result) > 0)
  7323. {
  7324. while (result && (row = mysql_fetch_row(result)))
  7325. {
  7326. int32 item_id = atoul(row[1]);
  7327. int16 max_claim = atoul(row[2]);
  7328. int16 curr_claim = max_claim;
  7329. int8 one_per_char = atoul(row[3]);
  7330. int64 vet_reward_time = atoi64(row[4]);
  7331. int32 acct_id = GetCharacterAccountID(char_id);
  7332. if (one_per_char == 1) {
  7333. max_claim = 1;
  7334. curr_claim = 1;
  7335. }
  7336. Query query2;
  7337. MYSQL_RES* res = query2.RunQuery2(Q_INSERT, "insert ignore into character_claim_items (char_id, item_id, max_claim, curr_claim, one_per_char, veteran_reward_time, account_id) values (%i, %i, %i, %i, %i, %I64i, %i)", char_id, item_id, max_claim, curr_claim, one_per_char, vet_reward_time, acct_id);
  7338. total++;
  7339. }
  7340. }
  7341. }
  7342. LogWrite(WORLD__DEBUG, 3, "World", "--Loaded %u Claim Item(s)", total);
  7343. }
  7344. int16 WorldDatabase::CountCharClaimItems(int32 char_id) {
  7345. Query query;
  7346. MYSQL_ROW row;
  7347. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT count(*) from character_claim_items where char_id=%i and curr_claim >0", char_id);
  7348. if (result && mysql_num_rows(result) > 0) {
  7349. MYSQL_ROW row;
  7350. row = mysql_fetch_row(result);
  7351. return atoi(row[0]); // Return count
  7352. }
  7353. return 0;
  7354. }
  7355. vector<ClaimItems> WorldDatabase::LoadCharacterClaimItems(int32 char_id) {
  7356. vector<ClaimItems> claim;
  7357. //make sure we have a charID shouldn't need this but adding anyway.
  7358. if (!char_id)
  7359. return claim;
  7360. int32 acct_id = GetCharacterAccountID(char_id);
  7361. Query query;
  7362. MYSQL_ROW row;
  7363. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT item_id, max_claim, curr_claim, one_per_char, last_claim, veteran_reward_time from character_claim_items where char_id=%u and curr_claim > 0", char_id);
  7364. int16 i = 0;
  7365. if (result)
  7366. {
  7367. if (mysql_num_rows(result) > 0)
  7368. {
  7369. while (result && (row = mysql_fetch_row(result)))
  7370. {
  7371. ClaimItems c;
  7372. int8 max_claim = atoi(row[1]);
  7373. int8 curr_claim = atoi(row[2]);
  7374. //if one per char is set set max/cur to 1.
  7375. if (atoi(row[3]) == 1) {
  7376. max_claim = 1;
  7377. curr_claim = 1;
  7378. }
  7379. if (atoi(row[3]) == 0) {
  7380. //handle getting lowest claim amt from table, to prevent new chars skewing.
  7381. Query query2;
  7382. MYSQL_ROW row2;
  7383. MYSQL_RES* result2 = query2.RunQuery2(Q_SELECT, "SELECT min(curr_claim) AS min_claim FROM character_claim_items WHERE account_id=%i and item_id=%i", acct_id, atoi(row[0]));
  7384. //our current claim is now the lowest.
  7385. if (result2 && (row2 = mysql_fetch_row(result2)) && row2[0] != NULL) {
  7386. int8 curr_claim = atoi(row2[0]);
  7387. }
  7388. }
  7389. c.item_id = atoi(row[0]);
  7390. c.max_claim = max_claim;
  7391. c.curr_claim = curr_claim;
  7392. c.one_per_char = atoi(row[3]);
  7393. c.vet_reward_time = atoi(row[5]);
  7394. claim.push_back(c);
  7395. i++;
  7396. }
  7397. return claim;
  7398. }
  7399. }
  7400. return claim;
  7401. }
  7402. void WorldDatabase::ClaimItem(int32 char_id, int32 item_id, Client* client) {
  7403. if (!client || !item_id || !char_id) {
  7404. return;
  7405. }
  7406. Query query;
  7407. MYSQL_ROW row;
  7408. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT item_id, max_claim, curr_claim, one_per_char, last_claim, veteran_reward_time, one_per_char from character_claim_items where char_id=%u and item_id=%u", char_id, item_id);
  7409. if (result)
  7410. {
  7411. if (mysql_num_rows(result) > 0)
  7412. {
  7413. while (result && (row = mysql_fetch_row(result)))
  7414. {
  7415. Item* item = master_item_list.GetItem(item_id);
  7416. Player* player = client->GetPlayer();
  7417. InfoStruct* info = player->GetInfoStruct();
  7418. int32 item_id = atoi(row[0]);
  7419. int8 max_claim = atoi(row[1]);
  7420. int8 min_claim = 0;
  7421. int8 curr_claim = atoi(row[2]);
  7422. int32 vet_reward_req = atoi(row[5]);
  7423. int8 one_per_char = atoi(row[6]);
  7424. int32 acct_id = GetCharacterAccountID(char_id);
  7425. char claim_msg[512] = { 0 };
  7426. //no claims left.
  7427. if (curr_claim == 0) {
  7428. client->Message(CHANNEL_COLOR_RED, "You have nothing to claim.");
  7429. return;
  7430. }
  7431. //On live, characters must wait 6 seconds between claims. So..Lots of stuff here.
  7432. uint32 last_claim = info->get_last_claim_time(); //load our last claim
  7433. time_t now = time(0); //get the current epoc time
  7434. uint32 curr_time = now; // current time.
  7435. uint32 total_time = curr_time - last_claim; //Time since last claim (Current time - last claim)
  7436. if (total_time < 6) {
  7437. uint32 ttw = 6 - total_time;
  7438. char tmp[64] = { 0 };
  7439. sprintf(tmp, "You must wait %u more seconds.", ttw);
  7440. client->Message(CHANNEL_COLOR_RED, tmp);
  7441. return;
  7442. }
  7443. //handle the 2 different messages found from live (11/2022)
  7444. if (item->generic_info.item_type == 18) {
  7445. sprintf(claim_msg, "You have consumed the item\nYou have chosen to give this character a %s", item->name.c_str());
  7446. }
  7447. else {
  7448. sprintf(claim_msg, "You have consumed the item");
  7449. }
  7450. //not one per char, so remove 1 from account.
  7451. if (one_per_char == 0) {
  7452. Query query3;
  7453. MYSQL_ROW row3;
  7454. MYSQL_RES* result3 = query3.RunQuery2(Q_SELECT, "SELECT min(curr_claim) AS min_claim FROM character_claim_items WHERE account_id=%i and item_id=%i", acct_id, item_id);
  7455. if (result3 && (row3 = mysql_fetch_row(result3)) && row3[0] != NULL) {
  7456. int16 min_claim = atoi(row3[0]);
  7457. //remove 1 from claim on one per char
  7458. int32 acct_id = GetCharacterAccountID(char_id);
  7459. Query query2;
  7460. query2.RunQuery2(Q_UPDATE, "UPDATE `character_claim_items` SET curr_claim=(SELECT min(curr_claim) AS min_claim FROM character_claim_items WHERE account_id=%i and item_id=%i) - 1 , `last_claim`=%u WHERE account_id=%i and item_id=%i AND curr_claim > 0", acct_id, item_id, curr_time, acct_id, item_id);
  7461. //give the item to the player, and update last claim time.
  7462. bool item_added = client->AddItem(item_id);
  7463. if (item_added == 0) {
  7464. return;
  7465. }
  7466. info->set_last_claim_time(curr_time);
  7467. client->Message(CHANNEL_COLOR_YELLOW, claim_msg);
  7468. //reload the window to show new count.
  7469. client->ShowClaimWindow();
  7470. return;
  7471. }
  7472. }
  7473. Query query4;
  7474. query4.RunQuery2(Q_UPDATE, "UPDATE `character_claim_items` SET `curr_claim`=0 , `last_claim`=%u WHERE char_id=%i and item_id=%i", curr_time, char_id, item_id);
  7475. //give the item to the player, and update last claim time.
  7476. //client->AddItem(item_id);
  7477. bool item_added = client->AddItem(item_id);
  7478. if (item_added == 0) {
  7479. return;
  7480. }
  7481. info->set_last_claim_time(curr_time);
  7482. client->Message(CHANNEL_COLOR_YELLOW, claim_msg);
  7483. //reload the window to show new count.
  7484. client->ShowClaimWindow();
  7485. return;
  7486. }
  7487. }
  7488. }
  7489. return;
  7490. }
  7491. //returns account age in seconds
  7492. int32 WorldDatabase::GetAccountAge(int32 account_id) {
  7493. if (!account_id)
  7494. return 0;
  7495. int32 acct_age = 0;
  7496. int32 acct_created = 0;
  7497. time_t now = time(0); //get the current epoc time
  7498. uint32 curr_time = now; // current time.
  7499. Query query;
  7500. //order by created date and grab the oldest one.
  7501. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT UNIX_TIMESTAMP(created_date) FROM characters WHERE account_id=%i ORDER BY created_date LIMIT 1", account_id);
  7502. if (result && mysql_num_rows(result) > 0) {
  7503. MYSQL_ROW row = mysql_fetch_row(result);
  7504. acct_created = atoi(row[0]);
  7505. }
  7506. if (!curr_time || !acct_created)
  7507. return 0;
  7508. acct_age = curr_time - acct_created;
  7509. return acct_age;
  7510. }
  7511. void WorldDatabase::SaveSignMark(int32 char_id, int32 sign_id, char* char_name, Client* client) {
  7512. if (!database_new.Query("update spawn_signs set char_id=%u, char_name='%s' where widget_id=%u", char_id, char_name, sign_id)) {
  7513. LogWrite(SIGN__DEBUG, 0, "Sign", "ERROR in WorldDatabase::SaveSignMark");
  7514. return;
  7515. }
  7516. }
  7517. string WorldDatabase::GetSignMark(int32 char_id, int32 sign_id, char* char_name) {
  7518. Query query;
  7519. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT char_name from spawn_signs where widget_id=%u", sign_id);
  7520. char* charname = 0;
  7521. if (result && mysql_num_rows(result) > 0) {
  7522. MYSQL_ROW row = mysql_fetch_row(result);
  7523. charname = new char[strlen(row[0]) + 1];
  7524. memset(charname, 0, strlen(row[0]) + 1);
  7525. strcpy(charname, row[0]);
  7526. }
  7527. return charname;
  7528. }
  7529. int32 WorldDatabase::GetMysqlExpCurve(int level) {
  7530. Query query;
  7531. MYSQL_ROW row;
  7532. //this should never happen but just in case.
  7533. if(!level){
  7534. level = 95;
  7535. }
  7536. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "SELECT exp_needed from exp_per_level where level=%u", level);
  7537. if (result && mysql_num_rows(result) > 0) {
  7538. MYSQL_ROW row = mysql_fetch_row(result);
  7539. return atoi(row[0]);
  7540. }
  7541. //return 1 so we dont break shit later divide by 0 and all that.
  7542. return 1;
  7543. }