1 /*
2  * Copyright (C) 2006-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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */
19 
20 #ifndef WL_LOGIC_MAP_OBJECTS_WORLD_TERRAIN_DESCRIPTION_H
21 #define WL_LOGIC_MAP_OBJECTS_WORLD_TERRAIN_DESCRIPTION_H
22 
23 #include "base/macros.h"
24 #include "graphic/color.h"
25 #include "graphic/image.h"
26 #include "logic/widelands.h"
27 
28 class LuaTable;
29 class Texture;
30 
31 namespace Widelands {
32 
33 class EditorCategory;
34 class World;
35 
36 /// TerrainTextures have a fixed size and are squares.
37 constexpr int kTextureSideLength = 64;
38 
39 class TerrainDescription {
40 public:
41 	enum class Is {
42 		kArable = 0,
43 		kWalkable = 1,
44 		kWater = 2,
45 		kUnreachable = 4,
46 		kMineable = 8,
47 		kUnwalkable = 16,
48 	};
49 
50 	struct Type {
51 		explicit Type(TerrainDescription::Is init_is);
52 
53 		TerrainDescription::Is is;
54 		const char* descname;
55 		const Image* icon;
56 	};
57 
58 	TerrainDescription(const LuaTable& table, const World&);
59 	~TerrainDescription();
60 
61 	/// The name used internally for this terrain.
62 	const std::string& name() const;
63 
64 	/// The name showed to users of Widelands. Usually translated.
65 	const std::string& descname() const;
66 
67 	const std::vector<std::string>& texture_paths() const;
68 
69 	/// Returns the texture for the given gametime.
70 	const Image& get_texture(uint32_t gametime) const;
71 	void add_texture(const Image* texture);
72 
73 	// Sets the base minimap color.
74 	void set_minimap_color(const RGBColor& color);
75 
76 	// Return the basic terrain colour to be used in the minimap.
77 	// 'shade' must be a brightness value, i.e. in [-128, 127].
78 	const RGBColor& get_minimap_color(int shade);
79 
80 	/// Returns the type of terrain this is (water, walkable, and so on).
81 	Is get_is() const;
82 	/// Returns a list of the types that match get_is()
83 	const std::vector<TerrainDescription::Type> get_types() const;
84 
85 	/// Returns the valid resource with the given index.
86 	DescriptionIndex get_valid_resource(DescriptionIndex index) const;
87 
88 	/// Returns the number of valid resources.
89 	size_t get_num_valid_resources() const;
90 
91 	/// Returns the the valid resources.
92 	std::vector<DescriptionIndex> valid_resources() const;
93 
94 	/// Returns true if this resource can be found in this terrain type.
95 	bool is_resource_valid(DescriptionIndex res) const;
96 
97 	/// Returns the resource index that can by default always be found in this
98 	/// terrain.
99 	DescriptionIndex get_default_resource() const;
100 
101 	/// Returns the default amount of resources you can find in this terrain.
102 	ResourceAmount get_default_resource_amount() const;
103 
104 	/// Returns the dither layer, i.e. the information in which zlayer this
105 	/// texture should be drawn.
106 	int32_t dither_layer() const;
107 
108 	/// Returns the editor category.
109 	const EditorCategory* editor_category() const;
110 
111 	/// Parameters for terrain affinity of immovables.
112 	/// Temperature is in arbitrary units.
113 	int temperature() const;
114 
115 	/// Humidity, ranging from 0 to 1000.
116 	int humidity() const;
117 
118 	/// Fertility, ranging from 0 to 1000.
119 	int fertility() const;
120 
121 	// The terrain which certain workers can transform this terrain into.
122 	const std::string& enhancement() const;
123 
124 	/// Additional tooptip entries for the editor
custom_tooltips()125 	const std::vector<std::string>& custom_tooltips() const {
126 		return custom_tooltips_;
127 	}
128 
129 private:
130 	const std::string name_;
131 	const std::string descname_;
132 	const EditorCategory* editor_category_;  ///< not owned.
133 	Is is_;
134 	std::vector<std::string> custom_tooltips_;
135 	std::vector<DescriptionIndex> valid_resources_;
136 	DescriptionIndex default_resource_index_;
137 	int default_resource_amount_;
138 	int dither_layer_;
139 	int frame_length_;
140 	int temperature_;
141 	int fertility_;
142 	int humidity_;
143 	std::string enhancement_;
144 	std::vector<std::string> texture_paths_;
145 	std::vector<const Image*> textures_;
146 	RGBColor minimap_colors_[256];
147 
148 	DISALLOW_COPY_AND_ASSIGN(TerrainDescription);
149 };
150 
151 inline TerrainDescription::Is operator|(TerrainDescription::Is left, TerrainDescription::Is right) {
152 	return TerrainDescription::Is(static_cast<int>(left) | static_cast<int>(right));
153 }
154 inline int operator&(TerrainDescription::Is left, TerrainDescription::Is right) {
155 	return static_cast<int>(left) & static_cast<int>(right);
156 }
157 
158 }  // namespace Widelands
159 
160 #endif  // end of include guard: WL_LOGIC_MAP_OBJECTS_WORLD_TERRAIN_DESCRIPTION_H
161