Debian Codegraph @

DataModel/Track.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <algorithm>
22 #include <deque>
23 #include <map>
24 #include <random>
25 #include <string>
26
27 #include "DataModel/Feedback.h"
28 #include "DataModel/Loco.h"
29 #include "DataModel/Track.h"
30 #include "Manager.h"
31 #include "Utils/Integer.h"
32
33 using std::deque;
34 using std::map;
35 using std::string;
36 using std::to_string;
37 using std::vector;
38
39 namespace DataModel
40 {
41 std::string Track::Serialize() const
42 {
43 std::string str;
44 str = "objectType=Track";
45 str += ";selectrouteapproach=";
46 str += to_string(selectRouteApproach);
47 str += ";trackstate=";
48 str += to_string(trackState);
49 str += ";trackstatedelayed=";
50 str += to_string(trackStateDelayed);
51 str += ";locoorientation=";
52 str += to_string(locoBaseOrientation);
53 str += ";blocked=";
54 str += to_string(blocked);
55 str += ";locodelayed=";
56 str += to_string(locoBaseDelayed.GetObjectID());
57 str += ";locotypedelayed=";
58 str += to_string(locoBaseDelayed.GetObjectType());
59 str += ";allowlocoturn=";
60 str += to_string(allowLocoTurn);
61 str += ";releasewhenfree=";
62 str += to_string(releaseWhenFree);
63 str += ";showname=";
64 str += to_string(showName);
65 str += ";displayname=";
66 str += displayName;
67 str += ";";
68 str += LayoutItem::Serialize();
69 str += ";";
70 str += LockableItem::Serialize();
71 str += ";tracktype=";
72 str += to_string(trackType);
73 str += ";main=";
74 str += to_string(mainID);
75 return str;
76 }
77
78 bool Track::Deserialize(const std::string& serialized)
79 {
80 map<string, string> arguments;
81 ParseArguments(serialized, arguments);
82 string objectType = Utils::Utils::GetStringMapEntry(arguments, "objectType");
83 if (objectType.compare("Track") != 0)
84 {
85 return false;
86 }
87 LayoutItem::Deserialize(arguments);
88 LockableItem::Deserialize(arguments);
89
90 // FIXME: remove later: 2024-03-22 feedback vector has been replaced by relation
91 string feedbackStrings = Utils::Utils::GetStringMapEntry(arguments, "feedbacks");
92 deque<string> feedbackStringVector;
93 Utils::Utils::SplitString(feedbackStrings, ",", feedbackStringVector);
94 for (auto& feedbackString : feedbackStringVector)
95 {
96 const FeedbackID feedbackID = Utils::Integer::StringToInteger(feedbackString);
97 Feedback* feedback = manager->GetFeedback(feedbackID);
98 if (!feedback)
99 {
100 continue;
101 }
102 feedback->SetTrack(this);
103 feedbacks.push_back(new Relation(manager,
104 ObjectIdentifier(ObjectTypeTrack, GetID()),
105 ObjectIdentifier(ObjectTypeFeedback, feedbackID),
106 Relation::RelationTypeTrackFeedback));
107 }
108
109 selectRouteApproach = static_cast<SelectRouteApproach>(Utils::Utils::GetIntegerMapEntry(arguments, "selectrouteapproach", SelectRouteSystemDefault));
110 trackState = static_cast<DataModel::Feedback::FeedbackState>(Utils::Utils::GetBoolMapEntry(arguments, "trackstate", DataModel::Feedback::FeedbackStateFree));
111 trackStateDelayed = static_cast<DataModel::Feedback::FeedbackState>(Utils::Utils::GetBoolMapEntry(arguments, "trackstatedelayed", trackState));
112 locoBaseOrientation = static_cast<Orientation>(Utils::Utils::GetBoolMapEntry(arguments, "locoorientation", OrientationRight));
113 blocked = Utils::Utils::GetBoolMapEntry(arguments, "blocked", false);
114 const ObjectIdentifier& locoBaseDelayedIdentifier = GetLocoBase();
115 locoBaseDelayed.SetObjectID(Utils::Utils::GetIntegerMapEntry(arguments, "locodelayed", locoBaseDelayedIdentifier.GetObjectID()));
116 locoBaseDelayed.SetObjectType(static_cast<ObjectType>(Utils::Utils::GetIntegerMapEntry(arguments, "locotypedelayed", locoBaseDelayedIdentifier.GetObjectType())));
117 allowLocoTurn = Utils::Utils::GetBoolMapEntry(arguments, "allowlocoturn", true);
118 releaseWhenFree = Utils::Utils::GetBoolMapEntry(arguments, "releasewhenfree", false);
119 showName = Utils::Utils::GetBoolMapEntry(arguments, "showname", true);
120 displayName = Utils::Utils::GetStringMapEntry(arguments, "displayname", GetName());
121 SetWidth(Width1);
122 SetVisible(VisibleYes);
123 trackType = static_cast<TrackType>(Utils::Utils::GetIntegerMapEntry(arguments, "tracktype", TrackTypeStraight));
124 switch (trackType)
125 {
126 case TrackTypeTurn:
127 case TrackTypeTunnelEnd:
128 SetHeight(Height1);
129 break;
130
131 case TrackTypeCrossingLeft:
132 case TrackTypeCrossingRight:
133 case TrackTypeCrossingSymetric:
134 SetHeight(Height2);
135 break;
136
137 default:
138 break;
139 }
140 mainID = static_cast<TrackID>(Utils::Utils::GetIntegerMapEntry(arguments, "master", TrackNone)); // FIXME: 2025-02-28 can be removed later
141 mainID = static_cast<TrackID>(Utils::Utils::GetIntegerMapEntry(arguments, "main", mainID));
142 return true;
143 }
144
145 void Track::DeleteFeedbacks()
146 {
147 while (feedbacks.size() > 0)
148 {
149 Relation* feedbackRelation = feedbacks.back();
150 Feedback* feedback = manager->GetFeedback(feedbackRelation->ObjectID2());
151 if (feedback)
152 {
153 feedback->SetTrack();
154 }
155 feedbacks.pop_back();
156 delete feedbackRelation;
157 }
158 }
159
160 void Track::AssignFeedbacks(const std::vector<DataModel::Relation*>& newFeedbacks)
161 {
162 DeleteFeedbacks();
163 feedbacks = newFeedbacks;
164 for (auto feedbackRelation : feedbacks)
165 {
166 Feedback* feedback = manager->GetFeedback(feedbackRelation->ObjectID2());
167 if (feedback)
168 {
169 feedback->SetTrack(this);
170 }
171 }
172 }
173
174 void Track::DeleteSignals()
175 {
176 while (signals.size() > 0)
177 {
178 Relation* signalRelation = signals.back();
179 Signal* signal = manager->GetSignal(signalRelation->ObjectID2());
180 if (signal)
181 {
182 signal->SetTrack();
183 }
184 signals.pop_back();
185 delete signalRelation;
186 }
187 }
188
189 void Track::AssignSignals(const std::vector<DataModel::Relation*>& newSignals)
190 {
191 DeleteSignals();
192 signals = newSignals;
193 for (auto signalRelation : signals)
194 {
195 Signal* signal = manager->GetSignal(signalRelation->ObjectID2());
196 if (signal)
197 {
198 signal->SetTrack(this);
199 }
200 }
201 }
202
203 void Track::StopAllSignals(const ObjectIdentifier& locoBaseIdentifier)
204 {
205 for (auto signalRelation : signals)
206 {
207 Signal* signal = manager->GetSignal(signalRelation->ObjectID2());
208 if (!signal)
209 {
210 continue;
211 }
212 const ObjectIdentifier& locoBaseOfSignal = signal->GetLocoBase();
213 if (locoBaseOfSignal.IsSet() && (locoBaseIdentifier != locoBaseOfSignal))
214 {
215 continue;
216 }
217 manager->SignalState(ControlTypeInternal, signal, SignalStateStop, true);
218 }
219 }
220
221 bool Track::CanSetLocoBaseOrientation(const Orientation orientation, const ObjectIdentifier& locoBaseIdentifier)
222 {
223 if (locoBaseOrientation == orientation)
224 {
225 return true;
226 }
227
228 if (cluster)
229 {
230 return cluster->CanSetLocoBaseOrientation(static_cast<Orientation>(orientation ^ clusterInverted), locoBaseIdentifier);
231 }
232
233 const DataModel::LockableItem::LockState lockState = GetLockState();
234 return ((lockState == DataModel::LockableItem::LockStateFree)
235 || ((lockState == DataModel::LockableItem::LockStateReserved)
236 && (!locoBaseIdentifier.IsSet()
237 || (locoBaseIdentifier == GetLocoBase()))));
238 }
239
240 bool Track::SetLocoBaseOrientation(const Orientation orientation)
241 {
242 if (locoBaseOrientation == orientation)
243 {
244 return true;
245 }
246
247 if (cluster)
248 {
249 return cluster->SetLocoBaseOrientation(static_cast<Orientation>(orientation ^ clusterInverted), GetLocoBase());
250 }
251
252 locoBaseOrientation = orientation;
253 return true;
254 }
255
256 bool Track::Reserve(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
257 {
258 std::lock_guard<std::mutex> Guard(updateMutex);
259 if (this->locoBaseDelayed.IsSet() && (this->locoBaseDelayed != locoBaseIdentifier))
260 {
261 logger->Debug(Languages::TextTrackIsUsedByLoco, GetName(), manager->GetLocoBaseName(locoBaseDelayed));
262 return false;
263 }
264 if (blocked)
265 {
266 logger->Debug(Languages::TextTrackStatusIsBlocked, GetName());
267 return false;
268 }
269 if (trackState != DataModel::Feedback::FeedbackStateFree)
270 {
271 logger->Debug(Languages::TextIsNotFree, GetName());
272 return false;
273 }
274 return ReserveForce(logger, locoBaseIdentifier);
275 }
276
277 bool Track::ReserveForce(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
278 {
279 bool ret = LockableItem::Reserve(logger, locoBaseIdentifier);
280 if (!ret)
281 {
282 return false;
283 }
284
285 this->locoBaseDelayed = locoBaseIdentifier;
286 return true;
287 }
288
289 bool Track::Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
290 {
291 bool ret = LockableItem::Lock(logger, locoBaseIdentifier);
292 if (ret)
293 {
294 PublishState();
295 }
296
297 return ret;
298 }
299
300 bool Track::Release(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
301 {
302 StopAllSignals(locoBaseIdentifier);
303 {
304 std::lock_guard<std::mutex> Guard(updateMutex);
305 bool ret = LockableItem::Release(logger, locoBaseIdentifier);
306 if (!ret)
307 {
308 return false;
309 }
310
311 if (trackState != DataModel::Feedback::FeedbackStateFree)
312 {
313 return true;
314 }
315 this->locoBaseDelayed.Clear();
316 this->trackStateDelayed = DataModel::Feedback::FeedbackStateFree;
317 }
318 PublishState();
319 return true;
320 }
321
322 bool Track::ReleaseForce(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
323 {
324 StopAllSignals(locoBaseIdentifier);
325 bool ret;
326 {
327 std::lock_guard<std::mutex> Guard(updateMutex);
328 ret = ReleaseForceUnlocked(logger, locoBaseIdentifier);
329 }
330 PublishState();
331 return ret;
332 }
333
334 bool Track::ReleaseForceUnlocked(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
335 {
336 bool ret = LockableItem::Release(logger, locoBaseIdentifier);
337 this->trackState = DataModel::Feedback::FeedbackStateFree;
338 this->locoBaseDelayed.Clear();
339 this->trackStateDelayed = DataModel::Feedback::FeedbackStateFree;
340 return ret;
341 }
342
343 bool Track::SetFeedbackState(const FeedbackID feedbackID, const DataModel::Feedback::FeedbackState newTrackState)
344 {
345 {
346 std::lock_guard<std::mutex> Guard(updateMutex);
347 const DataModel::Feedback::FeedbackState oldTrackState = this->trackState;
348 const bool oldBlocked = blocked;
349 const bool ret = FeedbackStateInternal(feedbackID, newTrackState);
350 if (!ret)
351 {
352 return false;
353 }
354 if ((oldTrackState == newTrackState) && (oldBlocked == blocked))
355 {
356 return true;
357 }
358 }
359 PublishState();
360 return true;
361 }
362
363 bool Track::FeedbackStateInternal(const FeedbackID feedbackID, const DataModel::Feedback::FeedbackState newTrackState)
364 {
365 if (newTrackState == DataModel::Feedback::FeedbackStateOccupied)
366 {
367 LocoBase* locoBase = manager->GetLocoBase(GetLocoBaseDelayed());
368 if (!locoBase)
369 {
370 if ((!blocked) && manager->GetStopOnFeedbackInFreeTrack())
371 {
372 manager->Booster(ControlTypeInternal, BoosterStateStop);
373 blocked = true;
374 }
375 }
376 else
377 {
378 locoBase->LocationReached(feedbackID);
379 }
380
381 this->trackState = DataModel::Feedback::FeedbackStateOccupied;
382 this->trackStateDelayed = DataModel::Feedback::FeedbackStateOccupied;
383 return true;
384 }
385
386 for (auto feedbackRelation : feedbacks)
387 {
388 DataModel::Feedback* feedback = manager->GetFeedbackUnlocked(feedbackRelation->ObjectID2());
389 if (!feedback)
390 {
391 continue;
392 }
393 if (feedback->GetState() != DataModel::Feedback::FeedbackStateFree)
394 {
395 // if another feedback is still occupied
396 return false;
397 }
398 }
399 this->trackState = DataModel::Feedback::FeedbackStateFree;
400
401 const ObjectIdentifier& locoBaseIdentifier = GetLocoBase();
402 if (releaseWhenFree)
403 {
404 const LocoBase* locoBase = manager->GetLocoBase(locoBaseIdentifier);
405 if (locoBase && locoBase->CheckFreeingTrack(GetID()))
406 {
407 StopAllSignals(locoBaseIdentifier);
408 const bool ret = ReleaseForceUnlocked(locoBase->GetLogger(), locoBaseIdentifier);
409 PublishState();
410 return ret;
411 }
412 }
413
414 if (locoBaseIdentifier.IsSet())
415 {
416 return true;
417 }
418
419 this->trackStateDelayed = DataModel::Feedback::FeedbackStateFree;
420 this->locoBaseDelayed.Clear();
421 return true;
422 }
423
424 bool Track::AddRoute(Route* route)
425 {
426 std::lock_guard<std::mutex> Guard(updateMutex);
427 for (auto r : routes)
428 {
429 if (r == route)
430 {
431 return false;
432 }
433 }
434 routes.push_back(route);
435 return true;
436 }
437
438 bool Track::DeleteRoute(Route* route)
439 {
440 std::lock_guard<std::mutex> Guard(updateMutex);
441 size_t sizeBefore = routes.size();
442 routes.erase(std::remove(routes.begin(), routes.end(), route), routes.end());
443 size_t sizeAfter = routes.size();
444 return sizeBefore > sizeAfter;
445 }
446
447 bool Track::AddExtension(Track* track)
448 {
449 if (mainID != TrackNone)
450 {
451 return false;
452 }
453
454 std::lock_guard<std::mutex> Guard(updateMutex);
455 for (auto& extension : extensions)
456 {
457 if (extension == track)
458 {
459 return false;
460 }
461 }
462 extensions.push_back(track);
463 return true;
464 }
465
466 bool Track::DeleteExtension(const Track* track)
467 {
468 std::lock_guard<std::mutex> Guard(updateMutex);
469 size_t sizeBefore = extensions.size();
470 extensions.erase(std::remove(extensions.begin(), extensions.end(), track), extensions.end());
471 size_t sizeAfter = extensions.size();
472 return sizeBefore > sizeAfter;
473 }
474
475 void Track::UpdateMain()
476 {
477 mainTrack = (mainID == TrackNone ? nullptr : manager->GetTrack(mainID));
478 }
479
480 SelectRouteApproach Track::GetSelectRouteApproachCalculated() const
481 {
482 if (selectRouteApproach == SelectRouteSystemDefault)
483 {
484 return manager->GetSelectRouteApproach();
485 }
486 return selectRouteApproach;
487 }
488
489 bool Track::GetValidRoutes(Logger::Logger* logger,
490 const LocoBase* loco,
491 const bool allowLocoTurn,
492 std::vector<Route*>& validRoutes) const
493 {
494 {
495 std::lock_guard<std::mutex> Guard(updateMutex);
496 for (auto route : routes)
497 {
498 if (route->FromTrackOrientation(logger, GetID(), locoBaseOrientation, loco, allowLocoTurn))
499 {
500 validRoutes.push_back(route);
501 }
502 }
503 }
504 OrderValidRoutes(validRoutes);
505 return true;
506 }
507
508 void Track::OrderValidRoutes(vector<Route*>& validRoutes) const
509 {
510 switch (GetSelectRouteApproachCalculated())
511 {
512
513 case SelectRouteRandom:
514 {
515 static std::random_device rd;
516 static std::mt19937 g(rd());
517 std::shuffle(validRoutes.begin(), validRoutes.end(), g);
518 return;
519 }
520
521 case SelectRouteMinTrackLength:
522 std::sort(validRoutes.begin(), validRoutes.end(), Route::CompareShortest);
523 return;
524
525 case SelectRouteLongestUnused:
526 std::sort(validRoutes.begin(), validRoutes.end(), Route::CompareLastUsed);
527 return;
528
529 case SelectRouteDoNotCare:
530 default:
531 // do nothing
532 return;
533 }
534 }
535
536 void Track::PublishState() const
537 {
538 manager->TrackPublishState(this);
539 }
540 } // namespace DataModel