1

I apologize for the useage of "Better" but thats seriously what I need.

I am writing a "river" generating algorithm and I have a bunch of comparisons I need to make. I was wondering if there is a better way to write all of the comparisons I have to make.

if Rain.value(atPosition: vector_float2(Float(columns),Float(rows))) == 1.0 {
    fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    if Rain.value(atPosition: vector_float2(Float(columns - 1),Float(rows + 1))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
    if Rain.value(atPosition: vector_float2(Float(columns - 1),Float(rows))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
    if Rain.value(atPosition: vector_float2(Float(columns - 1),Float(rows - 1))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
    if Rain.value(atPosition: vector_float2(Float(columns),Float(rows - 1))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
    if Rain.value(atPosition: vector_float2(Float(columns + 1),Float(rows - 1))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
    if Rain.value(atPosition: vector_float2(Float(columns + 1),Float(rows))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
    if Rain.value(atPosition: vector_float2(Float(columns + 1),Float(rows + 1))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
    if Rain.value(atPosition: vector_float2(Float(columns),Float(rows + 1))) == 0.9 {
        fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
    }
}

Basically the "Rain" is a perlin HeightMap that I am using to determine the rainfall in my tileMap. These if-statements are checking the 8 adjacent tiles in the fullMap(that meet the criteria of High rainfall) and determining the lowest value and then setting a water tile in the form of a river.

This method is incredibly in-efficient as I am going to continue to write if-statements into these if statements to check each adjacent tile after that. It is going to be a massive chunk of code. Is there a better way to check each adjacent tile around a certain tile in a tileMap to reduce the size of my code?

E. Huckabee
  • 1,788
  • 1
  • 13
  • 29

2 Answers2

1

Since you are looking at the number ±1, you could use two range loops:

outerLoop:
for dc in -1...1 {
    for dr in -1...1 {
        if dr == 0 && dc == 0 {
            continue
        }
        if Rain.value(atPosition: vector_float2(Float(columns+dc),Float(rows+dr))) == 0.9 {
            fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
            break outerLoop
        }
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

This is similar to @dasblinkenlight's answer.

If you end up doing this often in your code, you might want to create an array of adjacentOffsets and loop through those:

let adjacentOffsets = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

if Rain.value(atPosition: vector_float2(Float(columns),Float(rows))) == 1.0 {
    for (dc, dr) in adjacentOffsets {
        if Rain.value(atPosition: vector_float2(Float(columns+dc),Float(rows+dr))) == 0.9 {
            fullMap.setTileGroup(tileDef4, forColumn: columns, row: rows)
            break
        }
    }
}
vacawama
  • 150,663
  • 30
  • 266
  • 294