diff options
Diffstat (limited to 'src/map/itemdb.c')
-rw-r--r-- | src/map/itemdb.c | 565 |
1 files changed, 199 insertions, 366 deletions
diff --git a/src/map/itemdb.c b/src/map/itemdb.c index fb6bcc730..efa9b18e5 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -4,28 +4,29 @@ #define HERCULES_CORE -#include "../config/core.h" // DBPATH, RENEWAL +#include "config/core.h" // DBPATH, RENEWAL #include "itemdb.h" +#include "map/battle.h" // struct battle_config +#include "map/map.h" +#include "map/mob.h" // MAX_MOB_DB +#include "map/pc.h" // W_MUSICAL, W_WHIP +#include "map/script.h" // item script processing +#include "common/HPM.h" +#include "common/conf.h" +#include "common/memmgr.h" +#include "common/nullpo.h" +#include "common/random.h" +#include "common/showmsg.h" +#include "common/strlib.h" +#include "common/utils.h" + #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "battle.h" // struct battle_config -#include "map.h" -#include "mob.h" // MAX_MOB_DB -#include "pc.h" // W_MUSICAL, W_WHIP -#include "script.h" // item script processing -#include "../common/HPM.h" -#include "../common/conf.h" -#include "../common/malloc.h" -#include "../common/nullpo.h" -#include "../common/random.h" -#include "../common/showmsg.h" -#include "../common/strlib.h" -#include "../common/utils.h" - struct itemdb_interface itemdb_s; +struct itemdb_interface *itemdb; /** * Search for item name @@ -75,7 +76,7 @@ struct item_data* itemdb_searchname(const char *str) { return item; //Second priority to Client displayed name. - if( strcasecmp(item->jname,str) == 0 ) + if (!item2 && strcasecmp(item->jname,str) == 0) item2 = item; } @@ -159,14 +160,14 @@ int itemdb_searchname_array(struct item_data** data, int size, const char *str, /* [Ind/Hercules] */ int itemdb_chain_item(unsigned short chain_id, int *rate) { struct item_chain_entry *entry; - + if( chain_id >= itemdb->chain_count ) { ShowError("itemdb_chain_item: unknown chain id %d\n", chain_id); return UNKNOWN_ITEM_ID; } - + entry = &itemdb->chains[chain_id].items[ rnd()%itemdb->chains[chain_id].qty ]; - + if( rnd()%10000 >= entry->rate ) return 0; @@ -177,44 +178,47 @@ int itemdb_chain_item(unsigned short chain_id, int *rate) { /* [Ind/Hercules] */ void itemdb_package_item(struct map_session_data *sd, struct item_package *package) { int i = 0, get_count, j, flag; - + for( i = 0; i < package->must_qty; i++ ) { struct item it; memset(&it, 0, sizeof(it)); it.nameid = package->must_items[i].id; it.identify = 1; - + if( package->must_items[i].hours ) { it.expire_time = (unsigned int)(time(NULL) + ((package->must_items[i].hours*60)*60)); } - + if( package->must_items[i].named ) { it.card[0] = CARD0_FORGE; it.card[1] = 0; it.card[2] = GetWord(sd->status.char_id, 0); it.card[3] = GetWord(sd->status.char_id, 1); } - + if( package->must_items[i].announce ) clif->package_announce(sd,package->must_items[i].id,package->id); - + + if ( package->must_items[i].force_serial ) + it.unique_id = itemdb->unique_id(sd); + get_count = itemdb->isstackable(package->must_items[i].id) ? package->must_items[i].qty : 1; - + it.amount = get_count == 1 ? 1 : get_count; - + for( j = 0; j < package->must_items[i].qty; j += get_count ) { if ( ( flag = pc->additem(sd, &it, get_count, LOG_TYPE_SCRIPT) ) ) clif->additem(sd, 0, 0, flag); } } - + if( package->random_qty ) { for( i = 0; i < package->random_qty; i++ ) { struct item_package_rand_entry *entry; - + entry = &package->random_groups[i].random_list[rnd()%package->random_groups[i].random_qty]; - + while( 1 ) { if( rnd()%10000 >= entry->rate ) { entry = entry->next; @@ -222,28 +226,28 @@ void itemdb_package_item(struct map_session_data *sd, struct item_package *packa } else { struct item it; memset(&it, 0, sizeof(it)); - + it.nameid = entry->id; it.identify = 1; - + if( entry->hours ) { it.expire_time = (unsigned int)(time(NULL) + ((entry->hours*60)*60)); } - + if( entry->named ) { it.card[0] = CARD0_FORGE; it.card[1] = 0; it.card[2] = GetWord(sd->status.char_id, 0); it.card[3] = GetWord(sd->status.char_id, 1); } - + if( entry->announce ) clif->package_announce(sd,entry->id,package->id); - + get_count = itemdb->isstackable(entry->id) ? entry->qty : 1; - + it.amount = get_count == 1 ? 1 : get_count; - + for( j = 0; j < entry->qty; j += get_count ) { if ( ( flag = pc->additem(sd, &it, get_count, LOG_TYPE_SCRIPT) ) ) clif->additem(sd, 0, 0, flag); @@ -253,9 +257,8 @@ void itemdb_package_item(struct map_session_data *sd, struct item_package *packa } } } - - return; } + /*========================================== * Return a random item id from group. (takes into account % chance giving/tot group) *------------------------------------------*/ @@ -263,17 +266,17 @@ int itemdb_searchrandomid(struct item_group *group) { if (group->qty) return group->nameid[rnd()%group->qty]; - + ShowError("itemdb_searchrandomid: No item entries for group id %d\n", group->id); return UNKNOWN_ITEM_ID; } bool itemdb_in_group(struct item_group *group, int nameid) { int i; - + for( i = 0; i < group->qty; i++ ) if( group->nameid[i] == nameid ) return true; - + return false; } @@ -518,7 +521,6 @@ int itemdb_isstackable2(struct item_data *data) } } - /*========================================== * Trade Restriction functions [Skotlex] *------------------------------------------*/ @@ -565,10 +567,10 @@ int itemdb_isrestricted(struct item* item, int gmlv, int gmlv2, int (*func)(stru if (!func(item_data, gmlv, gmlv2)) return 0; - + if(item_data->slot == 0 || itemdb_isspecial(item->card[0])) return 1; - + for(i = 0; i < item_data->slot; i++) { if (!item->card[i]) continue; if (!func(itemdb->search(item->card[i]), gmlv, gmlv2)) @@ -619,12 +621,12 @@ void itemdb_read_groups(void) { ShowError("can't read %s\n", config_filename); return; } - + gsize = aMalloc( libconfig->setting_length(item_group_conf.root) * sizeof(unsigned int) ); - + for(i = 0; i < libconfig->setting_length(item_group_conf.root); i++) gsize[i] = 0; - + i = 0; while( (itg = libconfig->setting_get_elem(item_group_conf.root,i++)) ) { const char *name = config_setting_name(itg); @@ -635,7 +637,7 @@ void itemdb_read_groups(void) { --i; continue; } - + c = 0; while( (it = libconfig->setting_get_elem(itg,c++)) ) { if( config_setting_is_list(it) ) @@ -643,19 +645,18 @@ void itemdb_read_groups(void) { else gsize[ i - 1 ] += 1; } - } - + i = 0; CREATE(itemdb->groups, struct item_group, libconfig->setting_length(item_group_conf.root)); itemdb->group_count = (unsigned short)libconfig->setting_length(item_group_conf.root); - + while( (itg = libconfig->setting_get_elem(item_group_conf.root,i++)) ) { struct item_data *data = itemdb->name2id(config_setting_name(itg)); int ecount = 0; - + data->group = &itemdb->groups[count]; - + itemdb->groups[count].id = data->nameid; itemdb->groups[count].qty = gsize[ count ]; @@ -668,13 +669,13 @@ void itemdb_read_groups(void) { repeat = libconfig->setting_get_int_elem(it,1); } else itname = libconfig->setting_get_string_elem(itg,c - 1); - + if( itname[0] == 'I' && itname[1] == 'D' && strlen(itname) < 8 ) { if( !( data = itemdb->exists(atoi(itname+2)) ) ) ShowWarning("itemdb_read_groups: unknown item ID '%d' in group '%s'!\n",atoi(itname+2),config_setting_name(itg)); } else if( !( data = itemdb->name2id(itname) ) ) ShowWarning("itemdb_read_groups: unknown item '%s' in group '%s'!\n",itname,config_setting_name(itg)); - + itemdb->groups[count].nameid[ecount] = data ? data->nameid : 0; if( repeat > 1 ) { //memset would be better? I failed to get the following to work though hu @@ -685,25 +686,24 @@ void itemdb_read_groups(void) { } ecount += repeat; } - count++; } - + libconfig->destroy(&item_group_conf); aFree(gsize); - ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, config_filename); } + /* [Ind/Hercules] - HCache for Packages */ void itemdb_write_cached_packages(const char *config_filename) { FILE *file; unsigned short pcount = itemdb->package_count; unsigned short i; - + if( !(file = HCache->open(config_filename,"wb")) ) { return; } - + // first 2 bytes = package count hwrite(&pcount,sizeof(pcount),1,file); @@ -719,7 +719,7 @@ void itemdb_write_cached_packages(const char *config_filename) { //now we loop into must for(c = 0; c < must_qty; c++) { struct item_package_must_entry *entry = &itemdb->packages[i].must_items[c]; - unsigned char announce = entry->announce == 1 ? 1 : 0, named = entry->named == 1 ? 1 : 0; + unsigned char announce = entry->announce == 1 ? 1 : 0, named = entry->named == 1 ? 1 : 0, force_serial = entry->force_serial == 1 ? 1 : 0; //first 2 byte = item id hwrite(&entry->id,sizeof(entry->id),1,file); //next 2 byte = qty @@ -729,19 +729,21 @@ void itemdb_write_cached_packages(const char *config_filename) { //next 1 byte = announce (1:0) hwrite(&announce,sizeof(announce),1,file); //next 1 byte = named (1:0) - hwrite(&named,sizeof(announce),1,file); + hwrite(&named,sizeof(named),1,file); + //next 1 byte = ForceSerial (1:0) + hwrite(&force_serial,sizeof(force_serial),1,file); } //now we loop into random groups for(c = 0; c < random_qty; c++) { struct item_package_rand_group *group = &itemdb->packages[i].random_groups[c]; unsigned short group_qty = group->random_qty, h; - + //next 2 bytes = how many entries in this group hwrite(&group_qty,sizeof(group_qty),1,file); //now we loop into the group's list for(h = 0; h < group_qty; h++) { struct item_package_rand_entry *entry = &itemdb->packages[i].random_groups[c].random_list[h]; - unsigned char announce = entry->announce == 1 ? 1 : 0, named = entry->named == 1 ? 1 : 0; + unsigned char announce = entry->announce == 1 ? 1 : 0, named = entry->named == 1 ? 1 : 0, force_serial = entry->force_serial == 1 ? 1 : 0; //first 2 byte = item id hwrite(&entry->id,sizeof(entry->id),1,file); //next 2 byte = qty @@ -753,13 +755,14 @@ void itemdb_write_cached_packages(const char *config_filename) { //next 1 byte = announce (1:0) hwrite(&announce,sizeof(announce),1,file); //next 1 byte = named (1:0) - hwrite(&named,sizeof(announce),1,file); + hwrite(&named,sizeof(named),1,file); + //next 1 byte = ForceSerial (1:0) + hwrite(&force_serial,sizeof(force_serial),1,file); } } } - fclose(file); - + return; } bool itemdb_read_cached_packages(const char *config_filename) { @@ -770,7 +773,7 @@ bool itemdb_read_cached_packages(const char *config_filename) { if( !(file = HCache->open(config_filename,"rb")) ) { return false; } - + // first 2 bytes = package count hread(&pcount,sizeof(pcount),1,file); @@ -782,32 +785,32 @@ bool itemdb_read_cached_packages(const char *config_filename) { struct item_data *pdata; struct item_package *package = &itemdb->packages[i]; unsigned short c; - + //into a package, first 2 bytes = id. hread(&id,sizeof(id),1,file); //next 2 bytes = must count hread(&must_qty,sizeof(must_qty),1,file); //next 2 bytes = random count hread(&random_qty,sizeof(random_qty),1,file); - + if( !(pdata = itemdb->exists(id)) ) ShowWarning("itemdb_read_cached_packages: unknown package item '%d', skipping..\n",id); else pdata->package = &itemdb->packages[i]; - + package->id = id; package->random_qty = random_qty; package->must_qty = must_qty; package->must_items = NULL; package->random_groups = NULL; - + if( package->must_qty ) { CREATE(package->must_items, struct item_package_must_entry, package->must_qty); //now we loop into must for(c = 0; c < package->must_qty; c++) { struct item_package_must_entry *entry = &itemdb->packages[i].must_items[c]; unsigned short mid = 0, qty = 0, hours = 0; - unsigned char announce = 0, named = 0; + unsigned char announce = 0, named = 0, force_serial = 0; struct item_data *data; //first 2 byte = item id hread(&mid,sizeof(mid),1,file); @@ -818,8 +821,10 @@ bool itemdb_read_cached_packages(const char *config_filename) { //next 1 byte = announce (1:0) hread(&announce,sizeof(announce),1,file); //next 1 byte = named (1:0) - hread(&named,sizeof(announce),1,file); - + hread(&named,sizeof(named),1,file); + //next 1 byte = ForceSerial (1:0) + hread(&force_serial,sizeof(force_serial),1,file); + if( !(data = itemdb->exists(mid)) ) ShowWarning("itemdb_read_cached_packages: unknown item '%d' in package '%s'!\n",mid,itemdb_name(package->id)); @@ -828,6 +833,7 @@ bool itemdb_read_cached_packages(const char *config_filename) { entry->qty = qty; entry->announce = announce ? 1 : 0; entry->named = named ? 1 : 0; + entry->force_serial = force_serial ? 1 : 0; } } if( package->random_qty ) { @@ -836,22 +842,22 @@ bool itemdb_read_cached_packages(const char *config_filename) { for(c = 0; c < package->random_qty; c++) { unsigned short group_qty = 0, h; struct item_package_rand_entry *prev = NULL; - + //next 2 bytes = how many entries in this group hread(&group_qty,sizeof(group_qty),1,file); - + package->random_groups[c].random_qty = group_qty; CREATE(package->random_groups[c].random_list, struct item_package_rand_entry, package->random_groups[c].random_qty); - + //now we loop into the group's list for(h = 0; h < group_qty; h++) { struct item_package_rand_entry *entry = &itemdb->packages[i].random_groups[c].random_list[h]; unsigned short mid = 0, qty = 0, hours = 0, rate = 0; - unsigned char announce = 0, named = 0; + unsigned char announce = 0, named = 0, force_serial = 0; struct item_data *data; if( prev ) prev->next = entry; - + //first 2 byte = item id hread(&mid,sizeof(mid),1,file); //next 2 byte = qty @@ -863,18 +869,20 @@ bool itemdb_read_cached_packages(const char *config_filename) { //next 1 byte = announce (1:0) hread(&announce,sizeof(announce),1,file); //next 1 byte = named (1:0) - hread(&named,sizeof(announce),1,file); - + hread(&named,sizeof(named),1,file); + //next 1 byte = ForceSerial (1:0) + hread(&force_serial,sizeof(force_serial),1,file); + if( !(data = itemdb->exists(mid)) ) ShowWarning("itemdb_read_cached_packages: unknown item '%d' in package '%s'!\n",mid,itemdb_name(package->id)); - + entry->id = data ? data->nameid : 0; entry->rate = rate; entry->hours = hours; entry->qty = qty; entry->announce = announce ? 1 : 0; entry->named = named ? 1 : 0; - + entry->force_serial = force_serial ? 1 : 0; prev = entry; } if( prev ) @@ -882,9 +890,7 @@ bool itemdb_read_cached_packages(const char *config_filename) { } } } - fclose(file); - ShowStatus("Done reading '"CL_WHITE"%hu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"' ("CL_GREEN"C"CL_RESET").\n", pcount, config_filename); return true; @@ -901,42 +907,41 @@ void itemdb_read_packages(void) { int i = 0, count = 0, c = 0, highest_gcount = 0; unsigned int *must = NULL, *random = NULL, *rgroup = NULL, **rgroups = NULL; struct item_package_rand_entry **prev = NULL; - + if( HCache->check(config_filename) ) { if( itemdb->read_cached_packages(config_filename) ) return; } - + if (libconfig->read_file(&item_packages_conf, config_filename)) { ShowError("can't read %s\n", config_filename); return; } - + must = aMalloc( libconfig->setting_length(item_packages_conf.root) * sizeof(unsigned int) ); random = aMalloc( libconfig->setting_length(item_packages_conf.root) * sizeof(unsigned int) ); rgroup = aMalloc( libconfig->setting_length(item_packages_conf.root) * sizeof(unsigned int) ); rgroups = aMalloc( libconfig->setting_length(item_packages_conf.root) * sizeof(unsigned int *) ); - for(i = 0; i < libconfig->setting_length(item_packages_conf.root); i++) { must[i] = 0; random[i] = 0; rgroup[i] = 0; rgroups[i] = NULL; } - + /* validate tree, drop poisonous fruits! */ i = 0; while( (itg = libconfig->setting_get_elem(item_packages_conf.root,i++)) ) { const char *name = config_setting_name(itg); - + if( !itemdb->name2id(name) ) { ShowWarning("itemdb_read_packages: unknown package item '%s', skipping..\n",name); libconfig->setting_remove(item_packages_conf.root, name); --i; continue; } - + c = 0; while( (it = libconfig->setting_get_elem(itg,c++)) ) { int rval = 0; @@ -946,7 +951,7 @@ void itemdb_read_packages(void) { --c; continue; } - + if( rval == 0 ) must[ i - 1 ] += 1; else { @@ -963,7 +968,7 @@ void itemdb_read_packages(void) { for(i = 0; i < highest_gcount; i++) { prev[i] = NULL; } - + for(i = 0; i < libconfig->setting_length(item_packages_conf.root); i++ ) { rgroups[i] = aMalloc( rgroup[i] * sizeof(unsigned int) ); for( c = 0; c < rgroup[i]; c++ ) { @@ -977,33 +982,33 @@ void itemdb_read_packages(void) { c = 0; while( (it = libconfig->setting_get_elem(itg,c++)) ) { int rval = 0; - if( ( t = libconfig->setting_get_member(it, "Random")) && ( rval = libconfig->setting_get_int(t) ) > 0 ) { + if ((t = libconfig->setting_get_member(it, "Random")) != NULL && (rval = libconfig->setting_get_int(t)) > 0) { rgroups[i - 1][rval - 1] += 1; } } } - + CREATE(itemdb->packages, struct item_package, libconfig->setting_length(item_packages_conf.root)); itemdb->package_count = (unsigned short)libconfig->setting_length(item_packages_conf.root); - + /* write */ i = 0; while( (itg = libconfig->setting_get_elem(item_packages_conf.root,i++)) ) { struct item_data *data = itemdb->name2id(config_setting_name(itg)); int r = 0, m = 0; - + for(r = 0; r < highest_gcount; r++) { prev[r] = NULL; } - + data->package = &itemdb->packages[count]; - + itemdb->packages[count].id = data->nameid; itemdb->packages[count].random_groups = NULL; itemdb->packages[count].must_items = NULL; itemdb->packages[count].random_qty = rgroup[ i - 1 ]; itemdb->packages[count].must_qty = must[ i - 1 ]; - + if( itemdb->packages[count].random_qty ) { CREATE(itemdb->packages[count].random_groups, struct item_package_rand_group, itemdb->packages[count].random_qty); for( c = 0; c < itemdb->packages[count].random_qty; c++ ) { @@ -1016,14 +1021,14 @@ void itemdb_read_packages(void) { } if( itemdb->packages[count].must_qty ) CREATE(itemdb->packages[count].must_items, struct item_package_must_entry, itemdb->packages[count].must_qty); - + c = 0; while( (it = libconfig->setting_get_elem(itg,c++)) ) { int icount = 1, expire = 0, rate = 10000, gid = 0; - bool announce = false, named = false; - + bool announce = false, named = false, force_serial = false; + itname = config_setting_name(it); - + if( itname[0] == 'I' && itname[1] == 'D' && strlen(itname) < 8 ) { if( !( data = itemdb->exists(atoi(itname+2)) ) ) ShowWarning("itemdb_read_packages: unknown item ID '%d' in package '%s'!\n",atoi(itname+2),config_setting_name(itg)); @@ -1032,10 +1037,10 @@ void itemdb_read_packages(void) { if( ( t = libconfig->setting_get_member(it, "Count")) ) icount = libconfig->setting_get_int(t); - + if( ( t = libconfig->setting_get_member(it, "Expire")) ) expire = libconfig->setting_get_int(t); - + if( ( t = libconfig->setting_get_member(it, "Rate")) ) { if( (rate = (unsigned short)libconfig->setting_get_int(t)) > 10000 ) { ShowWarning("itemdb_read_packages: invalid rate (%d) for item '%s' in package '%s'!\n",rate,itname,config_setting_name(itg)); @@ -1048,7 +1053,10 @@ void itemdb_read_packages(void) { if( ( t = libconfig->setting_get_member(it, "Named")) && libconfig->setting_get_bool(t) ) named = true; - + + if( ( t = libconfig->setting_get_member(it, "ForceSerial")) && libconfig->setting_get_bool(t) ) + force_serial = true; + if( !( t = libconfig->setting_get_member(it, "Random") ) ) { ShowWarning("itemdb_read_packages: missing 'Random' field for item '%s' in package '%s', defaulting to must!\n",itname,config_setting_name(itg)); gid = 0; @@ -1061,15 +1069,16 @@ void itemdb_read_packages(void) { itemdb->packages[count].must_items[m].hours = expire; itemdb->packages[count].must_items[m].announce = announce == true ? 1 : 0; itemdb->packages[count].must_items[m].named = named == true ? 1 : 0; + itemdb->packages[count].must_items[m].force_serial = force_serial == true ? 1 : 0; m++; } else { int gidx = gid - 1; - + r = itemdb->packages[count].random_groups[gidx].random_qty; - + if( prev[gidx] ) prev[gidx]->next = &itemdb->packages[count].random_groups[gidx].random_list[r]; - + itemdb->packages[count].random_groups[gidx].random_list[r].id = data ? data->nameid : 0; itemdb->packages[count].random_groups[gidx].random_list[r].qty = icount; if( (itemdb->packages[count].random_groups[gidx].random_list[r].rate = rate) == 10000 ) { @@ -1078,18 +1087,18 @@ void itemdb_read_packages(void) { itemdb->packages[count].random_groups[gidx].random_list[r].hours = expire; itemdb->packages[count].random_groups[gidx].random_list[r].announce = announce == true ? 1 : 0; itemdb->packages[count].random_groups[gidx].random_list[r].named = named == true ? 1 : 0; + itemdb->packages[count].random_groups[gidx].random_list[r].force_serial = force_serial == true ? 1 : 0; itemdb->packages[count].random_groups[gidx].random_qty += 1; - + prev[gidx] = &itemdb->packages[count].random_groups[gidx].random_list[r]; } - } - + for(r = 0; r < highest_gcount; r++) { if( prev[r] ) prev[r]->next = &itemdb->packages[count].random_groups[r].random_list[0]; } - + for( r = 0; r < itemdb->packages[count].random_qty; r++ ) { if( itemdb->packages[count].random_groups[r].random_qty == 1 ) { //item packages don't stop looping until something comes out of them, so if you have only one item in it the drop is guaranteed. @@ -1098,11 +1107,9 @@ void itemdb_read_packages(void) { itemdb->packages[count].random_groups[r].random_list[0].rate = 10000; } } - count++; } - - + aFree(must); aFree(random); for(i = 0; i < libconfig->setting_length(item_packages_conf.root); i++ ) { @@ -1111,12 +1118,12 @@ void itemdb_read_packages(void) { aFree(rgroups); aFree(rgroup); aFree(prev); - + libconfig->destroy(&item_packages_conf); if( HCache->enabled ) itemdb->write_cached_packages(config_filename); - + ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, config_filename); } @@ -1129,7 +1136,7 @@ void itemdb_read_chains(void) { const char *config_filename = "db/pre-re/item_chain.conf"; // FIXME hardcoded name #endif int i = 0, count = 0; - + if (libconfig->read_file(&item_chain_conf, config_filename)) { ShowError("can't read %s\n", config_filename); return; @@ -1137,7 +1144,7 @@ void itemdb_read_chains(void) { CREATE(itemdb->chains, struct item_chain, libconfig->setting_length(item_chain_conf.root)); itemdb->chain_count = (unsigned short)libconfig->setting_length(item_chain_conf.root); - + #ifdef ENABLE_CASE_CHECK script->parser_current_file = config_filename; #endif // ENABLE_CASE_CHECK @@ -1147,12 +1154,12 @@ void itemdb_read_chains(void) { const char *name = config_setting_name(itc); int c = 0; config_setting_t *entry = NULL; - + script->set_constant2(name,i-1,0); itemdb->chains[count].qty = (unsigned short)libconfig->setting_length(itc); - + CREATE(itemdb->chains[count].items, struct item_chain_entry, libconfig->setting_length(itc)); - + while( (entry = libconfig->setting_get_elem(itc,c++)) ) { const char *itname = config_setting_name(entry); if( itname[0] == 'I' && itname[1] == 'D' && strlen(itname) < 8 ) { @@ -1160,32 +1167,32 @@ void itemdb_read_chains(void) { ShowWarning("itemdb_read_chains: unknown item ID '%d' in chain '%s'!\n",atoi(itname+2),name); } else if( !( data = itemdb->name2id(itname) ) ) ShowWarning("itemdb_read_chains: unknown item '%s' in chain '%s'!\n",itname,name); - + if( prev ) prev->next = &itemdb->chains[count].items[c - 1]; - + itemdb->chains[count].items[c - 1].id = data ? data->nameid : 0; itemdb->chains[count].items[c - 1].rate = data ? libconfig->setting_get_int(entry) : 0; - + prev = &itemdb->chains[count].items[c - 1]; } - + if( prev ) prev->next = &itemdb->chains[count].items[0]; - + count++; } #ifdef ENABLE_CASE_CHECK script->parser_current_file = NULL; #endif // ENABLE_CASE_CHECK - + libconfig->destroy(&item_chain_conf); - + if( !script->get_constant("ITMCHAIN_ORE",&i) ) ShowWarning("itemdb_read_chains: failed to find 'ITMCHAIN_ORE' chain to link to cache!\n"); else itemdb->chain_cache[ECC_ORE] = i; - + ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, config_filename); } @@ -1194,21 +1201,19 @@ void itemdb_read_chains(void) { **/ int itemdb_combo_split_atoi (char *str, int *val) { int i; - + for (i=0; i<MAX_ITEMS_PER_COMBO; i++) { if (!str) break; - + val[i] = atoi(str); - str = strchr(str,':'); - if (str) *str++=0; } - + if( i == 0 ) //No data found. return 0; - + return i; } /** @@ -1217,41 +1222,38 @@ int itemdb_combo_split_atoi (char *str, int *val) { void itemdb_read_combos() { uint32 lines = 0, count = 0; char line[1024]; - char filepath[256]; FILE* fp; - + sprintf(filepath, "%s/%s", map->db_path, DBPATH"item_combo_db.txt"); - + if ((fp = fopen(filepath, "r")) == NULL) { ShowError("itemdb_read_combos: File not found \"%s\".\n", filepath); return; } - + // process rows one by one while(fgets(line, sizeof(line), fp)) { char *str[2], *p; - + lines++; if (line[0] == '/' && line[1] == '/') continue; - + memset(str, 0, sizeof(str)); - + p = line; - p = trim(p); - if (*p == '\0') continue;// empty line - + if (!strchr(p,',')) { /* is there even a single column? */ ShowError("itemdb_read_combos: Insufficient columns in line %d of \"%s\", skipping.\n", lines, filepath); continue; } - + str[0] = p; p = strchr(p,','); *p = '\0'; @@ -1260,12 +1262,12 @@ void itemdb_read_combos() { str[1] = p; p = strchr(p,','); p++; - + if (str[1][0] != '{') { ShowError("itemdb_read_combos(#1): Invalid format (Script column) in line %d of \"%s\", skipping.\n", lines, filepath); continue; } - + /* no ending key anywhere (missing \}\) */ if ( str[1][strlen(str[1])-1] != '}' ) { ShowError("itemdb_read_combos(#2): Invalid format (Script column) in line %d of \"%s\", skipping.\n", lines, filepath); @@ -1274,12 +1276,12 @@ void itemdb_read_combos() { int items[MAX_ITEMS_PER_COMBO]; int v = 0, retcount = 0; struct item_combo *combo = NULL; - + if((retcount = itemdb->combo_split_atoi(str[0], items)) < 2) { ShowError("itemdb_read_combos: line %d of \"%s\" doesn't have enough items to make for a combo (min:2), skipping.\n", lines, filepath); continue; } - + /* validate */ for(v = 0; v < retcount; v++) { if( !itemdb->exists(items[v]) ) { @@ -1290,11 +1292,11 @@ void itemdb_read_combos() { /* failed at some item */ if( v < retcount ) continue; - + RECREATE(itemdb->combos, struct item_combo*, ++itemdb->combo_count); - + CREATE(combo, struct item_combo, 1); - + combo->count = retcount; combo->script = script->parse(str[1], filepath, lines, 0, NULL); combo->id = itemdb->combo_count - 1; @@ -1302,37 +1304,28 @@ void itemdb_read_combos() { for( v = 0; v < retcount; v++ ) { combo->nameid[v] = items[v]; } - + itemdb->combos[itemdb->combo_count - 1] = combo; - + /* populate the items to refer to this combo */ for( v = 0; v < retcount; v++ ) { struct item_data * it; int index; - + it = itemdb->exists(items[v]); - index = it->combos_count; - RECREATE(it->combos, struct item_combo*, ++it->combos_count); - it->combos[index] = combo; } - } - count++; } - fclose(fp); - ShowStatus("Done reading '"CL_WHITE"%"PRIu32""CL_RESET"' entries in '"CL_WHITE"item_combo_db"CL_RESET"'.\n", count); - + return; } - - /*====================================== * Applies gender restrictions according to settings. [Skotlex] *======================================*/ @@ -1480,7 +1473,7 @@ int itemdb_validate_entry(struct item_data *entry, int n, const char *source) { entry->flag.available = 1; entry->view_id = 0; } - + entry->sex = itemdb->gendercheck(entry); //Apply gender filtering. // Validated. Finally insert it @@ -1509,111 +1502,8 @@ void itemdb_readdb_additional_fields(int itemid, config_setting_t *it, int n, co } /** - * Processes one itemdb entry from the sql backend, loading and inserting it - * into the item database. - * - * @param *handle MySQL connection handle. It is expected to have data - * available (i.e. already queried) and it won't be freed (it - * is care of the caller to do so) - * @param n Ordinal number of the entry, to be displayed in case of - * validation errors. - * @param *source Source of the entry (table name), to be displayed in case of - * validation errors. - * @return Nameid of the validated entry, or 0 in case of failure. - */ -int itemdb_readdb_sql_sub(Sql *handle, int n, const char *source) { - struct item_data id = { 0 }; - char *data = NULL; - - /* - * `id` smallint(5) unsigned NOT NULL DEFAULT '0' - * `name_english` varchar(50) NOT NULL DEFAULT '' - * `name_japanese` varchar(50) NOT NULL DEFAULT '' - * `type` tinyint(2) unsigned NOT NULL DEFAULT '0' - * `price_buy` mediumint(10) DEFAULT NULL - * `price_sell` mediumint(10) DEFAULT NULL - * `weight` smallint(5) unsigned DEFAULT NULL - * `atk` smallint(5) unsigned DEFAULT NULL - * `matk` smallint(5) unsigned DEFAULT NULL - * `defence` smallint(5) unsigned DEFAULT NULL - * `range` tinyint(2) unsigned DEFAULT NULL - * `slots` tinyint(2) unsigned DEFAULT NULL - * `equip_jobs` int(12) unsigned DEFAULT NULL - * `equip_upper` tinyint(8) unsigned DEFAULT NULL - * `equip_genders` tinyint(2) unsigned DEFAULT NULL - * `equip_locations` smallint(4) unsigned DEFAULT NULL - * `weapon_level` tinyint(2) unsigned DEFAULT NULL - * `equip_level_min` smallint(5) unsigned DEFAULT NULL - * `equip_level_max` smallint(5) unsigned DEFAULT NULL - * `refineable` tinyint(1) unsigned DEFAULT NULL - * `view` smallint(3) unsigned DEFAULT NULL - * `bindonequip` tinyint(1) unsigned DEFAULT NULL - * `buyingstore` tinyint(1) NOT NULL DEFAULT NULL - * `delay` mediumint(9) NOT NULL DEFAULT NULL - * `trade_flag` smallint(4) NOT NULL DEFAULT NULL - * `trade_group` smallint(4) NOT NULL DEFAULT NULL - * `nouse_flag` smallint(4) NOT NULL DEFAULT NULL - * `nouse_group` smallint(4) NOT NULL DEFAULT NULL - * `stack_amount` mediumint(6) NOT NULL DEFAULT NULL - * `stack_flag` smallint(2) NOT NULL DEFAULT NULL - * `sprite` mediumint(6) NOT NULL DEFAULT NULL - * `script` text - * `equip_script` text - * `unequip_script` text - */ - SQL->GetData(handle, 0, &data, NULL); id.nameid = (uint16)atoi(data); - SQL->GetData(handle, 1, &data, NULL); safestrncpy(id.name, data, sizeof(id.name)); - SQL->GetData(handle, 2, &data, NULL); safestrncpy(id.jname, data, sizeof(id.jname)); - SQL->GetData(handle, 3, &data, NULL); id.type = atoi(data); - SQL->GetData(handle, 4, &data, NULL); id.value_buy = data ? atoi(data) : -1; // Using invalid price -1 when missing, it'll be validated later - SQL->GetData(handle, 5, &data, NULL); id.value_sell = data ? atoi(data) : -1; - SQL->GetData(handle, 6, &data, NULL); id.weight = data ? atoi(data) : 0; - SQL->GetData(handle, 7, &data, NULL); id.atk = data ? atoi(data) : 0; - SQL->GetData(handle, 8, &data, NULL); id.matk = data ? atoi(data) : 0; - SQL->GetData(handle, 9, &data, NULL); id.def = data ? atoi(data) : 0; - SQL->GetData(handle, 10, &data, NULL); id.range = data ? atoi(data) : 0; - SQL->GetData(handle, 11, &data, NULL); id.slot = data ? atoi(data) : 0; - SQL->GetData(handle, 12, &data, NULL); itemdb->jobid2mapid(id.class_base, data ? (unsigned int)strtoul(data,NULL,0) : UINT_MAX); - SQL->GetData(handle, 13, &data, NULL); id.class_upper = data ? (unsigned int)atoi(data) : ITEMUPPER_ALL; - SQL->GetData(handle, 14, &data, NULL); id.sex = data ? atoi(data) : 2; - SQL->GetData(handle, 15, &data, NULL); id.equip = data ? atoi(data) : 0; - SQL->GetData(handle, 16, &data, NULL); id.wlv = data ? atoi(data) : 0; - SQL->GetData(handle, 17, &data, NULL); id.elv = data ? atoi(data) : 0; - SQL->GetData(handle, 18, &data, NULL); id.elvmax = data ? atoi(data) : 0; - SQL->GetData(handle, 19, &data, NULL); id.flag.no_refine = data && atoi(data) ? 0 : 1; - SQL->GetData(handle, 20, &data, NULL); id.look = data ? atoi(data) : 0; - SQL->GetData(handle, 21, &data, NULL); id.flag.bindonequip = data && atoi(data) ? 1 : 0; - SQL->GetData(handle, 22, &data, NULL); id.flag.buyingstore = data && atoi(data) ? 1 : 0; - SQL->GetData(handle, 23, &data, NULL); id.delay = data ? atoi(data) : 0; - SQL->GetData(handle, 24, &data, NULL); id.flag.trade_restriction = data ? atoi(data) : ITR_NONE; - SQL->GetData(handle, 25, &data, NULL); id.gm_lv_trade_override = data ? atoi(data) : 0; - SQL->GetData(handle, 26, &data, NULL); id.item_usage.flag = data ? atoi(data) : INR_NONE; - SQL->GetData(handle, 27, &data, NULL); id.item_usage.override = data ? atoi(data) : 0; - SQL->GetData(handle, 28, &data, NULL); id.stack.amount = data ? atoi(data) : 0; - SQL->GetData(handle, 29, &data, NULL); - if (data) { - int stack_flag = atoi(data); - id.stack.inventory = (stack_flag&1)!=0; - id.stack.cart = (stack_flag&2)!=0; - id.stack.storage = (stack_flag&4)!=0; - id.stack.guildstorage = (stack_flag&8)!=0; - } - SQL->GetData(handle, 30, &data, NULL); - if (data) { - id.view_id = atoi(data); - if (id.view_id) - id.flag.available = 1; - } - SQL->GetData(handle, 31, &data, NULL); id.script = data && *data ? script->parse(data, source, -id.nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL; - SQL->GetData(handle, 32, &data, NULL); id.equip_script = data && *data ? script->parse(data, source, -id.nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL; - SQL->GetData(handle, 33, &data, NULL); id.unequip_script = data && *data ? script->parse(data, source, -id.nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL; - - return itemdb->validate_entry(&id, n, source); -} - -/** - * Processes one itemdb entry from the sql backend, loading and inserting it - * into the item database. + * Processes one itemdb entry from the libconfig backend, loading and inserting + * it into the item database. * * @param *it Libconfig setting entry. It is expected to be valid and it * won't be freed (it is care of the caller to do so if @@ -1657,6 +1547,7 @@ int itemdb_readdb_libconfig_sub(config_setting_t *it, int n, const char *source) * BindOnEquip: (true or false) * BuyingStore: (true or false) * Delay: Delay to use item + * ForceSerial: (true or false) * Trade: { * override: Group to override * nodrop: (true or false) @@ -1790,7 +1681,10 @@ int itemdb_readdb_libconfig_sub(config_setting_t *it, int n, const char *source) if( (t = libconfig->setting_get_member(it, "BindOnEquip")) ) id.flag.bindonequip = libconfig->setting_get_bool(t) ? 1 : 0; - + + if( (t = libconfig->setting_get_member(it, "ForceSerial")) ) + id.flag.force_serial = libconfig->setting_get_bool(t) ? 1 : 0; + if ( (t = libconfig->setting_get_member(it, "BuyingStore")) ) id.flag.buyingstore = libconfig->setting_get_bool(t) ? 1 : 0; @@ -1903,7 +1797,7 @@ int itemdb_readdb_libconfig_sub(config_setting_t *it, int n, const char *source) id.flag.available = 1; id.view_id = i32; } - + if( libconfig->setting_lookup_string(it, "Script", &str) ) id.script = *str ? script->parse(str, source, -id.nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL; @@ -1947,7 +1841,7 @@ int itemdb_readdb_libconfig(const char *filename) { config_setting_t *itdb, *it; char filepath[256]; int i = 0, count = 0; - + sprintf(filepath, "%s/%s", map->db_path, filename); memset(&duplicate,0,sizeof(duplicate)); if( libconfig->read_file(&item_db_conf, filepath) || !(itdb = libconfig->setting_get_member(item_db_conf.root, "item_db")) ) { @@ -1972,45 +1866,6 @@ int itemdb_readdb_libconfig(const char *filename) { } libconfig->destroy(&item_db_conf); ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, filename); - - return count; -} - -/** - * Reads from a sql itemdb table and inserts the found entries into the item - * database, overwriting duplicate ones (i.e. item_db2 overriding item_db.) - * - * @param *tablename Table name to query. - * @return The number of found entries. - */ -int itemdb_readdb_sql(const char *tablename) { - int i = 0, count = 0; - - // retrieve all rows from the item database - if( SQL_ERROR == SQL->Query(map->mysql_handle, "SELECT `id`, `name_english`, `name_japanese`, `type`," - " `price_buy`, `price_sell`, `weight`, `atk`," - " `matk`, `defence`, `range`, `slots`," - " `equip_jobs`, `equip_upper`, `equip_genders`, `equip_locations`," - " `weapon_level`, `equip_level_min`, `equip_level_max`, `refineable`," - " `view`, `bindonequip`, `buyingstore`, `delay`," - " `trade_flag`, `trade_group`, `nouse_flag`, `nouse_group`," - " `stack_amount`, `stack_flag`, `sprite`, `script`," - " `equip_script`, `unequip_script`" - "FROM `%s`", tablename) ) { - Sql_ShowDebug(map->mysql_handle); - return 0; - } - - // process rows one by one - while( SQL_SUCCESS == SQL->NextRow(map->mysql_handle) ) { - if( itemdb->readdb_sql_sub(map->mysql_handle, i++, tablename) ) - count++; - } - - // free the query result - SQL->FreeResult(map->mysql_handle); - - ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, tablename); return count; } @@ -2030,24 +1885,14 @@ uint64 itemdb_unique_id(struct map_session_data *sd) { void itemdb_read(bool minimal) { int i; DBData prev; - - if (map->db_use_sql_item_db) { - const char* item_db_name[] = { - map->item_db_db, - map->item_db2_db - }; - for(i = 0; i < ARRAYLENGTH(item_db_name); i++) - itemdb->readdb_sql(item_db_name[i]); - } else { - const char* filename[] = { - DBPATH"item_db.conf", - "item_db2.conf", - }; - for(i = 0; i < ARRAYLENGTH(filename); i++) - itemdb->readdb_libconfig(filename[i]); - } - + const char *filename[] = { + DBPATH"item_db.conf", + "item_db2.conf", + }; + for (i = 0; i < ARRAYLENGTH(filename); i++) + itemdb->readdb_libconfig(filename[i]); + for( i = 0; i < ARRAYLENGTH(itemdb->array); ++i ) { if( itemdb->array[i] ) { if( itemdb->names->put(itemdb->names,DB->str2key(itemdb->array[i]->name),DB->ptr2data(itemdb->array[i]),&prev) ) { @@ -2056,7 +1901,7 @@ void itemdb_read(bool minimal) { } } } - + if (minimal) return; @@ -2102,17 +1947,7 @@ void destroy_item_data(struct item_data* self, int free_self) script->free_code(self->unequip_script); if( self->combos ) aFree(self->combos); - if (self->hdata) - { - int i; - for (i = 0; i < self->hdatac; i++ ) { - if (self->hdata[i]->flag.free ) { - aFree(self->hdata[i]->data); - } - aFree(self->hdata[i]); - } - aFree(self->hdata); - } + HPM->data_store_destroy(&self->hdata); #if defined(DEBUG) // trash item memset(self, 0xDD, sizeof(struct item_data)); @@ -2141,7 +1976,7 @@ void itemdb_clear(bool total) { if( itemdb->array[i] ) itemdb->destroy_item_data(itemdb->array[i], 1); } - + if( itemdb->groups ) { for( i = 0; i < itemdb->group_count; i++ ) { @@ -2153,7 +1988,7 @@ void itemdb_clear(bool total) { itemdb->groups = NULL; itemdb->group_count = 0; - + if (itemdb->chains) { for (i = 0; i < itemdb->chain_count; i++) { if (itemdb->chains[i].items) @@ -2164,7 +1999,7 @@ void itemdb_clear(bool total) { itemdb->chains = NULL; itemdb->chain_count = 0; - + if (itemdb->packages) { for (i = 0; i < itemdb->package_count; i++) { if (itemdb->packages[i].random_groups) { @@ -2180,7 +2015,7 @@ void itemdb_clear(bool total) { itemdb->packages = NULL; } itemdb->package_count = 0; - + if (itemdb->combos) { for (i = 0; i < itemdb->combo_count; i++) { if (itemdb->combos[i]->script) // Check if script was loaded @@ -2192,28 +2027,28 @@ void itemdb_clear(bool total) { itemdb->combos = NULL; itemdb->combo_count = 0; - + if (total) return; - + itemdb->other->clear(itemdb->other, itemdb->final_sub); - + memset(itemdb->array, 0, sizeof(itemdb->array)); - - db_clear(itemdb->names); + db_clear(itemdb->names); } + void itemdb_reload(void) { struct s_mapiterator* iter; struct map_session_data* sd; int i,d,k; - + itemdb->clear(false); // read new data itemdb->read(false); - + //Epoque's awesome @reloaditemdb fix - thanks! [Ind] //- Fixes the need of a @reloadmobdb after a @reloaditemdb to re-link monster drop data for( i = 0; i < MAX_MOB_DB; i++ ) { @@ -2234,7 +2069,7 @@ void itemdb_reload(void) { if (k == MAX_SEARCH) continue; - + if (id->mob[k].id != i && k != MAX_SEARCH - 1) memmove(&id->mob[k+1], &id->mob[k], (MAX_SEARCH-k-1)*sizeof(id->mob[0])); id->mob[k].chance = entry->dropitem[d].p; @@ -2264,7 +2099,7 @@ void itemdb_reload(void) { void itemdb_name_constants(void) { DBIterator *iter = db_iterator(itemdb->names); struct item_data *data; - + #ifdef ENABLE_CASE_CHECK script->parser_current_file = "Item Database (Likely an invalid or conflicting AegisName)"; #endif // ENABLE_CASE_CHECK @@ -2278,7 +2113,7 @@ void itemdb_name_constants(void) { } void do_final_itemdb(void) { itemdb->clear(true); - + itemdb->other->destroy(itemdb->other, itemdb->final_sub); itemdb->destroy_item_data(&itemdb->dummy, 0); db_destroy(itemdb->names); @@ -2295,14 +2130,14 @@ void do_init_itemdb(bool minimal) { return; clif->cashshop_load(); - + /** it failed? we disable it **/ if( !clif->parse_roulette_db() ) battle_config.feature_roulette = 0; } void itemdb_defaults(void) { itemdb = &itemdb_s; - + itemdb->init = do_init_itemdb; itemdb->final = do_final_itemdb; itemdb->reload = itemdb_reload; @@ -2371,10 +2206,8 @@ void itemdb_defaults(void) { itemdb->gendercheck = itemdb_gendercheck; itemdb->validate_entry = itemdb_validate_entry; itemdb->readdb_additional_fields = itemdb_readdb_additional_fields; - itemdb->readdb_sql_sub = itemdb_readdb_sql_sub; itemdb->readdb_libconfig_sub = itemdb_readdb_libconfig_sub; itemdb->readdb_libconfig = itemdb_readdb_libconfig; - itemdb->readdb_sql = itemdb_readdb_sql; itemdb->unique_id = itemdb_unique_id; itemdb->read = itemdb_read; itemdb->destroy_item_data = destroy_item_data; |