|
|
|
|
@ -502,6 +502,7 @@ int Railways::FindWay(string blockstart, string blockend, string lockedfor, stri
|
|
|
|
|
string lockedby;
|
|
|
|
|
int i, x ,y;
|
|
|
|
|
int found = 0;
|
|
|
|
|
int locoflags = server->locomotives.GetFlags(lockedfor);
|
|
|
|
|
|
|
|
|
|
// check blocklen
|
|
|
|
|
if (blockstart.length() <= 3 || blockend.length() <= 3) return 0;
|
|
|
|
|
@ -678,7 +679,11 @@ int Railways::FindWay(string blockstart, string blockend, string lockedfor, stri
|
|
|
|
|
// if block is off ignore it completly
|
|
|
|
|
else if (rpos->type == RAILWAY_BLOCK) {
|
|
|
|
|
printf ("%s:%d block found\n", __FILE__, __LINE__);
|
|
|
|
|
if (server->blocks.IsOff(rpos->name)) {
|
|
|
|
|
int blflags = server->blocks.GetFlags(rpos->name);
|
|
|
|
|
|
|
|
|
|
if ((server->blocks.IsOff(rpos->name) ||
|
|
|
|
|
((blflags & BLOCK_F_ENDSTATION) && !(locoflags & LOCO_F_CANREVERSE)) ||
|
|
|
|
|
((blflags & BLOCK_F_SHORT) && !(locoflags & LOCO_F_SHORTTRAIN)))) {
|
|
|
|
|
printf ("%s:%d block is off\n", __FILE__, __LINE__);
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
@ -743,6 +748,242 @@ int Railways::FindWay(string blockstart, string blockend, string lockedfor, stri
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Railways::FindRandomWay(string blockstart, string lockedfor, string *next) {
|
|
|
|
|
// direction 0 ... RIGHT and DOWN
|
|
|
|
|
// direction 1 ... LEFT and UP
|
|
|
|
|
struct s_findway_data fd_pos;
|
|
|
|
|
struct s_findway_data fd_tmp;
|
|
|
|
|
struct s_findway_data fd_start;
|
|
|
|
|
struct s_findway_data fd_end;
|
|
|
|
|
Railway *rpos, *rnext;
|
|
|
|
|
std::list<struct s_findway_data> fd_list;
|
|
|
|
|
struct s_findway_map *fd_data = NULL;
|
|
|
|
|
string lockedby;
|
|
|
|
|
int i, x ,y;
|
|
|
|
|
int found = 0;
|
|
|
|
|
int locoflags = server->locomotives.GetFlags(lockedfor);
|
|
|
|
|
|
|
|
|
|
debug (0, "Railway::FindRandomWay");
|
|
|
|
|
|
|
|
|
|
// check blocklen
|
|
|
|
|
if (blockstart.length() <= 3) return 0;
|
|
|
|
|
|
|
|
|
|
// allocate and clear memory for checked data
|
|
|
|
|
fd_data = (struct s_findway_map *) malloc (sizeof(struct s_findway_map) * width * height);
|
|
|
|
|
if (fd_data == NULL) {
|
|
|
|
|
printf ("%s:%d (%s) FATAL ERROR: could not allocate enough memory\n", __FILE__, __LINE__, __FUNCTION__);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
memset (fd_data, 0x0, sizeof(struct s_findway_map) * width*height);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// find start and endpoint mark on fd_data, also startpoint to fd_list
|
|
|
|
|
fd_end.x = fd_start.x = -1;
|
|
|
|
|
fd_end.y = fd_start.y = -1;
|
|
|
|
|
fd_end.enterfrom = fd_start.enterfrom = -1;
|
|
|
|
|
fd_end.parma = fd_start.parma = -1;
|
|
|
|
|
|
|
|
|
|
if (FindReference(&fd_start.x, &fd_start.y, blockstart.substr(2, string::npos))) {
|
|
|
|
|
if (Get(fd_start.x, fd_start.y).dir == 1) {
|
|
|
|
|
if (blockstart[0] == '+') fd_start.enterfrom = 0;
|
|
|
|
|
else fd_start.enterfrom = 2;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (blockstart[0] == '+') fd_start.enterfrom = 3;
|
|
|
|
|
else fd_start.enterfrom = 1;
|
|
|
|
|
}
|
|
|
|
|
fd_start.way = "b:" + blockstart;
|
|
|
|
|
fd_data[GetRIdx(fd_start.x, fd_start.y)].e = (1 << fd_start.enterfrom);
|
|
|
|
|
fd_data[GetRIdx(fd_start.x, fd_start.y)].score = 1;
|
|
|
|
|
fd_start.parma = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
debug (0, "Railway::FindRandomWay could not find startblock (%s).", blockstart.c_str());
|
|
|
|
|
free (fd_data);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd_pos = NextPos(fd_start, Get(fd_start.x, fd_start.y).dir);
|
|
|
|
|
Lock();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// loop through the map to find all possible ways to the destination
|
|
|
|
|
// this will be done in respect of lockedby blocks and ways.
|
|
|
|
|
//
|
|
|
|
|
while ((fd_list.size() > 0 || fd_pos.enterfrom >= 0) && found == 0) {
|
|
|
|
|
// DebugPrintFindWay(fd_data);
|
|
|
|
|
if (fd_pos.enterfrom < 0) {
|
|
|
|
|
std::list<struct s_findway_data>::iterator iter;
|
|
|
|
|
|
|
|
|
|
iter = fd_list.begin();
|
|
|
|
|
if (iter != fd_list.end()) {
|
|
|
|
|
fd_pos = *iter;
|
|
|
|
|
fd_list.pop_front();
|
|
|
|
|
// printf ("get from stack (%d,%d)\n", fd_pos.x, fd_pos.y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// printf ("%s:%d fd_start (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_start.x, fd_start.y, fd_start.enterfrom);
|
|
|
|
|
// printf ("%s:%d fd_end (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_end.x, fd_end.y, fd_end.enterfrom);
|
|
|
|
|
// printf ("%s:%d fd_pos (%d,%d) enterfrom:%d\n", __FILE__, __LINE__, fd_pos.x, fd_pos.y, fd_pos.enterfrom);
|
|
|
|
|
if (fd_pos.enterfrom < 0 && fd_pos.enterfrom > 3) {
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
else if (fd_pos.enterfrom >= 0 && fd_start.x == fd_pos.x && fd_start.y == fd_pos.y && fd_start.enterfrom == fd_pos.enterfrom) {
|
|
|
|
|
// start found
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
else if (fd_data[GetRIdx(fd_pos.x, fd_pos.y)].e & (1 << fd_pos.enterfrom)) {
|
|
|
|
|
// old entry found
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
else if (fd_pos.enterfrom >= 0) {
|
|
|
|
|
rpos = &railways[GetRIdx(fd_pos.x, fd_pos.y)];
|
|
|
|
|
fd_data[GetRIdx(fd_pos.x, fd_pos.y)].e |= (1 << fd_pos.enterfrom);
|
|
|
|
|
|
|
|
|
|
if (rpos->type == RAILWAY_NORMAL || rpos->type == RAILWAY_SENSOR) {
|
|
|
|
|
fd_pos = NextPos(fd_pos, rpos->dir);
|
|
|
|
|
fd_pos.parma++;
|
|
|
|
|
rnext = &railways[GetRIdx(fd_pos.x, fd_pos.y)];
|
|
|
|
|
if (rnext->lockedby[0] != 0) {
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (rpos->type == RAILWAY_TURNOUT) {
|
|
|
|
|
// random turn or no turn
|
|
|
|
|
if (rand() & 1 == 1) {
|
|
|
|
|
fd_tmp = NextPos(fd_pos, rpos->altdir);
|
|
|
|
|
fd_tmp.way += (string)",t:" + (string)rpos->name + (string)":1";
|
|
|
|
|
fd_tmp.parma += 10; // 10 bad point for using the turnout
|
|
|
|
|
fd_list.push_back(fd_tmp);
|
|
|
|
|
fd_pos = NextPos(fd_pos, rpos->dir);
|
|
|
|
|
fd_pos.way += (string)",t:" + (string)rpos->name + (string)":0";
|
|
|
|
|
fd_pos.parma++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fd_tmp = NextPos(fd_pos, rpos->dir);
|
|
|
|
|
fd_tmp.way += (string)",t:" + (string)rpos->name + (string)":0";
|
|
|
|
|
fd_tmp.parma += 10; // 10 bad point for using the turnout
|
|
|
|
|
fd_list.push_back(fd_tmp);
|
|
|
|
|
fd_pos = NextPos(fd_pos, rpos->altdir);
|
|
|
|
|
fd_pos.way += (string)",t:" + (string)rpos->name + (string)":1";
|
|
|
|
|
fd_pos.parma++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (rpos->type == RAILWAY_CONNECTOR) {
|
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
|
|
x = -1;
|
|
|
|
|
y = -1;
|
|
|
|
|
while (found == 0 && _FindReference(&x, &y, rpos->name) == 1) {
|
|
|
|
|
if (fd_pos.x != x || fd_pos.y != y) {
|
|
|
|
|
fd_pos.oldx = fd_pos.x;
|
|
|
|
|
fd_pos.oldy = fd_pos.y;
|
|
|
|
|
fd_pos.oldenterfrom = fd_pos.enterfrom;
|
|
|
|
|
fd_pos.x = x;
|
|
|
|
|
fd_pos.y = y;
|
|
|
|
|
rpos = &railways[GetRIdx(fd_pos.x, fd_pos.y)];
|
|
|
|
|
fd_pos = NextPos(fd_pos, rpos->dir);
|
|
|
|
|
fd_pos.parma++;
|
|
|
|
|
found = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found == 0) {
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// if block is off ignore it completly
|
|
|
|
|
else if (rpos->type == RAILWAY_BLOCK) {
|
|
|
|
|
int blflags = server->blocks.GetFlags(rpos->name);
|
|
|
|
|
|
|
|
|
|
if ((server->blocks.IsOff(rpos->name) ||
|
|
|
|
|
((blflags & BLOCK_F_ENDSTATION) && !(locoflags & LOCO_F_CANREVERSE)) ||
|
|
|
|
|
((blflags & BLOCK_F_SHORT) && !(locoflags & LOCO_F_SHORTTRAIN)))) {
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// lockedby = server->blocks.GetLockedby(rpos->name);
|
|
|
|
|
lockedby = server->blocks.GetLockedby(rpos->name);
|
|
|
|
|
if (lockedby.length() == 0 || lockedby.compare(lockedfor) == 0) {
|
|
|
|
|
if (fd_pos.enterfrom == 0 || fd_pos.enterfrom == 3)
|
|
|
|
|
fd_pos.way += (string)",b:+:" + (string)rpos->name;
|
|
|
|
|
else fd_pos.way += (string)",b:-:" + (string)rpos->name;
|
|
|
|
|
|
|
|
|
|
*next = fd_pos.way + ",b:" + (string)rpos->name;
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
found = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else { // finished nothing more to check
|
|
|
|
|
fd_pos.enterfrom = -1;
|
|
|
|
|
fd_pos.x = -1;
|
|
|
|
|
fd_pos.y = -1;
|
|
|
|
|
fd_pos.way = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found == 0) *next = "";
|
|
|
|
|
else {
|
|
|
|
|
//
|
|
|
|
|
// next = "b:+blockname,t:name:0,t:name:1,b:-blockname"
|
|
|
|
|
debug (0, "* Random Temp Next: %s", next->c_str());
|
|
|
|
|
int i;
|
|
|
|
|
i = 0;
|
|
|
|
|
size_t npos;
|
|
|
|
|
if ((npos = next->find(",b:")) != string::npos) {
|
|
|
|
|
*next = next->substr(0, next->find(",", npos+1));
|
|
|
|
|
debug (0, "* Random next: '%s' found", next->c_str());
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// nothing found?
|
|
|
|
|
debug (0, "* Random Nothing Found next: '%s' but no next block?", next->c_str());
|
|
|
|
|
*next = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
UnLock();
|
|
|
|
|
|
|
|
|
|
free (fd_data);
|
|
|
|
|
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Railways::LockWay (string way, string lockedby, int lockonoff) {
|
|
|
|
|
int res = 0;
|
|
|
|
|
size_t pos1, pos2;
|
|
|
|
|
@ -832,17 +1073,20 @@ int Railways::LockWay (string way, string lockedby, int lockonoff) {
|
|
|
|
|
// do lock start and endpoint... unlock only start
|
|
|
|
|
if (pos.x == end.x && pos.y == end.y && r->type == RAILWAY_BLOCK) {
|
|
|
|
|
if (lockonoff == 1) {
|
|
|
|
|
UnLock();
|
|
|
|
|
server->blocks.SetLockedby(r->name, lockedby, 1);
|
|
|
|
|
strncpy (r->lockedby, lockedby.c_str(), REFERENCENAME_LEN);
|
|
|
|
|
Lock();
|
|
|
|
|
jp.Clear();
|
|
|
|
|
jp.AddObject("railway",_GetJSONRailway(pos.x, pos.y));
|
|
|
|
|
if(network) network->ChangeListPushToAll(jp.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (pos.x == start.x && pos.y == start.y && r->type == RAILWAY_BLOCK) {
|
|
|
|
|
server->blocks.SetLockedby(r->name, lockedby, lockonoff);
|
|
|
|
|
UnLock();
|
|
|
|
|
if (lockonoff) server->blocks.SetLockedby(r->name, lockedby, lockonoff);
|
|
|
|
|
if (lockonoff) strncpy (r->lockedby, lockedby.c_str(), REFERENCENAME_LEN);
|
|
|
|
|
else r->lockedby[0] = 0;
|
|
|
|
|
Lock();
|
|
|
|
|
jp.Clear();
|
|
|
|
|
jp.AddObject("railway",_GetJSONRailway(pos.x, pos.y));
|
|
|
|
|
if(network) network->ChangeListPushToAll(jp.ToString());
|
|
|
|
|
|