50 #define LOG_LUA LOG_STREAM(info, log_ai_engine_lua)
51 #define ERR_LUA LOG_STREAM(err, log_ai_engine_lua)
145 int hashed_index = (loc.
x + 1) * 16384 + (loc.
y + 1) + 2000;
168 unit* leader =
nullptr;
193 bool unreach_is_ok =
false;
208 return ai_move(L,
true,
false);
213 return ai_move(L,
false,
false);
224 int attacker_weapon = -1;
229 if (attacker_weapon != -1) {
417 ai::attacks_vector::const_iterator it = attacks.begin();
418 for (
int i = 1; it != attacks.end(); ++it, ++
i)
429 std::set<map_location> locs;
496 int size = recruiting.size();
569 std::vector< std::pair < map_location, map_location > >::const_iterator move = moves.begin();
571 for (
int i = 1; move != moves.end(); ++move, ++
i)
678 move_map::const_iterator it = m.begin();
691 while (key == it->first) {
705 }
while (it != m.end());
790 if(iter == aspects.end()) {
795 if(iter->first ==
"attacks") {
802 typedef std::vector<std::string> string_list;
814 std::set<map_location>
result;
815 aspect->get().get_locations(result);
826 if (!u.valid() || u->side() != my_side) {
841 lua_pushstring(L,
"attempted to write to the ai.aspects table, which is read-only");
867 static luaL_Reg const callbacks[] = {
914 {
nullptr,
nullptr } };
933 static luaL_Reg const mutating_callbacks[] = {
943 {
nullptr,
nullptr } };
976 return length_ai + 1;
986 ERR_LUA <<
"error while initializing ai: " <<m <<
'\n';
1028 ERR_LUA <<
"error while creating ai function: " <<m <<
'\n';
1128 l_obj->store(L, -1);
static int ai_stopunit_select(lua_State *L, bool exec, bool remove_movement, bool remove_attacks)
bool leader_threat
Is true if the unit is a threat to our leader.
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
#define lua_isnoneornil(L, n)
static int cfun_ai_get_passive_leader_shares_keep(lua_State *L)
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
static void push_move_map(lua_State *L, const move_map &m)
double avg_damage_taken
The average hitpoints damage taken.
virtual const move_map & get_srcdst() const =0
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
#define lua_pushcfunction(L, f)
static ai::readonly_context & get_readonly_context(lua_State *L)
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
virtual void set_src_dst_enemy_valid_lua()=0
virtual bool is_src_dst_valid_lua() const =0
static int cfun_ai_check_synced_command(lua_State *L)
const map_location & get_location() const
static int cfun_ai_execute_stopunit_attacks(lua_State *L)
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
virtual const aspect_map & get_aspects() const =0
virtual void set_src_dst_valid_lua()=0
LUA_API void lua_settop(lua_State *L, int idx)
static attack_result_ptr execute_attack_action(side_number side, bool execute, const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect &advancements=unit_advancements_aspect())
Ask the game to attack an enemy defender using our unit attacker from attackers current location...
bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error)
Calls a Lua function stored below its nArgs arguments at the top of the stack.
LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
virtual std::string get_grouping() const =0
LUA_API void lua_pushboolean(lua_State *L, int b)
static int transform_ai_action(lua_State *L, ai::action_result_ptr action_result)
double avg_damage_inflicted
The average hitpoints damage inflicted.
Managing the AI-Game interaction - AI actions and their results.
virtual config get_leader_goal() const =0
LUA_API void lua_getglobal(lua_State *L, const char *var)
virtual const map_location & suitable_keep(const map_location &leader_location, const pathfind::paths &leader_paths)=0
get most suitable keep for leader - nearest free that can be reached in 1 turn, if none - return near...
static int cfun_ai_get_scout_village_targeting(lua_State *L)
static int cfun_ai_execute_stopunit_moves(lua_State *L)
static int cfun_ai_execute_recruit(lua_State *L)
LUA_API int lua_gettop(lua_State *L)
static int cfun_ai_get_avoid(lua_State *L)
virtual bool is_dst_src_enemy_valid_lua() const =0
static int cfun_ai_check_attack(lua_State *L)
static int cfun_ai_execute_recall(lua_State *L)
void get_persistent_data(config &) const
LUA_API void lua_settable(lua_State *L, int idx)
static int cfun_ai_get_dstsrc(lua_State *L)
#define lua_tointeger(L, i)
double vulnerability
The vulnerability is the power projection of enemy units onto the hex we're standing on...
GLuint const GLfloat * val
virtual void set_dst_src_valid_lua()=0
lua_ai_context & context_
virtual bool get_simple_targeting() const =0
static int cfun_ai_get_village_value(lua_State *L)
static lua_ai_context * create(lua_State *L, char const *code, engine_lua *engine)
virtual bool get_support_villages() const =0
virtual const move_map & get_enemy_dstsrc() const =0
static int cfun_attack_rating(lua_State *L)
#define lua_tonumber(L, i)
virtual const move_map & get_enemy_srcdst() const =0
static int cfun_ai_get_srcdst(lua_State *L)
LUA_API int lua_isuserdata(lua_State *L, int idx)
virtual double get_village_value() const =0
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
double resources_used
The sum of the values of units used in the attack.
double chance_to_kill
Estimated % chance to kill the unit.
static int cfun_ai_recalculate_move_maps(lua_State *L)
static config unit_name(const unit *u)
virtual const attacks_vector & get_attacks() const =0
void lua_push(lua_State *L, const T &val)
void handle(const config &cfg, bool read_only, lua_object_ptr l_obj)
map_location luaW_checklocation(lua_State *L, int index)
Converts an optional table or pair of integers to a map location object.
static void push_location_key(lua_State *L, const map_location &loc)
static int cfun_ai_get_leader_goal(lua_State *L)
LUA_API int lua_absindex(lua_State *L, int idx)
int target_starting_damage
Lua object(value) wrapper implementation.
static int cfun_ai_execute_attack(lua_State *L)
static int cfun_ai_get_support_villages(lua_State *L)
static int ai_recall(lua_State *L, bool exec)
virtual bool get_passive_leader() const =0
static int cfun_ai_execute_synced_command(lua_State *L)
unit * luaW_tounit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
GLuint GLsizei GLsizei * length
virtual const unit_advancements_aspect & get_advancements() const =0
double target_value
The value of the unit being targeted.
std::vector< attack_analysis > attacks_vector
static int cfun_ai_get_villages_per_scout(lua_State *L)
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
#define lua_upvalueindex(i)
static int cfun_ai_get_leader_value(lua_State *L)
A small explanation about what's going on here: Each action has access to two game_info objects First...
static int ai_synced_command(lua_State *L, bool exec)
void set_arguments(const config &)
LUA_API void lua_setglobal(lua_State *L, const char *var)
static recall_result_ptr execute_recall_action(side_number side, bool execute, const std::string &unit_id, const map_location &where, const map_location &from)
Ask the game to recall a unit for us on specified location.
void set_persistent_data(const config &)
static int cfun_ai_is_src_dst_valid(lua_State *L)
bool luaW_getglobal(lua_State *L, const std::vector< std::string > &path)
Pushes the value found by following the variadic names (char *), if the value is not nil...
LUA_API int lua_isstring(lua_State *L, int idx)
bool luaW_toboolean(lua_State *L, int n)
virtual const terrain_filter & get_avoid() const =0
static int cfun_ai_get_leader_ignores_keep(lua_State *L)
const std::vector< std::string > get_advancements(const unit_map::const_iterator &unit) const
double alternative_terrain_quality
The weighted average of the % defense of the best possible terrain that the attacking units could rea...
Proxy table for the AI context.
static int ai_move(lua_State *L, bool exec, bool remove_movement)
static lua_ai_action_handler * create(lua_State *L, char const *code, lua_ai_context &context)
static int ai_recruit(lua_State *L, bool exec)
static int cfun_ai_get_aggression(lua_State *L)
static void push_attack_analysis(lua_State *L, const attack_analysis &)
lua_ai_load(lua_ai_context &ctx, bool read_only)
static int impl_ai_aspect_get(lua_State *L)
static int cfun_ai_get_enemy_dstsrc(lua_State *L)
static int cfun_ai_get_attacks(lua_State *L)
static int cfun_ai_is_src_dst_enemy_valid(lua_State *L)
LUA_API int lua_setmetatable(lua_State *L, int objindex)
virtual int get_villages_per_scout() const =0
static int cfun_ai_get_simple_targeting(lua_State *L)
LUA_API void lua_remove(lua_State *L, int idx)
static const map_location & null_location()
static int cfun_ai_execute_stopunit_all(lua_State *L)
static void push_movements(lua_State *L, const std::vector< std::pair< map_location, map_location > > &moves)
static int cfun_ai_get_grouping(lua_State *L)
static int cfun_ai_check_stopunit(lua_State *L)
LUA_API void lua_pushnil(lua_State *L)
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Encapsulates the map of the game.
bool uses_leader
Is true if this attack sequence makes use of the leader.
#define lua_isboolean(L, n)
static int cfun_ai_execute_move_full(lua_State *L)
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
static int cfun_ai_get_targets(lua_State *L)
static int cfun_ai_check_recruit(lua_State *L)
LUA_API void * lua_touserdata(lua_State *L, int idx)
static int cfun_ai_is_dst_src_valid(lua_State *L)
static void init(lua_State *L)
void get_locations(std::set< map_location > &locs, bool with_border=false) const
LUA_API void lua_rawset(lua_State *L, int idx)
lua_ai_context(lua_State *l, int num, int side)
virtual const team & current_team() const =0
void get_arguments(config &) const
virtual void set_dst_src_enemy_valid_lua()=0
virtual bool is_src_dst_enemy_valid_lua() const =0
std::map< std::string, aspect_ptr > aspect_map
std::vector< std::pair< map_location, map_location > > movements
static int cfun_ai_recalculate_move_maps_enemy(lua_State *L)
double avg_losses
The value on average, of units lost in the combat.
static recruit_result_ptr execute_recruit_action(side_number side, bool execute, const std::string &unit_name, const map_location &where, const map_location &from)
Ask the game to recruit a unit for us on specified location.
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
static synced_command_result_ptr execute_synced_command_action(side_number side, bool execute, const std::string &lua_code, const map_location &location)
Ask the game to run Lua code.
#define lua_tostring(L, i)
virtual int get_attack_depth() const =0
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
virtual const std::vector< std::string > get_recruitment_pattern() const =0
virtual bool get_passive_leader_shares_keep() const =0
static int impl_ai_aspect_set(lua_State *L)
static int cfun_ai_get_attack_depth(lua_State *L)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API int lua_isnumber(lua_State *L, int idx)
static int ai_attack(lua_State *L, bool exec)
virtual double get_leader_value() const =0
#define lua_call(L, n, r)
Proxy class for calling AI action handlers defined in Lua.
static stopunit_result_ptr execute_stopunit_action(side_number side, bool execute, const map_location &unit_location, bool remove_movement, bool remove_attacks)
Ask the game to remove unit movements and/or attack.
static int cfun_ai_execute_move_partial(lua_State *L)
static int cfun_ai_check_move(lua_State *L)
LUA_API size_t lua_rawlen(lua_State *L, int idx)
lua_ai_action_handler(lua_State *l, lua_ai_context &context, int num)
#define LUA_REGISTRYINDEX
virtual ai_context_ptr get_ai_context()
static int impl_ai_get(lua_State *L)
LUA_API int lua_error(lua_State *L)
static lg::log_domain log_ai_engine_lua("ai/engine/lua")
virtual const move_map & get_dstsrc() const =0
void luaW_pushconfig(lua_State *L, config const &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
virtual double get_aggression() const =0
void luaW_pushlocation(lua_State *L, const map_location &ml)
Converts a map location object to a Lua table pushed at the top of the stack.
readonly_context & get_readonly_context()
Standard logging facilities (interface).
virtual side_number get_side() const =0
Get the side number.
Object which contains all the possible locations a unit can move to, with associated best routes to t...
virtual void recalculate_move_maps() const =0
virtual double get_leader_aggression() const =0
static size_t generate_and_push_ai_state(lua_State *L, ai::engine_lua *engine)
static int cfun_ai_get_caution(lua_State *L)
bool is_surrounded
Is true if the units involved in this attack sequence are surrounded.
static int cfun_ai_get_passive_leader(lua_State *L)
virtual double get_scout_village_targeting() const =0
A config object defines a single node in a WML file, with access to child nodes.
static void generate_and_push_ai_table(lua_State *L, ai::engine_lua *engine)
LUA AI Support engine - creating specific ai components from config.
virtual void recalculate_move_maps_enemy() const =0
virtual bool is_dst_src_valid_lua() const =0
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
This module contains various pathfinding functions and utilities.
LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *tname)
static int cfun_ai_get_leader_aggression(lua_State *L)
LUA_API void lua_rawget(lua_State *L, int idx)
GLsizei const GLcharARB ** string
virtual std::vector< target > find_targets(const move_map &enemy_dstsrc)=0
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
typesafe_aspect< T > * try_aspect_as(aspect_ptr p)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
static int cfun_ai_get_enemy_srcdst(lua_State *L)
static int cfun_ai_get_suitable_keep(lua_State *L)
static move_result_ptr execute_move_action(side_number side, bool execute, const map_location &from, const map_location &to, bool remove_movement, bool unreach_is_ok=false)
Ask the game to move our unit from location 'from' to location 'to', optionally - doing a partial mov...
virtual double get_caution() const =0
static int cfun_ai_is_dst_src_enemy_valid(lua_State *L)
static int cfun_ai_get_recruitment_pattern(lua_State *L)
double rating(double aggression, const readonly_context &ai_obj) const
static ai::engine_lua & get_engine(lua_State *L)
static const std::string & get_error_name(int error_code)
get human-readable name of the error by code.
static int cfun_ai_check_recall(lua_State *L)
const std::string valid
Little parts of regex templates used to parse Wml annoations.
virtual bool get_leader_ignores_keep() const =0
#define luaL_checkstring(L, n)