瀏覽代碼

Added unused sprites...

master
父節點
當前提交
21bad5438f
共有 5 個文件被更改,包括 166 次插入145 次删除
  1. +62
    -58
      chapter/chapter_0.c
  2. +9
    -9
      chapter/chapter_0.h
  3. +36
    -41
      chapter/chapter_5.c
  4. +25
    -9
      chapter/chapter_5.h
  5. +34
    -28
      chapter/chapter_6.c

+ 62
- 58
chapter/chapter_0.c 查看文件

@@ -172,51 +172,51 @@ use it to interate the strings, while in "unlimited" versions, we iterate on poi
same results, you can use any of them. same results, you can use any of them.
*/ */


int string_compare (char * string_0, char * string_1) {
fatal_failure (string_0 == NULL, "string_compare: Destination string is null pointer."); // This will be seen in next 5 functions too, we don't want NULL here.
fatal_failure (string_1 == NULL, "string_compare: Source string is null pointer.");
int string_compare (char * destination, char * source) {
fatal_failure (destination == NULL, "string_compare: Destination string is null pointer."); // This will be seen in next 5 functions too, we don't want NULL here.
fatal_failure (source == NULL, "string_compare: Source string is null pointer.");


for (; (* string_0 != CHARACTER_NULL) && (* string_1 != CHARACTER_NULL); ++string_0, ++string_1) { // We iterate until either string reaches the null character.
if (* string_0 != * string_1) { // In case that characters at the same offset are different:
return (FALSE); // > We return FALSE, 0, since strings aren't the same...
for (; (* destination != CHARACTER_NULL) && (* source != CHARACTER_NULL); ++destination, ++source) { // We iterate until either string reaches the null character.
if (* destination != * source) { // In case that characters at the same offset are different:
return (FALSE); // > We return FALSE, 0, since strings aren't the same...
} }
} }


if (* string_0 != * string_1) { // Now, we'll do one last termination check.
if (* destination != * source) { // Now, we'll do one last termination check.
return (FALSE); return (FALSE);
} }


return (TRUE); // Otherwise, strings are same, we return TRUE, 1.
return (TRUE); // Otherwise, strings are same, we return TRUE, 1.
} }


char * string_copy (char * string_0, char * string_1) {
char * result = string_0; // We need to save pointer to destination string before changing it.
char * string_copy (char * destination, char * source) {
char * result = destination; // We need to save pointer to destination string before changing it.


fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_copy: Source string is null pointer.");
fatal_failure (destination == NULL, "string_copy: Destination string is null pointer.");
fatal_failure (source == NULL, "string_copy: Source string is null pointer.");


for (; * string_1 != CHARACTER_NULL; ++string_0, ++string_1) { // This time and in next function, we iterate only source string.
* string_0 = * string_1; // And we assign character at the same offset to destination string (aka copy it).
for (; * source != CHARACTER_NULL; ++destination, ++source) { // This time and in next function, we iterate only source string.
* destination = * source; // And we assign character at the same offset to destination string (aka copy it).
} }


* string_0 = CHARACTER_NULL; // Copying null termination, since the loop stopped on that condition.
* destination = CHARACTER_NULL; // Copying null termination, since the loop stopped on that condition.


return (result); // Lastly, we return the destination string, in order to be able to bind functions.
return (result); // Lastly we return the destination string, in order to be able to bind functions.
} }


