More to come...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
8.2KB

  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. static string_t memory_store [MEMORY_LIMIT];
  21. static memory_t memorize (number_t size) {
  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. static void move_player (void) { ++player.x; }
  28. static void game_configure (void) {
  29. curses_configure ();
  30. curses_bind (SIGNAL_W, move_player);
  31. curses_bind (SIGNAL_S, move_player);
  32. curses_bind (SIGNAL_A, move_player);
  33. curses_bind (SIGNAL_D, move_player);
  34. game.active = curses_active;
  35. game.screen_width = curses_screen_width;
  36. game.screen_height = curses_screen_height;
  37. player.x = 0;
  38. player.y = 0;
  39. }
  40. static void game_synchronize (void) {
  41. return;
  42. }
  43. game_t game;
  44. player_t player;
  45. skill_t * game_skill (string_t name, bundle_t * points, ...) {
  46. skill_t * skill;
  47. va_list list;
  48. number_t action;
  49. va_start (list, points);
  50. skill = memorize ((int) sizeof (* skill));
  51. string_copy ((skill->name = memorize (string_length (name) + 1)), name);
  52. memory_copy ((skill->points = memorize ((int) sizeof (* skill->points))), points, (int) sizeof (* points));
  53. for (;;) {
  54. action = (number_t) va_arg (list, int);
  55. if (action > 0) {
  56. skill->positive_count += 1;
  57. skill->positive = memorize ((int) sizeof (action));
  58. skill->positive [skill->positive_count - 1] = (action_t) action;
  59. } else break;
  60. }
  61. va_end (list);
  62. return (skill);
  63. }
  64. attribute_t * game_attribute (string_t name, bundle_t * points, ...) {
  65. attribute_t * attribute;
  66. va_list list;
  67. number_t action;
  68. va_start (list, points);
  69. attribute = memorize ((int) sizeof (* attribute));
  70. string_copy ((attribute->name = memorize (string_length (name) + 1)), name);
  71. memory_copy ((attribute->points = memorize ((int) sizeof (* attribute->points))), points, (int) sizeof (* points));
  72. for (;;) {
  73. action = (number_t) va_arg (list, int);
  74. if (action > 0) {
  75. attribute->positive_count += 1;
  76. attribute->positive = memorize ((int) sizeof (action));
  77. attribute->positive [attribute->positive_count - 1] = (action_t) action;
  78. } else if (action < 0) {
  79. attribute->negative_count += 1;
  80. attribute->negative = memorize ((int) sizeof (action));
  81. attribute->negative [attribute->negative_count - 1] = (action_t) action;
  82. } else {
  83. break;
  84. }
  85. }
  86. va_end (list);
  87. return (attribute);
  88. }
  89. void game_render_skill (skill_t * skill, number_t x, number_t y) {
  90. curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x, y);
  91. curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
  92. curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y);
  93. curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y);
  94. curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y);
  95. curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x = 34, y);
  96. }
  97. void game_render_attribute (attribute_t * attribute, number_t x, number_t y) {
  98. curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x, y);
  99. curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
  100. curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y);
  101. curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y);
  102. curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y);
  103. curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x = 34, y);
  104. }
  105. void play_game (void) {
  106. bundle_t skill_points = { 10, 120, 0, 0 };
  107. bundle_t attribute_points = { 1, 12, 0, 0 };
  108. skill_t * blades, * axes, * bows, * spears, * puppet_magic, * nature_magic, * rune_magic, * charm_magic;
  109. attribute_t * strength, * edurance, * wisdom, * agility;
  110. strength = game_attribute ("Strength", & attribute_points, GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0);
  111. edurance = game_attribute ("Edurance", & attribute_points, GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0);
  112. wisdom = game_attribute ("Wisdom", & attribute_points, GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0);
  113. agility = game_attribute ("Agility", & attribute_points, GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0);
  114. blades = game_skill ("Blades", & skill_points, GAME_ACTION_SWING_BLADE, 0);
  115. axes = game_skill ("Axes", & skill_points, GAME_ACTION_SWING_AXE, 0);
  116. bows = game_skill ("Bows", & skill_points, GAME_ACTION_SHOOT_ARROW, 0);
  117. spears = game_skill ("Spears", & skill_points, GAME_ACTION_THROW_SPEAR, 0);
  118. puppet_magic = game_skill ("Puppet Magic", & skill_points, GAME_ACTION_SUMMON_PUPPET, 0);
  119. nature_magic = game_skill ("Nature Magic", & skill_points, GAME_ACTION_CALL_NATURE, 0);
  120. rune_magic = game_skill ("Rune Magic", & skill_points, GAME_ACTION_CITE_RUNE, 0);
  121. charm_magic = game_skill ("Charm Magic", & skill_points, GAME_ACTION_CAST_CHARM, 0);
  122. game_configure ();
  123. while (curses_active) {
  124. curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
  125. curses_render_string ("Attributes:", COLOUR_WHITE, EFFECT_BOLD, 0, 0);
  126. game_render_attribute (strength, 2, 1);
  127. game_render_attribute (edurance, 2, 2);
  128. game_render_attribute (wisdom, 2, 3);
  129. game_render_attribute (agility, 2, 4);
  130. curses_render_string ("Skills:", COLOUR_WHITE, EFFECT_BOLD, 0, 5);
  131. game_render_skill (blades, 2, 6);
  132. game_render_skill (axes, 2, 7);
  133. game_render_skill (bows, 2, 8);
  134. game_render_skill (spears, 2, 9);
  135. game_render_skill (puppet_magic, 2, 10);
  136. game_render_skill (nature_magic, 2, 11);
  137. game_render_skill (rune_magic, 2, 12);
  138. game_render_skill (charm_magic, 2, 13);
  139. curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player.x, player.y);
  140. curses_synchronize ();
  141. game_synchronize ();
  142. }
  143. //~char*a,*b,*c;
  144. //~a=memorize(12); string_copy(a,"heyo world\n"); terminal_colour(1,1); echo(a); terminal_cancel();
  145. //~b=memorize( 3); string_copy(b,"!\n"); terminal_colour(2,1); echo(b); terminal_cancel();
  146. //~c=memorize(12); string_copy(c,"cyaa world\n"); terminal_colour(3,1); echo(c); terminal_cancel();
  147. //~out(memory_store,512);
  148. //~terminal_colour(1,1); echo(number_to_string((int)a)); terminal_cancel();
  149. //~terminal_colour(2,1); echo(number_to_string((int)b)); terminal_cancel();
  150. //~terminal_colour(3,1); echo(number_to_string((int)c)); terminal_cancel(); echo("\n");
  151. //~terminal_colour(1,1); echo(a); terminal_cancel();
  152. //~terminal_colour(2,1); echo(b); terminal_cancel();
  153. //~terminal_colour(3,1); echo(c); terminal_cancel();
  154. }
  155. #endif