简体   繁体   中英

Path-finding algorithm for a game

I have this assigment in university where I'm given the code of a C++ game involving pathfinding. The pathfinding is made using a wave function and the assigment requires me to make a certain change to the way pathfinding works.

The assigment requires the pathfinding to always choose the path farthest away from any object other than clear space. Like shown here:

在此处输入图片说明

And here's the result I've gotten so far:

在此处输入图片说明

Below I've posted the part of the Update function concerning pathfinding as I'm pretty sure that's where I'll have to make a change.

for (int y = 0, o = 0; y < LEVEL_HEIGHT; y++) {
    for (int x = 0; x < LEVEL_WIDTH; x++, o++) {
        int nCost = !bricks[o].type;
        if (nCost) {
            for (int j = 0; j < 4; j++)
            {
                int dx = s_directions[j][0], dy = s_directions[j][1];
                if ((y == 0 && dy < 0)
                    || (y == LEVEL_HEIGHT - 1 && dy > 0)
                    || (x == 0 && dx < 0)
                    || (x == LEVEL_WIDTH - 1 && dx > 0)
                    || bricks[o + dy * LEVEL_WIDTH + dx].type)
                {
                    nCost = 2;
                    break;
                }
            }
        }
        pfWayCost[o] = (float)nCost;
    }
}

Also here is the Wave function if needed for further clarity on the problem.

I'd be very grateful for any ideas on how to proceed, since I've been struggling with this for quite some time now.

Your problem can be reduced to a problem known as minimum-bottle-neck-spanning-tree .

For the reduction do the following:

  1. calculate the costs for every point/cell in space as the minimal distance to an object.
  2. make a graph were edges correspond to the points in the space and the weights of the edges are the costs calculated in the prior step. The vertices of the graph corresponds to the boundaries between cell.

For one dimensional space with 4 cells with costs 10, 20, 3, 5:

|10|20|3|5|

the graph would look like:

 A--(w=10)--B--(w=20)--C--(w=3)--D--(w=5)--E

With nodes AE corresponding to the boundaries of the cells.

  1. run for example the Prim's algorithm to find the MST. You are looking for the direct way from the entry point (in the example above A) to the exit point (E) in the resulting tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM