From f31e02ac80ab5d72d467aef225e8b2b595df909a Mon Sep 17 00:00:00 2001 From: xolatile Date: Wed, 22 Nov 2023 14:37:39 -0500 Subject: [PATCH] World collision boundaries... --- chapter/chapter_5.c | 132 ++++++++++++++++++++++++++-------------------------- chapter/chapter_5.h | 15 +++--- 2 files changed, 74 insertions(+), 73 deletions(-) diff --git a/chapter/chapter_5.c b/chapter/chapter_5.c index 794b624..1ae05dc 100644 --- a/chapter/chapter_5.c +++ b/chapter/chapter_5.c @@ -15,15 +15,17 @@ It is distributed in the hope that it will be useful or harmful, it really depen So, what are actually getters and setters, and why you should never use them? Lets explain. */ +#define UNUSED(variable) (void) variable + #define MEMORY_LIMIT (1024 * 1024) -static void generate_fill_function (world_t * world, number_t width, number_t height, number_t x, number_t y, block_t * block) { +static procedure_t generate_full_fill_function (world_t * world, number_t width, number_t height, number_t x, number_t y, block_t * block) { number_t i, j; - (void) width; - (void) height; - (void) x; - (void) y; + UNUSED (width); + UNUSED (height); + UNUSED (x); + UNUSED (y); for (j = 0; j < world->height; ++j) { for (i = 0; i < world->width; ++i) { @@ -32,7 +34,7 @@ static void generate_fill_function (world_t * world, number_t width, number_t he } } -static void generate_rectangle_function (world_t * world, number_t width, number_t height, number_t x, number_t y, block_t * block) { +static procedure_t generate_rectangle_fill_function (world_t * world, number_t width, number_t height, number_t x, number_t y, block_t * block) { number_t i, j; for (j = 0; j < height; ++j) { @@ -42,6 +44,20 @@ static void generate_rectangle_function (world_t * world, number_t width, number } } +static procedure_t generate_rectangle_line_function (world_t * world, number_t width, number_t height, number_t x, number_t y, block_t * block) { + number_t o; + + for (o = 0; o < width; ++o) { + world->block [world->width * y + o + x] = block; + world->block [world->width * (height - 1 + y) + o + x] = block; + } + + for (o = 0; o < height; ++o) { + world->block [world->width * (y + o) + x] = block; + world->block [world->width * (y + o) + width - 1 + x] = block; + } +} + memory_t memorize (number_t size) { static string_t memory_store [MEMORY_LIMIT] = { 0 }; static number_t memory_count = 0; @@ -184,8 +200,6 @@ world_t * game_world (number_t width, number_t height, ...) { number_t w, h, x, y; block_t * block; - //~number_t i, j; - world = memorize ((number_t) sizeof (* world)); world->width = width; @@ -198,7 +212,7 @@ world_t * game_world (number_t width, number_t height, ...) { for (;;) { generator = (generator_t *) va_arg (list, void *); - if (generator != (generator_t *) 0XDEADBEEF) { + if (generator != NULL) { w = (number_t) va_arg (list, int); h = (number_t) va_arg (list, int); x = (number_t) va_arg (list, int); @@ -209,48 +223,40 @@ world_t * game_world (number_t width, number_t height, ...) { } else break; } - //~for (j = 0; j < height; ++j) { - //~for (i = 0; i < width; ++i) { - //~world->block [j * width + i] = terrain; - //~} - //~} - - //~world->block [6] = wall; - va_end (list); return (world); } -void game_render_attribute (attribute_t * attribute, number_t x, number_t y) { +procedure_t game_render_attribute (attribute_t * attribute, number_t x, number_t y) { curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x, y); curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y); - curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y); - curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y); - curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y); - curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x = 34, y); + curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y); + curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y); + curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y); + curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y); } -void game_render_skill (skill_t * skill, number_t x, number_t y) { +procedure_t game_render_skill (skill_t * skill, number_t x, number_t y) { curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x, y); curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y); - curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y); - curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y); - curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y); - curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x = 34, y); + curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y); + curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y); + curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y); + curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y); } -void game_render_player (player_t * player) { +procedure_t game_render_player (player_t * player) { curses_render_character ((char) player->symbol->character, player->symbol->colour, player->symbol->effect, player->x, player->y); } -void game_render_block (block_t * block, number_t x, number_t y) { +procedure_t game_render_block (block_t * block, number_t x, number_t y) { curses_render_character ((char) block->symbol->character, block->symbol->colour, block->symbol->effect, x, y); } -void game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height) { +procedure_t game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height) { number_t i, j; for (j = 0; (j < height) && (j < world->height); ++j) { @@ -260,41 +266,35 @@ void game_render_world (world_t * world, number_t x, number_t y, number_t width, } } -void play_game (void) { - player_t * player; - block_t * grass, * stone_floor, * stone_wall; - world_t * world; +procedure_t play_game (procedure_t) { + generator_t * full_fill = game_generator (generate_full_fill_function); + generator_t * rectangle_fill = game_generator (generate_rectangle_fill_function); + generator_t * rectangle_line = game_generator (generate_rectangle_line_function); - generator_t * fill, * rectangle; - attribute_t * strength, * edurance, * wisdom, * agility; - skill_t * blades, * axes, * bows, * spears, * puppet_magic, * nature_magic, * rune_magic, * charm_magic; + attribute_t * strength = game_attribute ("Strength", format_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0); + attribute_t * edurance = game_attribute ("Edurance", format_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0); + attribute_t * wisdom = game_attribute ("Wisdom", format_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0); + attribute_t * agility = game_attribute ("Agility", format_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0); - fill = game_generator (generate_fill_function); - rectangle = game_generator (generate_rectangle_function); + skill_t * blades = game_skill ("Blades", format_bundle (10, 120, 0), GAME_ACTION_SWING_BLADE, 0); + skill_t * axes = game_skill ("Axes", format_bundle (10, 120, 0), GAME_ACTION_SWING_AXE, 0); + skill_t * bows = game_skill ("Bows", format_bundle (10, 120, 0), GAME_ACTION_SHOOT_ARROW, 0); + skill_t * spears = game_skill ("Spears", format_bundle (10, 120, 0), GAME_ACTION_THROW_SPEAR, 0); + skill_t * puppet_magic = game_skill ("Puppet Magic", format_bundle (10, 120, 0), GAME_ACTION_SUMMON_PUPPET, 0); + skill_t * nature_magic = game_skill ("Nature Magic", format_bundle (10, 120, 0), GAME_ACTION_CALL_NATURE, 0); + skill_t * rune_magic = game_skill ("Rune Magic", format_bundle (10, 120, 0), GAME_ACTION_CITE_RUNE, 0); + skill_t * charm_magic = game_skill ("Charm Magic", format_bundle (10, 120, 0), GAME_ACTION_CAST_CHARM, 0); - strength = game_attribute ("Strength", format_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0); - edurance = game_attribute ("Edurance", format_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0); - wisdom = game_attribute ("Wisdom", format_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0); - agility = game_attribute ("Agility", format_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0); + player_t * player = game_player ("Riri", format_symbol ('@', COLOUR_CYAN, EFFECT_BOLD)); - blades = game_skill ("Blades", format_bundle (10, 120, 0), GAME_ACTION_SWING_BLADE, 0); - axes = game_skill ("Axes", format_bundle (10, 120, 0), GAME_ACTION_SWING_AXE, 0); - bows = game_skill ("Bows", format_bundle (10, 120, 0), GAME_ACTION_SHOOT_ARROW, 0); - spears = game_skill ("Spears", format_bundle (10, 120, 0), GAME_ACTION_THROW_SPEAR, 0); - puppet_magic = game_skill ("Puppet Magic", format_bundle (10, 120, 0), GAME_ACTION_SUMMON_PUPPET, 0); - nature_magic = game_skill ("Nature Magic", format_bundle (10, 120, 0), GAME_ACTION_CALL_NATURE, 0); - rune_magic = game_skill ("Rune Magic", format_bundle (10, 120, 0), GAME_ACTION_CITE_RUNE, 0); - charm_magic = game_skill ("Charm Magic", format_bundle (10, 120, 0), GAME_ACTION_CAST_CHARM, 0); + block_t * grass = game_block ("Grass", format_symbol (',', COLOUR_GREEN, EFFECT_BOLD), FALSE, FALSE); + block_t * stone_floor = game_block ("Stone Floor", format_symbol ('.', COLOUR_GREY, EFFECT_BOLD), FALSE, FALSE); + block_t * stone_wall = game_block ("Stone Wall", format_symbol ('#', COLOUR_GREY, EFFECT_BOLD), TRUE, FALSE); - player = game_player ("Riri", format_symbol ('@', COLOUR_CYAN, EFFECT_BOLD)); - - grass = game_block ("Grass", format_symbol (',', COLOUR_GREEN, EFFECT_BOLD), FALSE, FALSE); - stone_floor = game_block ("Stone Floor", format_symbol ('.', COLOUR_GREY, EFFECT_BOLD), FALSE, FALSE); - stone_wall = game_block ("Stone Wall", format_symbol ('#', COLOUR_GREY, EFFECT_BOLD), TRUE, FALSE); - - world = game_world (80, 24, fill, 0, 0, 0, 0, grass, - rectangle, 20, 10, 2, 4, stone_wall, - (generator_t *) 0XDEADBEEF); + world_t * world = game_world (80, 24, full_fill, 0, 0, 0, 0, grass, + rectangle_fill, 5, 9, 2, 4, stone_floor, + rectangle_line, 5, 9, 2, 4, stone_wall, + NULL); curses_configure (); @@ -319,16 +319,16 @@ void play_game (void) { game_render_skill (rune_magic, 2, 12); game_render_skill (charm_magic, 2, 13); - game_render_world (world, 40, 0, curses_screen_width, curses_screen_height); + game_render_world (world, 0, 0, curses_screen_width, curses_screen_height); game_render_player (player); switch (curses_character) { - case SIGNAL_ARROW_UP: player->y -= 1; break; - case SIGNAL_ARROW_DOWN: player->y += 1; break; - case SIGNAL_ARROW_LEFT: player->x -= 1; break; - case SIGNAL_ARROW_RIGHT: player->x += 1; break; - default: break; + case SIGNAL_ARROW_UP: player->y -= 1; limit (& player->y, 0, world->height - 1); break; + case SIGNAL_ARROW_DOWN: player->y += 1; limit (& player->y, 0, world->height - 1); break; + case SIGNAL_ARROW_LEFT: player->x -= 1; limit (& player->x, 0, world->width - 1); break; + case SIGNAL_ARROW_RIGHT: player->x += 1; limit (& player->x, 0, world->width - 1); break; + default: break; } curses_synchronize (); diff --git a/chapter/chapter_5.h b/chapter/chapter_5.h index fee3317..ed57e7b 100644 --- a/chapter/chapter_5.h +++ b/chapter/chapter_5.h @@ -26,6 +26,7 @@ First of all, lets talk briefly about keyword 'typedef' and why I hate to use it */ typedef int number_t; +typedef void procedure_t; typedef char * string_t; typedef void * memory_t; @@ -81,7 +82,7 @@ typedef struct world_t { block_t * * block; } world_t; -typedef void (* generate_t) (world_t *, number_t, number_t, number_t, number_t, block_t *); +typedef procedure_t (* generate_t) (world_t *, number_t, number_t, number_t, number_t, block_t *); typedef struct generator_t { generate_t generate; @@ -99,12 +100,12 @@ extern player_t * game_player (string_t name, symbol_t * symbol); extern block_t * game_block (string_t name, symbol_t * symbol, number_t collision, number_t override); extern world_t * game_world (number_t width, number_t height, ...); -extern void game_render_attribute (attribute_t * attribute, number_t x, number_t y); -extern void game_render_skill (skill_t * skill, number_t x, number_t y); -extern void game_render_player (player_t * player); -extern void game_render_block (block_t * block, number_t x, number_t y); -extern void game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height); +extern procedure_t game_render_attribute (attribute_t * attribute, number_t x, number_t y); +extern procedure_t game_render_skill (skill_t * skill, number_t x, number_t y); +extern procedure_t game_render_player (player_t * player); +extern procedure_t game_render_block (block_t * block, number_t x, number_t y); +extern procedure_t game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height); -extern void play_game (void); +extern procedure_t play_game (procedure_t); #endif