-/* $Id$
-
-This file is part of R²C²
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
#ifndef LIBR2C2_VEHICLE_H_
#define LIBR2C2_VEHICLE_H_
#include "geometry.h"
+#include "object.h"
+#include "trackoffsetiter.h"
+#include "vehicleplacement.h"
#include "vehicletype.h"
namespace R2C2 {
class Layout;
-class Track;
+class Train;
-class Vehicle
+class attachment_error: public std::logic_error
{
public:
- enum PlaceMode
- {
- CENTER,
- FRONT_AXLE,
- FRONT_BUFFER,
- BACK_AXLE,
- BACK_BUFFER
- };
+ attachment_error(const std::string &w): std::logic_error(w) { }
+ virtual ~attachment_error() throw() { }
+};
+
+class Vehicle: public Object
+{
+public:
struct Axle
{
const VehicleType::Axle *type;
- float angle;
+ Angle angle;
Axle(const VehicleType::Axle &);
};
struct Bogie
{
const VehicleType::Bogie *type;
- float direction;
- std::vector<Axle> axles;
+ Angle direction;
+ std::vector<Axle *> axles;
Bogie(const VehicleType::Bogie &);
};
{
const VehicleType::Rod *type;
Vector position;
- float angle;
+ Angle angle;
Rod(const VehicleType::Rod &);
};
private:
- struct TrackPosition
- {
- Track *track;
- unsigned ep;
- float offs;
-
- TrackPosition();
- TrackPosition(Track *, unsigned, float);
- void advance(float);
- TrackPoint get_point() const;
- };
-
- Layout &layout;
const VehicleType &type;
+ Train *train;
Vehicle *next;
Vehicle *prev;
- TrackPosition track_pos;
- Vector position;
- float direction;
+ VehiclePlacement placement;
std::vector<Axle> axles;
+ std::vector<Axle *> fixed_axles;
std::vector<Bogie> bogies;
std::vector<Rod> rods;
unsigned front_sensor;
Vehicle(Layout &, const VehicleType &);
~Vehicle();
- const VehicleType &get_type() const { return type; }
+ virtual Vehicle *clone(Layout * = 0) const;
+ virtual const VehicleType &get_type() const { return type; }
+ void set_train(Train *);
+ Train *get_train() const { return train; }
void attach_back(Vehicle &);
void attach_front(Vehicle &);
void detach_back();
Vehicle *get_next() const { return next; }
Vehicle *get_previous() const { return prev; }
- void place(Track &, unsigned, float, PlaceMode = CENTER);
+ // TODO implement these - should call place() with suitable parameters
+ virtual void set_position(const Vector &) { }
+ virtual void set_rotation(const Angle &) { }
+ virtual void set_tilt(const Angle &) { }
+ void place(const TrackOffsetIter &, VehiclePlacement::Anchor = VehiclePlacement::CENTER);
void unplace();
void advance(float);
- Track *get_track() const { return track_pos.track; }
- unsigned get_entry() const { return track_pos.ep; }
- float get_offset() const { return track_pos.offs; }
- const Vector &get_position() const { return position; }
- float get_direction() const { return direction; }
+ const VehiclePlacement &get_placement() const { return placement; }
+ bool is_placed() const { return placement.is_placed(); }
const Axle &get_axle(unsigned) const;
+ const Axle &get_fixed_axle(unsigned) const;
const Bogie &get_bogie(unsigned) const;
const Axle &get_bogie_axle(unsigned, unsigned) const;
const Rod &get_rod(unsigned) const;
private:
- void update_position();
+ void update_position(int);
void update_position_from(const Vehicle &);
void propagate_position();
void propagate_forward();
void propagate_backward();
- void check_sensor(float, unsigned &);
+ void check_sensor(const TrackOffsetIter &, unsigned &, bool);
void turn_axles(float);
void update_rods();
+ float resolve_rod_constraint(Rod &, const VehicleType::RodConstraint &);
+
+public:
+ virtual unsigned get_n_link_slots() const;
+ virtual Vehicle *get_link(unsigned) const;
+ virtual int get_link_slot(const Object &) const;
- void adjust_for_distance(TrackPosition &, TrackPosition &, float, float = 0.5) const;
- TrackPoint get_point(const Vector &, const Vector &, float = 0.5) const;
- TrackPoint get_point(const TrackPosition &, float, float = 0.5) const;
+ virtual bool collide_ray(const Ray &, float * = 0) const;
};
} // namespace R2C2