]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.h
Improve the interface and algorithms of BlockAllocator
[r2c2.git] / source / libr2c2 / train.h
index fa638f8d5034390b25388a1fa06b7e125538ef80..03b8b66d3e1e929869431c0ec472853d823dbdaf 100644 (file)
@@ -6,7 +6,7 @@
 #include <msp/datafile/objectloader.h>
 #include <msp/time/timestamp.h>
 #include "block.h"
-#include "blockiter.h"
+#include "blockallocator.h"
 #include "controller.h"
 #include "trainai.h"
 
@@ -30,8 +30,7 @@ public:
                Loader(Train &);
        private:
                virtual void finish();
-               void block(unsigned);
-               void block_hint(unsigned);
+               void blocks();
                void name(const std::string &);
                void quantized_speed();
                void router();
@@ -46,8 +45,6 @@ public:
        sigc::signal<void, Block &> signal_advanced;
 
 private:
-       typedef std::list<BlockIter> BlockList;
-
        Layout &layout;
        const VehicleType &loco_type;
        unsigned address;
@@ -55,11 +52,7 @@ private:
        std::string name;
        const Train *preceding_train;
        std::vector<Vehicle *> vehicles;
-       BlockList blocks;
-       BlockList::iterator cur_blocks_end;
-       Block *pending_block;
-       Block *stop_at_block;
-       bool reserving;
+       BlockAllocator allocator;
        bool advancing;
        Controller *controller;
        std::list<TrainAI *> ais;
@@ -70,8 +63,8 @@ private:
        Msp::Time::TimeStamp stop_timeout;
        unsigned functions;
 
+       BlockIter last_entry_block;
        Msp::Time::TimeStamp last_entry_time;
-       float travel_dist;
        bool pure_speed;
        SpeedQuantizer *speed_quantizer;
        bool accurate_position;
@@ -109,18 +102,25 @@ public:
 
        void add_ai(TrainAI &);
        void remove_ai(TrainAI &);
-       TrainAI *get_tagged_ai(const std::string &) const;
        void ai_message(const TrainAI::Message &);
 
-       void place(Block &, unsigned);
+       template<typename T>
+       T *get_ai_of_type() const
+       {
+               for(std::list<TrainAI *>::const_iterator i=ais.begin(); i!=ais.end(); ++i)
+                       if(T *ai = dynamic_cast<T *>(*i))
+                               return ai;
+               return 0;
+       }
+
+       void place(const BlockIter &);
        void unplace();
-       bool is_placed() const { return !blocks.empty(); }
+       bool is_placed() const { return !allocator.empty(); }
        void stop_at(Block *);
        bool free_block(Block &);
        void free_noncritical_blocks();
-       const BlockIter &get_head_block() const;
-       const BlockIter &get_tail_block() const;
-       int get_entry_to_block(const Block &) const;
+       void reserve_more();
+       const BlockAllocator &get_block_allocator() const { return allocator; }
        float get_reserved_distance() const;
 
        void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
@@ -131,16 +131,9 @@ private:
        void loco_speed_event(unsigned, unsigned, bool);
        void loco_func_event(unsigned, unsigned, bool);
        void block_state_changed(Block &, Block::State);
-       void turnout_path_changed(Track &);
        void halt_event(bool);
        void block_reserved(const Block &, const Train *);
-public:
-       void reserve_more();
-private:
        float get_reserved_distance_until(const Block *, bool) const;
-       void release_blocks();
-       void release_blocks(BlockList::iterator, BlockList::iterator);
-       void reverse_blocks(BlockList &) const;
 };
 
 } // namespace R2C2