X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftrain.cpp;h=eadc37de3d460a136ed8e94f0ebd108e9d258a53;hb=378f04522aa762729c0d5651fb60ad7e4d8ba9f3;hp=a6fd46d1e40519381c9723058b7252736a5f3b98;hpb=ed7a7139f4858fe5d0c756798538d25893bf58b2;p=r2c2.git diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index a6fd46d..eadc37d 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -13,20 +13,20 @@ Distributed under the GPL #include "catalogue.h" #include "driver.h" #include "layout.h" -#include "locotype.h" #include "route.h" #include "simplephysics.h" #include "timetable.h" #include "tracktype.h" #include "train.h" #include "vehicle.h" +#include "vehicletype.h" using namespace std; using namespace Msp; namespace Marklin { -Train::Train(Layout &l, const LocoType &t, unsigned a): +Train::Train(Layout &l, const VehicleType &t, unsigned a): layout(l), loco_type(t), address(a), @@ -49,6 +49,9 @@ Train::Train(Layout &l, const LocoType &t, unsigned a): accurate_position(false), overshoot_dist(false) { + if(!loco_type.is_locomotive()) + throw InvalidParameterValue("Initial vehicle must be a locomotive"); + vehicles.push_back(new Vehicle(layout, loco_type)); layout.add_train(*this); @@ -87,6 +90,30 @@ void Train::set_priority(int p) priority = p; } +void Train::add_vehicle(const VehicleType &vt) +{ + Vehicle *veh = new Vehicle(layout, vt); + vehicles.back()->attach_back(*veh); + vehicles.push_back(veh); +} + +void Train::remove_vehicle(unsigned i) +{ + if(i>=vehicles.size()) + throw InvalidParameterValue("Vehicle index out of range"); + if(i==0) + throw InvalidParameterValue("Can't remove the locomotive"); + delete vehicles[i]; + vehicles.erase(vehicles.begin()+i); + if(iattach_back(*vehicles[i]); +} + +unsigned Train::get_n_vehicles() const +{ + return vehicles.size(); +} + Vehicle &Train::get_vehicle(unsigned i) { if(i>=vehicles.size())