1 /*
2  * Copyright (C) 2004-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #ifndef WL_AI_AI_HINTS_H
21 #define WL_AI_AI_HINTS_H
22 
23 #include <memory>
24 #include <unordered_map>
25 
26 #include "base/log.h"
27 #include "base/macros.h"
28 #include "logic/widelands.h"
29 #include "scripting/lua_table.h"
30 
31 namespace Widelands {
32 enum class AiType : uint8_t { kVeryWeak, kWeak, kNormal };
33 }
34 
35 /// This struct is used to read out the data given in [aihints] section of a
36 /// buildings conf file. It is used to tell the computer player about the
37 /// special properties of a building.
38 struct BuildingHints {
39 	explicit BuildingHints(std::unique_ptr<LuaTable>);
~BuildingHintsBuildingHints40 	~BuildingHints() {
41 	}
42 
supported_productionBuildingHints43 	std::set<std::string> supported_production() const {
44 		return supported_production_;
45 	}
46 
has_minesBuildingHints47 	bool has_mines() const {
48 		return !mines_.empty();
49 	}
50 
get_minesBuildingHints51 	char const* get_mines() const {
52 		return mines_.c_str();
53 	}
54 
get_needs_waterBuildingHints55 	bool get_needs_water() const {
56 		return needs_water_;
57 	}
58 
is_space_consumerBuildingHints59 	bool is_space_consumer() const {
60 		return space_consumer_;
61 	}
is_expansion_typeBuildingHints62 	bool is_expansion_type() const {
63 		return expansion_;
64 	}
is_fighting_typeBuildingHints65 	bool is_fighting_type() const {
66 		return fighting_;
67 	}
is_mountain_conquerorBuildingHints68 	bool is_mountain_conqueror() const {
69 		return mountain_conqueror_;
70 	}
71 
requires_supportersBuildingHints72 	bool requires_supporters() const {
73 		return requires_supporters_;
74 	}
75 
is_shipyardBuildingHints76 	bool is_shipyard() const {
77 		return shipyard_;
78 	}
79 
supports_seafaringBuildingHints80 	bool supports_seafaring() const {
81 		return supports_seafaring_;
82 	}
83 
collects_ware_from_mapBuildingHints84 	const std::string& collects_ware_from_map() const {
85 		return collects_ware_from_map_;
86 	}
87 
get_prohibited_tillBuildingHints88 	uint32_t get_prohibited_till() const {
89 		return prohibited_till_;
90 	}
91 
basic_amountBuildingHints92 	uint32_t basic_amount() const {
93 		return basic_amount_;
94 	}
95 
get_forced_afterBuildingHints96 	uint32_t get_forced_after() const {
97 		return forced_after_;
98 	}
99 
get_mines_percentBuildingHints100 	uint8_t get_mines_percent() const {
101 		return mines_percent_;
102 	}
103 
104 	int16_t get_ai_limit(Widelands::AiType) const;
105 
106 	void set_trainingsites_max_percent(int percent);
107 
108 	uint8_t trainingsites_max_percent() const;
109 
110 private:
111 	const std::string mines_;
112 	const bool needs_water_;
113 	const bool space_consumer_;
114 	const bool expansion_;
115 	const bool fighting_;
116 	const bool mountain_conqueror_;
117 	const bool shipyard_;
118 	const bool supports_seafaring_;
119 	const std::string collects_ware_from_map_;
120 	const int32_t prohibited_till_;
121 	const uint32_t basic_amount_;
122 	const int32_t forced_after_;
123 	const int8_t mines_percent_;
124 	const int16_t very_weak_ai_limit_;
125 	const int16_t weak_ai_limit_;
126 	const int16_t normal_ai_limit_;
127 	const bool requires_supporters_;
128 	int trainingsites_max_percent_;
129 	std::set<std::string> supported_production_;
130 
131 	DISALLOW_COPY_AND_ASSIGN(BuildingHints);
132 };
133 
134 /// Hints common to wares and workers
135 struct WareWorkerHints {
136 	WareWorkerHints() = default;
137 
138 	/// Returns the preciousness of the ware/worker, or kInvalidWare if the tribe doesn't use the
139 	/// ware/worker or the worker has no preciousness defined for the tribe.
140 	int preciousness(const std::string& tribename) const;
141 
142 protected:
143 	void read_preciousness(const std::string& name, const LuaTable& table);
144 
145 private:
146 	// tribename, preciousness. No default.
147 	std::unordered_map<std::string, int> preciousnesses_;
148 };
149 
150 /// Hints for wares
151 struct WareHints : WareWorkerHints {
152 	explicit WareHints(const std::string& ware_name, const LuaTable& table);
153 };
154 
155 /// Hints for workers
156 struct WorkerHints : WareWorkerHints {
157 	explicit WorkerHints(const std::string& worker_name, const LuaTable& table);
158 };
159 
160 #endif  // end of include guard: WL_AI_AI_HINTS_H
161