Extended chapter 5...

This commit is contained in:
Ognjen Milan Robovic 2023-11-21 08:43:45 -05:00
parent 975cb4f01b
commit 0a710b8944
4 changed files with 212 additions and 37 deletions

View File

@ -346,6 +346,42 @@ char * string_realign (char * string, int amount, char character) { //
}
/*
Ignore what next two functions do, it's about memory management that we'll cover in later chapters.
*/
int memory_compare (void * memory, void * source, int length) {
int offset;
char * cast_0 = (char *) memory;
char * cast_1 = (char *) source;
fatal_failure (memory == NULL, "memory_compare: Memory is null pointer.");
fatal_failure (source == NULL, "memory_compare: Source is null pointer.");
for (offset = 0; offset != length; ++offset) {
if (cast_0 [offset] != cast_1 [offset]) {
return (FALSE);
}
}
return (TRUE);
}
void memory_copy (void * memory, void * source, int length) {
int offset;
char * cast_0 = (char *) memory;
char * cast_1 = (char *) source;
fatal_failure (memory == NULL, "memory_copy: Memory is null pointer.");
fatal_failure (source == NULL, "memory_copy: Source is null pointer.");
for (offset = 0; offset != length; ++offset) {
cast_0 [offset] = cast_1 [offset];
}
}
/*
Again, please consider these 'terminal_*' functions black magic, as well as 'number_to_string' and 'format_to_string' as they are more complex to cover them at this point, we'll
talk more about them later... For now, just take a look at how I format the code in them.
*/

View File

