diff --git a/chapter/chapter_5.c b/chapter/chapter_5.c index 88dfceb..a49f426 100644 --- a/chapter/chapter_5.c +++ b/chapter/chapter_5.c @@ -269,18 +269,19 @@ world_t * game_world (number_t width, number_t height, ...) { generator_t * generator = (generator_t *) va_arg (list, void *); if (generator != NULL) { - 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 *); + memory_t data = (memory_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); - generator->generate (world, w, h, x, y, block); + generator->generate (world, data, w, h, x, y); } else break; } va_end (list); + game_free_memory (world->bots); game_free_memory (world->block); game_free_memory (world); @@ -324,6 +325,10 @@ procedure_t game_render_world (world_t * world, number_t x, number_t y, number_t game_render_block (world->block [j * world->width + i], i + x, j + y); } } + + for (number_t index = 0; index < world->bot_count; ++index) { + game_render_bot (& world->bots [index]); + } } #endif diff --git a/chapter/chapter_5.h b/chapter/chapter_5.h index 713bd22..6d5f366 100644 --- a/chapter/chapter_5.h +++ b/chapter/chapter_5.h @@ -110,10 +110,14 @@ typedef struct block_t { typedef struct world_t { number_t width; number_t height; + number_t item_count; + number_t bot_count; block_t * * block; + bot_t * bots; + player_t * player; } world_t; -typedef procedure_t (* generate_t) (world_t *, number_t, number_t, number_t, number_t, block_t *); +typedef procedure_t (* generate_t) (world_t *, memory_t, number_t, number_t, number_t, number_t); typedef struct generator_t { generate_t generate; diff --git a/chapter/chapter_6.c b/chapter/chapter_6.c index 90c973c..4459d80 100644 --- a/chapter/chapter_6.c +++ b/chapter/chapter_6.c @@ -13,36 +13,48 @@ It is distributed in the hope that it will be useful or harmful, it really depen #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); +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; + world->block [j * world->width + i] = (block_t *) data; } } } -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) { +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; + world->block [(j + y) * world->width + (i + x)] = (block_t *) data; } } } -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) { +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; - world->block [world->width * (height - 1 + y) + offset + x] = block; + 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; - world->block [world->width * (y + offset) + width - 1 + x] = block; + 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); } } @@ -50,6 +62,7 @@ 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)); @@ -76,9 +89,10 @@ procedure_t play_game (procedure_t) { 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, 0, 0, 0, 0, grass, - rectangle_fill, 5, 9, 2, 4, stone_floor, - rectangle_line, 5, 9, 2, 4, stone_wall, + 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, NULL); player->attributes [0]->points->current = 7; @@ -106,7 +120,10 @@ procedure_t play_game (procedure_t) { game_render_world (world, 0, 0, game_screen_offset, curses_screen_height); - game_render_bot (goblin); + game_render_bot (goblin);hob_goblin->x=1;hob_goblin->y=1; + game_render_bot (hob_goblin);orc->x=2;orc->y=2; + game_render_bot (orc);ogre->x=3;ogre->y=3; + game_render_bot (ogre); game_render_player (player);