itemdb.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995
  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 <map>
  5. #include <stdlib.h>
  6. #include "../common/nullpo.hpp"
  7. #include "../common/random.hpp"
  8. #include "../common/showmsg.hpp"
  9. #include "../common/strlib.hpp"
  10. #include "../common/utils.hpp"
  11. #include "../common/utilities.hpp"
  12. #include "battle.hpp" // struct battle_config
  13. #include "cashshop.hpp"
  14. #include "clif.hpp"
  15. #include "intif.hpp"
  16. #include "log.hpp"
  17. #include "mob.hpp"
  18. #include "pc.hpp"
  19. #include "status.hpp"
  20. using namespace rathena;
  21. ComboDatabase itemdb_combo;
  22. ItemGroupDatabase itemdb_group;
  23. struct s_roulette_db rd;
  24. static void itemdb_jobid2mapid(uint64 bclass[3], e_mapid jobmask, bool active);
  25. const std::string ItemDatabase::getDefaultLocation() {
  26. return std::string(db_path) + "/item_db.yml";
  27. }
  28. /**
  29. * Reads and parses an entry from the item_db.
  30. * @param node: YAML node containing the entry.
  31. * @return count of successfully parsed rows
  32. */
  33. uint64 ItemDatabase::parseBodyNode(const YAML::Node &node) {
  34. t_itemid nameid;
  35. if (!this->asUInt32(node, "Id", nameid))
  36. return 0;
  37. std::shared_ptr<item_data> item = this->find(nameid);
  38. bool exists = item != nullptr;
  39. if (!exists) {
  40. if (!this->nodesExist(node, { "AegisName", "Name" }))
  41. return 0;
  42. item = std::make_shared<item_data>();
  43. item->nameid = nameid;
  44. item->flag.available = true;
  45. }
  46. if (this->nodeExists(node, "AegisName")) {
  47. std::string name;
  48. if (!this->asString(node, "AegisName", name))
  49. return 0;
  50. if (name.length() > ITEM_NAME_LENGTH) {
  51. this->invalidWarning(node["AegisName"], "AegisName \"%s\" exceeds maximum of %d characters, capping...\n", name.c_str(), ITEM_NAME_LENGTH - 1);
  52. }
  53. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  54. if (id != nullptr && id->nameid != nameid) {
  55. this->invalidWarning(node["AegisName"], "Found duplicate item Aegis name for %s, skipping.\n", name.c_str());
  56. return 0;
  57. }
  58. if( exists ){
  59. // Create a copy
  60. std::string aegisname = item->name;
  61. // Convert it to lower
  62. util::tolower( aegisname );
  63. // Remove old AEGIS name from lookup
  64. this->aegisNameToItemDataMap.erase( aegisname );
  65. }
  66. item->name.resize(ITEM_NAME_LENGTH);
  67. item->name = name.c_str();
  68. // Create a copy
  69. std::string aegisname = name;
  70. // Convert it to lower
  71. util::tolower( aegisname );
  72. this->aegisNameToItemDataMap[aegisname] = item;
  73. }
  74. if (this->nodeExists(node, "Name")) {
  75. std::string name;
  76. if (!this->asString(node, "Name", name))
  77. return 0;
  78. if (name.length() > ITEM_NAME_LENGTH) {
  79. this->invalidWarning(node["Name"], "Name \"%s\" exceeds maximum of %d characters, capping...\n", name.c_str(), ITEM_NAME_LENGTH - 1);
  80. }
  81. if( exists ){
  82. // Create a copy
  83. std::string ename = item->ename;
  84. // Convert it to lower
  85. util::tolower( ename );
  86. // Remove old name from lookup
  87. this->nameToItemDataMap.erase( ename );
  88. }
  89. item->ename.resize(ITEM_NAME_LENGTH);
  90. item->ename = name.c_str();
  91. // Create a copy
  92. std::string ename = name;
  93. // Convert it to lower
  94. util::tolower( ename );
  95. this->nameToItemDataMap[ename] = item;
  96. }
  97. if (this->nodeExists(node, "Type")) {
  98. std::string type;
  99. if (!this->asString(node, "Type", type))
  100. return 0;
  101. std::string type_constant = "IT_" + type;
  102. int64 constant;
  103. if (!script_get_constant(type_constant.c_str(), &constant) || constant < IT_HEALING || constant >= IT_MAX) {
  104. this->invalidWarning(node["Type"], "Invalid item type %s, defaulting to IT_ETC.\n", type.c_str());
  105. constant = IT_ETC;
  106. }
  107. item->type = static_cast<item_types>(constant);
  108. } else {
  109. if (!exists)
  110. item->type = IT_ETC;
  111. }
  112. if (this->nodeExists(node, "SubType")) {
  113. std::string type;
  114. if (!this->asString(node, "SubType", type))
  115. return 0;
  116. if (item->type == IT_WEAPON) {
  117. std::string type_constant = "W_" + type;
  118. int64 constant;
  119. if (!script_get_constant(type_constant.c_str(), &constant) || constant < W_FIST || constant >= MAX_WEAPON_TYPE) {
  120. this->invalidWarning(node["SubType"], "Invalid weapon type %s, defaulting to W_FIST.\n", type.c_str());
  121. item->subtype = W_FIST;
  122. }
  123. item->subtype = static_cast<weapon_type>(constant);
  124. } else if (item->type == IT_AMMO) {
  125. std::string type_constant = "AMMO_" + type;
  126. int64 constant;
  127. if (!script_get_constant(type_constant.c_str(), &constant) || constant <= AMMO_NONE || constant >= MAX_AMMO_TYPE) {
  128. this->invalidWarning(node["SubType"], "Invalid ammo type %s, defaulting to AMMO_NONE.\n", type.c_str());
  129. item->subtype = AMMO_NONE;
  130. }
  131. item->subtype = static_cast<e_ammo_type>(constant);
  132. } else if (item->type == IT_CARD) {
  133. std::string type_constant = "CARD_" + type;
  134. int64 constant;
  135. if (!script_get_constant(type_constant.c_str(), &constant) || constant < CARD_NORMAL || constant >= MAX_CARD_TYPE) {
  136. this->invalidWarning(node["SubType"], "Invalid card type %s, defaulting to CARD_NORMAL.\n", type.c_str());
  137. item->subtype = CARD_NORMAL;
  138. }
  139. item->subtype = static_cast<e_card_type>(constant);
  140. } else
  141. this->invalidWarning(node["SubType"], "Item sub type is not supported for this item type.\n");
  142. } else {
  143. if (!exists)
  144. item->subtype = 0;
  145. }
  146. if (this->nodeExists(node, "Buy")) {
  147. uint32 buy;
  148. if (!this->asUInt32(node, "Buy", buy))
  149. return 0;
  150. item->value_buy = buy;
  151. item->value_sell = 0;
  152. } else {
  153. if (!exists) {
  154. item->value_buy = 0;
  155. }
  156. }
  157. if (this->nodeExists(node, "Sell")) {
  158. uint32 sell;
  159. if (!this->asUInt32(node, "Sell", sell))
  160. return 0;
  161. item->value_sell = sell;
  162. item->value_buy = 0;
  163. } else {
  164. if (!exists) {
  165. item->value_sell = 0;
  166. }
  167. }
  168. if (this->nodeExists(node, "Weight")) {
  169. uint32 weight;
  170. if (!this->asUInt32(node, "Weight", weight))
  171. return 0;
  172. item->weight = weight;
  173. } else {
  174. if (!exists)
  175. item->weight = 0;
  176. }
  177. if (this->nodeExists(node, "Attack")) {
  178. uint32 atk;
  179. if (!this->asUInt32(node, "Attack", atk))
  180. return 0;
  181. item->atk = atk;
  182. } else {
  183. if (!exists)
  184. item->atk = 0;
  185. }
  186. #ifdef RENEWAL
  187. if (this->nodeExists(node, "MagicAttack")) {
  188. uint32 matk;
  189. if (!this->asUInt32(node, "MagicAttack", matk))
  190. return 0;
  191. item->matk = matk;
  192. } else {
  193. if (!exists)
  194. item->matk = 0;
  195. }
  196. #endif
  197. if (this->nodeExists(node, "Defense")) {
  198. uint32 def;
  199. if (!this->asUInt32(node, "Defense", def))
  200. return 0;
  201. if (def > DEFTYPE_MAX) {
  202. this->invalidWarning(node["Defense"], "Item defense %d exceeds DEFTYPE_MAX (%d), capping to DEFTYPE_MAX.\n", def, DEFTYPE_MAX);
  203. def = DEFTYPE_MAX;
  204. }
  205. item->def = def;
  206. } else {
  207. if (!exists)
  208. item->def = 0;
  209. }
  210. if (this->nodeExists(node, "Range")) {
  211. uint16 range;
  212. if (!this->asUInt16(node, "Range", range))
  213. return 0;
  214. if (range > AREA_SIZE) {
  215. this->invalidWarning(node["Range"], "Item attack range %d exceeds AREA_SIZE (%d), capping to AREA_SIZE.\n", range, AREA_SIZE);
  216. range = AREA_SIZE;
  217. }
  218. item->range = range;
  219. } else {
  220. if (!exists)
  221. item->range = 0;
  222. }
  223. if (this->nodeExists(node, "Slots")) {
  224. uint16 slots;
  225. if (!this->asUInt16(node, "Slots", slots))
  226. return 0;
  227. if (slots > MAX_SLOTS) {
  228. this->invalidWarning(node["Slots"], "Item slots %d exceeds MAX_SLOTS (%d), capping to MAX_SLOTS.\n", slots, MAX_SLOTS);
  229. slots = MAX_SLOTS;
  230. }
  231. item->slots = slots;
  232. } else {
  233. if (!exists)
  234. item->slots = 0;
  235. }
  236. if (this->nodeExists(node, "Jobs")) {
  237. const YAML::Node &jobNode = node["Jobs"];
  238. item->class_base[0] = item->class_base[1] = item->class_base[2] = 0;
  239. if (this->nodeExists(jobNode, "All")) {
  240. bool active;
  241. if (!this->asBool(jobNode, "All", active))
  242. return 0;
  243. itemdb_jobid2mapid(item->class_base, MAPID_ALL, active);
  244. }
  245. for (const auto &jobit : jobNode) {
  246. std::string jobName = jobit.first.as<std::string>();
  247. // Skipped because processed above the loop
  248. if (jobName.compare("All") == 0)
  249. continue;
  250. std::string jobName_constant = "EAJ_" + jobName;
  251. int64 constant;
  252. if (!script_get_constant(jobName_constant.c_str(), &constant)) {
  253. this->invalidWarning(jobNode[jobName], "Invalid item job %s, defaulting to All.\n", jobName.c_str());
  254. itemdb_jobid2mapid(item->class_base, MAPID_ALL, true);
  255. break;
  256. }
  257. bool active;
  258. if (!this->asBool(jobNode, jobName, active))
  259. return 0;
  260. itemdb_jobid2mapid(item->class_base, static_cast<e_mapid>(constant), active);
  261. }
  262. } else {
  263. if (!exists) {
  264. item->class_base[0] = item->class_base[1] = item->class_base[2] = 0;
  265. itemdb_jobid2mapid(item->class_base, MAPID_ALL, true);
  266. }
  267. }
  268. if (this->nodeExists(node, "Classes")) {
  269. const YAML::Node &classNode = node["Classes"];
  270. if (this->nodeExists(classNode, "All")) {
  271. bool active;
  272. if (!this->asBool(classNode, "All", active))
  273. return 0;
  274. if (active)
  275. item->class_upper |= ITEMJ_ALL;
  276. else
  277. item->class_upper &= ~ITEMJ_ALL;
  278. }
  279. for (const auto &classit : classNode) {
  280. std::string className = classit.first.as<std::string>();
  281. // Skipped because processed above the loop
  282. if (className.compare("All") == 0)
  283. continue;
  284. std::string className_constant = "ITEMJ_" + className;
  285. int64 constant;
  286. if (!script_get_constant(className_constant.c_str(), &constant)) {
  287. this->invalidWarning(classNode[className], "Invalid class %s, defaulting to All.\n", className.c_str());
  288. item->class_upper |= ITEMJ_ALL;
  289. break;
  290. }
  291. bool active;
  292. if (!this->asBool(classNode, className, active))
  293. return 0;
  294. if (active)
  295. item->class_upper |= constant;
  296. else
  297. item->class_upper &= ~constant;
  298. }
  299. } else {
  300. if (!exists)
  301. item->class_upper = ITEMJ_ALL;
  302. }
  303. if (this->nodeExists(node, "Gender")) {
  304. std::string gender;
  305. if (!this->asString(node, "Gender", gender))
  306. return 0;
  307. std::string gender_constant = "SEX_" + gender;
  308. int64 constant;
  309. if (!script_get_constant(gender_constant.c_str(), &constant) || constant < SEX_FEMALE || constant > SEX_BOTH) {
  310. this->invalidWarning(node["Gender"], "Invalid item gender %s, defaulting to SEX_BOTH.\n", gender.c_str());
  311. constant = SEX_BOTH;
  312. }
  313. item->sex = static_cast<e_sex>(constant);
  314. item->sex = this->defaultGender( node, item );
  315. } else {
  316. if (!exists) {
  317. item->sex = SEX_BOTH;
  318. item->sex = this->defaultGender( node, item );
  319. }
  320. }
  321. if (this->nodeExists(node, "Locations")) {
  322. const YAML::Node &locationNode = node["Locations"];
  323. for (const auto &locit : locationNode) {
  324. std::string equipName = locit.first.as<std::string>(), equipName_constant = "EQP_" + equipName;
  325. int64 constant;
  326. if (!script_get_constant(equipName_constant.c_str(), &constant)) {
  327. this->invalidWarning(locationNode[equipName], "Invalid equip location %s, defaulting to IT_ETC.\n", equipName.c_str());
  328. item->type = IT_ETC;
  329. break;
  330. }
  331. bool active;
  332. if (!this->asBool(locationNode, equipName, active))
  333. return 0;
  334. if (active) {
  335. if (constant & EQP_SHADOW_GEAR && item->type != IT_SHADOWGEAR) {
  336. 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());
  337. item->type = IT_ETC;
  338. }
  339. item->equip |= constant;
  340. } else
  341. item->equip &= ~constant;
  342. }
  343. } else {
  344. if (!exists) {
  345. if (itemdb_isequip2(item.get())) {
  346. this->invalidWarning(node, "Invalid item equip location as it has no equip location, defaulting to IT_ETC.\n");
  347. item->type = IT_ETC;
  348. } else
  349. item->equip = 0;
  350. }
  351. }
  352. if (this->nodeExists(node, "WeaponLevel")) {
  353. uint16 lv;
  354. if (!this->asUInt16(node, "WeaponLevel", lv))
  355. return 0;
  356. if (lv > MAX_WEAPON_LEVEL) {
  357. this->invalidWarning(node["WeaponLevel"], "Invalid weapon level %d, defaulting to 0.\n", lv);
  358. lv = 0;
  359. }
  360. if (item->type != IT_WEAPON) {
  361. this->invalidWarning(node["WeaponLevel"], "Item type is not a weapon, defaulting to 0.\n");
  362. lv = 0;
  363. }
  364. item->weapon_level = lv;
  365. } else {
  366. if (!exists)
  367. item->weapon_level = 0;
  368. }
  369. if( this->nodeExists( node, "ArmorLevel" ) ){
  370. uint16 level;
  371. if( !this->asUInt16( node, "ArmorLevel", level ) ){
  372. return 0;
  373. }
  374. if( level > MAX_ARMOR_LEVEL ){
  375. this->invalidWarning( node["ArmorLevel"], "Invalid armor level %d, defaulting to 0.\n", level );
  376. level = 0;
  377. }
  378. if( item->type != IT_ARMOR ){
  379. this->invalidWarning( node["ArmorLevel"], "Item type is not an armor, defaulting to 0.\n" );
  380. level = 0;
  381. }
  382. item->armor_level = level;
  383. }else{
  384. if( !exists ){
  385. item->armor_level = 0;
  386. }
  387. }
  388. if (this->nodeExists(node, "EquipLevelMin")) {
  389. uint16 lv;
  390. if (!this->asUInt16(node, "EquipLevelMin", lv))
  391. return 0;
  392. if (lv > MAX_LEVEL) {
  393. this->invalidWarning(node["EquipLevelMin"], "Minimum equip level %d exceeds MAX_LEVEL (%d), capping to MAX_LEVEL.\n", lv, MAX_LEVEL);
  394. lv = MAX_LEVEL;
  395. }
  396. item->elv = lv;
  397. } else {
  398. if (!exists)
  399. item->elv = 0;
  400. }
  401. if (this->nodeExists(node, "EquipLevelMax")) {
  402. uint16 lv;
  403. if (!this->asUInt16(node, "EquipLevelMax", lv))
  404. return 0;
  405. if (lv < item->elv) {
  406. this->invalidWarning(node["EquipLevelMax"], "Maximum equip level %d is less than minimum equip level %d, capping to minimum equip level.\n", lv, item->elv);
  407. lv = item->elv;
  408. }
  409. if (lv > MAX_LEVEL) {
  410. this->invalidWarning(node["EquipLevelMax"], "Maximum equip level %d exceeds MAX_LEVEL (%d), capping to MAX_LEVEL.\n", lv, MAX_LEVEL);
  411. lv = MAX_LEVEL;
  412. }
  413. item->elvmax = lv;
  414. } else {
  415. if (!exists)
  416. item->elvmax = MAX_LEVEL;
  417. }
  418. if (this->nodeExists(node, "Refineable")) {
  419. bool refine;
  420. if (!this->asBool(node, "Refineable", refine))
  421. return 0;
  422. item->flag.no_refine = !refine;
  423. } else {
  424. if (!exists)
  425. item->flag.no_refine = true;
  426. }
  427. if (this->nodeExists(node, "View")) {
  428. uint32 look;
  429. if (!this->asUInt32(node, "View", look))
  430. return 0;
  431. item->look = look;
  432. } else {
  433. if (!exists)
  434. item->look = 0;
  435. }
  436. if (this->nodeExists(node, "AliasName")) {
  437. std::string view;
  438. if (!this->asString(node, "AliasName", view))
  439. return 0;
  440. std::shared_ptr<item_data> view_data = item_db.search_aegisname( view.c_str() );
  441. if (view_data == nullptr) {
  442. this->invalidWarning(node["AliasName"], "Unable to change the alias because %s is an unknown item.\n", view.c_str());
  443. return 0;
  444. }
  445. item->view_id = view_data->nameid;
  446. } else {
  447. if (!exists)
  448. item->view_id = 0;
  449. }
  450. if (this->nodeExists(node, "Flags")) {
  451. const YAML::Node &flagNode = node["Flags"];
  452. if (this->nodeExists(flagNode, "BuyingStore")) {
  453. bool active;
  454. if (!this->asBool(flagNode, "BuyingStore", active))
  455. return 0;
  456. if (!itemdb_isstackable2(item.get()) && active) {
  457. this->invalidWarning(flagNode["BuyingStore"], "Non-stackable item cannot be enabled for buying store.\n");
  458. active = false;
  459. }
  460. item->flag.buyingstore = active;
  461. } else {
  462. if (!exists)
  463. item->flag.buyingstore = false;
  464. }
  465. if (this->nodeExists(flagNode, "DeadBranch")) {
  466. bool active;
  467. if (!this->asBool(flagNode, "DeadBranch", active))
  468. return 0;
  469. item->flag.dead_branch = active;
  470. } else {
  471. if (!exists)
  472. item->flag.dead_branch = false;
  473. }
  474. if (this->nodeExists(flagNode, "Container")) {
  475. bool active;
  476. if (!this->asBool(flagNode, "Container", active))
  477. return 0;
  478. item->flag.group = active;
  479. } else {
  480. if (!exists)
  481. item->flag.group = false;
  482. }
  483. if (this->nodeExists(flagNode, "UniqueId")) {
  484. bool active;
  485. if (!this->asBool(flagNode, "UniqueId", active))
  486. return 0;
  487. if (!itemdb_isstackable2(item.get()) && active) {
  488. this->invalidWarning(flagNode["UniqueId"], "Non-stackable item cannot be enabled for UniqueId.\n");
  489. active = false;
  490. }
  491. item->flag.guid = active;
  492. } else {
  493. if (!exists)
  494. item->flag.guid = false;
  495. }
  496. if (this->nodeExists(flagNode, "BindOnEquip")) {
  497. bool active;
  498. if (!this->asBool(flagNode, "BindOnEquip", active))
  499. return 0;
  500. item->flag.bindOnEquip = active;
  501. } else {
  502. if (!exists)
  503. item->flag.bindOnEquip = false;
  504. }
  505. if (this->nodeExists(flagNode, "DropAnnounce")) {
  506. bool active;
  507. if (!this->asBool(flagNode, "DropAnnounce", active))
  508. return 0;
  509. item->flag.broadcast = active;
  510. } else {
  511. if (!exists)
  512. item->flag.broadcast = false;
  513. }
  514. if (this->nodeExists(flagNode, "NoConsume")) {
  515. bool active;
  516. if (!this->asBool(flagNode, "NoConsume", active))
  517. return 0;
  518. if (active)
  519. item->flag.delay_consume |= DELAYCONSUME_NOCONSUME;
  520. else
  521. item->flag.delay_consume &= ~DELAYCONSUME_NOCONSUME;
  522. } else {
  523. if (!exists) {
  524. if (!(item->flag.delay_consume & DELAYCONSUME_TEMP))
  525. item->flag.delay_consume = DELAYCONSUME_NONE;
  526. }
  527. }
  528. if (this->nodeExists(flagNode, "DropEffect")) {
  529. std::string effect;
  530. if (!this->asString(flagNode, "DropEffect", effect))
  531. return 0;
  532. std::string effect_constant = "DROPEFFECT_" + effect;
  533. int64 constant;
  534. if (!script_get_constant(effect_constant.c_str(), &constant) || constant < DROPEFFECT_NONE || constant > DROPEFFECT_MAX) {
  535. this->invalidWarning(flagNode["DropEffect"], "Invalid item drop effect %s, defaulting to DROPEFFECT_NONE.\n", effect.c_str());
  536. constant = DROPEFFECT_NONE;
  537. }
  538. item->flag.dropEffect = static_cast<e_item_drop_effect>(constant);
  539. } else {
  540. if (!exists)
  541. item->flag.dropEffect = DROPEFFECT_NONE;
  542. }
  543. } else {
  544. if (!exists) {
  545. item->flag.buyingstore = false;
  546. item->flag.dead_branch = false;
  547. item->flag.group = false;
  548. item->flag.guid = false;
  549. item->flag.bindOnEquip = false;
  550. item->flag.broadcast = false;
  551. if (!(item->flag.delay_consume & DELAYCONSUME_TEMP))
  552. item->flag.delay_consume = DELAYCONSUME_NONE;
  553. item->flag.dropEffect = DROPEFFECT_NONE;
  554. }
  555. }
  556. if (this->nodeExists(node, "Delay")) {
  557. const YAML::Node &delayNode = node["Delay"];
  558. if (this->nodeExists(delayNode, "Duration")) {
  559. uint32 duration;
  560. if (!this->asUInt32(delayNode, "Duration", duration))
  561. return 0;
  562. item->delay.duration = duration;
  563. } else {
  564. if (!exists)
  565. item->delay.duration = 0;
  566. }
  567. if (this->nodeExists(delayNode, "Status")) {
  568. std::string status;
  569. if (!this->asString(delayNode, "Status", status))
  570. return 0;
  571. std::string status_constant = "SC_" + status;
  572. int64 constant;
  573. if (!script_get_constant(status_constant.c_str(), &constant) || constant < SC_NONE || constant >= SC_MAX) {
  574. this->invalidWarning(delayNode[status], "Invalid item delay status %s, defaulting to SC_NONE.\n", status.c_str());
  575. constant = SC_NONE;
  576. }
  577. item->delay.sc = static_cast<sc_type>(constant);
  578. } else {
  579. if (!exists)
  580. item->delay.sc = SC_NONE;
  581. }
  582. } else {
  583. if (!exists) {
  584. item->delay.duration = 0;
  585. item->delay.sc = SC_NONE;
  586. }
  587. }
  588. if (this->nodeExists(node, "Stack")) {
  589. const YAML::Node &stackNode = node["Stack"];
  590. if (this->nodeExists(stackNode, "Amount")) {
  591. uint16 amount;
  592. if (!this->asUInt16(stackNode, "Amount", amount))
  593. return 0;
  594. if (!itemdb_isstackable2(item.get())) {
  595. this->invalidWarning(stackNode["Amount"], "Non-stackable item cannot be enabled for stacking.\n");
  596. amount = 0;
  597. }
  598. item->stack.amount = amount;
  599. } else {
  600. if (!exists)
  601. item->stack.amount = 0;
  602. }
  603. if (this->nodeExists(stackNode, "Inventory")) {
  604. bool active;
  605. if (!this->asBool(stackNode, "Inventory", active))
  606. return 0;
  607. item->stack.inventory = active;
  608. } else {
  609. if (!exists)
  610. item->stack.inventory = false;
  611. }
  612. if (this->nodeExists(stackNode, "Cart")) {
  613. bool active;
  614. if (!this->asBool(stackNode, "Cart", active))
  615. return 0;
  616. item->stack.cart = active;
  617. } else {
  618. if (!exists)
  619. item->stack.cart = false;
  620. }
  621. if (this->nodeExists(stackNode, "Storage")) {
  622. bool active;
  623. if (!this->asBool(stackNode, "Storage", active))
  624. return 0;
  625. item->stack.storage = active;
  626. } else {
  627. if (!exists)
  628. item->stack.storage = false;
  629. }
  630. if (this->nodeExists(stackNode, "GuildStorage")) {
  631. bool active;
  632. if (!this->asBool(stackNode, "GuildStorage", active))
  633. return 0;
  634. item->stack.guild_storage = active;
  635. } else {
  636. if (!exists)
  637. item->stack.guild_storage = false;
  638. }
  639. } else {
  640. if (!exists) {
  641. item->stack.amount = 0;
  642. item->stack.inventory = false;
  643. item->stack.cart = false;
  644. item->stack.storage = false;
  645. item->stack.guild_storage = false;
  646. }
  647. }
  648. if (this->nodeExists(node, "NoUse")) {
  649. const YAML::Node &nouseNode = node["NoUse"];
  650. if (this->nodeExists(nouseNode, "Override")) {
  651. uint16 override;
  652. if (!this->asUInt16(nouseNode, "Override", override))
  653. return 0;
  654. if (override > 100) {
  655. this->invalidWarning(nouseNode["Override"], "Item no use override level %d exceeds 100, capping to 100.\n", override);
  656. override = 100;
  657. }
  658. item->item_usage.override = override;
  659. } else {
  660. if (!exists)
  661. item->item_usage.override = 0;
  662. }
  663. if (this->nodeExists(nouseNode, "Sitting")) {
  664. bool active;
  665. if (!this->asBool(nouseNode, "Sitting", active))
  666. return 0;
  667. item->item_usage.sitting = active;
  668. } else {
  669. if (!exists)
  670. item->item_usage.sitting = false;
  671. }
  672. } else {
  673. if (!exists) {
  674. item->item_usage.override = 0;
  675. item->item_usage.sitting = false;
  676. }
  677. }
  678. if (this->nodeExists(node, "Trade")) {
  679. const YAML::Node &tradeNode = node["Trade"];
  680. if (this->nodeExists(tradeNode, "Override")) {
  681. uint16 override;
  682. if (!this->asUInt16(tradeNode, "Override", override))
  683. return 0;
  684. if (override > 100) {
  685. this->invalidWarning(tradeNode["Override"], "Item trade override level %d exceeds 100, capping to 100.\n", override);
  686. override = 100;
  687. }
  688. item->gm_lv_trade_override = override;
  689. } else {
  690. if (!exists)
  691. item->gm_lv_trade_override = 0;
  692. }
  693. if (this->nodeExists(tradeNode, "NoDrop")) {
  694. bool active;
  695. if (!this->asBool(tradeNode, "NoDrop", active))
  696. return 0;
  697. item->flag.trade_restriction.drop = active;
  698. } else {
  699. if (!exists)
  700. item->flag.trade_restriction.drop = false;
  701. }
  702. if (this->nodeExists(tradeNode, "NoTrade")) {
  703. bool active;
  704. if (!this->asBool(tradeNode, "NoTrade", active))
  705. return 0;
  706. item->flag.trade_restriction.trade = active;
  707. } else {
  708. if (!exists)
  709. item->flag.trade_restriction.trade = false;
  710. }
  711. if (this->nodeExists(tradeNode, "TradePartner")) {
  712. bool active;
  713. if (!this->asBool(tradeNode, "TradePartner", active))
  714. return 0;
  715. item->flag.trade_restriction.trade_partner = active;
  716. } else {
  717. if (!exists)
  718. item->flag.trade_restriction.trade_partner = false;
  719. }
  720. if (this->nodeExists(tradeNode, "NoSell")) {
  721. bool active;
  722. if (!this->asBool(tradeNode, "NoSell", active))
  723. return 0;
  724. item->flag.trade_restriction.sell = active;
  725. } else {
  726. if (!exists)
  727. item->flag.trade_restriction.sell = false;
  728. }
  729. if (this->nodeExists(tradeNode, "NoCart")) {
  730. bool active;
  731. if (!this->asBool(tradeNode, "NoCart", active))
  732. return 0;
  733. item->flag.trade_restriction.cart = active;
  734. } else {
  735. if (!exists)
  736. item->flag.trade_restriction.cart = false;
  737. }
  738. if (this->nodeExists(tradeNode, "NoStorage")) {
  739. bool active;
  740. if (!this->asBool(tradeNode, "NoStorage", active))
  741. return 0;
  742. item->flag.trade_restriction.storage = active;
  743. } else {
  744. if (!exists)
  745. item->flag.trade_restriction.storage = false;
  746. }
  747. if (this->nodeExists(tradeNode, "NoGuildStorage")) {
  748. bool active;
  749. if (!this->asBool(tradeNode, "NoGuildStorage", active))
  750. return 0;
  751. item->flag.trade_restriction.guild_storage = active;
  752. } else {
  753. if (!exists)
  754. item->flag.trade_restriction.guild_storage = false;
  755. }
  756. if (this->nodeExists(tradeNode, "NoMail")) {
  757. bool active;
  758. if (!this->asBool(tradeNode, "NoMail", active))
  759. return 0;
  760. item->flag.trade_restriction.mail = active;
  761. } else {
  762. if (!exists)
  763. item->flag.trade_restriction.mail = false;
  764. }
  765. if (this->nodeExists(tradeNode, "NoAuction")) {
  766. bool active;
  767. if (!this->asBool(tradeNode, "NoAuction", active))
  768. return 0;
  769. item->flag.trade_restriction.auction = active;
  770. } else {
  771. if (!exists)
  772. item->flag.trade_restriction.auction = false;
  773. }
  774. } else {
  775. if (!exists) {
  776. item->gm_lv_trade_override = 0;
  777. item->flag.trade_restriction.drop = false;
  778. item->flag.trade_restriction.trade = false;
  779. item->flag.trade_restriction.trade_partner = false;
  780. item->flag.trade_restriction.sell = false;
  781. item->flag.trade_restriction.cart = false;
  782. item->flag.trade_restriction.storage = false;
  783. item->flag.trade_restriction.guild_storage = false;
  784. item->flag.trade_restriction.mail = false;
  785. item->flag.trade_restriction.auction = false;
  786. }
  787. }
  788. if (this->nodeExists(node, "Script")) {
  789. std::string script;
  790. if (!this->asString(node, "Script", script))
  791. return 0;
  792. if (exists && item->script) {
  793. script_free_code(item->script);
  794. item->script = nullptr;
  795. }
  796. item->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), node["Script"].Mark().line + 1, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  797. } else {
  798. if (!exists)
  799. item->script = nullptr;
  800. }
  801. if (this->nodeExists(node, "EquipScript")) {
  802. std::string script;
  803. if (!this->asString(node, "EquipScript", script))
  804. return 0;
  805. if (exists && item->equip_script) {
  806. script_free_code(item->equip_script);
  807. item->equip_script = nullptr;
  808. }
  809. item->equip_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), node["EquipScript"].Mark().line + 1, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  810. } else {
  811. if (!exists)
  812. item->equip_script = nullptr;
  813. }
  814. if (this->nodeExists(node, "UnEquipScript")) {
  815. std::string script;
  816. if (!this->asString(node, "UnEquipScript", script))
  817. return 0;
  818. if (exists && item->unequip_script) {
  819. script_free_code(item->unequip_script);
  820. item->unequip_script = nullptr;
  821. }
  822. item->unequip_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), node["UnEquipScript"].Mark().line + 1, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  823. } else {
  824. if (!exists)
  825. item->unequip_script = nullptr;
  826. }
  827. if (!exists)
  828. this->put(nameid, item);
  829. return 1;
  830. }
  831. void ItemDatabase::loadingFinished(){
  832. for (auto &tmp_item : item_db) {
  833. std::shared_ptr<item_data> item = tmp_item.second;
  834. // Items that are consumed only after target confirmation
  835. if (item->type == IT_DELAYCONSUME) {
  836. item->type = IT_USABLE;
  837. item->flag.delay_consume |= DELAYCONSUME_TEMP;
  838. } else {
  839. item->flag.delay_consume &= ~DELAYCONSUME_TEMP; // Remove delayed consumption flag if switching types
  840. }
  841. if( item->type == IT_WEAPON ){
  842. if( item->weapon_level == 0 ){
  843. ShowWarning( "Item %s is a weapon, but does not have a weapon level. Consider adding it. Defaulting to 1.\n", item->name.c_str() );
  844. item->weapon_level = 1;
  845. }
  846. if( item->armor_level != 0 ){
  847. ShowWarning( "Item %s is a weapon, but has an armor level. Defaulting to 0.\n", item->name.c_str() );
  848. item->armor_level = 0;
  849. }
  850. }else if( item->type == IT_ARMOR ){
  851. if( item->armor_level == 0 ){
  852. ShowWarning( "Item %s is an armor, but does not have an armor level. Consider adding it. Defaulting to 1.\n", item->name.c_str() );
  853. item->armor_level = 1;
  854. }
  855. if( item->weapon_level != 0 ){
  856. ShowWarning( "Item %s is an armor, but has a weapon level. Defaulting to 0.\n", item->name.c_str() );
  857. item->weapon_level = 0;
  858. }
  859. }else{
  860. if( item->weapon_level != 0 ){
  861. ShowWarning( "Item %s is not a weapon, but has a weapon level. Defaulting to 0.\n", item->name.c_str() );
  862. item->weapon_level = 0;
  863. }
  864. if( item->armor_level != 0 ){
  865. ShowWarning( "Item %s is not an armor, but has an armor level. Defaulting to 0.\n", item->name.c_str() );
  866. item->armor_level = 0;
  867. }
  868. }
  869. // When a particular price is not given, we should base it off the other one
  870. if (item->value_buy == 0 && item->value_sell > 0)
  871. item->value_buy = item->value_sell * 2;
  872. else if (item->value_buy > 0 && item->value_sell == 0)
  873. item->value_sell = item->value_buy / 2;
  874. if (item->value_buy / 124. < item->value_sell / 75.) {
  875. 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);
  876. item->value_sell = 1;
  877. }
  878. // Shields need to have a view ID to be able to be recognized by ST_SHIELD check in skill.cpp
  879. if( item->type == IT_ARMOR && ( item->equip & EQP_SHIELD ) != 0 && item->look == 0 ){
  880. ShowWarning( "Item %s (%u) is a shield and should have a view id. Defaulting to Guard...\n", item->name.c_str(), item->nameid );
  881. item->look = 1;
  882. }
  883. }
  884. if( !this->exists( ITEMID_DUMMY ) ){
  885. // Create dummy item
  886. std::shared_ptr<item_data> dummy_item = std::make_shared<item_data>();
  887. dummy_item->nameid = ITEMID_DUMMY;
  888. dummy_item->weight = 1;
  889. dummy_item->value_sell = 1;
  890. dummy_item->type = IT_ETC;
  891. dummy_item->name = "UNKNOWN_ITEM";
  892. dummy_item->ename = "Unknown Item";
  893. dummy_item->view_id = UNKNOWN_ITEM_ID;
  894. item_db.put( ITEMID_DUMMY, dummy_item );
  895. }
  896. }
  897. /**
  898. * Applies gender restrictions according to settings.
  899. * @param node: YAML node containing the entry.
  900. * @param node: the already parsed item data.
  901. * @return gender that should be used.
  902. */
  903. e_sex ItemDatabase::defaultGender( const YAML::Node &node, std::shared_ptr<item_data> id ){
  904. if (id->nameid == WEDDING_RING_M) //Grom Ring
  905. return SEX_MALE;
  906. if (id->nameid == WEDDING_RING_F) //Bride Ring
  907. return SEX_FEMALE;
  908. if( id->type == IT_WEAPON ){
  909. if( id->subtype == W_MUSICAL ){
  910. if( id->sex != SEX_MALE ){
  911. this->invalidWarning( node, "Musical instruments are always male-only, defaulting to SEX_MALE.\n" );
  912. }
  913. return SEX_MALE;
  914. }
  915. if( id->subtype == W_WHIP ){
  916. if( id->sex != SEX_FEMALE ){
  917. this->invalidWarning( node, "Whips are always female-only, defaulting to SEX_FEMALE.\n" );
  918. }
  919. return SEX_FEMALE;
  920. }
  921. }
  922. return static_cast<e_sex>( id->sex );
  923. }
  924. std::shared_ptr<item_data> ItemDatabase::search_aegisname( const char* name ){
  925. // Create a copy
  926. std::string lowername = name;
  927. // Convert it to lower
  928. util::tolower( lowername );
  929. return util::umap_find( this->aegisNameToItemDataMap, lowername );
  930. }
  931. std::shared_ptr<item_data> ItemDatabase::searchname( const char *name ){
  932. // Create a copy
  933. std::string lowername = name;
  934. // Convert it to lower
  935. util::tolower( lowername );
  936. std::shared_ptr<item_data> result = util::umap_find( this->aegisNameToItemDataMap, lowername );
  937. if( result != nullptr ){
  938. return result;
  939. }
  940. return util::umap_find( this->nameToItemDataMap, lowername );
  941. }
  942. ItemDatabase item_db;
  943. /**
  944. * Check if an item exists in a group
  945. * @param group_id: Item Group ID
  946. * @param nameid: Item to check for in group
  947. * @return True if item is in group, else false
  948. */
  949. bool ItemGroupDatabase::item_exists(uint16 group_id, t_itemid nameid)
  950. {
  951. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  952. if (group == nullptr)
  953. return false;
  954. for (const auto &random : group->random) {
  955. for (const auto &it : random.second->data) {
  956. if (it.second->nameid == nameid)
  957. return true;
  958. }
  959. }
  960. return false;
  961. }
  962. /**
  963. * Check if an item exists from a group in a player's inventory
  964. * @param group_id: Item Group ID
  965. * @return Item's index if found or -1 otherwise
  966. */
  967. int16 ItemGroupDatabase::item_exists_pc(map_session_data *sd, uint16 group_id)
  968. {
  969. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  970. if (group == nullptr || group->random.empty())
  971. return -1;
  972. for (const auto &random : group->random) {
  973. for (const auto &it : random.second->data) {
  974. int16 item_position = pc_search_inventory(sd, it.second->nameid);
  975. if (item_position != -1)
  976. return item_position;
  977. }
  978. }
  979. return -1;
  980. }
  981. /*==========================================
  982. * Return item data from item name. (lookup)
  983. * @param str Item Name
  984. * @param aegis_only
  985. * @return item data
  986. *------------------------------------------*/
  987. static struct item_data* itemdb_searchname1(const char *str, bool aegis_only)
  988. {
  989. for (const auto &it : item_db) {
  990. // Absolute priority to Aegis code name.
  991. if (strcmpi(it.second->name.c_str(), str) == 0)
  992. return it.second.get();
  993. // If only Aegis name is allowed, continue with the next entry
  994. if (aegis_only)
  995. continue;
  996. //Second priority to Client displayed name.
  997. if (strcmpi(it.second->ename.c_str(), str) == 0)
  998. return it.second.get();
  999. }
  1000. return nullptr;
  1001. }
  1002. /*==========================================
  1003. * Finds up to N matches. Returns number of matches [Skotlex]
  1004. * @param *data
  1005. * @param size
  1006. * @param str
  1007. * @return Number of matches item
  1008. *------------------------------------------*/
  1009. int itemdb_searchname_array(struct item_data** data, int size, const char *str)
  1010. {
  1011. int count = 0;
  1012. for (const auto &it : item_db) {
  1013. if (count < size) {
  1014. if (stristr(it.second->name.c_str(), str) != nullptr || stristr(it.second->ename.c_str(), str) != nullptr || strcmpi(it.second->ename.c_str(), str) == 0)
  1015. data[count++] = it.second.get();
  1016. } else
  1017. break;
  1018. }
  1019. return count;
  1020. }
  1021. std::shared_ptr<s_item_group_entry> get_random_itemsubgroup(std::shared_ptr<s_item_group_random> random) {
  1022. if (random == nullptr)
  1023. return nullptr;
  1024. for (size_t j = 0, max = random->data.size() * 3; j < max; j++) {
  1025. std::shared_ptr<s_item_group_entry> entry = util::umap_random(random->data);
  1026. if (entry->rate == 0 || rnd() % random->total_rate < entry->rate) // always return entry for rate 0 ('must' item)
  1027. return entry;
  1028. }
  1029. return util::umap_random(random->data);
  1030. }
  1031. /**
  1032. * Return a random group entry from Item Group
  1033. * @param group_id
  1034. * @param sub_group: 0 is 'must' item group, random groups start from 1
  1035. * @return Item group entry or NULL on fail
  1036. */
  1037. std::shared_ptr<s_item_group_entry> ItemGroupDatabase::get_random_entry(uint16 group_id, uint8 sub_group) {
  1038. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  1039. if (group == nullptr) {
  1040. ShowError("get_random_entry: Invalid group id %hu.\n", group_id);
  1041. return nullptr;
  1042. }
  1043. if (group->random.empty()) {
  1044. ShowError("get_random_entry: No item entries for group id %hu.\n", group_id);
  1045. return nullptr;
  1046. }
  1047. if (group->random.count(sub_group) == 0) {
  1048. ShowError("get_random_entry: No item entries for group id %hu and sub group %hu.\n", group_id, sub_group);
  1049. return nullptr;
  1050. }
  1051. return get_random_itemsubgroup(group->random[sub_group]);
  1052. }
  1053. /**
  1054. * Return a random Item ID from Item Group
  1055. * @param group_id
  1056. * @param sub_group: 0 is 'must' item group, random groups start from 1
  1057. * @return Item ID or UNKNOWN_ITEM_ID on fail
  1058. */
  1059. t_itemid ItemGroupDatabase::get_random_item_id(uint16 group_id, uint8 sub_group) {
  1060. std::shared_ptr<s_item_group_entry> entry = this->get_random_entry(group_id, sub_group);
  1061. return entry != nullptr ? entry->nameid : UNKNOWN_ITEM_ID;
  1062. }
  1063. /** [Cydh]
  1064. * Gives item(s) to the player based on item group
  1065. * @param sd: Player that obtains item from item group
  1066. * @param group_id: The group ID of item that obtained by player
  1067. * @param *group: struct s_item_group from itemgroup_db[group_id].random[idx] or itemgroup_db[group_id].must[sub_group][idx]
  1068. */
  1069. static void itemdb_pc_get_itemgroup_sub(map_session_data *sd, bool identify, std::shared_ptr<s_item_group_entry> data) {
  1070. if (data == nullptr)
  1071. return;
  1072. item tmp = {};
  1073. tmp.nameid = data->nameid;
  1074. tmp.bound = data->bound;
  1075. tmp.identify = identify ? identify : itemdb_isidentified(data->nameid);
  1076. tmp.expire_time = (data->duration) ? (unsigned int)(time(NULL) + data->duration*60) : 0;
  1077. if (data->isNamed) {
  1078. tmp.card[0] = itemdb_isequip(data->nameid) ? CARD0_FORGE : CARD0_CREATE;
  1079. tmp.card[1] = 0;
  1080. tmp.card[2] = GetWord(sd->status.char_id, 0);
  1081. tmp.card[3] = GetWord(sd->status.char_id, 1);
  1082. }
  1083. uint16 get_amt = 0;
  1084. if (itemdb_isstackable(data->nameid) && data->isStacked)
  1085. get_amt = data->amount;
  1086. else
  1087. get_amt = 1;
  1088. tmp.amount = get_amt;
  1089. // Do loop for non-stackable item
  1090. for (uint16 i = 0; i < data->amount; i += get_amt) {
  1091. char flag = 0;
  1092. tmp.unique_id = data->GUID ? pc_generate_unique_id(sd) : 0; // Generate GUID
  1093. if ((flag = pc_additem(sd, &tmp, get_amt, LOG_TYPE_SCRIPT))) {
  1094. clif_additem(sd, 0, 0, flag);
  1095. if (pc_candrop(sd, &tmp))
  1096. map_addflooritem(&tmp, tmp.amount, sd->bl.m, sd->bl.x,sd->bl.y, 0, 0, 0, 0, 0);
  1097. }
  1098. else if (!flag && data->isAnnounced)
  1099. intif_broadcast_obtain_special_item(sd, data->nameid, sd->itemid, ITEMOBTAIN_TYPE_BOXITEM);
  1100. }
  1101. }
  1102. /** [Cydh]
  1103. * Find item(s) that will be obtained by player based on Item Group
  1104. * @param group_id: The group ID that will be gained by player
  1105. * @param nameid: The item that trigger this item group
  1106. * @return val: 0:success, 1:no sd, 2:invalid item group
  1107. */
  1108. uint8 ItemGroupDatabase::pc_get_itemgroup(uint16 group_id, bool identify, map_session_data *sd) {
  1109. nullpo_retr(1,sd);
  1110. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  1111. if (group == nullptr) {
  1112. ShowError("pc_get_itemgroup: Invalid group id '%d' specified.\n",group_id);
  1113. return 2;
  1114. }
  1115. if (group->random.empty())
  1116. return 0;
  1117. // Get all the 'must' item(s) (subgroup 0)
  1118. uint16 subgroup = 0;
  1119. std::shared_ptr<s_item_group_random> random = util::umap_find(group->random, subgroup);
  1120. if (random != nullptr && !random->data.empty()) {
  1121. for (const auto &it : random->data)
  1122. itemdb_pc_get_itemgroup_sub(sd, identify, it.second);
  1123. }
  1124. // Get 1 'random' item from each subgroup
  1125. for (const auto &random : group->random) {
  1126. if (random.first == 0 || random.second->data.empty())
  1127. continue;
  1128. itemdb_pc_get_itemgroup_sub(sd, identify, get_random_itemsubgroup(random.second));
  1129. }
  1130. return 0;
  1131. }
  1132. /** Searches for the item_data. Use this to check if item exists or not.
  1133. * @param nameid
  1134. * @return *item_data if item is exist, or NULL if not
  1135. */
  1136. struct item_data* itemdb_exists(t_itemid nameid) {
  1137. std::shared_ptr<item_data> item = item_db.find(nameid);
  1138. return item ? item.get() : nullptr;
  1139. }
  1140. /// Returns name type of ammunition [Cydh]
  1141. const char *itemdb_typename_ammo (e_ammo_type ammo) {
  1142. switch (ammo) {
  1143. case AMMO_ARROW: return "Arrow";
  1144. case AMMO_DAGGER: return "Throwable Dagger";
  1145. case AMMO_BULLET: return "Bullet";
  1146. case AMMO_SHELL: return "Shell";
  1147. case AMMO_GRENADE: return "Grenade";
  1148. case AMMO_SHURIKEN: return "Shuriken";
  1149. case AMMO_KUNAI: return "Kunai";
  1150. case AMMO_CANNONBALL: return "Cannonball";
  1151. case AMMO_THROWWEAPON: return "Throwable Item/Sling Item";
  1152. }
  1153. return "Ammunition";
  1154. }
  1155. /// Returns human readable name for given item type.
  1156. /// @param type Type id to retrieve name for ( IT_* ).
  1157. const char* itemdb_typename(enum item_types type)
  1158. {
  1159. switch(type)
  1160. {
  1161. case IT_HEALING: return "Potion/Food";
  1162. case IT_USABLE: return "Usable";
  1163. case IT_ETC: return "Etc.";
  1164. case IT_WEAPON: return "Weapon";
  1165. case IT_ARMOR: return "Armor";
  1166. case IT_CARD: return "Card";
  1167. case IT_PETEGG: return "Pet Egg";
  1168. case IT_PETARMOR: return "Pet Accessory";
  1169. case IT_AMMO: return "Arrow/Ammunition";
  1170. case IT_DELAYCONSUME: return "Delay-Consume Usable";
  1171. case IT_SHADOWGEAR: return "Shadow Equipment";
  1172. case IT_CASH: return "Cash Usable";
  1173. }
  1174. return "Unknown Type";
  1175. }
  1176. /**
  1177. * Converts the jobmask from the format in itemdb to the format used by the map server.
  1178. * @param bclass: Pointer to store itemdb format
  1179. * @param jobmask: Job Mask to convert
  1180. * @param active: Whether the flag is active or not
  1181. * @author: Skotlex
  1182. */
  1183. static void itemdb_jobid2mapid(uint64 bclass[3], e_mapid jobmask, bool active)
  1184. {
  1185. uint64 temp_mask[3] = { 0 };
  1186. if (jobmask != MAPID_ALL) {
  1187. // Calculate the required bit to set
  1188. uint64 job = 1ULL << ( jobmask & MAPID_BASEMASK );
  1189. // 2-1
  1190. if( ( jobmask & JOBL_2_1 ) != 0 ){
  1191. temp_mask[1] |= job;
  1192. // 2-2
  1193. }else if( ( jobmask & JOBL_2_2 ) != 0 ){
  1194. temp_mask[2] |= job;
  1195. // Basejob
  1196. }else{
  1197. temp_mask[0] |= job;
  1198. }
  1199. } else {
  1200. temp_mask[0] = temp_mask[1] = temp_mask[2] = MAPID_ALL;
  1201. }
  1202. for (int i = 0; i < ARRAYLENGTH(temp_mask); i++) {
  1203. if (temp_mask[i] == 0)
  1204. continue;
  1205. if (active)
  1206. bclass[i] |= temp_mask[i];
  1207. else
  1208. bclass[i] &= ~temp_mask[i];
  1209. }
  1210. }
  1211. /*==========================================
  1212. * Loads an item from the db. If not found, it will return the dummy item.
  1213. * @param nameid
  1214. * @return *item_data or *dummy_item if item not found
  1215. *------------------------------------------*/
  1216. struct item_data* itemdb_search(t_itemid nameid) {
  1217. std::shared_ptr<item_data> id;
  1218. if (!(id = item_db.find(nameid))) {
  1219. ShowWarning("itemdb_search: Item ID %u does not exists in the item_db. Using dummy data.\n", nameid);
  1220. id = item_db.find(ITEMID_DUMMY);
  1221. }
  1222. return id.get();
  1223. }
  1224. /** Checks if item is equip type or not
  1225. * @param id Item data
  1226. * @return True if item is equip, false otherwise
  1227. */
  1228. bool itemdb_isequip2(struct item_data *id) {
  1229. nullpo_ret(id);
  1230. switch (id->type) {
  1231. case IT_WEAPON:
  1232. case IT_ARMOR:
  1233. case IT_AMMO:
  1234. case IT_SHADOWGEAR:
  1235. return true;
  1236. default:
  1237. return false;
  1238. }
  1239. }
  1240. /** Checks if item is stackable or not
  1241. * @param id Item data
  1242. * @return True if item is stackable, false otherwise
  1243. */
  1244. bool itemdb_isstackable2(struct item_data *id)
  1245. {
  1246. nullpo_ret(id);
  1247. return id->isStackable();
  1248. }
  1249. /*==========================================
  1250. * Trade Restriction functions [Skotlex]
  1251. *------------------------------------------*/
  1252. bool itemdb_isdropable_sub(struct item_data *item, int gmlv, int unused) {
  1253. return (item && (!(item->flag.trade_restriction.drop) || gmlv >= item->gm_lv_trade_override));
  1254. }
  1255. bool itemdb_cantrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  1256. return (item && (!(item->flag.trade_restriction.trade) || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  1257. }
  1258. bool itemdb_canpartnertrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  1259. return (item && (item->flag.trade_restriction.trade_partner || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  1260. }
  1261. bool itemdb_cansell_sub(struct item_data* item, int gmlv, int unused) {
  1262. return (item && (!(item->flag.trade_restriction.sell) || gmlv >= item->gm_lv_trade_override));
  1263. }
  1264. bool itemdb_cancartstore_sub(struct item_data* item, int gmlv, int unused) {
  1265. return (item && (!(item->flag.trade_restriction.cart) || gmlv >= item->gm_lv_trade_override));
  1266. }
  1267. bool itemdb_canstore_sub(struct item_data* item, int gmlv, int unused) {
  1268. return (item && (!(item->flag.trade_restriction.storage) || gmlv >= item->gm_lv_trade_override));
  1269. }
  1270. bool itemdb_canguildstore_sub(struct item_data* item, int gmlv, int unused) {
  1271. return (item && (!(item->flag.trade_restriction.guild_storage) || gmlv >= item->gm_lv_trade_override));
  1272. }
  1273. bool itemdb_canmail_sub(struct item_data* item, int gmlv, int unused) {
  1274. return (item && (!(item->flag.trade_restriction.mail) || gmlv >= item->gm_lv_trade_override));
  1275. }
  1276. bool itemdb_canauction_sub(struct item_data* item, int gmlv, int unused) {
  1277. return (item && (!(item->flag.trade_restriction.auction) || gmlv >= item->gm_lv_trade_override));
  1278. }
  1279. bool itemdb_isrestricted(struct item* item, int gmlv, int gmlv2, bool (*func)(struct item_data*, int, int))
  1280. {
  1281. struct item_data* item_data = itemdb_search(item->nameid);
  1282. int i;
  1283. if (!func(item_data, gmlv, gmlv2))
  1284. return false;
  1285. if(item_data->slots == 0 || itemdb_isspecial(item->card[0]))
  1286. return true;
  1287. for(i = 0; i < item_data->slots; i++) {
  1288. if (!item->card[i]) continue;
  1289. if (!func(itemdb_search(item->card[i]), gmlv, gmlv2))
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. bool itemdb_ishatched_egg(struct item* item) {
  1295. if (item && item->card[0] == CARD0_PET && item->attribute == 1)
  1296. return true;
  1297. return false;
  1298. }
  1299. /** Specifies if item-type should drop unidentified.
  1300. * @param nameid ID of item
  1301. */
  1302. char itemdb_isidentified(t_itemid nameid) {
  1303. int type=itemdb_type(nameid);
  1304. switch (type) {
  1305. case IT_WEAPON:
  1306. case IT_ARMOR:
  1307. case IT_PETARMOR:
  1308. case IT_SHADOWGEAR:
  1309. return 0;
  1310. default:
  1311. return 1;
  1312. }
  1313. }
  1314. const std::string ItemGroupDatabase::getDefaultLocation() {
  1315. return std::string(db_path) + "/item_group_db.yml";
  1316. }
  1317. /**
  1318. * Reads and parses an entry from the item_group_db.
  1319. * @param node: YAML node containing the entry.
  1320. * @return count of successfully parsed rows
  1321. */
  1322. uint64 ItemGroupDatabase::parseBodyNode(const YAML::Node &node) {
  1323. std::string group_name;
  1324. if (!this->asString(node, "Group", group_name))
  1325. return 0;
  1326. std::string group_name_constant = "IG_" + group_name;
  1327. int64 constant;
  1328. if (!script_get_constant(group_name_constant.c_str(), &constant) || constant < IG_BLUEBOX) {
  1329. if (strncasecmp(group_name.c_str(), "IG_", 3) != 0)
  1330. this->invalidWarning(node["Group"], "Invalid group %s.\n", group_name.c_str());
  1331. else
  1332. this->invalidWarning(node["Group"], "Invalid group %s. Note that 'IG_' is automatically appended to the group name.\n", group_name.c_str());
  1333. return 0;
  1334. }
  1335. uint16 id = static_cast<uint16>(constant);
  1336. std::shared_ptr<s_item_group_db> group = this->find(id);
  1337. bool exists = group != nullptr;
  1338. if (!exists) {
  1339. group = std::make_shared<s_item_group_db>();
  1340. group->id = id;
  1341. }
  1342. if (this->nodeExists(node, "SubGroups")) {
  1343. const YAML::Node &subNode = node["SubGroups"];
  1344. for (const YAML::Node &subit : subNode) {
  1345. if (this->nodeExists(subit, "Clear")) {
  1346. uint16 id;
  1347. if (!this->asUInt16(subit, "Clear", id))
  1348. continue;
  1349. if (group->random.erase(id) == 0)
  1350. this->invalidWarning(subit["Clear"], "The SubGroup %hu doesn't exist in the group %s. Clear failed.\n", id, group_name.c_str());
  1351. continue;
  1352. }
  1353. if (!this->nodesExist(subit, { "SubGroup", "List" })) {
  1354. continue;
  1355. }
  1356. uint16 subgroup;
  1357. if (this->nodeExists(subit, "SubGroup")) {
  1358. if (!this->asUInt16(subit, "SubGroup", subgroup))
  1359. continue;
  1360. } else {
  1361. subgroup = 1;
  1362. }
  1363. std::shared_ptr<s_item_group_random> random = util::umap_find(group->random, subgroup);
  1364. if (random == nullptr) {
  1365. random = std::make_shared<s_item_group_random>();
  1366. group->random[subgroup] = random;
  1367. }
  1368. const YAML::Node &listNode = subit["List"];
  1369. for (const YAML::Node &listit : listNode) {
  1370. if (this->nodeExists(listit, "Clear")) {
  1371. std::string item_name;
  1372. if (!this->asString(listit, "Clear", item_name))
  1373. continue;
  1374. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  1375. if (item == nullptr) {
  1376. this->invalidWarning(listit["Clear"], "Unknown Item %s. Clear failed.\n", item_name.c_str());
  1377. continue;
  1378. }
  1379. if (random->data.erase(item->nameid) == 0)
  1380. 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());
  1381. continue;
  1382. }
  1383. std::string item_name;
  1384. if (!this->asString(listit, "Item", item_name))
  1385. continue;
  1386. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  1387. if (item == nullptr) {
  1388. this->invalidWarning(listit["Item"], "Unknown Item %s.\n", item_name.c_str());
  1389. continue;
  1390. }
  1391. std::shared_ptr<s_item_group_entry> entry = util::umap_find(random->data, item->nameid);
  1392. bool entry_exists = entry != nullptr;
  1393. if (!entry_exists) {
  1394. entry = std::make_shared<s_item_group_entry>();
  1395. random->data[item->nameid] = entry;
  1396. }
  1397. entry->nameid = item->nameid;
  1398. if (this->nodeExists(listit, "Rate")) {
  1399. uint16 rate;
  1400. if (!this->asUInt16(listit, "Rate", rate))
  1401. continue;
  1402. entry->rate = rate;
  1403. } else {
  1404. if (!entry_exists)
  1405. entry->rate = 0;
  1406. }
  1407. if (subgroup == 0 && entry->rate > 0) {
  1408. this->invalidWarning(listit["Item"], "SubGroup 0 is reserved for item without Rate ('must' item). Defaulting Rate to 0.\n");
  1409. entry->rate = 0;
  1410. }
  1411. if (subgroup != 0 && entry->rate == 0) {
  1412. this->invalidWarning(listit["Item"], "Entry must have a Rate for group above 0, skipping.\n");
  1413. continue;
  1414. }
  1415. if (this->nodeExists(listit, "Amount")) {
  1416. uint16 amount;
  1417. if (!this->asUInt16(listit, "Amount", amount))
  1418. continue;
  1419. entry->amount = cap_value(amount, 1, MAX_AMOUNT);
  1420. } else {
  1421. if (!entry_exists)
  1422. entry->amount = 1;
  1423. }
  1424. if (this->nodeExists(listit, "Duration")) {
  1425. uint16 duration;
  1426. if (!this->asUInt16(listit, "Duration", duration))
  1427. continue;
  1428. entry->duration = duration;
  1429. } else {
  1430. if (!entry_exists)
  1431. entry->duration = 0;
  1432. }
  1433. if (this->nodeExists(listit, "Announced")) {
  1434. bool isAnnounced;
  1435. if (!this->asBool(listit, "Announced", isAnnounced))
  1436. continue;
  1437. entry->isAnnounced = isAnnounced;
  1438. } else {
  1439. if (!entry_exists)
  1440. entry->isAnnounced = false;
  1441. }
  1442. if (this->nodeExists(listit, "UniqueId")) {
  1443. bool guid;
  1444. if (!this->asBool(listit, "UniqueId", guid))
  1445. continue;
  1446. entry->GUID = guid;
  1447. } else {
  1448. if (!entry_exists)
  1449. entry->GUID = item->flag.guid;
  1450. }
  1451. if (this->nodeExists(listit, "Stacked")) {
  1452. bool isStacked;
  1453. if (!this->asBool(listit, "Stacked", isStacked))
  1454. continue;
  1455. entry->isStacked = isStacked;
  1456. } else {
  1457. if (!entry_exists)
  1458. entry->isStacked = true;
  1459. }
  1460. if (this->nodeExists(listit, "Named")) {
  1461. bool named;
  1462. if (!this->asBool(listit, "Named", named))
  1463. continue;
  1464. entry->isNamed = named;
  1465. } else {
  1466. if (!entry_exists)
  1467. entry->isNamed = false;
  1468. }
  1469. if (this->nodeExists(listit, "Bound")) {
  1470. std::string bound_name;
  1471. if (!this->asString(listit, "Bound", bound_name))
  1472. continue;
  1473. std::string bound_name_constant = "BOUND_" + bound_name;
  1474. int64 bound;
  1475. if (!script_get_constant(bound_name_constant.c_str(), &bound) || bound < BOUND_NONE || bound >= BOUND_MAX) {
  1476. this->invalidWarning(listit["Group"], "Invalid Bound %s.\n", bound_name.c_str());
  1477. continue;
  1478. }
  1479. entry->bound = static_cast<uint8>(bound);
  1480. } else {
  1481. if (!entry_exists)
  1482. entry->bound = BOUND_NONE;
  1483. }
  1484. }
  1485. }
  1486. }
  1487. if (!exists)
  1488. this->put(id, group);
  1489. return 1;
  1490. }
  1491. void ItemGroupDatabase::loadingFinished() {
  1492. for (const auto &group : *this) {
  1493. for (const auto &random : group.second->random) {
  1494. random.second->total_rate = 0;
  1495. for (const auto &it : random.second->data) {
  1496. random.second->total_rate += it.second->rate;
  1497. }
  1498. }
  1499. }
  1500. }
  1501. /** Read item forbidden by mapflag (can't equip item)
  1502. * Structure: <nameid>,<mode>
  1503. */
  1504. static bool itemdb_read_noequip(char* str[], int columns, int current) {
  1505. t_itemid nameid;
  1506. int flag;
  1507. struct item_data *id;
  1508. nameid = strtoul(str[0], nullptr, 10);
  1509. flag = atoi(str[1]);
  1510. if( ( id = itemdb_exists(nameid) ) == NULL )
  1511. {
  1512. ShowWarning("itemdb_read_noequip: Invalid item id %u.\n", nameid);
  1513. return false;
  1514. }
  1515. if (flag >= 0)
  1516. id->flag.no_equip |= flag;
  1517. else
  1518. id->flag.no_equip &= ~abs(flag);
  1519. return true;
  1520. }
  1521. const std::string ComboDatabase::getDefaultLocation() {
  1522. return std::string(db_path) + "/item_combos.yml";
  1523. }
  1524. uint16 ComboDatabase::find_combo_id( const std::vector<t_itemid>& items ){
  1525. for (const auto &it : *this) {
  1526. if (it.second->nameid == items) {
  1527. return it.first;
  1528. }
  1529. }
  1530. return 0;
  1531. }
  1532. /**
  1533. * Reads and parses an entry from the item_combos.
  1534. * @param node: YAML node containing the entry.
  1535. * @return count of successfully parsed rows
  1536. */
  1537. uint64 ComboDatabase::parseBodyNode(const YAML::Node &node) {
  1538. std::vector<std::vector<t_itemid>> items_list;
  1539. if( !this->nodesExist( node, { "Combos" } ) ){
  1540. return 0;
  1541. }
  1542. const YAML::Node &combosNode = node["Combos"];
  1543. for (const auto &comboit : combosNode) {
  1544. static const std::string nodeName = "Combo";
  1545. if (!this->nodesExist(comboit, { nodeName })) {
  1546. return 0;
  1547. }
  1548. const YAML::Node &comboNode = comboit[nodeName];
  1549. if (!comboNode.IsSequence()) {
  1550. this->invalidWarning(comboNode, "%s should be a sequence.\n", nodeName.c_str());
  1551. return 0;
  1552. }
  1553. std::vector<t_itemid> items = {};
  1554. for (const auto &it : comboNode) {
  1555. std::string item_name = it.as<std::string>();
  1556. std::shared_ptr<item_data> item = item_db.search_aegisname(item_name.c_str());
  1557. if (item == nullptr) {
  1558. this->invalidWarning(it, "Invalid item %s, skipping.\n", item_name.c_str());
  1559. return 0;
  1560. }
  1561. items.push_back(item->nameid);
  1562. }
  1563. if (items.empty()) {
  1564. this->invalidWarning(comboNode, "Empty combo, skipping.\n");
  1565. return 0;
  1566. }
  1567. if (items.size() < 2) {
  1568. this->invalidWarning(comboNode, "Not enough item to make a combo (need at least 2). Skipping.\n");
  1569. return 0;
  1570. }
  1571. std::sort(items.begin(), items.end());
  1572. items_list.push_back(items);
  1573. }
  1574. if (items_list.empty()) {
  1575. this->invalidWarning(combosNode, "No combos defined, skipping.\n");
  1576. return 0;
  1577. }
  1578. if (this->nodeExists(node, "Clear")) {
  1579. bool clear = false;
  1580. if (!this->asBool(node, "Clear", clear))
  1581. return 0;
  1582. // Remove the combo (if exists)
  1583. if (clear) {
  1584. for (const auto& itemsit : items_list) {
  1585. uint16 id = this->find_combo_id(itemsit);
  1586. if (id == 0) {
  1587. this->invalidWarning(node["Clear"], "Unable to clear the combo.\n");
  1588. return 0;
  1589. }
  1590. this->erase(id);
  1591. }
  1592. return 1;
  1593. }
  1594. }
  1595. uint64 count = 0;
  1596. for (const auto &itemsit : items_list) {
  1597. // Find the id when the combo exists
  1598. uint16 id = this->find_combo_id(itemsit);
  1599. std::shared_ptr<s_item_combo> combo = this->find(id);
  1600. bool exists = combo != nullptr;
  1601. if (!exists) {
  1602. combo = std::make_shared<s_item_combo>();
  1603. combo->nameid.insert(combo->nameid.begin(), itemsit.begin(), itemsit.end());
  1604. combo->id = ++this->combo_num;
  1605. }
  1606. if (this->nodeExists(node, "Script")) {
  1607. std::string script;
  1608. if (!this->asString(node, "Script", script))
  1609. return 0;
  1610. if (exists) {
  1611. script_free_code(combo->script);
  1612. combo->script = nullptr;
  1613. }
  1614. combo->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), node["Script"].Mark().line + 1, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  1615. } else {
  1616. if (!exists) {
  1617. combo->script = nullptr;
  1618. }
  1619. }
  1620. if (!exists)
  1621. this->put( combo->id, combo );
  1622. count++;
  1623. }
  1624. return count;
  1625. }
  1626. void ComboDatabase::loadingFinished() {
  1627. // Populate item_data to refer to the combo
  1628. for (const auto &combo : *this) {
  1629. for (const auto &itm : combo.second->nameid) {
  1630. item_data *it = itemdb_exists(itm);
  1631. it->combos.push_back(combo.second);
  1632. }
  1633. }
  1634. }
  1635. /**
  1636. * Process Roulette items
  1637. */
  1638. bool itemdb_parse_roulette_db(void)
  1639. {
  1640. int i, j;
  1641. uint32 count = 0;
  1642. // retrieve all rows from the item database
  1643. if (SQL_ERROR == Sql_Query(mmysql_handle, "SELECT * FROM `%s`", roulette_table)) {
  1644. Sql_ShowDebug(mmysql_handle);
  1645. return false;
  1646. }
  1647. for (i = 0; i < MAX_ROULETTE_LEVEL; i++)
  1648. rd.items[i] = 0;
  1649. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  1650. int k, limit = MAX_ROULETTE_COLUMNS - i;
  1651. for (k = 0; k < limit && SQL_SUCCESS == Sql_NextRow(mmysql_handle); k++) {
  1652. char* data;
  1653. t_itemid item_id;
  1654. unsigned short amount;
  1655. int level, flag;
  1656. Sql_GetData(mmysql_handle, 1, &data, NULL); level = atoi(data);
  1657. Sql_GetData(mmysql_handle, 2, &data, NULL); item_id = strtoul(data, nullptr, 10);
  1658. Sql_GetData(mmysql_handle, 3, &data, NULL); amount = atoi(data);
  1659. Sql_GetData(mmysql_handle, 4, &data, NULL); flag = atoi(data);
  1660. if (!itemdb_exists(item_id)) {
  1661. ShowWarning("itemdb_parse_roulette_db: Unknown item ID '%u' in level '%d'\n", item_id, level);
  1662. continue;
  1663. }
  1664. if (amount < 1 || amount > MAX_AMOUNT){
  1665. ShowWarning("itemdb_parse_roulette_db: Unsupported amount '%hu' for item ID '%u' in level '%d'\n", amount, item_id, level);
  1666. continue;
  1667. }
  1668. if (flag < 0 || flag > 1) {
  1669. ShowWarning("itemdb_parse_roulette_db: Unsupported flag '%d' for item ID '%u' in level '%d'\n", flag, item_id, level);
  1670. continue;
  1671. }
  1672. j = rd.items[i];
  1673. RECREATE(rd.nameid[i], t_itemid, ++rd.items[i]);
  1674. RECREATE(rd.qty[i], unsigned short, rd.items[i]);
  1675. RECREATE(rd.flag[i], int, rd.items[i]);
  1676. rd.nameid[i][j] = item_id;
  1677. rd.qty[i][j] = amount;
  1678. rd.flag[i][j] = flag;
  1679. ++count;
  1680. }
  1681. }
  1682. // free the query result
  1683. Sql_FreeResult(mmysql_handle);
  1684. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  1685. int limit = MAX_ROULETTE_COLUMNS - i;
  1686. if (rd.items[i] == limit)
  1687. continue;
  1688. if (rd.items[i] > limit) {
  1689. ShowWarning("itemdb_parse_roulette_db: level %d has %d items, only %d supported, capping...\n", i + 1, rd.items[i], limit);
  1690. rd.items[i] = limit;
  1691. continue;
  1692. }
  1693. /** this scenario = rd.items[i] < limit **/
  1694. ShowWarning("itemdb_parse_roulette_db: Level %d has %d items, %d are required. Filling with Apples...\n", i + 1, rd.items[i], limit);
  1695. rd.items[i] = limit;
  1696. RECREATE(rd.nameid[i], t_itemid, rd.items[i]);
  1697. RECREATE(rd.qty[i], unsigned short, rd.items[i]);
  1698. RECREATE(rd.flag[i], int, rd.items[i]);
  1699. for (j = 0; j < MAX_ROULETTE_COLUMNS - i; j++) {
  1700. if (rd.qty[i][j])
  1701. continue;
  1702. rd.nameid[i][j] = ITEMID_APPLE;
  1703. rd.qty[i][j] = 1;
  1704. rd.flag[i][j] = 0;
  1705. }
  1706. }
  1707. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, roulette_table);
  1708. return true;
  1709. }
  1710. /**
  1711. * Free Roulette items
  1712. */
  1713. static void itemdb_roulette_free(void) {
  1714. int i;
  1715. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  1716. if (rd.nameid[i])
  1717. aFree(rd.nameid[i]);
  1718. if (rd.qty[i])
  1719. aFree(rd.qty[i]);
  1720. if (rd.flag[i])
  1721. aFree(rd.flag[i]);
  1722. rd.nameid[i] = NULL;
  1723. rd.qty[i] = NULL;
  1724. rd.flag[i] = NULL;
  1725. rd.items[i] = 0;
  1726. }
  1727. }
  1728. /**
  1729. * Convert SQL data to YAML Node
  1730. * @param str: Array of parsed SQL data
  1731. * @return True on success or false otherwise
  1732. */
  1733. static bool itemdb_read_sqldb_sub(std::vector<std::string> str) {
  1734. YAML::Node node;
  1735. int32 index = -1;
  1736. node["Id"] = std::stoul(str[++index], nullptr, 10);
  1737. if (!str[++index].empty())
  1738. node["AegisName"] = str[index];
  1739. if (!str[++index].empty())
  1740. node["Name"] = str[index];
  1741. if (!str[++index].empty())
  1742. node["Type"] = str[index];
  1743. if (!str[++index].empty())
  1744. node["SubType"] = str[index];
  1745. if (!str[++index].empty())
  1746. node["Buy"] = std::stoi(str[index]);
  1747. if (!str[++index].empty())
  1748. node["Sell"] = std::stoi(str[index]);
  1749. if (!str[++index].empty())
  1750. node["Weight"] = std::stoi(str[index]);
  1751. if (!str[++index].empty())
  1752. node["Attack"] = std::stoi(str[index]);
  1753. if (!str[++index].empty())
  1754. node["Defense"] = std::stoi(str[index]);
  1755. if (!str[++index].empty())
  1756. node["Range"] = std::stoi(str[index]);
  1757. if (!str[++index].empty())
  1758. node["Slots"] = std::stoi(str[index]);
  1759. YAML::Node jobs;
  1760. if (!str[++index].empty())
  1761. jobs["All"] = std::stoi(str[index]) ? "true" : "false";
  1762. if (!str[++index].empty())
  1763. jobs["Acolyte"] = std::stoi(str[index]) ? "true" : "false";
  1764. if (!str[++index].empty())
  1765. jobs["Alchemist"] = std::stoi(str[index]) ? "true" : "false";
  1766. if (!str[++index].empty())
  1767. jobs["Archer"] = std::stoi(str[index]) ? "true" : "false";
  1768. if (!str[++index].empty())
  1769. jobs["Assassin"] = std::stoi(str[index]) ? "true" : "false";
  1770. if (!str[++index].empty())
  1771. jobs["BardDancer"] = std::stoi(str[index]) ? "true" : "false";
  1772. if (!str[++index].empty())
  1773. jobs["Blacksmith"] = std::stoi(str[index]) ? "true" : "false";
  1774. if (!str[++index].empty())
  1775. jobs["Crusader"] = std::stoi(str[index]) ? "true" : "false";
  1776. if (!str[++index].empty())
  1777. jobs["Gunslinger"] = std::stoi(str[index]) ? "true" : "false";
  1778. if (!str[++index].empty())
  1779. jobs["Hunter"] = std::stoi(str[index]) ? "true" : "false";
  1780. if (!str[++index].empty())
  1781. jobs["Knight"] = std::stoi(str[index]) ? "true" : "false";
  1782. if (!str[++index].empty())
  1783. jobs["Mage"] = std::stoi(str[index]) ? "true" : "false";
  1784. if (!str[++index].empty())
  1785. jobs["Merchant"] = std::stoi(str[index]) ? "true" : "false";
  1786. if (!str[++index].empty())
  1787. jobs["Monk"] = std::stoi(str[index]) ? "true" : "false";
  1788. if (!str[++index].empty())
  1789. jobs["Ninja"] = std::stoi(str[index]) ? "true" : "false";
  1790. if (!str[++index].empty())
  1791. jobs["Novice"] = std::stoi(str[index]) ? "true" : "false";
  1792. if (!str[++index].empty())
  1793. jobs["Priest"] = std::stoi(str[index]) ? "true" : "false";
  1794. if (!str[++index].empty())
  1795. jobs["Rogue"] = std::stoi(str[index]) ? "true" : "false";
  1796. if (!str[++index].empty())
  1797. jobs["Sage"] = std::stoi(str[index]) ? "true" : "false";
  1798. if (!str[++index].empty())
  1799. jobs["SoulLinker"] = std::stoi(str[index]) ? "true" : "false";
  1800. if (!str[++index].empty())
  1801. jobs["StarGladiator"] = std::stoi(str[index]) ? "true" : "false";
  1802. if (!str[++index].empty())
  1803. jobs["SuperNovice"] = std::stoi(str[index]) ? "true" : "false";
  1804. if (!str[++index].empty())
  1805. jobs["Swordman"] = std::stoi(str[index]) ? "true" : "false";
  1806. if (!str[++index].empty())
  1807. jobs["Taekwon"] = std::stoi(str[index]) ? "true" : "false";
  1808. if (!str[++index].empty())
  1809. jobs["Thief"] = std::stoi(str[index]) ? "true" : "false";
  1810. if (!str[++index].empty())
  1811. jobs["Wizard"] = std::stoi(str[index]) ? "true" : "false";
  1812. YAML::Node classes;
  1813. if (!str[++index].empty())
  1814. classes["All"] = std::stoi(str[index]) ? "true" : "false";
  1815. if (!str[++index].empty())
  1816. classes["Normal"] = std::stoi(str[index]) ? "true" : "false";
  1817. if (!str[++index].empty())
  1818. classes["Upper"] = std::stoi(str[index]) ? "true" : "false";
  1819. if (!str[++index].empty())
  1820. classes["Baby"] = std::stoi(str[index]) ? "true" : "false";
  1821. if (!str[++index].empty())
  1822. node["Gender"] = str[index];
  1823. YAML::Node locations;
  1824. if (!str[++index].empty())
  1825. locations["Head_Top"] = std::stoi(str[index]) ? "true" : "false";
  1826. if (!str[++index].empty())
  1827. locations["Head_Mid"] = std::stoi(str[index]) ? "true" : "false";
  1828. if (!str[++index].empty())
  1829. locations["Head_Low"] = std::stoi(str[index]) ? "true" : "false";
  1830. if (!str[++index].empty())
  1831. locations["Armor"] = std::stoi(str[index]) ? "true" : "false";
  1832. if (!str[++index].empty())
  1833. locations["Right_Hand"] = std::stoi(str[index]) ? "true" : "false";
  1834. if (!str[++index].empty())
  1835. locations["Left_Hand"] = std::stoi(str[index]) ? "true" : "false";
  1836. if (!str[++index].empty())
  1837. locations["Garment"] = std::stoi(str[index]) ? "true" : "false";
  1838. if (!str[++index].empty())
  1839. locations["Shoes"] = std::stoi(str[index]) ? "true" : "false";
  1840. if (!str[++index].empty())
  1841. locations["Right_Accessory"] = std::stoi(str[index]) ? "true" : "false";
  1842. if (!str[++index].empty())
  1843. locations["Left_Accessory"] = std::stoi(str[index]) ? "true" : "false";
  1844. if (!str[++index].empty())
  1845. locations["Costume_Head_Top"] = std::stoi(str[index]) ? "true" : "false";
  1846. if (!str[++index].empty())
  1847. locations["Costume_Head_Mid"] = std::stoi(str[index]) ? "true" : "false";
  1848. if (!str[++index].empty())
  1849. locations["Costume_Head_Low"] = std::stoi(str[index]) ? "true" : "false";
  1850. if (!str[++index].empty())
  1851. locations["Costume_Garment"] = std::stoi(str[index]) ? "true" : "false";
  1852. if (!str[++index].empty())
  1853. locations["Ammo"] = std::stoi(str[index]) ? "true" : "false";
  1854. if (!str[++index].empty())
  1855. locations["Shadow_Armor"] = std::stoi(str[index]) ? "true" : "false";
  1856. if (!str[++index].empty())
  1857. locations["Shadow_Weapon"] = std::stoi(str[index]) ? "true" : "false";
  1858. if (!str[++index].empty())
  1859. locations["Shadow_Shield"] = std::stoi(str[index]) ? "true" : "false";
  1860. if (!str[++index].empty())
  1861. locations["Shadow_Shoes"] = std::stoi(str[index]) ? "true" : "false";
  1862. if (!str[++index].empty())
  1863. locations["Shadow_Right_Accessory"] = std::stoi(str[index]) ? "true" : "false";
  1864. if (!str[++index].empty())
  1865. locations["Shadow_Left_Accessory"] = std::stoi(str[index]) ? "true" : "false";
  1866. node["Locations"] = locations;
  1867. if (!str[++index].empty())
  1868. node["WeaponLevel"] = std::stoi(str[index]);
  1869. if (!str[++index].empty())
  1870. node["ArmorLevel"] = std::stoi(str[index]);
  1871. if (!str[++index].empty())
  1872. node["EquipLevelMin"] = std::stoi(str[index]);
  1873. if (!str[++index].empty())
  1874. node["EquipLevelMax"] = std::stoi(str[index]);
  1875. if (!str[++index].empty())
  1876. node["Refineable"] = std::stoi(str[index]) ? "true" : "false";
  1877. if (!str[++index].empty())
  1878. node["View"] = std::stoi(str[index]);
  1879. if (!str[++index].empty())
  1880. node["AliasName"] = str[index];
  1881. YAML::Node flags;
  1882. if (!str[++index].empty())
  1883. flags["BuyingStore"] = std::stoi(str[index]) ? "true" : "false";
  1884. if (!str[++index].empty())
  1885. flags["DeadBranch"] = std::stoi(str[index]) ? "true" : "false";
  1886. if (!str[++index].empty())
  1887. flags["Container"] = std::stoi(str[index]) ? "true" : "false";
  1888. if (!str[++index].empty())
  1889. flags["UniqueId"] = std::stoi(str[index]) ? "true" : "false";
  1890. if (!str[++index].empty())
  1891. flags["BindOnEquip"] = std::stoi(str[index]) ? "true" : "false";
  1892. if (!str[++index].empty())
  1893. flags["DropAnnounce"] = std::stoi(str[index]) ? "true" : "false";
  1894. if (!str[++index].empty())
  1895. flags["NoConsume"] = std::stoi(str[index]) ? "true" : "false";
  1896. if (!str[++index].empty())
  1897. flags["DropEffect"] = str[index];
  1898. node["Flags"] = flags;
  1899. YAML::Node delay;
  1900. if (!str[++index].empty())
  1901. delay["Duration"] = std::stoi(str[index]);
  1902. if (!str[++index].empty())
  1903. delay["Status"] = str[index];
  1904. node["Delay"] = delay;
  1905. YAML::Node stack;
  1906. if (!str[++index].empty())
  1907. stack["Amount"] = std::stoi(str[index]);
  1908. if (!str[++index].empty())
  1909. stack["Inventory"] = std::stoi(str[index]) ? "true" : "false";
  1910. if (!str[++index].empty())
  1911. stack["Cart"] = std::stoi(str[index]) ? "true" : "false";
  1912. if (!str[++index].empty())
  1913. stack["Storage"] = std::stoi(str[index]) ? "true" : "false";
  1914. if (!str[++index].empty())
  1915. stack["GuildStorage"] = std::stoi(str[index]) ? "true" : "false";
  1916. node["Stack"] = stack;
  1917. YAML::Node nouse;
  1918. if (!str[++index].empty())
  1919. nouse["Override"] = std::stoi(str[index]);
  1920. if (!str[++index].empty())
  1921. nouse["Sitting"] = std::stoi(str[index]) ? "true" : "false";
  1922. node["NoUse"] = nouse;
  1923. YAML::Node trade;
  1924. if (!str[++index].empty())
  1925. trade["Override"] = std::stoi(str[index]);
  1926. if (!str[++index].empty())
  1927. trade["NoDrop"] = std::stoi(str[index]) ? "true" : "false";
  1928. if (!str[++index].empty())
  1929. trade["NoTrade"] = std::stoi(str[index]) ? "true" : "false";
  1930. if (!str[++index].empty())
  1931. trade["TradePartner"] = std::stoi(str[index]) ? "true" : "false";
  1932. if (!str[++index].empty())
  1933. trade["NoSell"] = std::stoi(str[index]) ? "true" : "false";
  1934. if (!str[++index].empty())
  1935. trade["NoCart"] = std::stoi(str[index]) ? "true" : "false";
  1936. if (!str[++index].empty())
  1937. trade["NoStorage"] = std::stoi(str[index]) ? "true" : "false";
  1938. if (!str[++index].empty())
  1939. trade["NoGuildStorage"] = std::stoi(str[index]) ? "true" : "false";
  1940. if (!str[++index].empty())
  1941. trade["NoMail"] = std::stoi(str[index]) ? "true" : "false";
  1942. if (!str[++index].empty())
  1943. trade["NoAuction"] = std::stoi(str[index]) ? "true" : "false";
  1944. node["Trade"] = trade;
  1945. if (!str[++index].empty())
  1946. node["Script"] = str[index];
  1947. if (!str[++index].empty())
  1948. node["EquipScript"] = str[index];
  1949. if (!str[++index].empty())
  1950. node["UnEquipScript"] = str[index];
  1951. #ifdef RENEWAL
  1952. if (!str[++index].empty())
  1953. node["MagicAttack"] = std::stoi(str[index]);
  1954. if (!str[++index].empty())
  1955. classes["Third"] = std::stoi(str[index]) ? "true" : "false";
  1956. if (!str[++index].empty())
  1957. classes["Third_Upper"] = std::stoi(str[index]) ? "true" : "false";
  1958. if (!str[++index].empty())
  1959. classes["Third_Baby"] = std::stoi(str[index]) ? "true" : "false";
  1960. if (!str[++index].empty())
  1961. classes["Fourth"] = std::stoi(str[index]) ? "true" : "false";
  1962. if (!str[++index].empty())
  1963. jobs["KagerouOboro"] = std::stoi(str[index]) ? "true" : "false";
  1964. if (!str[++index].empty())
  1965. jobs["Rebellion"] = std::stoi(str[index]) ? "true" : "false";
  1966. if (!str[++index].empty())
  1967. jobs["Summoner"] = std::stoi(str[index]) ? "true" : "false";
  1968. #endif
  1969. node["Classes"] = classes;
  1970. node["Jobs"] = jobs;
  1971. return item_db.parseBodyNode(node) > 0;
  1972. }
  1973. /**
  1974. * Read SQL item_db table
  1975. */
  1976. static int itemdb_read_sqldb(void) {
  1977. const char* item_db_name[] = {
  1978. item_table,
  1979. item2_table
  1980. };
  1981. for( uint8 fi = 0; fi < ARRAYLENGTH(item_db_name); ++fi ) {
  1982. // retrieve all rows from the item database
  1983. if( SQL_ERROR == Sql_Query(mmysql_handle, "SELECT `id`,`name_aegis`,`name_english`,`type`,`subtype`,`price_buy`,`price_sell`,`weight`,`attack`,`defense`,`range`,`slots`,"
  1984. "`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`,"
  1985. "`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`,"
  1986. "`class_all`,`class_normal`,`class_upper`,`class_baby`,`gender`,"
  1987. "`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`,"
  1988. "`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`,"
  1989. "`weapon_level`,`armor_level`,`equip_level_min`,`equip_level_max`,`refineable`,`view`,`alias_name`,"
  1990. "`flag_buyingstore`,`flag_deadbranch`,`flag_container`,`flag_uniqueid`,`flag_bindonequip`,`flag_dropannounce`,`flag_noconsume`,`flag_dropeffect`,"
  1991. "`delay_duration`,`delay_status`,`stack_amount`,`stack_inventory`,`stack_cart`,`stack_storage`,`stack_guildstorage`,`nouse_override`,`nouse_sitting`,"
  1992. "`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`"
  1993. #ifdef RENEWAL
  1994. ",`magic_attack`,`class_third`,`class_third_upper`,`class_third_baby`,`class_fourth`,`job_kagerouoboro`,`job_rebellion`,`job_summoner`"
  1995. #endif
  1996. " FROM `%s`", item_db_name[fi]) ) {
  1997. Sql_ShowDebug(mmysql_handle);
  1998. continue;
  1999. }
  2000. uint32 total_columns = Sql_NumColumns(mmysql_handle);
  2001. uint64 total_rows = Sql_NumRows(mmysql_handle), rows = 0, count = 0;
  2002. // process rows one by one
  2003. while( SQL_SUCCESS == Sql_NextRow(mmysql_handle) ) {
  2004. ShowStatus( "Loading [%" PRIu64 "/%" PRIu64 "] rows from '" CL_WHITE "%s" CL_RESET "'" CL_CLL "\r", ++rows, total_rows, item_db_name[fi] );
  2005. std::vector<std::string> data = {};
  2006. for( uint32 i = 0; i < total_columns; ++i ) {
  2007. char *str;
  2008. Sql_GetData(mmysql_handle, i, &str, nullptr);
  2009. if (str == nullptr)
  2010. data.push_back("");
  2011. else
  2012. data.push_back(str);
  2013. }
  2014. if (!itemdb_read_sqldb_sub(data))
  2015. continue;
  2016. ++count;
  2017. }
  2018. // free the query result
  2019. Sql_FreeResult(mmysql_handle);
  2020. ShowStatus("Done reading '" CL_WHITE "%" PRIu64 CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, item_db_name[fi]);
  2021. }
  2022. item_db.loadingFinished();
  2023. return 0;
  2024. }
  2025. /** Check if the item is restricted by item_noequip.txt
  2026. * @param id Item that will be checked
  2027. * @param m Map ID
  2028. * @return true: can't be used; false: can be used
  2029. */
  2030. bool itemdb_isNoEquip(struct item_data *id, uint16 m) {
  2031. if (!id->flag.no_equip)
  2032. return false;
  2033. struct map_data *mapdata = map_getmapdata(m);
  2034. if ((id->flag.no_equip&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  2035. (id->flag.no_equip&2 && mapdata->flag[MF_PVP]) || // PVP
  2036. (id->flag.no_equip&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  2037. (id->flag.no_equip&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  2038. (id->flag.no_equip&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  2039. (id->flag.no_equip&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  2040. )
  2041. return true;
  2042. return false;
  2043. }
  2044. const std::string RandomOptionDatabase::getDefaultLocation() {
  2045. return std::string(db_path) + "/item_randomopt_db.yml";
  2046. }
  2047. /**
  2048. * Reads and parses an entry from the item_randomopt_db.
  2049. * @param node: YAML node containing the entry.
  2050. * @return count of successfully parsed rows
  2051. */
  2052. uint64 RandomOptionDatabase::parseBodyNode(const YAML::Node &node) {
  2053. uint16 id;
  2054. if (!this->asUInt16(node, "Id", id))
  2055. return 0;
  2056. std::shared_ptr<s_random_opt_data> randopt = this->find(id);
  2057. bool exists = randopt != nullptr;
  2058. if (!exists) {
  2059. if (!this->nodesExist(node, { "Option", "Script" }))
  2060. return 0;
  2061. randopt = std::make_shared<s_random_opt_data>();
  2062. randopt->id = id;
  2063. }
  2064. if (this->nodeExists(node, "Option")) {
  2065. std::string name;
  2066. if (!this->asString(node, "Option", name))
  2067. return 0;
  2068. if (randopt->name.compare(name) != 0 && random_option_db.option_exists(name)) {
  2069. this->invalidWarning(node["Option"], "Found duplicate random option name for %s, skipping.\n", name.c_str());
  2070. return 0;
  2071. }
  2072. randopt->name = name;
  2073. }
  2074. if (this->nodeExists(node, "Script")) {
  2075. std::string script;
  2076. if (!this->asString(node, "Script", script))
  2077. return 0;
  2078. if (randopt->script) {
  2079. aFree(randopt->script);
  2080. randopt->script = nullptr;
  2081. }
  2082. randopt->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), id, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  2083. }
  2084. if (!exists)
  2085. this->put(id, randopt);
  2086. return 1;
  2087. }
  2088. void RandomOptionDatabase::loadingFinished(){
  2089. const char* prefix = "RDMOPT_";
  2090. for( const auto& pair : *this ){
  2091. std::string name = prefix + pair.second->name;
  2092. int64 constant;
  2093. // Check if it has already been set
  2094. if( script_get_constant( name.c_str(), &constant ) ){
  2095. // It is already the same
  2096. if( constant == pair.first ){
  2097. continue;
  2098. }else{
  2099. // Export it to the script engine -> will issue a warning
  2100. }
  2101. }
  2102. script_set_constant( name.c_str(), pair.first, false, false );
  2103. }
  2104. }
  2105. RandomOptionDatabase random_option_db;
  2106. /**
  2107. * Check if the given random option name exists.
  2108. * @param name: Random option name
  2109. * @return True on success or false on failure
  2110. */
  2111. bool RandomOptionDatabase::option_exists(std::string name) {
  2112. for (const auto &opt : random_option_db) {
  2113. if (opt.second->name.compare(name) == 0)
  2114. return true;
  2115. }
  2116. return false;
  2117. }
  2118. /**
  2119. * Return the constant value of the given random option.
  2120. * @param name: Random option name
  2121. * @param id: Random option ID
  2122. * @return True on success or false on failure
  2123. */
  2124. bool RandomOptionDatabase::option_get_id(std::string name, uint16 &id) {
  2125. for (const auto &opt : random_option_db) {
  2126. if (opt.second->name.compare(name) == 0) {
  2127. id = opt.first;
  2128. return true;
  2129. }
  2130. }
  2131. return false;
  2132. }
  2133. const std::string RandomOptionGroupDatabase::getDefaultLocation() {
  2134. return std::string(db_path) + "/item_randomopt_group.yml";
  2135. }
  2136. bool RandomOptionGroupDatabase::add_option(const YAML::Node &node, std::shared_ptr<s_random_opt_group_entry> &entry) {
  2137. uint16 option_id;
  2138. if (this->nodeExists(node, "Option")) {
  2139. std::string opt_name;
  2140. if (!this->asString(node, "Option", opt_name))
  2141. return false;
  2142. if (!random_option_db.option_get_id(opt_name, option_id)) {
  2143. this->invalidWarning(node["Option"], "Invalid Random Option name %s given.\n", opt_name.c_str());
  2144. return false;
  2145. }
  2146. } else {
  2147. this->invalidWarning(node, "Missing Option node.\n");
  2148. return false;
  2149. }
  2150. entry = std::make_shared<s_random_opt_group_entry>();
  2151. entry->id = option_id;
  2152. if (this->nodeExists(node, "MinValue")) {
  2153. int16 value;
  2154. if (!this->asInt16(node, "MinValue", value))
  2155. return false;
  2156. entry->min_value = value;
  2157. } else {
  2158. entry->min_value = 0;
  2159. }
  2160. if (this->nodeExists(node, "MaxValue")) {
  2161. int16 value;
  2162. if (!this->asInt16(node, "MaxValue", value))
  2163. return false;
  2164. entry->max_value = value;
  2165. } else {
  2166. entry->max_value = 0;
  2167. }
  2168. if (entry->min_value > entry->max_value) {
  2169. this->invalidWarning(node["MaxValue"], "MinValue %d is greater than MaxValue %d, setting MaxValue to MinValue + 1.\n", entry->min_value, entry->max_value);
  2170. entry->max_value = entry->min_value + 1;
  2171. }
  2172. if (this->nodeExists(node, "Param")) {
  2173. int16 value;
  2174. if (!this->asInt16(node, "Param", value))
  2175. return false;
  2176. entry->param = static_cast<int8>(value);
  2177. } else {
  2178. entry->param = 0;
  2179. }
  2180. if (this->nodeExists(node, "Chance")) {
  2181. uint16 chance;
  2182. if (!this->asUInt16Rate(node, "Chance", chance))
  2183. return false;
  2184. entry->chance = chance;
  2185. } else {
  2186. entry->chance = 0;
  2187. }
  2188. return true;
  2189. }
  2190. /**
  2191. * Reads and parses an entry from the item_randomopt_group.
  2192. * @param node: YAML node containing the entry.
  2193. * @return count of successfully parsed rows
  2194. */
  2195. uint64 RandomOptionGroupDatabase::parseBodyNode(const YAML::Node &node) {
  2196. uint16 id;
  2197. if (!this->asUInt16(node, "Id", id))
  2198. return 0;
  2199. std::shared_ptr<s_random_opt_group> randopt = this->find(id);
  2200. bool exists = randopt != nullptr;
  2201. if (!exists) {
  2202. if (!this->nodesExist(node, { "Group" }))
  2203. return 0;
  2204. randopt = std::make_shared<s_random_opt_group>();
  2205. randopt->id = id;
  2206. }
  2207. if (this->nodeExists(node, "Group")) {
  2208. std::string name;
  2209. if (!this->asString(node, "Group", name))
  2210. return 0;
  2211. if (randopt->name.compare(name) != 0 && random_option_group.option_exists(name)) {
  2212. this->invalidWarning(node["Group"], "Found duplicate random option group name for %s, skipping.\n", name.c_str());
  2213. return 0;
  2214. }
  2215. randopt->name = name;
  2216. }
  2217. for (const YAML::Node &slotNode : node["Slots"]) {
  2218. if (randopt->slots.size() >= MAX_ITEM_RDM_OPT) {
  2219. this->invalidWarning(slotNode, "Reached maximum of %d Random Option group options. Skipping the remaining slots...\n", MAX_ITEM_RDM_OPT);
  2220. break;
  2221. }
  2222. uint16 slot;
  2223. if (!this->asUInt16(slotNode, "Slot", slot))
  2224. return 0;
  2225. if (slot < 1 || slot > MAX_ITEM_RDM_OPT) {
  2226. this->invalidWarning(slotNode["Slot"], "Invalid Random Opton Slot number %hu given, must be between 1~%d, skipping.\n", slot, MAX_ITEM_RDM_OPT);
  2227. return 0;
  2228. }
  2229. std::vector<std::shared_ptr<s_random_opt_group_entry>> entries;
  2230. for (const YAML::Node &optionNode : slotNode["Options"]) {
  2231. std::shared_ptr<s_random_opt_group_entry> entry;
  2232. if (!this->add_option(optionNode, entry))
  2233. return 0;
  2234. entries.push_back(entry);
  2235. }
  2236. randopt->slots[slot - 1] = entries;
  2237. }
  2238. if (this->nodeExists(node, "MaxRandom")) {
  2239. uint16 max;
  2240. if (!this->asUInt16(node, "MaxRandom", max))
  2241. return 0;
  2242. if (max > MAX_ITEM_RDM_OPT) {
  2243. this->invalidWarning(node["MaxRandom"], "Exceeds the maximum of %d Random Option group options, capping to MAX_ITEM_RDM_OPT.\n", MAX_ITEM_RDM_OPT);
  2244. max = MAX_ITEM_RDM_OPT;
  2245. }
  2246. randopt->max_random = max;
  2247. } else {
  2248. if (!exists)
  2249. randopt->max_random = 0;
  2250. }
  2251. if (this->nodeExists(node, "Random")) {
  2252. randopt->random_options.clear();
  2253. for (const YAML::Node &randomNode : node["Random"]) {
  2254. std::shared_ptr<s_random_opt_group_entry> entry;
  2255. if (!this->add_option(randomNode, entry))
  2256. return 0;
  2257. randopt->random_options.push_back(entry);
  2258. }
  2259. }
  2260. if (!exists)
  2261. this->put(id, randopt);
  2262. return 1;
  2263. }
  2264. RandomOptionGroupDatabase random_option_group;
  2265. /**
  2266. * Check if the given random option group name exists.
  2267. * @param name: Random option name
  2268. * @return True on success or false on failure
  2269. */
  2270. bool RandomOptionGroupDatabase::option_exists(std::string name) {
  2271. for (const auto &opt : random_option_group) {
  2272. if (opt.second->name.compare(name) == 0)
  2273. return true;
  2274. }
  2275. return false;
  2276. }
  2277. /**
  2278. * Return the constant value of the given random option group.
  2279. * @param name: Random option group name
  2280. * @param id: Random option group ID
  2281. * @return True on success or false on failure
  2282. */
  2283. bool RandomOptionGroupDatabase::option_get_id(std::string name, uint16 &id) {
  2284. for (const auto &opt : random_option_group) {
  2285. if (opt.second->name.compare(name) == 0) {
  2286. id = opt.first;
  2287. return true;
  2288. }
  2289. }
  2290. return false;
  2291. }
  2292. /**
  2293. * Read all item-related databases
  2294. */
  2295. static void itemdb_read(void) {
  2296. int i;
  2297. const char* dbsubpath[] = {
  2298. "",
  2299. "/" DBIMPORT,
  2300. };
  2301. if (db_use_sqldbs)
  2302. itemdb_read_sqldb();
  2303. else
  2304. item_db.load();
  2305. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  2306. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  2307. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  2308. char* dbsubpath1 = (char*)aMalloc(n1+1);
  2309. char* dbsubpath2 = (char*)aMalloc(n2+1);
  2310. if(i==0) {
  2311. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  2312. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  2313. }
  2314. else {
  2315. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  2316. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  2317. }
  2318. sv_readdb(dbsubpath2, "item_noequip.txt", ',', 2, 2, -1, &itemdb_read_noequip, i > 0);
  2319. aFree(dbsubpath1);
  2320. aFree(dbsubpath2);
  2321. }
  2322. itemdb_group.load();
  2323. itemdb_combo.load();
  2324. random_option_db.load();
  2325. random_option_group.load();
  2326. }
  2327. /*==========================================
  2328. * Initialize / Finalize
  2329. *------------------------------------------*/
  2330. bool item_data::isStackable()
  2331. {
  2332. switch (this->type) {
  2333. case IT_WEAPON:
  2334. case IT_ARMOR:
  2335. case IT_PETEGG:
  2336. case IT_PETARMOR:
  2337. case IT_SHADOWGEAR:
  2338. return false;
  2339. }
  2340. return true;
  2341. }
  2342. int item_data::inventorySlotNeeded(int quantity)
  2343. {
  2344. return (this->flag.guid || !this->isStackable()) ? quantity : 1;
  2345. }
  2346. /**
  2347. * Reload Item DB
  2348. */
  2349. void itemdb_reload(void) {
  2350. struct s_mapiterator* iter;
  2351. struct map_session_data* sd;
  2352. item_db.clear();
  2353. itemdb_combo.clear();
  2354. itemdb_group.clear();
  2355. random_option_db.clear();
  2356. random_option_group.clear();
  2357. if (battle_config.feature_roulette)
  2358. itemdb_roulette_free();
  2359. // read new data
  2360. itemdb_read();
  2361. cashshop_reloaddb();
  2362. if (battle_config.feature_roulette)
  2363. itemdb_parse_roulette_db();
  2364. mob_reload_itemmob_data();
  2365. // readjust itemdb pointer cache for each player
  2366. iter = mapit_geteachpc();
  2367. for( sd = (struct map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (struct map_session_data*)mapit_next(iter) ) {
  2368. memset(sd->item_delay, 0, sizeof(sd->item_delay)); // reset item delays
  2369. sd->combos.clear(); // clear combo bonuses
  2370. pc_setinventorydata(sd);
  2371. pc_check_available_item(sd, ITMCHK_ALL); // Check for invalid(ated) items.
  2372. pc_load_combo(sd); // Check to see if new combos are available
  2373. status_calc_pc(sd, SCO_FORCE); //
  2374. }
  2375. mapit_free(iter);
  2376. }
  2377. /**
  2378. * Finalizing Item DB
  2379. */
  2380. void do_final_itemdb(void) {
  2381. item_db.clear();
  2382. itemdb_combo.clear();
  2383. itemdb_group.clear();
  2384. random_option_db.clear();
  2385. random_option_group.clear();
  2386. if (battle_config.feature_roulette)
  2387. itemdb_roulette_free();
  2388. }
  2389. /**
  2390. * Initializing Item DB
  2391. */
  2392. void do_init_itemdb(void) {
  2393. itemdb_read();
  2394. if (battle_config.feature_roulette)
  2395. itemdb_parse_roulette_db();
  2396. }