]> git.tdb.fi Git - r2c2.git/commitdiff
Compute deterministic IDs for blocks
authorMikko Rasa <tdb@tdb.fi>
Sat, 26 Dec 2009 09:29:23 +0000 (09:29 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 26 Dec 2009 09:29:23 +0000 (09:29 +0000)
Save train route and block information

source/engineer/engineer.cpp
source/engineer/trainpanel.cpp
source/libmarklin/block.cpp
source/libmarklin/block.h
source/libmarklin/trafficmanager.cpp
source/libmarklin/trafficmanager.h
source/libmarklin/train.cpp
source/libmarklin/train.h

index 7bd0f77d50c7721ca411745595e92fbe247a540b..308e5b2d4db0e80437a790ac7cbd1bf8ae38075c 100644 (file)
@@ -176,6 +176,10 @@ int Engineer::main()
                y -= tpanel->get_geometry().h;
        }
 
+       const list<Block *> &blocks = trfc_mgr->get_blocks();
+       for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+               reset_block_color(**i);
+
        wnd->show();
 
        Application::main();
@@ -265,6 +269,9 @@ void Engineer::tick()
        const list<Train *> &trains = trfc_mgr->get_trains();
        for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
+               if(!(*i)->is_placed())
+                       continue;
+
                GL::PushMatrix _push;
 
                const Point &tp = (*i)->get_position();
index 5a1e531dda3457099cddd9bed2ab1b00be546221..339f4eca669f7e9d4337dd25ab674c4937a260c7 100644 (file)
@@ -52,7 +52,8 @@ TrainPanel::TrainPanel(Engineer &e, const GLtk::Resources &r, Train &t):
        tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainPanel::forward_toggled));
        train.get_locomotive().signal_reverse_changed.connect(sigc::mem_fun(this, &TrainPanel::train_reverse_changed));
 
-       add(*(lbl_route=new GLtk::Label(res, "Free run")));
+       const Route *route = train.get_route();
+       add(*(lbl_route=new GLtk::Label(res, (route ? route->get_name() : "Free run"))));
        lbl_route->set_style("digital");
        lbl_route->set_geometry(GLtk::Geometry(10, 58, geom.w-20, 24));
        train.signal_route_changed.connect(sigc::mem_fun(this, &TrainPanel::train_route_changed));
index d4a6878a68b1b6ba475b85fd17014fe490b6cceb..a29469c4fd9b97e32751987e3c42ac6f903b078e 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
+#include <algorithm>
 #include "control.h"
 #include "block.h"
 #include "tracktype.h"
@@ -16,11 +17,9 @@ using namespace Msp;
 
 namespace Marklin {
 
-unsigned Block::next_id = 1;
-
 Block::Block(TrafficManager &tm, Track &start):
        trfc_mgr(tm),
-       id(next_id++),
+       id(0),
        sensor_id(start.get_sensor_id()),
        turnout_id(start.get_turnout_id()),
        train(0)
@@ -49,6 +48,11 @@ Block::Block(TrafficManager &tm, Track &start):
                        }
        }
 
+       if(sensor_id)
+               id = 0x1000|sensor_id;
+       else if(turnout_id)
+               id = 0x2000|turnout_id;
+
        for(unsigned i=0; i<endpoints.size(); ++i)
        {
                unsigned path = 1<<i;
@@ -119,6 +123,15 @@ void Block::check_link(Block &other)
                                j->link = this;
                        }
        }
+
+       if(!sensor_id && !turnout_id && endpoints.size()==2)
+       {
+               unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
+               unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
+               if(id2<id1)
+                       swap(id1, id2);
+               id = (id1<<16)|id2;
+       }
 }
 
 Block *Block::get_link(unsigned epi) const
index 7c0f8ac2aff063f1c4bee3558df8119a3cb4cb49..b72215ae03d1608092fd0c41943fdea7b7abeb54 100644 (file)
@@ -42,6 +42,7 @@ private:
 public:
        Block(TrafficManager &, Track &);
 
+       unsigned get_id() const { return id; }
        unsigned get_sensor_id() const { return sensor_id; }
        unsigned get_turnout_id() const { return turnout_id; }
        const std::set<Track *> &get_tracks() const { return tracks; }
@@ -55,8 +56,6 @@ public:
        void print_debug();
 private:
        void find_paths(Track &, unsigned, unsigned, std::set<Track *> &);
-
-       static unsigned next_id;
 };
 
 } // namespace Marklin
index b652d04cc5eeddb483ae9eb6792b9725f9240ca6..49dda42954d05ccdcb8b58f067899743111c4dbf 100644 (file)
@@ -58,6 +58,15 @@ TrafficManager::~TrafficManager()
                delete *i;
 }
 