char * string_concatenate (char * string_0, char * string_1) {
char * result = string_0;
char * string_concatenate (char * destination, char * source) {
char * result = destination;


fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer.");
fatal_failure (destination == NULL, "string_concatenate: Destination string is null pointer.");
fatal_failure (source == NULL, "string_concatenate: Source string is null pointer.");


string_0 += string_length (string_0); // We'll first offset destination string to the end of it.
// Because we want to start copying from the end, aka concatenate it.
for (; * string_1 != CHARACTER_NULL; ++string_0, ++string_1) { // The rest of the function is same as string_copy, so:
* string_0 = * string_1; // We could even use it here, but that defies the purpose of learning now.
destination += string_length (destination); // We'll first offset destination string to the end of it.
// Because we want to start copying from the end, aka concatenate it.
for (; * source != CHARACTER_NULL; ++destination, ++source) { // The rest of the function is same as string_copy, so:
* destination = * source; // We could even use it here, but that defies the purpose of learning now.
} }


* string_0 = CHARACTER_NULL; // Again, assign null termination.
* destination = CHARACTER_NULL; // Again, assign null termination.


return (result); return (result);
} }
@@ -232,56 +232,54 @@ write comments for 'string_copy_limit', 'string_concatenate_limit' and 'string_r
current knowledge of C language. current knowledge of C language.
*/ */


int string_compare_limit (char * string_0, char * string_1, int limit) {
int string_compare_limit (char * destination, char * source, int limit) {
int offset; int offset;


fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null pointer."); // This is the new trend, check for unimportant things.
fatal_failure (string_1 == NULL, "string_compare_limit: Source string is null pointer."); // At least this isn't too verbose. I hope...
fatal_failure (destination == NULL, "string_compare_limit: Destination string is null pointer."); // This is the new trend, check for unimportant things.
fatal_failure (source == NULL, "string_compare_limit: Source string is null pointer."); // At least this isn't too verbose. I hope...


for (offset = 0; offset < limit; ++offset) { // Now, we'll iterate until 'limit' is reached, but it can overrun.
if (string_0 [offset] != string_1 [offset]) { // All said here applies to next two functions as well...
return (FALSE); // As soon as 2 characters mismatch, they're not same, we return FALSE.
for (offset = 0; offset < limit; ++offset) { // Now, we'll iterate until 'limit' is reached, but it can overrun.
if (destination [offset] != source [offset]) { // All said here applies to next two functions as well...
return (FALSE); // As soon as 2 characters mismatch, they're not same, we return FALSE.
} }
} }


return (TRUE); // Otherwise, we're reached the end, they're same, we return TRUE.
return (TRUE); // Otherwise, we're reached the end, they're same, we return TRUE.
} }


char * string_copy_limit (char * string_0, char * string_1, int limit) {
char * string_copy_limit (char * destination, char * source, int limit) {
int offset; int offset;


fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null pointer.");
fatal_failure (destination == NULL, "string_copy_limit: Destination string is null pointer.");


if ((limit <= 0) || (string_1 == NULL)) {
return (string_0);
if ((limit <= 0) || (source == NULL)) {
return (destination);
} }


for (offset = 0; offset < limit; ++offset) { for (offset = 0; offset < limit; ++offset) {
string_0 [offset] = string_1 [offset];
destination [offset] = source [offset];
} }


return (string_0);
return (destination);
} }


char * string_concatenate_limit (char * string_0, char * string_1, int limit) {
int offset, length_0, length_1;
char * string_concatenate_limit (char * destination, char * source, int limit) {
int offset, destination_length, source_length;


fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null pointer.");
fatal_failure (destination == NULL, "string_concatenate_limit: Destination string is null pointer.");


if ((limit <= 0) || (string_1 == NULL)) {
return (string_0);
if ((limit <= 0) || (source == NULL)) {
return (destination);
} }


length_0 = string_length (string_0);
length_1 = string_length (string_1);
destination_length = string_length (destination);
source_length = string_length (source);


for (offset = 0; (offset < length_1) && (offset < limit); ++offset) {
string_0 [length_0 + offset] = string_1 [offset];
for (offset = 0; (offset < source_length) && (offset < limit); ++offset) {
destination [destination_length + offset] = source [offset];
} }


return (string_0);
return (destination);
} }


