1 /*
2  * Copyright (C) 2004-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_ECONOMY_ROADBASE_H
21 #define WL_ECONOMY_ROADBASE_H
22 
23 #include "base/macros.h"
24 #include "base/wexception.h"
25 #include "graphic/road_segments.h"
26 #include "logic/map_objects/immovable.h"
27 #include "logic/path.h"
28 
29 namespace Widelands {
30 struct Carrier;
31 
32 class RoadBaseDescr : public MapObjectDescr {
33 public:
RoadBaseDescr(char const * const init_name,char const * const init_descname,MapObjectType mot)34 	RoadBaseDescr(char const* const init_name, char const* const init_descname, MapObjectType mot)
35 	   : MapObjectDescr(mot, init_name, init_descname, "") {
36 	}
~RoadBaseDescr()37 	~RoadBaseDescr() override {
38 	}
39 
40 private:
41 	DISALLOW_COPY_AND_ASSIGN(RoadBaseDescr);
42 };
43 
44 /**
45  * RoadBase is a special object which connects two flags.
46  * The RoadBase itself is never rendered; however, the appropriate Field::roads are
47  * set to represent the RoadBase visually.
48  * The actual steps involved in a RoadBase are stored as a Path from the starting
49  * flag to the ending flag. Apart from that, however, the two flags are treated
50  * exactly the same, as far as most transactions are concerned. There are minor
51  * exceptions: placement of carriers if the path's length is odd, splitting
52  * a RoadBase when a flag is inserted.
53  *
54  */
55 struct RoadBase : public PlayerImmovable {
56 
57 	enum FlagId { FlagStart = 0, FlagEnd = 1 };
58 
59 	RoadBase(const RoadBaseDescr& d);
60 
get_flagRoadBase61 	Flag& get_flag(FlagId const flag) const {
62 		return *flags_[flag];
63 	}
64 
65 	int32_t get_size() const override;
66 	bool get_passable() const override;
67 	PositionList get_positions(const EditorGameBase&) const override;
68 
69 	Flag& base_flag() override;
70 
71 	int32_t get_cost(FlagId fromflag);
get_pathRoadBase72 	const Path& get_path() const {
73 		return path_;
74 	}
get_idle_indexRoadBase75 	int32_t get_idle_index() const {
76 		return idle_index_;
77 	}
78 
79 	void presplit(Game&, Coords split);
80 	virtual void postsplit(Game&, Flag&);
81 
82 	virtual bool notify_ware(Game&, FlagId);
83 
84 	virtual void assign_carrier(Carrier&, uint8_t);
85 
86 protected:
87 	bool init(EditorGameBase&) override;
88 	void cleanup(EditorGameBase&) override;
89 
90 	/** The road is drawn by the terrain renderer via marked fields. */
drawRoadBase91 	void draw(uint32_t, InfoToDraw, const Vector2f&, const Coords&, float, RenderTarget*) override {
92 	}
93 
94 	void set_path(EditorGameBase&, const Path&);
95 
96 	void mark_map(EditorGameBase&);
97 	void unmark_map(EditorGameBase&);
98 
99 	virtual void link_into_flags(EditorGameBase&, bool = false);
100 
101 	virtual RoadSegment road_type_for_drawing() const = 0;
102 	void set_roadtype(EditorGameBase&, FCoords, uint8_t, RoadSegment) const;
is_bridgeRoadBase103 	virtual bool is_bridge(const EditorGameBase&, const FCoords&, uint8_t) const {
104 		return false;
105 	}
106 
107 	Flag* flags_[2];      ///< start and end flag
108 	int32_t flagidx_[2];  ///< index of this road in the flag's road array
109 
110 	/// cost for walking this road (0 = from start to end, 1 = from end to start)
111 	int32_t cost_[2];
112 
113 	Path path_;            ///< path goes from start to end
114 	uint32_t idle_index_;  ///< index into path where carriers should idle
115 };
116 }  // namespace Widelands
117 
118 #endif  // end of include guard: WL_ECONOMY_ROADBASE_H
119