~widelands-dev/widelands/trunk

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
/*
 * Copyright (C) 2008-2024 by the Widelands Development Team
 *
 * 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; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 *
 */

#ifndef WL_LOGIC_MAP_OBJECTS_TRIBES_REQUIREMENTS_H
#define WL_LOGIC_MAP_OBJECTS_TRIBES_REQUIREMENTS_H

#include <climits>
#include <map>
#include <memory>
#include <vector>

#include "logic/map_objects/tribes/training_attribute.h"

class FileRead;
class FileWrite;

namespace Widelands {

class MapObject;
class EditorGameBase;
class MapObjectLoader;
struct MapObjectSaver;

struct RequirementsStorage;

/**
 * Requirements can be attached to Requests.
 *
 * Requirements are matched to a \ref MapObject 's \ref TrainingAttribute as
 * returned by \ref MapObject::get_training_attribute .
 */
struct Requirements {
private:
	struct BaseCapsule {
		virtual ~BaseCapsule() = default;

		[[nodiscard]] virtual bool check(const MapObject&) const = 0;
		virtual void write(FileWrite&, EditorGameBase&, MapObjectSaver&) const = 0;
		[[nodiscard]] virtual const RequirementsStorage& storage() const = 0;
	};

	template <typename T> struct Capsule : public BaseCapsule {
		Capsule(const T& init_m) : m(init_m) {  // NOLINT allow implicit conversion
		}

		[[nodiscard]] bool check(const MapObject& obj) const override {
			return m.check(obj);
		}

		void write(FileWrite& fw, EditorGameBase& egbase, MapObjectSaver& mos) const override {
			m.write(fw, egbase, mos);
		}

		[[nodiscard]] const RequirementsStorage& storage() const override {
			return T::storage;
		}

		T m;
	};

public:
	Requirements() = default;

	template <typename T>
	Requirements(const T& req)  // NOLINT allow implicit conversion
	   : m(new Capsule<T>(req)) {
	}

	/**
	 * \return \c true if the object satisfies the requirements.
	 */
	[[nodiscard]] bool check(const MapObject&) const;

	// For Save/Load Games
	void read(FileRead&, EditorGameBase&, MapObjectLoader&);
	void write(FileWrite&, EditorGameBase&, MapObjectSaver&) const;

private:
	std::shared_ptr<BaseCapsule> m;
};

/**
 * On-disk IDs for certain requirements.
 *
 * Only add enums at the end, and make their value explicit.
 */
enum {
	requirementIdOr = 1,
	requirementIdAnd = 2,
	requirementIdAttribute = 3,
};

/**
 * Factory-like system for requirement loading from files.
 */
struct RequirementsStorage {
	using Reader = Requirements (*)(FileRead&, EditorGameBase&, MapObjectLoader&);

	RequirementsStorage(uint32_t id, Reader reader);
	[[nodiscard]] uint32_t id() const;

	static Requirements read(FileRead&, EditorGameBase&, MapObjectLoader&);

private:
	using StorageMap = std::map<uint32_t, RequirementsStorage*>;

	uint32_t id_;
	Reader reader_;

	static StorageMap& storageMap();
};

/**
 * Require that at least one of the sub-requirements added with \ref add()
 * is met. Defaults to \c false if no sub-requirement is added.
 */
struct RequireOr {
	void add(const Requirements&);

	[[nodiscard]] bool check(const MapObject&) const;
	void write(FileWrite&, EditorGameBase& egbase, MapObjectSaver&) const;

	static const RequirementsStorage storage;

private:
	std::vector<Requirements> m;
};

/**
 * Require that all sub-requirements added \ref add() are met.
 * Defaults to \c true if no sub-requirement is added.
 */
struct RequireAnd {
	void add(const Requirements&);

	[[nodiscard]] bool check(const MapObject&) const;
	void write(FileWrite&, EditorGameBase& egbase, MapObjectSaver&) const;

	static const RequirementsStorage storage;

private:
	std::vector<Requirements> m;
};

/**
 * Require that a \ref TrainingAttribute lies in the given, inclusive, range.
 */
struct RequireAttribute {
	RequireAttribute(TrainingAttribute const init_at, int32_t const init_min, int32_t const init_max)
	   : at(init_at), min(init_min), max(init_max) {
	}

	RequireAttribute() = default;
	[[nodiscard]] bool check(const MapObject&) const;
	void write(FileWrite&, EditorGameBase& egbase, MapObjectSaver&) const;

	static const RequirementsStorage storage;

	[[nodiscard]] int32_t get_min() const {
		return min;
	}
	[[nodiscard]] int32_t get_max() const {
		return max;
	}

private:
	TrainingAttribute at = TrainingAttribute::kTotal;
	int32_t min = SHRT_MIN;
	int32_t max = SHRT_MAX;
};
}  // namespace Widelands

#endif  // end of include guard: WL_LOGIC_MAP_OBJECTS_TRIBES_REQUIREMENTS_H