File: editor_game_base.h

package info (click to toggle)
widelands 1:19+repack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (279 lines) | stat: -rw-r--r-- 9,558 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2002-2004, 2006-2008, 2010-2011 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef WL_LOGIC_EDITOR_GAME_BASE_H
#define WL_LOGIC_EDITOR_GAME_BASE_H

#include <cstring>
#include <memory>
#include <string>
#include <vector>

#include "base/macros.h"
#include "logic/map.h"
#include "logic/map_objects/bob.h"
#include "logic/map_objects/tribes/building.h"
#include "logic/player_area.h"
#include "notifications/notifications.h"
#include "scripting/lua_interface.h"

namespace UI {
struct ProgressWindow;
}
struct FullscreenMenuLaunchGame;
class InteractiveBase;

namespace Widelands {

class PlayersManager;

class Battle;
class Bob;
class BuildingDescr;
class Immovable;
class Map;
struct ObjectManager;
class Player;
struct PlayerImmovable;
class Tribes;
class TribeDescr;
struct Flag;
struct AttackController;

struct NoteFieldPossession {
	CAN_BE_SENT_AS_NOTE(NoteId::FieldPossession)

	// Has this been lost or gained?
	enum class Ownership { LOST, GAINED };
	Ownership ownership;

	// The field that has been lost/gained.
	FCoords fc;

	// The player that has lost or gained this field.
	Player* player;

	NoteFieldPossession(const FCoords& init_fc, Ownership const init_ownership, Player* init_player)
	   : ownership(init_ownership), fc(init_fc), player(init_player) {
	}
};

class EditorGameBase {
public:
	friend class InteractiveBase;
	friend struct FullscreenMenuLaunchGame;
	friend struct GameClassPacket;

	EditorGameBase(LuaInterface* lua);
	virtual ~EditorGameBase();

	void set_map(Map*);
	// TODO(sirver): this should just be const Map& map() and Map* mutable_map().
	Map& map() const {
		return *map_;
	}
	Map* get_map() {
		return map_;
	}
	Map& get_map() const {
		return *map_;
	}
	const ObjectManager& objects() const {
		return objects_;
	}
	ObjectManager& objects() {
		return objects_;
	}

	// logic handler func
	virtual void think();

	// Player commands
	void remove_player(PlayerNumber);
	Player* add_player(PlayerNumber,
	                   uint8_t initialization_index,
	                   const std::string& tribe,
	                   const std::string& name,
	                   TeamNumber team = 0);
	Player* get_player(int32_t n) const;
	Player& player(int32_t n) const;
	virtual Player* get_safe_player(PlayerNumber);

	// loading stuff
	void allocate_player_maps();
	virtual void postload();
	void load_graphics(UI::ProgressWindow& loader_ui);
	virtual void cleanup_for_load();

	void set_road(const FCoords&, uint8_t direction, uint8_t roadtype);

	// warping stuff. instantly creating map_objects
	Building&
	warp_building(const Coords&,
	              PlayerNumber,
	              DescriptionIndex,
	              Building::FormerBuildings former_buildings = Building::FormerBuildings());
	Building&
	warp_constructionsite(const Coords&,
	                      PlayerNumber,
	                      DescriptionIndex,
	                      bool loading = false,
	                      Building::FormerBuildings former_buildings = Building::FormerBuildings());
	Building&
	warp_dismantlesite(const Coords&,
	                   PlayerNumber,
	                   bool loading = false,
	                   Building::FormerBuildings former_buildings = Building::FormerBuildings());
	Bob& create_critter(const Coords&, DescriptionIndex bob_type_idx, Player* owner = nullptr);
	Bob& create_critter(const Coords&, const std::string& name, Player* owner = nullptr);
	Immovable& create_immovable(const Coords&,
	                            DescriptionIndex idx,
	                            MapObjectDescr::OwnerType = MapObjectDescr::OwnerType::kWorld);
	Immovable& create_immovable(const Coords&,
	                            const std::string& name,
	                            MapObjectDescr::OwnerType = MapObjectDescr::OwnerType::kWorld);
	Bob& create_ship(const Coords&, int ship_type_idx, Player* owner = nullptr);
	Bob& create_ship(const Coords&, const std::string& name, Player* owner = nullptr);

	uint32_t get_gametime() const {
		return gametime_;
	}
	InteractiveBase* get_ibase() const {
		return ibase_.get();
	}

