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.

323 lines
11KB

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