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