@ -219,6 +219,9 @@ extern char * string_reverse_limit (char * string, int li
extern char * string_realign (char * string, int amount, char character); // This is a simple function that realigns a string to right, we'll use it way later ahead.
extern int memory_compare (void * memory, void * source, int length); // We'll cover these functions later, they are more complex.
extern void memory_copy (void * memory, void * source, int length);
// In chapter two, we'll explain ASCII escape sequences, for now, consider this to be some black magic.
extern void terminal_clear (void); // Offset and clear terminal screen output.
extern void terminal_colour (int colour, int effect); // Set terminal character attributes.

View File

@ -10,13 +10,10 @@ It is distributed in the hope that it will be useful or harmful, it really depen
#define CHAPTER_5_SOURCE
#include "chapter_5.h"
/*
static void (* game_action [GAME_ACTION_COUNT]) (game_t * game, player_t * player);
*/
static number_t game_is_active (game_t * game) { return (game->active = curses_active); }
/*
So, what are actually getters and setters, and why you should never use them? Lets explain.
@C
static number_t game_get_screen_width (game_t * game) { return (game->screen_width); }
static number_t game_get_screen_height (game_t * game) { return (game->screen_height); }
@ -25,36 +22,162 @@ static number_t game_set_screen_height (game_t * game, number_t height) { return
@
*/
static void game_configure (game_t * game, player_t * player) {
game_t game;
player_t player;
static void move_player (void) { ++player.x; }
static void game_configure (void) {
curses_configure ();
game->active = curses_active;
game->screen_width = curses_screen_width;
game->screen_height = curses_screen_height;
curses_bind (SIGNAL_W, move_player);
curses_bind (SIGNAL_S, move_player);
curses_bind (SIGNAL_A, move_player);
curses_bind (SIGNAL_D, move_player);
player->x = 0;
player->y = 0;
game.active = curses_active;
game.screen_width = curses_screen_width;
game.screen_height = curses_screen_height;
player.x = 0;
player.y = 0;
}
static void game_synchronize (game_t * game, player_t * player) {
(void) game;
static void game_synchronize (void) {
return;
}
curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
skill_t * game_skill (string_t name, bundle_t * points, ...) {
skill_t * skill;
va_list list;
number_t action;
curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player->x, player->y);
va_start (list, points);
curses_synchronize ();
skill = allocate ((int) sizeof (* skill));
string_copy ((skill->name = allocate (string_length (name) + 1)), name);
memory_copy ((skill->points = allocate ((int) sizeof (* skill->points))), points, (int) sizeof (* points));
for (;;) {
action = (number_t) va_arg (list, int);
if (action > 0) {
skill->positive_count += 1;
skill->positive = reallocate (skill->positive, skill->positive_count * (int) sizeof (action));
skill->positive [skill->positive_count - 1] = (action_t) action;
} else break;
}
va_end (list);
return (skill);
}
attribute_t * game_attribute (string_t name, bundle_t * points, ...) {
attribute_t * attribute;
va_list list;
number_t action;
va_start (list, points);
attribute = allocate ((int) sizeof (* attribute));
string_copy ((attribute->name = allocate (string_length (name) + 1)), name);
memory_copy ((attribute->points = allocate ((int) sizeof (* attribute->points))), points, (int) sizeof (* points));
for (;;) {
action = (number_t) va_arg (list, int);
if (action > 0) {
attribute->positive_count += 1;
attribute->positive = reallocate (attribute->positive, attribute->positive_count * (int) sizeof (action));
attribute->positive [attribute->positive_count - 1] = (action_t) action;
} else if (action < 0) {
attribute->negative_count += 1;
attribute->negative = reallocate (attribute->negative, attribute->negative_count * (int) sizeof (action));
attribute->negative [attribute->negative_count - 1] = (action_t) -action;
} else {
break;
}
}
va_end (list);
return (attribute);
}
void game_render_skill (skill_t * skill, number_t x, number_t y) {
curses_render_string (skill->name, COLOUR_BLUE, EFFECT_NORMAL, x, y);
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 20, 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);
}
void game_render_attribute (attribute_t * attribute, number_t x, number_t y) {
curses_render_string (attribute->name, COLOUR_BLUE, EFFECT_NORMAL, x, y);
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 20, 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);
}
void play_game (void) {
game_t game;
player_t player;
bundle_t skill_points = { 10, 120, 0, 0 };
bundle_t attribute_points = { 1, 12, 0, 0 };
game_configure (& game, & player);
skill_t * blades, * axes, * bows, * spears, * puppet_magic, * nature_magic, * rune_magic, * charm_magic;
attribute_t * strength, * edurance, * wisdom, * agility;
while (game_is_active (& game)) {
game_synchronize (& game, & player);
strength = game_attribute ("Strength", & attribute_points, GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0);
edurance = game_attribute ("Edurance", & attribute_points, GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_REST, 0);
wisdom = game_attribute ("Wisdom", & attribute_points, GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_WALK, 0);
agility = game_attribute ("Agility", & attribute_points, GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0);
blades = game_skill ("Blades", & skill_points, GAME_ACTION_SWING_BLADE, 0);
axes = game_skill ("Axes", & skill_points, GAME_ACTION_SWING_AXE, 0);
bows = game_skill ("Bows", & skill_points, GAME_ACTION_SHOOT_ARROW, 0);
spears = game_skill ("Spears", & skill_points, GAME_ACTION_THROW_SPEAR, 0);
puppet_magic = game_skill ("Puppet Magic", & skill_points, GAME_ACTION_SUMMON_PUPPET, 0);
nature_magic = game_skill ("Nature Magic", & skill_points, GAME_ACTION_CALL_NATURE, 0);
rune_magic = game_skill ("Rune Magic", & skill_points, GAME_ACTION_CITE_RUNE, 0);
charm_magic = game_skill ("Charm Magic", & skill_points, GAME_ACTION_CAST_CHARM, 0);
game_configure ();
while (curses_active) {
curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
game_render_attribute (strength, 0, 0);
game_render_attribute (edurance, 0, 1);
game_render_attribute (wisdom, 0, 2);
game_render_attribute (agility, 0, 3);
curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player.x, player.y);
curses_synchronize ();
game_synchronize ();
}
strength->name = deallocate (strength->name); strength->points = deallocate (strength->points);strength->positive = deallocate (strength->positive); strength->negative = deallocate (strength->negative); strength = deallocate (strength);
edurance->name = deallocate (edurance->name); edurance->points = deallocate (edurance->points);edurance->positive = deallocate (edurance->positive); edurance->negative = deallocate (edurance->negative); edurance = deallocate (edurance);
wisdom->name = deallocate (wisdom->name); wisdom->points = deallocate (wisdom->points); wisdom->positive = deallocate (wisdom->positive); wisdom->negative = deallocate (wisdom->negative); wisdom = deallocate (wisdom);
agility->name = deallocate (agility->name); agility->points = deallocate (agility->points); agility->positive = deallocate (agility->positive); agility->negative = deallocate (agility->negative); agility = deallocate (agility);
blades->name = deallocate (blades->name); blades->points = deallocate (blades->points);blades->positive = deallocate (blades->positive); blades = deallocate (blades);
axes->name = deallocate (axes->name); axes->points = deallocate (axes->points);axes->positive = deallocate (axes->positive); axes = deallocate (axes);
bows->name = deallocate (bows->name); bows->points = deallocate (bows->points); bows->positive = deallocate (bows->positive); bows = deallocate (bows);
spears->name = deallocate (spears->name); spears->points = deallocate (spears->points); spears->positive = deallocate (spears->positive); spears = deallocate (spears);
puppet_magic->name = deallocate (puppet_magic->name); puppet_magic->points = deallocate (puppet_magic->points);puppet_magic->positive = deallocate (puppet_magic->positive); puppet_magic = deallocate (puppet_magic);
nature_magic->name = deallocate (nature_magic->name); nature_magic->points = deallocate (nature_magic->points);nature_magic->positive = deallocate (nature_magic->positive); nature_magic = deallocate (nature_magic);
rune_magic->name = deallocate (rune_magic->name); rune_magic->points = deallocate (rune_magic->points); rune_magic->positive = deallocate (rune_magic->positive); rune_magic = deallocate (rune_magic);
charm_magic->name = deallocate (charm_magic->name); charm_magic->points = deallocate (charm_magic->points); charm_magic->positive = deallocate (charm_magic->positive); charm_magic = deallocate (charm_magic);
}
#endif

