The Battle for Wesnoth  1.17.17+dev
stage.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2023
3  by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * Stage of a composite AI
18  * @file
19  */
20 
21 #include "ai/composite/ai.hpp"
22 #include "ai/composite/engine.hpp"
23 #include "ai/composite/stage.hpp"
24 #include "ai/contexts.hpp"
25 #include "log.hpp"
26 #include "resources.hpp"
27 #include "tod_manager.hpp"
28 #include <map>
29 #include <string>
30 
31 namespace ai {
32 
33 static lg::log_domain log_ai_stage("ai/stage");
34 #define DBG_AI_STAGE LOG_STREAM(debug, log_ai_stage)
35 #define LOG_AI_STAGE LOG_STREAM(info, log_ai_stage)
36 #define ERR_AI_STAGE LOG_STREAM(err, log_ai_stage)
37 
38 // =======================================================================
39 // COMPOSITE AI STAGE
40 // =======================================================================
41 
42 stage::stage( ai_context &context, const config &cfg )
43  : recursion_counter_(context.get_recursion_count()), cfg_(cfg)
44 {
45  init_ai_context_proxy(context);
46 }
47 
49 {
50  LOG_AI_STAGE << "side "<< get_side() << " : "<<" created stage with name=["<<cfg_["name"]<<"]";
51 }
52 
54 {
55 }
56 
58 {
59  return do_play_stage();
60 }
61 
63 {
65 }
66 
68 {
69  config cfg;
70  cfg["engine"] = cfg_["engine"];
71  cfg["name"] = cfg_["name"];
72  cfg["id"] = cfg_["id"];
73  return cfg;
74 }
75 
76 std::string stage::get_id() const
77 {
78  return cfg_["id"];
79 }
80 
81 std::string stage::get_engine() const
82 {
83  return cfg_["engine"];
84 }
85 
86 std::string stage::get_name() const
87 {
88  return cfg_["name"];
89 }
90 
91 // =======================================================================
92 // COMPOSITE AI IDLE STAGE
93 // =======================================================================
94 
95 idle_stage::idle_stage( ai_context &context, const config &cfg )
96  : stage(context,cfg)
97 {
98 }
99 
101 {
102 }
103 
105  LOG_AI_STAGE << "Turn " << resources::tod_manager->turn() << ": playing idle stage for side: "<< get_side();
106  return false;
107 }
108 
109 // This is defined in the source file so that it can easily access the logger
110 bool stage_factory::is_duplicate(const std::string& name)
111 {
112  if (get_list().find(name) != get_list().end()) {
113  ERR_AI_STAGE << "Error: Attempt to double-register stage " << name;
114  return true;
115  }
116  return false;
117 }
118 
119 } //end of namespace ai
void init_ai_context_proxy(ai_context &target)
Definition: contexts.hpp:94
virtual bool do_play_stage()
Play the turn - implementation.
Definition: stage.cpp:104
idle_stage(ai_context &context, const config &cfg)
Definition: stage.cpp:95
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:70
virtual side_number get_side() const override
Get the side number.
Definition: contexts.hpp:403
bool is_duplicate(const std::string &name)
Definition: stage.cpp:110
static factory_map & get_list()
Definition: stage.hpp:97
int get_recursion_count() const
get the value of the recursion counter
Definition: stage.cpp:62
recursion_counter recursion_counter_
Definition: stage.hpp:75
virtual std::string get_engine() const
Definition: stage.cpp:81
virtual std::string get_id() const
Definition: stage.cpp:76
virtual ~stage()
Destructor.
Definition: stage.cpp:53
config cfg_
Definition: stage.hpp:77
bool play_stage()
Play the turn - strategy.
Definition: stage.cpp:57
stage(ai_context &context, const config &cfg)
Constructor.
Definition: stage.cpp:42
virtual bool do_play_stage()=0
Play the turn - implementation.
virtual config to_config() const
serialize
Definition: stage.cpp:67
virtual void on_create()
Initialization.
Definition: stage.cpp:48
virtual std::string get_name() const
Definition: stage.cpp:86
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
int turn() const
Composite AI with turn sequence which is a vector of stages.
Helper functions for the object which operates in the context of AI for specific side this is part of...
AI Support engine - creating specific ai components from config.
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:61
static lg::log_domain log_ai_stage("ai/stage")
::tod_manager * tod_manager
Definition: resources.cpp:30
#define LOG_AI_STAGE
Definition: stage.cpp:35
#define ERR_AI_STAGE
Definition: stage.cpp:36
Composite AI stages.