0
\$\begingroup\$

Hi all! I have a one problem. I trying to create 2D cave generation system in C# with XNA game studio, but then I running program I getting "System.StackOverflowException". As you can see in this image: enter image description here

And I get this after I try to create new object from it own class. I in short explain how my cave generator works.

My map is containing ID’s (integers) of the different types of cells(like wall, water or empty Space).

First off all my "Map" Class creates filled with walls map and after that, in center of the map, it creating "Miner" object. It digs map and makes caves. But here is one problem, I wan't to create more miners. So then my miner digging map it in some place must create other miner, but I getting "System.StackOverflowException" Exception. Please help me... And thanks anyway. :)

Here is my miner code:

Miner.cs

public class Miner
{
    Random rand = new Random();

    public bool active { get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public Map map { get; set; }
    public int minersCount { get; set; }

    public Miner(Map map, bool active, int x, int y)
    {
        this.map = map;
        this.active = active;
        this.x = x;
        this.y = y;
        minersCount = 1;

        if (active)
        {
            StartDigging();
        }
    }

    bool IsOutOfBounds(int x, int y)
    {
        if (x == 0 || y == 0)
        {
            return true;
        }
        else if (x > map.mapWidth - 2 || y > map.mapHeight - 2)
        {
            return true;
        }
        return false;
    }

    bool IsLastMiner()
    {
        if (minersCount == 1)
        {
            return true;
        }
        return false;
    }

    public void StartDigging()
    {
        if (active)
        {
            int dir = 0;
            bool needStop = false;
            int ID = -1;

            while (!needStop && !IsOutOfBounds(x, y))
            {
                while (dir == 0)
                {
                    dir = ChooseDirection();
                }

                if (!AroundIsNothing())
                {
                    while (ID == -1)
                    {
                        ID = GetIDFromDirection(dir);
                    }
                }
                else
                {
                    if (!IsLastMiner())
                    {
                        needStop = true;
                    }
                }

                if (ID == 1)
                {
                    DigToDirection(dir);
                    dir = 0;
                }

                if (ID == 0 && IsLastMiner())
                {
                    MoveToDirection(dir);
                    dir = 0;
                }

                TryToCreateNewMiner();
            }
        }
    }

    public void TryToCreateNewMiner()
    {
        if (RandomPercent(8))
        {
            Miner newMiner = new Miner(map, true, x, y);
        }
        return;
    }

    bool AroundIsNothing()
    {
        if (map.map[x + 1, y] == 0 && map.map[x, y + 1] == 0 &&
            map.map[x - 1, y] == 0 && map.map[x, y - 1] == 0)
        {
            return true;
        }
        return false;
    }

    void MoveToDirection(int dir)
    {
        if (dir == 1)
        {
            x = x + 1;
        }
        else if (dir == 2)
        {
            y = y + 1;
        }
        else if (dir == 3)
        {
            x = x - 1;
        }
        else if (dir == 4)
        {
            y = y - 1;
        }
    }

    void DigToDirection(int dir)
    {
        if (dir == 1)
        {
            map.map[x + 1, y] = 0;
            x = x + 1;
        }
        else if (dir == 2)
        {
            map.map[x, y + 1] = 0;
            y = y + 1;
        }
        else if (dir == 3)
        {
            map.map[x - 1, y] = 0;
            x = x - 1;
        }
        else if (dir == 4)
        {
            map.map[x, y - 1] = 0;
            y = y - 1;
        }
    }

    int GetIDFromDirection(int dir)
    {
        if (dir == 1)
        {
            return map.map[x + 1, y];
        }
        else if (dir == 2)
        {
            return map.map[x, y + 1];
        }
        else if (dir == 3)
        {
            return map.map[x - 1, y];
        }
        else if (dir == 4)
        {
            return map.map[x, y - 1];
        }
        return -1;
    }

    int ChooseDirection()
    {
        return rand.Next(1, 5);
    }

    bool RandomPercent(int percent)
    {
        if (percent >= rand.Next(1, 101))
        {
            return true;
        }
        return false;
    }
}

As you can see then miner digging, he have about 8% chance that will create new miner. But then it creating it I getting error. New miner is creating at " TryToCreateNewMiner()" method.

\$\endgroup\$

closed as off-topic by MichaelHouse Nov 7 '14 at 15:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ @LJ_1102 I think he's got that part right. If it's greater than 8 than it returns false which is what he wants. But there does seem to be a greater chance than 8% here. Also, your only reference to each miner is going out of scope right after it's made. \$\endgroup\$ – Ben Nov 7 '14 at 14:07
  • \$\begingroup\$ @Ben You're right, deleted that comment. \$\endgroup\$ – LJᛃ Nov 7 '14 at 14:13
1
\$\begingroup\$

Your TryToCreateNewMiner function can call it again. If your RandomPercent calculator keeps returning true, the callstack gets deeper and deeper.

TryToCreateNewMiner->Miner->StartDigging->TryToCreateNewMiner

\$\endgroup\$
  • \$\begingroup\$ Oh right! Now I understand what I doing wrong. Thanks for help :) \$\endgroup\$ – Alex Nov 7 '14 at 14:15
  • \$\begingroup\$ If that's the problem, click the checkmark to indicate this answered your question. It tells other people that this question has been answered. \$\endgroup\$ – Almo Nov 7 '14 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.