Minimal A* implementation in C. No dynamic memory allocation.

Overview

Micro A Star Path Finder

This is a minimal A* path finder implementation in C, without any dynamic memory allocation.

Usage

The maximum size of the map is defined by the macro PATH_FINDER_MAX_CELLS, which can be modified to support larger maps.

The size of the map is defined by the variables cols and rows in the path_finder structure, and the total of cells must be smaller or equal to PATH_FINDER_MAX_CELLS.

struct path_finder path_finder = {0};
path_finder.cols = 24;
path_finder.rows = 24;
init_path_finder(&path_finder);
path_finder.data = game;
/* Callback to fill the initial state of the cells */
path_finder.fill_func = fill_cb;
/* Callback to set the custom score to the cells while finding a path */
path_finder.score_func = score_cb;
/* Fill with the initial state of the cells and the custom score */
path_finder_fill(&path_finder, game_object);
path_finder_set_start(&path_finder, from_col, from_row);
path_finder_set_end(&path_finder, to_col, to_row);
/* Find the path */
path_finder_find(&path_finder, game_object);

The callback fill_func is necessary to define which cells are passable and which are non-passable:

/* The parameters `col` and `row` indicate the cell of the map we are setting as passable or non-passable */
static bool fill_cb(struct path_finder *path_finder, int32_t col, int32_t row)
{
	struct game *game;
	uint8_t is_passable;
	game = path_finder->data;
	is_passable = 0;
	if (is_wall(game, col, row) == 1) {
		is_passable = 0;
	}
	return is_passable;
}

The callback score_func is optional and is called during the path_finder_find execution. The callback also takes a custom pointer, useful to point to a specific object. Use it to add custom score to the cells:

/* The parameters `col` and `row` indicate the cell of the map we are setting a score */
static int32_t score_cb(struct path_finder *path_finder, int32_t col, int32_t row, void *data)
{
	struct game *game;
	struct game_object *game_object;
	int32_t value;
	game = path_finder->data;
	game_object = data;
	value = 0;
	if (is_danger_zone(game, col, row) == 1) {
		/* The higher the value, the more avoided the cell is */
		value = 5;
		if (is_fearless(game_object) == 1) {
			/* Unless the character is fearless */
			value = 0;
		}
	}
	/* The value returned is incremented in the cell score */
	return value;
}

