diff --git a/code/game/turfs/simulated/river.dm b/code/game/turfs/simulated/river.dm index 2febfc20d78..7c9034b9ea5 100644 --- a/code/game/turfs/simulated/river.dm +++ b/code/game/turfs/simulated/river.dm @@ -67,21 +67,25 @@ var/detouring = 0 var/cur_dir = get_dir(cur_turf, target_turf) while(cur_turf != target_turf) - if(detouring) //randomly snake around a bit - if(prob(21)) - detouring = 0 - cur_dir = get_dir(cur_turf, target_turf) - else if(prob(20)) - detouring = 1 - if(prob(50)) - cur_dir = turn(cur_dir, 45) + var/attempts = 100 + do + if(detouring) //randomly snake around a bit + if(prob(20)) + detouring = 0 + cur_dir = get_dir(cur_turf, target_turf) + else if(prob(20)) + detouring = 1 + if(prob(50)) + cur_dir = turn(cur_dir, 45) + else + cur_dir = turn(cur_dir, -45) else - cur_dir = turn(cur_dir, -45) - else - cur_dir = get_dir(cur_turf, target_turf) + cur_dir = get_dir(cur_turf, target_turf) + // we may veer off the map entirely, returning a null turf; if so, go back and try again + while(get_step(cur_turf, cur_dir) == null && attempts-- > 0) cur_turf = get_step(cur_turf, cur_dir) - if(isnull(cur_turf)) //This might be the fuck up. Kill the loop if this happens + if(isnull(cur_turf)) stack_trace("Encountered a null turf in river loop, target turf was [target_turf], x=[target_turf.x], y=[target_turf.y].") break var/area/new_area = get_area(cur_turf)