1 /*
2  * Copyright (C) 2002-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_LOGIC_MAP_OBJECTS_TRIBES_WORKER_DESCR_H
21 #define WL_LOGIC_MAP_OBJECTS_TRIBES_WORKER_DESCR_H
22 
23 #include <memory>
24 
25 #include "ai/ai_hints.h"
26 #include "base/macros.h"
27 #include "graphic/animation/diranimations.h"
28 #include "logic/map_objects/bob.h"
29 #include "logic/map_objects/immovable.h"
30 #include "scripting/lua_table.h"
31 
32 namespace Widelands {
33 
34 // TODO(Antonio Trueba#1#): Get rid of forward class declaration
35 // (chicked-and-egg problem)
36 struct WorkerProgram;
37 
38 class WorkerDescr : public BobDescr {
39 	friend class TribeDescr;
40 	friend class Warehouse;
41 	friend struct WorkerProgram;
42 
43 public:
44 	using Buildcost = std::map<std::string, Quantity>;
45 
46 	WorkerDescr(const std::string& init_descname,
47 	            MapObjectType type,
48 	            const LuaTable& table,
49 	            const Tribes& tribes);
50 	WorkerDescr(const std::string& init_descname, const LuaTable& t, const Tribes& tribes);
51 	~WorkerDescr() override;
52 
53 	Bob& create_object() const override;
54 
is_buildable()55 	bool is_buildable() const {
56 		return buildable_;
57 	}
buildcost()58 	const Buildcost& buildcost() const {
59 		assert(is_buildable());
60 		return buildcost_;
61 	}
62 
ware_hotspot()63 	const Vector2i& ware_hotspot() const {
64 		return ware_hotspot_;
65 	}
66 
67 	/// How much of the worker type that an economy should store in warehouses.
68 	/// The special value std::numeric_limits<uint32_t>::max() means that the
69 	/// the target quantity of this ware type will never be checked and should
70 	/// not be configurable.
default_target_quantity()71 	Quantity default_target_quantity() const {
72 		return default_target_quantity_;
73 	}
74 
has_demand_check()75 	bool has_demand_check() const {
76 		return default_target_quantity() != std::numeric_limits<uint32_t>::max();
77 	}
78 
79 	/// Called when a demand check for this ware type is encountered during
80 	/// parsing. If there was no default target quantity set in the ware type's
81 	/// configuration, set the default value 1.
set_has_demand_check()82 	void set_has_demand_check() {
83 		if (default_target_quantity_ == std::numeric_limits<uint32_t>::max())
84 			default_target_quantity_ = 1;
85 	}
86 
get_right_walk_anims(bool const carries_ware,Worker *)87 	virtual const DirAnimations& get_right_walk_anims(bool const carries_ware, Worker*) const {
88 		return carries_ware ? walkload_anims_ : walk_anims_;
89 	}
90 	WorkerProgram const* get_program(const std::string&) const;
91 
92 	// For leveling
get_needed_experience()93 	int32_t get_needed_experience() const {
94 		return needed_experience_;
95 	}
becomes()96 	DescriptionIndex becomes() const {
97 		return becomes_;
98 	}
99 	DescriptionIndex worker_index() const;
100 	bool can_act_as(DescriptionIndex) const;
101 
102 	// Add a building to the list of employers
103 	void add_employer(const DescriptionIndex& building_index);
104 
105 	// The buildings where this worker can work
106 	const std::set<DescriptionIndex>& employers() const;
107 
108 	Worker& create(EditorGameBase&, Player*, PlayerImmovable*, Coords) const;
109 
110 	uint32_t movecaps() const override;
111 
112 	using Programs = std::map<std::string, std::unique_ptr<WorkerProgram>>;
programs()113 	const Programs& programs() const {
114 		return programs_;
115 	}
116 
117 	/// AI hints for this worker type. Can be nullptr.
ai_hints()118 	const WorkerHints* ai_hints() const {
119 		return ai_hints_.get();
120 	}
121 
122 protected:
123 	Programs programs_;
124 
125 private:
126 	const Vector2i ware_hotspot_;
127 
128 	DirAnimations walk_anims_;
129 	DirAnimations walkload_anims_;
130 
131 	Quantity default_target_quantity_;
132 	const bool buildable_;
133 	Buildcost buildcost_;
134 
135 	/**
136 	 * Type that this worker can become, i.e. level up to, or INVALID_INDEX if the worker cannot
137 	 * level up.
138 	 */
139 	const DescriptionIndex becomes_;
140 
141 	/**
142 	 * Number of experience points required for leveling up,
143 	 * or INVALID_INDEX if the worker cannot level up.
144 	 */
145 	const int32_t needed_experience_;
146 
147 	/// Buildings where this worker can work
148 	std::set<DescriptionIndex> employers_;
149 
150 private:
151 	// Hints for the AI
152 	std::unique_ptr<WorkerHints> ai_hints_;
153 
154 	const Tribes& tribes_;
155 	DISALLOW_COPY_AND_ASSIGN(WorkerDescr);
156 };
157 }  // namespace Widelands
158 
159 #endif  // end of include guard: WL_LOGIC_MAP_OBJECTS_TRIBES_WORKER_DESCR_H
160