]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.h
Rename TrackPoint to a more generic OrientedPoint
[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 "trackoffsetiter.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         const VehicleType &type;
62         Train *train;
63         Vehicle *next;
64         Vehicle *prev;
65         TrackOffsetIter track;
66         std::vector<Axle> axles;
67         std::vector<Bogie> bogies;
68         std::vector<Rod> rods;
69         unsigned front_sensor;
70         unsigned back_sensor;
71
72 public:
73         Vehicle(Layout &, const VehicleType &);
74         ~Vehicle();
75
76         virtual Vehicle *clone(Layout * = 0) const;
77         virtual const VehicleType &get_type() const { return type; }
78
79         void set_train(Train *);
80         Train *get_train() const { return train; }
81         void attach_back(Vehicle &);
82         void attach_front(Vehicle &);
83         void detach_back();
84         void detach_front();
85         Vehicle *get_next() const { return next; }
86         Vehicle *get_previous() const { return prev; }
87
88         // TODO implement these - should call place() with suitable parameters
89         virtual void set_position(const Vector &) { }
90         virtual void set_rotation(const Angle &) { }
91         virtual void set_tilt(const Angle &) { }
92         void place(const TrackIter &, float, PlaceMode = CENTER);
93         void unplace();
94         void advance(float);
95         const TrackOffsetIter &get_track_iter() const { return track; }
96         Track *get_track() const { return track.track(); }
97         unsigned get_entry() const { return track.entry(); }
98         float get_offset() const { return track.offset(); }
99         const Axle &get_fixed_axle(unsigned) const;
100         const Bogie &get_bogie(unsigned) const;
101         const Axle &get_bogie_axle(unsigned, unsigned) const;
102         const Rod &get_rod(unsigned) const;
103 private:
104         void update_position();
105         void update_position_from(const Vehicle &);
106         void propagate_position();
107         void propagate_forward();
108         void propagate_backward();
109         void check_sensor(float, unsigned &);
110         void turn_axles(float);
111         void update_rods();
112
113         void adjust_for_distance(TrackOffsetIter &, TrackOffsetIter &, float, float = 0.5) const;
114         OrientedPoint get_point(const Vector &, const Vector &, float = 0.5) const;
115         OrientedPoint get_point(const TrackOffsetIter &, float, float = 0.5) const;
116
117 public:
118         virtual unsigned get_n_link_slots() const;
119         virtual Vehicle *get_link(unsigned) const;
120         virtual int get_link_slot(const Object &) const;
121 };
122
123 } // namespace R2C2
124
125 #endif