More to come...
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

282 行
10KB

  1. /*
  2. Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. Xhartae is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  4. 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.
  5. 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.
  6. */
  7. #ifndef CHAPTER_5_SOURCE
  8. #define CHAPTER_5_SOURCE
  9. #include "chapter_5.h"
  10. /*
  11. So, what are actually getters and setters, and why you should never use them? Lets explain.
  12. @C
  13. static number_t game_get_screen_width (game_t * game) { return (game->screen_width); }
  14. static number_t game_get_screen_height (game_t * game) { return (game->screen_height); }
  15. static number_t game_set_screen_width (game_t * game, number_t width) { return (game->screen_width = width); }
  16. static number_t game_set_screen_height (game_t * game, number_t height) { return (game->screen_height = height); }
  17. @
  18. */
  19. #define MEMORY_LIMIT (1024 * 1024)
  20. memory_t memorize (number_t size) {
  21. static string_t memory_store [MEMORY_LIMIT] = { 0 };
  22. static number_t memory_count = 0;
  23. fatal_failure (memory_count + size >= MEMORY_LIMIT, "memorize: You have reached the 1 MiB memory limit.");
  24. memory_count += size;
  25. return ((memory_t) ((string_t) memory_store + memory_count - size));
  26. }
  27. bundle_t * format_bundle (number_t minimum, number_t maximum, number_t current) {
  28. bundle_t * bundle;
  29. bundle = memorize ((number_t) sizeof (* bundle));
  30. bundle->minimum = minimum;
  31. bundle->maximum = maximum;
  32. bundle->current = current;
  33. return (bundle);
  34. }
  35. symbol_t * format_symbol (number_t character, number_t colour, number_t effect) {
  36. symbol_t * symbol;
  37. symbol = memorize ((number_t) sizeof (* symbol));
  38. symbol->character = character;
  39. symbol->colour = colour;
  40. symbol->effect = effect;
  41. return (symbol);
  42. }
  43. attribute_t * game_attribute (string_t name, bundle_t * points, ...) {
  44. attribute_t * attribute;
  45. va_list list;
  46. number_t action;
  47. va_start (list, points);
  48. attribute = memorize ((number_t) sizeof (* attribute));
  49. string_copy ((attribute->name = memorize (string_length (name) + 1)), name);
  50. memory_copy ((attribute->points = memorize ((number_t) sizeof (* attribute->points))), points, (number_t) sizeof (* points));
  51. for (;;) {
  52. action = (number_t) va_arg (list, int);
  53. if (action > 0) {
  54. attribute->positive_count += 1;
  55. attribute->positive = memorize ((number_t) sizeof (action));
  56. attribute->positive [attribute->positive_count - 1] = (action_t) action;
  57. } else if (action < 0) {
  58. attribute->negative_count += 1;
  59. attribute->negative = memorize ((number_t) sizeof (action));
  60. attribute->negative [attribute->negative_count - 1] = (action_t) action;
  61. } else break;
  62. }
  63. va_end (list);
  64. return (attribute);
  65. }
  66. skill_t * game_skill (string_t name, bundle_t * points, ...) {
  67. skill_t * skill;
  68. va_list list;
  69. number_t action;
  70. va_start (list, points);
  71. skill = memorize ((number_t) sizeof (* skill));
  72. string_copy ((skill->name = memorize (string_length (name) + 1)), name);
  73. memory_copy ((skill->points = memorize ((number_t) sizeof (* skill->points))), points, (number_t) sizeof (* points));
  74. for (;;) {
  75. action = (number_t) va_arg (list, int);
  76. if (action > 0) {
  77. skill->positive_count += 1;
  78. skill->positive = memorize ((number_t) sizeof (action));
  79. skill->positive [skill->positive_count - 1] = (action_t) action;
  80. } else break;
  81. }
  82. va_end (list);
  83. return (skill);
  84. }
  85. player_t * game_player (string_t name, symbol_t * symbol) {
  86. player_t * player;
  87. player = memorize ((number_t) sizeof (* player));
  88. string_copy ((player->name = memorize (string_length (name) + 1)), name);
  89. memory_copy ((player->symbol = memorize ((number_t) sizeof (* player->symbol))), symbol, (number_t) sizeof (* symbol));
  90. player->x = 3;
  91. player->y = 3;
  92. return (player);
  93. }
  94. block_t * game_block (string_t name, symbol_t * symbol, number_t collision, number_t override) {
  95. block_t * block;
  96. block = memorize ((number_t) sizeof (* block));
  97. string_copy ((block->name = memorize (string_length (name) + 1)), name);
  98. memory_copy ((block->symbol = memorize ((number_t) sizeof (* block->symbol))), symbol, (number_t) sizeof (* symbol));
  99. block->collision = collision;
  100. block->override = override;
  101. return (block);
  102. }
  103. world_t * game_world (number_t width, number_t height, block_t * floor, block_t * wall) {
  104. world_t * world;
  105. number_t x, y;
  106. world = memorize ((number_t) sizeof (* world));
  107. world->width = width;
  108. world->height = height;
  109. world->block = memorize (width * height * (number_t) sizeof (* world->block));
  110. for (y = 0; y < height; ++y) {
  111. for (x = 0; x < width; ++x) {
  112. world->block [y * width + x] = floor;
  113. }
  114. }
  115. world->block [6] = wall;
  116. return (world);
  117. }
  118. void game_render_attribute (attribute_t * attribute, number_t x, number_t y) {
  119. curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x, y);
  120. curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
  121. curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y);
  122. curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y);
  123. curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y);
  124. curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x = 34, y);
  125. }
  126. void game_render_skill (skill_t * skill, number_t x, number_t y) {
  127. curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x, y);
  128. curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
  129. curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y);
  130. curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y);
  131. curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y);
  132. curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x = 34, y);
  133. }
  134. void game_render_player (player_t * player) {
  135. curses_render_character ((char) player->symbol->character, player->symbol->colour, player->symbol->effect, player->x, player->y);
  136. }
  137. void game_render_block (block_t * block, number_t x, number_t y) {
  138. curses_render_character ((char) block->symbol->character, block->symbol->colour, block->symbol->effect, x, y);
  139. }
  140. void game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height) {
  141. number_t i, j;
  142. for (j = 0; (j < height) && (j < world->height); ++j) {
  143. for (i = 0; (i < width) && (i < world->width); ++i) {
  144. game_render_block (world->block [(j + y) * world->width + (i + x)], i, j);
  145. }
  146. }
  147. }
  148. void play_game (void) {
  149. player_t * player;
  150. block_t * floor, * wall;
  151. world_t * world;
  152. skill_t * blades, * axes, * bows, * spears, * puppet_magic, * nature_magic, * rune_magic, * charm_magic;
  153. attribute_t * strength, * edurance, * wisdom, * agility;
  154. strength = game_attribute ("Strength", format_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0);
  155. edurance = game_attribute ("Edurance", format_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0);
  156. wisdom = game_attribute ("Wisdom", format_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0);
  157. agility = game_attribute ("Agility", format_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0);
  158. blades = game_skill ("Blades", format_bundle (10, 120, 0), GAME_ACTION_SWING_BLADE, 0);
  159. axes = game_skill ("Axes", format_bundle (10, 120, 0), GAME_ACTION_SWING_AXE, 0);
  160. bows = game_skill ("Bows", format_bundle (10, 120, 0), GAME_ACTION_SHOOT_ARROW, 0);
  161. spears = game_skill ("Spears", format_bundle (10, 120, 0), GAME_ACTION_THROW_SPEAR, 0);
  162. puppet_magic = game_skill ("Puppet Magic", format_bundle (10, 120, 0), GAME_ACTION_SUMMON_PUPPET, 0);
  163. nature_magic = game_skill ("Nature Magic", format_bundle (10, 120, 0), GAME_ACTION_CALL_NATURE, 0);
  164. rune_magic = game_skill ("Rune Magic", format_bundle (10, 120, 0), GAME_ACTION_CITE_RUNE, 0);
  165. charm_magic = game_skill ("Charm Magic", format_bundle (10, 120, 0), GAME_ACTION_CAST_CHARM, 0);
  166. player = game_player ("Riri", format_symbol ('@', COLOUR_CYAN, EFFECT_BOLD));
  167. floor = game_block ("Floor", format_symbol ('.', COLOUR_GREY, EFFECT_BOLD), FALSE, FALSE);
  168. wall = game_block ("Wall", format_symbol ('#', COLOUR_GREY, EFFECT_BOLD), TRUE, FALSE);
  169. world = game_world (80, 24, floor, wall);
  170. curses_configure ();
  171. while (curses_active) {
  172. curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
  173. //~curses_render_string ("Attributes:", COLOUR_WHITE, EFFECT_BOLD, 0, 0);
  174. //~game_render_attribute (strength, 2, 1);
  175. //~game_render_attribute (edurance, 2, 2);
  176. //~game_render_attribute (wisdom, 2, 3);
  177. //~game_render_attribute (agility, 2, 4);
  178. //~curses_render_string ("Skills:", COLOUR_WHITE, EFFECT_BOLD, 0, 5);
  179. //~game_render_skill (blades, 2, 6);
  180. //~game_render_skill (axes, 2, 7);
  181. //~game_render_skill (bows, 2, 8);
  182. //~game_render_skill (spears, 2, 9);
  183. //~game_render_skill (puppet_magic, 2, 10);
  184. //~game_render_skill (nature_magic, 2, 11);
  185. //~game_render_skill (rune_magic, 2, 12);
  186. //~game_render_skill (charm_magic, 2, 13);
  187. game_render_world (world, 0, 0, curses_screen_width, curses_screen_height);
  188. game_render_player (player);
  189. switch (curses_character) {
  190. case 'w': player->y -= 1; break;
  191. case 's': player->y += 1; break;
  192. case 'a': player->x -= 1; break;
  193. case 'd': player->x += 1; break;
  194. default: break;
  195. }
  196. curses_synchronize ();
  197. }
  198. }
  199. #endif