View File

@ -25,46 +25,59 @@ make a simple terminal rogue-like game using what we wrote in previous chapters.
First of all, lets talk briefly about keyword 'typedef' and why I hate to use it.
*/
typedef int number_t;
typedef char * string_t;
typedef void * memory_t;
typedef enum action_t {
GAME_ACTION_IDLE, GAME_ACTION_WALK, GAME_ACTION_REST, GAME_ACTION_CAMP,
GAME_ACTION_NONE,
GAME_ACTION_WAIT, GAME_ACTION_WALK, GAME_ACTION_REST, GAME_ACTION_CAMP,
GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR,
GAME_ACTION_SUMMON_PUPPET, GAME_ACTION_CALL_NATURE, GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM,
GAME_ACTION_COUNT
} action_t;
typedef int number_t;
typedef char * string_t;
typedef void * memory_t;
typedef struct game_t {
number_t active, screen_width, screen_height;
} game_t;
typedef struct bundle_t {
number_t minimum, maximum, current, boosted;
number_t minimum, maximum, current, booster;
} bundle_t;
/*
typedef struct skill_t {
string_t name;
bundle_t stat;
action_t positive [4];
string_t name;
number_t positive_count;
number_t learning_rate;
bundle_t * points;
action_t * positive;
} skill_t;
typedef struct attribute_t {
string_t name;
bundle_t stat;
action_t positive [4], negative [4];
string_t name;
number_t positive_count, negative_count;
bundle_t * points;
action_t * positive, * negative;
} attribute_t;
*/
typedef struct player_t {
string_t name;
number_t x, y;
bundle_t * health, * armour, * mana, * stamina;
/*attribute_t strength, edurance, intelligence, agility;
bundle_t health, armour, mana, stamina;
attribute_t strength, edurance, intelligence, agility;
skill_t blades, axes, bows, spears;
skill_t puppet_magic, nature_magic, rune_magic, charm_magic;*/
skill_t puppet_magic, nature_magic, rune_magic, charm_magic;
} player_t;
extern game_t game;
extern player_t player;
extern skill_t * game_skill (string_t name, bundle_t * points, ...);
extern attribute_t * game_attribute (string_t name, bundle_t * points, ...);
extern void game_render_skill (skill_t * skill, number_t x, number_t y);
extern void game_render_attribute (attribute_t * attribute, number_t x, number_t y);
extern void play_game (void);
#endif