The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
aspect_advancements.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2016 by Felix Bauer <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 #include "global.hpp"
17 
18 #include "log.hpp" // for LOG_STREAM, logger, etc
19 #include "lua/lauxlib.h" // for luaL_ref, LUA_REFNIL
20 #include "lua/lua.h" // for lua_isstring, etc
21 #include "map/location.hpp" // for map_location
22 #include "serialization/string_utils.hpp" // for split
23 #include "units/unit.hpp"
24 #include "units/map.hpp" // for unit_map::const_iterator, etc
25 
26 #include <ostream> // for operator<<, basic_ostream, etc
27 #include <string> // for string, char_traits, etc
28 #include <vector> // for vector
29 
30 struct lua_State;
31 
32 
33 
34 static lg::log_domain log_ai_engine_lua("ai/engine/lua");
35 #define LOG_LUA LOG_STREAM(info, log_ai_engine_lua)
36 #define ERR_LUA LOG_STREAM(err, log_ai_engine_lua)
37 
38 namespace ai{
39 
41  val_(), L_(),ref_()
42 {
43 }
44 
46  : val_("Lua Function")
47  , L_(L)
48  , ref_()
49 {
50  lua_settop(L, n);
51 
52  //on the top of the Lua-Stack is now the pointer to the function. Save it:
54 
55 }
56 
58 {
59 }
60 
62 {
63  if(L_) {
64  // Remove the function from the registry
66  }
67 }
68 
69 const std::vector<std::string> unit_advancements_aspect::get_advancements(const unit_map::const_iterator& unit) const
70 {
71 
72 
73  if(!unit.valid())
74  {
75  return std::vector<std::string>();
76  }
77 
78  const std::string& unit_id = (*unit).id();
79  const int unit_x = (*unit).get_location().x + 1;
80  const int unit_y = (*unit).get_location().y + 1;
81 
82  LOG_LUA << "Entering unit_advancements_aspect::get_advancements() in instance " << this << " with unit " << unit_id << " on (x,y) = (" << unit_x << ", " << unit_y << ")\n";
83 
84  if(L_ == nullptr || ref_ == LUA_REFNIL)
85  {
86  //If we end up here, most likely the aspect don't use the lua-engine.
87  //Just to make sure:
88  if (val_ == "Lua Function")
89  {
90  return std::vector<std::string>();
91  }
92  return utils::split(val_);
93  }
94 
95  //put the Pointer back on the Stack
97 
98  if(lua_isstring(L_, -1))
99  {
100  return utils::split(lua_tostring(L_, -1));
101  }
102 
103  if(!lua_isfunction(L_, -1))
104  {
105  ERR_LUA << "Can't evaluate advancement aspect: Value is neither a string nor a function." << std::endl;
106  return std::vector<std::string>();
107  }
108 
109  //push parameter to the stack
110  lua_pushinteger(L_, unit_x);
111  lua_pushinteger(L_, unit_y);
112 
113  //To make unit_id a Parameter of the Lua function:
114  //lua_pushfstring(L_, unit_id.c_str());
115 
116  //call function
117  if(lua_pcall(L_, 2, 1, 0) != 0)
118  {
119  ERR_LUA << "LUA Error while evaluating advancements_aspect: " << lua_tostring(L_, -1) << std::endl;
120  return std::vector<std::string>();
121  }
122  if (!lua_isstring(L_, -1))
123  {
124  ERR_LUA << "LUA Error while evaluating advancements_aspect: Function must return String " << std::endl;
125  return std::vector<std::string>();
126  }
127 
128  //get result from Lua-Stack
129  const std::string retval = std::string(lua_tostring(L_, -1));
130  lua_pop(L_, 1);
131 
132  LOG_LUA << "Called Lua advancement function. Result was: \"" << retval << "\".\n";
133 
134  return utils::split(retval);
135 }
136 
137 
139 {
140  return val_;
141 }
142 }
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
Definition: lapi.cpp:643
static lg::log_domain log_ai_engine_lua("ai/engine/lua")
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.cpp:159
Definition: unit.hpp:95
LUALIB_API int luaL_ref(lua_State *L, int t)
Definition: lauxlib.cpp:524
#define val_(o)
Definition: lobject.h:112
GLuint const GLfloat * val
Definition: glew.h:2614
#define LOG_LUA
#define lua_pop(L, n)
Definition: lua.h:322
#define lua_pcall(L, n, r, f)
Definition: lua.h:258
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:268
const std::vector< std::string > get_advancements(const unit_map::const_iterator &unit) const
#define ERR_LUA
#define lua_isfunction(L, n)
Definition: lua.h:330
#define lua_tostring(L, i)
Definition: lua.h:345
#define LUA_REGISTRYINDEX
Definition: lua.h:39
GLclampd n
Definition: glew.h:5903
Standard logging facilities (interface).
const std::string get_value() const
LUALIB_API void luaL_unref(lua_State *L, int t, int ref)
Definition: lauxlib.cpp:545
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
bool valid() const
Definition: map.hpp:229
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
GLsizei const GLcharARB ** string
Definition: glew.h:4503
#define LUA_REFNIL
Definition: lauxlib.h:70