Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Need some help with Hunt-and-Kill?

Asked by
bloxxyz 274 Moderation Voter
9 years ago

Hi, I hope this is a reasonable question. I do not understand this script at all. It's from the wiki, and I just don't get it. I'm probably an Intermediate scripter, however I don't know things like what a "node" is, and how this script was set up and what exactly it does. I'm really interested in it, I've been studying it for a while and I'd like some help breaking this down just a little? Or at least lead me into a direction to where I can smoothly read this script? I don't understand things like neighbors, and some of the very foreign syntax. Thanks for the help if you can. The wiki article I was looking at is here:

http://wiki.roblox.com/index.php?title=Hunt-and-Kill

Here's the script from that article, if you need it:

function Hunt_and_Kill(node)
    node.visited = true

    --Take visited neighbors out of the possible selections
    local neighbors = node:neighbors()
    for i=#neighbors,1,-1 do
        if neighbors[i].visited then
            table.remove(neighbors,i)
        end
    end
    --if there are unvisited neighbors of node, select a random one
    if #neighbors>0 then
        local new_node = neighbors[math.random(#neighbors)]
        --delete the wall between the two nodes
        deleteBetween(node,new_node)
        --repeat process on new_node
        Hunt_and_Kill(new_node)

    --if there are no unvisited neighbors, begin hunt for new neighbor
    else
        for y = 1, bottom_of_maze do
            for x = 1, end_of_maze do
                --if the node has not been visited and is next to a visited neighbor, select
                if not maze[x][y].visited then
                    local visitedNeighbor = false
                    for _, neighbor in pairs(maze[x][y]:neighbors()) do
                        if v.visted then
                            visitedNeighbor = neighbor
                            break
                        end
                    end
                    --if found, begin process over again
                    if visitedNeighbor then
                        Hunt_and_Kill(visitedNeighbor)
                    end
                end
            end
        end
    end
end

1 answer

Log in to vote
1
Answered by 9 years ago

A node is "something" that connects to other nodes. In this case, it's a square in the maze's grid.

I found a good image to illustrate what this means: mazeBefore.gif

Each square is a node that may or may not connect to the squares directly adjacent to it horizontally/vertically -- ie, it's "neighbours". For any two adjacent nodes, they are connected if there is an opening from one node to the other; otherwise there is a wall blocking that movement. (ex in the image, the top-left square is connected to the one to the right of it).

Script breakdown:

Line 2: Records that the node has been visited so that we don't revisit nodes.

Line 3: ":neighbours()" is presumably a function that would return the valid neighbours of the node (ie the nodes directly to the left, right, above, and below the original node)

Lines 6-10: Remove from the "neighbours" list any nodes we've already visited

Lines 12-20: If there is a valid neighbour, we follow Procedure Steps 2-4 (see wiki). Line 15 refers to a function that would be responsible for removing the wall between "node" and "new_node". Line 17 performs recursion; it essentially restarts the algorithm for the new node. (Recursion isn't necessary; the algorithm could just as easily use a "repeat ... until node == nil" loop)

The remaining lines deal with Procedure Steps 5-6. It's doing a search for a node that has a visited neighbour and then connecting the two, and then recursing on the unvisited node. (If there is no such node, the algorithm is complete and the maze has been filled in.)

Ad

Answer this question