char * string_reverse_limit (char * string, int limit) { char * string_reverse_limit (char * string, int limit) {
@@ -349,14 +347,17 @@ char * string_realign (char * string, int amount, char character) { //
Ignore what next two functions do, it's about memory management that we'll cover in later chapters. Ignore what next two functions do, it's about memory management that we'll cover in later chapters.
*/ */


int memory_compare (void * memory, void * source, int length) {
int memory_compare (void * destination, void * source, int length) {
int offset; int offset;


char * cast_0 = (char *) memory;
char * cast_0 = (char *) destination;
char * cast_1 = (char *) source; char * cast_1 = (char *) source;


fatal_failure (memory == NULL, "memory_compare: Memory is null pointer.");
fatal_failure (source == NULL, "memory_compare: Source is null pointer.");
fatal_failure (destination == NULL, "memory_compare: Memory is null pointer.");

if (source == NULL) {
return (FALSE);
}


for (offset = 0; offset != length; ++offset) { for (offset = 0; offset != length; ++offset) {
if (cast_0 [offset] != cast_1 [offset]) { if (cast_0 [offset] != cast_1 [offset]) {
@@ -367,14 +368,17 @@ int memory_compare (void * memory, void * source, int length) {
return (TRUE); return (TRUE);
} }


void memory_copy (void * memory, void * source, int length) {
void memory_copy (void * destination, void * source, int length) {
int offset; int offset;


char * cast_0 = (char *) memory;
char * cast_0 = (char *) destination;
char * cast_1 = (char *) source; char * cast_1 = (char *) source;


fatal_failure (memory == NULL, "memory_copy: Memory is null pointer.");
fatal_failure (source == NULL, "memory_copy: Source is null pointer.");
fatal_failure (destination == NULL, "memory_copy: Memory is null pointer.");

if (source == NULL) {
return;
}


for (offset = 0; offset != length; ++offset) { for (offset = 0; offset != length; ++offset) {
cast_0 [offset] = cast_1 [offset]; cast_0 [offset] = cast_1 [offset];


+ 9
- 9
chapter/chapter_0.h 查看文件

@@ -207,15 +207,15 @@ extern void * deallocate (void * data );


extern int string_length (char * string); // We deal with strings a lot in this program, so string functions will be more important than character functions from chapter one. extern int string_length (char * string); // We deal with strings a lot in this program, so string functions will be more important than character functions from chapter one.


extern int string_compare (char * string_0, char * string_1); // See how nicely they align, right?
extern char * string_copy (char * string_0, char * string_1);
extern char * string_concatenate (char * string_0, char * string_1);
extern char * string_reverse (char * string); // Notice last function, we didn't align ');'...
extern int string_compare_limit (char * string_0, char * string_1, int limit); // These ones too, it's beautiful (in my opinion), tho some consider it useless.
extern char * string_copy_limit (char * string_0, char * string_1, int limit);
extern char * string_concatenate_limit (char * string_0, char * string_1, int limit);
extern char * string_reverse_limit (char * string, int limit); // And we align the last argument in this case, use whatever you prefer.
extern int string_compare (char * destination, char * source); // See how nicely they align, right?
extern char * string_copy (char * destination, char * source);
extern char * string_concatenate (char * destination, char * source);
extern char * string_reverse (char * string); // Notice last function, we didn't align ');'...
extern int string_compare_limit (char * destination, char * source, int limit); // These ones too, it's beautiful (in my opinion), tho some consider it useless.
extern char * string_copy_limit (char * destination, char * source, int limit);
extern char * string_concatenate_limit (char * destination, char * source, int limit);
extern char * string_reverse_limit (char * string, int limit); // And we align the last argument in this case, use whatever you prefer.


extern char * string_realign (char * string, int amount, char character); // This is a simple function that realigns a string to right, we'll use it way later ahead. extern char * string_realign (char * string, int amount, char character); // This is a simple function that realigns a string to right, we'll use it way later ahead.




+ 36
- 41
chapter/chapter_5.c 查看文件

@@ -37,7 +37,7 @@ procedure_t game_clean_up (procedure_t) {
} }


number_t randomize (number_t lower, number_t upper) { number_t randomize (number_t lower, number_t upper) {
return ((number_t) rand () % (upper - lower) + lower);
return ((number_t) rand () % (upper - lower + 1) + lower);
} }


bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current) { bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current) {
@@ -64,6 +64,18 @@ symbol_t * game_symbol (number_t character, number_t colour, number_t effect) {
return (symbol); return (symbol);
} }


sprite_t * game_sprite (number_t width, number_t height, memory_t data) {
sprite_t * sprite = allocate ((number_t) sizeof (* sprite));

sprite->width = width;
sprite->height = height;
sprite->data = data;

game_free_memory (sprite);

return (sprite);
}

generator_t * game_generator (generate_t function) { generator_t * game_generator (generate_t function) {
generator_t * generator = allocate ((number_t) sizeof (* generator)); generator_t * generator = allocate ((number_t) sizeof (* generator));


@@ -141,7 +153,7 @@ skill_t * game_skill (string_t name, bundle_t * points, ...) {
return (skill); return (skill);
} }


bot_t * game_bot (string_t name, symbol_t * symbol, bundle_t * health, bundle_t * mana, bundle_t * stamina) {
bot_t * game_bot (string_t name, symbol_t * symbol, sprite_t * sprite, bundle_t * health, bundle_t * mana, bundle_t * stamina) {
bot_t * bot = allocate ((number_t) sizeof (* bot)); bot_t * bot = allocate ((number_t) sizeof (* bot));


string_copy ((bot->name = allocate (string_length (name) + 1)), name); string_copy ((bot->name = allocate (string_length (name) + 1)), name);
@@ -150,12 +162,14 @@ bot_t * game_bot (string_t name, symbol_t * symbol, bundle_t * health, bundle_t
bot->y = 0; bot->y = 0;


memory_copy ((bot->symbol = allocate ((number_t) sizeof (* bot->symbol))), symbol, (number_t) sizeof (* symbol)); memory_copy ((bot->symbol = allocate ((number_t) sizeof (* bot->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((bot->sprite = allocate ((number_t) sizeof (* bot->sprite))), sprite, (number_t) sizeof (* sprite));
memory_copy ((bot->health = allocate ((number_t) sizeof (* bot->health))), health, (number_t) sizeof (* health)); memory_copy ((bot->health = allocate ((number_t) sizeof (* bot->health))), health, (number_t) sizeof (* health));
memory_copy ((bot->mana = allocate ((number_t) sizeof (* bot->mana))), mana, (number_t) sizeof (* mana)); memory_copy ((bot->mana = allocate ((number_t) sizeof (* bot->mana))), mana, (number_t) sizeof (* mana));
memory_copy ((bot->stamina = allocate ((number_t) sizeof (* bot->stamina))), stamina, (number_t) sizeof (* stamina)); memory_copy ((bot->stamina = allocate ((number_t) sizeof (* bot->stamina))), stamina, (number_t) sizeof (* stamina));


game_free_memory (bot->name); game_free_memory (bot->name);
game_free_memory (bot->symbol); game_free_memory (bot->symbol);
game_free_memory (bot->sprite);
game_free_memory (bot->health); game_free_memory (bot->health);
game_free_memory (bot->mana); game_free_memory (bot->mana);
game_free_memory (bot->stamina); game_free_memory (bot->stamina);
@@ -164,7 +178,7 @@ bot_t * game_bot (string_t name, symbol_t * symbol, bundle_t * health, bundle_t
return (bot); return (bot);
} }


player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bundle_t * mana, bundle_t * stamina, ...) {
player_t * game_player (string_t name, symbol_t * symbol, sprite_t * sprite, bundle_t * health, bundle_t * mana, bundle_t * stamina, ...) {
va_list list; va_list list;


player_t * player = allocate ((number_t) sizeof (* player)); player_t * player = allocate ((number_t) sizeof (* player));
@@ -175,6 +189,7 @@ player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bun
player->y = 3; player->y = 3;


memory_copy ((player->symbol = allocate ((number_t) sizeof (* player->symbol))), symbol, (number_t) sizeof (* symbol)); memory_copy ((player->symbol = allocate ((number_t) sizeof (* player->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((player->sprite = allocate ((number_t) sizeof (* player->sprite))), sprite, (number_t) sizeof (* sprite));
memory_copy ((player->health = allocate ((number_t) sizeof (* player->health))), health, (number_t) sizeof (* health)); memory_copy ((player->health = allocate ((number_t) sizeof (* player->health))), health, (number_t) sizeof (* health));
memory_copy ((player->mana = allocate ((number_t) sizeof (* player->mana))), mana, (number_t) sizeof (* mana)); memory_copy ((player->mana = allocate ((number_t) sizeof (* player->mana))), mana, (number_t) sizeof (* mana));
memory_copy ((player->stamina = allocate ((number_t) sizeof (* player->stamina))), stamina, (number_t) sizeof (* stamina)); memory_copy ((player->stamina = allocate ((number_t) sizeof (* player->stamina))), stamina, (number_t) sizeof (* stamina));
@@ -208,6 +223,7 @@ player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bun


game_free_memory (player->name); game_free_memory (player->name);
game_free_memory (player->symbol); game_free_memory (player->symbol);
game_free_memory (player->sprite);
game_free_memory (player->health); game_free_memory (player->health);
game_free_memory (player->mana); game_free_memory (player->mana);
game_free_memory (player->stamina); game_free_memory (player->stamina);
@@ -218,18 +234,20 @@ player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bun
return (player); return (player);
} }


block_t * game_block (string_t name, symbol_t * symbol, number_t collision, number_t override) {
block_t * game_block (string_t name, symbol_t * symbol, sprite_t * sprite, number_t collision, number_t override) {
block_t * block = allocate ((number_t) sizeof (* block)); block_t * block = allocate ((number_t) sizeof (* block));


string_copy ((block->name = allocate (string_length (name) + 1)), name); string_copy ((block->name = allocate (string_length (name) + 1)), name);


memory_copy ((block->symbol = allocate ((number_t) sizeof (* block->symbol))), symbol, (number_t) sizeof (* symbol)); memory_copy ((block->symbol = allocate ((number_t) sizeof (* block->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((block->sprite = allocate ((number_t) sizeof (* block->sprite))), sprite, (number_t) sizeof (* sprite));


block->collision = collision; block->collision = collision;
block->override = override; block->override = override;


game_free_memory (block->name); game_free_memory (block->name);
game_free_memory (block->symbol); game_free_memory (block->symbol);
game_free_memory (block->sprite);
game_free_memory (block); game_free_memory (block);


return (block); return (block);
@@ -269,46 +287,23 @@ world_t * game_world (number_t width, number_t height, ...) {
return (world); return (world);
} }


procedure_t game_render_attribute (number_t x, number_t y, ...) {
va_list list;

va_start (list, y);

for (;; ++y) {
attribute_t * attribute = (attribute_t *) va_arg (list, void *);

if (attribute != NULL) {
curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y);
curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y);
curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y);
curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y);
} else break;
}

va_end (list);
procedure_t game_render_attribute (attribute_t * attribute, number_t x, number_t y) {
curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y);
curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y);
curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y);
curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y);
} }


procedure_t game_render_skill (number_t x, number_t y, ...) {
va_list list;

va_start (list, y);

for (;; ++y) {
skill_t * skill = (skill_t *) va_arg (list, void *);

if (skill != NULL) {
curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y);
curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y);
curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y);
curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y);
} else break;
}
procedure_t game_render_skill (skill_t * skill, number_t x, number_t y) {
curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x + 0, y);
curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y);
curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x + 20, y);
curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x + 24, y);
curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x + 28, y);
curses_render_string (" ]", COLOUR_GREY, EFFECT_BOLD, x + 32, y);


va_end (list);
} }


procedure_t game_render_bot (bot_t * bot) { procedure_t game_render_bot (bot_t * bot) {


+ 25
- 9
chapter/chapter_5.h 查看文件

@@ -39,13 +39,23 @@ typedef enum action_t {
} action_t; } action_t;


typedef struct bundle_t { typedef struct bundle_t {
number_t minimum, maximum, current;
number_t minimum;
number_t maximum;
number_t current;
} bundle_t; } bundle_t;


typedef struct symbol_t { typedef struct symbol_t {
number_t character, colour, effect;
number_t character;
number_t colour;
number_t effect;
} symbol_t; } symbol_t;


typedef struct sprite_t {
number_t width;
number_t height;
memory_t data;
} sprite_t;

typedef struct attribute_t { typedef struct attribute_t {
string_t name; string_t name;
number_t positive_count; number_t positive_count;
@@ -68,6 +78,7 @@ typedef struct bot_t {
number_t x; number_t x;
number_t y; number_t y;
symbol_t * symbol; symbol_t * symbol;
sprite_t * sprite;
bundle_t * health; bundle_t * health;
bundle_t * mana; bundle_t * mana;
bundle_t * stamina; bundle_t * stamina;
@@ -78,6 +89,7 @@ typedef struct player_t {
number_t x; number_t x;
number_t y; number_t y;
symbol_t * symbol; symbol_t * symbol;
sprite_t * sprite;
bundle_t * health; bundle_t * health;
bundle_t * mana; bundle_t * mana;
bundle_t * stamina; bundle_t * stamina;
@@ -92,6 +104,7 @@ typedef struct block_t {
number_t collision; number_t collision;
number_t override; number_t override;
symbol_t * symbol; symbol_t * symbol;
sprite_t * sprite;
} block_t; } block_t;


typedef struct world_t { typedef struct world_t {
@@ -110,27 +123,30 @@ extern procedure_t game_clean_up (procedure_t);


extern number_t randomize (number_t lower, number_t upper); extern number_t randomize (number_t lower, number_t upper);


extern bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current);
extern symbol_t * game_symbol (number_t character, number_t colour, number_t effect);
extern bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current);
extern symbol_t * game_symbol (number_t character, number_t colour, number_t effect);
extern sprite_t * game_sprite (number_t width, number_t height, memory_t data);


extern generator_t * game_generator (generate_t generator); extern generator_t * game_generator (generate_t generator);


extern attribute_t * game_attribute (string_t name, bundle_t * points, ...); extern attribute_t * game_attribute (string_t name, bundle_t * points, ...);
extern skill_t * game_skill (string_t name, bundle_t * points, ...); extern skill_t * game_skill (string_t name, bundle_t * points, ...);


extern bot_t * game_bot (string_t name, symbol_t * symbol, bundle_t * health, bundle_t * mana, bundle_t * stamina);
extern player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bundle_t * mana, bundle_t * stamina, ...);
extern bot_t * game_bot (string_t name, symbol_t * symbol, sprite_t * sprite, bundle_t * health, bundle_t * mana, bundle_t * stamina);
extern player_t * game_player (string_t name, symbol_t * symbol, sprite_t * sprite, bundle_t * health, bundle_t * mana, bundle_t * stamina, ...);

extern block_t * game_block (string_t name, symbol_t * symbol, sprite_t * sprite, number_t collision, number_t override);


extern block_t * game_block (string_t name, symbol_t * symbol, number_t collision, number_t override);
extern world_t * game_world (number_t width, number_t height, ...); extern world_t * game_world (number_t width, number_t height, ...);


extern procedure_t game_render_attribute (number_t x, number_t y, ...);
extern procedure_t game_render_skill (number_t x, number_t y, ...);
extern procedure_t game_render_attribute (attribute_t * attribute, number_t x, number_t y);
extern procedure_t game_render_skill (skill_t * skill, number_t x, number_t y);


extern procedure_t game_render_bot (bot_t * bot); extern procedure_t game_render_bot (bot_t * bot);
extern procedure_t game_render_player (player_t * player); extern procedure_t game_render_player (player_t * player);


extern procedure_t game_render_block (block_t * block, number_t x, number_t y); extern procedure_t game_render_block (block_t * block, number_t x, number_t y);

extern procedure_t game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height); extern procedure_t game_render_world (world_t * world, number_t x, number_t y, number_t width, number_t height);


#endif #endif

+ 34
- 28
chapter/chapter_6.c 查看文件

@@ -51,37 +51,30 @@ procedure_t play_game (procedure_t) {
generator_t * rectangle_fill = game_generator (generate_rectangle_fill_function); generator_t * rectangle_fill = game_generator (generate_rectangle_fill_function);
generator_t * rectangle_line = game_generator (generate_rectangle_line_function); generator_t * rectangle_line = game_generator (generate_rectangle_line_function);


//~attribute_t * strength = game_attribute ("Strength", game_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0);
//~attribute_t * edurance = game_attribute ("Edurance", game_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0);
//~attribute_t * wisdom = game_attribute ("Wisdom", game_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0);
//~attribute_t * agility = game_attribute ("Agility", game_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0);

skill_t * blades = game_skill ("Blades", game_bundle (10, 120, 0), GAME_ACTION_SWING_BLADE, 0);
skill_t * axes = game_skill ("Axes", game_bundle (10, 120, 0), GAME_ACTION_SWING_AXE, 0);
skill_t * bows = game_skill ("Bows", game_bundle (10, 120, 0), GAME_ACTION_SHOOT_ARROW, 0);
skill_t * spears = game_skill ("Spears", game_bundle (10, 120, 0), GAME_ACTION_THROW_SPEAR, 0);
skill_t * puppet_magic = game_skill ("Puppet Magic", game_bundle (10, 120, 0), GAME_ACTION_SUMMON_PUPPET, 0);
skill_t * nature_magic = game_skill ("Nature Magic", game_bundle (10, 120, 0), GAME_ACTION_CALL_NATURE, 0);
skill_t * rune_magic = game_skill ("Rune Magic", game_bundle (10, 120, 0), GAME_ACTION_CITE_RUNE, 0);
skill_t * charm_magic = game_skill ("Charm Magic", game_bundle (10, 120, 0), GAME_ACTION_CAST_CHARM, 0);

bot_t * goblin = game_bot ("Goblin", game_symbol ('g', COLOUR_RED, EFFECT_NORMAL), game_bundle (0, 11, 11), game_bundle (0, 3, 3), game_bundle (0, 23, 23));
bot_t * hob_goblin = game_bot ("Hob Goblin", game_symbol ('g', COLOUR_RED, EFFECT_BOLD), game_bundle (0, 17, 17), game_bundle (0, 7, 7), game_bundle (0, 31, 31));
bot_t * orc = game_bot ("Orc", game_symbol ('G', COLOUR_RED, EFFECT_NORMAL), game_bundle (0, 23, 23), game_bundle (0, 5, 5), game_bundle (0, 47, 47));
bot_t * ogre = game_bot ("Ogre", game_symbol ('G', COLOUR_RED, EFFECT_BOLD), game_bundle (0, 37, 37), game_bundle (0, 2, 2), game_bundle (0, 83, 83));

player_t * player = game_player ("Riri", game_symbol ('@', COLOUR_CYAN, EFFECT_BOLD), game_bundle (0, 29, 29), game_bundle (0, 3, 3), game_bundle (0, 37, 37),
//~strength, edurance, wisdom, agility, NULL,
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));
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));
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));
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));

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),
game_attribute ("Strength", game_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0), game_attribute ("Strength", game_bundle (1, 12, 0), GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0),
game_attribute ("Edurance", game_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0), game_attribute ("Edurance", game_bundle (1, 12, 0), GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0),
game_attribute ("Wisdom", game_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0), game_attribute ("Wisdom", game_bundle (1, 12, 0), GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0),
game_attribute ("Agility", game_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0), game_attribute ("Agility", game_bundle (1, 12, 0), GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0),
NULL, NULL,
blades, axes, bows, spears, puppet_magic, nature_magic, rune_magic, charm_magic, NULL);

block_t * grass = game_block ("Grass", game_symbol (',', COLOUR_GREEN, EFFECT_BOLD), FALSE, FALSE);
block_t * stone_floor = game_block ("Stone Floor", game_symbol ('.', COLOUR_GREY, EFFECT_BOLD), FALSE, FALSE);
block_t * stone_wall = game_block ("Stone Wall", game_symbol ('#', COLOUR_GREY, EFFECT_BOLD), TRUE, FALSE);
game_skill ("Blades", game_bundle (10, 120, 0), GAME_ACTION_SWING_BLADE, 0),
game_skill ("Axes", game_bundle (10, 120, 0), GAME_ACTION_SWING_AXE, 0),
game_skill ("Bows", game_bundle (10, 120, 0), GAME_ACTION_SHOOT_ARROW, 0),
game_skill ("Spears", game_bundle (10, 120, 0), GAME_ACTION_THROW_SPEAR, 0),
game_skill ("Puppet Magic", game_bundle (10, 120, 0), GAME_ACTION_SUMMON_PUPPET, 0),
game_skill ("Nature Magic", game_bundle (10, 120, 0), GAME_ACTION_CALL_NATURE, 0),
game_skill ("Rune Magic", game_bundle (10, 120, 0), GAME_ACTION_CITE_RUNE, 0),
game_skill ("Charm Magic", game_bundle (10, 120, 0), GAME_ACTION_CAST_CHARM, 0),
NULL);

block_t * grass = game_block ("Grass", game_symbol (',', COLOUR_GREEN, EFFECT_BOLD), NULL, FALSE, FALSE);
block_t * stone_floor = game_block ("Stone Floor", game_symbol ('.', COLOUR_GREY, EFFECT_BOLD), NULL, FALSE, FALSE);
block_t * stone_wall = game_block ("Stone Wall", game_symbol ('#', COLOUR_GREY, EFFECT_BOLD), NULL, TRUE, FALSE);


world_t * world = game_world (300, 100, full_fill, 0, 0, 0, 0, grass, world_t * world = game_world (300, 100, full_fill, 0, 0, 0, 0, grass,
rectangle_fill, 5, 9, 2, 4, stone_floor, rectangle_fill, 5, 9, 2, 4, stone_floor,
@@ -93,6 +86,15 @@ procedure_t play_game (procedure_t) {
player->attributes [2]->points->current = 5; player->attributes [2]->points->current = 5;
player->attributes [3]->points->current = 3; player->attributes [3]->points->current = 3;


player->skills [0]->points->current = randomize (1, 10);
player->skills [1]->points->current = randomize (1, 10);
player->skills [2]->points->current = randomize (1, 10);
player->skills [3]->points->current = randomize (1, 10);
player->skills [4]->points->current = randomize (1, 10);
player->skills [5]->points->current = randomize (1, 10);
player->skills [6]->points->current = randomize (1, 10);
player->skills [7]->points->current = randomize (1, 10);

curses_configure (); curses_configure ();


terminal_show_cursor (FALSE); terminal_show_cursor (FALSE);
@@ -108,9 +110,13 @@ procedure_t play_game (procedure_t) {


game_render_player (player); game_render_player (player);


game_render_attribute (game_screen_offset, 1, player->attributes [0], player->attributes [1], player->attributes [2], player->attributes [3], NULL);
for (number_t attribute = 0; attribute < player->attribute_count; ++attribute) {
game_render_attribute (player->attributes [attribute], game_screen_offset, 1 + attribute);
}


game_render_skill (game_screen_offset, 6, blades, axes, bows, spears, puppet_magic, nature_magic, rune_magic, charm_magic, NULL);
for (number_t skill = 0; skill < player->skill_count; ++skill) {
game_render_skill (player->skills [skill], game_screen_offset, 6 + skill);
}


switch (curses_character) { switch (curses_character) {
case SIGNAL_ARROW_UP: player->y -= 1; limit (& player->y, 0, world->height - 1); break; case SIGNAL_ARROW_UP: player->y -= 1; limit (& player->y, 0, world->height - 1); break;


Loading…
取消
儲存