2023-11-20 09:43:12 -05:00
|
|
|
/*
|
|
|
|
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_5_SOURCE
|
|
|
|
#define CHAPTER_5_SOURCE
|
|
|
|
|
|
|
|
#include "chapter_5.h"
|
2023-11-20 20:52:40 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
So, what are actually getters and setters, and why you should never use them? Lets explain.
|
|
|
|
*/
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-21 14:21:35 -05:00
|
|
|
#define MEMORY_LIMIT (1024 * 1024)
|
2023-11-21 11:58:48 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
static void generate_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;
|
|
|
|
|
|
|
|
for (j = 0; j < world->height; ++j) {
|
|
|
|
for (i = 0; i < world->width; ++i) {
|
|
|
|
world->block [j * world->width + i] = block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void generate_rectangle_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
memory_t memorize (number_t size) {
|
2023-11-22 07:47:27 -05:00
|
|
|
static string_t memory_store [MEMORY_LIMIT] = { 0 };
|
|
|
|
static number_t memory_count = 0;
|
2023-11-21 11:58:48 -05:00
|
|
|
|
2023-11-21 14:21:35 -05:00
|
|
|
fatal_failure (memory_count + size >= MEMORY_LIMIT, "memorize: You have reached the 1 MiB memory limit.");
|
2023-11-21 11:58:48 -05:00
|
|
|
|
2023-11-21 14:21:35 -05:00
|
|
|
memory_count += size;
|
2023-11-21 11:58:48 -05:00
|
|
|
|
2023-11-21 14:21:35 -05:00
|
|
|
return ((memory_t) ((string_t) memory_store + memory_count - size));
|
2023-11-21 11:58:48 -05:00
|
|
|
}
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
bundle_t * format_bundle (number_t minimum, number_t maximum, number_t current) {
|
|
|
|
bundle_t * bundle;
|
|
|
|
|
|
|
|
bundle = memorize ((number_t) sizeof (* bundle));
|
|
|
|
|
|
|
|
bundle->minimum = minimum;
|
|
|
|
bundle->maximum = maximum;
|
|
|
|
bundle->current = current;
|
|
|
|
|
|
|
|
return (bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol_t * format_symbol (number_t character, number_t colour, number_t effect) {
|
2023-11-22 07:47:27 -05:00
|
|
|
symbol_t * symbol;
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
symbol = memorize ((number_t) sizeof (* symbol));
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
symbol->character = character;
|
|
|
|
symbol->colour = colour;
|
|
|
|
symbol->effect = effect;
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
return (symbol);
|
2023-11-21 08:43:45 -05:00
|
|
|
}
|
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
generator_t * game_generator (generate_t function) {
|
|
|
|
generator_t * generator;
|
|
|
|
|
|
|
|
generator = memorize ((number_t) sizeof (* generator));
|
|
|
|
|
|
|
|
generator->generate = function;
|
|
|
|
|
|
|
|
return (generator);
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:43:45 -05:00
|
|
|
attribute_t * game_attribute (string_t name, bundle_t * points, ...) {
|
|
|
|
attribute_t * attribute;
|
|
|
|
va_list list;
|
|
|
|
number_t action;
|
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
attribute = memorize ((number_t) sizeof (* attribute));
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
string_copy ((attribute->name = memorize (string_length (name) + 1)), name);
|
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
memory_copy ((attribute->points = memorize ((number_t) sizeof (* attribute->points))), points, (number_t) sizeof (* points));
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
va_start (list, points);
|
|
|
|
|
2023-11-21 08:43:45 -05:00
|
|
|
for (;;) {
|
|
|
|
action = (number_t) va_arg (list, int);
|
|
|
|
|
|
|
|
if (action > 0) {
|
|
|
|
attribute->positive_count += 1;
|
2023-11-22 07:47:27 -05:00
|
|
|
attribute->positive = memorize ((number_t) sizeof (action));
|
2023-11-21 08:43:45 -05:00
|
|
|
attribute->positive [attribute->positive_count - 1] = (action_t) action;
|
|
|
|
} else if (action < 0) {
|
|
|
|
attribute->negative_count += 1;
|
2023-11-22 07:47:27 -05:00
|
|
|
attribute->negative = memorize ((number_t) sizeof (action));
|
2023-11-21 14:21:35 -05:00
|
|
|
attribute->negative [attribute->negative_count - 1] = (action_t) action;
|
2023-11-22 07:47:27 -05:00
|
|
|
} else break;
|
2023-11-21 08:43:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end (list);
|
|
|
|
|
|
|
|
return (attribute);
|
2023-11-20 09:43:12 -05:00
|
|
|
}
|
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
skill_t * game_skill (string_t name, bundle_t * points, ...) {
|
|
|
|
skill_t * skill;
|
|
|
|
va_list list;
|
|
|
|
number_t action;
|
2023-11-22 07:47:27 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
skill = memorize ((number_t) sizeof (* skill));
|
2023-11-22 07:47:27 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
string_copy ((skill->name = memorize (string_length (name) + 1)), name);
|
|
|
|
|
|
|
|
memory_copy ((skill->points = memorize ((number_t) sizeof (* skill->points))), points, (number_t) sizeof (* points));
|
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
va_start (list, points);
|
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
for (;;) {
|
|
|
|
action = (number_t) va_arg (list, int);
|
|
|
|
|
|
|
|
if (action > 0) {
|
|
|
|
skill->positive_count += 1;
|
|
|
|
skill->positive = memorize ((number_t) sizeof (action));
|
|
|
|
skill->positive [skill->positive_count - 1] = (action_t) action;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end (list);
|
|
|
|
|
|
|
|
return (skill);
|
|
|
|
}
|
2023-11-22 07:47:27 -05:00
|
|
|
|
|
|
|
player_t * game_player (string_t name, symbol_t * symbol) {
|
|
|
|
player_t * player;
|
|
|
|
|
|
|
|
player = memorize ((number_t) sizeof (* player));
|
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
string_copy ((player->name = memorize (string_length (name) + 1)), name);
|
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
memory_copy ((player->symbol = memorize ((number_t) sizeof (* player->symbol))), symbol, (number_t) sizeof (* symbol));
|
|
|
|
|
|
|
|
player->x = 3;
|
|
|
|
player->y = 3;
|
|
|
|
|
|
|
|
return (player);
|
|
|
|
}
|
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
block_t * game_block (string_t name, symbol_t * symbol, number_t collision, number_t override) {
|
|
|
|
block_t * block;
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
block = memorize ((number_t) sizeof (* block));
|
|
|
|
|
|
|
|
string_copy ((block->name = memorize (string_length (name) + 1)), name);
|
|
|
|
|
|
|
|
memory_copy ((block->symbol = memorize ((number_t) sizeof (* block->symbol))), symbol, (number_t) sizeof (* symbol));
|
|
|
|
|
|
|
|
block->collision = collision;
|
|
|
|
block->override = override;
|
|
|
|
|
|
|
|
return (block);
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
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;
|
2023-11-22 09:35:02 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
//~number_t i, j;
|
2023-11-22 09:35:02 -05:00
|
|
|
|
|
|
|
world = memorize ((number_t) sizeof (* world));
|
|
|
|
|
|
|
|
world->width = width;
|
|
|
|
world->height = height;
|
|
|
|
|
|
|
|
world->block = memorize (width * height * (number_t) sizeof (* world->block));
|
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
va_start (list, height);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
generator = (generator_t *) va_arg (list, void *);
|
|
|
|
|
|
|
|
if (generator != (generator_t *) 0XDEADBEEF) {
|
|
|
|
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 *);
|
|
|
|
|
|
|
|
generator->generate (world, w, h, x, y, block);
|
|
|
|
} else break;
|
2023-11-22 09:35:02 -05:00
|
|
|
}
|
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
//~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);
|
2023-11-22 09:35:02 -05:00
|
|
|
|
|
|
|
return (world);
|
2023-11-21 08:43:45 -05:00
|
|
|
}
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-21 08:43:45 -05:00
|
|
|
void game_render_attribute (attribute_t * attribute, number_t x, number_t y) {
|
2023-11-21 11:58:48 -05:00
|
|
|
curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x, y);
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-21 11:58:48 -05:00
|
|
|
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
|
2023-11-21 08:43:45 -05:00
|
|
|
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);
|
2023-11-20 09:43:12 -05:00
|
|
|
}
|
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
void game_render_skill (skill_t * skill, number_t x, number_t y) {
|
|
|
|
curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x, y);
|
2023-11-22 07:47:27 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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) {
|
|
|
|
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) {
|
|
|
|
number_t i, j;
|
|
|
|
|
|
|
|
for (j = 0; (j < height) && (j < world->height); ++j) {
|
|
|
|
for (i = 0; (i < width) && (i < world->width); ++i) {
|
2023-11-22 13:51:06 -05:00
|
|
|
game_render_block (world->block [j * world->width + i], i + x, j + y);
|
2023-11-22 09:35:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void play_game (void) {
|
2023-11-22 07:47:27 -05:00
|
|
|
player_t * player;
|
2023-11-22 13:51:06 -05:00
|
|
|
block_t * grass, * stone_floor, * stone_wall;
|
2023-11-22 09:35:02 -05:00
|
|
|
world_t * world;
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
generator_t * fill, * rectangle;
|
2023-11-21 08:43:45 -05:00
|
|
|
attribute_t * strength, * edurance, * wisdom, * agility;
|
2023-11-22 13:51:06 -05:00
|
|
|
skill_t * blades, * axes, * bows, * spears, * puppet_magic, * nature_magic, * rune_magic, * charm_magic;
|
|
|
|
|
|
|
|
fill = game_generator (generate_fill_function);
|
|
|
|
rectangle = game_generator (generate_rectangle_function);
|
2023-11-20 09:43:12 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
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);
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
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);
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
player = game_player ("Riri", format_symbol ('@', COLOUR_CYAN, EFFECT_BOLD));
|
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
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);
|
2023-11-22 09:35:02 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
world = game_world (80, 24, fill, 0, 0, 0, 0, grass,
|
|
|
|
rectangle, 20, 10, 2, 4, stone_wall,
|
|
|
|
(generator_t *) 0XDEADBEEF);
|
2023-11-22 09:35:02 -05:00
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
curses_configure ();
|
2023-11-21 08:43:45 -05:00
|
|
|
|
|
|
|
while (curses_active) {
|
|
|
|
curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
|
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
curses_render_string ("Attributes:", COLOUR_WHITE, EFFECT_BOLD, 0, 0);
|
2023-11-22 09:35:02 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
game_render_attribute (strength, 2, 1);
|
|
|
|
game_render_attribute (edurance, 2, 2);
|
|
|
|
game_render_attribute (wisdom, 2, 3);
|
|
|
|
game_render_attribute (agility, 2, 4);
|
2023-11-21 11:58:48 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
curses_render_string ("Skills:", COLOUR_WHITE, EFFECT_BOLD, 0, 5);
|
2023-11-21 11:58:48 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
game_render_skill (blades, 2, 6);
|
|
|
|
game_render_skill (axes, 2, 7);
|
|
|
|
game_render_skill (bows, 2, 8);
|
|
|
|
game_render_skill (spears, 2, 9);
|
|
|
|
game_render_skill (puppet_magic, 2, 10);
|
|
|
|
game_render_skill (nature_magic, 2, 11);
|
|
|
|
game_render_skill (rune_magic, 2, 12);
|
|
|
|
game_render_skill (charm_magic, 2, 13);
|
2023-11-21 11:58:48 -05:00
|
|
|
|
2023-11-22 13:51:06 -05:00
|
|
|
game_render_world (world, 40, 0, curses_screen_width, curses_screen_height);
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 09:35:02 -05:00
|
|
|
game_render_player (player);
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
switch (curses_character) {
|
2023-11-22 13:51:06 -05:00
|
|
|
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;
|
2023-11-22 07:47:27 -05:00
|
|
|
}
|
2023-11-21 08:43:45 -05:00
|
|
|
|
2023-11-22 07:47:27 -05:00
|
|
|
curses_synchronize ();
|
2023-11-20 09:43:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|