+Block &TrafficManager::get_block(unsigned id) const
+{
+       for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+               if((*i)->get_id()==id)
+                       return **i;
+
+       throw KeyError("Unknown block", lexical_cast(id));
+}
+
 Block &TrafficManager::get_block_by_track(const Track &t) const
 {
        for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
index d4ea2b3c54010ffacecde8939cb61b2bce610a47..266d2ff65ccf037eafcd7fdce468fddbd8bb9ad3 100644 (file)
@@ -45,6 +45,7 @@ public:
        Control &get_control() const { return control; }
        Layout &get_layout() const { return layout; }
        const std::list<Block *> &get_blocks() const { return blocks; }
+       Block &get_block(unsigned) const;
        Block &get_block_by_track(const Track &) const;
        const std::list<Train *> &get_trains() const { return trains; }
        Train &get_train_by_locomotive(const Locomotive &) const;
index 5524a5d77c6a2740e24d4894a811dcac75441b44..e64ef00a1b78cee8c67edc65667836915d5c5f77 100644 (file)
@@ -11,6 +11,7 @@ Distributed under the GPL
 #include <msp/time/utils.h>
 #include "control.h"
 #include "except.h"
+#include "layout.h"
 #include "route.h"
 #include "tracktype.h"
 #include "trafficmanager.h"
@@ -186,6 +187,21 @@ void Train::save(list<DataFile::Statement> &st) const
        for(unsigned i=0; i<=14; ++i)
                if(real_speed[i].weight)
                        st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
+       if(route)
+               st.push_back((DataFile::Statement("route"), route->get_name()));
+
+       if(!cur_blocks.empty())
+       {
+               list<BlockRef> blocks = cur_blocks;
+               if(loco.get_reverse())
+                       reverse_blocks(blocks);
+
+               Block *prev = blocks.front().block->get_endpoints()[blocks.front().entry].link;
+               st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
+
+               for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+                       st.push_back((DataFile::Statement("block"), i->block->get_id()));
+       }
 }
 
 void Train::locomotive_reverse_changed(bool)
@@ -193,9 +209,7 @@ void Train::locomotive_reverse_changed(bool)
        for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
                i->block->reserve(0);
        rsv_blocks.clear();
-       cur_blocks.reverse();
-       for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
-               i->entry = i->block->traverse(i->entry);
+       reverse_blocks(cur_blocks);
        reserve_more();
 
        if(cur_track)
@@ -530,6 +544,13 @@ void Train::release_blocks(list<BlockRef> &blocks)
        blocks.clear();
 }
 
+void Train::reverse_blocks(list<BlockRef> &blocks) const
+{
+       blocks.reverse();
+       for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+               i->entry = i->block->traverse(i->entry);
+}
+
 
 Train::RealSpeed::RealSpeed():
        speed(0),
@@ -544,10 +565,36 @@ void Train::RealSpeed::add(float s, float w)
 
 
 Train::Loader::Loader(Train &t):
-       DataFile::BasicLoader<Train>(t)
+       DataFile::BasicLoader<Train>(t),
+       prev_block(0)
 {
+       add("block",       &Loader::block);
+       add("block_hint",  &Loader::block_hint);
        add("name",        &Train::name);
        add("real_speed",  &Loader::real_speed);
+       add("route",       &Loader::route);
+}
+
+void Train::Loader::block(unsigned id)
+{
+       Block &blk = obj.trfc_mgr.get_block(id);
+       int entry = -1;
+       if(prev_block)
+               entry = blk.get_endpoint_by_link(*prev_block);
+       if(entry<0)
+               entry = 0;
+
+       blk.reserve(&obj);
+       obj.cur_blocks.push_back(BlockRef(&blk, entry));
+       obj.status = "Stopped";
+       obj.set_position(blk.get_endpoints()[entry]);
+
+       prev_block = &blk;
+}
+
+void Train::Loader::block_hint(unsigned id)
+{
+       prev_block = &obj.trfc_mgr.get_block(id);
 }
 
 void Train::Loader::real_speed(unsigned i, float speed, float weight)
@@ -556,4 +603,9 @@ void Train::Loader::real_speed(unsigned i, float speed, float weight)
        obj.real_speed[i].weight = weight;
 }
 
+void Train::Loader::route(const string &n)
+{
+       obj.route = &obj.trfc_mgr.get_layout().get_route(n);
+}
+
 } // namespace Marklin
index 5f75a2a82bf837004352a7b537f841512a1f974d..5ef23537a4710b9f3e4e8832f9056d73c10e2813 100644 (file)
@@ -26,10 +26,16 @@ class Train: public sigc::trackable
 public:
        class Loader: public Msp::DataFile::BasicLoader<Train>
        {
+       private:
+               Block *prev_block;
+
        public:
                Loader(Train &);
        private:
+               void block(unsigned);
+               void block_hint(unsigned);
                void real_speed(unsigned, float, float);
+               void route(const std::string &);
        };
 
        sigc::signal<void, const std::string &> signal_name_changed;
@@ -91,6 +97,7 @@ public:
        const std::string &get_status() const { return status; }
        const Point &get_position() const { return pos; }
        void place(Block &, unsigned);
+       bool is_placed() const { return !cur_blocks.empty(); }
        bool free_block(Block &);
        int get_entry_to_block(Block &) const;
        void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &);
@@ -107,6 +114,7 @@ private:
        void set_status(const std::string &);
        void set_position(const Block::Endpoint &);
        void release_blocks(std::list<BlockRef> &);
+       void reverse_blocks(std::list<BlockRef> &) const;
 };
 
 } // namespace Marklin