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_IMMOVABLE_PROGRAM_H
21 #define WL_LOGIC_MAP_OBJECTS_IMMOVABLE_PROGRAM_H
22 
23 #include <memory>
24 
25 #include "logic/map_objects/immovable.h"
26 #include "logic/map_objects/map_object_program.h"
27 
28 namespace Widelands {
29 
30 /// Ordered sequence of actions (at least 1). Has a name.
31 struct ImmovableProgram : public MapObjectProgram {
32 
33 	/// Can be executed on an Immovable.
34 	struct Action {
35 		Action() = default;
36 		virtual ~Action();
37 		virtual void execute(Game&, Immovable&) const = 0;
38 
39 	private:
40 		DISALLOW_COPY_AND_ASSIGN(Action);
41 	};
42 
43 	/// Runs an animation.
44 	///
45 	/// Parameter syntax:
46 	///    parameters ::= animation duration
47 	/// Parameter semantics:
48 	///    animation:
49 	///       The name of an animation (defined in the immovable type).
50 	///    duration:
51 	///       A natural integer.
52 	///
53 	/// Starts the specified animation for the immovable. Blocks the execution
54 	/// of the program for the specified duration. (The duration does not have
55 	/// to equal the length of the animation. It will loop around. The animation
56 	/// will not be stopped by this command. It will run until another animation
57 	/// is started.)
58 	struct ActAnimate : public Action {
59 		ActAnimate(const std::vector<std::string>& arguments, const ImmovableDescr&);
60 		void execute(Game&, Immovable&) const override;
animationImmovableProgram::ActAnimate61 		uint32_t animation() const {
62 			return parameters.animation;
63 		}
64 
65 	private:
66 		AnimationParameters parameters;
67 	};
68 
69 	/// Transforms the immovable into another immovable or into a bob
70 	///
71 	/// Parameter syntax
72 	///    parameters ::= {probability} {bob|immovable} world|tribe:name
73 	/// Parameter semantics:
74 	///    probability: (defaults to 0 -- i.e. always)
75 	///       The probability (out of 255) for replacing the immovable with
76 	///       a new one; if the probability is 0 (i.e. the default), then the
77 	///       transformation always happens
78 	///    bob|immovable: (defaults to immovable)
79 	///       whether we'll be replaced by a bob or by an immovable
80 	///    world|tribe:
81 	///       whether the other object is taken from the world or from
82 	///       the owner's tribe
83 	///    name:
84 	///       name of the replacement object
85 	struct ActTransform : public Action {
86 		ActTransform(std::vector<std::string>& arguments, const ImmovableDescr&);
87 		void execute(Game&, Immovable&) const override;
88 
89 	private:
90 		std::string type_name;
91 		bool bob;
92 		uint8_t probability;
93 	};
94 
95 	/// Like ActTransform but the probability is determined by the suitability.
96 	struct ActGrow : public Action {
97 		ActGrow(std::vector<std::string>& arguments, const ImmovableDescr&);
98 		void execute(Game&, Immovable&) const override;
99 
100 	private:
101 		std::string type_name;
102 	};
103 
104 	struct ActRemove : public Action {
105 		ActRemove(std::vector<std::string>& arguments);
106 		void execute(Game&, Immovable&) const override;
107 
108 	private:
109 		uint8_t probability;
110 	};
111 
112 	struct ActSeed : public Action {
113 		ActSeed(std::vector<std::string>& arguments, const ImmovableDescr&);
114 		void execute(Game&, Immovable&) const override;
115 
116 	private:
117 		std::string type_name;
118 		uint8_t probability;
119 	};
120 
121 	/// Plays a sound effect.
122 	///
123 	/// Parameter syntax:
124 	///    parameters ::= directory sound [priority]
125 	/// Parameter semantics:
126 	///    path:
127 	///       The directory of the sound files, relative to the datadir, followed
128 	///       by the base filename of a sound effect (relative to the directory).
129 	///    priority:
130 	///       An integer. If omitted, 127 is used.
131 	///
132 	/// Plays the specified sound effect with the specified priority. Whether the
133 	/// sound effect is actually played is determined by the sound handler.
134 	struct ActPlaySound : public Action {
135 		ActPlaySound(const std::vector<std::string>& arguments);
136 		void execute(Game&, Immovable&) const override;
137 
138 	private:
139 		PlaySoundParameters parameters;
140 	};
141 
142 	/**
143 	 * Puts the immovable into construction mode.
144 	 *
145 	 * Parameter syntax:
146 	 *    parameters ::= animation build-time decay-time
147 	 * Parameter semantics:
148 	 *    animation:
149 	 *       The basic animation to be displayed during construction.
150 	 *    build-time:
151 	 *       Time for a single building step.
152 	 *    decay-time:
153 	 *       Time until construction decays one step if no progress has been made.
154 	 */
155 	struct ActConstruct : public Action {
156 		ActConstruct(std::vector<std::string>& arguments, const ImmovableDescr&);
157 		void execute(Game&, Immovable&) const override;
158 
buildtimeImmovableProgram::ActConstruct159 		Duration buildtime() const {
160 			return buildtime_;
161 		}
decaytimeImmovableProgram::ActConstruct162 		Duration decaytime() const {
163 			return decaytime_;
164 		}
165 
166 	private:
167 		std::string animation_name_;
168 		Duration buildtime_;
169 		Duration decaytime_;
170 	};
171 
172 	/// Create a program with a single action.
173 	ImmovableProgram(const std::string& init_name, std::unique_ptr<Action> action);
174 
175 	/// Create an immovable program from a number of lines.
176 	ImmovableProgram(const std::string& init_name,
177 	                 const std::vector<std::string>& lines,
178 	                 const ImmovableDescr& immovable);
179 
~ImmovableProgramImmovableProgram180 	~ImmovableProgram() {
181 	}
182 
sizeImmovableProgram183 	size_t size() const {
184 		return actions_.size();
185 	}
186 	const Action& operator[](size_t const idx) const {
187 		assert(idx < actions_.size());
188 		return *actions_[idx];
189 	}
190 
191 private:
192 	std::vector<std::unique_ptr<Action>> actions_;
193 };
194 
195 struct ImmovableActionData {
ImmovableActionDataImmovableActionData196 	ImmovableActionData() {
197 	}
~ImmovableActionDataImmovableActionData198 	virtual ~ImmovableActionData() {
199 	}
200 
201 	virtual const char* name() const = 0;
202 	virtual void save(FileWrite& fw, Immovable& imm) const = 0;
203 
204 	static ImmovableActionData* load(FileRead& fr, Immovable& imm, const std::string& name);
205 };
206 
207 struct ActConstructData : ImmovableActionData {
208 	const char* name() const override;
209 	void save(FileWrite& fw, Immovable& imm) const override;
210 	static ActConstructData* load(FileRead& fr, Immovable& imm);
211 
212 	Buildcost delivered;
213 };
214 }  // namespace Widelands
215 
216 #endif  // end of include guard: WL_LOGIC_MAP_OBJECTS_IMMOVABLE_PROGRAM_H
217