]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.h
94ae2b1683e648cc8f07cb9edfc1ff56d107ecdf
[r2c2.git] / source / libr2c2 / vehicle.h
1 #ifndef LIBR2C2_VEHICLE_H_
2 #define LIBR2C2_VEHICLE_H_
3
4 #include "geometry.h"
5 #include "object.h"
6 #include "trackiter.h"
7 #include "vehicletype.h"
8
9 namespace R2C2 {
10
11 class Layout;
12 class Train;
13
14 class attachment_error: public std::logic_error
15 {
16 public:
17         attachment_error(const std::string &w): std::logic_error(w) { }
18         virtual ~attachment_error() throw() { }
19 };
20
21
22 class Vehicle: public Object
23 {
24 public:
25         enum PlaceMode
26         {
27                 CENTER,
28                 FRONT_AXLE,
29                 FRONT_BUFFER,
30                 BACK_AXLE,
31                 BACK_BUFFER
32         };
33
34         struct Axle
35         {
36                 const VehicleType::Axle *type;
37                 Angle angle;
38
39                 Axle(const VehicleType::Axle &);
40         };
41
42         struct Bogie
43         {
44                 const VehicleType::Bogie *type;
45                 Angle direction;
46                 std::vector<Axle> axles;
47
48                 Bogie(const VehicleType::Bogie &);
49         };
50
51         struct Rod
52         {
53                 const VehicleType::Rod *type;
54                 Vector position;
55                 Angle angle;
56
57                 Rod(const VehicleType::Rod &);
58         };
59
60 private:
61         struct TrackPosition
62         {
63                 TrackIter track;
64                 float offs;
65
66                 TrackPosition();
67                 TrackPosition(const TrackIter &, float);
68                 void advance(float);
69                 TrackPoint get_point() const;
70         };
71
72         const VehicleType &type;
73         Train *train;
74         Vehicle *next;
75         Vehicle *prev;
76         TrackPosition track_pos;
77         std::vector<Axle> axles;
78         std::vector<Bogie> bogies;
79         std::vector<Rod> rods;
80         unsigned front_sensor;
81         unsigned back_sensor;
82
83 public:
84         Vehicle(Layout &, const VehicleType &);
85         ~Vehicle();
86
87         virtual Vehicle *clone(Layout * = 0) const;
88         virtual const VehicleType &get_type() const { return type; }
89
90         void set_train(Train *);
91         Train *get_train() const { return train; }
92         void attach_back(Vehicle &);
93         void attach_front(Vehicle &);
94         void detach_back();
95         void detach_front();
96         Vehicle *get_next() const { return next; }
97         Vehicle *get_previous() const { return prev; }
98
99         // TODO implement these - should call place() with suitable parameters
100         virtual void set_position(const Vector &) { }
101         virtual void set_rotation(const Angle &) { }
102         virtual void set_tilt(const Angle &) { }
103         void place(const TrackIter &, float, PlaceMode = CENTER);
104         void unplace();
105         void advance(float);
106         const TrackIter &get_track_iter() const { return track_pos.track; }
107         Track *get_track() const { return track_pos.track.track(); }
108         unsigned get_entry() const { return track_pos.track.entry(); }
109         float get_offset() const { return track_pos.offs; }
110         const Axle &get_fixed_axle(unsigned) const;
111         const Bogie &get_bogie(unsigned) const;
112         const Axle &get_bogie_axle(unsigned, unsigned) const;
113         const Rod &get_rod(unsigned) const;
114 private:
115         void update_position();
116         void update_position_from(const Vehicle &);
117         void propagate_position();
118         void propagate_forward();
119         void propagate_backward();
120         void check_sensor(float, unsigned &);
121         void turn_axles(float);
122         void update_rods();
123
124         void adjust_for_distance(TrackPosition &, TrackPosition &, float, float = 0.5) const;
125         TrackPoint get_point(const Vector &, const Vector &, float = 0.5) const;
126         TrackPoint get_point(const TrackPosition &, float, float = 0.5) const;
127
128 public:
129         virtual unsigned get_n_link_slots() const;
130         virtual Vehicle *get_link(unsigned) const;
131         virtual int get_link_slot(const Object &) const;
132 };
133
134 } // namespace R2C2
135
136 #endif