]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.h
Split vehicle placement code to a separate class
[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 "vehicleplacement.h"
8 #include "vehicletype.h"
9
10 namespace R2C2 {
11
12 class Layout;
13 class Train;
14
15 class attachment_error: public std::logic_error
16 {
17 public:
18         attachment_error(const std::string &w): std::logic_error(w) { }
19         virtual ~attachment_error() throw() { }
20 };
21
22
23 class Vehicle: public Object
24 {
25 public:
26         enum PlaceMode
27         {
28                 CENTER,
29                 FRONT_AXLE,
30                 FRONT_BUFFER,
31                 BACK_AXLE,
32                 BACK_BUFFER
33         };
34
35         struct Axle
36         {
37                 const VehicleType::Axle *type;
38                 Angle angle;
39
40                 Axle(const VehicleType::Axle &);
41         };
42
43         struct Bogie
44         {
45                 const VehicleType::Bogie *type;
46                 Angle direction;
47                 std::vector<Axle *> axles;
48
49                 Bogie(const VehicleType::Bogie &);
50         };
51
52         struct Rod
53         {
54                 const VehicleType::Rod *type;
55                 Vector position;
56                 Angle angle;
57
58                 Rod(const VehicleType::Rod &);
59         };
60
61 private:
62         const VehicleType &type;
63         Train *train;
64         Vehicle *next;
65         Vehicle *prev;
66         VehiclePlacement placement;
67         std::vector<Axle> axles;
68         std::vector<Axle *> fixed_axles;
69         std::vector<Bogie> bogies;
70         std::vector<Rod> rods;
71         unsigned front_sensor;
72         unsigned back_sensor;
73
74 public:
75         Vehicle(Layout &, const VehicleType &);
76         ~Vehicle();
77
78         virtual Vehicle *clone(Layout * = 0) const;
79         virtual const VehicleType &get_type() const { return type; }
80
81         void set_train(Train *);
82         Train *get_train() const { return train; }
83         void attach_back(Vehicle &);
84         void attach_front(Vehicle &);
85         void detach_back();
86         void detach_front();
87         Vehicle *get_next() const { return next; }
88         Vehicle *get_previous() const { return prev; }
89
90         // TODO implement these - should call place() with suitable parameters
91         virtual void set_position(const Vector &) { }
92         virtual void set_rotation(const Angle &) { }
93         virtual void set_tilt(const Angle &) { }
94         void place(const TrackOffsetIter &, VehiclePlacement::Anchor = VehiclePlacement::CENTER);
95         void unplace();
96         void advance(float);
97         const VehiclePlacement &get_placement() const { return placement; }
98         bool is_placed() const { return placement.is_placed(); }
99         const Axle &get_axle(unsigned) const;
100         const Axle &get_fixed_axle(unsigned) const;
101         const Bogie &get_bogie(unsigned) const;
102         const Axle &get_bogie_axle(unsigned, unsigned) const;
103         const Rod &get_rod(unsigned) const;
104 private:
105         void update_position(int);
106         void update_position_from(const Vehicle &);
107         void propagate_position();
108         void propagate_forward();
109         void propagate_backward();
110         void check_sensor(const TrackOffsetIter &, unsigned &, bool);
111         void turn_axles(float);
112         void update_rods();
113
114 public:
115         virtual unsigned get_n_link_slots() const;
116         virtual Vehicle *get_link(unsigned) const;
117         virtual int get_link_slot(const Object &) const;
118 };
119
120 } // namespace R2C2
121
122 #endif