More to come...
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

147 řádky
7.8KB

  1. #ifndef CHAPTER_Y_SOURCE
  2. #define CHAPTER_Y_SOURCE
  3. #include <stdlib.h>
  4. #include "chapter_y.h"
  5. #define UNUSED(variable) (void) variable
  6. 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) {
  7. UNUSED (width); UNUSED (height); UNUSED (x); UNUSED (y);
  8. for (number_t j = 0; j < world->height; ++j) {
  9. for (number_t i = 0; i < world->width; ++i) {
  10. world->block [j * world->width + i] = (block_t *) data;
  11. }
  12. }
  13. }
  14. 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) {
  15. for (number_t j = 0; j < height; ++j) {
  16. for (number_t i = 0; i < width; ++i) {
  17. world->block [(j + y) * world->width + (i + x)] = (block_t *) data;
  18. }
  19. }
  20. }
  21. 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) {
  22. for (number_t offset = 0; offset < width; ++offset) {
  23. world->block [world->width * y + offset + x] = (block_t *) data;
  24. world->block [world->width * (height - 1 + y) + offset + x] = (block_t *) data;
  25. }
  26. for (number_t offset = 0; offset < height; ++offset) {
  27. world->block [world->width * (y + offset) + x] = (block_t *) data;
  28. world->block [world->width * (y + offset) + width - 1 + x] = (block_t *) data;
  29. }
  30. }
  31. 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) {
  32. UNUSED (height); UNUSED (x); UNUSED (y);
  33. bot_t * bot = (bot_t *) data;
  34. number_t count = width;
  35. for (number_t index = 0; index < count; ++index) {
  36. world->bot_count += 1;
  37. world->bots = reallocate (world->bots, world->bot_count * (number_t) sizeof (* world->bots));
  38. 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));
  39. world->bots [world->bot_count - 1].x = randomize (1, 40);
  40. world->bots [world->bot_count - 1].y = randomize (1, 40);
  41. }
  42. }
  43. procedure_t play_game (procedure_t) {
  44. generator_t * full_fill = game_generator (generate_full_fill_function);
  45. generator_t * rectangle_fill = game_generator (generate_rectangle_fill_function);
  46. generator_t * rectangle_line = game_generator (generate_rectangle_line_function);
  47. generator_t * create_bots = game_generator (generate_create_bots_function);
  48. 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));
  49. 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));
  50. 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));
  51. 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));
  52. 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),
  53. game_attribute ("Strength", game_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0),
  54. game_attribute ("Edurance", game_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0),
  55. game_attribute ("Wisdom", game_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0),
  56. game_attribute ("Agility", game_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0),
  57. NULL,
  58. game_skill ("Blades", game_bundle (10, 120, 0), GAME_ACTION_SWING_BLADE, 0),
  59. game_skill ("Axes", game_bundle (10, 120, 0), GAME_ACTION_SWING_AXE, 0),
  60. game_skill ("Bows", game_bundle (10, 120, 0), GAME_ACTION_SHOOT_ARROW, 0),
  61. game_skill ("Spears", game_bundle (10, 120, 0), GAME_ACTION_THROW_SPEAR, 0),
  62. game_skill ("Puppet Magic", game_bundle (10, 120, 0), GAME_ACTION_SUMMON_PUPPET, 0),
  63. game_skill ("Nature Magic", game_bundle (10, 120, 0), GAME_ACTION_CALL_NATURE, 0),
  64. game_skill ("Rune Magic", game_bundle (10, 120, 0), GAME_ACTION_CITE_RUNE, 0),
  65. game_skill ("Charm Magic", game_bundle (10, 120, 0), GAME_ACTION_CAST_CHARM, 0),
  66. NULL);
  67. block_t * grass = game_block ("Grass", game_symbol (',', COLOUR_GREEN, EFFECT_BOLD), NULL, FALSE, FALSE);
  68. block_t * stone_floor = game_block ("Stone Floor", game_symbol ('.', COLOUR_GREY, EFFECT_BOLD), NULL, FALSE, FALSE);
  69. block_t * stone_wall = game_block ("Stone Wall", game_symbol ('#', COLOUR_GREY, EFFECT_BOLD), NULL, TRUE, FALSE);
  70. world_t * world = game_world (300, 100, full_fill, grass, 0, 0, 0, 0,
  71. rectangle_fill, stone_floor, 5, 9, 2, 4,
  72. rectangle_line, stone_wall, 5, 9, 2, 4,
  73. create_bots, goblin, 3, 0, 0, 0,
  74. create_bots, hob_goblin, 3, 0, 0, 0,
  75. create_bots, orc, 3, 0, 0, 0,
  76. create_bots, ogre, 3, 0, 0, 0,
  77. NULL);
  78. player->attributes [0]->points->current = 7;
  79. player->attributes [1]->points->current = 3;
  80. player->attributes [2]->points->current = 5;
  81. player->attributes [3]->points->current = 3;
  82. player->skills [0]->points->current = randomize (1, 10);
  83. player->skills [1]->points->current = randomize (1, 10);
  84. player->skills [2]->points->current = randomize (1, 10);
  85. player->skills [3]->points->current = randomize (1, 10);
  86. player->skills [4]->points->current = randomize (1, 10);
  87. player->skills [5]->points->current = randomize (1, 10);
  88. player->skills [6]->points->current = randomize (1, 10);
  89. player->skills [7]->points->current = randomize (1, 10);
  90. curses_configure ();
  91. terminal_show_cursor (FALSE);
  92. while (curses_active) {
  93. number_t game_screen_offset = curses_screen_width - 40;
  94. curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
  95. game_render_world (world, 0, 0, game_screen_offset, curses_screen_height);
  96. game_render_player (player);
  97. for (number_t attribute = 0; attribute < player->attribute_count; ++attribute) {
  98. game_render_attribute (player->attributes [attribute], game_screen_offset, 1 + attribute);
  99. }
  100. for (number_t skill = 0; skill < player->skill_count; ++skill) {
  101. game_render_skill (player->skills [skill], game_screen_offset, 6 + skill);
  102. }
  103. switch (curses_character) {
  104. case SIGNAL_ARROW_UP: player->y -= 1; limit (& player->y, 0, world->height - 1); break;
  105. case SIGNAL_ARROW_DOWN: player->y += 1; limit (& player->y, 0, world->height - 1); break;
  106. case SIGNAL_ARROW_LEFT: player->x -= 1; limit (& player->x, 0, world->width - 1); break;
  107. case SIGNAL_ARROW_RIGHT: player->x += 1; limit (& player->x, 0, world->width - 1); break;
  108. default: break;
  109. }
  110. curses_synchronize ();
  111. }
  112. terminal_show_cursor (TRUE);
  113. game_clean_up ();
  114. }
  115. #endif