]> git.tdb.fi Git - r2c2.git/blob - libr2c2/trainrouter.cpp
Don't crash if a train has no router
[r2c2.git] / libr2c2 / trainrouter.cpp
1 #include "layout.h"
2 #include "route.h"
3 #include "trackiter.h"
4 #include "train.h"
5 #include "trainrouteplanner.h"
6 #include "trainrouter.h"
7 #include "zone.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 TrainRouter::TrainRouter(Train &t):
15         TrainAI(t),
16         priority(0),
17         arriving(0),
18         dest_zone(0),
19         dest_block(0),
20         update_pending(false)
21 {
22         train.get_layout().signal_block_reserved.connect(sigc::mem_fun(this, &TrainRouter::block_reserved));
23         train.signal_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_advanced));
24 }
25
26 void TrainRouter::set_priority(int p)
27 {
28         priority = p;
29 }
30
31 bool TrainRouter::set_route(const Route *r)
32 {
33         BlockIter fncb = train.get_first_noncritical_block();
34
35         Route *lead = 0;
36         if(r && train.is_placed())
37         {
38                 const BlockAllocator &allocator = train.get_block_allocator();
39                 TrackIter first = allocator.first().track_iter();
40                 TrackIter next = fncb.track_iter();
41                 if(!r->has_track(*next))
42                 {
43                         lead = Route::find(next, *r);
44                         if(!lead)
45                                 return false;
46                         create_lead_route(lead, lead);
47                 }
48                 else if(!r->has_track(*first))
49                         lead = create_lead_route(0, r);
50         }
51
52         routes.clear();
53         if(lead)
54                 routes.push_back(lead);
55         if(r)
56                 routes.push_back(r);
57         train.stop_at(0);
58         arriving = 0;
59
60         /* TODO destination should also be cleared when manually setting a different
61         route, but not when the planner calls this. */
62         if(!r)
63         {
64                 dest_zone = 0;
65                 dest_block = 0;
66         }
67
68         train.refresh_blocks_from(*fncb);
69
70         const Route *route = get_route();
71         signal_route_changed.emit(route);
72         signal_event.emit(Message("route-changed", route));
73
74         return true;
75 }
76
77 void TrainRouter::add_wait(Block &block, Train *tr)
78 {
79         Wait wait;
80         wait.block = █
81         wait.train = tr;
82         waits.push_back(wait);
83 }
84
85 const Route *TrainRouter::get_route() const
86 {
87         if(routes.empty())
88                 return 0;
89         return routes.front();
90 }
91
92 void TrainRouter::set_destination(const Zone &zone)
93 {
94         dest_zone = &zone;
95         dest_block = 0;
96         update_pending = true;
97 }
98
99 void TrainRouter::set_destination(const Block &block)
100 {
101         dest_zone = 0;
102         dest_block = █
103         update_pending = true;
104 }
105
106 bool TrainRouter::has_destination() const
107 {
108         return dest_zone || dest_block;
109 }
110
111 bool TrainRouter::is_destination(Track &track) const
112 {
113         if(dest_zone)
114                 return dest_zone->has_track(track);
115         else if(dest_block)
116                 return dest_block->has_track(track);
117         else
118                 return false;
119 }
120
121 void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
122 {
123         delay = d;
124         update_pending = true;
125 }
126
127 void TrainRouter::message(const Message &msg)
128 {
129         if(msg.type=="set-route")
130         {
131                 if(msg.value.check_type<Route *>())
132                         set_route(msg.value.value<Route *>());
133                 else
134                         set_route(msg.value.value<const Route *>());
135         }
136         else if(msg.type=="clear-route")
137                 set_route(0);
138         else if(msg.type=="set-destination-block")
139         {
140                 if(msg.value.check_type<Block *>())
141                         set_destination(*msg.value.value<Block *>());
142                 else
143                         set_destination(*msg.value.value<const Block *>());
144         }
145         else if(msg.type=="set-destination-zone")
146         {
147                 if(msg.value.check_type<Zone *>())
148                         set_destination(*msg.value.value<Zone *>());
149                 else
150                         set_destination(*msg.value.value<const Zone *>());
151         }
152         else if(msg.type=="set-departure-delay")
153                 set_departure_delay(msg.value.value<Time::TimeDelta>());
154 }
155
156 void TrainRouter::tick(const Time::TimeDelta &dt)
157 {
158         if(delay)
159         {
160                 delay -= dt;
161                 if(delay<=Time::zero)
162                         delay = Time::zero;
163         }
164
165         if(update_pending)
166                 create_plans(train.get_layout());
167
168         if(arriving==1 && !train.get_speed())
169         {
170                 signal_arrived.emit();
171                 signal_event.emit(Message("arrived"));
172                 arriving = 2;
173         }
174         else if(arriving==2 && !train.get_block_allocator().is_active())
175                 set_route(0);
176 }
177
178 void TrainRouter::save(list<DataFile::Statement> &st) const
179 {
180         st.push_back((DataFile::Statement("priority"), priority));
181
182         if(!routes.empty())
183         {
184                 RouteList::const_iterator i = routes.begin();
185                 for(; (i!=routes.end() && (*i)->is_temporary()); ++i) ;
186                 if(i!=routes.end())
187                         st.push_back((DataFile::Statement("route"), (*i)->get_name()));
188         }
189 }
190
191 void TrainRouter::block_reserved(Block &block, Train *t)
192 {
193         if(t!=&train)
194         {
195                 if(!waits.empty() && waits.front().block==&block)
196                 {
197                         train.stop_at(0);
198                         waits.pop_front();
199                 }
200                 return;
201         }
202
203         BlockIter b_iter = t->get_block_allocator().iter_for(block);
204
205         RouteList::iterator route = routes.begin();
206         if(advance_route(route, block))
207         {
208                 // Check if the block is a turnout and set it to proper path
209                 if(unsigned taddr = block.get_turnout_address())
210                 {
211                         int path = (*route)->get_turnout(taddr);
212                         if(path>=0)
213                                 b_iter.track_iter()->set_active_path(path);
214                 }
215
216                 // Check if the next block is still part of the designated route
217                 BlockIter b_iter_next = b_iter.next(*route);
218
219                 RouteList::iterator next_route = route;
220                 if(!advance_route(next_route, *b_iter_next))
221                 {
222                         train.stop_at(&block);
223                         return;
224                 }
225
226                 if(!waits.empty() && waits.front().block==b_iter_next.block())
227                         train.stop_at(&block);
228         }
229 }
230
231 void TrainRouter::train_advanced(Block &block)
232 {
233         BlockIter b_iter = train.get_block_allocator().iter_for(block);
234
235         // Check if we've reached the next route
236         if(routes.size()>1)
237         {
238                 const Route &route = **++routes.begin();
239                 if(route.has_track(*b_iter.endpoint().track))
240                 {
241                         routes.pop_front();
242                         // XXX Exceptions?
243                         signal_event.emit(Message("route-changed", get_route()));
244                 }
245         }
246
247         if(!routes.empty())
248         {
249                 b_iter = b_iter.next();
250                 if(b_iter && !is_on_route(*b_iter))
251                         arriving = 1;
252         }
253 }
254
255 const Route *TrainRouter::get_route_for_block(const Block &block) const
256 {
257         const set<Track *> &tracks = block.get_tracks();
258         for(RouteList::const_iterator i=routes.begin(); i!=routes.end(); ++i)
259                 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
260                         if((*i)->has_track(**j))
261                                 return *i;
262
263         return 0;
264 }
265
266 Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
267 {
268         if(!lead)
269         {
270                 lead = new Route(train.get_layout());
271                 lead->set_name("Lead");
272                 lead->set_temporary(true);
273         }
274
275         set<Track *> tracks;
276         for(BlockIter i=train.get_block_allocator().first(); (i && i->get_train()==&train); i=i.next())
277         {
278                 const set<Track *> &btracks = i->get_tracks();
279                 for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
280                         if(!target || !target->has_track(**j))
281                                 tracks.insert(*j);
282         }
283
284         lead->add_tracks(tracks);
285
286         return lead;
287 }
288
289 bool TrainRouter::advance_route(RouteList::iterator &iter, const Block &block)
290 {
291         const set<Track *> &tracks = block.get_tracks();
292         for(; iter!=routes.end(); ++iter)
293                 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
294                         if((*iter)->has_track(**j))
295                                 return true;
296
297         return false;
298 }
299
300 bool TrainRouter::is_on_route(const Block &block)
301 {
302         RouteList::iterator iter = routes.begin();
303         return advance_route(iter, block);
304 }
305
306 void TrainRouter::create_plans(Layout &layout)
307 {
308         TrainRoutePlanner planner(layout);
309         planner.plan();
310
311         const map<unsigned, Train *> &trains = layout.get_trains();
312         for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
313                 if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
314                         router->update_pending = false;
315 }
316
317
318 TrainRouter::Wait::Wait():
319         block(0),
320         train(0)
321 { }
322
323
324 TrainRouter::Loader::Loader(TrainRouter &r):
325         DataFile::ObjectLoader<TrainRouter>(r)
326 {
327         add("priority", &TrainRouter::priority);
328         add("route",    &Loader::route);
329 }
330
331 void TrainRouter::Loader::route(const string &n)
332 {
333         obj.set_route(&obj.train.get_layout().get_route(n));
334 }
335
336 } // namespace R2C2