-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
engine_fai.cpp
144 lines (115 loc) · 3.93 KB
/
engine_fai.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
/*
Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project https://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.
*/
/**
* FAI AI Support engine - creating specific ai components from config
* @file
*/
#include "ai/formula/ai.hpp"
#include "ai/formula/engine_fai.hpp"
#include "ai/composite/rca.hpp"
#include "ai/formula/ai.hpp"
#include "ai/formula/candidates.hpp"
#include "ai/formula/stage_side_formulas.hpp"
#include "ai/formula/stage_unit_formulas.hpp"
#include "log.hpp"
#include "units/unit.hpp"
namespace ai {
static lg::log_domain log_ai_engine_fai("ai/engine/fai");
#define DBG_AI_ENGINE_FAI LOG_STREAM(debug, log_ai_engine_fai)
#define LOG_AI_ENGINE_FAI LOG_STREAM(info, log_ai_engine_fai)
#define ERR_AI_ENGINE_FAI LOG_STREAM(err, log_ai_engine_fai)
class fai_candidate_action_wrapper : public candidate_action {
public:
fai_candidate_action_wrapper( rca_context &context, const config &cfg, wfl::candidate_action_ptr fai_ca, formula_ai &_formula_ai )
: candidate_action(context,cfg),fai_ca_(fai_ca),formula_ai_(_formula_ai),cfg_(cfg)
{
}
virtual ~fai_candidate_action_wrapper() {}
virtual double evaluate()
{
formula_ai_.evaluate_candidate_action(fai_ca_);
return fai_ca_->get_score();
}
virtual void execute()
{
formula_ai_.execute_candidate_action(fai_ca_);
}
virtual config to_config() const
{
return cfg_;
}
private:
wfl::candidate_action_ptr fai_ca_;
formula_ai &formula_ai_;
const config cfg_;
};
engine_fai::engine_fai( readonly_context &context, const config &cfg )
: engine(context,cfg), formula_ai_(new formula_ai(context,cfg.child_or_empty("formula_ai")))
{
name_ = "fai";
formula_ai_->on_create();
}
engine_fai::~engine_fai()
{
}
void engine_fai::do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr >> b ){
wfl::candidate_action_ptr fai_ca = formula_ai_->load_candidate_action_from_config(cfg);
if (!fai_ca) {
ERR_AI_ENGINE_FAI << "side "<<ai_.get_side()<< " : ERROR creating candidate_action["<<cfg["name"]<<"]"<< std::endl;
DBG_AI_ENGINE_FAI << "config snippet contains: " << std::endl << cfg << std::endl;
return;
}
auto ca = std::make_shared<fai_candidate_action_wrapper>(context, cfg, fai_ca, *formula_ai_);
*b = ca;
}
void engine_fai::do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr >> b )
{
if (!cfg) {
return;
}
const std::string &name = cfg["name"];
stage_ptr st_ptr;
//dropped from 1.8, as it's not ready
//if (name=="rca_formulas") {
// st_ptr = stage_ptr(new stage_rca_formulas(context,cfg,formula_ai_));
if (name=="side_formulas") {
st_ptr = std::make_shared<stage_side_formulas>(context, cfg, *formula_ai_);
} else if (name=="unit_formulas") {
st_ptr = std::make_shared<stage_unit_formulas>(context, cfg, *formula_ai_);
} else {
ERR_AI_ENGINE_FAI << "unknown type of formula_ai stage: ["<< name <<"]"<<std::endl;
}
if (st_ptr) {
st_ptr->on_create();
*b = st_ptr;
}
}
std::string engine_fai::evaluate(const std::string &str)
{
return formula_ai_->evaluate(str);
}
void engine_fai::set_ai_context(ai_context *context)
{
if (context!=nullptr) {
DBG_AI_ENGINE_FAI << "fai engine: ai_context is set" << std::endl;
} else {
DBG_AI_ENGINE_FAI << "fai engine: ai_context is cleared" << std::endl;
}
formula_ai_->set_ai_context(context);
}
config engine_fai::to_config() const
{
config cfg = engine::to_config();
cfg.add_child("formula_ai",formula_ai_->to_config());
return cfg;
}
} //end of namespace ai