To check if a cell is a path, access the path finder array directly or use path_finder_is_path (be careful, this function doesn't check the passed values of column and row are inside the range of the map dimensions, as they should be):

cell_is_path = path_finder_is_path(&path_finder, col, row);

If a path is found by the path finder, the value of has_path inside the structure path_finder is set to 1.

It is also possible to process only one step of the path finder at a time, useful to divide the processing in multiple frames:

uint8_t still_working;
path_finder_begin(&path_finder);
still_working = path_finder_find_step(&path_finder, NULL);

Or, to run the entire process, but drawing the intermediate steps:

path_finder_begin(&path_finder);
while (path_finder_find_step(&path_finder, NULL) == 1) {
	draw();
}

Test

The test generates an output with the map:

./test 0 80 12345 0 0 23 11 24 13
  Passable chance: 80
            Start: 'S' (or 's' if fall in a wall)
              End: 'E' (or 'e' if fall in a wall)
        Open path: 'O'
      Closed path: 'X'
             Path: '*'
       Unpassable: '#'
Map:
##########################
#S#    #      #       #  #
#****###  #      # ##    #
### **#******##    #     #
###  ***#  #**  #   # ## #
#  #  ## #   * # #  #  # #
#   ##  #    *# ##       #
## #   # #  #****#       #
#    #     #    *******  #
#   #  #      #  #    *# #
# #  #  ##         # #**##
# # #       ##         **#
#                       E#
#  #    ## # ### #    #  #
##########################
A path was found!

LICENSE

The source code of this project is licensed under the terms of the ZLIB license:

Copyright (c) 2017 Felipe Ferreira da Silva

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
You might also like...
An open source implementation of the dark souls 2 game server.
An open source implementation of the dark souls 2 game server.

WARNING: This is non-functional, its an initial branch from ds3os. What is this project? An open source implementation of the dark souls 2 game server

A lightweight C++20 coroutine implementation for Unreal Engine 5.

UE5Coro This library implements basic C++20 coroutine support for Unreal Engine. Getting started Obviously you'll need to switch your project to C++20

Implementation of Conway's Game of Life in C++ with SDL2
Implementation of Conway's Game of Life in C++ with SDL2

Conway's Game of Life Implementation of Conway's Game of Life in C++ with SDL2. Usage make sure SDL2 is well configured. make run click on screen to m

Minimal A* implementation in C. No dynamic memory allocation.

Micro A Star Path Finder This is a minimal A* path finder implementation in C, without any dynamic memory allocation. Usage The maximum size of the ma

Custom memory allocators in C++ to improve the performance of dynamic memory allocation
Custom memory allocators in C++ to improve the performance of dynamic memory allocation

Table of Contents Introduction Build instructions What's wrong with Malloc? Custom allocators Linear Allocator Stack Allocator Pool Allocator Free lis

Thread Stack Spoofing - PoC for an advanced In-Memory evasion technique allowing to better hide injected shellcode's memory allocation from scanners and analysts.
Thread Stack Spoofing - PoC for an advanced In-Memory evasion technique allowing to better hide injected shellcode's memory allocation from scanners and analysts.

Thread Stack Spoofing PoC A PoC implementation for an advanced in-memory evasion technique that spoofs Thread Call Stack. This technique allows to byp

MFAT is a minimal I/O library for FAT (File Allocation Table) volumes.

MFAT MFAT is a minimal I/O library for FAT (File Allocation Table) volumes. The library has been designed for embedded systems, and its small code and

A memory allocation program, it is used for doing an experiment to find out the detail of Microsoft Windows taskmgr performance information
A memory allocation program, it is used for doing an experiment to find out the detail of Microsoft Windows taskmgr performance information

memory-allocation-test A memory allocation program, it is used for doing an experiment to find out the detail of Microsoft Windows taskmgr performance

A tool for tracking memory allocation based ld-preload

libmallocTrace A tool for tracking memory allocation based ld-preload how to build make cd example && make how to use a simple way is to execute some

C Program to input a string and adjust memory allocation according to the length of the string.

C-String C Program to input a string and adjust memory allocation according to the length of the string. With the help of this program, we have replic

Easy to integrate memory allocation library for Direct3D 12

D3D12 Memory Allocator Easy to integrate memory allocation library for Direct3D 12. Documentation: Browse online: D3D12 Memory Allocator (generated fr

Manual mapper that uses PTE manipulation, Virtual Address Descriptor (VAD) manipulation, and forceful memory allocation to hide executable pages. (VAD hide / NX bit swapping)
Manual mapper that uses PTE manipulation, Virtual Address Descriptor (VAD) manipulation, and forceful memory allocation to hide executable pages. (VAD hide / NX bit swapping)

Stealthy Kernel-mode Injector Manual mapper that uses PTE manipulation, Virtual Address Descriptor (VAD) manipulation, and forceful memory allocation

C++ library to create dynamic structures in plain memory of shared-memory segments

Ищи описание на хабре @mrlolthe1st. #define _CRT_SECURE_NO_WARNINGS #include "shared_structures.h" #include iostream #include fstream #include ca

An implementation of a Windows loader that can load dynamic-linked libraries (DLLs) directly from memory

memory-module-loader memory-module-loader is an implementation of a Windows loader that can load dynamic-link libraries (DLLs) directly from memory. T

FastDynamicCast - Fast dynamic cast in C++ for MSVC, outperforming the regular dynamic cast by up to 25 times
FastDynamicCast - Fast dynamic cast in C++ for MSVC, outperforming the regular dynamic cast by up to 25 times

Fast dynamic cast This is a single header, dynamic cast implementation which outperforms the regular dynamic_cast by up to 25 times. Works on MSVC 201

Minimal Linux Live (MLL) is a tiny educational Linux distribution, which is designed to be built from scratch by using a collection of automated shell scripts. Minimal Linux Live offers a core environment with just the Linux kernel, GNU C library, and Busybox userland utilities.
Minimal Linux Live (MLL) is a tiny educational Linux distribution, which is designed to be built from scratch by using a collection of automated shell scripts. Minimal Linux Live offers a core environment with just the Linux kernel, GNU C library, and Busybox userland utilities.

Minimal Linux Live (MLL) is a tiny educational Linux distribution, which is designed to be built from scratch by using a collection of automated shell scripts. Minimal Linux Live offers a core environment with just the Linux kernel, GNU C library, and Busybox userland utilities.

Inter-process communication library to enable allocation between processes/threads and send/receive of allocated regions between producers/consumer processes or threads using this ipc buffer.

This is a relatively simple IPC buffer that allows multiple processes and threads to share a dynamic heap allocator, designate "channels" between processes, and share that memory between producer/consumer pairs on those channels.

C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)
C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)

