summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--allocator.c21
-rw-r--r--build/zombies.exebin1679328 -> 1691324 bytes
-rw-r--r--include/allocator.h16
-rw-r--r--include/list.h5
-rw-r--r--include/pathfinding.h5
-rw-r--r--list.c33
-rw-r--r--main.c11
-rw-r--r--objects.c2
-rw-r--r--pathfinding.c57
-rw-r--r--zombies.c21
11 files changed, 107 insertions, 66 deletions
diff --git a/Makefile b/Makefile
index 9f421dc..c1e17c1 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
main:
rm -rf "build/"
mkdir -p "build/"
- gcc -m64 -g -DMODE_DEBUG main.c -o build/zombies.exe -lprojectbase-debug -Llibs/ -lSDL2 -lSDL2_mixer
+ gcc -m64 -g -DMODE_DEBUG main.c -o build/zombies.exe -lprojectbase-debug -Llibs/ -lSDL2 -lSDL2_mixer -lWs2_32
./build/zombies.exe
diff --git a/allocator.c b/allocator.c
new file mode 100644
index 0000000..aaa2197
--- /dev/null
+++ b/allocator.c
@@ -0,0 +1,21 @@
+#include "include/allocator.h"
+
+allocator create_allocator(uint64_t size) {
+ allocator allocator;
+ allocator.cursor = 0;
+ allocator.size = size;
+ allocator.memory = mem_alloc(size);
+ return allocator;
+}
+
+void* allocator_alloc(allocator* al, uint64_t size) {
+ if (al->cursor + size < al->size) {
+ al->cursor += size;
+ return al->memory + al->cursor - size;
+ }
+ log_assert(0, "Allocator out of space");
+}
+
+void destroy_allocator(allocator* al) {
+ mem_free(al->memory);
+} \ No newline at end of file
diff --git a/build/zombies.exe b/build/zombies.exe
index 6c42ba3..0c77b32 100644
--- a/build/zombies.exe
+++ b/build/zombies.exe
Binary files differ
diff --git a/include/allocator.h b/include/allocator.h
new file mode 100644
index 0000000..d1fe9f4
--- /dev/null
+++ b/include/allocator.h
@@ -0,0 +1,16 @@
+#ifndef INCLUDE_ALLOCATOR
+#define INCLUDE_ALLOCATOR
+
+#include <projectbase/project_base.h>
+
+typedef struct t_allocator {
+ void* memory;
+ uint64_t cursor;
+ uint64_t size;
+} allocator;
+
+allocator create_allocator(uint64_t size);
+void* allocator_alloc(allocator* al, uint64_t size);
+void destroy_allocator(allocator* al);
+
+#endif \ No newline at end of file
diff --git a/include/list.h b/include/list.h
index d4809de..41a5705 100644
--- a/include/list.h
+++ b/include/list.h
@@ -4,6 +4,8 @@
#include <projectbase/project_base.h>
+#include "allocator.h"
+
typedef struct t_list_item
{
void *next;
@@ -15,10 +17,11 @@ typedef struct t_list
list_item *start;
u32 count;
u16 size;
+ allocator* al;
} list;
void list_destroy(list* list);
-list list_create(u16 size);
+list list_create(u16 size, allocator* al);
list_item *list_push(list *list, void *data);
void* list_at(list *list, u32 index);
void list_remove_at(list *list, u32 index);
diff --git a/include/pathfinding.h b/include/pathfinding.h
index 2b38766..6bef101 100644
--- a/include/pathfinding.h
+++ b/include/pathfinding.h
@@ -3,6 +3,7 @@
#include <projectbase/project_base.h>
+#include "allocator.h"
#include "players.h"
#include "objects.h"
#include "list.h"
@@ -12,8 +13,8 @@ typedef struct t_pathfinding_request
vec2f start;
vec2f end;
array *to_fill;
- bool cancelled;
- bool done;
+ uint64_t timestamp;
+ mutex mutex;
} pathfinding_request;
array global_pathfinding_queue;
diff --git a/list.c b/list.c
index 35c41eb..f186285 100644
--- a/list.c
+++ b/list.c
@@ -1,48 +1,24 @@
-/*
-* Copyright 2019 Aldrik Ramaekers
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
-
#include "include/list.h"
-list list_create(u16 size)
+list list_create(u16 size, allocator* al)
{
list l;
l.size = size;
l.count = 0;
l.start = 0;
+ l.al = al;
return l;
}
void list_destroy(list* list) {
- list_item *prev = 0;
- list_item *current_item = list->start;
- while(current_item)
- {
- prev = current_item;
- current_item = current_item->next;
- mem_free(prev);
- }
-
+ list->start = 0;
list->count = 0;
}
list_item *list_push(list *list, void *data)
{
- list_item *item = mem_alloc(sizeof(list_item));
+ list_item *item = allocator_alloc(list->al, sizeof(list_item));
item->next = 0;
item->data = data;
@@ -100,7 +76,6 @@ void list_remove_at(list *list, u32 index)
else
list->start = current_item->next;
- mem_free(current_item);
goto done;
}
diff --git a/main.c b/main.c
index ccdfc91..8985596 100644
--- a/main.c
+++ b/main.c
@@ -8,6 +8,7 @@
#include "include/bullets.h"
#include "include/pathfinding.h"
#include "include/list.h"
+#include "include/allocator.h"
#include "map.c"
#include "players.c"
@@ -17,14 +18,20 @@
#include "math_helper.c"
#include "pathfinding.c"
#include "list.c"
+#include "allocator.c"
#define CONFIG_DIRECTORY "zombieshooter"
+font* fnt;
void update_func(platform_window* window) {
renderer->render_clear(window, rgb(0,255,0));
draw_grid(window);
draw_spawners(window);
+
+ char buf[200];
+ sprintf(buf, "QUEUE: %d", global_pathfinding_queue.length);
+ renderer->render_text(fnt, _global_camera.x, _global_camera.y, buf, rgb(255,255,255));
}
void resize_func(platform_window* window, u32 change_x,u32 change_y) {
@@ -43,12 +50,14 @@ int main(int argc, char **argv)
settings_set_number("USE_GPU", 1);
platform_toggle_vsync(window, true);
- //fnt = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 16);
+ fnt = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 16);
load_map_from_data();
create_objects();
pathfinding_init();
+ //networking_create_server();
+
thread t = thread_start(pathfinding_thread, 0);
thread_detach(&t);
diff --git a/objects.c b/objects.c
index 72af5a5..a787d7b 100644
--- a/objects.c
+++ b/objects.c
@@ -87,5 +87,5 @@ void create_objects() {
create_box(11, 10, 0);
spawn_player(my_id);
- spawn_player(my_id+1);
+ //spawn_player(my_id+1);
} \ No newline at end of file
diff --git a/pathfinding.c b/pathfinding.c
index b69d420..7196b2c 100644
--- a/pathfinding.c
+++ b/pathfinding.c
@@ -17,6 +17,9 @@ bool can_walk_at(float x, float y)
bool find_path_to(vec2f start_pos, vec2f end_pos, array *to_fill, pathfinding_request* request)
{
+ uint64_t timestamp = platform_get_time(TIME_PROCESS, TIME_MS);
+ allocator al = create_allocator(500000);
+
struct path_node {
struct path_node *parent;
vec2f position;
@@ -25,10 +28,10 @@ bool find_path_to(vec2f start_pos, vec2f end_pos, array *to_fill, pathfinding_re
s32 f;
};
- list open_array = list_create(sizeof(struct path_node));
- list closed_array = list_create(sizeof(struct path_node));
+ list open_array = list_create(sizeof(struct path_node), &al);
+ list closed_array = list_create(sizeof(struct path_node), &al);
- struct path_node *start_node = mem_alloc(sizeof(struct path_node));
+ struct path_node *start_node = allocator_alloc(&al, sizeof(struct path_node));
start_node->g = 0;
start_node->h = 0;
start_node->f = 0;
@@ -38,7 +41,7 @@ bool find_path_to(vec2f start_pos, vec2f end_pos, array *to_fill, pathfinding_re
list_push(&open_array, (uint8_t*)start_node);
struct path_node *current_node = 0;
- while(open_array.count > 0 && !request->cancelled)
+ while(open_array.count > 0)
{
// Get the current node
current_node = list_at(&open_array, 0);
@@ -63,6 +66,8 @@ bool find_path_to(vec2f start_pos, vec2f end_pos, array *to_fill, pathfinding_re
// Found the goal
if (distance_between(current_node->position, end_pos) <= 0)
{
+ mutex_lock(&request->mutex);
+
if (to_fill)
to_fill->length = 0;
struct path_node *current = current_node;
@@ -79,23 +84,25 @@ bool find_path_to(vec2f start_pos, vec2f end_pos, array *to_fill, pathfinding_re
struct path_node *prev = current;
current = current->parent;
- mem_free(prev);
}
if (to_fill) {
array_remove_at(to_fill, to_fill->length-1);
- request->done = true;
+ printf("PATHFINDING TOOK: %fms\n", (platform_get_time(TIME_PROCESS, TIME_MS)-timestamp)/1000.0f);
}
+ mutex_unlock(&request->mutex);
list_destroy(&open_array);
list_destroy(&closed_array);
-
+
+ destroy_allocator(&al);
+
return true;
}
vec2 adjecent[8] = { {0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1} };
- list children = list_create(sizeof(struct path_node));
+ list children = list_create(sizeof(struct path_node), &al);
// Generate children
for (s32 i = 0; i < 8; i++)
@@ -133,7 +140,7 @@ bool find_path_to(vec2f start_pos, vec2f end_pos, array *to_fill, pathfinding_re
}
// Create new node
- struct path_node *new_node = mem_alloc(sizeof(struct path_node));
+ struct path_node *new_node = allocator_alloc(&al, sizeof(struct path_node));
new_node->g = 0;
new_node->h = 0;
new_node->f = 0;
@@ -204,7 +211,8 @@ bool find_path_to(vec2f start_pos, vec2f end_pos, array *to_fill, pathfinding_re
list_remove_at(&open_array, current_index);
}
-
+
+ destroy_allocator(&al);
return false;
}
@@ -262,19 +270,13 @@ void* pathfinding_thread(void *args)
{
while(1)
{
- pathfinding_request request;
-
mutex_lock(&global_pathfinding_queue.mutex);
if (global_pathfinding_queue.length)
{
pathfinding_request* request = *(pathfinding_request**)array_at(&global_pathfinding_queue, 0);
array_remove_at(&global_pathfinding_queue, 0);
mutex_unlock(&global_pathfinding_queue.mutex);
-
- request->cancelled = false;
find_path_to(request->start, request->end, request->to_fill, request);
-
- continue;
}
else
{
@@ -282,7 +284,7 @@ void* pathfinding_thread(void *args)
continue;
}
- thread_sleep(100);
+ //thread_sleep(100);
}
return 0;
@@ -290,19 +292,28 @@ void* pathfinding_thread(void *args)
void make_pathfinding_request(vec2f start, vec2f end, array *to_fill, pathfinding_request *request)
{
- mutex_lock(&global_pathfinding_queue.mutex);
-
- request->cancelled = true;
-
start.x = (int)start.x;
start.y = (int)start.y;
end.x = (int)end.x;
end.y = (int)end.y;
-
+
+ mutex_lock(&global_pathfinding_queue.mutex);
+ for (int i = 0; i < global_pathfinding_queue.length; i++) {
+ pathfinding_request* ereq = *(pathfinding_request**)array_at(&global_pathfinding_queue, i);
+ if (request == ereq) {
+ ereq->start = start;
+ ereq->end = end;
+ ereq->timestamp = platform_get_time(TIME_PROCESS, TIME_MS);
+ mutex_unlock(&global_pathfinding_queue.mutex);
+ return;
+ }
+ }
+
request->start = start;
request->end = end;
request->to_fill = to_fill;
- request->done = false;
+
+ request->timestamp = 0;
array_push(&global_pathfinding_queue, (uint8_t*)&request);
mutex_unlock(&global_pathfinding_queue.mutex);
} \ No newline at end of file
diff --git a/zombies.c b/zombies.c
index 85dea2d..76a6e1b 100644
--- a/zombies.c
+++ b/zombies.c
@@ -35,7 +35,8 @@ void spawn_zombie(int x, int y) {
zombies[i].position = (vec3f){x,y, 0};
zombies[i].size = (vec3f){0.4, 0.4, 1};
zombies[i].time_since_last_path = 0.0f;
-
+ zombies[i].request.mutex = mutex_create();
+
player closest_player = get_closest_player_to_tile(x, y);
make_pathfinding_request((vec2f){x,y}, (vec2f){closest_player.playerx, closest_player.playery}, &zombies[i].path, &zombies[i].request);
@@ -46,7 +47,7 @@ void spawn_zombie(int x, int y) {
void draw_spawners(platform_window* window) {
map_info info = get_map_info(window);
- for (int x = 0; x < 2; x++) {
+ for (int x = 0; x < 1; x++) {
spawner spawner = spawner_tiles[x];
int render_x = (info.tile_width * spawner.position.x) + (info.px_incline * spawner.position.y);
int render_y = info.tile_height * spawner.position.y;
@@ -60,7 +61,7 @@ void draw_spawners(platform_window* window) {
rgb(100, 150, 50));
spawner_tiles[x].sec_since_last_spawn += update_delta;
- if (spawner_tiles[x].sec_since_last_spawn >= 3.0f) {
+ if (spawner_tiles[x].sec_since_last_spawn >= 2.0f) {
spawn_zombie(spawner.position.x, spawner.position.y);
spawner_tiles[x].sec_since_last_spawn = 0;
}
@@ -132,7 +133,7 @@ void draw_zombies_at_tile(platform_window* window, int x, int y) {
if ((int)o.position.x != x || (int)ceil(o.position.y) != y) continue;
zombies[i].time_since_last_path += update_delta;
- if (zombies[i].time_since_last_path > 0.1f) {
+ if (zombies[i].time_since_last_path > 0.05f) {
player closest_player = get_closest_player_to_tile(x, y);
make_pathfinding_request((vec2f){o.position.x,o.position.y},
(vec2f){closest_player.playerx, closest_player.playery + get_player_size_in_tile()},
@@ -140,11 +141,15 @@ void draw_zombies_at_tile(platform_window* window, int x, int y) {
zombies[i].time_since_last_path = 0;
}
else {
- if (zombies[i].request.done) {
- zombies[i].path = zombies[i].next_path;
- array_clear(&zombies[i].next_path);
- zombies[i].request.done = false;
+ if (mutex_trylock(&zombies[i].request.mutex))
+ {
+ if (zombies[i].request.to_fill->length) {
+ array_destroy(&zombies[i].path);
+ zombies[i].path = array_copy(&zombies[i].next_path);
+ array_clear(&zombies[i].next_path);
+ }
}
+ mutex_unlock(&zombies[i].request.mutex);
}
vec2f dir = get_direction_to_next_tile(o);