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