C#C
C#5mo ago
FiftyTifty

How to set bits in a bitmask?

tl;dr: I have a byte as a bitmask, how do I performantly set a bit as 0 or 1?

Long story:

I have a big 2D array (6144x4096 in length) of 3D tiles (XPos, YPos, ZPos) and need to check neighbouring tiles and placing a specific water sprite depending on which neighbours are land or water. Which is a big pain in my butt, thinking of arrays or lists as something other than a simple list of individual numbers breaks my head.

Simplest approach I think is to use a bitmask and reference that in an enum. But the docs for doing stuff with bits is super arcane and designed for programmers who have been doing this since the 70's, not for hobbyist modders without brains wired for coding 😅

Here's psuedocode from me breaking down the concept and what I'm working with, as a 2D array of vectors is too abstract to stay inside my head. Quite simple:
Get tile, check if it's super low
If it is then it's a water tile
check neighbouring tiles if they're higher
If they are, they're land
calculate which sides of the water tile border the land tiles.
//Bitmask
//0000
//NESW

byte bitmaskEdges = 0

Get tile and check tileNorth, tileEast, tileWest
Get NorthFacingTilesToPlace

    if tileCurrent Z is above -125
        tileCurrent is not water, skip tileCurrent

    Check neighbour tileNorth
    If Z is above -125
        tileNorth is an edge
        bitmaskEdges[0] = 1

    Check neighbour tileEast
    If Z is above -125
        tileEast is an edge
        bitmaskEdges[1] = 1

    Check neighbour tileWest
    If Z is above -125
        tileWest is an edge
        bitmaskEdges[3] = 1

    if bitMaskEdges > 0
        Add tile and bitmask to list
End

GetSouthFacingTiles
  Same as above but check for tileSouth, tileEast, tileWest
End
image.png
Was this page helpful?