~widelands-dev/widelands/trunk

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
/*
 * Copyright (C) 2011-2024 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, see <https://www.gnu.org/licenses/>.
 *
 */

#ifndef WL_ECONOMY_FERRY_FLEET_H
#define WL_ECONOMY_FERRY_FLEET_H

#include "base/macros.h"
#include "logic/map_objects/bob.h"

namespace Widelands {

struct Ferry;
class FerryFleetYardInterface;
class ProductionSite;
struct Waterway;

class FerryFleetDescr : public MapObjectDescr {
public:
	FerryFleetDescr(char const* const init_name, char const* const init_descname)
	   : MapObjectDescr(MapObjectType::FERRY_FLEET, init_name, init_descname) {
	}
	~FerryFleetDescr() override = default;

private:
	DISALLOW_COPY_AND_ASSIGN(FerryFleetDescr);
};

class FerryFleetYardInterfaceDescr : public BobDescr {
public:
	FerryFleetYardInterfaceDescr(char const* const init_name, char const* const init_descname)
	   : BobDescr(init_name,
	              init_descname,
	              MapObjectType::FERRY_FLEET_YARD_INTERFACE,
	              MapObjectDescr::OwnerType::kTribe) {
	}
	~FerryFleetYardInterfaceDescr() override = default;
	[[nodiscard]] Bob& create_object() const override;

private:
	DISALLOW_COPY_AND_ASSIGN(FerryFleetYardInterfaceDescr);
};

/**
 * Manage all ferries and waterways of a player that are connected by ocean.
 *
 * @paragraph Lifetime
 *
 * Fleet objects are created on-the-fly by @ref Ferry and @ref Waterway,
 * and destroy themselves when they become empty.
 *
 * The intention is for fleet objects to merge automatically and separate
 * again in reaction to changes in the map. However, this may not work
 * properly at the moment.
 */
struct FerryFleet : MapObject {
	const FerryFleetDescr& descr() const;

	explicit FerryFleet(Player* player);

	[[nodiscard]] bool active() const;

	bool init(EditorGameBase&) override;
	bool init(EditorGameBase&, Waterway*);
	void cleanup(EditorGameBase&) override;
	void update(EditorGameBase&, const Duration& tdelta = Duration(100));

	void add_ferry(Ferry* ferry);
	void add_interface(FerryFleetYardInterface* i);
	void remove_ferry(EditorGameBase& egbase, Ferry* ferry);
	void remove_interface(EditorGameBase& egbase, FerryFleetYardInterface* i);

	void log_general_info(const EditorGameBase&) const override;

	[[nodiscard]] uint32_t count_ferries() const {
		return ferries_.size();
	}
	[[nodiscard]] uint32_t count_unemployed_ferries() const;
	[[nodiscard]] uint32_t count_unattended_waterways() const {
		return pending_ferry_requests_.size();
	}
	[[nodiscard]] bool has_ferry(const Waterway& ww) const;
	[[nodiscard]] uint32_t count_total_waterways() const;

	[[nodiscard]] Quantity get_idle_ferries_target() const {
		return idle_ferries_target_;
	}
	void set_idle_ferries_target(EditorGameBase& egbase, Quantity t);
	[[nodiscard]] bool lacks_ferry() const {
		return !pending_ferry_requests_.empty() ||
		       count_unemployed_ferries() < get_idle_ferries_target();
	}

	void request_ferry(const EditorGameBase& egbase, Waterway* waterway, const Time&);
	void reroute_ferry_request(Game& game, Waterway* oldww, Waterway* newww);
	void cancel_ferry_request(Game& game, Waterway* waterway);

	[[nodiscard]] bool empty() const;

protected:
	void act(Game&, uint32_t data) override;

private:
	bool find_other_fleet(EditorGameBase& egbase);
	bool merge(EditorGameBase& egbase, FerryFleet* other);

	std::vector<Ferry*> ferries_;
	std::vector<FerryFleetYardInterface*> interfaces_;
	std::multimap<Time, Waterway*> pending_ferry_requests_;

	Quantity idle_ferries_target_{0U};
	Time target_last_modified_{0U};
	bool act_pending_{false};

	// saving and loading
protected:
	struct Loader : MapObject::Loader {
		Loader() = default;

		void load(FileRead&, uint8_t packet_version);
		void load_pointers() override;

	private:
		std::vector<uint32_t> ferries_;
		std::vector<uint32_t> interfaces_;
		std::multimap<Time, uint32_t> pending_ferry_requests_;
	};

public:
	bool has_new_save_support() override {
		return true;
	}
	void save(EditorGameBase&, MapObjectSaver&, FileWrite&) override;

	static MapObject::Loader* load(EditorGameBase&, MapObjectLoader&, FileRead&);
};

class FerryFleetYardInterface : public Bob {
public:
	FerryFleetYardInterface();
	static FerryFleetYardInterface*
	create(EditorGameBase& egbase, ProductionSite& ps, const Coords& pos);

	const FerryFleetYardInterfaceDescr& descr() const;
	void init_auto_task(Game& game) override;
	void cleanup(EditorGameBase&) override;
	void log_general_info(const EditorGameBase&) const override;

	[[nodiscard]] ProductionSite* get_building() const {
		return building_;
	}
	[[nodiscard]] FerryFleet* get_fleet() const {
		return fleet_;
	}
	void set_fleet(FerryFleet* fleet) {
		fleet_ = fleet;
	}

	void save(EditorGameBase&, MapObjectSaver&, FileWrite&) override;
	static Loader* load(EditorGameBase&, MapObjectLoader&, FileRead&);

private:
	ProductionSite* building_{nullptr};
	FerryFleet* fleet_{nullptr};

protected:
	struct Loader : Bob::Loader {
		Loader() = default;

		void load(FileRead& fr);
		void load_pointers() override;

	private:
		Serial building_;
	};
};

}  // namespace Widelands

#endif  // end of include guard: WL_ECONOMY_FERRY_FLEET_H