xhartae/chapter/chapter_y.c

147 lines
7.8 KiB
C

#ifndef CHAPTER_Y_SOURCE
#define CHAPTER_Y_SOURCE
#include <stdlib.h>
#include "chapter_y.h"
#define UNUSED(variable) (void) variable
static procedure_t generate_full_fill_function (world_t * world, memory_t data, number_t width, number_t height, number_t x, number_t y) {
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_t *) data;
}
}
}
static procedure_t generate_rectangle_fill_function (world_t * world, memory_t data, number_t width, number_t height, number_t x, number_t y) {
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_t *) data;
}
}
}
static procedure_t generate_rectangle_line_function (world_t * world, memory_t data, number_t width, number_t height, number_t x, number_t y) {
for (number_t offset = 0; offset < width; ++offset) {
world->block [world->width * y + offset + x] = (block_t *) data;
world->block [world->width * (height - 1 + y) + offset + x] = (block_t *) data;
}
for (number_t offset = 0; offset < height; ++offset) {
world->block [world->width * (y + offset) + x] = (block_t *) data;
world->block [world->width * (y + offset) + width - 1 + x] = (block_t *) data;
}
}
static procedure_t generate_create_bots_function (world_t * world, memory_t data, number_t width, number_t height, number_t x, number_t y) {
UNUSED (height); UNUSED (x); UNUSED (y);
bot_t * bot = (bot_t *) data;
number_t count = width;
for (number_t index = 0; index < count; ++index) {
world->bot_count += 1;
world->bots = reallocate (world->bots, world->bot_count * (number_t) sizeof (* world->bots));
memory_copy (& world->bots [world->bot_count - 1], game_bot (bot->name, bot->symbol, NULL, bot->health, bot->mana, bot->stamina), (number_t) sizeof (* world->bots));
world->bots [world->bot_count - 1].x = randomize (1, 40);
world->bots [world->bot_count - 1].y = randomize (1, 40);
}
}
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 * create_bots = game_generator (generate_create_bots_function);
bot_t * goblin = game_bot ("Goblin", game_symbol ('g', COLOUR_RED, EFFECT_NORMAL), NULL, game_bundle (0, 11, 11), game_bundle (0, 3, 3), game_bundle (0, 23, 23));
bot_t * hob_goblin = game_bot ("Hob Goblin", game_symbol ('g', COLOUR_RED, EFFECT_BOLD), NULL, game_bundle (0, 17, 17), game_bundle (0, 7, 7), game_bundle (0, 31, 31));
bot_t * orc = game_bot ("Orc", game_symbol ('G', COLOUR_RED, EFFECT_NORMAL), NULL, game_bundle (0, 23, 23), game_bundle (0, 5, 5), game_bundle (0, 47, 47));
bot_t * ogre = game_bot ("Ogre", game_symbol ('G', COLOUR_RED, EFFECT_BOLD), NULL, game_bundle (0, 37, 37), game_bundle (0, 2, 2), game_bundle (0, 83, 83));
player_t * player = game_player ("Riri", game_symbol ('@', COLOUR_CYAN, EFFECT_BOLD), NULL, game_bundle (0, 29, 29), game_bundle (0, 3, 3), game_bundle (0, 37, 37),
game_attribute ("Strength", game_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0),
game_attribute ("Edurance", game_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0),
game_attribute ("Wisdom", game_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0),
game_attribute ("Agility", game_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0),
NULL,
game_skill ("Blades", game_bundle (10, 120, 0), GAME_ACTION_SWING_BLADE, 0),
game_skill ("Axes", game_bundle (10, 120, 0), GAME_ACTION_SWING_AXE, 0),
game_skill ("Bows", game_bundle (10, 120, 0), GAME_ACTION_SHOOT_ARROW, 0),
game_skill ("Spears", game_bundle (10, 120, 0), GAME_ACTION_THROW_SPEAR, 0),
game_skill ("Puppet Magic", game_bundle (10, 120, 0), GAME_ACTION_SUMMON_PUPPET, 0),
game_skill ("Nature Magic", game_bundle (10, 120, 0), GAME_ACTION_CALL_NATURE, 0),
game_skill ("Rune Magic", game_bundle (10, 120, 0), GAME_ACTION_CITE_RUNE, 0),
game_skill ("Charm Magic", game_bundle (10, 120, 0), GAME_ACTION_CAST_CHARM, 0),
NULL);
block_t * grass = game_block ("Grass", game_symbol (',', COLOUR_GREEN, EFFECT_BOLD), NULL, FALSE, FALSE);
block_t * stone_floor = game_block ("Stone Floor", game_symbol ('.', COLOUR_GREY, EFFECT_BOLD), NULL, FALSE, FALSE);
block_t * stone_wall = game_block ("Stone Wall", game_symbol ('#', COLOUR_GREY, EFFECT_BOLD), NULL, TRUE, FALSE);
world_t * world = game_world (300, 100, full_fill, grass, 0, 0, 0, 0,
rectangle_fill, stone_floor, 5, 9, 2, 4,
rectangle_line, stone_wall, 5, 9, 2, 4,
create_bots, goblin, 3, 0, 0, 0,
create_bots, hob_goblin, 3, 0, 0, 0,
create_bots, orc, 3, 0, 0, 0,
create_bots, ogre, 3, 0, 0, 0,
NULL);
player->attributes [0]->points->current = 7;
player->attributes [1]->points->current = 3;
player->attributes [2]->points->current = 5;
player->attributes [3]->points->current = 3;
player->skills [0]->points->current = randomize (1, 10);
player->skills [1]->points->current = randomize (1, 10);
player->skills [2]->points->current = randomize (1, 10);
player->skills [3]->points->current = randomize (1, 10);
player->skills [4]->points->current = randomize (1, 10);
player->skills [5]->points->current = randomize (1, 10);
player->skills [6]->points->current = randomize (1, 10);
player->skills [7]->points->current = randomize (1, 10);
curses_configure ();
terminal_show_cursor (FALSE);
while (curses_active) {
number_t game_screen_offset = curses_screen_width - 40;
curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
game_render_world (world, 0, 0, game_screen_offset, curses_screen_height);
game_render_player (player);
for (number_t attribute = 0; attribute < player->attribute_count; ++attribute) {
game_render_attribute (player->attributes [attribute], game_screen_offset, 1 + attribute);
}
for (number_t skill = 0; skill < player->skill_count; ++skill) {
game_render_skill (player->skills [skill], game_screen_offset, 6 + skill);
}
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);
game_clean_up ();
}
#endif