From c6444adc01b8f43beb177be0b5eead4af76f84ca Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 12 Jan 2010 08:47:07 +0000 Subject: [PATCH] Fix some cases of flipLeader - still borked though --- maps/points/points.xml | 266 +++++++++++++++++++++++++++++++++++++++-- src/Train.cpp | 103 ++++++++++------ 2 files changed, 323 insertions(+), 46 deletions(-) diff --git a/maps/points/points.xml b/maps/points/points.xml index 6f71ea8..5320361 100644 --- a/maps/points/points.xml +++ b/maps/points/points.xml @@ -119,22 +119,174 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -143,6 +295,18 @@ + + + + + + + + + + + + @@ -151,6 +315,18 @@ + + + + + + + + + + + + @@ -159,6 +335,22 @@ + + + + + + + + + + + + + + + + @@ -167,14 +359,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -187,6 +407,10 @@ + + + + @@ -199,6 +423,10 @@ + + + + @@ -207,6 +435,14 @@ + + + + + + + + @@ -223,18 +459,34 @@ + + + + + + + + + + + + + + + + @@ -243,6 +495,10 @@ + + + + @@ -343,16 +599,8 @@ - - - - - - - - - + diff --git a/src/Train.cpp b/src/Train.cpp index 1ae86bb..196fbbd 100644 --- a/src/Train.cpp +++ b/src/Train.cpp @@ -90,6 +90,8 @@ private: void makeFollow(track::Choice aChoice); void flipLeader(); void dumpFollowQueue() const; + void eachPart(function callback); + void movePart(Part& part, double distance); static track::Connection reverseToken(const track::TravelToken& token); static void transformToPart(const Part& p); @@ -171,12 +173,10 @@ void Train::dumpFollowQueue() const { ostringstream ss; - const Part& leader = leading(); - for (list::const_iterator it = parts.begin(); it != parts.end(); ++it) { - if (*it == leader) + if ((*it).isLeading) ss << ">"; if ((*it).followQueue.empty()) @@ -217,46 +217,68 @@ void Train::flipLeader() parts.back().isLeading = false; engine().isLeading = true; } -} -// Move the train along the line a bit -void Train::move(double aDistance) -{ for (list::iterator it = parts.begin(); it != parts.end(); ++it) { + while (!(*it).followQueue.empty()) + (*it).followQueue.pop(); + } +} - // Never move in units greater than 1.0 - double d = abs(aDistance); - double sign = (aDistance >= 0.0 ? 1.0 : -1.0) * (*it).movementSign; - const double step = 0.25; +// Iterate through the parts in order from the front of the +// train to the back +void Train::eachPart(function callback) +{ + if (parts.front().isLeading) { + for (list::iterator it = parts.begin(); + it != parts.end(); ++it) + callback(*it); + } + else { + for (list::reverse_iterator it = parts.rbegin(); + it != parts.rend(); ++it) + callback(*it); + } +} - debug() << "move d=" << aDistance << " s=" << sign - << " ms=" << (*it).movementSign; +void Train::movePart(Part& part, double distance) +{ + // Never move in units greater than 1.0 + double d = abs(distance); + double sign = (distance >= 0.0 ? 1.0 : -1.0) * part.movementSign; + const double step = 0.25; + + //debug() << "move d=" << distance << " s=" << sign + // << " ms=" << part.movementSign; + + do { + part.segmentDelta += min(step, d) * sign; - do { - (*it).segmentDelta += min(step, d) * sign; - - const double segmentLength = - (*it).segment->segmentLength((*it).travelToken); - if ((*it).segmentDelta >= segmentLength) { - // Moved onto a new piece of track - const double over = (*it).segmentDelta - segmentLength; - enterSegment(*it, (*it).segment->nextPosition((*it).travelToken)); - (*it).segmentDelta = over; - } - else if ((*it).segmentDelta < 0.0) { - track::Connection prev = reverseToken((*it).travelToken); - enterSegment(*it, prev); - (*it).segmentDelta *= -1.0; - (*it).movementSign *= -1.0; - - if ((*it).isLeading) - flipLeader(); - } - - d -= step; - } while (d > 0.0); - } + const double segmentLength = + part.segment->segmentLength(part.travelToken); + if (part.segmentDelta >= segmentLength) { + // Moved onto a new piece of track + const double over = part.segmentDelta - segmentLength; + enterSegment(part, part.segment->nextPosition(part.travelToken)); + part.segmentDelta = over; + } + else if (part.segmentDelta < 0.0) { + track::Connection prev = reverseToken(part.travelToken); + enterSegment(part, prev); + part.segmentDelta *= -1.0; + part.movementSign *= -1.0; + } + + d -= step; + } while (d > 0.0); +} + +// Move the train along the line a bit +void Train::move(double aDistance) +{ + using namespace placeholders; + + eachPart(bind(&Train::movePart, this, _1, aDistance)); } void Train::updateSmokePosition(int aDelta) @@ -293,10 +315,17 @@ void Train::updateSmokePosition(int aDelta) void Train::update(int aDelta) { + int oldSpeedSign = engine().vehicle->speed() >= 0.0 ? 1 : 0; + for (list::iterator it = parts.begin(); it != parts.end(); ++it) (*it).vehicle->update(aDelta); + int newSpeedSign = engine().vehicle->speed() >= 0.0 ? 1 : 0; + + if (oldSpeedSign != newSpeedSign) + flipLeader(); + updateSmokePosition(aDelta); // How many metres does a tile correspond to? -- 2.39.2