Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/wesnoth/wesnoth
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jul 5, 2014
2 parents d7c7740 + f0ed7ce commit 9bfe3b5
Show file tree
Hide file tree
Showing 20 changed files with 1,545 additions and 672 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
# stderr.txt output + copies
stderr*.txt
stdout*.txt

# project files
.*project
Expand Down
12 changes: 12 additions & 0 deletions projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj
Expand Up @@ -822,6 +822,8 @@
EC219452196064A900420969 /* wmi_pager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC219450196064A900420969 /* wmi_pager.cpp */; };
EC219454196064C500420969 /* wesnoth.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC219453196064C500420969 /* wesnoth.cpp */; };
EC225C9718EF0ABC00025EC4 /* libintl.8.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = EC5C242D18EF07B4001FA499 /* libintl.8.dylib */; };
EC2864301967989000168DFF /* unit_filter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC28642E1967988F00168DFF /* unit_filter.cpp */; };
EC2864331967990500168DFF /* variable_info.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC2864311967990500168DFF /* variable_info.cpp */; };
EC28C58F19423426003B0F5E /* utils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC28C58D19423426003B0F5E /* utils.cpp */; };
EC28C59119423529003B0F5E /* save_index.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC28C59019423528003B0F5E /* save_index.cpp */; };
EC3381C718B520120020DD6F /* theme_list.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC3381C518B520120020DD6F /* theme_list.cpp */; };
Expand Down Expand Up @@ -2022,6 +2024,10 @@
EC219450196064A900420969 /* wmi_pager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wmi_pager.cpp; sourceTree = "<group>"; };
EC219451196064A900420969 /* wmi_pager.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = wmi_pager.hpp; sourceTree = "<group>"; };
EC219453196064C500420969 /* wesnoth.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wesnoth.cpp; sourceTree = "<group>"; };
EC28642E1967988F00168DFF /* unit_filter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = unit_filter.cpp; sourceTree = "<group>"; };
EC28642F1967988F00168DFF /* unit_filter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = unit_filter.hpp; sourceTree = "<group>"; };
EC2864311967990500168DFF /* variable_info.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = variable_info.cpp; sourceTree = "<group>"; };
EC2864321967990500168DFF /* variable_info.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = variable_info.hpp; sourceTree = "<group>"; };
EC28C58D19423426003B0F5E /* utils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = utils.cpp; path = sdl/utils.cpp; sourceTree = "<group>"; };
EC28C58E19423426003B0F5E /* utils.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = utils.hpp; path = sdl/utils.hpp; sourceTree = "<group>"; };
EC28C59019423528003B0F5E /* save_index.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = save_index.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2460,6 +2466,10 @@
29B97315FDCFA39411CA2CEA /* src */ = {
isa = PBXGroup;
children = (
EC2864311967990500168DFF /* variable_info.cpp */,
EC2864321967990500168DFF /* variable_info.hpp */,
EC28642E1967988F00168DFF /* unit_filter.cpp */,
EC28642F1967988F00168DFF /* unit_filter.hpp */,
EC219453196064C500420969 /* wesnoth.cpp */,
EC219450196064A900420969 /* wmi_pager.cpp */,
EC219451196064A900420969 /* wmi_pager.hpp */,
Expand Down Expand Up @@ -4219,6 +4229,7 @@
B5599AED0EC62181008DD061 /* unit_display.cpp in Sources */,
B5599AEE0EC62181008DD061 /* unit_animation.cpp in Sources */,
B5599AEF0EC62181008DD061 /* unit_abilities.cpp in Sources */,
EC2864301967989000168DFF /* unit_filter.cpp in Sources */,
B5599AF00EC62181008DD061 /* unit.cpp in Sources */,
EC84246B18F30D9100FC1EEF /* simple_rng.cpp in Sources */,
B5599AF10EC62181008DD061 /* tstring.cpp in Sources */,
Expand Down Expand Up @@ -4544,6 +4555,7 @@
B52EE91512135AC300CFBDAB /* action.cpp in Sources */,
B52EE91612135AC300CFBDAB /* attack.cpp in Sources */,
EC89A13F1879D17D00A3B0B1 /* lparser.cpp in Sources */,
EC2864331967990500168DFF /* variable_info.cpp in Sources */,
ECFB9FAC193BFB6E00146ED0 /* rect.cpp in Sources */,
B52EE91812135AC300CFBDAB /* manager.cpp in Sources */,
B52EE91A12135AC300CFBDAB /* move.cpp in Sources */,
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -906,6 +906,7 @@ set(wesnoth-main_SRC
unit_types.cpp
utils/sha1.cpp
variable.cpp
variable_info.cpp
variant.cpp
whiteboard/action.cpp
whiteboard/attack.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -538,6 +538,7 @@ wesnoth_sources = Split("""
unit_types.cpp
utils/sha1.cpp
variable.cpp
variable_info.cpp
variant.cpp
whiteboard/action.cpp
whiteboard/attack.cpp
Expand Down
99 changes: 66 additions & 33 deletions src/game_data.cpp
Expand Up @@ -49,6 +49,7 @@
#include "unit.hpp"
#include "unit_map.hpp"

#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>

Expand All @@ -75,7 +76,6 @@ game_data::game_data()
, wml_menu_items_()
, rng_()
, variables_()
, temporaries_()
, phase_(INITIAL)
, can_end_turn_(true)
, scenario_()
Expand All @@ -88,7 +88,6 @@ game_data::game_data(const config& level)
, wml_menu_items_()
, rng_(level)
, variables_(level.child_or_empty("variables"))
, temporaries_()
, phase_(INITIAL)
, can_end_turn_(level["can_end_turn"].to_bool(true))
, scenario_(level["id"])
Expand All @@ -104,69 +103,73 @@ game_data::game_data(const game_data& data)
, wml_menu_items_(data.wml_menu_items_)
, rng_(data.rng_)
, variables_(data.variables_)
, temporaries_()
, phase_(data.phase_)
, can_end_turn_(data.can_end_turn_)
, scenario_(data.scenario_)
, next_scenario_(data.next_scenario_)
{}

//throws
config::attribute_value &game_data::get_variable(const std::string& key)
{
return variable_info(key, true, variable_info::TYPE_SCALAR).as_scalar();
return get_variable_access_write(key).as_scalar();
}

config::attribute_value game_data::get_variable_const(const std::string &key) const
{
variable_info to_get(key, false, variable_info::TYPE_SCALAR);
if (!to_get.is_valid)
try
{
config::attribute_value &to_return = temporaries_[key];
if (key.size() > 7 && key.substr(key.size() - 7) == ".length") {
// length is a special attribute, so guarantee its correctness
to_return = 0;
}
return to_return;
return get_variable_access_read(key).as_scalar();
}
catch(const invalid_variablename_exception&)
{
return config::attribute_value();
}
return to_get.as_scalar();
}

//throws
config& game_data::get_variable_cfg(const std::string& key)
{
return variable_info(key, true, variable_info::TYPE_CONTAINER).as_container();
return get_variable_access_write(key).as_container();
}

void game_data::set_variable(const std::string& key, const t_string& value)
{
get_variable(key) = value;
try
{
get_variable(key) = value;
}
catch(const invalid_variablename_exception&)
{
ERR_NG << "variable " << key << "cannot be set to " << value << std::endl;
}
}

//throws
config& game_data::add_variable_cfg(const std::string& key, const config& value)
{
variable_info to_add(key, true, variable_info::TYPE_ARRAY);
return to_add.vars->add_child(to_add.key, value);
std::vector<config> temp = boost::assign::list_of(value);
return *get_variable_access_write(key).append_array(temp).first;
}

void game_data::clear_variable_cfg(const std::string& varname)
{
variable_info to_clear(varname, false, variable_info::TYPE_CONTAINER);
if(!to_clear.is_valid) return;
if(to_clear.explicit_index) {
to_clear.vars->remove_child(to_clear.key, to_clear.index);
} else {
to_clear.vars->clear_children(to_clear.key);
try
{
get_variable_access_throw(varname).clear(true);
}
catch(const invalid_variablename_exception&)
{
//variable doesn't exist, nothing to delete
}
}

void game_data::clear_variable(const std::string& varname)
{
variable_info to_clear(varname, false);
if(!to_clear.is_valid) return;
if(to_clear.explicit_index) {
to_clear.vars->remove_child(to_clear.key, to_clear.index);
} else {
to_clear.vars->clear_children(to_clear.key);
to_clear.vars->remove_attribute(to_clear.key);
try
{
get_variable_access_throw(varname).clear(false);
}
catch(const invalid_variablename_exception&)
{
//variable doesn't exist, nothing to delete
}
}

Expand Down Expand Up @@ -234,3 +237,33 @@ game_data* game_data::operator=(const game_data* info)
}
return this ;
}

namespace {
bool recursive_activation = false;

} // end anonymous namespace

void game_data::activate_scope_variable(std::string var_name) const
{

if(recursive_activation)
return;
const std::string::iterator itor = std::find(var_name.begin(),var_name.end(),'.');
if(itor != var_name.end()) {
var_name.erase(itor, var_name.end());
}
std::vector<scoped_wml_variable*>::const_reverse_iterator rit;
for(
rit = scoped_variables.rbegin();
rit != scoped_variables.rend();
++rit) {
if((**rit).name() == var_name) {
recursive_activation = true;
if(!(**rit).activated()) {
(**rit).activate();
}
recursive_activation = false;
break;
}
}
}
34 changes: 31 additions & 3 deletions src/game_data.hpp
Expand Up @@ -22,7 +22,7 @@
#include "game_events/wmi_container.hpp"
#include "map_location.hpp"
#include "simple_rng.hpp"

#include "variable_info.hpp"
#include <boost/shared_ptr.hpp>

class config_writer;
Expand Down Expand Up @@ -58,6 +58,26 @@ class game_data : public variable_set {
void set_variable(const std::string& varname, const t_string& value);
config& add_variable_cfg(const std::string& varname, const config& value=config());

void activate_scope_variable(std::string var_name) const;

// returns a variable_access that cannot be used to change the game variables
variable_access_const get_variable_access_read(const std::string& varname) const
{
assert(this != NULL);
activate_scope_variable(varname);
return variable_access_const(varname, variables_);
}

// returns a variable_access that cannot be used to change the game variables
variable_access_create get_variable_access_write(const std::string& varname)
{
assert(this != NULL);
activate_scope_variable(varname);
return variable_access_create(varname, variables_);
}



void clear_variable(const std::string& varname);
void clear_variable_cfg(const std::string& varname); // Clears only the config children

Expand Down Expand Up @@ -103,11 +123,19 @@ class game_data : public variable_set {
game_data* operator=(const game_data* info);

private:


///Used to delete variables.
variable_access_throw get_variable_access_throw(const std::string& varname)
{
assert(this != NULL);
activate_scope_variable(varname);
return variable_access_throw(varname, variables_);
}

game_events::wmi_container wml_menu_items_;
rand_rng::simple_rng rng_;
config variables_;
mutable config temporaries_; // lengths of arrays, etc.
friend struct variable_info;
PHASE phase_;
bool can_end_turn_;
std::string scenario_; /**< the scenario being played */
Expand Down

0 comments on commit 9bfe3b5

Please sign in to comment.