Git Product home page Git Product logo

dijkstra-map's Introduction

dijkstra-map

Python implementation to create a Dijkstra map (distance map)

Usage

Just import the flood function from the flood module. To use it the first parameter is an input 2D array that will have 0's in the start positions where you want the flooding to begin, the other elements can have any other value. Optionally, you can give another map with walls, and it has to be of the same size and a binary 2D array where True means walls; you can also give a limit, if you have something like a maximum distance (for example if you want to convert to an image you can set limit to 255 and the elements that are further than that will be set to 255).

Example:

import numpy as np
from dijkstra_map import dijkstra_map

input_map = np.array(
    [
        [1, 1, 1, 0],
        [1, 0, 1, 1],
        [1, 0, 1, 1],
        [1, 1, 1, 1]
    ],
    dtype=int
)

walls_map = np.array(
    [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 1, 0]
    ],
    dtype=int
)

output_map = dijkstra_map(input_map, walls_map)
print(output_map)

That would print:

[[1 1 1 0]
 [1 0 1 1]
 [1 0 0 2]
 [1 1 0 3]]

Note that wall elements are set to 0 when limit is not given. If we use a limit, they will take that value, for example:

import numpy as np
from dijkstra_map import dijkstra_map

input_map = np.array(
    [
        [1, 1, 1, 0],
        [1, 0, 1, 1],
        [1, 0, 1, 1],
        [1, 1, 1, 1]
    ],
    dtype=int
)

walls_map = np.array(
    [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 1, 0]
    ],
    dtype=int
)

output_map = dijkstra_map(input_map, walls_map, limit=2)
print(output_map)

Will give:

[[1 1 1 0]
 [1 0 1 1]
 [1 0 2 2]
 [1 1 2 2]]

Dependencies

This project works with Python and the external library numpy. To install it run:

python -m pip install -r requirements.txt

Examples

Path-finding

In this example game we have a 2D grid with the player represented as a blue circle and 3 enemies as other circles that chase the player. Path finding for the enemies is implemented using a dijkstra map that is recalculated on each update call.

path finding

Testing

To run the tests use unittest in this way:

python -m unittest -v tests

This will run every test in the tests module, if you add new tests make sure to import them into the __init__.py file inside the tests folder.

dijkstra-map's People

Contributors

henryxz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

belzecue

dijkstra-map's Issues

Unefficient implementation

I stumbled upon the original Dijkstra map article and found the concept very useful but the implementation idea is weird.

The whole algorithm can be implemented as a simple BFS. To make it work, BFS shall start not from a single cell (as usual) but from all goal cells.

Such implementation is not only more efficient but also shorter and easier to understand.

Please see my own custom implementation in case my explanation was not sufficiently clear:

def create_dijkstra_map(self):
    game_map = self.entity.game_map
    dijkstra_map = np.full((game_map.width, game_map.height), np.inf)

    queue: deque[tuple[int, int, int]] = deque()

    for x in range(game_map.width):
        for y in range(game_map.height):
            if self.entity.explored[x, y] == False and game_map.tiles["walkable"][x, y]:
                dijkstra_map[x, y] = 0
                queue.append((x, y, 0))

    while queue:
        x, y, distance = queue.popleft()
        neighbors = game_map.get_neighbors(x, y)
        for nx, ny in neighbors:
            if dijkstra_map[nx, ny] == np.inf and game_map.tiles["walkable"][nx, ny] and game_map.get_blocking_entity_at_location(nx, ny) is None:
                dijkstra_map[nx, ny] = distance + 1
                queue.append((nx, ny, distance + 1))

return dijkstra_map

Please tell me if I am missing something and original implementation is better/different in some way.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.