From d6c49075573f19bf61a84bd29d1ad191c5281219 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Wed, 28 Feb 2024 22:53:42 +0100 Subject: [PATCH] Lock and Unlock fixed: in case of an error the train is stopped and put on manual mode. --- server/locomotive.cc | 33 +++++++++++++++++++++++---------- server/railway.cc | 20 ++++++++++++-------- server/railway.h | 5 ++--- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/server/locomotive.cc b/server/locomotive.cc index 7f66a1d..44978f3 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -851,11 +851,11 @@ int Locomotives::Loco_SearchAndLock(Locomotive *loco) { } else strncpy (loco->blocknext, way.substr(pos+3,string::npos).c_str(), REFERENCENAME_LEN); strncpy (loco->auto_way, way.c_str(), WAYDATA_LEN); - if (server->railways.LockWay(way, loco->name) == 1) { + if (server->railways.LockWay(way, loco->name, 1, 0) == 1) { loco->auto_data = -1; return 1; } - server->railways.UnLockWay(way, loco->name); + server->railways.LockWay(way, loco->name, 0, RAILWAYS_LOCKF_KEEPSTART); } } } @@ -876,11 +876,20 @@ int Locomotives::Loco_SearchAndLock(Locomotive *loco) { } else strncpy (loco->blocknext, way.substr(pos+3,string::npos).c_str(), REFERENCENAME_LEN); strncpy (loco->auto_way, way.c_str(), WAYDATA_LEN); - if (server->railways.LockWay(way, loco->name) == 1) { + if (server->railways.LockWay(way, loco->name, 1, 0) == 1) { loco->auto_data = -1; return 1; } - server->railways.UnLockWay(way, loco->name); + if (server->railways.LockWay(way, loco->name, 0, RAILWAYS_LOCKF_KEEPSTART) == 0) { + SetSpeed(loco->name, 0); + SetModeMan(loco->name); + + server->LocomotiveSetMan(loco->name); + server->LocomotiveSetSpeed(loco->name, 0); + debug (DEBUG_ERROR, "*** ERROR *** %s:%d locomotive %s error occured. Could not undo the locking of the way.", + __FILE__, __LINE__, loco->name); + return 0; + } } } @@ -980,15 +989,19 @@ int Locomotives::Loco_OnRoute(Locomotive *loco) { // if (Loco_SearchAndLock(loco) == 1) { loco->auto_onroute = LOCO_OR_ENTERBLOCKNEXT; - debug (0, "Locomotives::Loco_OnRoute Found Way Prepare '%s'\n", loco->name); + debug (0, "Locomotives::Loco_OnRoute Found Way Prepare '%s'", loco->name); SetSpeed(loco->name, loco->flags & LOCO_F_REVERSE ? -loco->vslow : loco->vslow); timer_start(&loco->auto_timenext); } - else { + else if (loco->flags && LOCO_F_AUTO) { loco->auto_onroute = LOCO_OR_ENTERBLOCKSTOP; - debug (0, "Locomotives::Loco_OnRoute Slow Down '%s'\n", loco->name); + debug (0, "Locomotives::Loco_OnRoute Slow Down '%s'", loco->name); SetSpeed(loco->name, loco->flags & LOCO_F_REVERSE ? -loco->vslow : loco->vslow); } + else { + debug (0, "Locomotives::Loco_OnRoute '%s' some error occured.", loco->name); + SetSpeed(loco->name, 0); + } return 1; } @@ -1027,7 +1040,7 @@ int Locomotives::Loco_BlockEnterStop(Locomotive *loco) { SetSpeed(loco->name, 0); loco->auto_onroute = LOCO_OR_STOPWAIT; - server->railways.UnLockWay(loco->auto_wayold, loco->name); + server->railways.LockWay(loco->auto_wayold, loco->name, 0, 0); server->blocks.SetLockedby(loco->blockprev+2, loco->name, 0); loco->auto_wayold[0] = 0; timer_start(&loco->auto_timenext); @@ -1069,7 +1082,7 @@ int Locomotives::Loco_BlockEnterNext(Locomotive *loco) { debug (0, "Locomotives::Loco_BlockEnterNext could not prepare way in time '%s'\n", loco->name); SetSpeed(loco->name, 0); loco->auto_onroute = LOCO_OR_STOPWAIT; - server->railways.UnLockWay(loco->auto_wayold, loco->name); + server->railways.LockWay(loco->auto_wayold, loco->name, 0, 0); server->blocks.SetLockedby(loco->blockprev+2, loco->name, 0); loco->auto_wayold[0] = 0; timer_start(&loco->auto_timenext); @@ -1079,7 +1092,7 @@ int Locomotives::Loco_BlockEnterNext(Locomotive *loco) { else { debug (0, "* Locomotives::Loco_BlockEnterNext endblock reached %s", loco->name); - server->railways.UnLockWay(loco->auto_wayold, loco->name); + server->railways.LockWay(loco->auto_wayold, loco->name, 0, 0); server->blocks.SetLockedby(loco->blockprev+2, loco->name, 0); loco->auto_wayold[0] = 0; diff --git a/server/railway.cc b/server/railway.cc index 0ab783a..7f3b0b2 100644 --- a/server/railway.cc +++ b/server/railway.cc @@ -965,8 +965,10 @@ int Railways::FindRandomWay(string blockstart, string lockedfor, string *next) { * try to lock or unlock the way for the loco(lockedby) * this lock will only go from one to the next block. * Will also do the check for long trains and split blocks. + * + * flags: RAILWAYS_LOCKF_* see definition */ -int Railways::LockWay (string way, string lockedby, int lockonoff) { +int Railways::LockWay (string way, string lockedby, int lockonoff, int flags) { int res = 0; size_t pos1, pos2; size_t curwaypos; @@ -1115,8 +1117,10 @@ int Railways::LockWay (string way, string lockedby, int lockonoff) { return 0; } + // // lock start and endpoint... unlock only start if (r->type == RAILWAY_BLOCK) { + // endblock if ((strcmp(endblock.c_str()+2,r->name) == 0) || (endblock2.length() > 0 && (endblock2.compare(r->name) == 0))) { UnLock(); if (lockonoff) { @@ -1125,11 +1129,12 @@ int Railways::LockWay (string way, string lockedby, int lockonoff) { } Lock(); } + // startblock else if ((strcmp(startblock.c_str()+2, r->name) == 0) || (startblock2.length() > 0 && (startblock2.compare(r->name) == 0))) { UnLock(); - server->blocks.SetLockedby(r->name, lockedby, lockonoff); + if ((!flags & RAILWAYS_LOCKF_KEEPSTART) || lockonoff) server->blocks.SetLockedby(r->name, lockedby, lockonoff); if (lockonoff) strncpy (r->lockedby, lockedby.c_str(), REFERENCENAME_LEN); - else r->lockedby[0] = 0; + else if (!flags & RAILWAYS_LOCKF_KEEPSTART) r->lockedby[0] = 0; Lock(); } else { @@ -1210,13 +1215,12 @@ int Railways::LockWay (string way, string lockedby, int lockonoff) { else if (r->type == RAILWAY_CONNECTOR) { int found = 0; - int x, y, cnt; - printf ("%s:%d connector found Reference:'%s'\n", __FILE__, __LINE__, r->name); - x = -1; - y = -1; + int x = -1, y = -1, cnt = 0; + + debug (0, "LockWay connector found Reference:'%s'", r->name); while (found == 0 && _FindReference(&x, &y, r->name, (cnt++)) == 1) { if (pos.x != x || pos.y != y) { - printf ("%s:%d found %d,%d\n", __FILE__, __LINE__, x, y); + debug (0, "%s:%d found %d,%d", __FILE__, __LINE__, x, y); pos.oldx = pos.x; pos.oldy = pos.y; pos.oldenterfrom = pos.enterfrom; diff --git a/server/railway.h b/server/railway.h index 8df9b49..5a9ec2f 100644 --- a/server/railway.h +++ b/server/railway.h @@ -25,6 +25,7 @@ enum { #define RAILWAYS_MAX_WIDTH 500 #define RAILWAYS_MAX_HEIGHT 500 +#define RAILWAYS_LOCKF_KEEPSTART 0x0001 // keep start // direktion // @@ -132,9 +133,7 @@ class Railways { int FindWay(string blockstart, string blockend, string lockedfor, string *next); int FindWayCheckIfNoDoubleTurnouts (string *next); int FindRandomWay(string blockstart, string lockedfor, string *next); - int LockWay (string way, string lockedby, int lockonoff); - int LockWay (string way, string lockedby) { return LockWay(way, lockedby, 1); }; - int UnLockWay (string way, string lockedby) { return LockWay(way, lockedby, 0); }; + int LockWay (string way, string lockedby, int lockonoff, int flags); int SetLockedby (int x, int y, string name, int locked); struct s_findway_data NextPos(struct s_findway_data pos, int dirtype);