vconfig::child_list vconfig::get_children(const std::string& key) const { vconfig::child_list res; for (const config::any_child &child : cfg_->all_children_range()) { if (child.key == key) { res.push_back(vconfig(child.cfg, cache_)); } else if (child.key == "insert_tag") { vconfig insert_cfg(child.cfg); if(insert_cfg["name"] == key) { try { config::const_child_itors range = as_nonempty_range(insert_cfg["variable"]); for (const config& child : range) { res.push_back(vconfig(child, true)); } } catch(const invalid_variablename_exception&) { res.push_back(empty_vconfig()); } } } } return res; }
vconfig::child_list vconfig::get_children(const std::string& key) const { vconfig::child_list res; BOOST_FOREACH(const config::any_child &child, cfg_->all_children_range()) { if (child.key == key) { res.push_back(vconfig(child.cfg, cache_)); } else if (child.key == "insert_tag") { vconfig insert_cfg(child.cfg); if(insert_cfg["name"] == key) { variable_info vinfo(insert_cfg["variable"], false, variable_info::TYPE_CONTAINER); if(!vinfo.is_valid) { //push back an empty tag res.push_back(empty_vconfig()); } else if(vinfo.explicit_index) { res.push_back(vconfig(vinfo.as_container(), true)); } else { variable_info::array_range range = vinfo.as_array(); if(range.first == range.second) { //push back an empty tag res.push_back(empty_vconfig()); } while(range.first != range.second) { res.push_back(vconfig(*range.first++, true)); } } } } } return res; }
static terrain_filter cfg_to_value(const config &cfg) { if (const config &v = cfg.child("value")) { return terrain_filter(vconfig(v), *resources::units); } static config c("not"); return terrain_filter(vconfig(c),*resources::units); }
static terrain_filter cfg_to_value(const config &cfg) { if (const config &v = cfg.child("value")) { return terrain_filter(vconfig(v), manager::get_ai_info().units); } static config c("not"); return terrain_filter(vconfig(c),manager::get_ai_info().units); }
config vconfig::get_parsed_config() const { // Keeps track of insert_tag variables. static std::set<std::string> vconfig_recursion; config res; BOOST_FOREACH(const config::attribute &i, cfg_->attribute_range()) { res[i.first] = expand(i.first); } BOOST_FOREACH(const config::any_child &child, cfg_->all_children_range()) { if (child.key == "insert_tag") { vconfig insert_cfg(child.cfg); const t_string& name = insert_cfg["name"]; const t_string& vname = insert_cfg["variable"]; if(!vconfig_recursion.insert(vname).second) { throw recursion_error("vconfig::get_parsed_config() infinite recursion detected, aborting"); } try { variable_info vinfo(vname, false, variable_info::TYPE_CONTAINER); if(!vinfo.is_valid) { res.add_child(name); //add empty tag } else if(vinfo.explicit_index) { res.add_child(name, vconfig(vinfo.as_container()).get_parsed_config()); } else { variable_info::array_range range = vinfo.as_array(); if(range.first == range.second) { res.add_child(name); //add empty tag } while(range.first != range.second) { res.add_child(name, vconfig(*range.first++).get_parsed_config()); } } vconfig_recursion.erase(vname); } catch(recursion_error &err) { vconfig_recursion.erase(vname); WRN_NG << err.message << std::endl; if(vconfig_recursion.empty()) { res.add_child("insert_tag", insert_cfg.get_config()); } else { // throw to the top [insert_tag] which started the recursion throw; } } } else { res.add_child(child.key, vconfig(child.cfg).get_parsed_config()); } } return res; }
boost::shared_ptr<attacks_vector> aspect_attacks::analyze_targets() const { const move_map& srcdst = get_srcdst(); const move_map& dstsrc = get_dstsrc(); const move_map& enemy_srcdst = get_enemy_srcdst(); const move_map& enemy_dstsrc = get_enemy_dstsrc(); boost::shared_ptr<attacks_vector> res(new attacks_vector()); unit_map& units_ = *resources::units; std::vector<map_location> unit_locs; for(unit_map::const_iterator i = units_.begin(); i != units_.end(); ++i) { if (i->side() == get_side() && i->attacks_left() && !(i->can_recruit() && get_passive_leader())) { if (!i->matches_filter(vconfig(filter_own_), i->get_location())) { continue; } unit_locs.push_back(i->get_location()); } } bool used_locations[6]; std::fill(used_locations,used_locations+6,false); moves_map dummy_moves; move_map fullmove_srcdst, fullmove_dstsrc; calculate_possible_moves(dummy_moves,fullmove_srcdst,fullmove_dstsrc,false,true); unit_stats_cache().clear(); for(unit_map::const_iterator j = units_.begin(); j != units_.end(); ++j) { // Attack anyone who is on the enemy side, // and who is not invisible or petrified. if (current_team().is_enemy(j->side()) && !j->incapacitated() && !j->invisible(j->get_location())) { if (!j->matches_filter(vconfig(filter_enemy_), j->get_location())) { continue; } map_location adjacent[6]; get_adjacent_tiles(j->get_location(), adjacent); attack_analysis analysis; analysis.target = j->get_location(); analysis.vulnerability = 0.0; analysis.support = 0.0; do_attack_analysis(j->get_location(), srcdst, dstsrc, fullmove_srcdst, fullmove_dstsrc, enemy_srcdst, enemy_dstsrc, adjacent,used_locations,unit_locs,*res,analysis, current_team()); } } return res; }
config vconfig::get_parsed_config() const { // Keeps track of insert_tag variables. static std::set<std::string> vconfig_recursion; config res; for (const config::attribute &i : cfg_->attribute_range()) { res[i.first] = expand(i.first); } for (const config::any_child &child : cfg_->all_children_range()) { if (child.key == "insert_tag") { vconfig insert_cfg(child.cfg); const t_string& name = insert_cfg["name"]; const t_string& vname = insert_cfg["variable"]; if(!vconfig_recursion.insert(vname).second) { throw recursion_error("vconfig::get_parsed_config() infinite recursion detected, aborting"); } try { config::const_child_itors range = as_nonempty_range(vname); for (const config& child : range) { res.add_child(name, vconfig(child).get_parsed_config()); } } catch(const invalid_variablename_exception&) { res.add_child(name); } catch(recursion_error &err) { vconfig_recursion.erase(vname); WRN_NG << err.message << std::endl; if(vconfig_recursion.empty()) { res.add_child("insert_tag", insert_cfg.get_config()); } else { // throw to the top [insert_tag] which started the recursion throw; } } vconfig_recursion.erase(vname); } else { res.add_child(child.key, vconfig(child.cfg).get_parsed_config()); } } return res; }
static terrain_filter variant_to_value(const wfl::variant &var) { static config c("not"); terrain_filter value(vconfig(c),resources::filter_con); variant_to_value(var,value); return value; }
static terrain_filter variant_to_value(const variant &var) { static config c("not"); terrain_filter value(vconfig(c),*resources::units); variant_to_value(var,value); return value; }
void show_story(display &disp, const std::string &scenario_name, const config::const_child_itors &story) { const int total_segments = count_segments(story); int segment_count = 0; config::const_child_iterator itor = story.first; storyscreen::START_POSITION startpos = storyscreen::START_BEGINNING; while (itor != story.second) { storyscreen::controller ctl(disp, vconfig(*itor, true), scenario_name, segment_count, total_segments); storyscreen::STORY_RESULT result = ctl.show(startpos); switch(result) { case storyscreen::NEXT: if(itor != story.second) { ++itor; ++segment_count; startpos = storyscreen::START_BEGINNING; } break; case storyscreen::BACK: if(itor != story.first) { --itor; --segment_count; startpos = storyscreen::START_END; } break; case storyscreen::QUIT: return; } } return; }
static terrain_filter variant_to_value(const variant &var) { static config c("not"); terrain_filter value(vconfig(c),manager::get_ai_info().units); variant_to_value(var,value); return value; }
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg) { switch (lua_type(L, index)) { case LUA_TTABLE: { config cfg; bool ok = luaW_toconfig(L, index, cfg); if (!ok) return false; vcfg = vconfig(cfg, true); break; } case LUA_TUSERDATA: if (vconfig * ptr = static_cast<vconfig *> (luaL_testudata(L, index, vconfigKey))) { vcfg = *ptr; } else { return false; } case LUA_TNONE: case LUA_TNIL: break; default: return false; } return true; }
vconfig vconfig::all_children_iterator::get_child() const { if (inner_index_ >= 0 && i_->key == "insert_tag") { variable_info vinfo(vconfig(i_->cfg)["variable"], false, variable_info::TYPE_CONTAINER); if(!vinfo.is_valid) { return empty_vconfig(); } else if(inner_index_ == 0) { return vconfig(vinfo.as_container(), true); } variable_info::array_range r = vinfo.as_array(); std::advance(r.first, inner_index_); return vconfig(*r.first, true); } return vconfig(i_->cfg, cache_); }
void protect_goal::on_create() { goal::on_create(); if (!cfg_["engine"].empty() && cfg_["engine"] != "cpp") { unrecognized(); value_ = 0; return; } if (const config::attribute_value *v = cfg_.get("value")) { value_ = v->to_double(0); } if (const config::attribute_value *v = cfg_.get("protect_radius")) { radius_ = (*v).to_int(1); } if (radius_<1) { radius_=20; } const config &criteria = cfg_.child("criteria"); if (criteria) { filter_ptr_.reset(new terrain_filter(vconfig(criteria),resources::filter_con)); } }
void protect_goal::on_create() { goal::on_create(); if (cfg_.has_attribute("value")) { try { value_ = boost::lexical_cast<double>(cfg_["value"]); } catch (boost::bad_lexical_cast){ ERR_AI_GOAL << "bad value of protect_goal"<<std::endl; value_ = 0; } } if (cfg_.has_attribute("protect_radius")) { try { radius_ = boost::lexical_cast<int>(cfg_["protect_radius"]); } catch (boost::bad_lexical_cast){ ERR_AI_GOAL << "bad protection radius of protect_goal"<<std::endl; radius_ = 1; } } if (radius_<1) { radius_=20; } const config &criteria = cfg_.child("criteria"); if (criteria) { filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),get_info().units)); } }
void undo_action::execute_redo_umc_wml() { assert(resources::lua_kernel); for(const config& c : umc_commands_redo) { resources::lua_kernel->run_wml_action("command", vconfig(c), game_events::queued_event("redo", map_location(), map_location(), config())); } }
std::string vconfig::all_children_iterator::get_key() const { const std::string &key = i_->key; if (inner_index_ >= 0 && key == "insert_tag") { return vconfig(i_->cfg)["name"]; } return key; }
/** * Gets the parsed field of a vconfig object (_index metamethod). * Special fields __literal, __shallow_literal, __parsed, and * __shallow_parsed, return Lua tables. */ static int impl_vconfig_get(lua_State *L) { vconfig *v = static_cast<vconfig *>(lua_touserdata(L, 1)); if (lua_isnumber(L, 2)) { vconfig::all_children_iterator i = v->ordered_begin(); unsigned len = std::distance(i, v->ordered_end()); unsigned pos = lua_tointeger(L, 2) - 1; if (pos >= len) return 0; std::advance(i, pos); lua_createtable(L, 2, 0); lua_pushstring(L, i.get_key().c_str()); lua_rawseti(L, -2, 1); luaW_pushvconfig(L, vconfig(i.get_child())); lua_rawseti(L, -2, 2); return 1; } char const *m = luaL_checkstring(L, 2); if (strcmp(m, "__literal") == 0) { luaW_pushconfig(L, v->get_config()); return 1; } if (strcmp(m, "__parsed") == 0) { luaW_pushconfig(L, v->get_parsed_config()); return 1; } bool shallow_literal = strcmp(m, "__shallow_literal") == 0; if (shallow_literal || strcmp(m, "__shallow_parsed") == 0) { lua_newtable(L); BOOST_FOREACH(const config::attribute &a, v->get_config().attribute_range()) { if (shallow_literal) luaW_pushscalar(L, a.second); else luaW_pushscalar(L, v->expand(a.first)); lua_setfield(L, -2, a.first.c_str()); } vconfig::all_children_iterator i = v->ordered_begin(), i_end = v->ordered_end(); if (shallow_literal) { i.disable_insertion(); i_end.disable_insertion(); } for (int j = 1; i != i_end; ++i, ++j) { lua_createtable(L, 2, 0); lua_pushstring(L, i.get_key().c_str()); lua_rawseti(L, -2, 1); luaW_pushvconfig(L, i.get_child()); lua_rawseti(L, -2, 2); lua_rawseti(L, -2, j); } return 1; }
/** * Returns a child of *this whose key is @a key. * If no such child exists, returns an unconstructed vconfig (use null() to test * for this). */ vconfig vconfig::child(const std::string& key) const { if (const config &natural = cfg_->child(key)) { return vconfig(natural, cache_); } BOOST_FOREACH(const config &ins, cfg_->child_range("insert_tag")) { vconfig insert_cfg(ins); if(insert_cfg["name"] == key) { variable_info vinfo(insert_cfg["variable"], false, variable_info::TYPE_CONTAINER); if(!vinfo.is_valid) { return empty_vconfig(); } return vconfig(vinfo.as_container(), true); } } return unconstructed_vconfig(); }
vconfig vconfig::all_children_iterator::get_child() const { if (inner_index_ >= 0 && i_->key == "insert_tag") { try { config::const_child_itors range = as_nonempty_range(vconfig(i_->cfg)["variable"]); std::advance(range.first, inner_index_); return vconfig(*range.first, true); } catch(const invalid_variablename_exception&) { return empty_vconfig(); } } return vconfig(i_->cfg, cache_); }
//@todo: push to subclass bool goal::matches_unit(unit_map::const_iterator u) { if (!u.valid()) { return false; } const config &criteria = cfg_.child("criteria"); if (!criteria) { return false; } return u->second.matches_filter(vconfig(criteria),u->first); }
/** * Returns a child of *this whose key is @a key. * If no such child exists, returns an unconstructed vconfig (use null() to test * for this). */ vconfig vconfig::child(const std::string& key) const { if (const config &natural = cfg_->child(key)) { return vconfig(natural, cache_); } for (const config &ins : cfg_->child_range("insert_tag")) { vconfig insert_cfg(ins); if(insert_cfg["name"] == key) { try { config::const_child_itors range = as_nonempty_range(insert_cfg["variable"]); return vconfig(*range.first, true); } catch(const invalid_variablename_exception&) { return empty_vconfig(); } } } return unconstructed_vconfig(); }
void target_location_goal::on_create() { goal::on_create(); if (cfg_.has_attribute("value")) { try { value_ = boost::lexical_cast<double>(cfg_["value"]); } catch (boost::bad_lexical_cast){ ERR_AI_GOAL << "bad value of goal"<<std::endl; value_ = 0; } } const config &criteria = cfg_.child("criteria"); if (criteria) { filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),resources::filter_con)); } }
LEVEL_RESULT playsingle_controller::play_scenario( const config::const_child_itors &story, upload_log &log, bool skip_replay) { LOG_NG << "in playsingle_controller::play_scenario()...\n"; // Start music. foreach (const config &m, level_.child_range("music")) { sound::play_music_config(m); } sound::commit_music_changes(); if(!skip_replay) { foreach (const config &s, story) { show_storyscreen(*gui_, vconfig(s, true), level_["name"]); }
vconfig::all_children_iterator& vconfig::all_children_iterator::operator++() { if (inner_index_ >= 0 && i_->key == "insert_tag") { variable_info vinfo(vconfig(i_->cfg)["variable"], false, variable_info::TYPE_CONTAINER); if(vinfo.is_valid && !vinfo.explicit_index) { variable_info::array_range range = vinfo.as_array(); if (++inner_index_ < std::distance(range.first, range.second)) { return *this; } inner_index_ = 0; } } ++i_; return *this; }
void target_location_goal::on_create() { goal::on_create(); if (!cfg_["engine"].empty() && cfg_["engine"] != "cpp") { unrecognized(); value_ = 0; return; } if (cfg_.has_attribute("value")) { value_ = cfg_["value"].to_double(0); } const config &criteria = cfg_.child("criteria"); if (criteria) { filter_ptr_.reset(new terrain_filter(vconfig(criteria),resources::filter_con)); } }
void tlist::objectives(twindow& window, bool global) { play_controller* controller = resources::controller; int side_num = current_team_.side(); if (global) { side_num = -1; return; } config cfg; cfg["side"] = side_num; game_events::handle_event_command("show_objectives", game_events::queued_event("_from_interface", map_location(), map_location(), config()), vconfig(cfg)); dialogs::show_objectives(controller->level(), current_team_.objectives()); current_team_.reset_objectives_changed(); }
void target_unit_goal::add_targets(std::back_insert_iterator< std::vector< target > > target_list) { if (!(this)->active()) { return; } const config &criteria = cfg_.child("criteria"); if (!criteria) return; //find the enemy leaders and explicit targets foreach (const unit &u, *resources::units) { if (u.matches_filter(vconfig(criteria), u.get_location())) { LOG_AI_GOAL << "found explicit target unit at ... " << u.get_location() << " with value: " << value() << "\n"; *target_list = target(u.get_location(), value(), target::EXPLICIT); } } }
void target_unit_goal::add_targets(std::back_insert_iterator< std::vector< target >> target_list) { if (!(this)->active()) { return; } const config &criteria = cfg_.child("criteria"); if (!criteria) return; //find the enemy leaders and explicit targets const unit_filter ufilt{ vconfig(criteria) }; for (const unit &u : resources::gameboard->units()) { if (ufilt( u )) { LOG_AI_GOAL << "found explicit target unit at ... " << u.get_location() << " with value: " << value() << "\n"; *target_list = target(u.get_location(), value(), target::TYPE::EXPLICIT); } } }
void show_story(CVideo& video, const std::string &scenario_name, const config::const_child_itors &story) { events::event_context story_context; int segment_count = 0; config::const_child_iterator itor = story.begin(); storyscreen::START_POSITION startpos = storyscreen::START_BEGINNING; while (itor != story.end()) { storyscreen::controller ctl(video, vconfig(*itor, true), scenario_name, segment_count); storyscreen::STORY_RESULT result = ctl.show(startpos); switch(result) { case storyscreen::NEXT: if(itor != story.end()) { ++itor; ++segment_count; startpos = storyscreen::START_BEGINNING; } break; case storyscreen::BACK: if(itor != story.begin()) { --itor; --segment_count; startpos = storyscreen::START_END; } break; case storyscreen::QUIT: video2::trigger_full_redraw(); return; } } video2::trigger_full_redraw(); return; }