itemdb.cpp 72 KB

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