itemdb.cpp 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "itemdb.hpp"
  4. #include <chrono>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <map>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <unordered_map>
  11. #include "../common/nullpo.hpp"
  12. #include "../common/random.hpp"
  13. #include "../common/showmsg.hpp"
  14. #include "../common/strlib.hpp"
  15. #include "../common/utils.hpp"
  16. #include "../common/utilities.hpp"
  17. #include "battle.hpp" // struct battle_config
  18. #include "cashshop.hpp"
  19. #include "clif.hpp"
  20. #include "intif.hpp"
  21. #include "log.hpp"
  22. #include "mob.hpp"
  23. #include "pc.hpp"
  24. #include "status.hpp"
  25. using namespace rathena;
  26. ComboDatabase itemdb_combo;
  27. ItemGroupDatabase itemdb_group;
  28. struct s_roulette_db rd;
  29. static void itemdb_jobid2mapid(uint64 bclass[3], e_mapid jobmask, bool active);
  30. const std::string ItemDatabase::getDefaultLocation() {
  31. return std::string(db_path) + "/item_db.yml";
  32. }
  33. /**
  34. * Reads and parses an entry from the item_db.
  35. * @param node: YAML node containing the entry.
  36. * @return count of successfully parsed rows
  37. */
  38. uint64 ItemDatabase::parseBodyNode(const ryml::NodeRef& node) {
  39. t_itemid nameid;
  40. if (!this->asUInt32(node, "Id", nameid))
  41. return 0;
  42. std::shared_ptr<item_data> item = this->find(nameid);
  43. bool exists = item != nullptr;
  44. if (!exists) {
  45. if (!this->nodesExist(node, { "AegisName", "Name" }))
  46. return 0;
  47. item = std::make_shared<item_data>();
  48. item->nameid = nameid;
  49. item->flag.available = true;
  50. }
  51. if (this->nodeExists(node, "AegisName")) {
  52. std::string name;
  53. if (!this->asString(node, "AegisName", name))
  54. return 0;
  55. if (name.length() > ITEM_NAME_LENGTH) {
  56. this->invalidWarning(node["AegisName"], "AegisName \"%s\" exceeds maximum of %d characters, capping...\n", name.c_str(), ITEM_NAME_LENGTH - 1);
  57. }
  58. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  59. if (id != nullptr && id->nameid != nameid) {
  60. this->invalidWarning(node["AegisName"], "Found duplicate item Aegis name for %s, skipping.\n", name.c_str());
  61. return 0;
  62. }
  63. if( exists ){
  64. // Create a copy
  65. std::string aegisname = item->name;
  66. // Convert it to lower
  67. util::tolower( aegisname );
  68. // Remove old AEGIS name from lookup
  69. this->aegisNameToItemDataMap.erase( aegisname );
  70. }
  71. item->name.resize(ITEM_NAME_LENGTH);
  72. item->name = name.c_str();
  73. // Create a copy
  74. std::string aegisname = name;
  75. // Convert it to lower
  76. util::tolower( aegisname );
  77. this->aegisNameToItemDataMap[aegisname] = item;
  78. }
  79. if (this->nodeExists(node, "Name")) {
  80. std::string name;
  81. if (!this->asString(node, "Name", name))
  82. return 0;
  83. if (name.length() > ITEM_NAME_LENGTH) {
  84. this->invalidWarning(node["Name"], "Name \"%s\" exceeds maximum of %d characters, capping...\n", name.c_str(), ITEM_NAME_LENGTH - 1);
  85. }
  86. if( exists ){
  87. // Create a copy
  88. std::string ename = item->ename;
  89. // Convert it to lower
  90. util::tolower( ename );
  91. // Remove old name from lookup
  92. this->nameToItemDataMap.erase( ename );
  93. }
  94. item->ename.resize(ITEM_NAME_LENGTH);
  95. item->ename = name.c_str();
  96. // Create a copy
  97. std::string ename = name;
  98. // Convert it to lower
  99. util::tolower( ename );
  100. this->nameToItemDataMap[ename] = item;
  101. }
  102. if (this->nodeExists(node, "Type")) {
  103. std::string type;
  104. if (!this->asString(node, "Type", type))
  105. return 0;
  106. std::string type_constant = "IT_" + type;
  107. int64 constant;
  108. if (!script_get_constant(type_constant.c_str(), &constant) || constant < IT_HEALING || constant >= IT_MAX) {
  109. this->invalidWarning(node["Type"], "Invalid item type %s, defaulting to IT_ETC.\n", type.c_str());
  110. constant = IT_ETC;
  111. }
  112. item->type = static_cast<item_types>(constant);
  113. } else {
  114. if (!exists)
  115. item->type = IT_ETC;
  116. }
  117. if (this->nodeExists(node, "SubType")) {
  118. std::string type;
  119. if (!this->asString(node, "SubType", type))
  120. return 0;
  121. if (item->type == IT_WEAPON) {
  122. std::string type_constant = "W_" + type;
  123. int64 constant;
  124. if (!script_get_constant(type_constant.c_str(), &constant) || constant < W_FIST || constant >= MAX_WEAPON_TYPE) {
  125. this->invalidWarning(node["SubType"], "Invalid weapon type %s, defaulting to W_FIST.\n", type.c_str());
  126. item->subtype = W_FIST;
  127. }
  128. item->subtype = static_cast<weapon_type>(constant);
  129. } else if (item->type == IT_AMMO) {
  130. std::string type_constant = "AMMO_" + type;
  131. int64 constant;
  132. if (!script_get_constant(type_constant.c_str(), &constant) || constant <= AMMO_NONE || constant >= MAX_AMMO_TYPE) {
  133. this->invalidWarning(node["SubType"], "Invalid ammo type %s, defaulting to AMMO_NONE.\n", type.c_str());
  134. item->subtype = AMMO_NONE;
  135. }
  136. item->subtype = static_cast<e_ammo_type>(constant);
  137. } else if (item->type == IT_CARD) {
  138. std::string type_constant = "CARD_" + type;
  139. int64 constant;
  140. if (!script_get_constant(type_constant.c_str(), &constant) || constant < CARD_NORMAL || constant >= MAX_CARD_TYPE) {
  141. this->invalidWarning(node["SubType"], "Invalid card type %s, defaulting to CARD_NORMAL.\n", type.c_str());
  142. item->subtype = CARD_NORMAL;
  143. }
  144. item->subtype = static_cast<e_card_type>(constant);
  145. } else {
  146. this->invalidWarning(node["SubType"], "Item sub type is not supported for this item type.\n");
  147. return 0;
  148. }
  149. } else {
  150. if (!exists)
  151. item->subtype = 0;
  152. }
  153. if (this->nodeExists(node, "Buy")) {
  154. uint32 buy;
  155. if (!this->asUInt32(node, "Buy", buy))
  156. return 0;
  157. if( buy > MAX_ZENY ){
  158. this->invalidWarning( node["Buy"], "Buying price exceeds MAX_ZENY. Capping...\n" );
  159. buy = MAX_ZENY;
  160. }
  161. item->value_buy = buy;
  162. } else {
  163. if (!exists) {
  164. item->value_buy = 0;
  165. }
  166. }
  167. if (this->nodeExists(node, "Sell")) {
  168. uint32 sell;
  169. if (!this->asUInt32(node, "Sell", sell))
  170. return 0;
  171. if( sell > MAX_ZENY ){
  172. this->invalidWarning( node["Sell"], "Sell price exceeds MAX_ZENY. Capping...\n" );
  173. sell = MAX_ZENY;
  174. }
  175. item->value_sell = sell;
  176. } else {
  177. if (!exists) {
  178. item->value_sell = 0;
  179. }
  180. }
  181. if (this->nodeExists(node, "Weight")) {
  182. uint32 weight;
  183. if (!this->asUInt32(node, "Weight", weight))
  184. return 0;
  185. item->weight = weight;
  186. } else {
  187. if (!exists)
  188. item->weight = 0;
  189. }
  190. if (this->nodeExists(node, "Attack")) {
  191. uint32 atk;
  192. if (!this->asUInt32(node, "Attack", atk))
  193. return 0;
  194. item->atk = atk;
  195. } else {
  196. if (!exists)
  197. item->atk = 0;
  198. }
  199. #ifdef RENEWAL
  200. if (this->nodeExists(node, "MagicAttack")) {
  201. uint32 matk;
  202. if (!this->asUInt32(node, "MagicAttack", matk))
  203. return 0;
  204. item->matk = matk;
  205. } else {
  206. if (!exists)
  207. item->matk = 0;
  208. }
  209. #endif
  210. if (this->nodeExists(node, "Defense")) {
  211. uint32 def;
  212. if (!this->asUInt32(node, "Defense", def))
  213. return 0;
  214. if (def > DEFTYPE_MAX) {
  215. this->invalidWarning(node["Defense"], "Item defense %d exceeds DEFTYPE_MAX (%d), capping to DEFTYPE_MAX.\n", def, DEFTYPE_MAX);
  216. def = DEFTYPE_MAX;
  217. }
  218. item->def = def;
  219. } else {
  220. if (!exists)
  221. item->def = 0;
  222. }
  223. if (this->nodeExists(node, "Range")) {
  224. uint16 range;
  225. if (!this->asUInt16(node, "Range", range))
  226. return 0;
  227. if (range > AREA_SIZE) {
  228. this->invalidWarning(node["Range"], "Item attack range %d exceeds AREA_SIZE (%d), capping to AREA_SIZE.\n", range, AREA_SIZE);
  229. range = AREA_SIZE;
  230. }
  231. item->range = range;
  232. } else {
  233. if (!exists)
  234. item->range = 0;
  235. }
  236. if (this->nodeExists(node, "Slots")) {
  237. uint16 slots;
  238. if (!this->asUInt16(node, "Slots", slots))
  239. return 0;
  240. if (slots > MAX_SLOTS) {
  241. this->invalidWarning(node["Slots"], "Item slots %d exceeds MAX_SLOTS (%d), capping to MAX_SLOTS.\n", slots, MAX_SLOTS);
  242. slots = MAX_SLOTS;
  243. }
  244. item->slots = slots;
  245. } else {
  246. if (!exists)
  247. item->slots = 0;
  248. }
  249. if (this->nodeExists(node, "Jobs")) {
  250. const ryml::NodeRef& jobNode = node["Jobs"];
  251. item->class_base[0] = item->class_base[1] = item->class_base[2] = 0;
  252. if (this->nodeExists(jobNode, "All")) {
  253. bool active;
  254. if (!this->asBool(jobNode, "All", active))
  255. return 0;
  256. itemdb_jobid2mapid(item->class_base, MAPID_ALL, active);
  257. }
  258. for (const auto& jobit : jobNode) {
  259. std::string jobName;
  260. c4::from_chars(jobit.key(), &jobName);
  261. // Skipped because processed above the loop
  262. if (jobName.compare("All") == 0)
  263. continue;
  264. std::string jobName_constant = "EAJ_" + jobName;
  265. int64 constant;
  266. if (!script_get_constant(jobName_constant.c_str(), &constant)) {
  267. this->invalidWarning(jobNode[jobit.key()], "Invalid item job %s, defaulting to All.\n", jobName.c_str());
  268. itemdb_jobid2mapid(item->class_base, MAPID_ALL, true);
  269. break;
  270. }
  271. bool active;
  272. if (!this->asBool(jobNode, jobName, active))
  273. return 0;
  274. itemdb_jobid2mapid(item->class_base, static_cast<e_mapid>(constant), active);
  275. }
  276. } else {
  277. if (!exists) {
  278. item->class_base[0] = item->class_base[1] = item->class_base[2] = 0;
  279. itemdb_jobid2mapid(item->class_base, MAPID_ALL, true);
  280. }
  281. }
  282. if (this->nodeExists(node, "Classes")) {
  283. const auto& classNode = node["Classes"];
  284. if (this->nodeExists(classNode, "All")) {
  285. bool active;
  286. if (!this->asBool(classNode, "All", active))
  287. return 0;
  288. if (active)
  289. item->class_upper |= ITEMJ_ALL;
  290. else
  291. item->class_upper &= ~ITEMJ_ALL;
  292. }
  293. for (const auto& classit : classNode) {
  294. std::string className;
  295. c4::from_chars(classit.key(), &className);
  296. // Skipped because processed above the loop
  297. if (className.compare("All") == 0)
  298. continue;
  299. std::string className_constant = "ITEMJ_" + className;
  300. int64 constant;
  301. if (!script_get_constant(className_constant.c_str(), &constant)) {
  302. this->invalidWarning(classNode[classit.key()], "Invalid class upper %s, defaulting to All.\n", className.c_str());
  303. item->class_upper |= ITEMJ_ALL;
  304. break;
  305. }
  306. bool active;
  307. if (!this->asBool(classNode, className, active))
  308. return 0;
  309. if (active)
  310. item->class_upper |= constant;
  311. else
  312. item->class_upper &= ~constant;
  313. }
  314. } else {
  315. if (!exists)
  316. item->class_upper = ITEMJ_ALL;
  317. }
  318. if (this->nodeExists(node, "Gender")) {
  319. std::string gender;
  320. if (!this->asString(node, "Gender", gender))
  321. return 0;
  322. std::string gender_constant = "SEX_" + gender;
  323. int64 constant;
  324. if (!script_get_constant(gender_constant.c_str(), &constant) || constant < SEX_FEMALE || constant > SEX_BOTH) {
  325. this->invalidWarning(node["Gender"], "Invalid item gender %s, defaulting to SEX_BOTH.\n", gender.c_str());
  326. constant = SEX_BOTH;
  327. }
  328. item->sex = static_cast<e_sex>(constant);
  329. item->sex = this->defaultGender( node, item );
  330. } else {
  331. if (!exists) {
  332. item->sex = SEX_BOTH;
  333. item->sex = this->defaultGender( node, item );
  334. }
  335. }
  336. if (this->nodeExists(node, "Locations")) {
  337. const auto& locationNode = node["Locations"];
  338. for (const auto& locit : locationNode) {
  339. std::string equipName;
  340. c4::from_chars(locit.key(), &equipName);
  341. std::string equipName_constant = "EQP_" + equipName;
  342. int64 constant;
  343. if (!script_get_constant(equipName_constant.c_str(), &constant)) {
  344. this->invalidWarning(locationNode[locit.key()], "Invalid equip location %s, defaulting to IT_ETC.\n", equipName.c_str());
  345. item->type = IT_ETC;
  346. break;
  347. }
  348. bool active;
  349. if (!this->asBool(locationNode, equipName, active))
  350. return 0;
  351. if (active) {
  352. if (constant & EQP_SHADOW_GEAR && item->type != IT_SHADOWGEAR) {
  353. this->invalidWarning(node, "Invalid item equip location %s as it's not a Shadow Gear item type, defaulting to IT_ETC.\n", equipName.c_str());
  354. item->type = IT_ETC;
  355. }
  356. item->equip |= constant;
  357. } else
  358. item->equip &= ~constant;
  359. }
  360. } else {
  361. if (!exists) {
  362. if (itemdb_isequip2(item.get())) {
  363. this->invalidWarning(node, "Invalid item equip location as it has no equip location, defaulting to IT_ETC.\n");
  364. item->type = IT_ETC;
  365. } else
  366. item->equip = 0;
  367. }
  368. }
  369. if (this->nodeExists(node, "WeaponLevel")) {
  370. uint16 lv;
  371. if (!this->asUInt16(node, "WeaponLevel", lv))
  372. return 0;
  373. if (lv > MAX_WEAPON_LEVEL) {
  374. this->invalidWarning(node["WeaponLevel"], "Invalid weapon level %d, defaulting to 0.\n", lv);
  375. lv = 0;
  376. }
  377. if (item->type != IT_WEAPON) {
  378. this->invalidWarning(node["WeaponLevel"], "Item type is not a weapon, defaulting to 0.\n");
  379. lv = 0;
  380. }
  381. item->weapon_level = lv;
  382. } else {
  383. if (!exists)
  384. item->weapon_level = 0;
  385. }
  386. if( this->nodeExists( node, "ArmorLevel" ) ){
  387. uint16 level;
  388. if( !this->asUInt16( node, "ArmorLevel", level ) ){
  389. return 0;
  390. }
  391. if( level > MAX_ARMOR_LEVEL ){
  392. this->invalidWarning( node["ArmorLevel"], "Invalid armor level %d, defaulting to 0.\n", level );
  393. level = 0;
  394. }
  395. if( item->type != IT_ARMOR ){
  396. this->invalidWarning( node["ArmorLevel"], "Item type is not an armor, defaulting to 0.\n" );
  397. level = 0;
  398. }
  399. item->armor_level = level;
  400. }else{
  401. if( !exists ){
  402. item->armor_level = 0;
  403. }
  404. }
  405. if (this->nodeExists(node, "EquipLevelMin")) {
  406. uint16 lv;
  407. if (!this->asUInt16(node, "EquipLevelMin", lv))
  408. return 0;
  409. if (lv > MAX_LEVEL) {
  410. this->invalidWarning(node["EquipLevelMin"], "Minimum equip level %d exceeds MAX_LEVEL (%d), capping to MAX_LEVEL.\n", lv, MAX_LEVEL);
  411. lv = MAX_LEVEL;
  412. }
  413. item->elv = lv;
  414. } else {
  415. if (!exists)
  416. item->elv = 0;
  417. }
  418. if (this->nodeExists(node, "EquipLevelMax")) {
  419. uint16 lv;
  420. if (!this->asUInt16(node, "EquipLevelMax", lv))
  421. return 0;
  422. if (lv < item->elv) {
  423. this->invalidWarning(node["EquipLevelMax"], "Maximum equip level %d is less than minimum equip level %d, capping to minimum equip level.\n", lv, item->elv);
  424. lv = item->elv;
  425. }
  426. if (lv > MAX_LEVEL) {
  427. this->invalidWarning(node["EquipLevelMax"], "Maximum equip level %d exceeds MAX_LEVEL (%d), capping to MAX_LEVEL.\n", lv, MAX_LEVEL);
  428. lv = MAX_LEVEL;
  429. }
  430. item->elvmax = lv;
  431. } else {
  432. if (!exists)
  433. item->elvmax = MAX_LEVEL;
  434. }
  435. if (this->nodeExists(node, "Refineable")) {
  436. bool refine;
  437. if (!this->asBool(node, "Refineable", refine))
  438. return 0;
  439. item->flag.no_refine = !refine;
  440. } else {
  441. if (!exists)
  442. item->flag.no_refine = true;
  443. }
  444. if (this->nodeExists(node, "Gradable")) {
  445. bool gradable;
  446. if (!this->asBool(node, "Gradable", gradable))
  447. return 0;
  448. item->flag.gradable = gradable;
  449. } else {
  450. if (!exists)
  451. item->flag.gradable = false;
  452. }
  453. if (this->nodeExists(node, "View")) {
  454. uint32 look;
  455. if (!this->asUInt32(node, "View", look))
  456. return 0;
  457. item->look = look;
  458. } else {
  459. if (!exists)
  460. item->look = 0;
  461. }
  462. if (this->nodeExists(node, "AliasName")) {
  463. std::string view;
  464. if (!this->asString(node, "AliasName", view))
  465. return 0;
  466. std::shared_ptr<item_data> view_data = item_db.search_aegisname( view.c_str() );
  467. if (view_data == nullptr) {
  468. this->invalidWarning(node["AliasName"], "Unable to change the alias because %s is an unknown item.\n", view.c_str());
  469. return 0;
  470. }
  471. item->view_id = view_data->nameid;
  472. } else {
  473. if (!exists)
  474. item->view_id = 0;
  475. }
  476. if (this->nodeExists(node, "Flags")) {
  477. const auto& flagNode = node["Flags"];
  478. if (this->nodeExists(flagNode, "BuyingStore")) {
  479. bool active;
  480. if (!this->asBool(flagNode, "BuyingStore", active))
  481. return 0;
  482. if (!itemdb_isstackable2(item.get()) && active) {
  483. this->invalidWarning(flagNode["BuyingStore"], "Non-stackable item cannot be enabled for buying store.\n");
  484. active = false;
  485. }
  486. item->flag.buyingstore = active;
  487. } else {
  488. if (!exists)
  489. item->flag.buyingstore = false;
  490. }
  491. if (this->nodeExists(flagNode, "DeadBranch")) {
  492. bool active;
  493. if (!this->asBool(flagNode, "DeadBranch", active))
  494. return 0;
  495. item->flag.dead_branch = active;
  496. } else {
  497. if (!exists)
  498. item->flag.dead_branch = false;
  499. }
  500. if (this->nodeExists(flagNode, "Container")) {
  501. bool active;
  502. if (!this->asBool(flagNode, "Container", active))
  503. return 0;
  504. item->flag.group = active;
  505. } else {
  506. if (!exists)
  507. item->flag.group = false;
  508. }
  509. if (this->nodeExists(flagNode, "UniqueId")) {
  510. bool active;
  511. if (!this->asBool(flagNode, "UniqueId", active))
  512. return 0;
  513. if (!itemdb_isstackable2(item.get()) && active) {
  514. this->invalidWarning(flagNode["UniqueId"], "Non-stackable item cannot be enabled for UniqueId.\n");
  515. active = false;
  516. }
  517. item->flag.guid = active;
  518. } else {
  519. if (!exists)
  520. item->flag.guid = false;
  521. }
  522. if (this->nodeExists(flagNode, "BindOnEquip")) {
  523. bool active;
  524. if (!this->asBool(flagNode, "BindOnEquip", active))
  525. return 0;
  526. item->flag.bindOnEquip = active;
  527. } else {
  528. if (!exists)
  529. item->flag.bindOnEquip = false;
  530. }
  531. if (this->nodeExists(flagNode, "DropAnnounce")) {
  532. bool active;
  533. if (!this->asBool(flagNode, "DropAnnounce", active))
  534. return 0;
  535. item->flag.broadcast = active;
  536. } else {
  537. if (!exists)
  538. item->flag.broadcast = false;
  539. }
  540. if (this->nodeExists(flagNode, "NoConsume")) {
  541. bool active;
  542. if (!this->asBool(flagNode, "NoConsume", active))
  543. return 0;
  544. if (active)
  545. item->flag.delay_consume |= DELAYCONSUME_NOCONSUME;
  546. else
  547. item->flag.delay_consume &= ~DELAYCONSUME_NOCONSUME;
  548. } else {
  549. if (!exists) {
  550. if (!(item->flag.delay_consume & DELAYCONSUME_TEMP))
  551. item->flag.delay_consume = DELAYCONSUME_NONE;
  552. }
  553. }
  554. if (this->nodeExists(flagNode, "DropEffect")) {
  555. std::string effect;
  556. if (!this->asString(flagNode, "DropEffect", effect))
  557. return 0;
  558. std::string effect_constant = "DROPEFFECT_" + effect;
  559. int64 constant;
  560. if (!script_get_constant(effect_constant.c_str(), &constant) || constant < DROPEFFECT_NONE || constant > DROPEFFECT_MAX) {
  561. this->invalidWarning(flagNode["DropEffect"], "Invalid item drop effect %s, defaulting to DROPEFFECT_NONE.\n", effect.c_str());
  562. constant = DROPEFFECT_NONE;
  563. }
  564. item->flag.dropEffect = static_cast<e_item_drop_effect>(constant);
  565. } else {
  566. if (!exists)
  567. item->flag.dropEffect = DROPEFFECT_NONE;
  568. }
  569. } else {
  570. if (!exists) {
  571. item->flag.buyingstore = false;
  572. item->flag.dead_branch = false;
  573. item->flag.group = false;
  574. item->flag.guid = false;
  575. item->flag.bindOnEquip = false;
  576. item->flag.broadcast = false;
  577. if (!(item->flag.delay_consume & DELAYCONSUME_TEMP))
  578. item->flag.delay_consume = DELAYCONSUME_NONE;
  579. item->flag.dropEffect = DROPEFFECT_NONE;
  580. }
  581. }
  582. if (this->nodeExists(node, "Delay")) {
  583. const auto& delayNode = node["Delay"];
  584. if (this->nodeExists(delayNode, "Duration")) {
  585. uint32 duration;
  586. if (!this->asUInt32(delayNode, "Duration", duration))
  587. return 0;
  588. item->delay.duration = duration;
  589. } else {
  590. if (!exists)
  591. item->delay.duration = 0;
  592. }
  593. if (this->nodeExists(delayNode, "Status")) {
  594. std::string status;
  595. if (!this->asString(delayNode, "Status", status))
  596. return 0;
  597. std::string status_constant = "SC_" + status;
  598. int64 constant;
  599. if (!script_get_constant(status_constant.c_str(), &constant) || constant < SC_NONE || constant >= SC_MAX) {
  600. this->invalidWarning(delayNode[c4::to_csubstr(status)], "Invalid item delay status %s, defaulting to SC_NONE.\n", status.c_str());
  601. constant = SC_NONE;
  602. }
  603. item->delay.sc = static_cast<sc_type>(constant);
  604. } else {
  605. if (!exists)
  606. item->delay.sc = SC_NONE;
  607. }
  608. } else {
  609. if (!exists) {
  610. item->delay.duration = 0;
  611. item->delay.sc = SC_NONE;
  612. }
  613. }
  614. if (this->nodeExists(node, "Stack")) {
  615. const auto& stackNode = node["Stack"];
  616. if (this->nodeExists(stackNode, "Amount")) {
  617. uint16 amount;
  618. if (!this->asUInt16(stackNode, "Amount", amount))
  619. return 0;
  620. if (!itemdb_isstackable2(item.get())) {
  621. this->invalidWarning(stackNode["Amount"], "Non-stackable item cannot be enabled for stacking.\n");
  622. amount = 0;
  623. }
  624. item->stack.amount = amount;
  625. } else {
  626. if (!exists)
  627. item->stack.amount = 0;
  628. }
  629. if (this->nodeExists(stackNode, "Inventory")) {
  630. bool active;
  631. if (!this->asBool(stackNode, "Inventory", active))
  632. return 0;
  633. item->stack.inventory = active;
  634. } else {
  635. if (!exists)
  636. item->stack.inventory = false;
  637. }
  638. if (this->nodeExists(stackNode, "Cart")) {
  639. bool active;
  640. if (!this->asBool(stackNode, "Cart", active))
  641. return 0;
  642. item->stack.cart = active;
  643. } else {
  644. if (!exists)
  645. item->stack.cart = false;
  646. }
  647. if (this->nodeExists(stackNode, "Storage")) {
  648. bool active;
  649. if (!this->asBool(stackNode, "Storage", active))
  650. return 0;
  651. item->stack.storage = active;
  652. } else {
  653. if (!exists)
  654. item->stack.storage = false;
  655. }
  656. if (this->nodeExists(stackNode, "GuildStorage")) {
  657. bool active;
  658. if (!this->asBool(stackNode, "GuildStorage", active))
  659. return 0;
  660. item->stack.guild_storage = active;
  661. } else {
  662. if (!exists)
  663. item->stack.guild_storage = false;
  664. }
  665. } else {
  666. if (!exists) {
  667. item->stack.amount = 0;
  668. item->stack.inventory = false;
  669. item->stack.cart = false;
  670. item->stack.storage = false;
  671. item->stack.guild_storage = false;
  672. }
  673. }
  674. if (this->nodeExists(node, "NoUse")) {
  675. const auto& nouseNode = node["NoUse"];
  676. if (this->nodeExists(nouseNode, "Override")) {
  677. uint16 override;
  678. if (!this->asUInt16(nouseNode, "Override", override))
  679. return 0;
  680. if (override > 100) {
  681. this->invalidWarning(nouseNode["Override"], "Item no use override level %d exceeds 100, capping to 100.\n", override);
  682. override = 100;
  683. }
  684. item->item_usage.override = override;
  685. } else {
  686. if (!exists)
  687. item->item_usage.override = 100;
  688. }
  689. if (this->nodeExists(nouseNode, "Sitting")) {
  690. bool active;
  691. if (!this->asBool(nouseNode, "Sitting", active))
  692. return 0;
  693. item->item_usage.sitting = active;
  694. } else {
  695. if (!exists)
  696. item->item_usage.sitting = false;
  697. }
  698. } else {
  699. if (!exists) {
  700. item->item_usage.override = 100;
  701. item->item_usage.sitting = false;
  702. }
  703. }
  704. if (this->nodeExists(node, "Trade")) {
  705. const auto& tradeNode = node["Trade"];
  706. if (this->nodeExists(tradeNode, "Override")) {
  707. uint16 override;
  708. if (!this->asUInt16(tradeNode, "Override", override))
  709. return 0;
  710. if (override > 100) {
  711. this->invalidWarning(tradeNode["Override"], "Item trade override level %d exceeds 100, capping to 100.\n", override);
  712. override = 100;
  713. }
  714. item->gm_lv_trade_override = override;
  715. } else {
  716. if (!exists)
  717. item->gm_lv_trade_override = 100;
  718. }
  719. if (this->nodeExists(tradeNode, "NoDrop")) {
  720. bool active;
  721. if (!this->asBool(tradeNode, "NoDrop", active))
  722. return 0;
  723. item->flag.trade_restriction.drop = active;
  724. } else {
  725. if (!exists)
  726. item->flag.trade_restriction.drop = false;
  727. }
  728. if (this->nodeExists(tradeNode, "NoTrade")) {
  729. bool active;
  730. if (!this->asBool(tradeNode, "NoTrade", active))
  731. return 0;
  732. item->flag.trade_restriction.trade = active;
  733. } else {
  734. if (!exists)
  735. item->flag.trade_restriction.trade = false;
  736. }
  737. if (this->nodeExists(tradeNode, "TradePartner")) {
  738. bool active;
  739. if (!this->asBool(tradeNode, "TradePartner", active))
  740. return 0;
  741. item->flag.trade_restriction.trade_partner = active;
  742. } else {
  743. if (!exists)
  744. item->flag.trade_restriction.trade_partner = false;
  745. }
  746. if (this->nodeExists(tradeNode, "NoSell")) {
  747. bool active;
  748. if (!this->asBool(tradeNode, "NoSell", active))
  749. return 0;
  750. item->flag.trade_restriction.sell = active;
  751. } else {
  752. if (!exists)
  753. item->flag.trade_restriction.sell = false;
  754. }
  755. if (this->nodeExists(tradeNode, "NoCart")) {
  756. bool active;
  757. if (!this->asBool(tradeNode, "NoCart", active))
  758. return 0;
  759. item->flag.trade_restriction.cart = active;
  760. } else {
  761. if (!exists)
  762. item->flag.trade_restriction.cart = false;
  763. }
  764. if (this->nodeExists(tradeNode, "NoStorage")) {
  765. bool active;
  766. if (!this->asBool(tradeNode, "NoStorage", active))
  767. return 0;
  768. item->flag.trade_restriction.storage = active;
  769. } else {
  770. if (!exists)
  771. item->flag.trade_restriction.storage = false;
  772. }
  773. if (this->nodeExists(tradeNode, "NoGuildStorage")) {
  774. bool active;
  775. if (!this->asBool(tradeNode, "NoGuildStorage", active))
  776. return 0;
  777. item->flag.trade_restriction.guild_storage = active;
  778. } else {
  779. if (!exists)
  780. item->flag.trade_restriction.guild_storage = false;
  781. }
  782. if (this->nodeExists(tradeNode, "NoMail")) {
  783. bool active;
  784. if (!this->asBool(tradeNode, "NoMail", active))
  785. return 0;
  786. item->flag.trade_restriction.mail = active;
  787. } else {
  788. if (!exists)
  789. item->flag.trade_restriction.mail = false;
  790. }
  791. if (this->nodeExists(tradeNode, "NoAuction")) {
  792. bool active;
  793. if (!this->asBool(tradeNode, "NoAuction", active))
  794. return 0;
  795. item->flag.trade_restriction.auction = active;
  796. } else {
  797. if (!exists)
  798. item->flag.trade_restriction.auction = false;
  799. }
  800. } else {
  801. if (!exists) {
  802. item->gm_lv_trade_override = 100;
  803. item->flag.trade_restriction.drop = false;
  804. item->flag.trade_restriction.trade = false;
  805. item->flag.trade_restriction.trade_partner = false;
  806. item->flag.trade_restriction.sell = false;
  807. item->flag.trade_restriction.cart = false;
  808. item->flag.trade_restriction.storage = false;
  809. item->flag.trade_restriction.guild_storage = false;
  810. item->flag.trade_restriction.mail = false;
  811. item->flag.trade_restriction.auction = false;
  812. }
  813. }
  814. if (this->nodeExists(node, "Script")) {
  815. std::string script;
  816. if (!this->asString(node, "Script", script))
  817. return 0;
  818. if (exists && item->script) {
  819. script_free_code(item->script);
  820. item->script = nullptr;
  821. }
  822. item->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Script"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  823. } else {
  824. if (!exists)
  825. item->script = nullptr;
  826. }
  827. if (this->nodeExists(node, "EquipScript")) {
  828. std::string script;
  829. if (!this->asString(node, "EquipScript", script))
  830. return 0;
  831. if (exists && item->equip_script) {
  832. script_free_code(item->equip_script);
  833. item->equip_script = nullptr;
  834. }
  835. item->equip_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["EquipScript"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  836. } else {
  837. if (!exists)
  838. item->equip_script = nullptr;
  839. }
  840. if (this->nodeExists(node, "UnEquipScript")) {
  841. std::string script;
  842. if (!this->asString(node, "UnEquipScript", script))
  843. return 0;
  844. if (exists && item->unequip_script) {
  845. script_free_code(item->unequip_script);
  846. item->unequip_script = nullptr;
  847. }
  848. item->unequip_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["UnEquipScript"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  849. } else {
  850. if (!exists)
  851. item->unequip_script = nullptr;
  852. }
  853. if (!exists)
  854. this->put(nameid, item);
  855. return 1;
  856. }
  857. void ItemDatabase::loadingFinished(){
  858. for (auto &tmp_item : item_db) {
  859. std::shared_ptr<item_data> item = tmp_item.second;
  860. // Items that are consumed only after target confirmation
  861. if (item->type == IT_DELAYCONSUME) {
  862. item->type = IT_USABLE;
  863. item->flag.delay_consume |= DELAYCONSUME_TEMP;
  864. } else {
  865. item->flag.delay_consume &= ~DELAYCONSUME_TEMP; // Remove delayed consumption flag if switching types
  866. }
  867. if( item->type == IT_WEAPON ){
  868. if( item->weapon_level == 0 ){
  869. ShowWarning( "Item %s is a weapon, but does not have a weapon level. Consider adding it. Defaulting to 1.\n", item->name.c_str() );
  870. item->weapon_level = 1;
  871. }
  872. if( item->armor_level != 0 ){
  873. ShowWarning( "Item %s is a weapon, but has an armor level. Defaulting to 0.\n", item->name.c_str() );
  874. item->armor_level = 0;
  875. }
  876. }else if( item->type == IT_ARMOR ){
  877. if( item->armor_level == 0 ){
  878. ShowWarning( "Item %s is an armor, but does not have an armor level. Consider adding it. Defaulting to 1.\n", item->name.c_str() );
  879. item->armor_level = 1;
  880. }
  881. if( item->weapon_level != 0 ){
  882. ShowWarning( "Item %s is an armor, but has a weapon level. Defaulting to 0.\n", item->name.c_str() );
  883. item->weapon_level = 0;
  884. }
  885. }else{
  886. if( item->weapon_level != 0 ){
  887. ShowWarning( "Item %s is not a weapon, but has a weapon level. Defaulting to 0.\n", item->name.c_str() );
  888. item->weapon_level = 0;
  889. }
  890. if( item->armor_level != 0 ){
  891. ShowWarning( "Item %s is not an armor, but has an armor level. Defaulting to 0.\n", item->name.c_str() );
  892. item->armor_level = 0;
  893. }
  894. }
  895. // When a particular price is not given, we should base it off the other one
  896. if (item->value_buy == 0 && item->value_sell > 0)
  897. item->value_buy = item->value_sell * 2;
  898. else if (item->value_buy > 0 && item->value_sell == 0)
  899. item->value_sell = item->value_buy / 2;
  900. if (item->value_buy / 124. < item->value_sell / 75.) {
  901. ShowWarning("Buying/Selling [%d/%d] price of %s (%u) allows Zeny making exploit through buying/selling at discounted/overcharged prices! Defaulting Sell to 1 Zeny.\n", item->value_buy, item->value_sell, item->name.c_str(), item->nameid);
  902. item->value_sell = 1;
  903. }
  904. // Shields need to have a view ID to be able to be recognized by ST_SHIELD check in skill.cpp
  905. if( item->type == IT_ARMOR && ( item->equip & EQP_SHIELD ) != 0 && item->look == 0 ){
  906. ShowWarning( "Item %s (%u) is a shield and should have a view id. Defaulting to Guard...\n", item->name.c_str(), item->nameid );
  907. item->look = 1;
  908. }
  909. }
  910. if( !this->exists( ITEMID_DUMMY ) ){
  911. // Create dummy item
  912. std::shared_ptr<item_data> dummy_item = std::make_shared<item_data>();
  913. dummy_item->nameid = ITEMID_DUMMY;
  914. dummy_item->weight = 1;
  915. dummy_item->value_sell = 1;
  916. dummy_item->type = IT_ETC;
  917. dummy_item->name = "UNKNOWN_ITEM";
  918. dummy_item->ename = "Unknown Item";
  919. dummy_item->view_id = UNKNOWN_ITEM_ID;
  920. item_db.put( ITEMID_DUMMY, dummy_item );
  921. }
  922. TypesafeCachedYamlDatabase::loadingFinished();
  923. }
  924. /**
  925. * Applies gender restrictions according to settings.
  926. * @param node: YAML node containing the entry.
  927. * @param node: the already parsed item data.
  928. * @return gender that should be used.
  929. */
  930. e_sex ItemDatabase::defaultGender( const ryml::NodeRef& node, std::shared_ptr<item_data> id ){
  931. if (id->nameid == WEDDING_RING_M) //Grom Ring
  932. return SEX_MALE;
  933. if (id->nameid == WEDDING_RING_F) //Bride Ring
  934. return SEX_FEMALE;
  935. if( id->type == IT_WEAPON ){
  936. if( id->subtype == W_MUSICAL ){
  937. if( id->sex != SEX_MALE ){
  938. this->invalidWarning( node, "Musical instruments are always male-only, defaulting to SEX_MALE.\n" );
  939. }
  940. return SEX_MALE;
  941. }
  942. if( id->subtype == W_WHIP ){
  943. if( id->sex != SEX_FEMALE ){
  944. this->invalidWarning( node, "Whips are always female-only, defaulting to SEX_FEMALE.\n" );
  945. }
  946. return SEX_FEMALE;
  947. }
  948. }
  949. return static_cast<e_sex>( id->sex );
  950. }
  951. std::shared_ptr<item_data> ItemDatabase::search_aegisname( const char* name ){
  952. // Create a copy
  953. std::string lowername = name;
  954. // Convert it to lower
  955. util::tolower( lowername );
  956. return util::umap_find( this->aegisNameToItemDataMap, lowername );
  957. }
  958. std::shared_ptr<item_data> ItemDatabase::searchname( const char *name ){
  959. // Create a copy
  960. std::string lowername = name;
  961. // Convert it to lower
  962. util::tolower( lowername );
  963. std::shared_ptr<item_data> result = util::umap_find( this->aegisNameToItemDataMap, lowername );
  964. if( result != nullptr ){
  965. return result;
  966. }
  967. return util::umap_find( this->nameToItemDataMap, lowername );
  968. }
  969. /**
  970. * Generates an item link string
  971. * @param data: Item info
  972. * @return <ITEML> string for the item
  973. * @author [Cydh]
  974. **/
  975. std::string ItemDatabase::create_item_link( struct item& item ){
  976. std::shared_ptr<item_data> data = this->find( item.nameid );
  977. if( data == nullptr ){
  978. ShowError( "Tried to create itemlink for unknown item %u.\n", item.nameid );
  979. return "Unknown item";
  980. }
  981. // All these dates are unconfirmed
  982. #if PACKETVER >= 20100000
  983. if( !battle_config.feature_itemlink ){
  984. // Feature is disabled
  985. return data->ename;
  986. }
  987. struct item_data* id = data.get();
  988. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  989. const std::string start_tag = "<ITEML>";
  990. const std::string closing_tag = "</ITEML>";
  991. #else // PACKETVER >= 20100000
  992. const std::string start_tag = "<ITEMLINK>";
  993. const std::string closing_tag = "</ITEMLINK>";
  994. #endif
  995. std::string itemstr = start_tag;
  996. itemstr += util::string_left_pad(util::base62_encode(id->equip), '0', 5);
  997. itemstr += itemdb_isequip2(id) ? "1" : "0";
  998. itemstr += util::base62_encode(item.nameid);
  999. if (item.refine > 0) {
  1000. itemstr += "%" + util::string_left_pad(util::base62_encode(item.refine), '0', 2);
  1001. }
  1002. if (itemdb_isequip2(id)) {
  1003. itemstr += "&" + util::string_left_pad(util::base62_encode(id->look), '0', 2);
  1004. }
  1005. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  1006. itemstr += "'" + util::string_left_pad(util::base62_encode(item.enchantgrade), '0', 2);
  1007. #endif
  1008. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  1009. const std::string card_sep = ")";
  1010. const std::string optid_sep = "+";
  1011. const std::string optpar_sep = ",";
  1012. const std::string optval_sep = "-";
  1013. #else
  1014. const std::string card_sep = "(";
  1015. const std::string optid_sep = "*";
  1016. const std::string optpar_sep = "+";
  1017. const std::string optval_sep = ",";
  1018. #endif
  1019. for (uint8 i = 0; i < MAX_SLOTS; ++i) {
  1020. itemstr += card_sep + util::string_left_pad(util::base62_encode(item.card[i]), '0', 2);
  1021. }
  1022. #if PACKETVER >= 20150225
  1023. for (uint8 i = 0; i < MAX_ITEM_RDM_OPT; ++i) {
  1024. if (item.option[i].id == 0) {
  1025. break; // ignore options including ones beyond this one since the client won't even display them
  1026. }
  1027. // Option ID
  1028. itemstr += optid_sep + util::string_left_pad(util::base62_encode(item.option[i].id), '0', 2);
  1029. // Param
  1030. itemstr += optpar_sep + util::string_left_pad(util::base62_encode(item.option[i].param), '0', 2);
  1031. // Value
  1032. itemstr += optval_sep + util::string_left_pad(util::base62_encode(item.option[i].value), '0', 2);
  1033. }
  1034. #endif
  1035. itemstr += closing_tag;
  1036. return itemstr;
  1037. #else
  1038. // Did not exist before that
  1039. return data->ename;
  1040. #endif
  1041. }
  1042. std::string ItemDatabase::create_item_link( t_itemid id ){
  1043. struct item it = {};
  1044. it.nameid = id;
  1045. return this->create_item_link( it );
  1046. }
  1047. ItemDatabase item_db;
  1048. /**
  1049. * Check if an item exists in a group
  1050. * @param group_id: Item Group ID
  1051. * @param nameid: Item to check for in group
  1052. * @return True if item is in group, else false
  1053. */
  1054. bool ItemGroupDatabase::item_exists(uint16 group_id, t_itemid nameid)
  1055. {
  1056. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  1057. if (group == nullptr)
  1058. return false;
  1059. for (const auto &random : group->random) {
  1060. for (const auto &it : random.second->data) {
  1061. if (it.second->nameid == nameid)
  1062. return true;
  1063. }
  1064. }
  1065. return false;
  1066. }
  1067. /**
  1068. * Check if an item exists from a group in a player's inventory
  1069. * @param group_id: Item Group ID
  1070. * @return Item's index if found or -1 otherwise
  1071. */
  1072. int16 ItemGroupDatabase::item_exists_pc(map_session_data *sd, uint16 group_id)
  1073. {
  1074. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  1075. if (group == nullptr || group->random.empty())
  1076. return -1;
  1077. for (const auto &random : group->random) {
  1078. for (const auto &it : random.second->data) {
  1079. int16 item_position = pc_search_inventory(sd, it.second->nameid);
  1080. if (item_position != -1)
  1081. return item_position;
  1082. }
  1083. }
  1084. return -1;
  1085. }
  1086. const std::string LaphineSynthesisDatabase::getDefaultLocation(){
  1087. return std::string( db_path ) + "/laphine_synthesis.yml";
  1088. }
  1089. uint64 LaphineSynthesisDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1090. t_itemid item_id;
  1091. {
  1092. std::string name;
  1093. if( !this->asString( node, "Item", name ) ){
  1094. return 0;
  1095. }
  1096. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1097. if( id == nullptr ){
  1098. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1099. return 0;
  1100. }
  1101. item_id = id->nameid;
  1102. }
  1103. std::shared_ptr<s_laphine_synthesis> entry = this->find( item_id );
  1104. bool exists = entry != nullptr;
  1105. if( !exists ){
  1106. if( !this->nodesExist( node, { "RewardGroup", "Requirements" } ) ){
  1107. return 0;
  1108. }
  1109. entry = std::make_shared<s_laphine_synthesis>();
  1110. entry->item_id = item_id;
  1111. }
  1112. if( this->nodeExists( node, "RewardGroup" ) ){
  1113. std::string name;
  1114. if( !this->asString( node, "RewardGroup", name ) ){
  1115. return 0;
  1116. }
  1117. int64 constant;
  1118. if( !script_get_constant( ( "IG_" + name ).c_str(), &constant ) ){
  1119. this->invalidWarning( node["RewardGroup"], "Unknown reward group \"%s\".\n", name.c_str() );
  1120. return 0;
  1121. }
  1122. if( !itemdb_group.exists( (uint16)constant ) ){
  1123. this->invalidWarning( node["RewardGroup"], "Unknown reward group ID \"%" PRId64 "\".\n", constant );
  1124. return 0;
  1125. }
  1126. entry->rewardGroupId = (uint16)constant;
  1127. }else{
  1128. if( !exists ){
  1129. entry->rewardGroupId = 0;
  1130. }
  1131. }
  1132. if( this->nodeExists( node, "RequiredRequirementsCount" ) ){
  1133. uint16 amount;
  1134. if( !this->asUInt16( node, "RequiredRequirementsCount", amount ) ){
  1135. return 0;
  1136. }
  1137. entry->requiredRequirements = amount;
  1138. }else{
  1139. if( !exists ){
  1140. entry->requiredRequirements = 1;
  1141. }
  1142. }
  1143. if( this->nodeExists( node, "Requirements" ) ){
  1144. for( const ryml::NodeRef& requirementNode : node["Requirements"] ){
  1145. std::string name;
  1146. if( !this->asString( requirementNode, "Item", name ) ){
  1147. return 0;
  1148. }
  1149. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1150. if( id == nullptr ){
  1151. this->invalidWarning( requirementNode["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1152. return 0;
  1153. }
  1154. std::shared_ptr<s_laphine_synthesis_requirement> requirement = util::umap_find( entry->requirements, id->nameid );
  1155. bool requirement_exists = requirement != nullptr;
  1156. if( !requirement_exists ){
  1157. requirement = std::make_shared<s_laphine_synthesis_requirement>();
  1158. requirement->item_id = id->nameid;
  1159. }
  1160. if( this->nodeExists( requirementNode, "Amount" ) ){
  1161. uint16 amount;
  1162. if( !this->asUInt16( requirementNode, "Amount", amount ) ){
  1163. return 0;
  1164. }
  1165. if( amount > MAX_AMOUNT ){
  1166. this->invalidWarning( requirementNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  1167. amount = MAX_AMOUNT;
  1168. }
  1169. requirement->amount = amount;
  1170. }else{
  1171. if( !requirement_exists ){
  1172. requirement->amount = 1;
  1173. }
  1174. }
  1175. if( !requirement_exists ){
  1176. entry->requirements[id->nameid] = requirement;
  1177. }
  1178. }
  1179. }
  1180. if( this->nodeExists( node, "MinimumRefine" ) ){
  1181. uint16 refine;
  1182. if( !this->asUInt16( node, "MinimumRefine", refine ) ){
  1183. return 0;
  1184. }
  1185. if( refine > MAX_REFINE ){
  1186. this->invalidWarning( node["MinimumRefine"], "Minimum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1187. refine = MAX_REFINE;
  1188. }
  1189. entry->minimumRefine = refine;
  1190. }else{
  1191. if( !exists ){
  1192. entry->minimumRefine = 0;
  1193. }
  1194. }
  1195. if( this->nodeExists( node, "MaximumRefine" ) ){
  1196. uint16 refine;
  1197. if( !this->asUInt16( node, "MaximumRefine", refine ) ){
  1198. return 0;
  1199. }
  1200. if( refine > MAX_REFINE ){
  1201. this->invalidWarning( node["MaximumRefine"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1202. refine = MAX_REFINE;
  1203. }
  1204. entry->maximumRefine = refine;
  1205. }else{
  1206. if( !exists ){
  1207. entry->maximumRefine = MAX_REFINE;
  1208. }
  1209. }
  1210. if( !exists ){
  1211. this->put( entry->item_id, entry );
  1212. }
  1213. return 1;
  1214. }
  1215. LaphineSynthesisDatabase laphine_synthesis_db;
  1216. const std::string LaphineUpgradeDatabase::getDefaultLocation(){
  1217. return std::string( db_path ) + "/laphine_upgrade.yml";
  1218. }
  1219. uint64 LaphineUpgradeDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1220. t_itemid item_id;
  1221. {
  1222. std::string name;
  1223. if( !this->asString( node, "Item", name ) ){
  1224. return 0;
  1225. }
  1226. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1227. if( id == nullptr ){
  1228. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1229. return 0;
  1230. }
  1231. item_id = id->nameid;
  1232. }
  1233. std::shared_ptr<s_laphine_upgrade> entry = this->find( item_id );
  1234. bool exists = entry != nullptr;
  1235. if( !exists ){
  1236. if( !this->nodesExist( node, { "TargetItems" } ) ){
  1237. return 0;
  1238. }
  1239. entry = std::make_shared<s_laphine_upgrade>();
  1240. entry->item_id = item_id;
  1241. }
  1242. if( this->nodeExists( node, "RandomOptionGroup" ) ){
  1243. std::string name;
  1244. if( !this->asString( node, "RandomOptionGroup", name ) ){
  1245. return 0;
  1246. }
  1247. uint16 id;
  1248. if( !random_option_group.option_get_id( name, id ) ){
  1249. this->invalidWarning( node["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  1250. return 0;
  1251. }
  1252. entry->randomOptionGroup = random_option_group.find( id );
  1253. }else{
  1254. if( !exists ){
  1255. entry->randomOptionGroup = nullptr;
  1256. }
  1257. }
  1258. if( this->nodeExists( node, "TargetItems" ) ){
  1259. for( const ryml::NodeRef& targetNode : node["TargetItems"] ){
  1260. std::string name;
  1261. if( !this->asString( targetNode, "Item", name ) ){
  1262. return 0;
  1263. }
  1264. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1265. if( id == nullptr ){
  1266. this->invalidWarning( targetNode["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1267. return 0;
  1268. }
  1269. if( !util::vector_exists( entry->target_item_ids, id->nameid ) ){
  1270. entry->target_item_ids.push_back( id->nameid );
  1271. }
  1272. }
  1273. }
  1274. if( this->nodeExists( node, "MinimumRefine" ) ){
  1275. uint16 refine;
  1276. if( !this->asUInt16( node, "MinimumRefine", refine ) ){
  1277. return 0;
  1278. }
  1279. if( refine > MAX_REFINE ){
  1280. this->invalidWarning( node["MinimumRefine"], "Minimum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1281. refine = MAX_REFINE;
  1282. }
  1283. entry->minimumRefine = refine;
  1284. }else{
  1285. if( !exists ){
  1286. entry->minimumRefine = 0;
  1287. }
  1288. }
  1289. if( this->nodeExists( node, "MaximumRefine" ) ){
  1290. uint16 refine;
  1291. if( !this->asUInt16( node, "MaximumRefine", refine ) ){
  1292. return 0;
  1293. }
  1294. if( refine > MAX_REFINE ){
  1295. this->invalidWarning( node["MaximumRefine"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1296. refine = MAX_REFINE;
  1297. }
  1298. entry->maximumRefine = refine;
  1299. }else{
  1300. if( !exists ){
  1301. entry->maximumRefine = MAX_REFINE;
  1302. }
  1303. }
  1304. if( this->nodeExists( node, "RequiredRandomOptions" ) ){
  1305. uint16 amount;
  1306. if( !this->asUInt16( node, "RequiredRandomOptions", amount ) ){
  1307. return 0;
  1308. }
  1309. if( amount > MAX_ITEM_RDM_OPT ){
  1310. this->invalidWarning( node["RequiredRandomOptions"], "Required random option amount %hu is too high, capping to MAX_ITEM_RDM_OPT...\n", amount );
  1311. amount = MAX_ITEM_RDM_OPT;
  1312. }
  1313. entry->requiredRandomOptions = amount;
  1314. }else{
  1315. if( !exists ){
  1316. entry->requiredRandomOptions = 0;
  1317. }
  1318. }
  1319. if( this->nodeExists( node, "CardsAllowed" ) ){
  1320. bool allowed;
  1321. if( !this->asBool( node, "CardsAllowed", allowed ) ){
  1322. return 0;
  1323. }
  1324. entry->cardsAllowed = allowed;
  1325. }else{
  1326. if( !exists ){
  1327. entry->cardsAllowed = false;
  1328. }
  1329. }
  1330. if( this->nodeExists( node, "ResultRefine" ) ){
  1331. uint16 refine;
  1332. if( !this->asUInt16( node, "ResultRefine", refine ) ){
  1333. return 0;
  1334. }
  1335. if( refine > MAX_REFINE ){
  1336. this->invalidWarning( node["ResultRefine"], "Result refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1337. refine = MAX_REFINE;
  1338. }
  1339. entry->resultRefine = refine;
  1340. }else{
  1341. if( !exists ){
  1342. entry->resultRefine = 0;
  1343. }
  1344. }
  1345. if( this->nodeExists( node, "ResultRefineMinimum" ) ){
  1346. uint16 refine;
  1347. if( !this->asUInt16( node, "ResultRefineMinimum", refine ) ){
  1348. return 0;
  1349. }
  1350. if( refine > MAX_REFINE ){
  1351. this->invalidWarning( node["ResultRefineMinimum"], "Result refine minimum %hu is too high, capping to MAX_REFINE...\n", refine );
  1352. refine = MAX_REFINE;
  1353. }
  1354. entry->resultRefineMinimum = refine;
  1355. }else{
  1356. if( !exists ){
  1357. entry->resultRefineMinimum = 0;
  1358. }
  1359. }
  1360. if( this->nodeExists( node, "ResultRefineMaximum" ) ){
  1361. uint16 refine;
  1362. if( !this->asUInt16( node, "ResultRefineMaximum", refine ) ){
  1363. return 0;
  1364. }
  1365. if( refine > MAX_REFINE ){
  1366. this->invalidWarning( node["ResultRefineMaximum"], "Result refine maximum %hu is too high, capping to MAX_REFINE...\n", refine );
  1367. refine = MAX_REFINE;
  1368. }
  1369. entry->resultRefineMaximum = refine;
  1370. }else{
  1371. if( !exists ){
  1372. entry->resultRefineMaximum = 0;
  1373. }
  1374. }
  1375. if( !exists ){
  1376. this->put( entry->item_id, entry );
  1377. }
  1378. return 1;
  1379. }
  1380. LaphineUpgradeDatabase laphine_upgrade_db;
  1381. const std::string ItemReformDatabase::getDefaultLocation(){
  1382. return std::string( db_path ) + "/item_reform.yml";
  1383. }
  1384. uint64 ItemReformDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1385. t_itemid item_id;
  1386. {
  1387. std::string name;
  1388. if( !this->asString( node, "Item", name ) ){
  1389. return 0;
  1390. }
  1391. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1392. if( id == nullptr ){
  1393. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1394. return 0;
  1395. }
  1396. item_id = id->nameid;
  1397. }
  1398. std::shared_ptr<s_item_reform> entry = this->find( item_id );
  1399. bool exists = entry != nullptr;
  1400. if( !exists ){
  1401. if( !this->nodesExist( node, { "BaseItems" } ) ){
  1402. return 0;
  1403. }
  1404. entry = std::make_shared<s_item_reform>();
  1405. entry->item_id = item_id;
  1406. }
  1407. if( this->nodeExists( node, "BaseItems" ) ){
  1408. for( const ryml::NodeRef& baseNode : node["BaseItems"] ){
  1409. t_itemid base_itemid;
  1410. {
  1411. std::string name;
  1412. if( !this->asString( baseNode, "BaseItem", name ) ){
  1413. return 0;
  1414. }
  1415. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1416. if( id == nullptr ){
  1417. this->invalidWarning( baseNode["BaseItem"], "Unknown item \"%s\".\n", name.c_str() );
  1418. return 0;
  1419. }
  1420. base_itemid = id->nameid;
  1421. }
  1422. std::shared_ptr<s_item_reform_base> base = util::umap_find( entry->base_items, base_itemid );
  1423. bool base_exists = base != nullptr;
  1424. if( !base_exists ){
  1425. if( !this->nodesExist( baseNode, { "ResultItem" } ) ){
  1426. return 0;
  1427. }
  1428. base = std::make_shared<s_item_reform_base>();
  1429. base->item_id = base_itemid;
  1430. }
  1431. if( this->nodeExists( baseNode, "MinimumRefine" ) ){
  1432. uint16 refine;
  1433. if( !this->asUInt16( baseNode, "MinimumRefine", refine ) ){
  1434. return 0;
  1435. }
  1436. if( refine > MAX_REFINE ){
  1437. this->invalidWarning( baseNode["MinimumRefine"], "Minimum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1438. refine = MAX_REFINE;
  1439. }
  1440. base->minimumRefine = refine;
  1441. }else{
  1442. if( !base_exists ){
  1443. base->minimumRefine = 0;
  1444. }
  1445. }
  1446. if( this->nodeExists( baseNode, "MaximumRefine" ) ){
  1447. uint16 refine;
  1448. if( !this->asUInt16( baseNode, "MaximumRefine", refine ) ){
  1449. return 0;
  1450. }
  1451. if( refine > MAX_REFINE ){
  1452. this->invalidWarning( baseNode["MaximumRefine"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1453. refine = MAX_REFINE;
  1454. }
  1455. base->maximumRefine = refine;
  1456. }else{
  1457. if( !base_exists ){
  1458. base->maximumRefine = MAX_REFINE;
  1459. }
  1460. }
  1461. if( this->nodeExists( baseNode, "RequiredRandomOptions" ) ){
  1462. uint16 amount;
  1463. if( !this->asUInt16( baseNode, "RequiredRandomOptions", amount ) ){
  1464. return 0;
  1465. }
  1466. if( amount > MAX_ITEM_RDM_OPT ){
  1467. this->invalidWarning( baseNode["RequiredRandomOptions"], "Required random option amount %hu is too high, capping to MAX_ITEM_RDM_OPT...\n", amount );
  1468. amount = MAX_ITEM_RDM_OPT;
  1469. }
  1470. base->requiredRandomOptions = amount;
  1471. }else{
  1472. if( !base_exists ){
  1473. base->requiredRandomOptions = 0;
  1474. }
  1475. }
  1476. if( this->nodeExists( baseNode, "CardsAllowed" ) ){
  1477. bool allowed;
  1478. if( !this->asBool( baseNode, "CardsAllowed", allowed ) ){
  1479. return 0;
  1480. }
  1481. base->cardsAllowed = allowed;
  1482. }else{
  1483. if( !base_exists ){
  1484. base->cardsAllowed = true;
  1485. }
  1486. }
  1487. if( this->nodeExists( baseNode, "Materials" ) ){
  1488. for( const ryml::NodeRef& materialNode : baseNode["Materials"] ){
  1489. std::string name;
  1490. if( !this->asString( materialNode, "Material", name ) ){
  1491. return 0;
  1492. }
  1493. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1494. if( id == nullptr ){
  1495. this->invalidWarning( materialNode["Material"], "Unknown item \"%s\".\n", name.c_str() );
  1496. return 0;
  1497. }
  1498. t_itemid material_id = id->nameid;
  1499. bool material_exists = util::umap_find( base->materials, material_id ) != nullptr;
  1500. uint16 amount;
  1501. if( this->nodeExists( materialNode, "Amount" ) ){
  1502. if( !this->asUInt16( materialNode, "Amount", amount ) ){
  1503. return 0;
  1504. }
  1505. if( amount > MAX_AMOUNT ){
  1506. this->invalidWarning( materialNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  1507. amount = MAX_AMOUNT;
  1508. }
  1509. }else{
  1510. if( !material_exists ){
  1511. amount = 1;
  1512. }
  1513. }
  1514. if( amount > 0 ){
  1515. base->materials[material_id] = amount;
  1516. }else{
  1517. base->materials.erase( material_id );
  1518. }
  1519. }
  1520. }
  1521. if( this->nodeExists( baseNode, "ResultItem" ) ){
  1522. std::string name;
  1523. if( !this->asString( baseNode, "ResultItem", name ) ){
  1524. return 0;
  1525. }
  1526. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1527. if( id == nullptr ){
  1528. this->invalidWarning( baseNode["ResultItem"], "Unknown item \"%s\".\n", name.c_str() );
  1529. return 0;
  1530. }
  1531. base->resultItemId = id->nameid;
  1532. }
  1533. if( this->nodeExists( baseNode, "ChangeRefine" ) ){
  1534. int16 refine;
  1535. if( !this->asInt16( baseNode, "ChangeRefine", refine ) ){
  1536. return 0;
  1537. }
  1538. if( refine > MAX_REFINE ){
  1539. this->invalidWarning( baseNode["MaximumRefine"], "Refine change %hu is too high, capping to MAX_REFINE...\n", refine );
  1540. refine = MAX_REFINE;
  1541. }else if( refine < -MAX_REFINE ){
  1542. this->invalidWarning( baseNode["MaximumRefine"], "Refine change %hu is too low, capping to -MAX_REFINE...\n", refine );
  1543. refine = -MAX_REFINE;
  1544. }
  1545. base->refineChange = refine;
  1546. }else{
  1547. if( !base_exists ){
  1548. base->refineChange = 0;
  1549. }
  1550. }
  1551. if( this->nodeExists( baseNode, "RandomOptionGroup" ) ){
  1552. std::string name;
  1553. if( !this->asString( baseNode, "RandomOptionGroup", name ) ){
  1554. return 0;
  1555. }
  1556. uint16 id;
  1557. if( !random_option_group.option_get_id( name, id ) ){
  1558. this->invalidWarning( baseNode["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  1559. return 0;
  1560. }
  1561. base->randomOptionGroup = random_option_group.find( id );
  1562. }else{
  1563. if( !base_exists ){
  1564. base->randomOptionGroup = nullptr;
  1565. }
  1566. }
  1567. if( this->nodeExists( baseNode, "ClearSlots" ) ){
  1568. bool clear;
  1569. if( !this->asBool( baseNode, "ClearSlots", clear ) ){
  1570. return 0;
  1571. }
  1572. base->clearSlots = clear;
  1573. }else{
  1574. if( !base_exists ){
  1575. base->clearSlots = false;
  1576. }
  1577. }
  1578. if( this->nodeExists( baseNode, "RemoveEnchantgrade" ) ){
  1579. bool clear;
  1580. if( !this->asBool( baseNode, "RemoveEnchantgrade", clear ) ){
  1581. return 0;
  1582. }
  1583. base->removeEnchantgrade = clear;
  1584. }else{
  1585. if( !base_exists ){
  1586. base->removeEnchantgrade = false;
  1587. }
  1588. }
  1589. if( !base_exists ){
  1590. entry->base_items[base_itemid] = base;
  1591. }
  1592. }
  1593. }
  1594. if( !exists ){
  1595. this->put( entry->item_id, entry );
  1596. }
  1597. return 1;
  1598. }
  1599. ItemReformDatabase item_reform_db;
  1600. const std::string ItemEnchantDatabase::getDefaultLocation(){
  1601. return std::string( db_path ) + "/item_enchant.yml";
  1602. }
  1603. bool ItemEnchantDatabase::parseMaterials( const ryml::NodeRef& node, std::unordered_map<t_itemid, uint16>& materials ){
  1604. if( this->nodeExists( node, "Materials" ) ){
  1605. for( const ryml::NodeRef& materialNode : node["Materials"] ){
  1606. std::string name;
  1607. if( !this->asString( materialNode, "Material", name ) ){
  1608. return false;
  1609. }
  1610. std::shared_ptr<item_data> item = item_db.search_aegisname( name.c_str() );
  1611. if( item == nullptr ){
  1612. this->invalidWarning( materialNode["Material"], "Unknown item \"%s\".\n", name.c_str() );
  1613. return false;
  1614. }
  1615. t_itemid material_id = item->nameid;
  1616. bool material_exists = util::umap_find( materials, material_id ) != nullptr;
  1617. uint16 amount;
  1618. if( this->nodeExists( materialNode, "Amount" ) ){
  1619. if( !this->asUInt16( materialNode, "Amount", amount ) ){
  1620. return false;
  1621. }
  1622. if( amount > MAX_AMOUNT ){
  1623. this->invalidWarning( materialNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  1624. amount = MAX_AMOUNT;
  1625. }
  1626. }else{
  1627. if( !material_exists ){
  1628. amount = 1;
  1629. }else{
  1630. amount = materials[material_id];
  1631. }
  1632. }
  1633. if( amount > 0 ){
  1634. materials[material_id] = amount;
  1635. }else{
  1636. materials.erase( material_id );
  1637. }
  1638. }
  1639. }
  1640. return true;
  1641. }
  1642. uint64 ItemEnchantDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1643. uint64 id;
  1644. if( !this->asUInt64( node, "Id", id ) ){
  1645. return 0;
  1646. }
  1647. std::shared_ptr<s_item_enchant> enchant = this->find( id );
  1648. bool exists = enchant != nullptr;
  1649. if( !exists ){
  1650. if( !this->nodesExist( node, { "TargetItems", "Order", "Slots" } ) ){
  1651. return 0;
  1652. }
  1653. enchant = std::make_shared<s_item_enchant>();
  1654. enchant->id = id;
  1655. }
  1656. if( this->nodeExists( node, "TargetItems" ) ){
  1657. const ryml::NodeRef& targetItemsNode = node["TargetItems"];
  1658. for( const auto& it : targetItemsNode ){
  1659. std::string name;
  1660. c4::from_chars( it.key(), &name );
  1661. std::shared_ptr<item_data> item = item_db.search_aegisname( name.c_str() );
  1662. if( item == nullptr ){
  1663. this->invalidWarning( it, "Unknown item \"%s\".\n", name.c_str() );
  1664. return 0;
  1665. }
  1666. bool enable;
  1667. if( !this->asBool( targetItemsNode, name, enable ) ){
  1668. return 0;
  1669. }
  1670. if( enable ){
  1671. if( util::vector_exists( enchant->target_item_ids, item->nameid ) ){
  1672. this->invalidWarning( it, "Target item \"%s\" is already in the list.\n", name.c_str() );
  1673. }else{
  1674. enchant->target_item_ids.push_back( item->nameid );
  1675. }
  1676. }else{
  1677. if( !util::vector_exists( enchant->target_item_ids, item->nameid ) ){
  1678. this->invalidWarning( it, "Target item \"%s\" is not in the list.\n", name.c_str() );
  1679. }else{
  1680. util::vector_erase_if_exists( enchant->target_item_ids, item->nameid );
  1681. }
  1682. }
  1683. }
  1684. }
  1685. if( this->nodeExists( node, "MinimumRefine" ) ){
  1686. uint16 refine;
  1687. if( !this->asUInt16( node, "MinimumRefine", refine ) ){
  1688. return 0;
  1689. }
  1690. if( refine > MAX_REFINE ){
  1691. this->invalidWarning( node["MinimumRefine"], "Minimum refine %hu exceeds MAX_REFINE. Capping...\n", refine );
  1692. refine = MAX_REFINE;
  1693. }
  1694. enchant->minimumRefine = refine;
  1695. }else{
  1696. if( !exists ){
  1697. enchant->minimumRefine = 0;
  1698. }
  1699. }
  1700. if( this->nodeExists( node, "MinimumEnchantgrade" ) ){
  1701. uint16 enchantgrade;
  1702. if( !this->asUInt16( node, "MinimumEnchantgrade", enchantgrade ) ){
  1703. return 0;
  1704. }
  1705. if( enchantgrade > MAX_ENCHANTGRADE ){
  1706. this->invalidWarning( node["MinimumEnchantgrade"], "Minimum enchantgrade %hu exceeds MAX_ENCHANTGRADE. Capping...\n", enchantgrade );
  1707. enchantgrade = MAX_ENCHANTGRADE;
  1708. }
  1709. enchant->minimumEnchantgrade = enchantgrade;
  1710. }else{
  1711. if( !exists ){
  1712. enchant->minimumEnchantgrade = 0;
  1713. }
  1714. }
  1715. if( this->nodeExists( node, "AllowRandomOptions" ) ){
  1716. bool allow;
  1717. if( !this->asBool( node, "AllowRandomOptions", allow ) ){
  1718. return 0;
  1719. }
  1720. enchant->allowRandomOptions = allow;
  1721. }else{
  1722. if( !exists ){
  1723. enchant->allowRandomOptions = true;
  1724. }
  1725. }
  1726. if( this->nodeExists( node, "Reset" ) ){
  1727. const ryml::NodeRef& resetNode = node["Reset"];
  1728. if( this->nodeExists( resetNode, "Chance" ) ){
  1729. uint32 chance;
  1730. if( !this->asUInt32Rate( resetNode, "Chance", chance, 100000 ) ){
  1731. return 0;
  1732. }
  1733. enchant->reset.chance = chance;
  1734. }else{
  1735. if( !exists ){
  1736. enchant->reset.chance = 0;
  1737. }
  1738. }
  1739. if( this->nodeExists( resetNode, "Price" ) ){
  1740. uint32 zeny;
  1741. if( !this->asUInt32( resetNode, "Price", zeny ) ){
  1742. return 0;
  1743. }
  1744. if( zeny > MAX_ZENY ){
  1745. this->invalidWarning( resetNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1746. zeny = MAX_ZENY;
  1747. }
  1748. enchant->reset.zeny = zeny;
  1749. }else{
  1750. if( !exists ){
  1751. enchant->reset.zeny = 0;
  1752. }
  1753. }
  1754. if( !this->parseMaterials( resetNode, enchant->reset.materials ) ){
  1755. return 0;
  1756. }
  1757. }else{
  1758. if( !exists ){
  1759. enchant->reset = {};
  1760. }
  1761. }
  1762. if( this->nodeExists( node, "Order" ) ){
  1763. enchant->order.clear();
  1764. for( const auto& it : node["Order"] ){
  1765. uint16 slot;
  1766. if( !this->asUInt16( it, "Slot", slot ) ){
  1767. return 0;
  1768. }
  1769. if( slot >= MAX_SLOTS ){
  1770. this->invalidWarning( it, "Slot %hu exceeds MAX_SLOTS...\n", slot );
  1771. return 0;
  1772. }
  1773. enchant->order.push_back( slot );
  1774. }
  1775. }
  1776. if( this->nodeExists( node, "Slots" ) ){
  1777. for( const ryml::NodeRef& slotNode : node["Slots"] ){
  1778. uint16 slot;
  1779. if( !this->asUInt16( slotNode, "Slot", slot ) ){
  1780. return 0;
  1781. }
  1782. if( slot >= MAX_SLOTS ){
  1783. this->invalidWarning( slotNode["Slot"], "Slot %hu exceeds MAX_SLOTS...\n", slot );
  1784. return 0;
  1785. }
  1786. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  1787. bool slot_exists = enchant_slot != nullptr;
  1788. if( !slot_exists ){
  1789. enchant_slot = std::make_shared<s_item_enchant_slot>();
  1790. enchant_slot->slot = slot;
  1791. for( int i = 0; i <= MAX_ENCHANTGRADE; i++ ){
  1792. enchant_slot->normal.enchantgradeChanceIncrease[i] = 0;
  1793. }
  1794. }
  1795. if( this->nodeExists( slotNode, "Price" ) ){
  1796. uint32 zeny;
  1797. if( !this->asUInt32( slotNode, "Price", zeny ) ){
  1798. return 0;
  1799. }
  1800. if( zeny > MAX_ZENY ){
  1801. this->invalidWarning( slotNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1802. zeny = MAX_ZENY;
  1803. }
  1804. enchant_slot->normal.zeny = zeny;
  1805. }else{
  1806. if( !slot_exists ){
  1807. enchant_slot->normal.zeny = 0;
  1808. }
  1809. }
  1810. if( !this->parseMaterials( slotNode, enchant_slot->normal.materials ) ){
  1811. return 0;
  1812. }
  1813. if( this->nodeExists( slotNode, "Chance" ) ){
  1814. uint32 chance;
  1815. if( !this->asUInt32Rate( slotNode, "Chance", chance, 100000 ) ){
  1816. return 0;
  1817. }
  1818. enchant_slot->normal.chance = chance;
  1819. }else{
  1820. if( !slot_exists ){
  1821. enchant_slot->normal.chance = 100000;
  1822. }
  1823. }
  1824. if( this->nodeExists( slotNode, "EnchantgradeBonus" ) ){
  1825. for( const ryml::NodeRef& enchantgradeNode : slotNode["EnchantgradeBonus"] ){
  1826. uint16 enchantgrade;
  1827. if( !this->asUInt16( enchantgradeNode, "Enchantgrade", enchantgrade ) ){
  1828. return 0;
  1829. }
  1830. if( enchantgrade > MAX_ENCHANTGRADE ){
  1831. this->invalidWarning( enchantgradeNode["Enchantgrade"], "Enchant grade %hu exceeds MAX_ENCHANTGRADE.\n", enchantgrade );
  1832. return 0;
  1833. }
  1834. uint32 chance;
  1835. if( !this->asUInt32Rate( slotNode, "Chance", chance, 100000 ) ){
  1836. return 0;
  1837. }
  1838. enchant_slot->normal.enchantgradeChanceIncrease[enchantgrade] = chance;
  1839. }
  1840. }
  1841. if( this->nodeExists( slotNode, "Enchants" ) ){
  1842. for( const ryml::NodeRef& enchantNode : slotNode["Enchants"] ){
  1843. uint16 enchantgrade;
  1844. if( !this->asUInt16( enchantNode, "Enchantgrade", enchantgrade ) ){
  1845. return 0;
  1846. }
  1847. if( enchantgrade > MAX_ENCHANTGRADE ){
  1848. this->invalidWarning( enchantNode["Enchantgrade"], "Enchant grade %hu exceeds MAX_ENCHANTGRADE...\n", enchantgrade );
  1849. return 0;
  1850. }
  1851. std::shared_ptr<s_item_enchant_normal> enchants_for_enchantgrade = util::umap_find( enchant_slot->normal.enchants, enchantgrade );
  1852. bool enchants_for_enchantgrade_exists = enchants_for_enchantgrade != nullptr;
  1853. if( !enchants_for_enchantgrade_exists ){
  1854. enchants_for_enchantgrade = std::make_shared<s_item_enchant_normal>();
  1855. enchants_for_enchantgrade->enchantgrade = enchantgrade;
  1856. }
  1857. if( this->nodeExists( enchantNode, "Items" ) ){
  1858. for( const ryml::NodeRef& itemNode : enchantNode["Items"] ){
  1859. std::string enchant_name;
  1860. if( !this->asString( itemNode, "Item", enchant_name ) ){
  1861. return 0;
  1862. }
  1863. std::shared_ptr<item_data> enchant_item = item_db.search_aegisname( enchant_name.c_str() );
  1864. if( enchant_item == nullptr ){
  1865. this->invalidWarning( itemNode["Item"], "Unknown item \"%s\".\n", enchant_name.c_str() );
  1866. return 0;
  1867. }
  1868. std::shared_ptr<s_item_enchant_normal_sub> enchant = util::umap_find( enchants_for_enchantgrade->enchants, enchant_item->nameid );
  1869. bool enchant_exists = enchant != nullptr;
  1870. if( !enchant_exists ){
  1871. if( !this->nodesExist( itemNode, { "Chance" } ) ){
  1872. return 0;
  1873. }
  1874. enchant = std::make_shared<s_item_enchant_normal_sub>();
  1875. enchant->item_id = enchant_item->nameid;
  1876. }
  1877. if( this->nodeExists( itemNode, "Chance" ) ){
  1878. uint32 chance;
  1879. if( !this->asUInt32Rate( itemNode, "Chance", chance, 100000 ) ){
  1880. return 0;
  1881. }
  1882. enchant->chance = chance;
  1883. }
  1884. if( !enchant_exists ){
  1885. enchants_for_enchantgrade->enchants[enchant->item_id] = enchant;
  1886. }
  1887. }
  1888. }
  1889. if( !enchants_for_enchantgrade_exists ){
  1890. enchant_slot->normal.enchants[enchantgrade] = enchants_for_enchantgrade;
  1891. }
  1892. }
  1893. }
  1894. if( this->nodeExists( slotNode, "PerfectEnchants" ) ){
  1895. for( const ryml::NodeRef& enchantNode : slotNode["PerfectEnchants"] ){
  1896. std::string enchant_name;
  1897. if( !this->asString( enchantNode, "Item", enchant_name ) ){
  1898. return 0;
  1899. }
  1900. std::shared_ptr<item_data> enchant_item = item_db.search_aegisname( enchant_name.c_str() );
  1901. if( enchant_item == nullptr ){
  1902. this->invalidWarning( enchantNode["Item"], "Unknown item \"%s\".\n", enchant_name.c_str() );
  1903. return 0;
  1904. }
  1905. std::shared_ptr<s_item_enchant_perfect> enchant = util::umap_find( enchant_slot->perfect.enchants, enchant_item->nameid );
  1906. bool enchant_exists = enchant != nullptr;
  1907. if( !enchant_exists ){
  1908. enchant = std::make_shared<s_item_enchant_perfect>();
  1909. enchant->item_id = enchant_item->nameid;
  1910. }
  1911. if( this->nodeExists( enchantNode, "Price" ) ){
  1912. uint32 zeny;
  1913. if( !this->asUInt32( enchantNode, "Price", zeny ) ){
  1914. return 0;
  1915. }
  1916. if( zeny > MAX_ZENY ){
  1917. this->invalidWarning( enchantNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1918. zeny = MAX_ZENY;
  1919. }
  1920. enchant->zeny = zeny;
  1921. }else{
  1922. if( !slot_exists ){
  1923. enchant->zeny = 0;
  1924. }
  1925. }
  1926. if( !this->parseMaterials( enchantNode, enchant->materials ) ){
  1927. return 0;
  1928. }
  1929. if( !enchant_exists ){
  1930. enchant_slot->perfect.enchants[enchant->item_id] = enchant;
  1931. }
  1932. }
  1933. }
  1934. if( this->nodeExists( slotNode, "Upgrades" ) ){
  1935. for( const ryml::NodeRef& upgradeNode : slotNode["Upgrades"] ){
  1936. std::string enchant_name;
  1937. if( !this->asString( upgradeNode, "Enchant", enchant_name ) ){
  1938. return 0;
  1939. }
  1940. std::shared_ptr<item_data> enchant_item = item_db.search_aegisname( enchant_name.c_str() );
  1941. if( enchant_item == nullptr ){
  1942. this->invalidWarning( upgradeNode["Enchant"], "Unknown item \"%s\".\n", enchant_name.c_str() );
  1943. return 0;
  1944. }
  1945. std::shared_ptr<s_item_enchant_upgrade> enchant_upgrade = util::umap_find( enchant_slot->upgrade.enchants, enchant_item->nameid );
  1946. bool enchant_upgrade_exists = enchant_upgrade != nullptr;
  1947. if( !enchant_upgrade_exists ){
  1948. if( !this->nodesExist( upgradeNode, { "Upgrade" } ) ){
  1949. return 0;
  1950. }
  1951. enchant_upgrade = std::make_shared<s_item_enchant_upgrade>();
  1952. enchant_upgrade->enchant_item_id = enchant_item->nameid;
  1953. }
  1954. if( this->nodeExists( upgradeNode, "Upgrade" ) ){
  1955. std::string upgrade_name;
  1956. if( !this->asString( upgradeNode, "Upgrade", upgrade_name ) ){
  1957. return 0;
  1958. }
  1959. std::shared_ptr<item_data> upgrade_item = item_db.search_aegisname( upgrade_name.c_str() );
  1960. if( upgrade_item == nullptr ){
  1961. this->invalidWarning( upgradeNode["Upgrade"], "Unknown item \"%s\".\n", upgrade_name.c_str() );
  1962. return 0;
  1963. }
  1964. enchant_upgrade->upgrade_item_id = upgrade_item->nameid;
  1965. }
  1966. if( this->nodeExists( upgradeNode, "Price" ) ){
  1967. uint32 zeny;
  1968. if( !this->asUInt32( upgradeNode, "Price", zeny ) ){
  1969. return 0;
  1970. }
  1971. if( zeny > MAX_ZENY ){
  1972. this->invalidWarning( upgradeNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1973. zeny = MAX_ZENY;
  1974. }
  1975. enchant_upgrade->zeny = zeny;
  1976. }else{
  1977. if( !enchant_upgrade_exists ){
  1978. enchant_upgrade->zeny = 0;
  1979. }
  1980. }
  1981. if( !this->parseMaterials( upgradeNode, enchant_upgrade->materials ) ){
  1982. return 0;
  1983. }
  1984. if( !enchant_upgrade_exists ){
  1985. enchant_slot->upgrade.enchants[enchant_upgrade->enchant_item_id] = enchant_upgrade;
  1986. }
  1987. }
  1988. }
  1989. if( !slot_exists ){
  1990. enchant->slots[slot] = enchant_slot;
  1991. }
  1992. }
  1993. }
  1994. if( !exists ){
  1995. this->put( enchant->id, enchant );
  1996. }
  1997. return 1;
  1998. }
  1999. ItemEnchantDatabase item_enchant_db;
  2000. const std::string ItemPackageDatabase::getDefaultLocation(){
  2001. return std::string( db_path ) + "/item_packages.yml";
  2002. }
  2003. uint64 ItemPackageDatabase::parseBodyNode( const ryml::NodeRef& node ){
  2004. t_itemid item_id;
  2005. {
  2006. std::string name;
  2007. if( !this->asString( node, "Item", name ) ){
  2008. return 0;
  2009. }
  2010. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  2011. if( id == nullptr ){
  2012. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  2013. return 0;
  2014. }
  2015. item_id = id->nameid;
  2016. }
  2017. std::shared_ptr<s_item_package> entry = this->find( item_id );
  2018. bool exists = entry != nullptr;
  2019. if( !exists ){
  2020. if( !this->nodesExist( node, { "Groups" } ) ){
  2021. return 0;
  2022. }
  2023. entry = std::make_shared<s_item_package>();
  2024. entry->item_id = item_id;
  2025. }
  2026. if( this->nodeExists( node, "Groups" ) ){
  2027. for( const ryml::NodeRef& groupNode : node["Groups"] ){
  2028. uint32 groupIndex;
  2029. if( !this->asUInt32( groupNode, "Group", groupIndex) ){
  2030. return 0;
  2031. }
  2032. std::shared_ptr<s_item_package_group> group = util::umap_find( entry->groups, groupIndex );
  2033. bool group_exists = group != nullptr;
  2034. if( !group_exists ){
  2035. if( !this->nodesExist( groupNode, { "Items" } ) ){
  2036. return 0;
  2037. }
  2038. group = std::make_shared<s_item_package_group>();
  2039. group->groupIndex = groupIndex;
  2040. }
  2041. for( const ryml::NodeRef& itemNode : groupNode["Items"] ){
  2042. std::string name;
  2043. if( !this->asString( itemNode, "Item", name ) ){
  2044. return 0;
  2045. }
  2046. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  2047. if( id == nullptr ){
  2048. this->invalidWarning( itemNode["Item"], "Unknown item \"%s\".\n", name.c_str() );
  2049. return 0;
  2050. }
  2051. std::shared_ptr<s_item_package_item> package_item = util::umap_find( group->items, id->nameid );
  2052. bool package_item_exists = package_item != nullptr;
  2053. if( !package_item_exists ){
  2054. package_item = std::make_shared<s_item_package_item>();
  2055. package_item->item_id = id->nameid;
  2056. }
  2057. if( this->nodeExists( itemNode, "Amount" ) ){
  2058. uint16 amount;
  2059. if( !this->asUInt16( itemNode, "Amount", amount ) ){
  2060. return 0;
  2061. }
  2062. if( amount > MAX_AMOUNT ){
  2063. this->invalidWarning( itemNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  2064. amount = MAX_AMOUNT;
  2065. }else if( amount == 0 ){
  2066. if( !package_item_exists ){
  2067. this->invalidWarning( itemNode["Amount"], "Trying to remove non existant item \"%s\".\n", name.c_str() );
  2068. return 0;
  2069. }else{
  2070. group->items.erase( id->nameid );
  2071. }
  2072. }
  2073. package_item->amount = amount;
  2074. }else{
  2075. if( !package_item_exists ){
  2076. package_item->amount = 1;
  2077. }
  2078. }
  2079. if( this->nodeExists( itemNode, "RentalHours" ) ){
  2080. uint16 rentalhours;
  2081. if( !this->asUInt16( itemNode, "RentalHours", rentalhours ) ){
  2082. return 0;
  2083. }
  2084. package_item->rentalhours = rentalhours;
  2085. }else{
  2086. if( !package_item_exists ){
  2087. package_item->rentalhours = 0;
  2088. }
  2089. }
  2090. if( this->nodeExists( itemNode, "Refine" ) ){
  2091. uint16 refine;
  2092. if( !this->asUInt16( itemNode, "Refine", refine ) ){
  2093. return 0;
  2094. }
  2095. if( refine > MAX_REFINE ){
  2096. this->invalidWarning( itemNode["Refine"], "Refine %hu is too high, capping to MAX_REFINE...\n", refine );
  2097. refine = MAX_REFINE;
  2098. }
  2099. package_item->refine = refine;
  2100. }else{
  2101. if( !package_item_exists ){
  2102. package_item->refine = 0;
  2103. }
  2104. }
  2105. if( this->nodeExists( itemNode, "RandomOptionGroup" ) ){
  2106. std::string name;
  2107. if( !this->asString( itemNode, "RandomOptionGroup", name ) ){
  2108. return 0;
  2109. }
  2110. uint16 option_group_id;
  2111. if( !random_option_group.option_get_id( name, option_group_id ) ){
  2112. this->invalidWarning( itemNode["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  2113. return 0;
  2114. }
  2115. package_item->randomOptionGroup = random_option_group.find( option_group_id );
  2116. }else{
  2117. if( !package_item_exists ){
  2118. package_item->randomOptionGroup = nullptr;
  2119. }
  2120. }
  2121. if( !package_item_exists ){
  2122. group->items[package_item->item_id] = package_item;
  2123. }
  2124. }
  2125. if( !group_exists ){
  2126. entry->groups[group->groupIndex] = group;
  2127. }
  2128. }
  2129. }
  2130. if( !exists ){
  2131. this->put( entry->item_id, entry );
  2132. }
  2133. return 1;
  2134. }
  2135. ItemPackageDatabase item_package_db;
  2136. /*==========================================
  2137. * Finds up to N matches. Returns number of matches [Skotlex]
  2138. * @param *data
  2139. * @param size
  2140. * @param str
  2141. * @return Number of matches item
  2142. *------------------------------------------*/
  2143. uint16 itemdb_searchname_array(std::map<t_itemid, std::shared_ptr<item_data>> &data, uint16 size, const char *str)
  2144. {
  2145. for (const auto &item : item_db) {
  2146. std::shared_ptr<item_data> id = item.second;
  2147. if (id == nullptr)
  2148. continue;
  2149. if (stristr(id->name.c_str(), str) != nullptr || stristr(id->ename.c_str(), str) != nullptr || strcmpi(id->ename.c_str(), str) == 0)
  2150. data[id->nameid] = id;
  2151. }
  2152. if (data.size() > size)
  2153. util::map_resize(data, size);
  2154. return static_cast<uint16>(data.size());
  2155. }
  2156. std::shared_ptr<s_item_group_entry> get_random_itemsubgroup(std::shared_ptr<s_item_group_random> random) {
  2157. if (random == nullptr)
  2158. return nullptr;
  2159. for (size_t j = 0, max = random->data.size() * 3; j < max; j++) {
  2160. std::shared_ptr<s_item_group_entry> entry = util::umap_random(random->data);
  2161. if (entry->rate == 0 || rnd() % random->total_rate < entry->rate) // always return entry for rate 0 ('must' item)
  2162. return entry;
  2163. }
  2164. return util::umap_random(random->data);
  2165. }
  2166. /**
  2167. * Return a random group entry from Item Group
  2168. * @param group_id
  2169. * @param sub_group: 0 is 'must' item group, random groups start from 1
  2170. * @return Item group entry or NULL on fail
  2171. */
  2172. std::shared_ptr<s_item_group_entry> ItemGroupDatabase::get_random_entry(uint16 group_id, uint8 sub_group) {
  2173. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  2174. if (group == nullptr) {
  2175. ShowError("get_random_entry: Invalid group id %hu.\n", group_id);
  2176. return nullptr;
  2177. }
  2178. if (group->random.empty()) {
  2179. ShowError("get_random_entry: No item entries for group id %hu.\n", group_id);
  2180. return nullptr;
  2181. }
  2182. if (group->random.count(sub_group) == 0) {
  2183. ShowError("get_random_entry: No item entries for group id %hu and sub group %hu.\n", group_id, sub_group);
  2184. return nullptr;
  2185. }
  2186. return get_random_itemsubgroup(group->random[sub_group]);
  2187. }
  2188. /**
  2189. * Return a random Item ID from Item Group
  2190. * @param group_id
  2191. * @param sub_group: 0 is 'must' item group, random groups start from 1
  2192. * @return Item ID or UNKNOWN_ITEM_ID on fail
  2193. */
  2194. t_itemid ItemGroupDatabase::get_random_item_id(uint16 group_id, uint8 sub_group) {
  2195. std::shared_ptr<s_item_group_entry> entry = this->get_random_entry(group_id, sub_group);
  2196. return entry != nullptr ? entry->nameid : UNKNOWN_ITEM_ID;
  2197. }
  2198. /** [Cydh]
  2199. * Gives item(s) to the player based on item group
  2200. * @param sd: Player that obtains item from item group
  2201. * @param group_id: The group ID of item that obtained by player
  2202. * @param *group: struct s_item_group from itemgroup_db[group_id].random[idx] or itemgroup_db[group_id].must[sub_group][idx]
  2203. */
  2204. static void itemdb_pc_get_itemgroup_sub(map_session_data *sd, bool identify, std::shared_ptr<s_item_group_entry> data) {
  2205. if (data == nullptr)
  2206. return;
  2207. item tmp = {};
  2208. tmp.nameid = data->nameid;
  2209. tmp.bound = data->bound;
  2210. tmp.identify = identify ? identify : itemdb_isidentified(data->nameid);
  2211. tmp.expire_time = (data->duration) ? (unsigned int)(time(NULL) + data->duration*60) : 0;
  2212. if (data->isNamed) {
  2213. tmp.card[0] = itemdb_isequip(data->nameid) ? CARD0_FORGE : CARD0_CREATE;
  2214. tmp.card[1] = 0;
  2215. tmp.card[2] = GetWord(sd->status.char_id, 0);
  2216. tmp.card[3] = GetWord(sd->status.char_id, 1);
  2217. }
  2218. uint16 get_amt = 0;
  2219. if (itemdb_isstackable(data->nameid) && data->isStacked)
  2220. get_amt = data->amount;
  2221. else
  2222. get_amt = 1;
  2223. tmp.amount = get_amt;
  2224. // Do loop for non-stackable item
  2225. for (uint16 i = 0; i < data->amount; i += get_amt) {
  2226. char flag = 0;
  2227. tmp.unique_id = data->GUID ? pc_generate_unique_id(sd) : 0; // Generate GUID
  2228. if( itemdb_isequip( data->nameid ) ){
  2229. if( data->refineMinimum > 0 && data->refineMaximum > 0 ){
  2230. tmp.refine = rnd_value( data->refineMinimum, data->refineMaximum );
  2231. }else if( data->refineMinimum > 0 ){
  2232. tmp.refine = rnd_value( data->refineMinimum, MAX_REFINE );
  2233. }else if( data->refineMaximum > 0 ){
  2234. tmp.refine = rnd_value( 1, data->refineMaximum );
  2235. }else{
  2236. tmp.refine = 0;
  2237. }
  2238. if( data->randomOptionGroup != nullptr ){
  2239. memset( tmp.option, 0, sizeof( tmp.option ) );
  2240. data->randomOptionGroup->apply( tmp );
  2241. }
  2242. }
  2243. if ((flag = pc_additem(sd, &tmp, get_amt, LOG_TYPE_SCRIPT))) {
  2244. clif_additem(sd, 0, 0, flag);
  2245. if (pc_candrop(sd, &tmp))
  2246. map_addflooritem(&tmp, tmp.amount, sd->bl.m, sd->bl.x,sd->bl.y, 0, 0, 0, 0, 0);
  2247. }
  2248. else if (!flag && data->isAnnounced)
  2249. intif_broadcast_obtain_special_item(sd, data->nameid, sd->itemid, ITEMOBTAIN_TYPE_BOXITEM);
  2250. }
  2251. }
  2252. /** [Cydh]
  2253. * Find item(s) that will be obtained by player based on Item Group
  2254. * @param group_id: The group ID that will be gained by player
  2255. * @param nameid: The item that trigger this item group
  2256. * @return val: 0:success, 1:no sd, 2:invalid item group
  2257. */
  2258. uint8 ItemGroupDatabase::pc_get_itemgroup(uint16 group_id, bool identify, map_session_data *sd) {
  2259. nullpo_retr(1,sd);
  2260. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  2261. if (group == nullptr) {
  2262. ShowError("pc_get_itemgroup: Invalid group id '%d' specified.\n",group_id);
  2263. return 2;
  2264. }
  2265. if (group->random.empty())
  2266. return 0;
  2267. // Get all the 'must' item(s) (subgroup 0)
  2268. uint16 subgroup = 0;
  2269. std::shared_ptr<s_item_group_random> random = util::umap_find(group->random, subgroup);
  2270. if (random != nullptr && !random->data.empty()) {
  2271. for (const auto &it : random->data)
  2272. itemdb_pc_get_itemgroup_sub(sd, identify, it.second);
  2273. }
  2274. // Get 1 'random' item from each subgroup
  2275. for (const auto &random : group->random) {
  2276. if (random.first == 0 || random.second->data.empty())
  2277. continue;
  2278. itemdb_pc_get_itemgroup_sub(sd, identify, get_random_itemsubgroup(random.second));
  2279. }
  2280. return 0;
  2281. }
  2282. /** Searches for the item_data. Use this to check if item exists or not.
  2283. * @param nameid
  2284. * @return *item_data if item is exist, or NULL if not
  2285. */
  2286. std::shared_ptr<item_data> itemdb_exists(t_itemid nameid) {
  2287. return item_db.find(nameid);
  2288. }
  2289. /// Returns name type of ammunition [Cydh]
  2290. const char *itemdb_typename_ammo (e_ammo_type ammo) {
  2291. switch (ammo) {
  2292. case AMMO_ARROW: return "Arrow";
  2293. case AMMO_DAGGER: return "Throwable Dagger";
  2294. case AMMO_BULLET: return "Bullet";
  2295. case AMMO_SHELL: return "Shell";
  2296. case AMMO_GRENADE: return "Grenade";
  2297. case AMMO_SHURIKEN: return "Shuriken";
  2298. case AMMO_KUNAI: return "Kunai";
  2299. case AMMO_CANNONBALL: return "Cannonball";
  2300. case AMMO_THROWWEAPON: return "Throwable Item/Sling Item";
  2301. }
  2302. return "Ammunition";
  2303. }
  2304. /// Returns human readable name for given item type.
  2305. /// @param type Type id to retrieve name for ( IT_* ).
  2306. const char* itemdb_typename(enum item_types type)
  2307. {
  2308. switch(type)
  2309. {
  2310. case IT_HEALING: return "Potion/Food";
  2311. case IT_USABLE: return "Usable";
  2312. case IT_ETC: return "Etc.";
  2313. case IT_WEAPON: return "Weapon";
  2314. case IT_ARMOR: return "Armor";
  2315. case IT_CARD: return "Card";
  2316. case IT_PETEGG: return "Pet Egg";
  2317. case IT_PETARMOR: return "Pet Accessory";
  2318. case IT_AMMO: return "Arrow/Ammunition";
  2319. case IT_DELAYCONSUME: return "Delay-Consume Usable";
  2320. case IT_SHADOWGEAR: return "Shadow Equipment";
  2321. case IT_CASH: return "Cash Usable";
  2322. }
  2323. return "Unknown Type";
  2324. }
  2325. /**
  2326. * Converts the jobmask from the format in itemdb to the format used by the map server.
  2327. * @param bclass: Pointer to store itemdb format
  2328. * @param jobmask: Job Mask to convert
  2329. * @param active: Whether the flag is active or not
  2330. * @author: Skotlex
  2331. */
  2332. static void itemdb_jobid2mapid(uint64 bclass[3], e_mapid jobmask, bool active)
  2333. {
  2334. uint64 temp_mask[3] = { 0 };
  2335. if (jobmask != MAPID_ALL) {
  2336. // Calculate the required bit to set
  2337. uint64 job = 1ULL << ( jobmask & MAPID_BASEMASK );
  2338. // 2-1
  2339. if( ( jobmask & JOBL_2_1 ) != 0 ){
  2340. temp_mask[1] |= job;
  2341. // 2-2
  2342. }else if( ( jobmask & JOBL_2_2 ) != 0 ){
  2343. temp_mask[2] |= job;
  2344. // Basejob
  2345. }else{
  2346. temp_mask[0] |= job;
  2347. }
  2348. } else {
  2349. temp_mask[0] = temp_mask[1] = temp_mask[2] = MAPID_ALL;
  2350. }
  2351. for (int i = 0; i < ARRAYLENGTH(temp_mask); i++) {
  2352. if (temp_mask[i] == 0)
  2353. continue;
  2354. if (active)
  2355. bclass[i] |= temp_mask[i];
  2356. else
  2357. bclass[i] &= ~temp_mask[i];
  2358. }
  2359. }
  2360. /*==========================================
  2361. * Loads an item from the db. If not found, it will return the dummy item.
  2362. * @param nameid
  2363. * @return *item_data or *dummy_item if item not found
  2364. *------------------------------------------*/
  2365. struct item_data* itemdb_search(t_itemid nameid) {
  2366. std::shared_ptr<item_data> id;
  2367. if (!(id = item_db.find(nameid))) {
  2368. ShowWarning("itemdb_search: Item ID %u does not exists in the item_db. Using dummy data.\n", nameid);
  2369. id = item_db.find(ITEMID_DUMMY);
  2370. }
  2371. return id.get();
  2372. }
  2373. /** Checks if item is equip type or not
  2374. * @param id Item data
  2375. * @return True if item is equip, false otherwise
  2376. */
  2377. bool itemdb_isequip2(struct item_data *id) {
  2378. nullpo_ret(id);
  2379. switch (id->type) {
  2380. case IT_WEAPON:
  2381. case IT_ARMOR:
  2382. case IT_AMMO:
  2383. case IT_SHADOWGEAR:
  2384. return true;
  2385. default:
  2386. return false;
  2387. }
  2388. }
  2389. /** Checks if item is stackable or not
  2390. * @param id Item data
  2391. * @return True if item is stackable, false otherwise
  2392. */
  2393. bool itemdb_isstackable2(struct item_data *id)
  2394. {
  2395. nullpo_ret(id);
  2396. return id->isStackable();
  2397. }
  2398. /*==========================================
  2399. * Trade Restriction functions [Skotlex]
  2400. *------------------------------------------*/
  2401. bool itemdb_isdropable_sub(struct item_data *item, int gmlv, int unused) {
  2402. return (item && (!(item->flag.trade_restriction.drop) || gmlv >= item->gm_lv_trade_override));
  2403. }
  2404. bool itemdb_cantrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  2405. return (item && (!(item->flag.trade_restriction.trade) || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  2406. }
  2407. bool itemdb_canpartnertrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  2408. return (item && (item->flag.trade_restriction.trade_partner || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  2409. }
  2410. bool itemdb_cansell_sub(struct item_data* item, int gmlv, int unused) {
  2411. return (item && (!(item->flag.trade_restriction.sell) || gmlv >= item->gm_lv_trade_override));
  2412. }
  2413. bool itemdb_cancartstore_sub(struct item_data* item, int gmlv, int unused) {
  2414. return (item && (!(item->flag.trade_restriction.cart) || gmlv >= item->gm_lv_trade_override));
  2415. }
  2416. bool itemdb_canstore_sub(struct item_data* item, int gmlv, int unused) {
  2417. return (item && (!(item->flag.trade_restriction.storage) || gmlv >= item->gm_lv_trade_override));
  2418. }
  2419. bool itemdb_canguildstore_sub(struct item_data* item, int gmlv, int unused) {
  2420. return (item && (!(item->flag.trade_restriction.guild_storage) || gmlv >= item->gm_lv_trade_override));
  2421. }
  2422. bool itemdb_canmail_sub(struct item_data* item, int gmlv, int unused) {
  2423. return (item && (!(item->flag.trade_restriction.mail) || gmlv >= item->gm_lv_trade_override));
  2424. }
  2425. bool itemdb_canauction_sub(struct item_data* item, int gmlv, int unused) {
  2426. return (item && (!(item->flag.trade_restriction.auction) || gmlv >= item->gm_lv_trade_override));
  2427. }
  2428. bool itemdb_isrestricted(struct item* item, int gmlv, int gmlv2, bool (*func)(struct item_data*, int, int))
  2429. {
  2430. struct item_data* item_data = itemdb_search(item->nameid);
  2431. int i;
  2432. if (!func(item_data, gmlv, gmlv2))
  2433. return false;
  2434. if(item_data->slots == 0 || itemdb_isspecial(item->card[0]))
  2435. return true;
  2436. for(i = 0; i < item_data->slots; i++) {
  2437. if (!item->card[i]) continue;
  2438. if (!func(itemdb_search(item->card[i]), gmlv, gmlv2))
  2439. return false;
  2440. }
  2441. return true;
  2442. }
  2443. bool itemdb_ishatched_egg(struct item* item) {
  2444. if (item && item->card[0] == CARD0_PET && item->attribute == 1)
  2445. return true;
  2446. return false;
  2447. }
  2448. /** Specifies if item-type should drop unidentified.
  2449. * @param nameid ID of item
  2450. */
  2451. char itemdb_isidentified(t_itemid nameid) {
  2452. int type=itemdb_type(nameid);
  2453. switch (type) {
  2454. case IT_WEAPON:
  2455. case IT_ARMOR:
  2456. case IT_PETARMOR:
  2457. case IT_SHADOWGEAR:
  2458. return 0;
  2459. default:
  2460. return 1;
  2461. }
  2462. }
  2463. const std::string ItemGroupDatabase::getDefaultLocation() {
  2464. return std::string(db_path) + "/item_group_db.yml";
  2465. }
  2466. /**
  2467. * Reads and parses an entry from the item_group_db.
  2468. * @param node: YAML node containing the entry.
  2469. * @return count of successfully parsed rows
  2470. */
  2471. uint64 ItemGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  2472. std::string group_name;
  2473. if (!this->asString(node, "Group", group_name))
  2474. return 0;
  2475. std::string group_name_constant = "IG_" + group_name;
  2476. int64 constant;
  2477. if (!script_get_constant(group_name_constant.c_str(), &constant) || constant < IG_BLUEBOX) {
  2478. if (strncasecmp(group_name.c_str(), "IG_", 3) != 0)
  2479. this->invalidWarning(node["Group"], "Invalid group %s.\n", group_name.c_str());
  2480. else
  2481. this->invalidWarning(node["Group"], "Invalid group %s. Note that 'IG_' is automatically appended to the group name.\n", group_name.c_str());
  2482. return 0;
  2483. }
  2484. uint16 id = static_cast<uint16>(constant);
  2485. std::shared_ptr<s_item_group_db> group = this->find(id);
  2486. bool exists = group != nullptr;
  2487. if (!exists) {
  2488. group = std::make_shared<s_item_group_db>();
  2489. group->id = id;
  2490. }
  2491. if (this->nodeExists(node, "SubGroups")) {
  2492. const auto& subNode = node["SubGroups"];
  2493. for (const auto& subit : subNode) {
  2494. if (this->nodeExists(subit, "Clear")) {
  2495. uint16 id;
  2496. if (!this->asUInt16(subit, "Clear", id))
  2497. continue;
  2498. if (group->random.erase(id) == 0)
  2499. this->invalidWarning(subit["Clear"], "The SubGroup %hu doesn't exist in the group %s. Clear failed.\n", id, group_name.c_str());
  2500. continue;
  2501. }
  2502. if (!this->nodesExist(subit, { "SubGroup", "List" })) {
  2503. continue;
  2504. }
  2505. uint16 subgroup;
  2506. if (this->nodeExists(subit, "SubGroup")) {
  2507. if (!this->asUInt16(subit, "SubGroup", subgroup))
  2508. continue;
  2509. } else {
  2510. subgroup = 1;
  2511. }
  2512. std::shared_ptr<s_item_group_random> random = util::umap_find(group->random, subgroup);
  2513. if (random == nullptr) {
  2514. random = std::make_shared<s_item_group_random>();
  2515. group->random[subgroup] = random;
  2516. }
  2517. const auto& listNode = subit["List"];
  2518. for (const auto& listit : listNode) {
  2519. if (this->nodeExists(listit, "Clear")) {
  2520. std::string item_name;
  2521. if (!this->asString(listit, "Clear", item_name))
  2522. continue;
  2523. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  2524. if (item == nullptr) {
  2525. this->invalidWarning(listit["Clear"], "Unknown Item %s. Clear failed.\n", item_name.c_str());
  2526. continue;
  2527. }
  2528. if (random->data.erase(item->nameid) == 0)
  2529. this->invalidWarning(listit["Clear"], "Item %hu doesn't exist in the SubGroup %hu (group %s). Clear failed.\n", item->nameid, subgroup, group_name.c_str());
  2530. continue;
  2531. }
  2532. std::string item_name;
  2533. if (!this->asString(listit, "Item", item_name))
  2534. continue;
  2535. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  2536. if (item == nullptr) {
  2537. this->invalidWarning(listit["Item"], "Unknown Item %s.\n", item_name.c_str());
  2538. continue;
  2539. }
  2540. std::shared_ptr<s_item_group_entry> entry = util::umap_find(random->data, item->nameid);
  2541. bool entry_exists = entry != nullptr;
  2542. if (!entry_exists) {
  2543. entry = std::make_shared<s_item_group_entry>();
  2544. random->data[item->nameid] = entry;
  2545. }
  2546. entry->nameid = item->nameid;
  2547. if (this->nodeExists(listit, "Rate")) {
  2548. uint16 rate;
  2549. if (!this->asUInt16(listit, "Rate", rate))
  2550. continue;
  2551. entry->rate = rate;
  2552. } else {
  2553. if (!entry_exists)
  2554. entry->rate = 0;
  2555. }
  2556. if (subgroup == 0 && entry->rate > 0) {
  2557. this->invalidWarning(listit["Item"], "SubGroup 0 is reserved for item without Rate ('must' item). Defaulting Rate to 0.\n");
  2558. entry->rate = 0;
  2559. }
  2560. if (subgroup != 0 && entry->rate == 0) {
  2561. this->invalidWarning(listit["Item"], "Entry must have a Rate for group above 0, skipping.\n");
  2562. continue;
  2563. }
  2564. if (this->nodeExists(listit, "Amount")) {
  2565. uint16 amount;
  2566. if (!this->asUInt16(listit, "Amount", amount))
  2567. continue;
  2568. entry->amount = cap_value(amount, 1, MAX_AMOUNT);
  2569. } else {
  2570. if (!entry_exists)
  2571. entry->amount = 1;
  2572. }
  2573. if (this->nodeExists(listit, "Duration")) {
  2574. uint16 duration;
  2575. if (!this->asUInt16(listit, "Duration", duration))
  2576. continue;
  2577. entry->duration = duration;
  2578. } else {
  2579. if (!entry_exists)
  2580. entry->duration = 0;
  2581. }
  2582. if (this->nodeExists(listit, "Announced")) {
  2583. bool isAnnounced;
  2584. if (!this->asBool(listit, "Announced", isAnnounced))
  2585. continue;
  2586. entry->isAnnounced = isAnnounced;
  2587. } else {
  2588. if (!entry_exists)
  2589. entry->isAnnounced = false;
  2590. }
  2591. if (this->nodeExists(listit, "UniqueId")) {
  2592. bool guid;
  2593. if (!this->asBool(listit, "UniqueId", guid))
  2594. continue;
  2595. entry->GUID = guid;
  2596. } else {
  2597. if (!entry_exists)
  2598. entry->GUID = item->flag.guid;
  2599. }
  2600. if (this->nodeExists(listit, "Stacked")) {
  2601. bool isStacked;
  2602. if (!this->asBool(listit, "Stacked", isStacked))
  2603. continue;
  2604. entry->isStacked = isStacked;
  2605. } else {
  2606. if (!entry_exists)
  2607. entry->isStacked = true;
  2608. }
  2609. if (this->nodeExists(listit, "Named")) {
  2610. bool named;
  2611. if (!this->asBool(listit, "Named", named))
  2612. continue;
  2613. entry->isNamed = named;
  2614. } else {
  2615. if (!entry_exists)
  2616. entry->isNamed = false;
  2617. }
  2618. if (this->nodeExists(listit, "Bound")) {
  2619. std::string bound_name;
  2620. if (!this->asString(listit, "Bound", bound_name))
  2621. continue;
  2622. std::string bound_name_constant = "BOUND_" + bound_name;
  2623. int64 bound;
  2624. if (!script_get_constant(bound_name_constant.c_str(), &bound) || bound < BOUND_NONE || bound >= BOUND_MAX) {
  2625. this->invalidWarning(listit["Group"], "Invalid Bound %s.\n", bound_name.c_str());
  2626. continue;
  2627. }
  2628. entry->bound = static_cast<uint8>(bound);
  2629. } else {
  2630. if (!entry_exists)
  2631. entry->bound = BOUND_NONE;
  2632. }
  2633. if( this->nodeExists( listit, "RandomOptionGroup" ) ){
  2634. std::string name;
  2635. if( !this->asString( listit, "RandomOptionGroup", name ) ){
  2636. return 0;
  2637. }
  2638. uint16 id;
  2639. if( !random_option_group.option_get_id( name, id ) ){
  2640. this->invalidWarning( listit["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  2641. return 0;
  2642. }
  2643. entry->randomOptionGroup = random_option_group.find( id );
  2644. }else{
  2645. if( !entry_exists ){
  2646. entry->randomOptionGroup = nullptr;
  2647. }
  2648. }
  2649. if( this->nodeExists( listit, "RefineMinimum" ) ){
  2650. uint16 refine;
  2651. if( !this->asUInt16( listit, "RefineMinimum", refine ) ){
  2652. return 0;
  2653. }
  2654. if( refine > MAX_REFINE ){
  2655. this->invalidWarning( listit["RefineMinimum"], "Minimum refine % hu is too high, capping to MAX_REFINE...\n", refine );
  2656. refine = MAX_REFINE;
  2657. }
  2658. entry->refineMinimum = refine;
  2659. }else{
  2660. if( !exists ){
  2661. entry->refineMinimum = 0;
  2662. }
  2663. }
  2664. if( this->nodeExists( listit, "RefineMaximum" ) ){
  2665. uint16 refine;
  2666. if( !this->asUInt16( listit, "RefineMaximum", refine ) ){
  2667. return 0;
  2668. }
  2669. if( refine > MAX_REFINE ){
  2670. this->invalidWarning( listit["RefineMaximum"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  2671. refine = MAX_REFINE;
  2672. }
  2673. entry->refineMaximum = refine;
  2674. }else{
  2675. if( !exists ){
  2676. entry->refineMaximum = 0;
  2677. }
  2678. }
  2679. }
  2680. }
  2681. }
  2682. if (!exists)
  2683. this->put(id, group);
  2684. return 1;
  2685. }
  2686. void ItemGroupDatabase::loadingFinished() {
  2687. for (const auto &group : *this) {
  2688. for (const auto &random : group.second->random) {
  2689. random.second->total_rate = 0;
  2690. for (const auto &it : random.second->data) {
  2691. random.second->total_rate += it.second->rate;
  2692. }
  2693. }
  2694. }
  2695. TypesafeYamlDatabase::loadingFinished();
  2696. }
  2697. /** Read item forbidden by mapflag (can't equip item)
  2698. * Structure: <nameid>,<mode>
  2699. */
  2700. static bool itemdb_read_noequip(char* str[], int columns, int current) {
  2701. t_itemid nameid;
  2702. int flag;
  2703. nameid = strtoul(str[0], nullptr, 10);
  2704. flag = atoi(str[1]);
  2705. std::shared_ptr<item_data> id = item_db.find(nameid);
  2706. if( id == nullptr )
  2707. {
  2708. ShowWarning("itemdb_read_noequip: Invalid item id %u.\n", nameid);
  2709. return false;
  2710. }
  2711. if (flag >= 0)
  2712. id->flag.no_equip |= flag;
  2713. else
  2714. id->flag.no_equip &= ~abs(flag);
  2715. return true;
  2716. }
  2717. const std::string ComboDatabase::getDefaultLocation() {
  2718. return std::string(db_path) + "/item_combos.yml";
  2719. }
  2720. uint16 ComboDatabase::find_combo_id( const std::vector<t_itemid>& items ){
  2721. for (const auto &it : *this) {
  2722. if (it.second->nameid == items) {
  2723. return it.first;
  2724. }
  2725. }
  2726. return 0;
  2727. }
  2728. /**
  2729. * Reads and parses an entry from the item_combos.
  2730. * @param node: YAML node containing the entry.
  2731. * @return count of successfully parsed rows
  2732. */
  2733. uint64 ComboDatabase::parseBodyNode(const ryml::NodeRef& node) {
  2734. std::vector<std::vector<t_itemid>> items_list;
  2735. if( !this->nodesExist( node, { "Combos" } ) ){
  2736. return 0;
  2737. }
  2738. const ryml::NodeRef& combosNode = node["Combos"];
  2739. for (const auto& comboit : combosNode) {
  2740. static const std::string nodeName = "Combo";
  2741. if (!this->nodesExist(comboit, { nodeName })) {
  2742. return 0;
  2743. }
  2744. const ryml::NodeRef& comboNode = comboit["Combo"];
  2745. if (!comboNode.is_seq()) {
  2746. this->invalidWarning(comboNode, "%s should be a sequence.\n", nodeName.c_str());
  2747. return 0;
  2748. }
  2749. std::vector<t_itemid> items = {};
  2750. for (const auto it : comboNode) {
  2751. std::string item_name;
  2752. c4::from_chars(it.val(), &item_name);
  2753. std::shared_ptr<item_data> item = item_db.search_aegisname(item_name.c_str());
  2754. if (item == nullptr) {
  2755. this->invalidWarning(it, "Invalid item %s, skipping.\n", item_name.c_str());
  2756. return 0;
  2757. }
  2758. items.push_back(item->nameid);
  2759. }
  2760. if (items.empty()) {
  2761. this->invalidWarning(comboNode, "Empty combo, skipping.\n");
  2762. return 0;
  2763. }
  2764. if (items.size() < 2) {
  2765. this->invalidWarning(comboNode, "Not enough item to make a combo (need at least 2). Skipping.\n");
  2766. return 0;
  2767. }
  2768. std::sort(items.begin(), items.end());
  2769. items_list.push_back(items);
  2770. }
  2771. if (items_list.empty()) {
  2772. this->invalidWarning(combosNode, "No combos defined, skipping.\n");
  2773. return 0;
  2774. }
  2775. if (this->nodeExists(node, "Clear")) {
  2776. bool clear = false;
  2777. if (!this->asBool(node, "Clear", clear))
  2778. return 0;
  2779. // Remove the combo (if exists)
  2780. if (clear) {
  2781. for (const auto& itemsit : items_list) {
  2782. uint16 id = this->find_combo_id(itemsit);
  2783. if (id == 0) {
  2784. this->invalidWarning(node["Clear"], "Unable to clear the combo.\n");
  2785. return 0;
  2786. }
  2787. this->erase(id);
  2788. }
  2789. return 1;
  2790. }
  2791. }
  2792. uint64 count = 0;
  2793. for (const auto &itemsit : items_list) {
  2794. // Find the id when the combo exists
  2795. uint16 id = this->find_combo_id(itemsit);
  2796. std::shared_ptr<s_item_combo> combo = this->find(id);
  2797. bool exists = combo != nullptr;
  2798. if (!exists) {
  2799. combo = std::make_shared<s_item_combo>();
  2800. combo->nameid.insert(combo->nameid.begin(), itemsit.begin(), itemsit.end());
  2801. combo->id = ++this->combo_num;
  2802. }
  2803. if (this->nodeExists(node, "Script")) {
  2804. std::string script;
  2805. if (!this->asString(node, "Script", script))
  2806. return 0;
  2807. if (exists) {
  2808. script_free_code(combo->script);
  2809. combo->script = nullptr;
  2810. }
  2811. combo->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Script"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  2812. } else {
  2813. if (!exists) {
  2814. combo->script = nullptr;
  2815. }
  2816. }
  2817. if (!exists)
  2818. this->put( combo->id, combo );
  2819. count++;
  2820. }
  2821. return count;
  2822. }
  2823. void ComboDatabase::loadingFinished() {
  2824. // Populate item_data to refer to the combo
  2825. for (const auto &combo : *this) {
  2826. for (const auto &itm : combo.second->nameid) {
  2827. std::shared_ptr<item_data> it = item_db.find(itm);
  2828. if (it != nullptr)
  2829. it->combos.push_back(combo.second);
  2830. }
  2831. }
  2832. TypesafeYamlDatabase::loadingFinished();
  2833. }
  2834. /**
  2835. * Process Roulette items
  2836. */
  2837. bool itemdb_parse_roulette_db(void)
  2838. {
  2839. int i, j;
  2840. uint32 count = 0;
  2841. // retrieve all rows from the item database
  2842. if (SQL_ERROR == Sql_Query(mmysql_handle, "SELECT * FROM `%s`", roulette_table)) {
  2843. Sql_ShowDebug(mmysql_handle);
  2844. return false;
  2845. }
  2846. for (i = 0; i < MAX_ROULETTE_LEVEL; i++)
  2847. rd.items[i] = 0;
  2848. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  2849. int k, limit = MAX_ROULETTE_COLUMNS - i;
  2850. for (k = 0; k < limit && SQL_SUCCESS == Sql_NextRow(mmysql_handle); k++) {
  2851. char* data;
  2852. t_itemid item_id;
  2853. unsigned short amount;
  2854. int level, flag;
  2855. Sql_GetData(mmysql_handle, 1, &data, NULL); level = atoi(data);
  2856. Sql_GetData(mmysql_handle, 2, &data, NULL); item_id = strtoul(data, nullptr, 10);
  2857. Sql_GetData(mmysql_handle, 3, &data, NULL); amount = atoi(data);
  2858. Sql_GetData(mmysql_handle, 4, &data, NULL); flag = atoi(data);
  2859. if (!item_db.exists(item_id)) {
  2860. ShowWarning("itemdb_parse_roulette_db: Unknown item ID '%u' in level '%d'\n", item_id, level);
  2861. continue;
  2862. }
  2863. if (amount < 1 || amount > MAX_AMOUNT){
  2864. ShowWarning("itemdb_parse_roulette_db: Unsupported amount '%hu' for item ID '%u' in level '%d'\n", amount, item_id, level);
  2865. continue;
  2866. }
  2867. if (flag < 0 || flag > 1) {
  2868. ShowWarning("itemdb_parse_roulette_db: Unsupported flag '%d' for item ID '%u' in level '%d'\n", flag, item_id, level);
  2869. continue;
  2870. }
  2871. j = rd.items[i];
  2872. RECREATE(rd.nameid[i], t_itemid, ++rd.items[i]);
  2873. RECREATE(rd.qty[i], unsigned short, rd.items[i]);
  2874. RECREATE(rd.flag[i], int, rd.items[i]);
  2875. rd.nameid[i][j] = item_id;
  2876. rd.qty[i][j] = amount;
  2877. rd.flag[i][j] = flag;
  2878. ++count;
  2879. }
  2880. }
  2881. // free the query result
  2882. Sql_FreeResult(mmysql_handle);
  2883. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  2884. int limit = MAX_ROULETTE_COLUMNS - i;
  2885. if (rd.items[i] == limit)
  2886. continue;
  2887. if (rd.items[i] > limit) {
  2888. ShowWarning("itemdb_parse_roulette_db: level %d has %d items, only %d supported, capping...\n", i + 1, rd.items[i], limit);
  2889. rd.items[i] = limit;
  2890. continue;
  2891. }
  2892. /** this scenario = rd.items[i] < limit **/
  2893. ShowWarning("itemdb_parse_roulette_db: Level %d has %d items, %d are required. Filling with Apples...\n", i + 1, rd.items[i], limit);
  2894. rd.items[i] = limit;
  2895. RECREATE(rd.nameid[i], t_itemid, rd.items[i]);
  2896. RECREATE(rd.qty[i], unsigned short, rd.items[i]);
  2897. RECREATE(rd.flag[i], int, rd.items[i]);
  2898. for (j = 0; j < MAX_ROULETTE_COLUMNS - i; j++) {
  2899. if (rd.qty[i][j])
  2900. continue;
  2901. rd.nameid[i][j] = ITEMID_APPLE;
  2902. rd.qty[i][j] = 1;
  2903. rd.flag[i][j] = 0;
  2904. }
  2905. }
  2906. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, roulette_table);
  2907. return true;
  2908. }
  2909. /**
  2910. * Free Roulette items
  2911. */
  2912. static void itemdb_roulette_free(void) {
  2913. int i;
  2914. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  2915. if (rd.nameid[i])
  2916. aFree(rd.nameid[i]);
  2917. if (rd.qty[i])
  2918. aFree(rd.qty[i]);
  2919. if (rd.flag[i])
  2920. aFree(rd.flag[i]);
  2921. rd.nameid[i] = NULL;
  2922. rd.qty[i] = NULL;
  2923. rd.flag[i] = NULL;
  2924. rd.items[i] = 0;
  2925. }
  2926. }
  2927. /**
  2928. * Convert SQL data to YAML Node
  2929. * @param str: Array of parsed SQL data
  2930. * @return True on success or false otherwise
  2931. */
  2932. static bool itemdb_read_sqldb_sub(std::vector<std::string> str) {
  2933. ryml::Tree tree;
  2934. ryml::NodeRef rootNode = tree.rootref();
  2935. rootNode |= ryml::MAP;
  2936. int32 index = -1;
  2937. rootNode["Id"] << str[++index];
  2938. if (!str[++index].empty())
  2939. rootNode["AegisName"] << str[index];
  2940. if (!str[++index].empty())
  2941. rootNode["Name"] << str[index];
  2942. if (!str[++index].empty())
  2943. rootNode["Type"] << str[index];
  2944. if (!str[++index].empty())
  2945. rootNode["SubType"] << str[index];
  2946. if (!str[++index].empty())
  2947. rootNode["Buy"] << str[index];
  2948. if (!str[++index].empty())
  2949. rootNode["Sell"] << str[index];
  2950. if (!str[++index].empty())
  2951. rootNode["Weight"] << str[index];
  2952. if (!str[++index].empty())
  2953. rootNode["Attack"] << str[index];
  2954. if (!str[++index].empty())
  2955. rootNode["Defense"] << str[index];
  2956. if (!str[++index].empty())
  2957. rootNode["Range"] << str[index];
  2958. if (!str[++index].empty())
  2959. rootNode["Slots"] << str[index];
  2960. ryml::NodeRef jobs = rootNode["Jobs"];
  2961. jobs |= ryml::MAP;
  2962. if (!str[++index].empty())
  2963. jobs["All"] << (std::stoi(str[index]) ? "true" : "false");
  2964. if (!str[++index].empty())
  2965. jobs["Acolyte"] << (std::stoi(str[index]) ? "true" : "false");
  2966. if (!str[++index].empty())
  2967. jobs["Alchemist"] << (std::stoi(str[index]) ? "true" : "false");
  2968. if (!str[++index].empty())
  2969. jobs["Archer"] << (std::stoi(str[index]) ? "true" : "false");
  2970. if (!str[++index].empty())
  2971. jobs["Assassin"] << (std::stoi(str[index]) ? "true" : "false");
  2972. if (!str[++index].empty())
  2973. jobs["BardDancer"] << (std::stoi(str[index]) ? "true" : "false");
  2974. if (!str[++index].empty())
  2975. jobs["Blacksmith"] << (std::stoi(str[index]) ? "true" : "false");
  2976. if (!str[++index].empty())
  2977. jobs["Crusader"] << (std::stoi(str[index]) ? "true" : "false");
  2978. if (!str[++index].empty())
  2979. jobs["Gunslinger"] << (std::stoi(str[index]) ? "true" : "false");
  2980. if (!str[++index].empty())
  2981. jobs["Hunter"] << (std::stoi(str[index]) ? "true" : "false");
  2982. if (!str[++index].empty())
  2983. jobs["Knight"] << (std::stoi(str[index]) ? "true" : "false");
  2984. if (!str[++index].empty())
  2985. jobs["Mage"] << (std::stoi(str[index]) ? "true" : "false");
  2986. if (!str[++index].empty())
  2987. jobs["Merchant"] << (std::stoi(str[index]) ? "true" : "false");
  2988. if (!str[++index].empty())
  2989. jobs["Monk"] << (std::stoi(str[index]) ? "true" : "false");
  2990. if (!str[++index].empty())
  2991. jobs["Ninja"] << (std::stoi(str[index]) ? "true" : "false");
  2992. if (!str[++index].empty())
  2993. jobs["Novice"] << (std::stoi(str[index]) ? "true" : "false");
  2994. if (!str[++index].empty())
  2995. jobs["Priest"] << (std::stoi(str[index]) ? "true" : "false");
  2996. if (!str[++index].empty())
  2997. jobs["Rogue"] << (std::stoi(str[index]) ? "true" : "false");
  2998. if (!str[++index].empty())
  2999. jobs["Sage"] << (std::stoi(str[index]) ? "true" : "false");
  3000. if (!str[++index].empty())
  3001. jobs["SoulLinker"] << (std::stoi(str[index]) ? "true" : "false");
  3002. if (!str[++index].empty())
  3003. jobs["StarGladiator"] << (std::stoi(str[index]) ? "true" : "false");
  3004. if (!str[++index].empty())
  3005. jobs["SuperNovice"] << (std::stoi(str[index]) ? "true" : "false");
  3006. if (!str[++index].empty())
  3007. jobs["Swordman"] << (std::stoi(str[index]) ? "true" : "false");
  3008. if (!str[++index].empty())
  3009. jobs["Taekwon"] << (std::stoi(str[index]) ? "true" : "false");
  3010. if (!str[++index].empty())
  3011. jobs["Thief"] << (std::stoi(str[index]) ? "true" : "false");
  3012. if (!str[++index].empty())
  3013. jobs["Wizard"] << (std::stoi(str[index]) ? "true" : "false");
  3014. ryml::NodeRef classes = rootNode["Classes"];
  3015. classes |= ryml::MAP;
  3016. if (!str[++index].empty())
  3017. classes["All"] << (std::stoi(str[index]) ? "true" : "false");
  3018. if (!str[++index].empty())
  3019. classes["Normal"] << (std::stoi(str[index]) ? "true" : "false");
  3020. if (!str[++index].empty())
  3021. classes["Upper"] << (std::stoi(str[index]) ? "true" : "false");
  3022. if (!str[++index].empty())
  3023. classes["Baby"] << (std::stoi(str[index]) ? "true" : "false");
  3024. if (!str[++index].empty())
  3025. rootNode["Gender"] << str[index];
  3026. ryml::NodeRef locations = rootNode["Locations"];
  3027. locations |= ryml::MAP;
  3028. if (!str[++index].empty())
  3029. locations["Head_Top"] << (std::stoi(str[index]) ? "true" : "false");
  3030. if (!str[++index].empty())
  3031. locations["Head_Mid"] << (std::stoi(str[index]) ? "true" : "false");
  3032. if (!str[++index].empty())
  3033. locations["Head_Low"] << (std::stoi(str[index]) ? "true" : "false");
  3034. if (!str[++index].empty())
  3035. locations["Armor"] << (std::stoi(str[index]) ? "true" : "false");
  3036. if (!str[++index].empty())
  3037. locations["Right_Hand"] << (std::stoi(str[index]) ? "true" : "false");
  3038. if (!str[++index].empty())
  3039. locations["Left_Hand"] << (std::stoi(str[index]) ? "true" : "false");
  3040. if (!str[++index].empty())
  3041. locations["Garment"] << (std::stoi(str[index]) ? "true" : "false");
  3042. if (!str[++index].empty())
  3043. locations["Shoes"] << (std::stoi(str[index]) ? "true" : "false");
  3044. if (!str[++index].empty())
  3045. locations["Right_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3046. if (!str[++index].empty())
  3047. locations["Left_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3048. if (!str[++index].empty())
  3049. locations["Costume_Head_Top"] << (std::stoi(str[index]) ? "true" : "false");
  3050. if (!str[++index].empty())
  3051. locations["Costume_Head_Mid"] << (std::stoi(str[index]) ? "true" : "false");
  3052. if (!str[++index].empty())
  3053. locations["Costume_Head_Low"] << (std::stoi(str[index]) ? "true" : "false");
  3054. if (!str[++index].empty())
  3055. locations["Costume_Garment"] << (std::stoi(str[index]) ? "true" : "false");
  3056. if (!str[++index].empty())
  3057. locations["Ammo"] << (std::stoi(str[index]) ? "true" : "false");
  3058. if (!str[++index].empty())
  3059. locations["Shadow_Armor"] << (std::stoi(str[index]) ? "true" : "false");
  3060. if (!str[++index].empty())
  3061. locations["Shadow_Weapon"] << (std::stoi(str[index]) ? "true" : "false");
  3062. if (!str[++index].empty())
  3063. locations["Shadow_Shield"] << (std::stoi(str[index]) ? "true" : "false");
  3064. if (!str[++index].empty())
  3065. locations["Shadow_Shoes"] << (std::stoi(str[index]) ? "true" : "false");
  3066. if (!str[++index].empty())
  3067. locations["Shadow_Right_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3068. if (!str[++index].empty())
  3069. locations["Shadow_Left_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3070. if (!str[++index].empty())
  3071. rootNode["WeaponLevel"] << str[index];
  3072. if (!str[++index].empty())
  3073. rootNode["ArmorLevel"] << str[index];
  3074. if (!str[++index].empty())
  3075. rootNode["EquipLevelMin"] << str[index];
  3076. if (!str[++index].empty())
  3077. rootNode["EquipLevelMax"] << str[index];
  3078. if (!str[++index].empty())
  3079. rootNode["Refineable"] << (std::stoi(str[index]) ? "true" : "false");
  3080. if (!str[++index].empty())
  3081. rootNode["View"] << str[index];
  3082. if (!str[++index].empty())
  3083. rootNode["AliasName"] << str[index];
  3084. ryml::NodeRef flags = rootNode["Flags"];
  3085. flags |= ryml::MAP;
  3086. if (!str[++index].empty())
  3087. flags["BuyingStore"] << (std::stoi(str[index]) ? "true" : "false");
  3088. if (!str[++index].empty())
  3089. flags["DeadBranch"] << (std::stoi(str[index]) ? "true" : "false");
  3090. if (!str[++index].empty())
  3091. flags["Container"] << (std::stoi(str[index]) ? "true" : "false");
  3092. if (!str[++index].empty())
  3093. flags["UniqueId"] << (std::stoi(str[index]) ? "true" : "false");
  3094. if (!str[++index].empty())
  3095. flags["BindOnEquip"] << (std::stoi(str[index]) ? "true" : "false");
  3096. if (!str[++index].empty())
  3097. flags["DropAnnounce"] << (std::stoi(str[index]) ? "true" : "false");
  3098. if (!str[++index].empty())
  3099. flags["NoConsume"] << (std::stoi(str[index]) ? "true" : "false");
  3100. if (!str[++index].empty())
  3101. flags["DropEffect"] << str[index];
  3102. rootNode["Flags"] = flags;
  3103. ryml::NodeRef delay = rootNode["Delay"];
  3104. delay |= ryml::MAP;
  3105. if (!str[++index].empty())
  3106. delay["Duration"] << str[index];
  3107. if (!str[++index].empty())
  3108. delay["Status"] << str[index];
  3109. ryml::NodeRef stack = rootNode["Stack"];
  3110. stack |= ryml::MAP;
  3111. if (!str[++index].empty())
  3112. stack["Amount"] << str[index];
  3113. if (!str[++index].empty())
  3114. stack["Inventory"] << (std::stoi(str[index]) ? "true" : "false");
  3115. if (!str[++index].empty())
  3116. stack["Cart"] << (std::stoi(str[index]) ? "true" : "false");
  3117. if (!str[++index].empty())
  3118. stack["Storage"] << (std::stoi(str[index]) ? "true" : "false");
  3119. if (!str[++index].empty())
  3120. stack["GuildStorage"] << (std::stoi(str[index]) ? "true" : "false");
  3121. ryml::NodeRef nouse = rootNode["NoUse"];
  3122. nouse |= ryml::MAP;
  3123. if (!str[++index].empty())
  3124. nouse["Override"] << str[index];
  3125. if (!str[++index].empty())
  3126. nouse["Sitting"] << (std::stoi(str[index]) ? "true" : "false");
  3127. ryml::NodeRef trade = rootNode["Trade"];
  3128. trade |= ryml::MAP;
  3129. if (!str[++index].empty())
  3130. trade["Override"] << str[index];
  3131. if (!str[++index].empty())
  3132. trade["NoDrop"] << (std::stoi(str[index]) ? "true" : "false");
  3133. if (!str[++index].empty())
  3134. trade["NoTrade"] << (std::stoi(str[index]) ? "true" : "false");
  3135. if (!str[++index].empty())
  3136. trade["TradePartner"] << (std::stoi(str[index]) ? "true" : "false");
  3137. if (!str[++index].empty())
  3138. trade["NoSell"] << (std::stoi(str[index]) ? "true" : "false");
  3139. if (!str[++index].empty())
  3140. trade["NoCart"] << (std::stoi(str[index]) ? "true" : "false");
  3141. if (!str[++index].empty())
  3142. trade["NoStorage"] << (std::stoi(str[index]) ? "true" : "false");
  3143. if (!str[++index].empty())
  3144. trade["NoGuildStorage"] << (std::stoi(str[index]) ? "true" : "false");
  3145. if (!str[++index].empty())
  3146. trade["NoMail"] << (std::stoi(str[index]) ? "true" : "false");
  3147. if (!str[++index].empty())
  3148. trade["NoAuction"] << (std::stoi(str[index]) ? "true" : "false");
  3149. if (!str[++index].empty())
  3150. rootNode["Script"] << str[index];
  3151. if (!str[++index].empty())
  3152. rootNode["EquipScript"] << str[index];
  3153. if (!str[++index].empty())
  3154. rootNode["UnEquipScript"] << str[index];
  3155. #ifdef RENEWAL
  3156. if (!str[++index].empty())
  3157. rootNode["MagicAttack"] << str[index];
  3158. if (!str[++index].empty())
  3159. classes["Third"] << (std::stoi(str[index]) ? "true" : "false");
  3160. if (!str[++index].empty())
  3161. classes["Third_Upper"] << (std::stoi(str[index]) ? "true" : "false");
  3162. if (!str[++index].empty())
  3163. classes["Third_Baby"] << (std::stoi(str[index]) ? "true" : "false");
  3164. if (!str[++index].empty())
  3165. classes["Fourth"] << (std::stoi(str[index]) ? "true" : "false");
  3166. if (!str[++index].empty())
  3167. jobs["KagerouOboro"] << (std::stoi(str[index]) ? "true" : "false");
  3168. if (!str[++index].empty())
  3169. jobs["Rebellion"] << (std::stoi(str[index]) ? "true" : "false");
  3170. if (!str[++index].empty())
  3171. jobs["Summoner"] << (std::stoi(str[index]) ? "true" : "false");
  3172. if (!str[++index].empty())
  3173. jobs["Spirit_Handler"] << (std::stoi(str[index]) ? "true" : "false");
  3174. if (!str[++index].empty())
  3175. rootNode["Gradable"] << (std::stoi(str[index]) ? "true" : "false");
  3176. #endif
  3177. if( !jobs.has_children() ){
  3178. rootNode.remove_child( jobs );
  3179. }
  3180. if( !classes.has_children() ){
  3181. rootNode.remove_child( classes );
  3182. }
  3183. if( !locations.has_children() ){
  3184. rootNode.remove_child( locations );
  3185. }
  3186. if( !flags.has_children() ){
  3187. rootNode.remove_child( flags );
  3188. }
  3189. if( !delay.has_children() ){
  3190. rootNode.remove_child( delay );
  3191. }
  3192. if( !stack.has_children() ){
  3193. rootNode.remove_child( stack );
  3194. }
  3195. if( !nouse.has_children() ){
  3196. rootNode.remove_child( nouse );
  3197. }
  3198. if( !trade.has_children() ){
  3199. rootNode.remove_child( trade );
  3200. }
  3201. return item_db.parseBodyNode(rootNode) > 0;
  3202. }
  3203. /**
  3204. * Read SQL item_db table
  3205. */
  3206. static int itemdb_read_sqldb(void) {
  3207. const char* item_db_name[] = {
  3208. item_table,
  3209. item2_table
  3210. };
  3211. for( uint8 fi = 0; fi < ARRAYLENGTH(item_db_name); ++fi ) {
  3212. // retrieve all rows from the item database
  3213. if( SQL_ERROR == Sql_Query(mmysql_handle, "SELECT `id`,`name_aegis`,`name_english`,`type`,`subtype`,`price_buy`,`price_sell`,`weight`,`attack`,`defense`,`range`,`slots`,"
  3214. "`job_all`,`job_acolyte`,`job_alchemist`,`job_archer`,`job_assassin`,`job_barddancer`,`job_blacksmith`,`job_crusader`,`job_gunslinger`,`job_hunter`,`job_knight`,`job_mage`,`job_merchant`,"
  3215. "`job_monk`,`job_ninja`,`job_novice`,`job_priest`,`job_rogue`,`job_sage`,`job_soullinker`,`job_stargladiator`,`job_supernovice`,`job_swordman`,`job_taekwon`,`job_thief`,`job_wizard`,"
  3216. "`class_all`,`class_normal`,`class_upper`,`class_baby`,`gender`,"
  3217. "`location_head_top`,`location_head_mid`,`location_head_low`,`location_armor`,`location_right_hand`,`location_left_hand`,`location_garment`,`location_shoes`,`location_right_accessory`,`location_left_accessory`,"
  3218. "`location_costume_head_top`,`location_costume_head_mid`,`location_costume_head_low`,`location_costume_garment`,`location_ammo`,`location_shadow_armor`,`location_shadow_weapon`,`location_shadow_shield`,`location_shadow_shoes`,`location_shadow_right_accessory`,`location_shadow_left_accessory`,"
  3219. "`weapon_level`,`armor_level`,`equip_level_min`,`equip_level_max`,`refineable`,`view`,`alias_name`,"
  3220. "`flag_buyingstore`,`flag_deadbranch`,`flag_container`,`flag_uniqueid`,`flag_bindonequip`,`flag_dropannounce`,`flag_noconsume`,`flag_dropeffect`,"
  3221. "`delay_duration`,`delay_status`,`stack_amount`,`stack_inventory`,`stack_cart`,`stack_storage`,`stack_guildstorage`,`nouse_override`,`nouse_sitting`,"
  3222. "`trade_override`,`trade_nodrop`,`trade_notrade`,`trade_tradepartner`,`trade_nosell`,`trade_nocart`,`trade_nostorage`,`trade_noguildstorage`,`trade_nomail`,`trade_noauction`,`script`,`equip_script`,`unequip_script`"
  3223. #ifdef RENEWAL
  3224. ",`magic_attack`,`class_third`,`class_third_upper`,`class_third_baby`,`class_fourth`,`job_kagerouoboro`,`job_rebellion`,`job_summoner`,`job_spirit_handler`,`gradable`"
  3225. #endif
  3226. " FROM `%s`", item_db_name[fi]) ) {
  3227. Sql_ShowDebug(mmysql_handle);
  3228. continue;
  3229. }
  3230. uint32 total_columns = Sql_NumColumns(mmysql_handle);
  3231. uint64 total_rows = Sql_NumRows(mmysql_handle), rows = 0, count = 0;
  3232. // process rows one by one
  3233. while( SQL_SUCCESS == Sql_NextRow(mmysql_handle) ) {
  3234. ShowStatus( "Loading [%" PRIu64 "/%" PRIu64 "] rows from '" CL_WHITE "%s" CL_RESET "'" CL_CLL "\r", ++rows, total_rows, item_db_name[fi] );
  3235. std::vector<std::string> data = {};
  3236. for( uint32 i = 0; i < total_columns; ++i ) {
  3237. char *str;
  3238. Sql_GetData(mmysql_handle, i, &str, nullptr);
  3239. if (str == nullptr)
  3240. data.push_back("");
  3241. else
  3242. data.push_back(str);
  3243. }
  3244. if (!itemdb_read_sqldb_sub(data))
  3245. continue;
  3246. ++count;
  3247. }
  3248. // free the query result
  3249. Sql_FreeResult(mmysql_handle);
  3250. ShowStatus("Done reading '" CL_WHITE "%" PRIu64 CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, item_db_name[fi]);
  3251. }
  3252. item_db.loadingFinished();
  3253. return 0;
  3254. }
  3255. /** Check if the item is restricted by item_noequip.txt
  3256. * @param id Item that will be checked
  3257. * @param m Map ID
  3258. * @return true: can't be used; false: can be used
  3259. */
  3260. bool itemdb_isNoEquip(struct item_data *id, uint16 m) {
  3261. if (!id->flag.no_equip)
  3262. return false;
  3263. struct map_data *mapdata = map_getmapdata(m);
  3264. if ((id->flag.no_equip&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  3265. (id->flag.no_equip&2 && mapdata->flag[MF_PVP]) || // PVP
  3266. (id->flag.no_equip&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  3267. (id->flag.no_equip&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  3268. (id->flag.no_equip&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  3269. (id->flag.no_equip&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  3270. )
  3271. return true;
  3272. return false;
  3273. }
  3274. const std::string RandomOptionDatabase::getDefaultLocation() {
  3275. return std::string(db_path) + "/item_randomopt_db.yml";
  3276. }
  3277. /**
  3278. * Reads and parses an entry from the item_randomopt_db.
  3279. * @param node: YAML node containing the entry.
  3280. * @return count of successfully parsed rows
  3281. */
  3282. uint64 RandomOptionDatabase::parseBodyNode(const ryml::NodeRef& node) {
  3283. uint16 id;
  3284. if (!this->asUInt16(node, "Id", id))
  3285. return 0;
  3286. std::shared_ptr<s_random_opt_data> randopt = this->find(id);
  3287. bool exists = randopt != nullptr;
  3288. if (!exists) {
  3289. if (!this->nodesExist(node, { "Option", "Script" }))
  3290. return 0;
  3291. randopt = std::make_shared<s_random_opt_data>();
  3292. randopt->id = id;
  3293. }
  3294. if (this->nodeExists(node, "Option")) {
  3295. std::string name;
  3296. if (!this->asString(node, "Option", name))
  3297. return 0;
  3298. if (randopt->name.compare(name) != 0 && random_option_db.option_exists(name)) {
  3299. this->invalidWarning(node["Option"], "Found duplicate random option name for %s, skipping.\n", name.c_str());
  3300. return 0;
  3301. }
  3302. randopt->name = name;
  3303. }
  3304. if (this->nodeExists(node, "Script")) {
  3305. std::string script;
  3306. if (!this->asString(node, "Script", script))
  3307. return 0;
  3308. if (randopt->script) {
  3309. aFree(randopt->script);
  3310. randopt->script = nullptr;
  3311. }
  3312. randopt->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Script"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  3313. }
  3314. if (!exists)
  3315. this->put(id, randopt);
  3316. return 1;
  3317. }
  3318. void RandomOptionDatabase::loadingFinished(){
  3319. const char* prefix = "RDMOPT_";
  3320. for( const auto& pair : *this ){
  3321. std::string name = prefix + pair.second->name;
  3322. int64 constant;
  3323. // Check if it has already been set
  3324. if( script_get_constant( name.c_str(), &constant ) ){
  3325. // It is already the same
  3326. if( constant == pair.first ){
  3327. continue;
  3328. }else{
  3329. // Export it to the script engine -> will issue a warning
  3330. }
  3331. }
  3332. script_set_constant( name.c_str(), pair.first, false, false );
  3333. }
  3334. TypesafeYamlDatabase::loadingFinished();
  3335. }
  3336. RandomOptionDatabase random_option_db;
  3337. /**
  3338. * Check if the given random option name exists.
  3339. * @param name: Random option name
  3340. * @return True on success or false on failure
  3341. */
  3342. bool RandomOptionDatabase::option_exists(std::string name) {
  3343. for (const auto &opt : random_option_db) {
  3344. if (opt.second->name.compare(name) == 0)
  3345. return true;
  3346. }
  3347. return false;
  3348. }
  3349. /**
  3350. * Return the constant value of the given random option.
  3351. * @param name: Random option name
  3352. * @param id: Random option ID
  3353. * @return True on success or false on failure
  3354. */
  3355. bool RandomOptionDatabase::option_get_id(std::string name, uint16 &id) {
  3356. for (const auto &opt : random_option_db) {
  3357. if (opt.second->name.compare(name) == 0) {
  3358. id = opt.first;
  3359. return true;
  3360. }
  3361. }
  3362. return false;
  3363. }
  3364. const std::string RandomOptionGroupDatabase::getDefaultLocation() {
  3365. return std::string(db_path) + "/item_randomopt_group.yml";
  3366. }
  3367. bool RandomOptionGroupDatabase::add_option(const ryml::NodeRef& node, std::shared_ptr<s_random_opt_group_entry> &entry) {
  3368. uint16 option_id;
  3369. if (this->nodeExists(node, "Option")) {
  3370. std::string opt_name;
  3371. if (!this->asString(node, "Option", opt_name))
  3372. return false;
  3373. if (!random_option_db.option_get_id(opt_name, option_id)) {
  3374. this->invalidWarning(node["Option"], "Invalid Random Option name %s given.\n", opt_name.c_str());
  3375. return false;
  3376. }
  3377. } else {
  3378. this->invalidWarning(node, "Missing Option node.\n");
  3379. return false;
  3380. }
  3381. entry = std::make_shared<s_random_opt_group_entry>();
  3382. entry->id = option_id;
  3383. if (this->nodeExists(node, "MinValue")) {
  3384. int16 value;
  3385. if (!this->asInt16(node, "MinValue", value))
  3386. return false;
  3387. entry->min_value = value;
  3388. } else {
  3389. entry->min_value = 0;
  3390. }
  3391. if (this->nodeExists(node, "MaxValue")) {
  3392. int16 value;
  3393. if (!this->asInt16(node, "MaxValue", value))
  3394. return false;
  3395. entry->max_value = value;
  3396. } else {
  3397. entry->max_value = 0;
  3398. }
  3399. if (entry->min_value > entry->max_value) {
  3400. this->invalidWarning(node["MaxValue"], "MinValue %d is greater than MaxValue %d, setting MaxValue to MinValue + 1.\n", entry->min_value, entry->max_value);
  3401. entry->max_value = entry->min_value + 1;
  3402. }
  3403. if (this->nodeExists(node, "Param")) {
  3404. int16 value;
  3405. if (!this->asInt16(node, "Param", value))
  3406. return false;
  3407. entry->param = static_cast<int8>(value);
  3408. } else {
  3409. entry->param = 0;
  3410. }
  3411. if (this->nodeExists(node, "Chance")) {
  3412. uint16 chance;
  3413. if (!this->asUInt16Rate(node, "Chance", chance))
  3414. return false;
  3415. entry->chance = chance;
  3416. } else {
  3417. entry->chance = 0;
  3418. }
  3419. return true;
  3420. }
  3421. void s_random_opt_group::apply( struct item& item ){
  3422. auto apply_sub = []( s_item_randomoption& item_option, const std::shared_ptr<s_random_opt_group_entry>& option ){
  3423. item_option.id = option->id;
  3424. item_option.value = rnd_value( option->min_value, option->max_value );
  3425. item_option.param = option->param;
  3426. };
  3427. // (Re)initialize all the options
  3428. for( size_t i = 0; i < MAX_ITEM_RDM_OPT; i++ ){
  3429. item.option[i].id = 0;
  3430. item.option[i].value = 0;
  3431. item.option[i].param = 0;
  3432. };
  3433. // Apply Must options
  3434. for( size_t i = 0; i < this->slots.size(); i++ ){
  3435. // Try to apply an entry
  3436. for( size_t j = 0, max = this->slots[static_cast<uint16>(i)].size() * 3; j < max; j++ ){
  3437. std::shared_ptr<s_random_opt_group_entry> option = util::vector_random( this->slots[static_cast<uint16>(i)] );
  3438. if( rnd() % 10000 < option->chance ){
  3439. apply_sub( item.option[i], option );
  3440. break;
  3441. }
  3442. }
  3443. // If no entry was applied, assign one
  3444. if( item.option[i].id == 0 ){
  3445. std::shared_ptr<s_random_opt_group_entry> option = util::vector_random( this->slots[static_cast<uint16>(i)] );
  3446. // Apply an entry without checking the chance
  3447. apply_sub( item.option[i], option );
  3448. }
  3449. }
  3450. // Apply Random options (if available)
  3451. if( this->max_random > 0 ){
  3452. for( size_t i = 0; i < min( this->max_random, MAX_ITEM_RDM_OPT ); i++ ){
  3453. // If item already has an option in this slot, skip it
  3454. if( item.option[i].id > 0 ){
  3455. continue;
  3456. }
  3457. std::shared_ptr<s_random_opt_group_entry> option = util::vector_random( this->random_options );
  3458. if( rnd() % 10000 < option->chance ){
  3459. apply_sub( item.option[i], option );
  3460. }
  3461. }
  3462. }
  3463. // Fix any gaps, the client cannot handle this
  3464. for( size_t i = 0; i < MAX_ITEM_RDM_OPT; i++ ){
  3465. // If an option is empty
  3466. if( item.option[i].id == 0 ){
  3467. // Check if any other options, after the empty option exist
  3468. size_t j;
  3469. for( j = i + 1; j < MAX_ITEM_RDM_OPT; j++ ){
  3470. if( item.option[j].id != 0 ){
  3471. break;
  3472. }
  3473. }
  3474. // Another option was found, after the empty option
  3475. if( j < MAX_ITEM_RDM_OPT ){
  3476. // Move the later option forward
  3477. item.option[i].id = item.option[j].id;
  3478. item.option[i].value = item.option[j].value;
  3479. item.option[i].param = item.option[j].param;
  3480. // Reset the option that was moved
  3481. item.option[j].id = 0;
  3482. item.option[j].value = 0;
  3483. item.option[j].param = 0;
  3484. }else{
  3485. // Cancel early
  3486. break;
  3487. }
  3488. }
  3489. }
  3490. }
  3491. /**
  3492. * Reads and parses an entry from the item_randomopt_group.
  3493. * @param node: YAML node containing the entry.
  3494. * @return count of successfully parsed rows
  3495. */
  3496. uint64 RandomOptionGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  3497. uint16 id;
  3498. if (!this->asUInt16(node, "Id", id))
  3499. return 0;
  3500. std::shared_ptr<s_random_opt_group> randopt = this->find(id);
  3501. bool exists = randopt != nullptr;
  3502. if (!exists) {
  3503. if (!this->nodesExist(node, { "Group" }))
  3504. return 0;
  3505. randopt = std::make_shared<s_random_opt_group>();
  3506. randopt->id = id;
  3507. }
  3508. if (this->nodeExists(node, "Group")) {
  3509. std::string name;
  3510. if (!this->asString(node, "Group", name))
  3511. return 0;
  3512. if (randopt->name.compare(name) != 0 && random_option_group.option_exists(name)) {
  3513. this->invalidWarning(node["Group"], "Found duplicate random option group name for %s, skipping.\n", name.c_str());
  3514. return 0;
  3515. }
  3516. randopt->name = name;
  3517. }
  3518. if (this->nodeExists(node, "Slots")) {
  3519. const auto& slotsNode = node["Slots"];
  3520. for (const ryml::NodeRef& slotNode : slotsNode) {
  3521. if (randopt->slots.size() >= MAX_ITEM_RDM_OPT) {
  3522. this->invalidWarning(slotNode, "Reached maximum of %d Random Option group options. Skipping the remaining slots...\n", MAX_ITEM_RDM_OPT);
  3523. break;
  3524. }
  3525. uint16 slot;
  3526. if (!this->asUInt16(slotNode, "Slot", slot))
  3527. return 0;
  3528. if (slot < 1 || slot > MAX_ITEM_RDM_OPT) {
  3529. this->invalidWarning(slotNode["Slot"], "Invalid Random Opton Slot number %hu given, must be between 1~%d, skipping.\n", slot, MAX_ITEM_RDM_OPT);
  3530. return 0;
  3531. }
  3532. if (!this->nodeExists(slotNode, "Options")) {
  3533. this->invalidWarning(slotNode, "Random option slot does not contain Options node, skipping.\n");
  3534. return 0;
  3535. }
  3536. std::vector<std::shared_ptr<s_random_opt_group_entry>> entries;
  3537. const auto& optionsNode = slotNode["Options"];
  3538. for (const auto& optionNode : optionsNode) {
  3539. std::shared_ptr<s_random_opt_group_entry> entry;
  3540. if (!this->add_option(optionNode, entry))
  3541. return 0;
  3542. entries.push_back(entry);
  3543. }
  3544. randopt->slots[slot - 1] = entries;
  3545. }
  3546. }
  3547. if (this->nodeExists(node, "MaxRandom")) {
  3548. uint16 max;
  3549. if (!this->asUInt16(node, "MaxRandom", max))
  3550. return 0;
  3551. if (max > MAX_ITEM_RDM_OPT) {
  3552. this->invalidWarning(node["MaxRandom"], "Exceeds the maximum of %d Random Option group options, capping to MAX_ITEM_RDM_OPT.\n", MAX_ITEM_RDM_OPT);
  3553. max = MAX_ITEM_RDM_OPT;
  3554. }
  3555. randopt->max_random = max;
  3556. } else {
  3557. if (!exists)
  3558. randopt->max_random = 0;
  3559. }
  3560. if (this->nodeExists(node, "Random")) {
  3561. randopt->random_options.clear();
  3562. const auto& randomNode = node["Random"];
  3563. for (const auto& randomNode : randomNode) {
  3564. std::shared_ptr<s_random_opt_group_entry> entry;
  3565. if (!this->add_option(randomNode, entry))
  3566. return 0;
  3567. randopt->random_options.push_back(entry);
  3568. }
  3569. }
  3570. if (!exists)
  3571. this->put(id, randopt);
  3572. return 1;
  3573. }
  3574. RandomOptionGroupDatabase random_option_group;
  3575. /**
  3576. * Check if the given random option group name exists.
  3577. * @param name: Random option name
  3578. * @return True on success or false on failure
  3579. */
  3580. bool RandomOptionGroupDatabase::option_exists(std::string name) {
  3581. for (const auto &opt : random_option_group) {
  3582. if (opt.second->name.compare(name) == 0)
  3583. return true;
  3584. }
  3585. return false;
  3586. }
  3587. /**
  3588. * Return the constant value of the given random option group.
  3589. * @param name: Random option group name
  3590. * @param id: Random option group ID
  3591. * @return True on success or false on failure
  3592. */
  3593. bool RandomOptionGroupDatabase::option_get_id(std::string name, uint16 &id) {
  3594. for (const auto &opt : random_option_group) {
  3595. if (opt.second->name.compare(name) == 0) {
  3596. id = opt.first;
  3597. return true;
  3598. }
  3599. }
  3600. return false;
  3601. }
  3602. /**
  3603. * Read all item-related databases
  3604. */
  3605. static void itemdb_read(void) {
  3606. int i;
  3607. const char* dbsubpath[] = {
  3608. "",
  3609. "/" DBIMPORT,
  3610. };
  3611. if (db_use_sqldbs)
  3612. itemdb_read_sqldb();
  3613. else
  3614. item_db.load();
  3615. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  3616. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  3617. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  3618. char* dbsubpath1 = (char*)aMalloc(n1+1);
  3619. char* dbsubpath2 = (char*)aMalloc(n2+1);
  3620. if(i==0) {
  3621. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  3622. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  3623. }
  3624. else {
  3625. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  3626. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  3627. }
  3628. sv_readdb(dbsubpath2, "item_noequip.txt", ',', 2, 2, -1, &itemdb_read_noequip, i > 0);
  3629. aFree(dbsubpath1);
  3630. aFree(dbsubpath2);
  3631. }
  3632. random_option_db.load();
  3633. random_option_group.load();
  3634. itemdb_group.load();
  3635. itemdb_combo.load();
  3636. laphine_synthesis_db.load();
  3637. laphine_upgrade_db.load();
  3638. item_reform_db.load();
  3639. item_enchant_db.load();
  3640. item_package_db.load();
  3641. if (battle_config.feature_roulette)
  3642. itemdb_parse_roulette_db();
  3643. }
  3644. /*==========================================
  3645. * Initialize / Finalize
  3646. *------------------------------------------*/
  3647. bool item_data::isStackable()
  3648. {
  3649. switch (this->type) {
  3650. case IT_WEAPON:
  3651. case IT_ARMOR:
  3652. case IT_PETEGG:
  3653. case IT_PETARMOR:
  3654. case IT_SHADOWGEAR:
  3655. return false;
  3656. }
  3657. return true;
  3658. }
  3659. int item_data::inventorySlotNeeded(int quantity)
  3660. {
  3661. return (this->flag.guid || !this->isStackable()) ? quantity : 1;
  3662. }
  3663. void itemdb_gen_itemmoveinfo()
  3664. {
  3665. ShowInfo("itemdb_gen_itemmoveinfo: Generating itemmoveinfov5.txt.\n");
  3666. auto starttime = std::chrono::system_clock::now();
  3667. auto os = std::ofstream("./generated/clientside/data/itemmoveinfov5.txt", std::ios::trunc);
  3668. std::map<t_itemid, std::shared_ptr<item_data>> sorted_itemdb(item_db.begin(), item_db.end());
  3669. os << "// ItemID,\tDrop,\tVending,\tStorage,\tCart,\tNpc Sale,\tMail,\tAuction,\tGuildStorage\n";
  3670. os << "// This format does not accept blank lines. Be careful.\n";
  3671. item_data tmp_id{};
  3672. for (auto it = sorted_itemdb.begin(); it != sorted_itemdb.end(); ++it) {
  3673. if (it->second->type == IT_UNKNOWN)
  3674. continue;
  3675. if (memcmp(&it->second->flag.trade_restriction, &tmp_id.flag.trade_restriction, sizeof(tmp_id.flag.trade_restriction)) == 0)
  3676. continue;
  3677. os << it->first << "\t" << it->second->flag.trade_restriction.drop << "\t" << it->second->flag.trade_restriction.trade << "\t" << it->second->flag.trade_restriction.storage << "\t" << it->second->flag.trade_restriction.cart << "\t" << it->second->flag.trade_restriction.sell << "\t" << it->second->flag.trade_restriction.mail << "\t" << it->second->flag.trade_restriction.auction << "\t" << it->second->flag.trade_restriction.guild_storage << "\t// " << it->second->name << "\n";
  3678. }
  3679. os.close();
  3680. auto currenttime = std::chrono::system_clock::now();
  3681. ShowInfo("itemdb_gen_itemmoveinfo: Done generating itemmoveinfov5.txt. The process took %lldms\n", std::chrono::duration_cast<std::chrono::milliseconds>(currenttime - starttime).count());
  3682. }
  3683. /**
  3684. * Reload Item DB
  3685. */
  3686. void itemdb_reload(void) {
  3687. struct s_mapiterator* iter;
  3688. map_session_data* sd;
  3689. do_final_itemdb();
  3690. // read new data
  3691. itemdb_read();
  3692. cashshop_reloaddb();
  3693. mob_reload_itemmob_data();
  3694. // readjust itemdb pointer cache for each player
  3695. iter = mapit_geteachpc();
  3696. for( sd = (map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (map_session_data*)mapit_next(iter) ) {
  3697. memset(sd->item_delay, 0, sizeof(sd->item_delay)); // reset item delays
  3698. sd->combos.clear(); // clear combo bonuses
  3699. pc_setinventorydata(sd);
  3700. pc_check_available_item(sd, ITMCHK_ALL); // Check for invalid(ated) items.
  3701. pc_load_combo(sd); // Check to see if new combos are available
  3702. status_calc_pc(sd, SCO_FORCE); //
  3703. }
  3704. mapit_free(iter);
  3705. }
  3706. /**
  3707. * Finalizing Item DB
  3708. */
  3709. void do_final_itemdb(void) {
  3710. item_db.clear();
  3711. itemdb_combo.clear();
  3712. itemdb_group.clear();
  3713. random_option_db.clear();
  3714. random_option_group.clear();
  3715. laphine_synthesis_db.clear();
  3716. laphine_upgrade_db.clear();
  3717. item_reform_db.clear();
  3718. item_enchant_db.clear();
  3719. item_package_db.clear();
  3720. if (battle_config.feature_roulette)
  3721. itemdb_roulette_free();
  3722. }
  3723. /**
  3724. * Initializing Item DB
  3725. */
  3726. void do_init_itemdb(void) {
  3727. itemdb_read();
  3728. }