-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
engine_lua.cpp
390 lines (321 loc) · 11.2 KB
/
engine_lua.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
Copyright (C) 2009 - 2016 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* LUA AI Support engine - creating specific ai components from config
* @file
*/
#include "ai.hpp"
#include "engine_lua.hpp"
#include "goal.hpp"
#include "rca.hpp"
#include "stage.hpp"
#include "aspect.hpp"
#include "ai/gamestate_observer.hpp"
#include "log.hpp"
#include "resources.hpp"
#include "ai/lua/core.hpp"
#include "ai/lua/lua_object.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "util.hpp"
#include "units/unit.hpp"
#include "units/map.hpp"
namespace ai {
static lg::log_domain log_ai_engine_lua("ai/engine/lua");
#define DBG_AI_LUA LOG_STREAM(debug, log_ai_engine_lua)
#define LOG_AI_LUA LOG_STREAM(info, log_ai_engine_lua)
#define WRN_AI_LUA LOG_STREAM(warn, log_ai_engine_lua)
#define ERR_AI_LUA LOG_STREAM(err, log_ai_engine_lua)
#ifdef _MSC_VER
#pragma warning(push)
//silence "inherits via dominance" warnings
#pragma warning(disable:4250)
#endif
typedef boost::shared_ptr< lua_object<int> > lua_int_obj;
class lua_candidate_action_wrapper_base : public candidate_action {
public:
lua_candidate_action_wrapper_base( rca_context &context, const config &cfg)
: candidate_action(context, cfg),evaluation_action_handler_(),execution_action_handler_(),serialized_evaluation_state_()
{
// do nothing
}
virtual ~lua_candidate_action_wrapper_base() {}
virtual double evaluate()
{
serialized_evaluation_state_ = config();
lua_int_obj l_obj = lua_int_obj(new lua_object<int>());
if (evaluation_action_handler_) {
evaluation_action_handler_->handle(serialized_evaluation_state_, true, l_obj);
} else {
return BAD_SCORE;
}
boost::shared_ptr<int> result = l_obj->get();
return result ? *result : 0.0;
}
virtual void execute() {
lua_int_obj l_obj = lua_int_obj(new lua_object<int>());
if (execution_action_handler_) {
execution_action_handler_->handle(serialized_evaluation_state_, false, l_obj);
}
}
virtual config to_config() const {
config cfg = candidate_action::to_config();
cfg.add_child("state",serialized_evaluation_state_);
return cfg;
}
protected:
boost::shared_ptr<lua_ai_action_handler> evaluation_action_handler_;
boost::shared_ptr<lua_ai_action_handler> execution_action_handler_;
config serialized_evaluation_state_;
};
class lua_candidate_action_wrapper : public lua_candidate_action_wrapper_base {
public:
lua_candidate_action_wrapper( rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx)
: lua_candidate_action_wrapper_base(context,cfg),evaluation_(cfg["evaluation"]),execution_(cfg["execution"])
{
evaluation_action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(evaluation_.c_str(),lua_ai_ctx));
execution_action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(execution_.c_str(),lua_ai_ctx));
}
virtual ~lua_candidate_action_wrapper() {}
virtual config to_config() const
{
config cfg = lua_candidate_action_wrapper_base::to_config();
cfg["evaluation"] = evaluation_;
cfg["execution"] = execution_;
return cfg;
}
private:
std::string evaluation_;
std::string execution_;
};
class lua_candidate_action_wrapper_external : public lua_candidate_action_wrapper_base {
public:
lua_candidate_action_wrapper_external(rca_context& context, const config& cfg, lua_ai_context &lua_ai_ctx)
: lua_candidate_action_wrapper_base(context,cfg), location_(cfg["location"]), eval_parms_(cfg["eval_parms"]), exec_parms_(cfg["exec_parms"])
{
std::string eval_code;
std::string exec_code;
generate_code(eval_code, exec_code);
evaluation_action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(eval_code.c_str(),lua_ai_ctx));
execution_action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(exec_code.c_str(),lua_ai_ctx));
}
virtual ~lua_candidate_action_wrapper_external() {}
virtual config to_config() const
{
config cfg = lua_candidate_action_wrapper_base::to_config();
cfg["location"] = location_;
cfg["eval_parms"] = eval_parms_;
cfg["exec_parms"] = exec_parms_;
return cfg;
}
private:
std::string location_;
std::string eval_parms_;
std::string exec_parms_;
void generate_code(std::string& eval, std::string& exec) {
std::string code = "wesnoth.require(\"" + location_ + "\")";
eval = "return " + code + ":evaluation((...):get_ai(), {" + eval_parms_ + "}, (...))";
exec = code + ":execution((...):get_ai(), {" + exec_parms_ + "}, (...))";
}
};
class lua_sticky_candidate_action_wrapper : public lua_candidate_action_wrapper {
public:
lua_sticky_candidate_action_wrapper( rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx)
: lua_candidate_action_wrapper(context, cfg, lua_ai_ctx)
, bound_unit_()
{
map_location loc(cfg["unit_x"] - 1, cfg["unit_y"] - 1); // lua and c++ coords differ by one
bound_unit_ = unit_ptr(new unit(*resources::units->find(loc)));
}
virtual double evaluate()
{
if (resources::units->find(bound_unit_->underlying_id()).valid())
{
return lua_candidate_action_wrapper_base::evaluate();
}
else
{
this->set_to_be_removed();
return 0; // Is 0 what we return when we don't want the action to be executed?
}
}
virtual void execute()
{
lua_candidate_action_wrapper_base::execute();
this->disable(); // we do not want to execute the same sticky CA twice -> will be moved out to Lua later
}
private:
unit_ptr bound_unit_;
};
class lua_stage_wrapper : public stage {
public:
lua_stage_wrapper( ai_context &context, const config &cfg, lua_ai_context &lua_ai_ctx )
: stage(context,cfg),action_handler_(),code_(cfg["code"]),serialized_evaluation_state_(cfg.child_or_empty("state"))
{
action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(code_.c_str(),lua_ai_ctx));
}
virtual ~lua_stage_wrapper()
{
}
virtual bool do_play_stage()
{
gamestate_observer gs_o;
lua_int_obj l_obj = lua_int_obj(new lua_object<int>());
if (action_handler_) {
action_handler_->handle(serialized_evaluation_state_, false, l_obj);
}
return gs_o.is_gamestate_changed();
}
virtual config to_config() const
{
config cfg = stage::to_config();
cfg["code"] = code_;
cfg.add_child("state",serialized_evaluation_state_);
return cfg;
}
private:
boost::shared_ptr<lua_ai_action_handler> action_handler_;
std::string code_;
config serialized_evaluation_state_;
};
/**
* Note that initially we get access only to readonly context (engine is created rather early, when there's no way to move/attack.
* We inject full ai_context later.
*/
engine_lua::engine_lua( readonly_context &context, const config &cfg )
: engine(context,cfg)
, code_(get_engine_code(cfg))
, lua_ai_context_(resources::lua_kernel->create_lua_ai_context(
get_engine_code(cfg).c_str(), this))
{
name_ = "lua";
config data(cfg.child_or_empty("data"));
if (lua_ai_context_) { // The context might be NULL if the config contains errors
lua_ai_context_->set_persistent_data(data);
}
}
std::string engine_lua::get_engine_code(const config &cfg) const
{
if (cfg.has_attribute("code")) {
return cfg["code"].str();
}
// If there is no engine defined we create a dummy engine
std::string code = "local ai = ... return wesnoth.require(\"ai/lua/dummy_engine_lua.lua\").get_ai(ai)";
return code;
}
engine_lua::~engine_lua()
{
}
bool engine_lua::is_ok() const
{
return lua_ai_context_ ? true : false;
}
void engine_lua::push_ai_table()
{
if (game_config::debug)
{
lua_ai_context_->load_and_inject_ai_table(this);
}
}
void engine_lua::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b ){
if (!cfg) {
return;
}
if (!lua_ai_context_) {
return;
}
candidate_action_ptr ca_ptr;
if (!cfg["sticky"].to_bool())
{
if (cfg.has_attribute("location")) {
ca_ptr = candidate_action_ptr(new lua_candidate_action_wrapper_external(context,cfg,*lua_ai_context_));
} else {
ca_ptr = candidate_action_ptr(new lua_candidate_action_wrapper(context,cfg,*lua_ai_context_));
}
}
else
{
ca_ptr = candidate_action_ptr(new lua_sticky_candidate_action_wrapper(context,cfg,*lua_ai_context_));
}
if (ca_ptr) {
*b = ca_ptr;
}
}
void engine_lua::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b )
{
if (!cfg) {
return;
}
if (!lua_ai_context_) {
return;
}
stage_ptr st_ptr = stage_ptr(new lua_stage_wrapper(context,cfg,*lua_ai_context_));
if (st_ptr) {
st_ptr->on_create();
*b = st_ptr;
}
}
void engine_lua::do_parse_aspect_from_config( const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr > > b )
{
const std::string aspect_factory_key = id+"*lua_aspect"; // @note: factory key for a lua_aspect
lua_aspect_factory::factory_map::iterator f = lua_aspect_factory::get_list().find(aspect_factory_key);
if (f == lua_aspect_factory::get_list().end()){
ERR_AI_LUA << "side "<<ai_.get_side()<< " : UNKNOWN aspect["<<aspect_factory_key<<"]" << std::endl;
DBG_AI_LUA << "config snippet contains: " << std::endl << cfg << std::endl;
return;
}
aspect_ptr new_aspect = f->second->get_new_instance(ai_,cfg,id,lua_ai_context_);
if (!new_aspect) {
ERR_AI_LUA << "side "<<ai_.get_side()<< " : UNABLE TO CREATE aspect, key=["<<aspect_factory_key<<"]"<< std::endl;
DBG_AI_LUA << "config snippet contains: " << std::endl << cfg << std::endl;
return;
}
*b = new_aspect;
}
void engine_lua::do_parse_goal_from_config(const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b )
{
goal_factory::factory_map::iterator f = goal_factory::get_list().find(cfg["name"]);
if (f == goal_factory::get_list().end()){
ERR_AI_LUA << "side "<<ai_.get_side()<< " : UNKNOWN goal["<<cfg["name"]<<"]"<< std::endl;
DBG_AI_LUA << "config snippet contains: " << std::endl << cfg << std::endl;
return;
}
goal_ptr new_goal = f->second->get_new_instance(ai_,cfg);
new_goal->on_create(lua_ai_context_);
if (!new_goal || !new_goal->ok()) {
ERR_AI_LUA << "side "<<ai_.get_side()<< " : UNABLE TO CREATE goal["<<cfg["name"]<<"]"<< std::endl;
DBG_AI_LUA << "config snippet contains: " << std::endl << cfg << std::endl;
return;
}
*b = new_goal;
}
std::string engine_lua::evaluate(const std::string &/*str*/)
{
///@todo this is not mandatory, but if we want to allow lua to evaluate
// something 'in context' of this ai, this will be useful
return "";
}
config engine_lua::to_config() const
{
config cfg = engine::to_config();
cfg["id"] = get_id();
cfg["code"] = this->code_;
if (lua_ai_context_) {
config data = config();
lua_ai_context_->get_persistent_data(data);
cfg.add_child("data") = data;
}
return cfg;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
} //end of namespace ai