]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/vehicle.h
Fix a segfault in removing vehicles when creating a new train
[r2c2.git] / source / libmarklin / vehicle.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBMARKLIN_VEHICLE_H_
9 #define LIBMARKLIN_VEHICLE_H_
10
11 #include "geometry.h"
12
13 namespace Marklin {
14
15 class Layout;
16 class Track;
17 class VehicleType;
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 private:
32         struct TrackPosition
33         {
34                 Track *track;
35                 unsigned ep;
36                 float offs;
37
38                 TrackPosition();
39                 TrackPosition(Track *, unsigned, float);
40                 void advance(float);
41                 TrackPoint get_point() const;
42         };
43
44         Layout &layout;
45         const VehicleType &type;
46         Vehicle *next;
47         Vehicle *prev;
48         TrackPosition track_pos;
49         Point position;
50         float direction;
51         std::vector<float> bogie_dirs;
52         unsigned front_sensor;
53         unsigned back_sensor;
54
55 public:
56         Vehicle(Layout &, const VehicleType &);
57         ~Vehicle();
58
59         const VehicleType &get_type() const { return type; }
60
61         void attach_back(Vehicle &);
62         void attach_front(Vehicle &);
63         void detach_back();
64         void detach_front();
65         Vehicle *get_next() const { return next; }
66         Vehicle *get_previous() const { return prev; }
67
68         void place(Track &, unsigned, float, PlaceMode = CENTER);
69         void unplace();
70         void advance(float);
71         Track *get_track() const { return track_pos.track; }
72         unsigned get_entry() const { return track_pos.ep; }
73         float get_offset() const { return track_pos.offs; }
74         const Point &get_position() const { return position; }
75         float get_direction() const { return direction; }
76         float get_bogie_direction(unsigned) const;
77 private:
78         void update_position();
79         void update_position_from(const Vehicle &);
80         void propagate_position();
81         void propagate_forward();
82         void propagate_backward();
83         void check_sensor(float, unsigned &);
84
85         void adjust_for_distance(TrackPosition &, TrackPosition &, float, float = 0.5) const;
86         TrackPoint get_point(const Point &, const Point &, float = 0.5) const;
87         TrackPoint get_point(const TrackPosition &, float, float = 0.5) const;
88 };
89
90 } // namespace Marklin
91
92 #endif