@@ -232,6 +232,8 @@ void Item_factory::init(game* main_game) throw(std::string){
for (std::map<Item_tag, itype*>::iterator iter = new_templates.begin (); iter != new_templates.end (); ++iter) {
standard_itype_ids.push_back (iter->first );
}
load_item_groups_from (main_game, " data/raw/item_groups.json" );
}
// Returns the template with the given identification tag
@@ -326,7 +328,6 @@ void Item_factory::load_item_templates() throw(std::string){
load_item_templates_from (" data/raw/items/armor.json" );
load_item_templates_from (" data/raw/items/books.json" );
load_item_templates_from (" data/raw/items/archery.json" );
load_item_groups_from (" data/raw/item_groups.json" );
}
catch (std::string &error_message) {
throw ;
@@ -558,14 +559,14 @@ void Item_factory::load_item_templates_from(const std::string file_name) throw (
}
// Load values from this data file into m_template_groups
void Item_factory::load_item_groups_from (const std::string file_name) throw (std::string) {
void Item_factory::load_item_groups_from ( game *g, const std::string file_name ) throw ( std::string ) {
std::ifstream data_file;
picojson::value input_value;
data_file.open (file_name.c_str ());
if (! data_file.good ()) {
throw " Could not read " + file_name;
throw " Could not read " + file_name;
}
data_file >> input_value;
@@ -580,90 +581,118 @@ void Item_factory::load_item_groups_from(const std::string file_name) throw (std
// Crawl through once and create an entry for every definition
const picojson::array& all_items = input_value.get <picojson::array>();
for (picojson::array::const_iterator entry = all_items.begin (); entry != all_items.end (); ++entry) {
for (picojson::array::const_iterator entry = all_items.begin ();
entry != all_items.end (); ++entry) {
// TODO: Make sure we have at least an item or group child, as otherwise
// later things will bug out.
if ( !(entry->is <picojson::object>()) ){
debugmsg (" Invalid group definition, entry not a JSON object" );
continue ;
}
else {
const picojson::value::object& entry_body = entry->get <picojson::object>();
// The one element we absolutely require for an item definition is an id
picojson::value::object::const_iterator key_pair = entry_body.find (" id" );
if ( key_pair == entry_body.end () || !(key_pair->second .is <std::string>()) ){
debugmsg (" Group definition skipped, no id found or id was malformed." );
} else {
Item_tag group_id = key_pair->second .get <std::string>();
m_template_groups[group_id] = new Item_group (group_id);
}
const picojson::value::object& entry_body = entry->get <picojson::object>();
// The one element we absolutely require for an item definition is an id
picojson::value::object::const_iterator key_pair = entry_body.find ( " id" );
if ( key_pair == entry_body.end () || !(key_pair->second .is <std::string>()) ) {
debugmsg (" Group definition skipped, no id found or id was malformed." );
continue ;
}
Item_tag group_id = key_pair->second .get <std::string>();
m_template_groups[group_id] = new Item_group (group_id);
}
// Once all the group definitions are set, fill them out
for (picojson::array::const_iterator entry = all_items.begin (); entry != all_items.end (); ++entry) {
for (picojson::array::const_iterator entry = all_items.begin ();
entry != all_items.end (); ++entry) {
const picojson::value::object& entry_body = entry->get <picojson::object>();
Item_tag group_id = entry_body.find (" id" )->second .get <std::string>();
Item_group *current_group = m_template_groups.find (group_id)->second ;
// Add items
picojson::value::object::const_iterator key_pair = entry_body.find (" items" );
if ( key_pair != entry_body.end () ){
if ( !(key_pair->second .is <picojson::array>()) ){
debugmsg (" Invalid item list for group definition '%s', list of items not an array." ,group_id.c_str ());
} else {
// We have confirmed that we have a list of SOMETHING, now let's add them one at a time.
const picojson::array& items_to_add = key_pair->second .get <picojson::array>();
for (picojson::array::const_iterator item_pair = items_to_add.begin (); item_pair != items_to_add.end (); ++item_pair) {
// Before adding, make sure this element is in the right format, namely ["TAG_NAME", frequency number]
if (!(item_pair->is <picojson::array>())){
debugmsg (" Invalid item list for group definition '%s', element is not an array." ,group_id.c_str ());
} else if (item_pair->get <picojson::array>().size ()!=2 ){
debugmsg (" Invalid item list for group definition '%s', element does not have 2 values." ,group_id.c_str ());
} else {
picojson::array item_frequency_array = item_pair->get <picojson::array>();
// Finally, insure that the first value is a string, and the second is a number
if (!item_frequency_array[0 ].is <std::string>() || !item_frequency_array[1 ].is <double >() ){
debugmsg (" Invalid item list for group definition '%s', element is not a valid tag/frequency pair." ,group_id.c_str ());
} else {
current_group->add_entry (item_frequency_array[0 ].get <std::string>(), (int )item_frequency_array[1 ].get <double >());
}
}
if ( key_pair != entry_body.end () ) {
if ( !(key_pair->second .is <picojson::array>()) ) {
debugmsg (" Invalid item list for group definition '%s', list of items not an array." ,
group_id.c_str ());
// We matched the find above, so we're NOT a group.
continue ;
}
// We have confirmed that we have a list of SOMETHING, now let's add them one at a time.
const picojson::array& items_to_add = key_pair->second .get <picojson::array>();
for (picojson::array::const_iterator item_pair = items_to_add.begin ();
item_pair != items_to_add.end (); ++item_pair) {
// Before adding, make sure this element is in the right format,
// namely ["TAG_NAME", frequency number]
if ( !(item_pair->is <picojson::array>()) ) {
debugmsg (" Invalid item list for group definition '%s', element is not an array." ,
group_id.c_str ());
continue ;
}
if ( item_pair->get <picojson::array>().size () != 2 ) {
debugmsg ( " Invalid item list for group definition '%s', element does not have 2 values." ,
group_id.c_str () );
continue ;
}
picojson::array item_frequency_array = item_pair->get <picojson::array>();
// Insure that the first value is a string, and the second is a number
if ( !item_frequency_array[0 ].is <std::string>() ||
!item_frequency_array[1 ].is <double >() ) {
debugmsg (" Invalid item list for group definition '%s', element is not a valid tag/frequency pair." ,
group_id.c_str ());
continue ;
}
if ( m_missing_item == find_template ( item_frequency_array[0 ].get <std::string>() ) &&
0 == g->itypes .count ( item_frequency_array[0 ].get <std::string>() ) ) {
debugmsg ( " Item '%s' listed in group '%s' does not exist." ,
item_frequency_array[0 ].get <std::string>().c_str (), group_id.c_str () );
continue ;
}
current_group->add_entry (item_frequency_array[0 ].get <std::string>(),
(int )item_frequency_array[1 ].get <double >());
}
}
// Add groups
key_pair = entry_body.find (" groups" );
if (key_pair != entry_body.end ()){
if ( !(key_pair->second .is <picojson::array>()) ){
debugmsg (" Invalid group list for group definition '%s', list of items not an array." ,group_id.c_str ());
} else {
// We have confirmed that we have a list of SOMETHING, now let's add them one at a time.
const picojson::array& items_to_add = key_pair->second .get <picojson::array>();
for (picojson::array::const_iterator item_pair = items_to_add.begin (); item_pair != items_to_add.end (); ++item_pair) {
// Before adding, make sure this element is in the right format, namely ["TAG_NAME", frequency number]
if (!(item_pair->is <picojson::array>())){
debugmsg (" Invalid group list for group definition '%s', element is not an array." ,group_id.c_str ());
} else if (item_pair->get <picojson::array>().size ()!=2 ){
debugmsg (" Invalid group list for group definition '%s', element does not have 2 values." ,group_id.c_str ());
} else {
picojson::array item_frequency_array = item_pair->get <picojson::array>();
// Finally, insure that the first value is a string, and the second is a number
if (!item_frequency_array[0 ].is <std::string>() || !item_frequency_array[1 ].is <double >() ){
debugmsg (" Invalid group list for group definition '%s', element is not a valid tag/frequency pair." ,group_id.c_str ());
} else {
Item_group* subgroup = m_template_groups.find (item_frequency_array[0 ].get <std::string>())->second ;
current_group->add_group (subgroup, (int )item_frequency_array[1 ].get <double >());
}
}
debugmsg (" Invalid group list for group definition '%s', list of items not an array." ,
group_id.c_str ());
continue ;
}
// We have confirmed that we have a list of SOMETHING, now let's add them one at a time.
const picojson::array& items_to_add = key_pair->second .get <picojson::array>();
for ( picojson::array::const_iterator item_pair = items_to_add.begin ();
item_pair != items_to_add.end (); ++item_pair ) {
// Before adding, make sure this element is in the right format, namely ["TAG_NAME", frequency number]
if ( !(item_pair->is <picojson::array>()) ) {
debugmsg (" Invalid group list for group definition '%s', element is not an array." ,
group_id.c_str ());
continue ;
}
if ( item_pair->get <picojson::array>().size () != 2 ) {
debugmsg (" Invalid group list for group definition '%s', element does not have 2 values." ,
group_id.c_str ());
continue ;
}
picojson::array item_frequency_array = item_pair->get <picojson::array>();
// Finally, insure that the first value is a string, and the second is a number
if (!item_frequency_array[0 ].is <std::string>() ||
!item_frequency_array[1 ].is <double >() ){
debugmsg (" Invalid group list for group definition '%s', element is not a valid tag/frequency pair." ,
group_id.c_str ());
continue ;
}
Item_group* subgroup = m_template_groups.find (item_frequency_array[0 ].get <std::string>())->second ;
current_group->add_group ( subgroup, (int )item_frequency_array[1 ].get <double >() );
}
}
}
if (!json_good ())
if ( !json_good () ) {
throw " There was an error reading " + file_name;
}
}
// Grab color, with appropriate error handling
0 comments on commit
14d63ab