Register
Handmade Hero»Forums»Code»Day 036. Problem with the stairs generation.
2 posts
Day 036. Problem with the stairs generation.
1 month ago Edited by HazeOfFlex on Oct. 13, 2019, 1:04 p.m. Reason: Initial post
I have stairs in every room, instead of one in the end.
I can't figure out the problem. Can someone help me?
This is my generation code:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
u32 randomNumberIndex = 0;
        
u32 tilesPerWidth = 17;
u32 tilesPerHeight = 9;

u32 screenX = 0;
u32 screenY = 0;
        
u32 absTileZ = 0;

b32 isDoorTop = false;
b32 isDoorLeft = false;
b32 isDoorBottom = false;
b32 isDoorRight = false;
b32 isDoorUp = false;
b32 isDoorDown = false;
        
for(u32 screenIndex = 0;
    screenIndex < 100;
    screenIndex++)
{
    // TODO: Random number generator
    Assert(randomNumberIndex < ArrayCount(randomNumberTable));
    u32 randomChoice;

    if(isDoorUp || isDoorDown)
    {
        randomChoice = randomNumberTable[randomNumberIndex++] % 2;
    }
    else
    {
        randomChoice = randomNumberTable[randomNumberIndex++] % 3;
    }

    b32 isCreatedZDoor = false;
    if(randomChoice == 2)
    {
        isCreatedZDoor = true;
                
        if(absTileZ == 0)
        {
            isDoorUp = true;
        }
        else
        {
            isDoorDown = true;
        }
    }
    else if(randomChoice == 1)
    {
        isDoorRight = true;
    }
    else
    {
        isDoorTop = true;
    }
            
    for(u32 tileY = 0;
        tileY < tilesPerHeight;
        tileY++)
    {
        for(u32 tileX = 0;
            tileX < tilesPerWidth;
            tileX++)
        {
            u32 absTileX = screenX * tilesPerWidth
                + tileX;
                        
            u32 absTileY = screenY * tilesPerHeight
                + tileY;

            u32 tileValue = 1;
            if(tileX == 0 &&
               (!isDoorLeft || tileY != tilesPerHeight / 2))
            {
                tileValue = 2;
            }

            if(tileX == tilesPerWidth - 1 &&
               (!isDoorRight || tileY != tilesPerHeight / 2))
            {
                tileValue = 2;
            }

            if(tileY == 0 &&
               (!isDoorBottom || tileX != tilesPerWidth / 2))
            {
                tileValue = 2;
            }

            if(tileY == tilesPerHeight - 1 &&
               (!isDoorTop || tileX != tilesPerWidth / 2))
            {
                tileValue = 2;
            }

            if(tileX == 10 && tileY == 6)
            {
                if(isDoorUp)
                {
                    tileValue = 3;
                }
                else
                {
                    tileValue = 4;
                }
            }
                        
            SetTileValue
                (
                    &gameState->worldArena, tileMap,
                    absTileX, absTileY, absTileZ,
                    tileValue
                 );
        }
    }

    isDoorLeft = isDoorRight;
    isDoorBottom = isDoorTop;

    if(isCreatedZDoor)
    {
        isDoorUp = !isDoorUp;
        isDoorDown = !isDoorDown;
    }
    else
    {
        isDoorUp = false;
        isDoorDown = false;
    }
            
    isDoorTop = false;
    isDoorRight = false;

    if(randomChoice == 2)
    {
        if(absTileZ == 0)
        {
            absTileZ = 1;
        }
        else
        {
            absTileZ = 0;
        }
    }
    else if(randomChoice == 1)
    {
        screenX++;
    }
    else
    {
        screenY++;
    }
}
Simon Anciaux
680 posts
Day 036. Problem with the stairs generation.
1 month ago
You should run the code in the debugger, set a breakpoint on the first line of the loop and inspect the value while stepping in the program. Don't assume the values are correct, check them.

If you have stairs generated, it means that the program sets "tileValue" to 3 (up) or 4 (down). Put a breakpoint were "tileValue" is set to 3 or 4 and see how you arrived there.


You are missing an if on line 103.

This code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
if(tileX == 10 && tileY == 6)
{
    if(isDoorUp)
    {
        tileValue = 3;
    }
    else
    {
        tileValue = 4;
    }
}

should be
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
if(tileX == 10 && tileY == 6)
{
    if(isDoorUp)
    {
        tileValue = 3;
    }
    else if(isDoorDown)
    {
        tileValue = 4;
    }
}

Otherwise you will always place downward stairs on tile 10, 6 unless you needed to place upward stairs.
2 posts
Day 036. Problem with the stairs generation.
1 month ago
Ah, yes, thanks