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