1 #ifndef BTANKS_GAME_MONITOR_H__
2 #define BTANKS_GAME_MONITOR_H__
3 
4 /* Battle Tanks Game
5  * Copyright (C) 2006-2009 Battle Tanks team
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21 
22 /*
23  * Additional rights can be granted beyond the GNU General Public License
24  * on the terms provided in the Exception. If you modify this file,
25  * you may extend this exception to your version of the file,
26  * but you are not obligated to do so. If you do not wish to provide this
27  * exception without modification, you must delete this exception statement
28  * from your version and license this file solely under the GPL without exception.
29 */
30 
31 #include <deque>
32 #include <string>
33 #include <set>
34 #include <map>
35 
36 #include "mrt/singleton.h"
37 #include "mrt/serializable.h"
38 #include "alarm.h"
39 #include "math/v2.h"
40 #include "math/v3.h"
41 #include "sdlx/sdlx.h"
42 #include "export_btanks.h"
43 #include "menu/box.h"
44 #include "sl08/sl08.h"
45 #include "team.h"
46 
47 namespace sdlx {
48 class Surface;
49 class Rect;
50 }
51 
52 class BaseObject;
53 class Object;
54 class Campaign;
55 class PlayerSlot;
56 class SpecialZone;
57 
58 struct BTANKSAPI GameItem {
59 	GameItem(const std::string &classname, const std::string &animation, const std::string &property, const v2<int> position, const int z = 0) :
classnameGameItem60 		classname(classname), animation(animation), property(property), position(position), z(z), dir(0), id(-1), spawn_limit(-1), dead_on(0),
61 		destroy_for_victory(false), hidden(false), special(false) {}
62 	void respawn();
63 	void kill();
64 	void renameProperty(const std::string &name);
65 	void updateMapProperty();
66 	void setup(const std::string &name, const std::string &subname);
67 
68 	std::string classname, animation, property;
69 	v2<int> position;
70 	int z, dir;
71 
72 	int id, spawn_limit;
73 	Uint32 dead_on;
74 	bool destroy_for_victory;
75 	std::string save_for_victory;
76 	bool hidden, special;
77 };
78 
79 class LuaHooks;
80 
81 class BTANKSAPI IGameMonitor {
82 public:
83 	IGameMonitor();
84 	~IGameMonitor();
85 	DECLARE_SINGLETON(IGameMonitor);
86 
87 	void add(const GameItem &item, bool dont_respawn = false);
88 	GameItem& find(const std::string &property);
89 	void eraseLast(const std::string &property);
90 	GameItem& find(const Object *o);
91 	const GameItem& find(const Object *o) const;
92 	const std::string generatePropertyName(const std::string &prefix);
93 
94 	void checkItems(const float dt);
95 
getSpecials()96 	const std::vector<v3<int> >& getSpecials() const { return _specials; }
getFlags()97 	const std::vector<v3<int> >& getFlags() const { return _flags; }
98 
getItemsCount()99 	const size_t getItemsCount() const { return _items.size(); }
100 
101 	void game_over(const std::string &area, const std::string &message, float time, const bool win);
102 	void displayMessage(const std::string &area, const std::string &message, float time, const bool global = false);
103 	void hideMessage();
104 	void setTimer(const std::string &area, const std::string &message, float time, const bool win_at_end);
105 	void resetTimer();
106 
107 	void clear();
108 
109 	void tick(const float dt);
110 
111 	void pushState(const std::string &state, float time);
112 	const std::string popState(const float dt);
113 
114 	void render(sdlx::Surface &window);
115 
116 	const bool disabled(const Object *o) const;
117 	void disable(const std::string &classname, const bool value = true);
118 
119 	void serialize(mrt::Serializator &s) const;
120 	void deserialize(const mrt::Serializator &s);
121 
122 	void killAllClasses(const std::set<std::string> &classes);
123 
124 	void loadMap(Campaign * campaign, const std::string &name, const bool spawn = true, const bool skip_loadmap = false);
125 	void startGame(Campaign *campaign, const std::string &name);
126 
127 	//waypoints
128 	const bool hasWaypoints(const std::string &classname) const;
129 	const std::string getRandomWaypoint(const std::string &classname, const std::string &last_wp = std::string()) const;
130 	const std::string get_nearest_waypoint(const Object *obj, const std::string &classname) const;
131 	void get_waypoint(v2<float> &wp, const std::string &classname, const std::string &name);
132 
133 	void renderWaypoints(sdlx::Surface &surface, const sdlx::Rect &src, const sdlx::Rect &viewport);
134 
135 	void addBonuses(const PlayerSlot &slot);
136 
getCampaign()137 	const Campaign * getCampaign() const {return _campaign; }
138 
game_over()139 	const bool game_over() const { return _game_over; }
140 
141 	void onScriptZone(const int slot_id, const SpecialZone &zone, const bool global);
setSpecials(const std::vector<int> & ex)142 	void setSpecials(const std::vector<int> &ex) { _external_specials = ex; }
143 
144 	const bool usedInCampaign(const std::string &base, const std::string &id) const;
145 	const void useInCampaign(const std::string &base, const std::string &id);
146 
147 	sl08::slot4<void, int, int, int, int, IGameMonitor> on_map_resize_slot;
148 	void parseWaypoints(int, int, int, int);
149 
150 	void onTooltip(const std::string &event, const int slot_id, const std::string &area, const std::string &message);
151 
152 	void startGameTimer(const std::string &name, const float period, const bool repeat);
153 	void stopGameTimer(const std::string &name);
154 
155 	const int getBase(const Team::ID id) const;
156 
157 private:
158 	sl08::slot1<void, const Object *, IGameMonitor> add_object_slot;
159 	sl08::slot1<void, const Object *, IGameMonitor> delete_object_slot;
160 	void addObject(const Object *o);
161 	void deleteObject(const Object *o);
162 
163 	void saveCampaign();
164 
165 	sl08::slot2<const std::string, const std::string &, const std::string &, IGameMonitor> on_console_slot;
166 	const std::string onConsole(const std::string &cmd, const std::string &param);
167 
168 	bool _game_over, _win;
169 
170 	typedef std::deque<GameItem> Items;
171 	Items _items;
172 	std::vector<int> _flag_id;
173 	std::vector<v3<int> > _specials, _flags;
174 	std::vector<int> _external_specials;
175 
176 	Alarm _check_items;
177 
178 	//displaying messages
179 	Box _state_bg;
180 	std::string _state;
181 	Alarm _state_timer;
182 
183 	std::string _timer_message, _timer_message_area;
184 	float _timer;
185 	bool _timer_win_at_end;
186 
187 	std::set<std::string> _disabled;
188 	std::set<std::string> _destroy_classes;
189 	std::set<int> _present_objects;
190 	bool _objects_limit_reached;
191 
192 	//waypoints stuff
193 	typedef std::map<const std::string, v2<int> > WaypointMap;
194 	typedef std::map<const std::string, WaypointMap> WaypointClassMap;
195 	typedef std::multimap<const std::string, std::string> WaypointEdgeMap;
196 
197 	WaypointMap 	 _all_waypoints;
198 	WaypointClassMap _waypoints;
199 	WaypointEdgeMap  _waypoint_edges;
200 
201 	Campaign * _campaign;
202 
203 	struct GameBonus {
204 		std::string classname, animation;
205 		int id;
GameBonusGameBonus206 		GameBonus(const std::string &classname, const std::string &animation, const int id) :
207 			classname(classname), animation(animation), id(id) {}
208 	};
209 	std::vector<GameBonus> bonuses;
210 #ifdef ENABLE_LUA
211 	LuaHooks* lua_hooks;
212 #endif
213 
214 	std::set<std::pair<std::string, std::string> > used_maps;
215 
216 	void processGameTimers(const float dt);
217 	struct Timer {
218 		float t, period;
219 		bool repeat;
TimerTimer220 		Timer(const float period, const bool repeat): t(0), period(period), repeat(repeat) {}
221 	};
222 	typedef std::map<const std::string, Timer> Timers;
223 	Timers timers;
224 
225 	int team_base[4];
226 	float total_time;
227 };
228 
229 PUBLIC_SINGLETON(BTANKSAPI, GameMonitor, IGameMonitor);
230 
231 #endif
232 
233