Continuable is a C++14 library that provides full support for: lazy async continuation chaining based on callbacks (then) and expression templates, ca

Owner
Felipe da Silva
C programmer (16 years) and Biologist at Federal University of Minas Gerais (UFMG). Migrating account…
Felipe da Silva
Exploit allowing to load arbitrary code on the PSX using only a memory card (no game needed)

FreePSXBoot Exploit allowing to load arbitrary code on the PSX (i.e. PlayStation 1) using only a memory card (no game needed). In other words, it's a

null 399 Jan 2, 2023
A third party program to change Minecraft RTX's settings externally, directly in-memory.

RenderBender A third party program to change Minecraft RTX's settings externally, directly in-memory. Get the latest release here. About RenderBender

Jesse Daems 20 Dec 19, 2022
Multiplayer Voxel Survival in C99, includes a badly written implementation of LISP

Imagine a mix between Minecraft, Quake ]I[ and Emacs. Apart from that there is no clear plan for this game, just a bunch of ideas that hopefully will turn out to be fun. Some of these are stored here in this repo, others have been talked about on Twitch during dev streams.

Ben 79 Nov 21, 2022
An open source re-implementation of RollerCoaster Tycoon 2 🎢

An open source re-implementation of RollerCoaster Tycoon 2 ??

OpenRCT2 11.3k Jan 3, 2023
Game Of Life Implementation in C using Raylib

Game of life Rules The game evolution is determined by simple rules applied on each cells. Any live cell with fewer than 2 live Neighbors dies. Underp

Jonas STIRNEMANN 2 Oct 28, 2021
An open source implementation of the dark souls 3 game server.

What is this project? An open source implementation of the dark souls 3 game server. Idealistically made for the purpose of allow better alternatives

Tim Leonard 547 Dec 30, 2022
A lightweight but complete ECS implementation for modern C++.

ECS A lightweight but complete ECS implementation for modern C++. Features Cache friendly design implemented on top of an EnTT-like sparse set. Clean,

null 7 Sep 3, 2022
Simple implementation of Tic-Tac-Toe game on C++.

Simple implementation of Tic-Tac-Toe game on C++.

null 1 Dec 28, 2021
A generic game loop implementation in C++

lasso A generic game loop implementation in C++ based on Fix Your Timestep! lasso is at early stages of design and development and is subject to incom

Gabriel Galli 8 Jul 19, 2022
Tic Tac Toe implementation using c programming language with minimax algorithm.

Tic Tac Toe implementation using c programming language with minimax algorithm.

Sehan Weerasekara 1 Feb 19, 2022