void game::init_dreams() { catajson dreams_file("data/raw/dreams.json"); if (!json_good()) { throw (std::string)"data/raw/dreams.json wasn't found"; } for (dreams_file.set_begin(); dreams_file.has_curr(); dreams_file.next()) { catajson dreamcurr = dreams_file.curr(); dream newdream; catajson messages = dreamcurr.get("message"); for (messages.set_begin(); messages.has_curr(); messages.next()) { newdream.message.push_back(messages.curr().as_string()); } newdream.strength = dreamcurr.get("strength").as_int(); newdream.category = string_to_mutcat(dreamcurr.get("category").as_string()); dreams.push_back(newdream); } if (!json_good()) { exit(1); } }
material_map material_type::load_materials() { material_map allMaterials; catajson materialsRaw("data/raw/materials.json", true); unsigned int id = 0; for (materialsRaw.set_begin(); materialsRaw.has_curr() && json_good(); materialsRaw.next()) { ++id; catajson currMaterial = materialsRaw.curr(); std::string ident = currMaterial.get("ident").as_string(); std::string name = currMaterial.get("name").as_string(); int bash_resist = currMaterial.get("bash_resist").as_int(); int cut_resist = currMaterial.get("cut_resist").as_int(); std::string bash_dmg_verb = currMaterial.get("bash_dmg_verb").as_string(); std::string cut_dmg_verb = currMaterial.get("cut_dmg_verb").as_string(); int acid_resist = currMaterial.get("acid_resist").as_int(); int elec_resist = currMaterial.get("elec_resist").as_int(); int fire_resist = currMaterial.get("fire_resist").as_int(); int density = currMaterial.get("density").as_int(); catajson adjList = currMaterial.get("dmg_adj"); std::string dmg_adj[4]; dmg_adj[0] = adjList.get(0).as_string(); dmg_adj[1] = adjList.get(1).as_string(); dmg_adj[2] = adjList.get(2).as_string(); dmg_adj[3] = adjList.get(3).as_string(); name = _(name.c_str()); bash_dmg_verb = _(bash_dmg_verb.c_str()); cut_dmg_verb = _(cut_dmg_verb.c_str()); dmg_adj[0] = _(dmg_adj[0].c_str()); dmg_adj[1] = _(dmg_adj[1].c_str()); dmg_adj[2] = _(dmg_adj[2].c_str()); dmg_adj[3] = _(dmg_adj[3].c_str()); material_type newMaterial(id, ident, name, bash_resist, cut_resist, bash_dmg_verb, cut_dmg_verb, dmg_adj, acid_resist, elec_resist, fire_resist, density); allMaterials[ident] = newMaterial; } if(!json_good()) exit(1); return allMaterials; }
void snippet_library::load() throw (std::string) { catajson snippetRaw("data/raw/snippets.json"); if(!json_good()) throw (std::string)"Could not read data/raw/snippets.json"; catajson snippetList = snippetRaw.get("snippets"); for( snippetList.set_begin(); snippetList.has_curr(); snippetList.next() ) { const catajson curr = snippetList.curr(); // required fields const std::string category = curr.get("category").as_string(); const std::string text = curr.get("text").as_string(); const int hash = djb2_hash( (const unsigned char*)text.c_str() ); snippets.insert( std::pair<int, std::string>(hash, text) ); categories.insert( std::pair<std::string, int>(category, hash) ); } if(!json_good()) throw (std::string)"There was an error reading data/raw/snippets.json"; }
void MonsterGroupManager::LoadJSONGroups() throw (std::string) { //open the file std::ifstream file; file.open(monGroupFilePath); if(!file.good()) { throw (std::string)"Unable to load file " + monGroupFilePath; } //load the data picojson::value groupsRaw; file >> groupsRaw; /*std::string error = picojson::get_last_error(); if(! error.empty()) { printf("'%s' : %s", monGroupFilePath, error.c_str()); return; }*/ //check the data if (! groupsRaw.is<picojson::array>()) { throw (std::string)"The monster group file " + monGroupFilePath + (std::string)" does not contain the expected JSON data"; } init_translation(); picojson::object jsonobj; MonsterGroup g; const picojson::array& groups = groupsRaw.get<picojson::array>(); for (picojson::array::const_iterator it_groups = groups.begin(); it_groups != groups.end(); ++it_groups) { jsonobj = it_groups->get<picojson::object>(); g = GetMGroupFromJSON(&jsonobj); monsterGroupMap[g.name] = g; } if(!json_good()) { throw (std::string)"There was an error reading " + monGroupFilePath; } }
// Load values from this data file into m_template_groups 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; } data_file >> input_value; data_file.close(); if(! json_good()) { throw "The data in " + file_name + " is not an array"; } if (! input_value.is<picojson::array>()) { throw file_name + "is not an array of item groups"; } //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) { // 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; } 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) { 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()); // 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()); 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() ) { throw "There was an error reading " + file_name; } }
// Load values from this data file into m_templates void Item_factory::load_item_templates_from(const std::string file_name) throw (std::string){ catajson all_items(file_name); if(! json_good()) throw (std::string)"Could not open " + file_name; if (! all_items.is_array()) throw file_name + (std::string)"is not an array of item_templates"; //Crawl through and extract the items for (all_items.set_begin(); all_items.has_curr(); all_items.next()) { catajson entry = all_items.curr(); // The one element we absolutely require for an item definition is an id if(!entry.has("id") || !entry.get("id").is_string()) { debugmsg("Item definition skipped, no id found or id was malformed."); } else { Item_tag new_id = entry.get("id").as_string(); // If everything works out, add the item to the group list... // unless a similar item is already there if(m_templates.find(new_id) != m_templates.end()) { debugmsg("Item definition skipped, id %s already exists.", new_id.c_str()); } else { itype* new_item_template; if (!entry.has("type")) { new_item_template = new itype(); } else { std::string type_label = entry.get("type").as_string(); if (type_label == "GUNMOD") { it_gunmod* gunmod_template = new it_gunmod(); gunmod_template->damage = entry.get("damage_modifier").as_int();; gunmod_template->loudness = entry.get("loudness_modifier").as_int(); gunmod_template->newtype = entry.get("ammo_modifier").as_string(); gunmod_template->used_on_pistol = is_mod_target(entry.get("mod_targets"), "pistol"); gunmod_template->used_on_shotgun = is_mod_target(entry.get("mod_targets"), "shotgun"); gunmod_template->used_on_smg = is_mod_target(entry.get("mod_targets"), "smg"); gunmod_template->used_on_rifle = is_mod_target(entry.get("mod_targets"), "rifle"); gunmod_template->dispersion = entry.get("dispersion_modifier").as_int(); gunmod_template->recoil = entry.get("recoil_modifier").as_int(); gunmod_template->burst = entry.get("burst_modifier").as_int(); gunmod_template->clip = entry.get("clip_size_modifier").as_int(); if( entry.has("acceptable_ammo") ) { tags_from_json( entry.get("acceptable_ammo"), gunmod_template->acceptible_ammo_types ); } new_item_template = gunmod_template; } else if (type_label == "COMESTIBLE") { it_comest* comest_template = new it_comest(); comest_template->comesttype = entry.get("comestible_type").as_string(); comest_template->tool = entry.get("tool").as_string(); comest_template->container = entry.get("container").as_string(); comest_template->quench = entry.get("quench").as_int(); comest_template->nutr = entry.get("nutrition").as_int(); comest_template->spoils = entry.get("spoils_in").as_int(); comest_template->addict = entry.get("addiction_potential").as_int(); comest_template->charges = entry.get("charges").as_int(); comest_template->stim = entry.get("stim").as_int(); comest_template->healthy = entry.get("heal").as_int(); comest_template->fun = entry.get("fun").as_int(); comest_template->add = addiction_type(entry.get("addiction_type").as_string()); new_item_template = comest_template; } else if (type_label == "GUN") { it_gun* gun_template = new it_gun(); gun_template->ammo = entry.get("ammo").as_string(); gun_template->skill_used = Skill::skill(entry.get("skill").as_string()); gun_template->dmg_bonus = entry.get("ranged_damage").as_int(); gun_template->range = entry.get("range").as_int(); gun_template->dispersion = entry.get("dispersion").as_int(); gun_template->recoil = entry.get("recoil").as_int(); gun_template->durability = entry.get("durability").as_int(); gun_template->burst = entry.get("burst").as_int(); gun_template->clip = entry.get("clip_size").as_int(); gun_template->reload_time = entry.get("reload").as_int(); gun_template->pierce = entry.has("pierce") ? entry.get("pierce").as_int() : 0; if( entry.has("ammo_effects") ) { tags_from_json(entry.get("ammo_effects"), gun_template->ammo_effects); } new_item_template = gun_template; } else if (type_label == "TOOL") { it_tool* tool_template = new it_tool(); tool_template->ammo = entry.get("ammo").as_string(); tool_template->max_charges = entry.get("max_charges").as_int(); tool_template->def_charges = entry.get("initial_charges").as_int(); tool_template->charges_per_use = entry.get("charges_per_use").as_int(); tool_template->turns_per_charge = entry.get("turns_per_charge").as_int(); tool_template->revert_to = entry.get("revert_to").as_string(); new_item_template = tool_template; } else if (type_label == "AMMO") { it_ammo* ammo_template = new it_ammo(); ammo_template->type = entry.get("ammo_type").as_string(); if(entry.has("casing")) { ammo_template->casing = entry.get("casing").as_string(); } ammo_template->damage = entry.get("damage").as_int(); ammo_template->pierce = entry.get("pierce").as_int(); ammo_template->range = entry.get("range").as_int(); ammo_template->dispersion = entry.get("dispersion").as_int(); ammo_template->recoil = entry.get("recoil").as_int(); ammo_template->count = entry.get("count").as_int(); if( entry.has("effects") ) { tags_from_json(entry.get("effects"), ammo_template->ammo_effects); } new_item_template = ammo_template; } else if (type_label == "ARMOR") { it_armor* armor_template = new it_armor(); armor_template->encumber = entry.get("encumberance").as_int(); armor_template->coverage = entry.get("coverage").as_int(); armor_template->thickness = entry.get("material_thickness").as_int(); armor_template->env_resist = entry.get("enviromental_protection").as_int(); armor_template->warmth = entry.get("warmth").as_int(); armor_template->storage = entry.get("storage").as_int(); armor_template->power_armor = entry.has("power_armor") ? entry.get("power_armor").as_bool() : false; armor_template->covers = entry.has("covers") ? flags_from_json(entry.get("covers"),"bodyparts") : 0; new_item_template = armor_template; } else if (type_label == "BOOK") { it_book* book_template = new it_book(); book_template->level = entry.get("max_level").as_int(); book_template->req = entry.get("required_level").as_int(); book_template->fun = entry.get("fun").as_int(); book_template->intel = entry.get("intelligence").as_int(); book_template->time = entry.get("time").as_int(); book_template->type = Skill::skill(entry.get("skill").as_string()); new_item_template = book_template; } else if (type_label == "CONTAINER") { it_container* container_template = new it_container(); container_template->contains = entry.get("contains").as_int(); new_item_template = container_template; } else { debugmsg("Item definition for %s skipped, unrecognized type: %s", new_id.c_str(), type_label.c_str()); break; } } new_item_template->id = new_id; m_templates[new_id] = new_item_template; // And then proceed to assign the correct field new_item_template->price = entry.get("price").as_int(); new_item_template->name = _(entry.get("name").as_string().c_str()); new_item_template->sym = entry.get("symbol").as_char(); new_item_template->color = color_from_string(entry.get("color").as_string()); new_item_template->description = _(entry.get("description").as_string().c_str()); if(entry.has("material")){ set_material_from_json(new_id, entry.get("material")); } else { new_item_template->m1 = "null"; new_item_template->m2 = "null"; } Item_tag new_phase = "solid"; if(entry.has("phase")){ new_phase = entry.get("phase").as_string(); } new_item_template->phase = phase_from_tag(new_phase); new_item_template->volume = entry.get("volume").as_int(); new_item_template->weight = entry.get("weight").as_int(); new_item_template->melee_dam = entry.get("bashing").as_int(); new_item_template->melee_cut = entry.get("cutting").as_int(); new_item_template->m_to_hit = entry.get("to_hit").as_int(); if( entry.has("flags") ) { new_item_template->item_tags = entry.get("flags").as_tags(); /* List of current flags FIT - Reduces encumberance by one VARSIZE - Can be made to fit via tailoring OVERSIZE - Can always be worn no matter encumberance/mutations/bionics/etc HOOD - Will increase warmth for head if head is cold and player is not wearing a helmet (headwear of material that is not wool or cotton) POCKETS - Will increase warmth for hands if hands are cold and the player is wielding nothing WATCH - Shows the current time, instead of sun/moon position ALARMCLOCK - Has an alarmclock feature MALE_TYPICAL - Typically only worn by men. FEMALE_TYPICAL - Typically only worn by women. USE_EAT_VERB - Use the eat verb, even if it's a liquid(soup, jam etc.) Container-only flags: SEALS RIGID WATERTIGHT */ } new_item_template->techniques = (!entry.has("techniques") ? 0 : flags_from_json(entry.get("techniques"), "techniques")); new_item_template->use = (!entry.has("use_action") ? &iuse::none : use_from_string(entry.get("use_action").as_string())); } } } if(!json_good()) throw "There was an error reading " + file_name; }
void game::init_traits_mutations() { catajson cjMutationsRaw("data/raw/mutations.json"); if (!json_good()) { throw (std::string)"data/raw/mutations.json wasn't found"; } mutations_category[""].clear(); //dont delete this! for (cjMutationsRaw.set_begin(); cjMutationsRaw.has_curr(); cjMutationsRaw.next()) { catajson cjMutation = cjMutationsRaw.curr(); std::string sMutation = cjMutation.get("id").as_string(); trait new_trait; new_trait.name = _(cjMutation.get("name").as_string().c_str()); new_trait.points = cjMutation.get("points").as_int(); new_trait.visiblity = cjMutation.get("visibility").as_int(); new_trait.ugliness = cjMutation.get("ugliness").as_int(); new_trait.description = _(cjMutation.get("description").as_string().c_str()); new_trait.startingtrait = false; if (cjMutation.has("starting_trait")) { new_trait.startingtrait = cjMutation.get("starting_trait").as_bool(); } traits[sMutation] = new_trait; mutation_data[sMutation].valid = cjMutation.get("valid").as_bool(); if (cjMutation.has("prereqs")) { catajson cjPrereqs = cjMutation.get("prereqs"); if (cjPrereqs.is_array()) { for (cjPrereqs.set_begin(); cjPrereqs.has_curr(); cjPrereqs.next()) { mutation_data[sMutation].prereqs.push_back(cjPrereqs.curr().as_string()); } } } if (cjMutation.has("cancels")) { catajson cjCancels = cjMutation.get("cancels"); if (cjCancels.is_array()) { for (cjCancels.set_begin(); cjCancels.has_curr(); cjCancels.next()) { mutation_data[sMutation].cancels.push_back(cjCancels.curr().as_string()); } } } if (cjMutation.has("changes_to")) { catajson cjChangesTo = cjMutation.get("changes_to"); if (cjChangesTo.is_array()) { for (cjChangesTo.set_begin(); cjChangesTo.has_curr(); cjChangesTo.next()) { mutation_data[sMutation].replacements.push_back(cjChangesTo.curr().as_string()); } } } if (cjMutation.has("leads_to")) { catajson cjLeadsTo = cjMutation.get("leads_to"); if (cjLeadsTo.is_array()) { for (cjLeadsTo.set_begin(); cjLeadsTo.has_curr(); cjLeadsTo.next()) { mutation_data[sMutation].additions.push_back(cjLeadsTo.curr().as_string()); } } } if (cjMutation.has("category")) { catajson cjCategory = cjMutation.get("category"); if (cjCategory.is_array()) { for (cjCategory.set_begin(); cjCategory.has_curr(); cjCategory.next()) { mutation_data[sMutation].category.push_back(cjCategory.curr().as_string()); mutations_category[cjCategory.curr().as_string()].push_back(sMutation); } } } } }
profmap profession::load_professions() { profmap allProfs; catajson profsRaw("data/raw/professions.json",true); unsigned int id = 0; for (profsRaw.set_begin(); profsRaw.has_curr() && json_good(); profsRaw.next()) { ++id; catajson currProf = profsRaw.curr(); std::string ident = currProf.get("ident").as_string(); std::string name = currProf.get("name").as_string(); std::string description = currProf.get("description").as_string(); signed int points = currProf.get("points").as_int(); name = _(name.c_str()); description = _(description.c_str()); profession newProfession(id, ident, name, description, points); catajson items = currProf.get("items"); for (items.set_begin(); items.has_curr(); items.next()) { newProfession.add_item(items.curr().as_string()); } // Addictions are optional if (currProf.has("addictions")) { catajson addictions = currProf.get("addictions"); for (addictions.set_begin(); addictions.has_curr(); addictions.next()) { catajson currAdd = addictions.curr(); std::string type_str = currAdd.get("type").as_string(); add_type type = addiction_type(type_str); int intensity = currAdd.get("intensity").as_int(); newProfession.add_addiction(type,intensity); } } // Skills are optional as well if (currProf.has("skills")) { catajson skills = currProf.get("skills"); for (skills.set_begin(); skills.has_curr(); skills.next()) { catajson currSkill = skills.curr(); std::string skill_str = currSkill.get("name").as_string(); // Verifying this skill exists MUST happen later since the // skills have not yet been loaded at this point. int level = currSkill.get("level").as_int(); newProfession.add_skill(skill_str, level); } } // Optional flags if (currProf.has("flags")) { catajson cflags = currProf.get("flags"); for (cflags.set_begin(); cflags.has_curr(); cflags.next()) { newProfession.flags.insert(cflags.curr().as_string()); } } allProfs[ident] = newProfession; } if(!json_good()) exit(1); return allProfs; }