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.

335 lines
12KB

  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_x.h"
  10. /*
  11. So, what are actually getters and setters, and why you should never use them? Lets explain.
  12. */
  13. #define MEMORY_LIMIT (1024 * 1024)
  14. static number_t game_memory_length = 0;
  15. static memory_t * game_memory = NULL;
  16. static procedure_t game_free_memory (memory_t memory) {
  17. ++game_memory_length;
  18. game_memory = reallocate (game_memory, game_memory_length * (number_t) sizeof (* game_memory));
  19. game_memory [game_memory_length - 1] = memory;
  20. }
  21. procedure_t game_clean_up (procedure_t) {
  22. for (number_t pointer = 0; pointer < game_memory_length; ++pointer) {
  23. game_memory [pointer] = deallocate (game_memory [pointer]);
  24. }
  25. game_memory = deallocate (game_memory);
  26. }
  27. number_t randomize (number_t lower, number_t upper) {
  28. return ((number_t) rand () % (upper - lower + 1) + lower);
  29. }
  30. bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current) {
  31. bundle_t * bundle = allocate ((number_t) sizeof (* bundle));
  32. bundle->minimum = minimum;
  33. bundle->maximum = maximum;
  34. bundle->current = current;
  35. game_free_memory (bundle);
  36. return (bundle);
  37. }
  38. symbol_t * game_symbol (number_t character, number_t colour, number_t effect) {
  39. symbol_t * symbol = allocate ((number_t) sizeof (* symbol));
  40. symbol->character = character;
  41. symbol->colour = colour;
  42. symbol->effect = effect;
  43. game_free_memory (symbol);
  44. return (symbol);
  45. }
  46. sprite_t * game_sprite (number_t width, number_t height, memory_t data) {
  47. sprite_t * sprite = allocate ((number_t) sizeof (* sprite));
  48. sprite->width = width;
  49. sprite->height = height;
  50. sprite->data = data;
  51. game_free_memory (sprite);
  52. return (sprite);
  53. }
  54. generator_t * game_generator (generate_t function) {
  55. generator_t * generator = allocate ((number_t) sizeof (* generator));
  56. generator->generate = function;
  57. game_free_memory (generator);
  58. return (generator);
  59. }
  60. attribute_t * game_attribute (string_t name, bundle_t * points, ...) {
  61. va_list list;
  62. attribute_t * attribute = allocate ((number_t) sizeof (* attribute));
  63. string_copy ((attribute->name = allocate (string_length (name) + 1)), name);
  64. memory_copy ((attribute->points = allocate ((number_t) sizeof (* attribute->points))), points, (number_t) sizeof (* points));
  65. va_start (list, points);
  66. for (;;) {
  67. number_t action = (number_t) va_arg (list, int);
  68. if (action > 0) {
  69. attribute->positive_count += 1;
  70. attribute->positive = reallocate (attribute->positive, attribute->positive_count * (number_t) sizeof (action));
  71. attribute->positive [attribute->positive_count - 1] = (action_t) action;
  72. } else if (action < 0) {
  73. attribute->negative_count += 1;
  74. attribute->negative = reallocate (attribute->negative, attribute->negative_count * (number_t) sizeof (action));
  75. attribute->negative [attribute->negative_count - 1] = (action_t) -action;
  76. } else break;
  77. }
  78. va_end (list);
  79. game_free_memory (attribute->name);
  80. game_free_memory (attribute->points);
  81. game_free_memory (attribute->positive);
  82. game_free_memory (attribute->negative);
  83. game_free_memory (attribute);
  84. return (attribute);
  85. }
  86. skill_t * game_skill (string_t name, bundle_t * points, ...) {
  87. va_list list;
  88. skill_t * skill = allocate ((number_t) sizeof (* skill));
  89. string_copy ((skill->name = allocate (string_length (name) + 1)), name);
  90. memory_copy ((skill->points = allocate ((number_t) sizeof (* skill->points))), points, (number_t) sizeof (* points));
  91. va_start (list, points);
  92. for (;;) {
  93. action_t action = (action_t) va_arg (list, int);
  94. if (action > 0) {
  95. skill->positive_count += 1;
  96. skill->positive = reallocate (skill->positive, skill->positive_count * (number_t) sizeof (action));
  97. skill->positive [skill->positive_count - 1] = action;
  98. } else break;
  99. }
  100. va_end (list);
  101. game_free_memory (skill->name);
  102. game_free_memory (skill->points);
  103. game_free_memory (skill->positive);
  104. game_free_memory (skill);
  105. return (skill);
  106. }
  107. bot_t * game_bot (string_t name, symbol_t * symbol, sprite_t * sprite, bundle_t * health, bundle_t * mana, bundle_t * stamina) {
  108. bot_t * bot = allocate ((number_t) sizeof (* bot));
  109. string_copy ((bot->name = allocate (string_length (name) + 1)), name);
  110. bot->x = 0;
  111. bot->y = 0;
  112. memory_copy ((bot->symbol = allocate ((number_t) sizeof (* bot->symbol))), symbol, (number_t) sizeof (* symbol));
  113. memory_copy ((bot->sprite = allocate ((number_t) sizeof (* bot->sprite))), sprite, (number_t) sizeof (* sprite));
  114. memory_copy ((bot->health = allocate ((number_t) sizeof (* bot->health))), health, (number_t) sizeof (* health));
  115. memory_copy ((bot->mana = allocate ((number_t) sizeof (* bot->mana))), mana, (number_t) sizeof (* mana));
  116. memory_copy ((bot->stamina = allocate ((number_t) sizeof (* bot->stamina))), stamina, (number_t) sizeof (* stamina));
  117. game_free_memory (bot->name);
  118. game_free_memory (bot->symbol);
  119. game_free_memory (bot->sprite);
  120. game_free_memory (bot->health);
  121. game_free_memory (bot->mana);
  122. game_free_memory (bot->stamina);
  123. game_free_memory (bot);
  124. return (bot);
  125. }
  126. player_t * game_player (string_t name, symbol_t * symbol, sprite_t * sprite, bundle_t * health, bundle_t * mana, bundle_t * stamina, ...) {
  127. va_list list;
  128. player_t * player = allocate ((number_t) sizeof (* player));
  129. string_copy ((player->name = allocate (string_length (name) + 1)), name);
  130. player->x = 3;
  131. player->y = 3;
  132. memory_copy ((player->symbol = allocate ((number_t) sizeof (* player->symbol))), symbol, (number_t) sizeof (* symbol));
  133. memory_copy ((player->sprite = allocate ((number_t) sizeof (* player->sprite))), sprite, (number_t) sizeof (* sprite));
  134. memory_copy ((player->health = allocate ((number_t) sizeof (* player->health))), health, (number_t) sizeof (* health));
  135. memory_copy ((player->mana = allocate ((number_t) sizeof (* player->mana))), mana, (number_t) sizeof (* mana));
  136. memory_copy ((player->stamina = allocate ((number_t) sizeof (* player->stamina))), stamina, (number_t) sizeof (* stamina));
  137. player->attribute_count = 0;
  138. player->skill_count = 0;
  139. va_start (list, stamina);
  140. for (;;) {
  141. attribute_t * attribute = (attribute_t *) va_arg (list, void *);
  142. if (attribute != NULL) {
  143. player->attribute_count += 1;
  144. player->attributes = reallocate (player->attributes, player->attribute_count * (number_t) sizeof (* player->attributes));
  145. player->attributes [player->attribute_count - 1] = attribute;
  146. } else break;
  147. }
  148. for (;;) {
  149. skill_t * skill = (skill_t *) va_arg (list, void *);
  150. if (skill != NULL) {
  151. player->skill_count += 1;
  152. player->skills = reallocate (player->skills, player->skill_count * (number_t) sizeof (* player->skills));
  153. player->skills [player->skill_count - 1] = skill;
  154. } else break;
  155. }
  156. va_end (list);
  157. game_free_memory (player->name);
  158. game_free_memory (player->symbol);
  159. game_free_memory (player->sprite);
  160. game_free_memory (player->health);
  161. game_free_memory (player->mana);
  162. game_free_memory (player->stamina);
  163. game_free_memory (player->attributes);
  164. game_free_memory (player->skills);
  165. game_free_memory (player);
  166. return (player);
  167. }
  168. block_t * game_block (string_t name, symbol_t * symbol, sprite_t * sprite, number_t collision, number_t override) {
  169. block_t * block = allocate ((number_t) sizeof (* block));
  170. string_copy ((block->name = allocate (string_length (name) + 1)), name);
  171. memory_copy ((block->symbol = allocate ((number_t) sizeof (* block->symbol))), symbol, (number_t) sizeof (* symbol));
  172. memory_copy ((block->sprite = allocate ((number_t) sizeof (* block->sprite))), sprite, (number_t) sizeof (* sprite));
  173. block->collision = collision;
  174. block->override = override;
  175. game_free_memory (block->name);
  176. game_free_memory (block->symbol);
  177. game_free_memory (block->sprite);
  178. game_free_memory (block);
  179. return (block);
  180. }
  181. world_t * game_world (number_t width, number_t height, ...) {
  182. va_list list;
  183. world_t * world = allocate ((number_t) sizeof (* world));
  184. world->width = width;
  185. world->height = height;
  186. world->block = allocate (width * height * (number_t) sizeof (* world->block));
  187. va_start (list, height);
  188. for (;;) {
  189. generator_t * generator = (generator_t *) va_arg (list, void *);
  190. if (generator != NULL) {
  191. memory_t data = (memory_t) va_arg (list, void *);
  192. number_t w = (number_t) va_arg (list, int);
  193. number_t h = (number_t) va_arg (list, int);
  194. number_t x = (number_t) va_arg (list, int);
  195. number_t y = (number_t) va_arg (list, int);
  196. generator->generate (world, data, w, h, x, y);
  197. } else break;
  198. }
  199. va_end (list);
  200. game_free_memory (world->bots);
  201. game_free_memory (world->block);
  202. game_free_memory (world);
  203. return (world);
  204. }
  205. procedure_t game_render_attribute (attribute_t * attribute, number_t x, number_t y) {
  206. curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
  207. curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
  208. curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y);
  209. curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y);
  210. curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y);
  211. curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y);
  212. }
  213. procedure_t game_render_skill (skill_t * skill, number_t x, number_t y) {
  214. curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
  215. curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
  216. curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y);
  217. curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y);
  218. curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y);
  219. curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y);
  220. }
  221. procedure_t game_render_bot (bot_t * bot) {
  222. curses_render_character ((char) bot->symbol->character, bot->symbol->colour, bot->symbol->effect, bot->x, bot->y);
  223. }
  224. procedure_t game_render_player (player_t * player) {
  225. curses_render_character ((char) player->symbol->character, player->symbol->colour, player->symbol->effect, player->x, player->y);
  226. }
  227. procedure_t game_render_block (block_t * block, number_t x, number_t y) {
  228. curses_render_character ((char) block->symbol->character, block->symbol->colour, block->symbol->effect, x, y);
  229. }
  230. procedure_t game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height) {
  231. for (number_t j = 0; (j < height) && (j < world->height); ++j) {
  232. for (number_t i = 0; (i < width) && (i < world->width); ++i) {
  233. game_render_block (world->block [j * world->width + i], i + x, j + y);
  234. }
  235. }
  236. for (number_t index = 0; index < world->bot_count; ++index) {
  237. game_render_bot (& world->bots [index]);
  238. }
  239. }
  240. #endif