From: Niki Roo Date: Tue, 15 Apr 2025 12:28:02 +0000 (+0200) Subject: new setup system X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=e730758fbcd907093b30c14150d879f2fd83a826;p=tdef.git new setup system --- diff --git a/src/tdef/engine.c b/src/tdef/engine.c index e2f23a3..bee6ddd 100644 --- a/src/tdef/engine.c +++ b/src/tdef/engine.c @@ -162,52 +162,3 @@ tower_base_t *engine_get_tower_base(engine_t *self, int type) { return NULL; } -tower_base_t *setup_tower_base(engine_t *self, int type, int cost) { - tower_base_t *base = engine_get_tower_base(self, type); - if (base) // already configured - return NULL; - - base = array_new(self->tbases); - init_tower_base(base, type); - base->cost = cost; - return base; -} - -int setup_wave(engine_t *self, int start_tick, int bits) { - wave_t *wave = array_new(self->waves); - if (!wave) - return 0; - - init_wave(wave, start_tick); - wave->bits = bits; - - return 1; -} - -enemy_t *setup_enemy(engine_t *self, int type, int id, size_t start_tick) { - if (!self->waves->count) - if (!setup_wave(self, 0, 0)) - return 0; - - //TODO: type and eney_base_t - - wave_t *wave = array_last(self->waves); - return wave_add_enemy(wave, id, start_tick); -} - -int setup_path(engine_t *self, int x, int y) { - if (x < 0 || y < 0) - return 0; - if (x >= self->map->width || y >= self->map->height) - return 0; - if (self->map->data[y * self->map->width + x]) - return 0; - - path_t *loc = array_new(self->map->paths); - init_path(loc, x, y); - loc->index = self->map->paths->count - 1; - - self->map->data[y * self->map->width + x] = path2any(loc); - return 1; -} - diff --git a/src/tdef/engine.h b/src/tdef/engine.h index 0de52a1..cd97f02 100644 --- a/src/tdef/engine.h +++ b/src/tdef/engine.h @@ -87,13 +87,5 @@ int engine_add_tower(engine_t *self, int type, int x, int y); tower_base_t *engine_get_tower_base(engine_t *self, int type); -tower_base_t *setup_tower_base(engine_t *self, int type, int cost); - -int setup_wave(engine_t *self, int start_tick, int bits); - -enemy_t *setup_enemy(engine_t *self, int type, int id, size_t start_tick); - -int setup_path(engine_t *self, int x, int y); - #endif /* ENGINE_H */ diff --git a/src/tdef/map.c b/src/tdef/map.c index 811a735..5cf7a17 100644 --- a/src/tdef/map.c +++ b/src/tdef/map.c @@ -97,6 +97,25 @@ void uninit_map(map_t *self) { } } +int map_add_path(map_t *self, int x, int y) { + if (x < 0 || y < 0) + return 0; + if (x >= self->width || y >= self->height) + return 0; + if (self->data[y * self->width + x]) + return 0; + + path_t *loc = array_new(self->paths); + if (!loc) + return 0; + + init_path(loc, x, y); + loc->index = self->paths->count - 1; + + self->data[y * self->width + x] = path2any(loc); + return 1; +} + void map_enemy_enters(map_t *self, enemy_t *enemy, array_t *events) { enemy->alive = 1; array_push(self->alive, &enemy); diff --git a/src/tdef/map.h b/src/tdef/map.h index a6f0416..b8e83c2 100644 --- a/src/tdef/map.h +++ b/src/tdef/map.h @@ -79,6 +79,8 @@ void free_map(map_t *self); */ void uninit_map(map_t *self); +int map_add_path(map_t *self, int x, int y); + void map_enemy_enters(map_t *self, enemy_t *enemy, array_t *events); void map_move_1(map_t *self, array_t *events); diff --git a/src/tdef/setup.c b/src/tdef/setup.c new file mode 100644 index 0000000..c75989a --- /dev/null +++ b/src/tdef/setup.c @@ -0,0 +1,62 @@ +/* + * TDef: small tower defense game + * + * Copyright (C) 2025 Niki Roo + * + * 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 . + */ + +#include +#include + +#include "setup.h" + +tower_base_t *setup_tower_base(engine_t *self, int type, int cost) { + tower_base_t *base = engine_get_tower_base(self, type); + if (base) // already configured + return NULL; + + base = array_new(self->tbases); + init_tower_base(base, type); + base->cost = cost; + + return base; +} + +int setup_wave(engine_t *self, int start_tick, int bits) { + wave_t *wave = array_new(self->waves); + if (!wave) + return 0; + + init_wave(wave, start_tick); + wave->bits = bits; + + return 1; +} + +enemy_t *setup_enemy(engine_t *self, int type, int id, size_t start_tick) { + if (!self->waves->count) + if (!setup_wave(self, 0, 0)) + return 0; + + //TODO: type and eney_base_t + + wave_t *wave = array_last(self->waves); + return wave_add_enemy(wave, id, start_tick); +} + +int setup_path(engine_t *self, int x, int y) { + return map_add_path(self->map, x, y); +} + diff --git a/src/tdef/setup.h b/src/tdef/setup.h new file mode 100644 index 0000000..fc74b98 --- /dev/null +++ b/src/tdef/setup.h @@ -0,0 +1,25 @@ +/** + * @file setup.h + * @author Niki + * @date 2025 + * + * @brief bla + * + * blablabla + * + */ + +#ifndef SETUP_H +#define SETUP_H + +#include "engine.h" + +tower_base_t *setup_tower_base(engine_t *self, int type, int cost); + +int setup_wave(engine_t *self, int start_tick, int bits); + +enemy_t *setup_enemy(engine_t *self, int type, int id, size_t start_tick); + +int setup_path(engine_t *self, int x, int y); + +#endif /* SETUP_H */ diff --git a/src/tdef/tdef.c b/src/tdef/tdef.c index 14319ad..8d7cf14 100644 --- a/src/tdef/tdef.c +++ b/src/tdef/tdef.c @@ -26,6 +26,8 @@ #include "engine.h" #include "reader.h" #include "event.h" +#include "command.h" +#include "setup.h" int displayMode = 0;