itemdb.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/nullpo.h"
  4. #include "../common/malloc.h"
  5. #include "../common/random.h"
  6. #include "../common/showmsg.h"
  7. #include "../common/strlib.h"
  8. #include "../common/utils.h"
  9. #include "itemdb.h"
  10. #include "map.h"
  11. #include "battle.h" // struct battle_config
  12. #include "cashshop.h"
  13. #include "script.h" // item script processing
  14. #include "pc.h" // W_MUSICAL, W_WHIP
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. static struct item_data* itemdb_array[MAX_ITEMDB];
  19. static DBMap* itemdb_other;// int nameid -> struct item_data*
  20. static struct s_item_group_db itemgroup_db[MAX_ITEMGROUP];
  21. struct item_data dummy_item; //This is the default dummy item used for non-existant items. [Skotlex]
  22. static DBMap *itemdb_combo;
  23. DBMap * itemdb_get_combodb(){
  24. return itemdb_combo;
  25. }
  26. /**
  27. * Search for item name
  28. * name = item alias, so we should find items aliases first. if not found then look for "jname" (full name)
  29. * @see DBApply
  30. */
  31. static int itemdb_searchname_sub(DBKey key, DBData *data, va_list ap)
  32. {
  33. struct item_data *item = db_data2ptr(data), **dst, **dst2;
  34. char *str;
  35. str=va_arg(ap,char *);
  36. dst=va_arg(ap,struct item_data **);
  37. dst2=va_arg(ap,struct item_data **);
  38. if(item == &dummy_item) return 0;
  39. //Absolute priority to Aegis code name.
  40. if (*dst != NULL) return 0;
  41. if( strcmpi(item->name,str)==0 )
  42. *dst=item;
  43. //Second priority to Client displayed name.
  44. if (*dst2 != NULL) return 0;
  45. if( strcmpi(item->jname,str)==0 )
  46. *dst2=item;
  47. return 0;
  48. }
  49. /*==========================================
  50. * Return item data from item name. (lookup)
  51. *------------------------------------------*/
  52. struct item_data* itemdb_searchname(const char *str)
  53. {
  54. struct item_data* item;
  55. struct item_data* item2=NULL;
  56. int i;
  57. for( i = 0; i < ARRAYLENGTH(itemdb_array); ++i )
  58. {
  59. item = itemdb_array[i];
  60. if( item == NULL )
  61. continue;
  62. // Absolute priority to Aegis code name.
  63. if( strcasecmp(item->name,str) == 0 )
  64. return item;
  65. //Second priority to Client displayed name.
  66. if( strcasecmp(item->jname,str) == 0 )
  67. item2 = item;
  68. }
  69. item = NULL;
  70. itemdb_other->foreach(itemdb_other,itemdb_searchname_sub,str,&item,&item2);
  71. return item?item:item2;
  72. }
  73. /**
  74. * @see DBMatcher
  75. */
  76. static int itemdb_searchname_array_sub(DBKey key, DBData data, va_list ap)
  77. {
  78. struct item_data *item = db_data2ptr(&data);
  79. char *str;
  80. str=va_arg(ap,char *);
  81. if (item == &dummy_item)
  82. return 1; //Invalid item.
  83. if(stristr(item->jname,str))
  84. return 0;
  85. if(stristr(item->name,str))
  86. return 0;
  87. return strcmpi(item->jname,str);
  88. }
  89. /*==========================================
  90. * Founds up to N matches. Returns number of matches [Skotlex]
  91. *------------------------------------------*/
  92. int itemdb_searchname_array(struct item_data** data, int size, const char *str)
  93. {
  94. struct item_data* item;
  95. int i;
  96. int count=0;
  97. // Search in the array
  98. for( i = 0; i < ARRAYLENGTH(itemdb_array); ++i )
  99. {
  100. item = itemdb_array[i];
  101. if( item == NULL )
  102. continue;
  103. if( stristr(item->jname,str) || stristr(item->name,str) )
  104. {
  105. if( count < size )
  106. data[count] = item;
  107. ++count;
  108. }
  109. }
  110. // search in the db
  111. if( count < size )
  112. {
  113. DBData *db_data[MAX_SEARCH];
  114. int db_count = 0;
  115. size -= count;
  116. db_count = itemdb_other->getall(itemdb_other, (DBData**)&db_data, size, itemdb_searchname_array_sub, str);
  117. for (i = 0; i < db_count; i++)
  118. data[count++] = db_data2ptr(db_data[i]);
  119. count += db_count;
  120. }
  121. return count;
  122. }
  123. /**
  124. * Return a random item id from group. (takes into account % chance giving/tot group)
  125. * NOTE: Sub group 0 will be set to default 1, since 0 isn't random group
  126. * @param group_id
  127. * @param sub_group: Default is 1
  128. * @return nameid
  129. */
  130. unsigned short itemdb_searchrandomid(uint16 group_id, uint8 sub_group)
  131. {
  132. if (sub_group)
  133. sub_group -= 1;
  134. if (!group_id || group_id >= MAX_ITEMGROUP || !&itemgroup_db[group_id]) {
  135. ShowError("itemdb_searchrandomid: Invalid group id %d\n", group_id);
  136. return UNKNOWN_ITEM_ID;
  137. }
  138. if (sub_group > MAX_ITEMGROUP_RANDGROUP) {
  139. ShowError("itemdb_searchrandomid: Invalid sub_group %d\n", sub_group+1);
  140. return UNKNOWN_ITEM_ID;
  141. }
  142. if (&itemgroup_db[group_id].random[sub_group] && itemgroup_db[group_id].random[sub_group].data_qty)
  143. return itemgroup_db[group_id].random[sub_group].data[rand()%itemgroup_db[group_id].random[sub_group].data_qty].nameid;
  144. ShowError("itemdb_searchrandomid: No item entries for group id %d and sub group %d\n", group_id, sub_group+1);
  145. return UNKNOWN_ITEM_ID;
  146. }
  147. /** [Cydh]
  148. * Return a number of item's amount that will be obtained for 'getrandgroupitem id,1;'
  149. * NOTE: Sub group 0 will be set to default 1, since 0 isn't random group
  150. * @param group_id
  151. * @param sub_group
  152. * @param nameid: The target item will be found
  153. * @return amount
  154. */
  155. uint16 itemdb_get_randgroupitem_count(uint16 group_id, uint8 sub_group, uint16 nameid) {
  156. uint16 i, amt = 1;
  157. if (sub_group)
  158. sub_group -= 1;
  159. if (!group_id || group_id >= MAX_ITEMGROUP || !&itemgroup_db[group_id]) {
  160. ShowError("itemdb_get_randgroupitem_count: Invalid group id %d\n", group_id);
  161. return amt;
  162. }
  163. if (sub_group > MAX_ITEMGROUP_RANDGROUP) {
  164. ShowError("itemdb_get_randgroupitem_count: Invalid sub_group id %d\n", group_id+1);
  165. return amt;
  166. }
  167. if (!(&itemgroup_db[group_id].random[sub_group]) || !itemgroup_db[group_id].random[sub_group].data_qty)
  168. return amt;
  169. ARR_FIND(0,itemgroup_db[group_id].random[sub_group].data_qty,i,itemgroup_db[group_id].random[sub_group].data[i].nameid == nameid);
  170. if (i < itemgroup_db[group_id].random[sub_group].data_qty)
  171. amt = itemgroup_db[group_id].random[sub_group].data[i].amount;
  172. return amt;
  173. }
  174. /** [Cydh]
  175. * Gives item(s) to the player based on item group
  176. * @param sd: Player that obtains item from item group
  177. * @param group_id: The group ID of item that obtained by player
  178. * @param *group: struct s_item_group from itemgroup_db[group_id].random[idx] or itemgroup_db[group_id].must[sub_group][idx]
  179. */
  180. static void itemdb_pc_get_itemgroup_sub(struct map_session_data *sd, uint16 group_id, struct s_item_group *group) {
  181. uint16 i;
  182. struct item tmp;
  183. nullpo_retv(group);
  184. memset(&tmp,0,sizeof(tmp));
  185. tmp.nameid = group->nameid;
  186. tmp.amount = (itemdb_isstackable(group->nameid)) ? group->amount : 1;
  187. tmp.bound = group->bound;
  188. tmp.identify = 1;
  189. tmp.expire_time = (group->duration) ? (unsigned int)(time(NULL) + group->duration*60) : 0;
  190. if (group->isNamed) {
  191. tmp.card[0] = itemdb_isequip(group->nameid) ? CARD0_FORGE : CARD0_CREATE;
  192. tmp.card[1] = 0;
  193. tmp.card[2] = GetWord(sd->status.char_id, 0);
  194. tmp.card[3] = GetWord(sd->status.char_id, 1);
  195. }
  196. //Do loop for non-stackable item
  197. for (i = 0; i < group->amount; i++) {
  198. int flag;
  199. if ((flag = pc_additem(sd,&tmp,tmp.amount,LOG_TYPE_SCRIPT)))
  200. clif_additem(sd,0,0,flag);
  201. else if (!flag && group->isAnnounced) { ///TODO: Move this broadcast to proper behavior (it should on at different packet)
  202. char output[CHAT_SIZE_MAX];
  203. sprintf(output,msg_txt(NULL,717),sd->status.name,itemdb_jname(group->nameid),itemdb_jname(sd->itemid));
  204. clif_broadcast(&sd->bl,output,strlen(output),0,ALL_CLIENT);
  205. //clif_broadcast_obtain_special_item();
  206. }
  207. if (itemdb_isstackable(group->nameid))
  208. break;
  209. }
  210. }
  211. /** [Cydh]
  212. * Find item(s) that will be obtained by player based on Item Group
  213. * @param group_id: The group ID that will be gained by player
  214. * @param nameid: The item that trigger this item group
  215. * @return val: 0:success, 1:no sd, 2:invalid item group
  216. */
  217. char itemdb_pc_get_itemgroup(uint16 group_id, struct map_session_data *sd) {
  218. uint16 i = 0;
  219. nullpo_retr(1,sd);
  220. if (!group_id || group_id >= MAX_ITEMGROUP) {
  221. ShowError("itemdb_pc_get_itemgroup: Invalid group id '%d' specified.",group_id);
  222. return 2;
  223. }
  224. //Get the 'must' item(s)
  225. if (itemgroup_db[group_id].must_qty)
  226. for (i = 0; i < itemgroup_db[group_id].must_qty; i++)
  227. if (&itemgroup_db[group_id].must[i] && itemdb_exists(itemgroup_db[group_id].must[i].nameid))
  228. itemdb_pc_get_itemgroup_sub(sd,group_id,&itemgroup_db[group_id].must[i]);
  229. //Get the 'random' item each random group
  230. for (i = 0; i < MAX_ITEMGROUP_RANDGROUP; i++) {
  231. uint16 rand;
  232. if (!(&itemgroup_db[group_id].random[i]) || !itemgroup_db[group_id].random[i].data_qty) //Skip empty random group
  233. continue;
  234. rand = rnd()%itemgroup_db[group_id].random[i].data_qty;
  235. //Woops, why is the data empty? Every check should be done when load the item group! So this is bad day for the player :P
  236. if (!&itemgroup_db[group_id].random[i].data[rand] || !itemgroup_db[group_id].random[i].data[rand].nameid) {
  237. continue;
  238. }
  239. if (itemdb_exists(itemgroup_db[group_id].random[i].data[rand].nameid))
  240. itemdb_pc_get_itemgroup_sub(sd,group_id,&itemgroup_db[group_id].random[i].data[rand]);
  241. }
  242. return 0;
  243. }
  244. /*==========================================
  245. * Calculates total item-group related bonuses for the given item
  246. *------------------------------------------*/
  247. int itemdb_group_bonus(struct map_session_data* sd, int itemid)
  248. {
  249. int bonus = 0, i, j;
  250. for (i=0; i < MAX_ITEMGROUP; i++) {
  251. if (!sd->itemgrouphealrate[i])
  252. continue;
  253. ARR_FIND(0,itemgroup_db[i].random[0].data_qty,j,itemgroup_db[i].random[0].data[j].nameid == itemid );
  254. if( j < itemgroup_db[i].random[0].data_qty )
  255. bonus += sd->itemgrouphealrate[i];
  256. }
  257. return bonus;
  258. }
  259. /// Searches for the item_data.
  260. /// Returns the item_data or NULL if it does not exist.
  261. struct item_data* itemdb_exists(int nameid)
  262. {
  263. struct item_data* item;
  264. if( nameid >= 0 && nameid < ARRAYLENGTH(itemdb_array) )
  265. return itemdb_array[nameid];
  266. item = (struct item_data*)idb_get(itemdb_other,nameid);
  267. if( item == &dummy_item )
  268. return NULL;// dummy data, doesn't exist
  269. return item;
  270. }
  271. /// Returns name type of ammunition [Cydh]
  272. const char *itemdb_typename_ammo (enum e_item_ammo ammo) {
  273. switch (ammo) {
  274. case AMMO_ARROW: return "Arrow";
  275. case AMMO_THROWABLE_DAGGER: return "Throwable Dagger";
  276. case AMMO_BULLET: return "Bullet";
  277. case AMMO_SHELL: return "Shell";
  278. case AMMO_GRENADE: return "Grenade";
  279. case AMMO_SHURIKEN: return "Shuriken";
  280. case AMMO_KUNAI: return "Kunai";
  281. case AMMO_CANNONBALL: return "Cannonball";
  282. case AMMO_THROWABLE_ITEM: return "Throwable Item/Sling Item";
  283. }
  284. return "Ammunition";
  285. }
  286. /// Returns human readable name for given item type.
  287. /// @param type Type id to retrieve name for ( IT_* ).
  288. const char* itemdb_typename(enum item_types type)
  289. {
  290. switch(type)
  291. {
  292. case IT_HEALING: return "Potion/Food";
  293. case IT_USABLE: return "Usable";
  294. case IT_ETC: return "Etc.";
  295. case IT_WEAPON: return "Weapon";
  296. case IT_ARMOR: return "Armor";
  297. case IT_CARD: return "Card";
  298. case IT_PETEGG: return "Pet Egg";
  299. case IT_PETARMOR: return "Pet Accessory";
  300. case IT_AMMO: return "Arrow/Ammunition";
  301. case IT_DELAYCONSUME: return "Delay-Consume Usable";
  302. case IT_SHADOWGEAR: return "Shadow Equipment";
  303. case IT_CASH: return "Cash Usable";
  304. }
  305. return "Unknown Type";
  306. }
  307. /*==========================================
  308. * Converts the jobid from the format in itemdb
  309. * to the format used by the map server. [Skotlex]
  310. *------------------------------------------*/
  311. static void itemdb_jobid2mapid(unsigned int *bclass, unsigned int jobmask)
  312. {
  313. int i;
  314. bclass[0]= bclass[1]= bclass[2]= 0;
  315. //Base classes
  316. if (jobmask & 1<<JOB_NOVICE)
  317. { //Both Novice/Super-Novice are counted with the same ID
  318. bclass[0] |= 1<<MAPID_NOVICE;
  319. bclass[1] |= 1<<MAPID_NOVICE;
  320. }
  321. for (i = JOB_NOVICE+1; i <= JOB_THIEF; i++)
  322. {
  323. if (jobmask & 1<<i)
  324. bclass[0] |= 1<<(MAPID_NOVICE+i);
  325. }
  326. //2-1 classes
  327. if (jobmask & 1<<JOB_KNIGHT)
  328. bclass[1] |= 1<<MAPID_SWORDMAN;
  329. if (jobmask & 1<<JOB_PRIEST)
  330. bclass[1] |= 1<<MAPID_ACOLYTE;
  331. if (jobmask & 1<<JOB_WIZARD)
  332. bclass[1] |= 1<<MAPID_MAGE;
  333. if (jobmask & 1<<JOB_BLACKSMITH)
  334. bclass[1] |= 1<<MAPID_MERCHANT;
  335. if (jobmask & 1<<JOB_HUNTER)
  336. bclass[1] |= 1<<MAPID_ARCHER;
  337. if (jobmask & 1<<JOB_ASSASSIN)
  338. bclass[1] |= 1<<MAPID_THIEF;
  339. //2-2 classes
  340. if (jobmask & 1<<JOB_CRUSADER)
  341. bclass[2] |= 1<<MAPID_SWORDMAN;
  342. if (jobmask & 1<<JOB_MONK)
  343. bclass[2] |= 1<<MAPID_ACOLYTE;
  344. if (jobmask & 1<<JOB_SAGE)
  345. bclass[2] |= 1<<MAPID_MAGE;
  346. if (jobmask & 1<<JOB_ALCHEMIST)
  347. bclass[2] |= 1<<MAPID_MERCHANT;
  348. if (jobmask & 1<<JOB_BARD)
  349. bclass[2] |= 1<<MAPID_ARCHER;
  350. // Bard/Dancer share the same slot now.
  351. // if (jobmask & 1<<JOB_DANCER)
  352. // bclass[2] |= 1<<MAPID_ARCHER;
  353. if (jobmask & 1<<JOB_ROGUE)
  354. bclass[2] |= 1<<MAPID_THIEF;
  355. //Special classes that don't fit above.
  356. if (jobmask & 1<<21) //Taekwon boy
  357. bclass[0] |= 1<<MAPID_TAEKWON;
  358. if (jobmask & 1<<22) //Star Gladiator
  359. bclass[1] |= 1<<MAPID_TAEKWON;
  360. if (jobmask & 1<<23) //Soul Linker
  361. bclass[2] |= 1<<MAPID_TAEKWON;
  362. if (jobmask & 1<<JOB_GUNSLINGER)
  363. bclass[0] |= 1<<MAPID_GUNSLINGER;
  364. if (jobmask & 1<<JOB_NINJA) { //Kagerou/Oboro jobs can equip Ninja equips. [Rytech]
  365. bclass[0] |= 1<<MAPID_NINJA;
  366. bclass[1] |= 1<<MAPID_NINJA;
  367. }
  368. if (jobmask & 1<<26) //Bongun/Munak
  369. bclass[0] |= 1<<MAPID_GANGSI;
  370. if (jobmask & 1<<27) //Death Knight
  371. bclass[1] |= 1<<MAPID_GANGSI;
  372. if (jobmask & 1<<28) //Dark Collector
  373. bclass[2] |= 1<<MAPID_GANGSI;
  374. if (jobmask & 1<<29) //Kagerou / Oboro
  375. bclass[1] |= 1<<MAPID_NINJA;
  376. if (jobmask & 1<<30) //Rebellion
  377. bclass[1] |= 1<<MAPID_GUNSLINGER;
  378. }
  379. static void create_dummy_data(void)
  380. {
  381. memset(&dummy_item, 0, sizeof(struct item_data));
  382. dummy_item.nameid=500;
  383. dummy_item.weight=1;
  384. dummy_item.value_sell=1;
  385. dummy_item.type=IT_ETC; //Etc item
  386. safestrncpy(dummy_item.name,"UNKNOWN_ITEM",sizeof(dummy_item.name));
  387. safestrncpy(dummy_item.jname,"UNKNOWN_ITEM",sizeof(dummy_item.jname));
  388. dummy_item.view_id=UNKNOWN_ITEM_ID;
  389. }
  390. static struct item_data* create_item_data(int nameid)
  391. {
  392. struct item_data *id;
  393. CREATE(id, struct item_data, 1);
  394. id->nameid = nameid;
  395. id->weight = 1;
  396. id->type = IT_ETC;
  397. return id;
  398. }
  399. /*==========================================
  400. * Loads (and creates if not found) an item from the db.
  401. *------------------------------------------*/
  402. struct item_data* itemdb_load(int nameid)
  403. {
  404. struct item_data *id;
  405. if( nameid >= 0 && nameid < ARRAYLENGTH(itemdb_array) )
  406. {
  407. id = itemdb_array[nameid];
  408. if( id == NULL || id == &dummy_item )
  409. id = itemdb_array[nameid] = create_item_data(nameid);
  410. return id;
  411. }
  412. id = (struct item_data*)idb_get(itemdb_other, nameid);
  413. if( id == NULL || id == &dummy_item )
  414. {
  415. id = create_item_data(nameid);
  416. idb_put(itemdb_other, nameid, id);
  417. }
  418. return id;
  419. }
  420. /*==========================================
  421. * Loads an item from the db. If not found, it will return the dummy item.
  422. *------------------------------------------*/
  423. struct item_data* itemdb_search(int nameid)
  424. {
  425. struct item_data* id;
  426. if( nameid >= 0 && nameid < ARRAYLENGTH(itemdb_array) )
  427. id = itemdb_array[nameid];
  428. else
  429. id = (struct item_data*)idb_get(itemdb_other, nameid);
  430. if( id == NULL )
  431. {
  432. ShowWarning("itemdb_search: Item ID %d does not exists in the item_db. Using dummy data.\n", nameid);
  433. id = &dummy_item;
  434. dummy_item.nameid = nameid;
  435. }
  436. return id;
  437. }
  438. /*==========================================
  439. * Returns if given item is a player-equippable piece.
  440. *------------------------------------------*/
  441. bool itemdb_isequip(int nameid)
  442. {
  443. int type=itemdb_type(nameid);
  444. switch (type) {
  445. case IT_WEAPON:
  446. case IT_ARMOR:
  447. case IT_AMMO:
  448. case IT_SHADOWGEAR:
  449. return true;
  450. default:
  451. return false;
  452. }
  453. }
  454. /*==========================================
  455. * Alternate version of itemdb_isequip
  456. *------------------------------------------*/
  457. bool itemdb_isequip2(struct item_data *data)
  458. {
  459. nullpo_ret(data);
  460. switch(data->type) {
  461. case IT_WEAPON:
  462. case IT_ARMOR:
  463. case IT_AMMO:
  464. case IT_SHADOWGEAR:
  465. return true;
  466. default:
  467. return false;
  468. }
  469. }
  470. /*==========================================
  471. * Returns if given item's type is stackable.
  472. *------------------------------------------*/
  473. bool itemdb_isstackable(uint16 nameid)
  474. {
  475. uint8 type = itemdb_type(nameid);
  476. switch(type) {
  477. case IT_WEAPON:
  478. case IT_ARMOR:
  479. case IT_PETEGG:
  480. case IT_PETARMOR:
  481. case IT_SHADOWGEAR:
  482. return false;
  483. default:
  484. return true;
  485. }
  486. }
  487. /*==========================================
  488. * Alternate version of itemdb_isstackable
  489. *------------------------------------------*/
  490. bool itemdb_isstackable2(struct item_data *data)
  491. {
  492. nullpo_ret(data);
  493. switch(data->type) {
  494. case IT_WEAPON:
  495. case IT_ARMOR:
  496. case IT_PETEGG:
  497. case IT_PETARMOR:
  498. case IT_SHADOWGEAR:
  499. return false;
  500. default:
  501. return true;
  502. }
  503. }
  504. /*==========================================
  505. * Trade Restriction functions [Skotlex]
  506. *------------------------------------------*/
  507. int itemdb_isdropable_sub(struct item_data *item, int gmlv, int unused) {
  508. return (item && (!(item->flag.trade_restriction&1) || gmlv >= item->gm_lv_trade_override));
  509. }
  510. int itemdb_cantrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  511. return (item && (!(item->flag.trade_restriction&2) || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  512. }
  513. int itemdb_canpartnertrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  514. return (item && (item->flag.trade_restriction&4 || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  515. }
  516. int itemdb_cansell_sub(struct item_data* item, int gmlv, int unused) {
  517. return (item && (!(item->flag.trade_restriction&8) || gmlv >= item->gm_lv_trade_override));
  518. }
  519. int itemdb_cancartstore_sub(struct item_data* item, int gmlv, int unused) {
  520. return (item && (!(item->flag.trade_restriction&16) || gmlv >= item->gm_lv_trade_override));
  521. }
  522. int itemdb_canstore_sub(struct item_data* item, int gmlv, int unused) {
  523. return (item && (!(item->flag.trade_restriction&32) || gmlv >= item->gm_lv_trade_override));
  524. }
  525. int itemdb_canguildstore_sub(struct item_data* item, int gmlv, int unused) {
  526. return (item && (!(item->flag.trade_restriction&64) || gmlv >= item->gm_lv_trade_override));
  527. }
  528. int itemdb_canmail_sub(struct item_data* item, int gmlv, int unused) {
  529. return (item && (!(item->flag.trade_restriction&128) || gmlv >= item->gm_lv_trade_override));
  530. }
  531. int itemdb_canauction_sub(struct item_data* item, int gmlv, int unused) {
  532. return (item && (!(item->flag.trade_restriction&256) || gmlv >= item->gm_lv_trade_override));
  533. }
  534. bool itemdb_isrestricted(struct item* item, int gmlv, int gmlv2, int (*func)(struct item_data*, int, int))
  535. {
  536. struct item_data* item_data = itemdb_search(item->nameid);
  537. int i;
  538. if (!func(item_data, gmlv, gmlv2))
  539. return false;
  540. if(item_data->slot == 0 || itemdb_isspecial(item->card[0]))
  541. return true;
  542. for(i = 0; i < item_data->slot; i++) {
  543. if (!item->card[i]) continue;
  544. if (!func(itemdb_search(item->card[i]), gmlv, gmlv2))
  545. return false;
  546. }
  547. return true;
  548. }
  549. /** Specifies if item-type should drop unidentified.
  550. * @param nameid ID of item
  551. */
  552. char itemdb_isidentified(int nameid) {
  553. int type=itemdb_type(nameid);
  554. switch (type) {
  555. case IT_WEAPON:
  556. case IT_ARMOR:
  557. case IT_PETARMOR:
  558. case IT_SHADOWGEAR:
  559. return 0;
  560. default:
  561. return 1;
  562. }
  563. }
  564. /** Search by name for the override flags available items (Give item another sprite)
  565. * Structure: <nameid>,<sprite>
  566. */
  567. static bool itemdb_read_itemavail(char* str[], int columns, int current) {
  568. int nameid, sprite;
  569. struct item_data *id;
  570. nameid = atoi(str[0]);
  571. if( ( id = itemdb_exists(nameid) ) == NULL )
  572. {
  573. ShowWarning("itemdb_read_itemavail: Invalid item id %d.\n", nameid);
  574. return false;
  575. }
  576. sprite = atoi(str[1]);
  577. if( sprite > 0 )
  578. {
  579. id->flag.available = 1;
  580. id->view_id = sprite;
  581. }
  582. else
  583. {
  584. id->flag.available = 0;
  585. }
  586. return true;
  587. }
  588. /** Read item group data
  589. * Structure: GroupID,ItemID,Rate{,Amount,isMust,isAnnounced,Duration,isNamed,isBound}
  590. */
  591. static void itemdb_read_itemgroup_sub(const char* filename, bool silent)
  592. {
  593. FILE *fp;
  594. int ln=0, entries=0;
  595. char line[1024];
  596. if ((fp=fopen(filename,"r")) == NULL) {
  597. if(silent == 0) ShowError("Can't read %s\n", filename);
  598. return;
  599. }
  600. while (fgets(line,sizeof(line),fp)) {
  601. uint16 nameid;
  602. int j, group_id, prob = 1, amt = 1, rand_group = 1, announced = 0, dur = 0, named = 0, bound = 0;
  603. char *str[3], *p;
  604. bool found = false;
  605. struct s_item_group_random *random;
  606. ln++;
  607. if (line[0] == '/' && line[1] == '/')
  608. continue;
  609. if (strstr(line,"import")) {
  610. char w1[1024], w2[1024];
  611. if (sscanf(line,"%[^:]: %[^\r\n]",w1,w2) == 2 &&
  612. strcmpi(w1,"import") == 0)
  613. {
  614. itemdb_read_itemgroup_sub(w2, 0);
  615. continue;
  616. }
  617. }
  618. memset(str,0,sizeof(str));
  619. for (j = 0, p = line; j < 3 && p;j++) {
  620. str[j] = p;
  621. if (j == 2)
  622. sscanf(str[j],"%d,%d,%d,%d,%d,%d,%d",&prob,&amt,&rand_group,&announced,&dur,&named,&bound);
  623. p = strchr(p,',');
  624. if (p) *p++=0;
  625. }
  626. if (str[0] == NULL)
  627. continue;
  628. if (j < 3) {
  629. if (j > 1) //Or else it barks on blank lines...
  630. ShowWarning("itemdb_read_itemgroup: Insufficient fields for entry at %s:%d\n", filename, ln);
  631. continue;
  632. }
  633. //Checking group_id
  634. trim(str[0]);
  635. if (ISDIGIT(str[0][0]))
  636. group_id = atoi(str[0]);
  637. else //Try reads group id by const
  638. script_get_constant(trim(str[0]),&group_id);
  639. if (!group_id || group_id >= MAX_ITEMGROUP) {
  640. ShowWarning("itemdb_read_itemgroup: Cannot save '%s' because invalid group id or group db is overflow in %s:%d\n", str[0], filename, ln);
  641. continue;
  642. }
  643. //Checking sub group
  644. if (rand_group < 0 || rand_group > MAX_ITEMGROUP_RANDGROUP) {
  645. ShowWarning("itemdb_read_itemgroup: Invalid sub group %d for group '%s' in %s:%d\n", rand_group, str[0], filename, ln);
  646. continue;
  647. }
  648. if (rand_group && prob < 1) {
  649. ShowWarning("itemdb_read_itemgroup: Invalid probaility for group '%s' sub: %d in %s:%d\n", str[0], rand_group, filename, ln);
  650. continue;
  651. }
  652. //Checking item
  653. trim(str[1]);
  654. if (ISDIGIT(str[1][0]) && itemdb_exists((nameid = atoi(str[1]))))
  655. found = true;
  656. else if (itemdb_searchname(str[1])) {
  657. found = true;
  658. nameid = itemdb_searchname(str[1])->nameid;
  659. }
  660. if (!found) {
  661. ShowWarning("itemdb_read_itemgroup: Non-existant item '%s' in %s:%d\n", str[1], filename, ln);
  662. continue;
  663. }
  664. amt = cap_value(amt,1,MAX_AMOUNT);
  665. dur = cap_value(dur,0,UINT16_MAX);
  666. bound = cap_value(bound,0,4);
  667. //Must item (rand_group == 0), place it here
  668. if (!rand_group) {
  669. uint16 idx = itemgroup_db[group_id].must_qty;
  670. if (!idx)
  671. CREATE(itemgroup_db[group_id].must,struct s_item_group,1);
  672. else
  673. RECREATE(itemgroup_db[group_id].must,struct s_item_group,idx+1);
  674. itemgroup_db[group_id].must[idx].nameid = nameid;
  675. itemgroup_db[group_id].must[idx].amount = amt;
  676. itemgroup_db[group_id].must[idx].isAnnounced = announced;
  677. itemgroup_db[group_id].must[idx].duration = dur;
  678. itemgroup_db[group_id].must[idx].isNamed = named;
  679. itemgroup_db[group_id].must[idx].bound = bound;
  680. itemgroup_db[group_id].must_qty++;
  681. //If 'must' item isn't set as random item, skip the next process
  682. if (!prob) {
  683. entries++;
  684. continue;
  685. }
  686. rand_group = 0;
  687. }
  688. else
  689. rand_group -= 1;
  690. random = &itemgroup_db[group_id].random[rand_group];
  691. //Check, if the entry for this random group already created or not
  692. if (!random->data_qty) {
  693. CREATE(random->data,struct s_item_group,prob);
  694. random->data_qty = 0;
  695. }
  696. else
  697. RECREATE(random->data,struct s_item_group,random->data_qty+prob);
  698. //Now put the entry to its rand_group
  699. for (j = random->data_qty; j < random->data_qty+prob; j++) {
  700. random->data[j].nameid = nameid;
  701. random->data[j].amount = amt;
  702. random->data[j].isAnnounced = announced;
  703. random->data[j].duration = dur;
  704. random->data[j].isNamed = named;
  705. random->data[j].bound = bound;
  706. }
  707. random->data_qty += prob;
  708. entries++;
  709. }
  710. fclose(fp);
  711. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, filename);
  712. return;
  713. }
  714. static void itemdb_read_itemgroup(const char* basedir, bool silent) {
  715. char filepath[256];
  716. sprintf(filepath, "%s/%s", basedir, "item_group_db.txt");
  717. itemdb_read_itemgroup_sub(filepath, silent);
  718. return;
  719. }
  720. /** Read item forbidden by mapflag (can't equip item)
  721. * Structure: <nameid>,<mode>
  722. */
  723. static bool itemdb_read_noequip(char* str[], int columns, int current) {
  724. int nameid;
  725. struct item_data *id;
  726. nameid = atoi(str[0]);
  727. if( ( id = itemdb_exists(nameid) ) == NULL )
  728. {
  729. ShowWarning("itemdb_read_noequip: Invalid item id %d.\n", nameid);
  730. return false;
  731. }
  732. id->flag.no_equip |= atoi(str[1]);
  733. return true;
  734. }
  735. /** Reads item trade restrictions [Skotlex]
  736. * Structure: <nameid>,<mask>,<gm level>
  737. */
  738. static bool itemdb_read_itemtrade(char* str[], int columns, int current) {
  739. int nameid, flag, gmlv;
  740. struct item_data *id;
  741. nameid = atoi(str[0]);
  742. if( ( id = itemdb_exists(nameid) ) == NULL )
  743. {
  744. //ShowWarning("itemdb_read_itemtrade: Invalid item id %d.\n", nameid);
  745. //return false;
  746. // FIXME: item_trade.txt contains items, which are commented in item database.
  747. return true;
  748. }
  749. flag = atoi(str[1]);
  750. gmlv = atoi(str[2]);
  751. if( flag < 0 || flag > 511 ) {//Check range
  752. ShowWarning("itemdb_read_itemtrade: Invalid trading mask %d for item id %d.\n", flag, nameid);
  753. return false;
  754. }
  755. if( gmlv < 1 )
  756. {
  757. ShowWarning("itemdb_read_itemtrade: Invalid override GM level %d for item id %d.\n", gmlv, nameid);
  758. return false;
  759. }
  760. id->flag.trade_restriction = flag;
  761. id->gm_lv_trade_override = gmlv;
  762. return true;
  763. }
  764. /** Reads item delay amounts [Paradox924X]
  765. * Structure: <nameid>,<delay>
  766. */
  767. static bool itemdb_read_itemdelay(char* str[], int columns, int current) {
  768. int nameid, delay;
  769. struct item_data *id;
  770. nameid = atoi(str[0]);
  771. if( ( id = itemdb_exists(nameid) ) == NULL )
  772. {
  773. ShowWarning("itemdb_read_itemdelay: Invalid item id %d.\n", nameid);
  774. return false;
  775. }
  776. delay = atoi(str[1]);
  777. if( delay < 0 )
  778. {
  779. ShowWarning("itemdb_read_itemdelay: Invalid delay %d for item id %d.\n", id->delay, nameid);
  780. return false;
  781. }
  782. id->delay = delay;
  783. return true;
  784. }
  785. /** Reads item stacking restrictions
  786. * Structure: <item id>,<stack limit amount>,<type>
  787. */
  788. static bool itemdb_read_stack(char* fields[], int columns, int current) {
  789. unsigned short nameid, amount;
  790. unsigned int type;
  791. struct item_data* id;
  792. nameid = (unsigned short)strtoul(fields[0], NULL, 10);
  793. if( ( id = itemdb_exists(nameid) ) == NULL )
  794. {
  795. ShowWarning("itemdb_read_stack: Unknown item id '%hu'.\n", nameid);
  796. return false;
  797. }
  798. if( !itemdb_isstackable2(id) )
  799. {
  800. ShowWarning("itemdb_read_stack: Item id '%hu' is not stackable.\n", nameid);
  801. return false;
  802. }
  803. amount = (unsigned short)strtoul(fields[1], NULL, 10);
  804. type = strtoul(fields[2], NULL, 10);
  805. if( !amount )
  806. {// ignore
  807. return true;
  808. }
  809. id->stack.amount = amount;
  810. id->stack.inventory = (type&1)!=0;
  811. id->stack.cart = (type&2)!=0;
  812. id->stack.storage = (type&4)!=0;
  813. id->stack.guildstorage = (type&8)!=0;
  814. return true;
  815. }
  816. /** Reads items allowed to be sold in buying stores
  817. * <nameid>
  818. */
  819. static bool itemdb_read_buyingstore(char* fields[], int columns, int current) {
  820. int nameid;
  821. struct item_data* id;
  822. nameid = atoi(fields[0]);
  823. if( ( id = itemdb_exists(nameid) ) == NULL )
  824. {
  825. ShowWarning("itemdb_read_buyingstore: Invalid item id %d.\n", nameid);
  826. return false;
  827. }
  828. if( !itemdb_isstackable2(id) )
  829. {
  830. ShowWarning("itemdb_read_buyingstore: Non-stackable item id %d cannot be enabled for buying store.\n", nameid);
  831. return false;
  832. }
  833. id->flag.buyingstore = true;
  834. return true;
  835. }
  836. /** Item usage restriction (item_nouse.txt)
  837. * <nameid>,<flag>,<override>
  838. */
  839. static bool itemdb_read_nouse(char* fields[], int columns, int current) {
  840. int nameid, flag, override;
  841. struct item_data* id;
  842. nameid = atoi(fields[0]);
  843. if( ( id = itemdb_exists(nameid) ) == NULL ) {
  844. ShowWarning("itemdb_read_nouse: Invalid item id %d.\n", nameid);
  845. return false;
  846. }
  847. flag = atoi(fields[1]);
  848. override = atoi(fields[2]);
  849. id->item_usage.flag = flag;
  850. id->item_usage.override = override;
  851. return true;
  852. }
  853. /** Misc Item flags
  854. * <item_id>,<flag>
  855. * &1 - Log as dead branch
  856. * &2 - As item container
  857. */
  858. static bool itemdb_read_flag(char* fields[], int columns, int current) {
  859. uint16 nameid = atoi(fields[0]);
  860. uint8 flag = atoi(fields[1]);
  861. struct item_data *id;
  862. if (!(id = itemdb_exists(nameid))) {
  863. ShowError("itemdb_read_flag: Invalid item item with id %d\n", nameid);
  864. return true;
  865. }
  866. if (flag&1)
  867. id->flag.dead_branch = 1;
  868. if (flag&2)
  869. id->flag.group = 1;
  870. return true;
  871. }
  872. /**
  873. * @return: amount of retrieved entries.
  874. **/
  875. static int itemdb_combo_split_atoi (char *str, int *val) {
  876. int i;
  877. for (i=0; i<MAX_ITEMS_PER_COMBO; i++) {
  878. if (!str) break;
  879. val[i] = atoi(str);
  880. str = strchr(str,':');
  881. if (str)
  882. *str++=0;
  883. }
  884. if( i == 0 ) //No data found.
  885. return 0;
  886. return i;
  887. }
  888. /**
  889. * <combo{:combo{:combo:{..}}}>,<{ script }>
  890. **/
  891. static void itemdb_read_combos(const char* basedir, bool silent) {
  892. uint32 lines = 0, count = 0;
  893. char line[1024];
  894. char path[256];
  895. FILE* fp;
  896. sprintf(path, "%s/%s", basedir, "item_combo_db.txt");
  897. if ((fp = fopen(path, "r")) == NULL) {
  898. if(silent==0) ShowError("itemdb_read_combos: File not found \"%s\".\n", path);
  899. return;
  900. }
  901. // process rows one by one
  902. while(fgets(line, sizeof(line), fp)) {
  903. char *str[2], *p;
  904. lines++;
  905. if (line[0] == '/' && line[1] == '/')
  906. continue;
  907. memset(str, 0, sizeof(str));
  908. p = line;
  909. p = trim(p);
  910. if (*p == '\0')
  911. continue;// empty line
  912. if (!strchr(p,','))
  913. {
  914. /* is there even a single column? */
  915. ShowError("itemdb_read_combos: Insufficient columns in line %d of \"%s\", skipping.\n", lines, path);
  916. continue;
  917. }
  918. str[0] = p;
  919. p = strchr(p,',');
  920. *p = '\0';
  921. p++;
  922. str[1] = p;
  923. p = strchr(p,',');
  924. p++;
  925. if (str[1][0] != '{') {
  926. ShowError("itemdb_read_combos(#1): Invalid format (Script column) in line %d of \"%s\", skipping.\n", lines, path);
  927. continue;
  928. }
  929. /* no ending key anywhere (missing \}\) */
  930. if ( str[1][strlen(str[1])-1] != '}' ) {
  931. ShowError("itemdb_read_combos(#2): Invalid format (Script column) in line %d of \"%s\", skipping.\n", lines, path);
  932. continue;
  933. } else {
  934. int items[MAX_ITEMS_PER_COMBO];
  935. int v = 0, retcount = 0;
  936. struct item_data * id = NULL;
  937. int idx = 0;
  938. if((retcount = itemdb_combo_split_atoi(str[0], items)) < 2) {
  939. ShowError("itemdb_read_combos: line %d of \"%s\" doesn't have enough items to make for a combo (min:2), skipping.\n", lines, path);
  940. continue;
  941. }
  942. /* validate */
  943. for(v = 0; v < retcount; v++) {
  944. if( !itemdb_exists(items[v]) ) {
  945. ShowError("itemdb_read_combos: line %d of \"%s\" contains unknown item ID %d, skipping.\n", lines, path,items[v]);
  946. break;
  947. }
  948. }
  949. /* failed at some item */
  950. if( v < retcount )
  951. continue;
  952. id = itemdb_exists(items[0]);
  953. idx = id->combos_count;
  954. /* first entry, create */
  955. if( id->combos == NULL ) {
  956. CREATE(id->combos, struct item_combo*, 1);
  957. id->combos_count = 1;
  958. } else {
  959. RECREATE(id->combos, struct item_combo*, ++id->combos_count);
  960. }
  961. CREATE(id->combos[idx],struct item_combo,1);
  962. id->combos[idx]->nameid = aMalloc( retcount * sizeof(unsigned short) );
  963. id->combos[idx]->count = retcount;
  964. id->combos[idx]->script = parse_script(str[1], path, lines, 0);
  965. id->combos[idx]->id = count;
  966. id->combos[idx]->isRef = false;
  967. /* populate ->nameid field */
  968. for( v = 0; v < retcount; v++ ) {
  969. id->combos[idx]->nameid[v] = items[v];
  970. }
  971. /* populate the children to refer to this combo */
  972. for( v = 1; v < retcount; v++ ) {
  973. struct item_data * it;
  974. int index;
  975. it = itemdb_exists(items[v]);
  976. index = it->combos_count;
  977. if( it->combos == NULL ) {
  978. CREATE(it->combos, struct item_combo*, 1);
  979. it->combos_count = 1;
  980. } else {
  981. RECREATE(it->combos, struct item_combo*, ++it->combos_count);
  982. }
  983. CREATE(it->combos[index],struct item_combo,1);
  984. /* we copy previously alloc'd pointers and just set it to reference */
  985. memcpy(it->combos[index],id->combos[idx],sizeof(struct item_combo));
  986. /* we flag this way to ensure we don't double-dealloc same data */
  987. it->combos[index]->isRef = true;
  988. }
  989. idb_put(itemdb_combo,id->combos[idx]->id,id->combos[idx]);
  990. }
  991. count++;
  992. }
  993. fclose(fp);
  994. ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",count,path);
  995. return;
  996. }
  997. /*======================================
  998. * Applies gender restrictions according to settings. [Skotlex]
  999. *======================================*/
  1000. static char itemdb_gendercheck(struct item_data *id)
  1001. {
  1002. if (id->nameid == WEDDING_RING_M) //Grom Ring
  1003. return 1;
  1004. if (id->nameid == WEDDING_RING_F) //Bride Ring
  1005. return 0;
  1006. if (id->look == W_MUSICAL && id->type == IT_WEAPON) //Musical instruments are always male-only
  1007. return 1;
  1008. if (id->look == W_WHIP && id->type == IT_WEAPON) //Whips are always female-only
  1009. return 0;
  1010. return (battle_config.ignore_items_gender) ? 2 : id->sex;
  1011. }
  1012. /**
  1013. * [RRInd]
  1014. * For backwards compatibility, in Renewal mode, MATK from weapons comes from the atk slot
  1015. * We use a ':' delimiter which, if not found, assumes the weapon does not provide any matk.
  1016. **/
  1017. #ifdef RENEWAL
  1018. static void itemdb_re_split_atoi(char *str, int *atk, int *matk) {
  1019. int i, val[2];
  1020. for (i=0; i<2; i++) {
  1021. if (!str) break;
  1022. val[i] = atoi(str);
  1023. str = strchr(str,':');
  1024. if (str)
  1025. *str++=0;
  1026. }
  1027. if( i == 0 ) {
  1028. *atk = *matk = 0;
  1029. return;//no data found
  1030. }
  1031. if( i == 1 ) {//Single Value, we assume it's the ATK
  1032. *atk = val[0];
  1033. *matk = 0;
  1034. return;
  1035. }
  1036. //We assume we have 2 values.
  1037. *atk = val[0];
  1038. *matk = val[1];
  1039. return;
  1040. }
  1041. #endif
  1042. /**
  1043. * Processes one itemdb entry
  1044. */
  1045. static bool itemdb_parse_dbrow(char** str, const char* source, int line, int scriptopt) {
  1046. /*
  1047. +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
  1048. | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
  1049. +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
  1050. | id | name_english | name_japanese | type | price_buy | price_sell | weight | attack | defence | range | slots | equip_jobs | equip_upper | equip_genders | equip_locations | weapon_level | equip_level | refineable | view | script | equip_script | unequip_script |
  1051. +----+--------------+---------------+------+-----------+------------+--------+--------+---------+-------+-------+------------+-------------+---------------+-----------------+--------------+-------------+------------+------+--------+--------------+----------------+
  1052. */
  1053. int nameid;
  1054. struct item_data* id;
  1055. nameid = atoi(str[0]);
  1056. if( nameid <= 0 )
  1057. {
  1058. ShowWarning("itemdb_parse_dbrow: Invalid id %d in line %d of \"%s\", skipping.\n", nameid, line, source);
  1059. return false;
  1060. }
  1061. //ID,Name,Jname,Type,Price,Sell,Weight,ATK,DEF,Range,Slot,Job,Job Upper,Gender,Loc,wLV,eLV,refineable,View
  1062. id = itemdb_load(nameid);
  1063. safestrncpy(id->name, str[1], sizeof(id->name));
  1064. safestrncpy(id->jname, str[2], sizeof(id->jname));
  1065. id->type = atoi(str[3]);
  1066. if( id->type < 0 || id->type == IT_UNKNOWN || id->type == IT_UNKNOWN2 || ( id->type > IT_SHADOWGEAR && id->type < IT_CASH ) || id->type >= IT_MAX )
  1067. {// catch invalid item types
  1068. ShowWarning("itemdb_parse_dbrow: Invalid item type %d for item %d. IT_ETC will be used.\n", id->type, nameid);
  1069. id->type = IT_ETC;
  1070. }
  1071. if (id->type == IT_DELAYCONSUME)
  1072. { //Items that are consumed only after target confirmation
  1073. id->type = IT_USABLE;
  1074. id->flag.delay_consume = 1;
  1075. } else //In case of an itemdb reload and the item type changed.
  1076. id->flag.delay_consume = 0;
  1077. //When a particular price is not given, we should base it off the other one
  1078. //(it is important to make a distinction between 'no price' and 0z)
  1079. if ( str[4][0] )
  1080. id->value_buy = atoi(str[4]);
  1081. else
  1082. id->value_buy = atoi(str[5]) * 2;
  1083. if ( str[5][0] )
  1084. id->value_sell = atoi(str[5]);
  1085. else
  1086. id->value_sell = id->value_buy / 2;
  1087. /*
  1088. if ( !str[4][0] && !str[5][0])
  1089. {
  1090. ShowWarning("itemdb_parse_dbrow: No buying/selling price defined for item %d (%s), using 20/10z\n", nameid, id->jname);
  1091. id->value_buy = 20;
  1092. id->value_sell = 10;
  1093. } else
  1094. */
  1095. if (id->value_buy/124. < id->value_sell/75.)
  1096. ShowWarning("itemdb_parse_dbrow: Buying/Selling [%d/%d] price of item %d (%s) allows Zeny making exploit through buying/selling at discounted/overcharged prices!\n",
  1097. id->value_buy, id->value_sell, nameid, id->jname);
  1098. id->weight = atoi(str[6]);
  1099. #ifdef RENEWAL
  1100. itemdb_re_split_atoi(str[7],&id->atk,&id->matk);
  1101. #else
  1102. id->atk = atoi(str[7]);
  1103. #endif
  1104. id->def = atoi(str[8]);
  1105. id->range = atoi(str[9]);
  1106. id->slot = atoi(str[10]);
  1107. if (id->slot > MAX_SLOTS)
  1108. {
  1109. ShowWarning("itemdb_parse_dbrow: Item %d (%s) specifies %d slots, but the server only supports up to %d. Using %d slots.\n", nameid, id->jname, id->slot, MAX_SLOTS, MAX_SLOTS);
  1110. id->slot = MAX_SLOTS;
  1111. }
  1112. itemdb_jobid2mapid(id->class_base, (unsigned int)strtoul(str[11],NULL,0));
  1113. id->class_upper = atoi(str[12]);
  1114. id->sex = atoi(str[13]);
  1115. id->equip = atoi(str[14]);
  1116. if (!id->equip && itemdb_isequip2(id))
  1117. {
  1118. ShowWarning("Item %d (%s) is an equipment with no equip-field! Making it an etc item.\n", nameid, id->jname);
  1119. id->type = IT_ETC;
  1120. }
  1121. if( id->type != IT_SHADOWGEAR && id->equip&EQP_SHADOW_GEAR )
  1122. {
  1123. ShowWarning("Item %d (%s) have invalid equipment slot! Making it an etc item.\n", nameid, id->jname);
  1124. id->type = IT_ETC;
  1125. }
  1126. id->wlv = cap_value(atoi(str[15]), REFINE_TYPE_ARMOR, REFINE_TYPE_MAX);
  1127. #ifdef RENEWAL
  1128. itemdb_re_split_atoi(str[16],&id->elv,&id->elvmax);
  1129. #else
  1130. id->elv = atoi(str[16]);
  1131. #endif
  1132. id->flag.no_refine = atoi(str[17]) ? 0 : 1; //FIXME: verify this
  1133. id->look = atoi(str[18]);
  1134. id->flag.available = 1;
  1135. id->view_id = 0;
  1136. id->sex = itemdb_gendercheck(id); //Apply gender filtering.
  1137. if (id->script) {
  1138. script_free_code(id->script);
  1139. id->script = NULL;
  1140. }
  1141. if (id->equip_script) {
  1142. script_free_code(id->equip_script);
  1143. id->equip_script = NULL;
  1144. }
  1145. if (id->unequip_script) {
  1146. script_free_code(id->unequip_script);
  1147. id->unequip_script = NULL;
  1148. }
  1149. if (*str[19])
  1150. id->script = parse_script(str[19], source, line, scriptopt);
  1151. if (*str[20])
  1152. id->equip_script = parse_script(str[20], source, line, scriptopt);
  1153. if (*str[21])
  1154. id->unequip_script = parse_script(str[21], source, line, scriptopt);
  1155. return true;
  1156. }
  1157. /**
  1158. * Read item from item db
  1159. * item_db2 overwriting item_db
  1160. */
  1161. static int itemdb_readdb(void){
  1162. const char* filename[] = {
  1163. DBPATH"item_db.txt",
  1164. DBIMPORT"/item_db.txt"
  1165. };
  1166. int fi;
  1167. for( fi = 0; fi < ARRAYLENGTH(filename); ++fi ) {
  1168. uint32 lines = 0, count = 0;
  1169. char line[1024];
  1170. char path[256];
  1171. FILE* fp;
  1172. sprintf(path, "%s/%s", db_path, filename[fi]);
  1173. fp = fopen(path, "r");
  1174. if( fp == NULL ) {
  1175. ShowWarning("itemdb_readdb: File not found \"%s\", skipping.\n", path);
  1176. continue;
  1177. }
  1178. // process rows one by one
  1179. while(fgets(line, sizeof(line), fp))
  1180. {
  1181. char *str[32], *p;
  1182. int i;
  1183. lines++;
  1184. if(line[0] == '/' && line[1] == '/')
  1185. continue;
  1186. memset(str, 0, sizeof(str));
  1187. p = line;
  1188. while( ISSPACE(*p) )
  1189. ++p;
  1190. if( *p == '\0' )
  1191. continue;// empty line
  1192. for( i = 0; i < 19; ++i )
  1193. {
  1194. str[i] = p;
  1195. p = strchr(p,',');
  1196. if( p == NULL )
  1197. break;// comma not found
  1198. *p = '\0';
  1199. ++p;
  1200. }
  1201. if( p == NULL )
  1202. {
  1203. ShowError("itemdb_readdb: Insufficient columns in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi(str[0]));
  1204. continue;
  1205. }
  1206. // Script
  1207. if( *p != '{' )
  1208. {
  1209. ShowError("itemdb_readdb: Invalid format (Script column) in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi(str[0]));
  1210. continue;
  1211. }
  1212. str[19] = p;
  1213. p = strstr(p+1,"},");
  1214. if( p == NULL )
  1215. {
  1216. ShowError("itemdb_readdb: Invalid format (Script column) in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi(str[0]));
  1217. continue;
  1218. }
  1219. p[1] = '\0';
  1220. p += 2;
  1221. // OnEquip_Script
  1222. if( *p != '{' )
  1223. {
  1224. ShowError("itemdb_readdb: Invalid format (OnEquip_Script column) in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi(str[0]));
  1225. continue;
  1226. }
  1227. str[20] = p;
  1228. p = strstr(p+1,"},");
  1229. if( p == NULL )
  1230. {
  1231. ShowError("itemdb_readdb: Invalid format (OnEquip_Script column) in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi(str[0]));
  1232. continue;
  1233. }
  1234. p[1] = '\0';
  1235. p += 2;
  1236. // OnUnequip_Script (last column)
  1237. if( *p != '{' )
  1238. {
  1239. ShowError("itemdb_readdb: Invalid format (OnUnequip_Script column) in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi(str[0]));
  1240. continue;
  1241. }
  1242. str[21] = p;
  1243. if ( str[21][strlen(str[21])-2] != '}' ) {
  1244. /* lets count to ensure it's not something silly e.g. a extra space at line ending */
  1245. int v, lcurly = 0, rcurly = 0;
  1246. for( v = 0; v < strlen(str[21]); v++ ) {
  1247. if( str[21][v] == '{' )
  1248. lcurly++;
  1249. else if ( str[21][v] == '}' )
  1250. rcurly++;
  1251. }
  1252. if( lcurly != rcurly ) {
  1253. ShowError("itemdb_readdb: Mismatching curly braces in line %d of \"%s\" (item with id %d), skipping.\n", lines, path, atoi(str[0]));
  1254. continue;
  1255. }
  1256. }
  1257. if (!itemdb_parse_dbrow(str, path, lines, 0))
  1258. continue;
  1259. count++;
  1260. }
  1261. fclose(fp);
  1262. ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, path);
  1263. }
  1264. return 0;
  1265. }
  1266. /**
  1267. * Read item_db table
  1268. */
  1269. static int itemdb_read_sqldb(void) {
  1270. const char* item_db_name[] = {
  1271. #ifdef RENEWAL
  1272. item_db_re_db,
  1273. #else
  1274. item_db_db,
  1275. #endif
  1276. item_db2_db
  1277. };
  1278. int fi;
  1279. for( fi = 0; fi < ARRAYLENGTH(item_db_name); ++fi ) {
  1280. uint32 lines = 0, count = 0;
  1281. // retrieve all rows from the item database
  1282. if( SQL_ERROR == Sql_Query(mmysql_handle, "SELECT * FROM `%s`", item_db_name[fi]) ) {
  1283. Sql_ShowDebug(mmysql_handle);
  1284. continue;
  1285. }
  1286. // process rows one by one
  1287. while( SQL_SUCCESS == Sql_NextRow(mmysql_handle) ) {// wrap the result into a TXT-compatible format
  1288. char* str[22];
  1289. char* dummy = "";
  1290. int i;
  1291. ++lines;
  1292. for( i = 0; i < 22; ++i ) {
  1293. Sql_GetData(mmysql_handle, i, &str[i], NULL);
  1294. if( str[i] == NULL )
  1295. str[i] = dummy; // get rid of NULL columns
  1296. }
  1297. if (!itemdb_parse_dbrow(str, item_db_name[fi], lines, SCRIPT_IGNORE_EXTERNAL_BRACKETS))
  1298. continue;
  1299. ++count;
  1300. }
  1301. // free the query result
  1302. Sql_FreeResult(mmysql_handle);
  1303. ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, item_db_name[fi]);
  1304. }
  1305. return 0;
  1306. }
  1307. /** Unique item ID function
  1308. * Only one operation by once
  1309. * @param flag
  1310. * 0 return new id
  1311. * 1 set new value, checked with current value
  1312. * 2 set new value bypassing anything
  1313. * 3/other
  1314. * @param value
  1315. * @return last value
  1316. *------------------------------------------*/
  1317. uint64 itemdb_unique_id(int8 flag, int64 value) {
  1318. static uint64 item_uid = 0;
  1319. if(flag)
  1320. {
  1321. if(flag == 1)
  1322. { if(item_uid < value)
  1323. return (item_uid = value);
  1324. }else if(flag == 2)
  1325. return (item_uid = value);
  1326. return item_uid;
  1327. }
  1328. return ++item_uid;
  1329. }
  1330. /**
  1331. * Load Unique ID for Item
  1332. */
  1333. static void itemdb_uid_load(void){
  1334. char * uid;
  1335. if (SQL_ERROR == Sql_Query(mmysql_handle, "SELECT `value` FROM `interreg` WHERE `varname`='unique_id'"))
  1336. Sql_ShowDebug(mmysql_handle);
  1337. if( SQL_SUCCESS != Sql_NextRow(mmysql_handle) )
  1338. {
  1339. ShowError("itemdb_uid_load: Unable to fetch unique_id data\n");
  1340. Sql_FreeResult(mmysql_handle);
  1341. return;
  1342. }
  1343. Sql_GetData(mmysql_handle, 0, &uid, NULL);
  1344. itemdb_unique_id(1, (uint64)strtoull(uid, NULL, 10));
  1345. Sql_FreeResult(mmysql_handle);
  1346. }
  1347. /** Check if the item is restricted by item_noequip.txt
  1348. * @param id Item that will be checked
  1349. * @param m Map ID
  1350. * @return true: can't be used; false: can be used
  1351. */
  1352. bool itemdb_isNoEquip(struct item_data *id, uint16 m) {
  1353. if (!id->flag.no_equip)
  1354. return false;
  1355. /* on restricted maps the item is consumed but the effect is not used */
  1356. if ((!map_flag_vs(m) && id->flag.no_equip&1) || // Normal
  1357. (map[m].flag.pvp && id->flag.no_equip&2) || // PVP
  1358. (map_flag_gvg(m) && id->flag.no_equip&4) || // GVG
  1359. (map[m].flag.battleground && id->flag.no_equip&8) || // Battleground
  1360. (map[m].flag.restricted && id->flag.no_equip&(8*map[m].zone)) // Zone restriction
  1361. )
  1362. return true;
  1363. return false;
  1364. }
  1365. /**
  1366. * Read all item-related databases
  1367. */
  1368. static void itemdb_read(void) {
  1369. int i;
  1370. const char* dbsubpath[] = {
  1371. "",
  1372. "/"DBIMPORT,
  1373. };
  1374. if (db_use_sqldbs)
  1375. itemdb_read_sqldb();
  1376. else
  1377. itemdb_readdb();
  1378. memset(&itemgroup_db, 0, sizeof(itemgroup_db));
  1379. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  1380. uint8 n1 = strlen(db_path)+strlen(dbsubpath[i])+1;
  1381. uint8 n2 = strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1;
  1382. char* dbsubpath1 = (char*)aMalloc(n1+1);
  1383. char* dbsubpath2 = (char*)aMalloc(n2+1);
  1384. if(i==0) {
  1385. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  1386. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  1387. }
  1388. else {
  1389. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  1390. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  1391. }
  1392. sv_readdb(dbsubpath1, "item_avail.txt", ',', 2, 2, -1, &itemdb_read_itemavail, i);
  1393. sv_readdb(dbsubpath1, "item_stack.txt", ',', 3, 3, -1, &itemdb_read_stack, i);
  1394. sv_readdb(dbsubpath1, "item_nouse.txt", ',', 3, 3, -1, &itemdb_read_nouse, i);
  1395. itemdb_read_itemgroup(dbsubpath2, i);
  1396. itemdb_read_combos(dbsubpath2,i); //TODO change this to sv_read ? id#script ?
  1397. sv_readdb(dbsubpath2, "item_noequip.txt", ',', 2, 2, -1, &itemdb_read_noequip, i);
  1398. sv_readdb(dbsubpath2, "item_trade.txt", ',', 3, 3, -1, &itemdb_read_itemtrade, i);
  1399. sv_readdb(dbsubpath2, "item_delay.txt", ',', 2, 2, -1, &itemdb_read_itemdelay, i);
  1400. sv_readdb(dbsubpath2, "item_buyingstore.txt", ',', 1, 1, -1, &itemdb_read_buyingstore, i);
  1401. sv_readdb(dbsubpath2, "item_flag.txt", ',', 2, 2, -1, &itemdb_read_flag, i);
  1402. aFree(dbsubpath1);
  1403. aFree(dbsubpath2);
  1404. }
  1405. itemdb_uid_load();
  1406. }
  1407. /*==========================================
  1408. * Initialize / Finalize
  1409. *------------------------------------------*/
  1410. /**
  1411. * Destroys the item_data.
  1412. */
  1413. static void destroy_item_data(struct item_data* self, bool free_self) {
  1414. if( self == NULL )
  1415. return;
  1416. // free scripts
  1417. if( self->script )
  1418. script_free_code(self->script);
  1419. if( self->equip_script )
  1420. script_free_code(self->equip_script);
  1421. if( self->unequip_script )
  1422. script_free_code(self->unequip_script);
  1423. if( self->combos_count ) {
  1424. int i;
  1425. for( i = 0; i < self->combos_count; i++ ) {
  1426. if( !self->combos[i]->isRef ) {
  1427. aFree(self->combos[i]->nameid);
  1428. script_free_code(self->combos[i]->script);
  1429. }
  1430. aFree(self->combos[i]);
  1431. }
  1432. aFree(self->combos);
  1433. }
  1434. #if defined(DEBUG)
  1435. // trash item
  1436. memset(self, 0xDD, sizeof(struct item_data));
  1437. #endif
  1438. // free self
  1439. if( free_self )
  1440. aFree(self);
  1441. }
  1442. /**
  1443. * @see DBApply
  1444. */
  1445. static int itemdb_final_sub(DBKey key, DBData *data, va_list ap)
  1446. {
  1447. struct item_data *id = db_data2ptr(data);
  1448. if( id != &dummy_item )
  1449. destroy_item_data(id, true);
  1450. return 0;
  1451. }
  1452. /**
  1453. * Reload Item DB
  1454. */
  1455. void itemdb_reload(void) {
  1456. struct s_mapiterator* iter;
  1457. struct map_session_data* sd;
  1458. int i,d,k;
  1459. // clear the previous itemdb data
  1460. for( i = 0; i < ARRAYLENGTH(itemdb_array); ++i )
  1461. if( itemdb_array[i] )
  1462. destroy_item_data(itemdb_array[i], true);
  1463. for (i = 0; i < MAX_ITEMGROUP; i++) {
  1464. uint8 j;
  1465. if (!(&itemgroup_db[i]))
  1466. continue;
  1467. if (itemgroup_db[i].must_qty)
  1468. aFree(itemgroup_db[i].must);
  1469. for (j = 0; j < MAX_ITEMGROUP_RANDGROUP; j++) {
  1470. if (!(&itemgroup_db[i].random[j]) || !itemgroup_db[i].random[j].data_qty)
  1471. continue;
  1472. aFree(itemgroup_db[i].random[j].data);
  1473. }
  1474. }
  1475. itemdb_other->clear(itemdb_other, itemdb_final_sub);
  1476. db_clear(itemdb_combo);
  1477. memset(itemdb_array, 0, sizeof(itemdb_array));
  1478. // read new data
  1479. itemdb_read();
  1480. cashshop_reloaddb();
  1481. //Epoque's awesome @reloaditemdb fix - thanks! [Ind]
  1482. //- Fixes the need of a @reloadmobdb after a @reloaditemdb to re-link monster drop data
  1483. for( i = 0; i < MAX_MOB_DB; i++ ) {
  1484. struct mob_db *entry;
  1485. if( !((i < 1324 || i > 1363) && (i < 1938 || i > 1946)) )
  1486. continue;
  1487. entry = mob_db(i);
  1488. for(d = 0; d < MAX_MOB_DROP; d++) {
  1489. struct item_data *id;
  1490. if( !entry->dropitem[d].nameid )
  1491. continue;
  1492. id = itemdb_search(entry->dropitem[d].nameid);
  1493. for (k = 0; k < MAX_SEARCH; k++) {
  1494. if (id->mob[k].chance <= entry->dropitem[d].p)
  1495. break;
  1496. }
  1497. if (k == MAX_SEARCH)
  1498. continue;
  1499. if (id->mob[k].id != i)
  1500. memmove(&id->mob[k+1], &id->mob[k], (MAX_SEARCH-k-1)*sizeof(id->mob[0]));
  1501. id->mob[k].chance = entry->dropitem[d].p;
  1502. id->mob[k].id = i;
  1503. }
  1504. }
  1505. // readjust itemdb pointer cache for each player
  1506. iter = mapit_geteachpc();
  1507. for( sd = (struct map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (struct map_session_data*)mapit_next(iter) ) {
  1508. memset(sd->item_delay, 0, sizeof(sd->item_delay)); // reset item delays
  1509. pc_setinventorydata(sd);
  1510. pc_check_available_item(sd); // Check for invalid(ated) items.
  1511. /* clear combo bonuses */
  1512. if( sd->combos.count ) {
  1513. aFree(sd->combos.bonus);
  1514. aFree(sd->combos.id);
  1515. sd->combos.bonus = NULL;
  1516. sd->combos.id = NULL;
  1517. sd->combos.count = 0;
  1518. if( pc_load_combo(sd) > 0 )
  1519. status_calc_pc(sd,0);
  1520. }
  1521. }
  1522. mapit_free(iter);
  1523. }
  1524. /**
  1525. * Finalizing Item DB
  1526. */
  1527. void do_final_itemdb(void) {
  1528. int i;
  1529. for( i = 0; i < ARRAYLENGTH(itemdb_array); ++i )
  1530. if( itemdb_array[i] )
  1531. destroy_item_data(itemdb_array[i], true);
  1532. for (i = 0; i < MAX_ITEMGROUP; i++) {
  1533. uint8 j;
  1534. if (!(&itemgroup_db[i]))
  1535. continue;
  1536. if (itemgroup_db[i].must_qty)
  1537. aFree(itemgroup_db[i].must);
  1538. for (j = 0; j < MAX_ITEMGROUP_RANDGROUP; j++) {
  1539. if (!(&itemgroup_db[i].random[j]) || !itemgroup_db[i].random[j].data_qty)
  1540. continue;
  1541. aFree(itemgroup_db[i].random[j].data);
  1542. }
  1543. }
  1544. itemdb_other->destroy(itemdb_other, itemdb_final_sub);
  1545. destroy_item_data(&dummy_item, false);
  1546. db_destroy(itemdb_combo);
  1547. }
  1548. /**
  1549. * Initializing Item DB
  1550. */
  1551. void do_init_itemdb(void) {
  1552. memset(itemdb_array, 0, sizeof(itemdb_array));
  1553. itemdb_other = idb_alloc(DB_OPT_BASE);
  1554. itemdb_combo = idb_alloc(DB_OPT_BASE);
  1555. create_dummy_data(); //Dummy data item.
  1556. itemdb_read();
  1557. }