rename location_filter to filter_location

hide the menu item if needs_select=yes and nothing was selected

prevent crash if setting menu items while executing that same item
This commit is contained in:
Patrick Parker 2007-04-05 06:16:33 +00:00
parent bd3de9a852
commit 6f99f6a06d
5 changed files with 60 additions and 35 deletions

View file

@ -499,8 +499,8 @@ _s, _s, _s, _s, Aa, Aa, _s, _s, _s, _s
[/have_unit]
[/not]
[/show_if]
[location_filter]
[/location_filter]
[filter_location]
[/filter_location]
[command]
{UNIT (Troll) (MagicTroll) ( "Magic Troll") $side_number $x1 $y1}
[sound]

View file

@ -314,7 +314,6 @@ bool conditional_passed(const unit_map* units,
namespace {
std::set<std::string> used_items;
/*
jhinrichs, 12.11.2006:
This variable controls the maximum number of hexes in a map, that can be parsed by an event.
@ -402,9 +401,13 @@ public:
bool& rebuild_screen() {return rebuild_screen_;}
void commit_wmi_commands();
private:
bool handle_event_command(const queued_event& event_info, const std::string& cmd, const vconfig cfg, bool& mutated);
typedef std::pair< std::string, config* > wmi_command_change;
std::vector< wmi_command_change > wmi_command_changes_;
std::string name_;
bool first_time_only_;
bool disabled_;
@ -1730,8 +1733,8 @@ bool event_handler::handle_event_command(const queued_event& event_info,
[/have_unit]
[/not]
[/show_if]
[location_filter]
[/location_filter]
[filter_location]
[/filter_location]
[command]
{UNIT (Troll) (Myname) ( _ "Myname") $side_number $x1 $y1}
[/command]
@ -1754,23 +1757,12 @@ bool event_handler::handle_event_command(const queued_event& event_info,
if(cfg.has_child("show_if")) {
mref->show_if = cfg.child("show_if").get_config();
}
if(cfg.has_child("location_filter")) {
mref->location_filter = cfg.child("location_filter").get_config();
if(cfg.has_child("filter_location")) {
mref->filter_location = cfg.child("filter_location").get_config();
}
if(cfg.has_child("command")) {
const bool no_current_handler = mref->command.empty();
mref->command = cfg.child("command").get_config();
if(no_current_handler) {
if(!mref->command.empty()) {
mref->command["name"] = mref->name;
mref->command["first_time_only"] = "no";
event_handler new_handler(mref->command);
events_map.insert(std::pair<std::string,event_handler>(
new_handler.name(), new_handler));
}
} else if(mref->command.empty()) {
mref->command.add_child("allow_undo");
}
config* new_command = new config(cfg.child("command").get_config());
wmi_command_changes_.push_back(wmi_command_change(id, new_command));
}
}
//unit serialization to and from variables
@ -2127,16 +2119,42 @@ bool event_handler::handle_event_command(const queued_event& event_info,
return rval;
}
void event_handler::commit_wmi_commands() {
//commit WML Menu Item command changes
while(wmi_command_changes_.size() > 0) {
wmi_command_change wcc = wmi_command_changes_.back();
wml_menu_item*& mref = state_of_game->wml_menu_items[wcc.first];
const bool no_current_handler = mref->command.empty();
mref->command = *(wcc.second);
if(no_current_handler) {
if(!mref->command.empty()) {
mref->command["name"] = mref->name;
mref->command["first_time_only"] = "no";
event_handler new_handler(mref->command);
events_map.insert(std::pair<std::string,event_handler>(
new_handler.name(), new_handler));
}
} else if(mref->command.empty()) {
mref->command["name"] = mref->name;
mref->command["first_time_only"] = "no";
mref->command.add_child("allow_undo");
}
LOG_NG << "setting command for " << name_ << "\n";
wcc.second->debug(lg::info(lg::engine));
delete wcc.second;
wmi_command_changes_.pop_back();
}
}
bool event_handler::handle_event(const queued_event& event_info, const vconfig conf)
{
vconfig cfg = conf;
if(cfg.null())
cfg = cfg_;
bool mutated = true;
bool skip_messages = false;
vconfig cfg = conf;
if(cfg.null()) {
cfg = cfg_;
}
for(config::all_children_iterator i = cfg.get_config().ordered_begin();
i != cfg.get_config().ordered_end(); ++i) {
@ -2160,6 +2178,7 @@ bool event_handler::handle_event(const queued_event& event_info, const vconfig c
// We do this once event has completed any music alterations
sound::commit_music_changes();
return mutated;
}
@ -2236,6 +2255,9 @@ bool process_event(event_handler& handler, const queued_event& ev)
state_of_game->last_selected = ev.loc1;
}
//wait until the handler finishes processing before changing menu items
handler.commit_wmi_commands();
if(handler.rebuild_screen()) {
handler.rebuild_screen() = false;
screen->recalculate_minimap();

View file

@ -637,8 +637,8 @@ void write_game(const game_state& gamestate, config& cfg, WRITE_GAME_MODE mode)
new_cfg["needs_select"]= (j->second->needs_select) ? "yes" : "no";
if(!j->second->show_if.empty())
new_cfg.add_child("show_if", j->second->show_if);
if(!j->second->location_filter.empty())
new_cfg.add_child("location_filter", j->second->location_filter);
if(!j->second->filter_location.empty())
new_cfg.add_child("filter_location", j->second->filter_location);
if(!j->second->command.empty())
new_cfg.add_child("command", j->second->command);
cfg.add_child("menu_item", new_cfg);
@ -684,8 +684,8 @@ void write_game(config_writer &out, const game_state& gamestate, WRITE_GAME_MODE
out.write_key_val("needs_select", (j->second->needs_select) ? "yes" : "no");
if(!j->second->show_if.empty())
out.write_child("show_if", j->second->show_if);
if(!j->second->location_filter.empty())
out.write_child("location_filter", j->second->location_filter);
if(!j->second->filter_location.empty())
out.write_child("filter_location", j->second->filter_location);
if(!j->second->command.empty())
out.write_child("command", j->second->command);
out.close_child("menu_item");
@ -1420,7 +1420,7 @@ wml_menu_item::wml_menu_item(const std::string& id, const config* cfg) : needs_s
needs_select = utils::string_bool((*cfg)["needs_select"], false);
config const* temp;
if((temp = (*cfg).child("show_if")) != NULL) show_if = *temp;
if((temp = (*cfg).child("location_filter")) != NULL) location_filter = *temp;
if((temp = (*cfg).child("filter_location")) != NULL) filter_location = *temp;
if((temp = (*cfg).child("command")) != NULL) command = *temp;
}
}

View file

@ -60,7 +60,7 @@ struct wml_menu_item
t_string description;
bool needs_select;
config show_if;
config location_filter;
config filter_location;
config command;
};

View file

@ -323,6 +323,7 @@ void play_controller::init_side(const unsigned int team_index, bool is_replay){
std::stringstream player_number_str;
player_number_str << player_number_;
gamestate_.set_variable("side_number",player_number_str.str());
gamestate_.last_selected = gamemap::location::null_location;
/*
normally, events must not be actively fired through replays, because they have been
@ -792,11 +793,13 @@ void play_controller::expand_wml_commands(std::vector<std::string>& items)
for (itor = gs_wmi.begin(); itor != gs_wmi.end()
&& newitems.size() < MAX_WML_COMMANDS; ++itor) {
config& show_if = itor->second->show_if;
config location_filter = itor->second->location_filter;
config filter_location = itor->second->filter_location;
if ((show_if.empty()
|| game_events::conditional_passed(&units_, &show_if))
&& (location_filter.empty()
|| map_.terrain_matches_filter(hex, &location_filter, status_, units_)))
&& (filter_location.empty()
|| map_.terrain_matches_filter(hex, &filter_location, status_, units_))
&& (!itor->second->needs_select
|| gamestate_.last_selected.valid()))
{
wml_commands_.push_back(itor->second);
std::string newitem = itor->second->description;