]> git.tdb.fi Git - r2c2.git/blob - libr2c2/terrain.h
Don't crash if a train has no router
[r2c2.git] / libr2c2 / terrain.h
1 #ifndef LIBR2C2_TERRAIN_H_
2 #define LIBR2C2_TERRAIN_H_
3
4 #include "object.h"
5 #include "terraintype.h"
6
7 namespace R2C2 {
8
9 class Terrain: public Object
10 {
11 public:
12         class Loader: public Msp::DataFile::ObjectLoader<Terrain>
13         {
14         private:
15                 unsigned next_tile;
16
17         public:
18                 Loader(Terrain &);
19
20         private:
21                 void position(float, float, float);
22                 void rotation(float);
23                 void size(unsigned, unsigned);
24                 void tile();
25                 void tile_coords(unsigned, unsigned);
26         };
27
28         struct Node
29         {
30                 class Loader: public Msp::DataFile::ObjectLoader<Node>
31                 {
32                 private:
33                         Terrain &terrain;
34
35                 public:
36                         Loader(Terrain &, Node &);
37
38                 private:
39                         void ground(unsigned);
40                 };
41
42                 float elevation;
43                 unsigned ground;
44                 unsigned wall;
45
46                 Node();
47
48                 void save(std::list<Msp::DataFile::Statement> &) const;
49         };
50
51         struct Tile
52         {
53                 class Loader: public Msp::DataFile::ObjectLoader<Tile>
54                 {
55                 private:
56                         Terrain &terrain;
57
58                 public:
59                         Loader(Terrain &, Tile &);
60
61                 private:
62                         void ground(unsigned);
63                         void elevation(float);
64                         void node(unsigned);
65                 };
66
67                 Node nodes[4];
68                 bool secondary_axis;
69
70                 Tile();
71
72                 void set_node_elevation(unsigned, float);
73                 void save(std::list<Msp::DataFile::Statement> &) const;
74         };
75
76         struct NodeCoordinates
77         {
78                 unsigned x, y;
79                 unsigned i;
80
81                 NodeCoordinates();
82                 NodeCoordinates(unsigned, unsigned, unsigned);
83         };
84
85         sigc::signal<void, unsigned, unsigned> signal_size_changed;
86         sigc::signal<void, unsigned, unsigned> signal_tile_changed;
87
88 private:
89         const TerrainType &type;
90         unsigned width;
91         unsigned height;
92         std::vector<Tile> tiles;
93
94 public:
95         Terrain(Layout &, const TerrainType &);
96         virtual ~Terrain();
97
98         virtual Terrain *clone(Layout *) const;
99
100         virtual const TerrainType &get_type() const { return type; }
101         virtual void set_position(const Vector &);
102         virtual void set_rotation(const Angle &);
103         virtual void set_tilt(const Angle &) { }
104
105         void set_size(unsigned, unsigned);
106         unsigned get_width() const { return width; }
107         unsigned get_height() const { return height; }
108         const Tile &get_tile(unsigned, unsigned) const;
109         void set_node_elevation(const NodeCoordinates &, float, bool = false);
110         float get_node_elevation(const NodeCoordinates &) const;
111         Vector get_node_position(const NodeCoordinates &) const;
112         NodeCoordinates get_closest_node(const Ray &) const;
113
114         void save(std::list<Msp::DataFile::Statement> &) const;
115 };
116
117 } // namespace R2C2
118
119 #endif