Permalink
Browse files

Add new aspect 'advancements'

As part of a GSoC proposal I added a new aspect so a scenario editor can control advancements in two ways:
1. Define a aspect with a string-value like "Swordsman, Knight", so the units of interesst will always advance to this
2. Use the LUA-Engine and return a function of the form advance(x, y) which will itself return a string-value
    like "Swordsman, Knight". Everytime a ai-unit advances advance(x, y) will be called.
The corresponding wikipage (http://wiki.wesnoth.org/AiWML) is going to be updated soon.
  • Loading branch information...
1 parent f9a231f commit 29e85847389f1dd0482cd6e6f35b072a6444390c @flixx flixx committed Apr 22, 2013
View
@@ -22,6 +22,10 @@ Version 1.11.4+dev:
* Added a new playlist FULL_MUSIC_PLAYLIST, which contains all Wesnoth tracks
in alphabetical order
* Added -Wno-null-conversion to the CMake pedantic flags.
+ * WML engine:
+ * Added new aspect 'advancements' which with lua engine can handle a
+ function return type of the form f(x, y) -> String. 'advancements'
+ tells the AI to what unit a given unit should advance to.
Version 1.11.4:
* AI:
@@ -40,6 +40,7 @@
[/aspect]
#enddef
+{DEFAULT_ASPECT_EMPTY advancements}
{DEFAULT_ASPECT_VALUE aggression 0.4}
{DEFAULT_ASPECT_VALUE attack_depth 5}
[aspect]
View
@@ -989,6 +989,9 @@
[entry]
name = "Fedor Khod'kov (teddy/fkhodkov)"
[/entry]
+ [entry]
+ name = "Felix Bauer (flix)"
+ [/entry]
[entry]
name = "Francesco Gigli (Jaramir)"
[/entry]
View
@@ -864,7 +864,7 @@ def local_sanity_check(filename, nav, key, prefix, value, comment):
"recruitment_ignore_bad_combat",
"recruitment_pattern",
"villages_per_scout", "leader_value", "village_value",
- "aggression", "caution", "attack_depth", "grouping"):
+ "aggression", "caution", "attack_depth", "grouping", "advancements"):
print errlead + key + " outside [ai] scope"
# Bad [recruit] attribute
if parent in ("[allow_recruit]", "[disallow_recruit]") and key == "recruit":
@@ -3050,6 +3050,62 @@
RelativePath="..\..\src\ai\lua\lua_object.hpp"
>
</File>
+ <File
+ RelativePath="..\..\src\ai\lua\unit_advancements_aspect.cpp"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\ai\lua\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\ai\lua\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Debug_with_VLD|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\ai\lua\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Test_Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\ai\lua\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Test_Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\ai\lua\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="ReleaseDEBUG|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\ai\lua\"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\..\src\ai\lua\unit_advancements_aspect.hpp"
+ >
+ </File>
</Filter>
<Filter
Name="akihara"
View
@@ -631,6 +631,7 @@ set(wesnoth-main_SRC
ai/interface.cpp
ai/lua/core.cpp
ai/lua/lua_object.cpp
+ ai/lua/unit_advancements_aspect.cpp
ai/manager.cpp
ai/registry.cpp
ai/testing.cpp
View
@@ -203,6 +203,7 @@ wesnoth_sources = Split("""
ai/interface.cpp
ai/lua/core.cpp
ai/lua/lua_object.cpp
+ ai/lua/unit_advancements_aspect.cpp
ai/manager.cpp
ai/registry.cpp
ai/testing.cpp
View
@@ -179,8 +179,8 @@ team& action_result::get_my_team() const
// attack_result
-attack_result::attack_result( side_number side, const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon, double aggression)
- : action_result(side), attacker_loc_(attacker_loc), defender_loc_(defender_loc), attacker_weapon_(attacker_weapon), aggression_(aggression){
+attack_result::attack_result( side_number side, const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect& advancements)
+ : action_result(side), attacker_loc_(attacker_loc), defender_loc_(defender_loc), attacker_weapon_(attacker_weapon), aggression_(aggression), advancements_(advancements){
}
@@ -302,7 +302,8 @@ void attack_result::do_execute()
}
recorder.add_seed("attack", rand_rng::get_last_seed());
attack_unit(attacker_loc_, defender_loc_, attacker_weapon, defender_weapon);
- dialogs::advance_unit(attacker_loc_, true);
+
+ dialogs::advance_unit(attacker_loc_, true, false, advancements_);
const unit_map::const_iterator defender = resources::units->find(defender_loc_);
if(defender != resources::units->end()) {
@@ -922,9 +923,10 @@ attack_result_ptr actions::execute_attack_action( side_number side,
const map_location& attacker_loc,
const map_location& defender_loc,
int attacker_weapon,
- double aggression)
+ double aggression,
+ const unit_advancements_aspect& advancements)
{
- attack_result_ptr action(new attack_result(side,attacker_loc,defender_loc,attacker_weapon,aggression));
+ attack_result_ptr action(new attack_result(side,attacker_loc,defender_loc,attacker_weapon,aggression,advancements));
execute ? action->execute() : action->check_before();
return action;
}
View
@@ -23,6 +23,7 @@
#include "game_info.hpp"
#include "../actions/move.hpp"
+#include "lua/unit_advancements_aspect.hpp"
namespace pathfind {
struct plain_route;
@@ -130,7 +131,8 @@ class attack_result : public action_result {
const map_location& attacker_loc,
const map_location& defender_loc,
int attacker_weapon,
- double aggression );
+ double aggression,
+ const unit_advancements_aspect& advancements = unit_advancements_aspect());
enum tresult {
E_EMPTY_ATTACKER = 1001,
@@ -156,6 +158,7 @@ class attack_result : public action_result {
const map_location& defender_loc_;
int attacker_weapon_;
double aggression_;
+ const unit_advancements_aspect& advancements_;
};
class move_result : public action_result {
@@ -314,7 +317,8 @@ static attack_result_ptr execute_attack_action( side_number side,
const map_location& attacker_loc,
const map_location& defender_loc,
int attacker_weapon,
- double aggression );
+ double aggression,
+ const unit_advancements_aspect& advancements = unit_advancements_aspect());
/**
@@ -27,6 +27,7 @@
#include "../../util.hpp"
#include "../../serialization/string_utils.hpp"
#include "../../resources.hpp"
+#include "../lua/unit_advancements_aspect.hpp"
namespace ai {
@@ -202,6 +203,35 @@ class config_value_translator<terrain_filter> {
}
};
+template<>
+class config_value_translator<unit_advancements_aspect> {
+public:
+
+ static unit_advancements_aspect cfg_to_value(const config &cfg)
+ {
+ return unit_advancements_aspect(cfg["value"]);
+ }
+
+ static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
+ {
+ value = cfg_to_value(cfg);
+ }
+
+ static void value_to_cfg(const unit_advancements_aspect &value, config &cfg)
+ {
+ cfg["value"] = value.get_value();
+
+ }
+
+ static config value_to_cfg(const unit_advancements_aspect &value)
+ {
+ config cfg;
+ value_to_cfg(value,cfg);
+ return cfg;
+ }
+};
+
+
// variant value translator
View
@@ -59,6 +59,7 @@ void configuration::init(const config &game_config)
ai_configurations_.clear();
era_ai_configurations_.clear();
well_known_aspects.clear();
+ well_known_aspects.push_back(well_known_aspect("advancements"));
well_known_aspects.push_back(well_known_aspect("aggression"));
well_known_aspects.push_back(well_known_aspect("attack_depth"));
well_known_aspects.push_back(well_known_aspect("attacks"));
View
@@ -90,14 +90,16 @@ team& readwrite_context_impl::current_team_w()
attack_result_ptr readwrite_context_impl::execute_attack_action(const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon){
unit_map::iterator i = resources::units->find(attacker_loc);
double m_aggression = i.valid() && i->can_recruit() ? get_leader_aggression() : get_aggression();
- return actions::execute_attack_action(get_side(),true,attacker_loc,defender_loc,attacker_weapon, m_aggression);
+ const unit_advancements_aspect& m_advancements = get_advancements();
+ return actions::execute_attack_action(get_side(),true,attacker_loc,defender_loc,attacker_weapon, m_aggression, m_advancements);
}
attack_result_ptr readonly_context_impl::check_attack_action(const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon){
unit_map::iterator i = resources::units->find(attacker_loc);
double m_aggression = i.valid() && i->can_recruit() ? get_leader_aggression() : get_aggression();
- return actions::execute_attack_action(get_side(),false,attacker_loc,defender_loc,attacker_weapon, m_aggression);
+ const unit_advancements_aspect& m_advancements = get_advancements();
+ return actions::execute_attack_action(get_side(),false,attacker_loc,defender_loc,attacker_weapon, m_aggression, m_advancements);
}
@@ -152,6 +154,7 @@ readonly_context_impl::readonly_context_impl(side_context &context, const config
: cfg_(cfg),
engines_(),
known_aspects_(),
+ advancements_(),
aggression_(),
attack_depth_(),
aspects_(),
@@ -195,6 +198,7 @@ readonly_context_impl::readonly_context_impl(side_context &context, const config
init_side_context_proxy(context);
manager::add_gamestate_observer(this);
+ add_known_aspect("advancements", advancements_);
add_known_aspect("aggression",aggression_);
add_known_aspect("attack_depth",attack_depth_);
add_known_aspect("attacks",attacks_);
@@ -491,6 +495,17 @@ std::map<map_location,defensive_position>& readonly_context_impl::defensive_posi
}
+const unit_advancements_aspect& readonly_context_impl::get_advancements() const
+{
+ if (advancements_) {
+ return advancements_->get();
+ }
+
+ static unit_advancements_aspect uaa = unit_advancements_aspect();
+ return uaa;
+}
+
+
double readonly_context_impl::get_aggression() const
{
if (aggression_) {
@@ -842,6 +857,7 @@ int readonly_context_impl::get_villages_per_scout() const
return 0;
}
+
bool readonly_context_impl::is_dst_src_valid_lua() const
{
return dst_src_valid_lua_;
View
@@ -24,6 +24,7 @@
#include "game_info.hpp"
#include "../generic_event.hpp"
#include "../config.hpp"
+#include "lua/unit_advancements_aspect.hpp"
//#include "../unit.hpp"
@@ -207,6 +208,9 @@ class readonly_context : public virtual side_context {
virtual std::map<map_location,defensive_position>& defensive_position_cache() const = 0;
+ virtual const unit_advancements_aspect& get_advancements() const = 0;
+
+
virtual double get_aggression() const = 0;
@@ -326,6 +330,7 @@ class readonly_context : public virtual side_context {
virtual int get_villages_per_scout() const = 0;
+
virtual bool is_active(const std::string &time_of_day, const std::string &turns) const = 0;
virtual bool is_dst_src_valid_lua() const = 0;
@@ -607,6 +612,12 @@ class readonly_context_proxy : public virtual readonly_context, public virtual s
}
+ virtual const unit_advancements_aspect& get_advancements() const
+ {
+ return target_->get_advancements();
+ }
+
+
virtual double get_aggression() const
{
return target_->get_aggression();
@@ -1264,6 +1275,9 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly
virtual std::map<map_location,defensive_position>& defensive_position_cache() const;
+ virtual const unit_advancements_aspect& get_advancements() const;
+
+
virtual double get_aggression() const;
@@ -1449,6 +1463,7 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly
known_aspect_map known_aspects_;
+ aspect_type< unit_advancements_aspect >::typesafe_ptr advancements_;
aspect_type<double>::typesafe_ptr aggression_;
aspect_type<int>::typesafe_ptr attack_depth_;
aspect_map aspects_;
@@ -31,6 +31,7 @@
#include "../default/contexts.hpp"
#include "terrain_filter.hpp"
#include "resources.hpp"
+#include "unit_advancements_aspect.hpp"
namespace ai {
@@ -190,6 +191,12 @@ inline boost::shared_ptr<std::vector<target> > lua_object< std::vector<target> >
return targets;
}
+template <>
+inline boost::shared_ptr<unit_advancements_aspect> lua_object<unit_advancements_aspect>::to_type(lua_State *L, int n)
+{
+ boost::shared_ptr<unit_advancements_aspect> uaa = boost::shared_ptr<unit_advancements_aspect>(new unit_advancements_aspect(L, n));
+ return uaa;
+}
} // end of namespace ai
Oops, something went wrong.

0 comments on commit 29e8584

Please sign in to comment.