Skip to content

Commit 05e24c6

Browse files
committed
Initial start on improved monster pathing
The goal is to use the A* algorithm when pathing monsters to make them smarter. Just need some tools to allow for the algorithm to perform better.
1 parent d08b770 commit 05e24c6

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

src/monster.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,13 @@ monster_drunk_walk(Monster *m, RoomMatrix *rm)
374374
static Direction
375375
get_optimal_move_towards(Monster *m, RoomMatrix *rm, const Position *dest)
376376
{
377+
// TODO(Linus): Replace with A*
378+
377379
int x_dist, y_dist;
378-
Position mPos;
380+
const Position pPos = rm->playerRoomPos;
381+
Position mPos = position_to_matrix_coords(&m->sprite->pos);
379382

380-
mPos = position_to_matrix_coords(&m->sprite->pos);
383+
// TODO(Linus): Add a heap to track options
381384

382385
unsigned int currentScore = 100;
383386
unsigned int chosenDirection = UP;

src/pos_heap.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* BreakHack - A dungeone crawler RPG
3+
* Copyright (C) 2025 Linus Probert <[email protected]>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#include "pos_heap.h"
19+
20+
void pheap_init(PHeap *ph)
21+
{
22+
}
23+
24+
25+
void pheap_insert(PHeap *ph, Position p)
26+
{
27+
}
28+
29+
void pheap_delete(PHeap *ph, Position p)
30+
{
31+
}
32+
33+
Position pheap_peek(PHeap *ph)
34+
{
35+
return ph->pos[0];
36+
}
37+

src/pos_heap.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* BreakHack - A dungeone crawler RPG
3+
* Copyright (C) 2025 Linus Probert <[email protected]>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#ifndef _POS_HEAP_H_
19+
#define _POS_HEAP_H_
20+
21+
#include "position.h"
22+
#include <stddef.h>
23+
24+
typedef struct PHeap {
25+
Position *pos;
26+
size_t size;
27+
size_t capacity;
28+
} PHeap;
29+
30+
/**
31+
* Initialize a PosHeap
32+
*
33+
* \param ph The heap to intialize
34+
*/
35+
void pheap_init(PHeap *ph);
36+
37+
/**
38+
* Insert a new position into the heap
39+
*
40+
* \param ph The heap to modify
41+
* \param p The value to insert
42+
*/
43+
void pheap_insert(PHeap *ph, Position p);
44+
45+
/**
46+
* Delete a position from a heap
47+
*
48+
* \param ph The heap to modify
49+
* \param p The position to delete
50+
*/
51+
void pheap_delete(PHeap *ph, Position p);
52+
53+
/**
54+
* Peek the first position in the heap
55+
*
56+
* Peek the top position in the heap. Peeking an empty heap is undefined
57+
* behaviour.
58+
*
59+
* \param ph The heap to peek
60+
* \returns The top element in the heap
61+
*/
62+
Position pheap_peek(PHeap *ph);
63+
64+
#endif // _POS_HEAP_H_
65+

0 commit comments

Comments
 (0)