#mazeSolving

1 messages · Page 1 of 1 (latest)

orchid mesa
#

ok, so

#

from a general standpoint, what you want to do is manage two lists

#

a list of valid positions it can move to next

#

and a list of positions it has already visited

dark dove
#

activeTiles - list you can move to next
visitedTiles - list you already visited

orchid mesa
#

is your function called in update?

dark dove
#

It's called via InvokeRepeating("MoveEmployee", 1.0f, 1.5f); in start

#

because I want there to be a delay every time the gameobject moves

orchid mesa
#

ok

dark dove
#

let me send the entire script via hastebin

orchid mesa
#

i think you want something like this

#

but look at it and see if it makes sense

#
  {
    visitedTiles.Add(currentPos);
    activeTiles = new List<Vector2>();
    Vector2 up = new Vector2(currentPos.x, currentPos.y + 1);
    Vector2 down = new Vector2(currentPos.x, currentPos.y - 1);
    Vector2 left = new Vector2(currentPos.x - 1, currentPos.y);
    Vector2 right = new Vector2(currentPos.x + 1, currentPos.y);

    if (!visitedTiles.Contains(up))
      activeTiles.Add(up);


    //Sets this employee to next position based on active tiles
    Vector2 nextPosition = activeTiles[Random.Range(0, activeTiles.Count)];
    gameObject.transform.position = nextPosition;

    //Add new position to visited tiles
    visitedTiles.Add(nextPosition);
  }
#

and you'd need to add the if...add for the other 3 directions

dark dove
orchid mesa
#

you do if you want everything everywhere to be added, but if you want your little dude every turn to only pick one of the 4 nearby locations, then you don't

#

if you want them to be able to go anywhere at all that they haven't been, then get rid of the new list line for active tiles

dark dove
#
private void MoveToRandomPos(Vector2 currentPos) 
    {
        visitedTiles.Add(currentPos);
        List<Vector2> activeTiles = new List<Vector2>();

        Vector2 up = new Vector2(currentPos.x, currentPos.y + 1);
        Vector2 down = new Vector2(currentPos.x, currentPos.y - 1);
        Vector2 left = new Vector2(currentPos.x - 1, currentPos.y);
        Vector2 right = new Vector2(currentPos.x + 1, currentPos.y);

        activeTiles.Add(up);
        activeTiles.Add(down);
        activeTiles.Add(left);
        activeTiles.Add(right);

        //Add directions to active tiles if not visited
        if (!visitedTiles.Contains(up))
            activeTiles.Add(up);

        if (!visitedTiles.Contains(down))
            activeTiles.Add(down);

        if (!visitedTiles.Contains(left))
            activeTiles.Add(left);

        if (!visitedTiles.Contains(right))
            activeTiles.Add(right);

        //Checks if new directions were visited, if so remove from active list
        for (int i = activeTiles.Count; i < activeTiles.Count; i--)
        {
            if (visitedTiles.Contains(activeTiles[i]))
            {
                activeTiles.Remove(activeTiles[i]);
            }
        }

        //Sets this employee to next position based on active tiles
        Vector2 nextPosition = activeTiles[Random.Range(0, activeTiles.Count)];
        gameObject.transform.position = nextPosition;

        //Add new position to visited tiles
        visitedTiles.Add(nextPosition);
    }```
#

Include the for loop?

#

I don't think you have to since we're checking visitedtiles already

orchid mesa
#

no, get rid of for loop, and the add before the if's

#
    {
        visitedTiles.Add(currentPos);
        List<Vector2> activeTiles = new List<Vector2>();

        Vector2 up = new Vector2(currentPos.x, currentPos.y + 1);
        Vector2 down = new Vector2(currentPos.x, currentPos.y - 1);
        Vector2 left = new Vector2(currentPos.x - 1, currentPos.y);
        Vector2 right = new Vector2(currentPos.x + 1, currentPos.y);

 
        //Add directions to active tiles if not visited
        if (!visitedTiles.Contains(up))
            activeTiles.Add(up);

        if (!visitedTiles.Contains(down))
            activeTiles.Add(down);

        if (!visitedTiles.Contains(left))
            activeTiles.Add(left);

        if (!visitedTiles.Contains(right))
            activeTiles.Add(right);

         //Sets this employee to next position based on active tiles
        Vector2 nextPosition = activeTiles[Random.Range(0, activeTiles.Count)];
        gameObject.transform.position = nextPosition;

        //Add new position to visited tiles
        visitedTiles.Add(nextPosition);
    }```
dark dove
#

Right

orchid mesa
#

does that make sense what its doing?

dark dove
#

Yeah, we're declaring a new list every time the AI moved basically

#

in that list are the new directions

#

Okay great, that's part of the A* pathfinding done haha. Thank you @orchid mesa

orchid mesa
#

and whats probably going to happen is your guy will wander around a bit and then corner himself and not move anymore

#

because he will have already visited all 4 of the nearby tiles

dark dove
#

Right, when that happens I should clear visitedTiles

orchid mesa
#

and wont have a valid place to go

dark dove
#

if (up, down, left, right are visited) clear visited

orchid mesa
#

ya, so you can add an if activetiles is empty at the end, clear visited tiles or something

dark dove
#

now I need to set a direction and get the GO location

#

but I'll research some of that

#

I used this for reference

orchid mesa
#

yep, what you're doing is really similar to a maze finding algorithm

#

which is why i called the thread that

dark dove
#

yes

#

I did a maze solving algorithm for my data structures class last year

#

but it was last year

#

and in java

orchid mesa
#

ya, concepts are similar, you just need to adapt to the language

dark dove
#

we also used intellij

#

and a custom class for the map lol

orchid mesa
#

not familiar with that

dark dove
#

its like visual studio

orchid mesa
#

one thing you may run across

#

is floating point precision errors

dark dove
#

yeah I already ceiltoint

orchid mesa
#

ah ok, ya thats good

#

you can also just use a vector2Int

dark dove
#

It sucks because I have two locations for the ai

#

the global and the grid

#

global location to gridlocation formula is basically
object.x - (Horizontal - 0.5f), object.y - (Vertical - 0.5f)

orchid mesa
#

and then you ceil it?

dark dove
#

yeah

#

well I ceil it during that too

#

but that's the formula w/o ceiling the float

#

I'm going to start playing around with getting the AI to the brewerLoc now though

#

Again, thanks for your help!