- Home /
2D grid base Rogue style FOV acting funky
WHAT I WANT: More than anything, is a functioning FOV system for a tile based Rogue game I'm making. The question at hand is what is causing my current algorithm to suck so hard?
THE SETUP:
void Scan(Row row, Queue<TileLogger> logger)
{
Vector2Int prevTile = new Vector2Int(10000,10000);
foreach(Vector2Int tilePos in row.Tiles())
{
Debug.Log(row.depth);
TileLogger newLog = new TileLogger(quadrant.TransformCoords(tilePos), "NotInside",0.5f,row.depth);
if(CheckTransform(tilePos, mapCols, mapRows))
{
newLog.type = $"depth: {newLog.curdepth} status: OnlyInside";
if(Mathf.Abs(tilePos.x) > radius || Mathf.Abs(tilePos.y) > radius)
{
newLog.type = $"depth: {newLog.curdepth} status: OutOfBounds";
return;
}
if(IsWall(tilePos) || IsSymmetric(row, tilePos))
{
Reveal(tilePos);
newLog.type = $"depth: {newLog.curdepth} status: WallOrSymmetric";
newLog.lightLevel = 1f;
}
if(IsWall(prevTile) && IsFloor(tilePos))
{
row.startSlope = Slope(tilePos);
newLog.type = $"depth: {newLog.curdepth} status: LastTileWallThisIsFloor. setting row.startslope to {row.startSlope}";
}
if(IsFloor(prevTile) && IsWall(tilePos))
{
Row nextRow = row.Next();
nextRow.endSlope = Slope(tilePos);
newLog.type =
$"depth: {newLog.curdepth} status: LastTileFloorThisIsWall. nextRow.endSlope = {nextRow.endSlope}, begin scan next row.";
Scan(nextRow, logger);
}
prevTile = tilePos;
}
logger.Enqueue(newLog);
}
logger.Enqueue(new TileLogger(Vector2Int.zero,
$"depth: {row.depth} Nontile event: Row finished, scanning next row. Else, returning up a row.",0.25f,row.depth));
if(IsFloor(prevTile))
{
Scan(row.Next(),logger);
}
}
(Please excuse the logging code, it makes it all a bit messy) There is a lot of code to my current implementation, and overall, its essentially a translation of the following: https://www.albertford.com/shadowcasting/ I followed the above and implemented a version in python that works beautifully, and as such I thought if I just converted it to c# for unity it'd work great. There is however, one big problem im encountering right now, and I have no idea why.
THE PROBLEM:
It should scan left to right, and there are ONLY TWO reasons it should change to the next row for scanning:
A) The current scanned tile is a wall tile, and the last scanned one is a floor tile
B) The row has reached capacity for the quadrant
But for some reason, my code skips from revealing the first tile in the second row, straight to the first tile of the third row. I have no idea how this is even possible. If anybody has any further help or questions relating to the code, please feel free to ask. Im also open to an entirely new approach, but so far this symmetric shadowcasting has been the best looking one. I will also paste some of the methods used in this scan.
Quadrant quadrant = new Quadrant(direction, origin);
void Reveal(Vector2Int tilePos)
{
Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
SetVisible(transformedTilePos, visibilityQueue, map2D);
}
bool IsWall(Vector2Int tilePos)
{
if(tilePos == new Vector2Int(10000,10000))
{
return false;
}
Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
return IsBlocking(transformedTilePos, map2D);
}
bool IsFloor(Vector2Int tilePos)
{
if(tilePos == new Vector2Int(10000,10000))
{
return false;
}
Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
return !IsBlocking(transformedTilePos, map2D);
}
bool CheckTransform(Vector2Int tilePos,int xLength, int yLength)
{
Vector2Int transformedTilePos = quadrant.TransformCoords(tilePos);
bool viable = true;
if(transformedTilePos.x > xLength)
viable = false;
if(transformedTilePos.x < 0)
viable = false;
if(transformedTilePos.y > yLength)
viable = false;
if(transformedTilePos.y < 0)
viable = false;
return viable;
}
Answer by SeaLeviathan · Mar 17, 2021 at 06:51 PM
Oy vey, I figured it out now. It just does things a little out of order, and I fixed my whole algorithm by floatcasting my integer divides for view slope!!! I missed that bit, but it works now so sweet. If you're a curious passerby, @ me if you want the working algorithm.
Your answer
Follow this Question
Related Questions
Improving Grid Algorithm 1 Answer
Grid Shape Detection 3 Answers
Texture grid displayed oddly when width =/= height 1 Answer
Generating grid with specified number of connections between nodes 0 Answers
board game algorithm 0 Answers