xhartae/chapter/chapter_5.c

215 lines
7.3 KiB
C
Raw Normal View History

/*
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.
*/
#define MEMORY_LIMIT (1024 * 1024)
2023-11-21 11:58:48 -05:00
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
fatal_failure (memory_count + size >= MEMORY_LIMIT, "memorize: You have reached the 1 MiB memory limit.");
2023-11-21 11:58:48 -05:00
memory_count += size;
2023-11-21 11:58:48 -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-23 15:23:23 -05:00
number_t randomize (number_t lower, number_t upper) {
return ((number_t) rand () % (upper - lower) + lower);
}
2023-11-22 09:35:02 -05:00
2023-11-23 15:23:23 -05:00
bundle_t * format_bundle (number_t minimum, number_t maximum, number_t current) {
bundle_t * bundle = memorize ((number_t) sizeof (* bundle));
2023-11-22 09:35:02 -05:00
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-23 15:23:23 -05:00
symbol_t * 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
}
generator_t * game_generator (generate_t function) {
2023-11-23 15:23:23 -05:00
generator_t * 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, ...) {
2023-11-23 15:23:23 -05:00
va_list list;
2023-11-21 08:43:45 -05:00
2023-11-23 15:23:23 -05:00
attribute_t * 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));
va_start (list, points);
2023-11-21 08:43:45 -05:00
for (;;) {
2023-11-23 07:47:10 -05:00
number_t action = (number_t) va_arg (list, int);
2023-11-21 08:43:45 -05:00
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));
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-22 09:35:02 -05:00
skill_t * game_skill (string_t name, bundle_t * points, ...) {
2023-11-23 15:23:23 -05:00
va_list list;
2023-11-22 07:47:27 -05:00
2023-11-23 15:23:23 -05:00
skill_t * 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));
va_start (list, points);
2023-11-22 09:35:02 -05:00
for (;;) {
2023-11-23 07:47:10 -05:00
number_t action = (number_t) va_arg (list, int);
2023-11-22 09:35:02 -05:00
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) {
2023-11-23 15:23:23 -05:00
player_t * player = memorize ((number_t) sizeof (* player));
2023-11-22 07:47:27 -05:00
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) {
2023-11-23 15:23:23 -05:00
block_t * block = memorize ((number_t) sizeof (* block));
2023-11-22 09:35:02 -05:00
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);
}
world_t * game_world (number_t width, number_t height, ...) {
2023-11-23 15:23:23 -05:00
va_list list;
2023-11-22 09:35:02 -05:00
2023-11-23 15:23:23 -05:00
world_t * world = memorize ((number_t) sizeof (* world));
2023-11-22 09:35:02 -05:00
world->width = width;
world->height = height;
world->block = memorize (width * height * (number_t) sizeof (* world->block));
va_start (list, height);
for (;;) {
2023-11-23 15:23:23 -05:00
generator_t * generator = (generator_t *) va_arg (list, void *);
2023-11-22 14:37:39 -05:00
if (generator != NULL) {
2023-11-23 15:23:23 -05:00
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;
2023-11-22 09:35:02 -05:00
}
va_end (list);
2023-11-22 09:35:02 -05:00
return (world);
2023-11-21 08:43:45 -05:00
}
2023-11-22 14:37:39 -05:00
procedure_t game_render_attribute (attribute_t * attribute, number_t x, number_t y) {
2023-11-23 15:23:23 -05:00
curses_render_string (" ", COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x + 3, y);
2023-11-21 11:58:48 -05:00
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
2023-11-22 14:37:39 -05:00
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);
}
2023-11-22 14:37:39 -05:00
procedure_t game_render_skill (skill_t * skill, number_t x, number_t y) {
2023-11-23 15:23:23 -05:00
curses_render_string (" ", COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x + 3, 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);
2023-11-22 14:37:39 -05:00
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);
2023-11-22 09:35:02 -05:00
}
2023-11-22 14:37:39 -05:00
procedure_t game_render_player (player_t * player) {
2023-11-22 09:35:02 -05:00
curses_render_character ((char) player->symbol->character, player->symbol->colour, player->symbol->effect, player->x, player->y);
}
2023-11-22 14:37:39 -05:00
procedure_t game_render_block (block_t * block, number_t x, number_t y) {
2023-11-22 09:35:02 -05:00
curses_render_character ((char) block->symbol->character, block->symbol->colour, block->symbol->effect, x, y);
}
2023-11-22 14:37:39 -05:00
procedure_t game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height) {
2023-11-23 15:23:23 -05:00
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);
2023-11-22 09:35:02 -05:00
}
}
}
#endif