Iâve replaced the if/else block with a std::map<string, iuse::(âŚ)>, [edit: what Kevin said]
It is better cause you can add tags/iuse:: from different mods without having it all hardcoded in one place, you could have the default tag/pointers and then add some from lua or something.
Also, made a std::map to load items based on category (it_comest, it_book, etc).
I don't suppose you can send me a pull request
hmmm, I dont know how to do that.
patch -p1 < âgit diffâ should apply the changes.
diff --git a/item_manager.cpp b/item_manager.cpp
index fda5acf..edfa9cf 100644
--- a/item_manager.cpp
+++ b/item_manager.cpp
@@ -16,6 +16,14 @@ Item_manager::Item_manager(){
m_missing_item->description = "Error: No item template of this type.";
m_templates["MISSING_ITEM"]=m_missing_item;
load_item_templates();
+
+ categoryLoaders.insert(CATLOAD_MAP::value_type("comestible", &load_comest));
+ categoryLoaders.insert(CATLOAD_MAP::value_type("ammo", &load_ammo));
+ categoryLoaders.insert(CATLOAD_MAP::value_type("armor", &load_armor));
+ categoryLoaders.insert(CATLOAD_MAP::value_type("gun", &load_gun));
+ categoryLoaders.insert(CATLOAD_MAP::value_type("book", &load_book));
+ categoryLoaders.insert(CATLOAD_MAP::value_type("tool", &load_tool));
+ categoryLoaders.insert(CATLOAD_MAP::value_type("container", &load_container));
}
void Item_manager::init(){
@@ -186,23 +194,17 @@ void Item_manager::load_item_templates_from(const std::string file_name){
if(m_templates.find(new_id) != m_templates.end()){
std::cerr << "Item definition skipped, id " << new_id << " already exists." << std::endl;
} else {
- item_template* new_item_template = new itype();
- new_item_template->id = new_id;
- m_templates[new_id] = new_item_template;
-
- // And then proceed to assign the correct field
- new_item_template->rarity = int_from_json(new_id, "rarity", entry_body);
- new_item_template->name = string_from_json(new_id, "name", entry_body);
- new_item_template->sym = char_from_json(new_id, "symbol", entry_body);
- new_item_template->color = color_from_json(new_id, "color", entry_body);
- new_item_template->description = string_from_json(new_id, "description", entry_body);
- new_item_template->m1 = materials_from_json(new_id, "material", entry_body)[0];
- new_item_template->m2 = materials_from_json(new_id, "material", entry_body)[1];
- new_item_template->volume = int_from_json(new_id, "volume", entry_body);
- new_item_template->weight = int_from_json(new_id, "weight", entry_body);
- new_item_template->melee_dam = int_from_json(new_id, "damage", entry_body);
- new_item_template->melee_cut = int_from_json(new_id, "cutting", entry_body);
- new_item_template->m_to_hit = int_from_json(new_id, "to_hit", entry_body);
+ item_template* new_item_template = 0;
+ std::string category = string_from_json(new_id, "category", entry_body);
+ CATLOAD_MAP::iterator it = categoryLoaders.find(category);
+ if (it != categoryLoaders.end())
+ new_item_template = (*it).second(new_id,entry_body);
+ else
+ new_item_template = load_default(new_id, entry_body);
+
+ //new_item_template->id = new_id;
+ if (new_item_template) // if json entry has errors, skip ?
+ m_templates[new_id] = new_item_template;
}
}
}
@@ -343,3 +345,217 @@ material Item_manager::material_from_tag(item_tag new_id, item_tag name){
return MNULL;
}
}
+
+Item_manager::IUSE_FUNCTIONPOINTER Item_manager::iuseFromString(const std::string& iuseName)
+{
+ IUSE_MAP::iterator it = iuseMapping.find(iuseName);
+ if (it != iuseMapping.end())
+ return (*it).second;
+
+ return &iuse::none;
+}
+
+void Item_manager::addIUseMapping(const std::string& iuseName, IUSE_FUNCTIONPOINTER func)
+{
+ iuseMapping.insert(IUSE_MAP::value_type(iuseName,func));
+}
+
+void Item_manager::setDefaultIUseMappings()
+{
+ iuseMapping.clear();
+ addIUseMapping("SEWAGE", &iuse::sewage);
+ addIUseMapping("HONEYCOMB", &iuse::honeycomb);
+ addIUseMapping("ROYAL_JELLY", &iuse::royal_jelly);
+ addIUseMapping("BANDAGE", &iuse::bandage);
+ addIUseMapping("FIRSTAID", &iuse::firstaid);
+ addIUseMapping("CAFF", &iuse::caff);
+ addIUseMapping("ALCOHOL", &iuse::alcohol);
+ addIUseMapping("ALCOHOL_WEAK", &iuse::alcohol_weak);
+ addIUseMapping("PKILL_1", &iuse::pkill_1);
+ addIUseMapping("PKILL_2", &iuse::pkill_2);
+ addIUseMapping("PKILL_3", &iuse::pkill_3);
+ addIUseMapping("PKILL_4", &iuse::pkill_4);
+ addIUseMapping("PKILL_L", &iuse::pkill_l);
+ addIUseMapping("XANAX", &iuse::xanax);
+ addIUseMapping("CIG", &iuse::cig);
+ addIUseMapping("ANTIBIOTIC", &iuse::antibiotic);
+ addIUseMapping("WEED", &iuse::weed);
+ addIUseMapping("COKE", &iuse::coke);
+ addIUseMapping("CRACK", &iuse::crack);
+ addIUseMapping("GRACK", &iuse::grack);
+ addIUseMapping("METH", &iuse::meth);
+ addIUseMapping("POISON", &iuse::poison);
+ addIUseMapping("HALLU", &iuse::hallu);
+ addIUseMapping("THORAZINE", &iuse::thorazine);
+ addIUseMapping("PROZAC", &iuse::prozac);
+ addIUseMapping("SLEEP", &iuse::sleep);
+ addIUseMapping("IODINE", &iuse::iodine);
+ addIUseMapping("FLUMED", &iuse::flumed);
+ addIUseMapping("FLUSLEEP", &iuse::flusleep);
+ addIUseMapping("INHALER", &iuse::inhaler);
+ addIUseMapping("BLECH", &iuse::blech);
+ addIUseMapping("MUTAGEN", &iuse::mutagen);
+ addIUseMapping("MUTAGEN_3", &iuse::mutagen_3);
+ addIUseMapping("PURIFIER", &iuse::purifier);
+ addIUseMapping("MARLOSS", &iuse::marloss);
+ addIUseMapping("DOGFOOD", &iuse::dogfood);
+ addIUseMapping("LIGHTER", &iuse::lighter);
+ addIUseMapping("SEW", &iuse::sew);
+ addIUseMapping("SCISSORS", &iuse::scissors);
+ addIUseMapping("EXTINGUISHER", &iuse::extinguisher);
+ addIUseMapping("HAMMER", &iuse::hammer);
+ addIUseMapping("LIGHT_OFF", &iuse::light_off);
+ addIUseMapping("LIGHT_ON", &iuse::light_on);
+ addIUseMapping("CAUTERIZE_ELEC", &iuse::cauterize_elec);
+ addIUseMapping("WATER_PURIFIER", &iuse::water_purifier);
+ addIUseMapping("TWO_WAY_RADIO", &iuse::two_way_radio);
+ addIUseMapping("RADIO_OFF", &iuse::radio_off);
+ addIUseMapping("RADIO_ON", &iuse::radio_on);
+ addIUseMapping("ROADMAP", &iuse::roadmap);
+ addIUseMapping("PICKLOCK", &iuse::picklock);
+ addIUseMapping("CROWBAR", &iuse::crowbar);
+ addIUseMapping("MAKEMOUND", &iuse::makemound);
+ addIUseMapping("DIG", &iuse::dig);
+ addIUseMapping("CHAINSAW_OFF", &iuse::chainsaw_off);
+ addIUseMapping("CHAINSAW_ON", &iuse::chainsaw_on);
+ addIUseMapping("JACKHAMMER", &iuse::jackhammer);
+ addIUseMapping("JACQUESHAMMER", &iuse::jacqueshammer);
+ addIUseMapping("PICKAXE", &iuse::pickaxe);
+ addIUseMapping("SET_TRAP", &iuse::set_trap);
+ addIUseMapping("GEIGER", &iuse::geiger);
+ addIUseMapping("TELEPORT", &iuse::teleport);
+ addIUseMapping("CAN_GOO", &iuse::can_goo);
+ addIUseMapping("PIPEBOMB", &iuse::pipebomb);
+ addIUseMapping("PIPEBOMB_ACT", &iuse::pipebomb_act);
+ addIUseMapping("GRENADE", &iuse::grenade);
+ addIUseMapping("GRENADE_ACT", &iuse::grenade_act);
+ addIUseMapping("FLASHBANG", &iuse::flashbang);
+ addIUseMapping("FLASHBANG_ACT", &iuse::flashbang_act);
+ addIUseMapping("C4", &iuse::c4);
+ addIUseMapping("C4ARMED", &iuse::c4armed);
+ addIUseMapping("EMPBOMB", &iuse::EMPbomb);
+ addIUseMapping("EMPBOMB_ACT", &iuse::EMPbomb_act);
+ addIUseMapping("SCRAMBLER", &iuse::scrambler);
+ addIUseMapping("SCRAMBLER_ACT", &iuse::scrambler_act);
+ addIUseMapping("GASBOMB", &iuse::gasbomb);
+ addIUseMapping("GASBOMB_ACT", &iuse::gasbomb_act);
+ addIUseMapping("SMOKEBOMB", &iuse::smokebomb);
+ addIUseMapping("SMOKEBOMB_ACT", &iuse::smokebomb_act);
+ addIUseMapping("ACIDBOMB", &iuse::acidbomb);
+ addIUseMapping("ACIDBOMB_ACT", &iuse::acidbomb_act);
+ addIUseMapping("MOLOTOV", &iuse::molotov);
+ addIUseMapping("MOLOTOV_LIT", &iuse::molotov_lit);
+ addIUseMapping("DYNAMITE", &iuse::dynamite);
+ addIUseMapping("DYNAMITE_ACT", &iuse::dynamite_act);
+ addIUseMapping("FIRECRACKER_PACK", &iuse::firecracker_pack);
+ addIUseMapping("FIRECRACKER_PACK_ACT", &iuse::firecracker_pack_act);
+ addIUseMapping("FIRECRACKER", &iuse::firecracker);
+ addIUseMapping("FIRECRACKER_ACT", &iuse::firecracker_act);
+ addIUseMapping("MININUKE", &iuse::mininuke);
+ addIUseMapping("MININUKE_ACT", &iuse::mininuke_act);
+ addIUseMapping("PHEROMONE", &iuse::pheromone);
+ addIUseMapping("PORTAL", &iuse::portal);
+ addIUseMapping("MANHACK", &iuse::manhack);
+ addIUseMapping("TURRET", &iuse::turret);
+ addIUseMapping("UPS_OFF", &iuse::UPS_off);
+ addIUseMapping("UPS_ON", &iuse::UPS_on);
+ addIUseMapping("TAZER", &iuse::tazer);
+ addIUseMapping("MP3", &iuse::mp3);
+ addIUseMapping("MP3_ON", &iuse::mp3_on);
+ addIUseMapping("VORTEX", &iuse::vortex);
+ addIUseMapping("DOG_WHISTLE", &iuse::dog_whistle);
+ addIUseMapping("VACUTAINER", &iuse::vacutainer);
+ addIUseMapping("KNIFE", &iuse::knife);
+ addIUseMapping("LUMBER", &iuse::lumber);
+ addIUseMapping("HACKSAW", &iuse::hacksaw);
+ addIUseMapping("TENT", &iuse::tent);
+ addIUseMapping("SHELTER", &iuse::shelter);
+ addIUseMapping("TORCH", &iuse::torch);
+ addIUseMapping("TORCH_LIT", &iuse::torch_lit);
+ addIUseMapping("CANDLE", &iuse::candle);
+ addIUseMapping("CANDLE_LIT", &iuse::candle_lit);
+ addIUseMapping("BULLET_PULLER", &iuse::bullet_puller);
+// addIUseMapping("SCREWDRIVER", &iuse::screwdriver);
+// addIUseMapping("WRENCH", &iuse::wrench);
+ addIUseMapping("BOLTCUTTERS", &iuse::boltcutters);
+ addIUseMapping("MOP", &iuse::mop);
+ addIUseMapping("SPRAY_CAN", &iuse::spray_can);
+ addIUseMapping("RAG", &iuse::rag);
+ addIUseMapping("PDA", &iuse::pda);
+ addIUseMapping("PDA_FLASHLIGHT", &iuse::pda_flashlight);
+ addIUseMapping("LAW", &iuse::LAW);
+ addIUseMapping("HEATPACK", &iuse::heatpack);
+ addIUseMapping("MCG_NOTE", &iuse::mcg_note);
+ addIUseMapping("ARTIFACT", &iuse::artifact);
+ //addIUseMapping("HEAL", &iuse::heal);
+ //addIUseMapping("TWIST_SPACE", &iuse::twist_space);
+ //addIUseMapping("MASS_VAMPIRE", &iuse::mass_vampire);
+ //addIUseMapping("GROWTH", &iuse::growth);
+ //addIUseMapping("WATER", &iuse::water);
+ //addIUseMapping("LAVA", &iuse::lava);
+}
+
+// Loader for item category
+item_template* load_comest(item_tag new_id, const picojson::value::object& value_map)
+{
+
+ return 0;
+}
+
+// Loader for item category
+item_template* load_ammo(item_tag new_id, const picojson::value::object& value_map)
+{
+ return 0;
+}
+
+// Loader for item category
+item_template* load_armor(item_tag new_id, const picojson::value::object& value_map)
+{
+ return 0;
+}
+
+// Loader for item category
+item_template* load_gun(item_tag new_id, const picojson::value::object& value_map)
+{
+ return 0;
+}
+
+// Loader for item category
+item_template* load_book(item_tag new_id, const picojson::value::object& value_map)
+{
+ return 0;
+}
+
+// Loader for item category
+item_template* load_tool(item_tag new_id, const picojson::value::object& value_map)
+{
+ return 0;
+}
+
+// Loader for item category
+item_template* load_container(item_tag new_id, const picojson::value::object& value_map)
+{
+ return 0;
+}
+
+// Loader for item category
+item_template* load_default(item_tag new_id, const picojson::value::object& entry_body)
+{
+// And then proceed to assign the correct field
+ item_template* new_item_template = new itype();
+/*
+ new_item_template->rarity = int_from_json(new_id, "rarity", entry_body);
+ new_item_template->name = string_from_json(new_id, "name", entry_body);
+ new_item_template->sym = char_from_json(new_id, "symbol", entry_body);
+ new_item_template->color = color_from_json(new_id, "color", entry_body);
+ new_item_template->description = string_from_json(new_id, "description", entry_body);
+ new_item_template->m1 = materials_from_json(new_id, "material", entry_body)[0];
+ new_item_template->m2 = materials_from_json(new_id, "material", entry_body)[1];
+ new_item_template->volume = int_from_json(new_id, "volume", entry_body);
+ new_item_template->weight = int_from_json(new_id, "weight", entry_body);
+ new_item_template->melee_dam = int_from_json(new_id, "damage", entry_body);
+ new_item_template->melee_cut = int_from_json(new_id, "cutting", entry_body);
+ new_item_template->m_to_hit = int_from_json(new_id, "to_hit", entry_body);*/
+ return new_item_template;
+}
+
diff --git a/item_manager.h b/item_manager.h
index 9d183e2..6f80f59 100644
--- a/item_manager.h
+++ b/item_manager.h
@@ -16,6 +16,14 @@ typedef std::map<item_tag, itype*> item_template_container;
typedef std::set<item_tag> tag_list;
typedef std::vector<item*> item_list;
+item_template* load_comest(item_tag new_id, const picojson::value::object& value_map);
+item_template* load_ammo(item_tag new_id, const picojson::value::object& value_map);
+item_template* load_armor(item_tag new_id, const picojson::value::object& value_map);
+item_template* load_gun(item_tag new_id, const picojson::value::object& value_map);
+item_template* load_book(item_tag new_id, const picojson::value::object& value_map);
+item_template* load_tool(item_tag new_id, const picojson::value::object& value_map);
+item_template* load_container(item_tag new_id, const picojson::value::object& value_map);
+item_template* load_default(item_tag new_id, const picojson::value::object& value_map);
class Item_manager
{
@@ -41,6 +49,12 @@ public:
item* create_random();
item_list create_random(int quantity);
+
+ typedef void (iuse::*IUSE_FUNCTIONPOINTER)(game *, player *, item *, bool);
+ void addIUseMapping(const std::string& iuseName, IUSE_FUNCTIONPOINTER func);
+ IUSE_FUNCTIONPOINTER iuseFromString(const std::string& iuseName);
+ void setDefaultIUseMappings();
+
private:
item_template_container m_templates;
itype* m_missing_item;
@@ -56,6 +70,13 @@ private:
material* materials_from_json(item_tag new_id, item_tag index, picojson::value::object value_map);
material material_from_tag(item_tag new_id, item_tag index);
+ typedef std::map<std::string, IUSE_FUNCTIONPOINTER> IUSE_MAP;
+ IUSE_MAP iuseMapping;
+
+ typedef item_template* (*CATLOAD_FUNCTIONPOINTER)(item_tag new_id, const picojson::value::object& value_map);
+ typedef std::map<std::string, CATLOAD_FUNCTIONPOINTER> CATLOAD_MAP;
+ CATLOAD_MAP categoryLoaders;
+
};
extern Item_manager* item_controller;