From b8237f23ca5cbded82b177e22dd14b01dd37ef65 Mon Sep 17 00:00:00 2001 From: xolatile Date: Thu, 23 Nov 2023 15:23:23 -0500 Subject: [PATCH] Splitting chapter 5 into 6... --- chapter/chapter_5.c | 176 ++++++++-------------------------------------------- chapter/chapter_5.h | 3 +- chapter/chapter_6.c | 124 ++++++++++++++++++++++++++++++++++++ chapter/chapter_6.h | 20 ++++++ compile.sh | 9 ++- xhartae.c | 2 + 6 files changed, 182 insertions(+), 152 deletions(-) create mode 100644 chapter/chapter_6.c create mode 100644 chapter/chapter_6.h diff --git a/chapter/chapter_5.c b/chapter/chapter_5.c index 024b869..1addb73 100644 --- a/chapter/chapter_5.c +++ b/chapter/chapter_5.c @@ -15,49 +15,8 @@ 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 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; - - UNUSED (width); - UNUSED (height); - UNUSED (x); - UNUSED (y); - - for (j = 0; j < world->height; ++j) { - for (i = 0; i < world->width; ++i) { - world->block [j * world->width + i] = 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) { - for (i = 0; i < width; ++i) { - world->block [(j + y) * world->width + (i + x)] = block; - } - } -} - -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; @@ -69,10 +28,12 @@ memory_t memorize (number_t size) { return ((memory_t) ((string_t) memory_store + memory_count - size)); } -bundle_t * format_bundle (number_t minimum, number_t maximum, number_t current) { - bundle_t * bundle; +number_t randomize (number_t lower, number_t upper) { + return ((number_t) rand () % (upper - lower) + lower); +} - bundle = memorize ((number_t) sizeof (* bundle)); +bundle_t * format_bundle (number_t minimum, number_t maximum, number_t current) { + bundle_t * bundle = memorize ((number_t) sizeof (* bundle)); bundle->minimum = minimum; bundle->maximum = maximum; @@ -82,9 +43,7 @@ bundle_t * format_bundle (number_t minimum, number_t maximum, number_t current) } symbol_t * format_symbol (number_t character, number_t colour, number_t effect) { - symbol_t * symbol; - - symbol = memorize ((number_t) sizeof (* symbol)); + symbol_t * symbol = memorize ((number_t) sizeof (* symbol)); symbol->character = character; symbol->colour = colour; @@ -94,9 +53,7 @@ symbol_t * format_symbol (number_t character, number_t colour, number_t effect) } generator_t * game_generator (generate_t function) { - generator_t * generator; - - generator = memorize ((number_t) sizeof (* generator)); + generator_t * generator = memorize ((number_t) sizeof (* generator)); generator->generate = function; @@ -104,10 +61,9 @@ generator_t * game_generator (generate_t function) { } attribute_t * game_attribute (string_t name, bundle_t * points, ...) { - attribute_t * attribute; - va_list list; + va_list list; - attribute = memorize ((number_t) sizeof (* attribute)); + attribute_t * attribute = memorize ((number_t) sizeof (* attribute)); string_copy ((attribute->name = memorize (string_length (name) + 1)), name); @@ -135,10 +91,9 @@ attribute_t * game_attribute (string_t name, bundle_t * points, ...) { } skill_t * game_skill (string_t name, bundle_t * points, ...) { - skill_t * skill; - va_list list; + va_list list; - skill = memorize ((number_t) sizeof (* skill)); + skill_t * skill = memorize ((number_t) sizeof (* skill)); string_copy ((skill->name = memorize (string_length (name) + 1)), name); @@ -162,9 +117,7 @@ skill_t * game_skill (string_t name, bundle_t * points, ...) { } player_t * game_player (string_t name, symbol_t * symbol) { - player_t * player; - - player = memorize ((number_t) sizeof (* player)); + player_t * player = memorize ((number_t) sizeof (* player)); string_copy ((player->name = memorize (string_length (name) + 1)), name); @@ -177,9 +130,7 @@ player_t * game_player (string_t name, symbol_t * symbol) { } block_t * game_block (string_t name, symbol_t * symbol, number_t collision, number_t override) { - block_t * block; - - block = memorize ((number_t) sizeof (* block)); + block_t * block = memorize ((number_t) sizeof (* block)); string_copy ((block->name = memorize (string_length (name) + 1)), name); @@ -192,13 +143,9 @@ block_t * game_block (string_t name, symbol_t * symbol, number_t collision, numb } world_t * game_world (number_t width, number_t height, ...) { - world_t * world; - va_list list; - generator_t * generator; - number_t w, h, x, y; - block_t * block; + va_list list; - world = memorize ((number_t) sizeof (* world)); + world_t * world = memorize ((number_t) sizeof (* world)); world->width = width; world->height = height; @@ -208,14 +155,14 @@ world_t * game_world (number_t width, number_t height, ...) { va_start (list, height); for (;;) { - generator = (generator_t *) va_arg (list, void *); + generator_t * generator = (generator_t *) va_arg (list, void *); if (generator != NULL) { - w = (number_t) va_arg (list, int); - h = (number_t) va_arg (list, int); - x = (number_t) va_arg (list, int); - y = (number_t) va_arg (list, int); - block = (block_t *) va_arg (list, void *); + number_t w = (number_t) va_arg (list, int); + number_t h = (number_t) va_arg (list, int); + number_t x = (number_t) va_arg (list, int); + number_t y = (number_t) va_arg (list, int); + block_t * block = (block_t *) va_arg (list, void *); generator->generate (world, w, h, x, y, block); } else break; @@ -227,7 +174,8 @@ world_t * game_world (number_t width, number_t height, ...) { } 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_CYAN, EFFECT_NORMAL, x + 0, y); + curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x + 3, 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 + 20, y); @@ -237,7 +185,8 @@ procedure_t game_render_attribute (attribute_t * attribute, number_t x, number_t } 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_CYAN, EFFECT_NORMAL, x + 0, y); + curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x + 3, 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 + 20, y); @@ -255,82 +204,11 @@ procedure_t game_render_block (block_t * block, number_t x, number_t y) { } 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) { - for (i = 0; (i < width) && (i < world->width); ++i) { + for (number_t j = 0; (j < height) && (j < world->height); ++j) { + for (number_t i = 0; (i < width) && (i < world->width); ++i) { game_render_block (world->block [j * world->width + i], i + x, j + y); } } } -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); - - 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); - - 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); - - player_t * player = game_player ("Riri", format_symbol ('@', COLOUR_CYAN, EFFECT_BOLD)); - - 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); - - 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, - 0); - - curses_configure (); - - while (curses_active) { - curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL); - - curses_render_string ("Attributes:", COLOUR_WHITE, EFFECT_BOLD, 80, 0); - - game_render_attribute (strength, 82, 1); - game_render_attribute (edurance, 82, 2); - game_render_attribute (wisdom, 82, 3); - game_render_attribute (agility, 82, 4); - - curses_render_string ("Skills:", COLOUR_WHITE, EFFECT_BOLD, 80, 5); - - game_render_skill (blades, 82, 6); - game_render_skill (axes, 82, 7); - game_render_skill (bows, 82, 8); - game_render_skill (spears, 82, 9); - game_render_skill (puppet_magic, 82, 10); - game_render_skill (nature_magic, 82, 11); - game_render_skill (rune_magic, 82, 12); - game_render_skill (charm_magic, 82, 13); - - 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; 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 (); - } -} - #endif diff --git a/chapter/chapter_5.h b/chapter/chapter_5.h index af2b6ef..f0a88d4 100644 --- a/chapter/chapter_5.h +++ b/chapter/chapter_5.h @@ -93,7 +93,8 @@ typedef struct generator_t { generate_t generate; } generator_t; -extern memory_t memorize (number_t size); +extern memory_t memorize (number_t size); +extern number_t randomize (number_t lower, number_t upper); extern bundle_t * format_bundle (number_t minimum, number_t maximum, number_t current); extern symbol_t * format_symbol (number_t character, number_t colour, number_t effect); diff --git a/chapter/chapter_6.c b/chapter/chapter_6.c new file mode 100644 index 0000000..3d587c6 --- /dev/null +++ b/chapter/chapter_6.c @@ -0,0 +1,124 @@ +/* +Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic + +Xhartae is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation. +And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version. +It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3. +*/ + +#ifndef CHAPTER_6_SOURCE +#define CHAPTER_6_SOURCE + +#include "chapter_6.h" + +#define UNUSED(variable) (void) variable + +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) { + UNUSED (width); + UNUSED (height); + UNUSED (x); + UNUSED (y); + + for (number_t j = 0; j < world->height; ++j) { + for (number_t i = 0; i < world->width; ++i) { + world->block [j * world->width + i] = 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) { + for (number_t j = 0; j < height; ++j) { + for (number_t i = 0; i < width; ++i) { + world->block [(j + y) * world->width + (i + x)] = block; + } + } +} + +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) { + for (number_t offset = 0; offset < width; ++offset) { + world->block [world->width * y + offset + x] = block; + world->block [world->width * (height - 1 + y) + offset + x] = block; + } + + for (number_t offset = 0; offset < height; ++offset) { + world->block [world->width * (y + offset) + x] = block; + world->block [world->width * (y + offset) + width - 1 + x] = block; + } +} + +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); + + 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); + + 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); + + player_t * player = game_player ("Riri", format_symbol ('@', COLOUR_CYAN, EFFECT_BOLD)); + + 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); + + world_t * world = game_world (300, 100, 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 (); + + terminal_show_cursor (FALSE); + + number_t game_screen_offset = curses_screen_width - 40; + + while (curses_active) { + curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL); + + game_render_world (world, 0, 0, game_screen_offset, curses_screen_height); + + game_render_player (player); + + curses_render_string (" Attributes:", COLOUR_WHITE, EFFECT_BOLD, game_screen_offset, 0); + + game_render_attribute (strength, game_screen_offset, 1); + game_render_attribute (edurance, game_screen_offset, 2); + game_render_attribute (wisdom, game_screen_offset, 3); + game_render_attribute (agility, game_screen_offset, 4); + + curses_render_string (" Skills:", COLOUR_WHITE, EFFECT_BOLD, game_screen_offset, 5); + + game_render_skill (blades, game_screen_offset, 6); + game_render_skill (axes, game_screen_offset, 7); + game_render_skill (bows, game_screen_offset, 8); + game_render_skill (spears, game_screen_offset, 9); + game_render_skill (puppet_magic, game_screen_offset, 10); + game_render_skill (nature_magic, game_screen_offset, 11); + game_render_skill (rune_magic, game_screen_offset, 12); + game_render_skill (charm_magic, game_screen_offset, 13); + + switch (curses_character) { + 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 (); + } + + terminal_show_cursor (TRUE); +} + +#endif diff --git a/chapter/chapter_6.h b/chapter/chapter_6.h new file mode 100644 index 0000000..a18252d --- /dev/null +++ b/chapter/chapter_6.h @@ -0,0 +1,20 @@ +/* +Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic + +Xhartae is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation. +And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version. +It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3. +*/ + +#ifndef CHAPTER_6_HEADER +#define CHAPTER_6_HEADER + +#include "chapter_0.h" +#include "chapter_1.h" +#include "chapter_2.h" +#include "chapter_3.h" +#include "chapter_5.h" + +extern procedure_t play_game (procedure_t); + +#endif diff --git a/compile.sh b/compile.sh index 4207baa..2830b55 100644 --- a/compile.sh +++ b/compile.sh @@ -10,10 +10,11 @@ gcc -g -Wall -Wextra -Wpedantic -O0 -c -o chapter/chapter_2.o chapter/chapter_2. gcc -g -Wall -Wextra -Wpedantic -O0 -c -o chapter/chapter_3.o chapter/chapter_3.c gcc -g -Wall -Wextra -Wpedantic -O0 -c -o chapter/chapter_4.o chapter/chapter_4.c gcc -g -Wall -Wextra -Wpedantic -O0 -c -o chapter/chapter_5.o chapter/chapter_5.c +gcc -g -Wall -Wextra -Wpedantic -O0 -c -o chapter/chapter_6.o chapter/chapter_6.c gcc -g -Wall -Wextra -Wpedantic -O0 -c -o xhartae.o xhartae.c -gcc -o xhartae xhartae.o chapter/chapter_0.o chapter/chapter_1.o chapter/chapter_2.o chapter/chapter_3.o chapter/chapter_4.o chapter/chapter_5.o +gcc -o xhartae xhartae.o chapter/chapter_0.o chapter/chapter_1.o chapter/chapter_2.o chapter/chapter_3.o chapter/chapter_4.o chapter/chapter_5.o chapter/chapter_6.o #~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_0.h #~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_0.c @@ -27,8 +28,12 @@ gcc -o xhartae xhartae.o chapter/chapter_0.o chapter/chapter_1.o chapter/chapter #~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_4.c #~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_5.h #~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_5.c +#~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_6.h +#~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_6.c #~splint -weak -warnposix -retvalother -syntax -type xhartae.c -#~valgrind --show-leak-kinds=all --leak-check=full ./xhartae +valgrind --show-leak-kinds=all --leak-check=full --log-file=log.txt ./xhartae + +xighlight -V < log.txt exit diff --git a/xhartae.c b/xhartae.c index 3ec4396..1069683 100644 --- a/xhartae.c +++ b/xhartae.c @@ -13,6 +13,7 @@ It is distributed in the hope that it will be useful or harmful, it really depen #include "chapter/chapter_3.c" #include "chapter/chapter_4.c" #include "chapter/chapter_5.c" + #include "chapter/chapter_6.c" #else #include "chapter/chapter_0.h" #include "chapter/chapter_1.h" @@ -20,6 +21,7 @@ It is distributed in the hope that it will be useful or harmful, it really depen #include "chapter/chapter_3.h" #include "chapter/chapter_4.h" #include "chapter/chapter_5.h" + #include "chapter/chapter_6.h" #endif /*