	// safe system for storing pointers to non-MapObject C++ objects
	// unlike objects in the ObjectManager, these pointers need not be
	// synchronized across the network, and they are not saved in savegames
	uint32_t add_trackpointer(void*);
	void* get_trackpointer(uint32_t serial);
	void remove_trackpointer(uint32_t serial);

	void inform_players_about_ownership(MapIndex, PlayerNumber);
	void inform_players_about_immovable(MapIndex, MapObjectDescr const*);
	void inform_players_about_road(FCoords, MapObjectDescr const*);

	void unconquer_area(PlayerArea<Area<FCoords>>, PlayerNumber destroying_player = 0);
	void conquer_area(PlayerArea<Area<FCoords>>);
	void conquer_area_no_building(PlayerArea<Area<FCoords>> const);

	void cleanup_objects() {
		objects().cleanup(*this);
	}

	// next function is used to update the current gametime,
	// for queue runs e.g.
	uint32_t& get_gametime_pointer() {
		return gametime_;
	}
	void set_ibase(InteractiveBase* const b);

	/// Lua frontend, used to run Lua scripts
	virtual LuaInterface& lua() {
		return *lua_;
	}

	PlayersManager* player_manager() {
		return player_manager_.get();
	}

	InteractiveGameBase* get_igbase();

	// Returns the world.
	const World& world() const;

	// Returns the world that can be modified. Prefer world() whenever possible.
	World* mutable_world();

	// Returns the tribes.
	const Tribes& tribes() const;

	// Returns the mutable tribes. Prefer tribes() whenever possible.
	Tribes* mutable_tribes();

private:
	/// Common function for create_critter and create_ship.
	Bob& create_bob(Coords, const BobDescr&, Player* owner = nullptr);

	/// \param preferred_player
	///  When conquer is false, this can be used to prefer a player over other
	///  players, when lost land is reassigned. This can for example be used to
	///  reward the player who actually destroyed a MilitarySite by giving an
	///  unconquered location that he has influence over to him, instead of some
	///  other player who has higher influence over that location. If 0, land is
	///  simply assigned by influence.
	///
	/// \param neutral_when_no_influence
	///  If true and the player completely loses influence over a location, it
	///  becomes neutral unless some other player claims it by having positive
	///  influence.
	///
	/// \param neutral_when_competing_influence
	///  If true and the player completely loses influence over a location and
	///  several players have positive and equal influence, the location becomes
	///  becomes neutral unless some other player claims it by having higher
	///  influence.
	///
	/// \param conquer_guarded_location_by_superior_influence
	///  If true, the conquering player will (automatically, without actually
	///  attacking) conquer a location even if another player already owns and
	///  covers the location with a militarysite, if the conquering player's
	///  influence becomes greater than the owner's influence.
	virtual void do_conquer_area(PlayerArea<Area<FCoords>> player_area,
	                             bool conquer,
	                             PlayerNumber preferred_player = 0,
	                             bool neutral_when_no_influence = false,
	                             bool neutral_when_competing_influence = false,
	                             bool conquer_guarded_location_by_superior_influence = false);
	void cleanup_playerimmovables_area(PlayerArea<Area<FCoords>>);

	// Changes the owner of 'fc' from the current player to the new player and
	// sends notifications about this.
	void change_field_owner(const FCoords& fc, PlayerNumber new_owner);

	uint32_t gametime_;
	ObjectManager objects_;

	std::unique_ptr<LuaInterface> lua_;
	std::unique_ptr<PlayersManager> player_manager_;

	std::unique_ptr<World> world_;
	std::unique_ptr<Tribes> tribes_;
	std::unique_ptr<InteractiveBase> ibase_;
	Map* map_;

	uint32_t lasttrackserial_;
	std::map<uint32_t, void*> trackpointers_;

	DISALLOW_COPY_AND_ASSIGN(EditorGameBase);
};

#define iterate_players_existing(p, nr_players, egbase, player)                                    \
	iterate_player_numbers(                                                                         \
	   p, nr_players) if (Widelands::Player* const player = (egbase).get_player(p))

#define iterate_players_existing_novar(p, nr_players, egbase)                                      \
	iterate_player_numbers(p, nr_players) if ((egbase).get_player(p))

#define iterate_players_existing_const(p, nr_players, egbase, player)                              \
	iterate_player_numbers(                                                                         \
	   p, nr_players) if (Widelands::Player const* const player = (egbase).get_player(p))
}

#endif  // end of include guard: WL_LOGIC_EDITOR_GAME_BASE_H