From: Christian Heller Date: Wed, 3 Jul 2013 03:10:02 +0000 (+0200) Subject: Enemies have hitpoints (5 at start), disappear when those reach 0. X-Git-Tag: tce~1170 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/condition?a=commitdiff_plain;h=b4bb10b6e9089bd267a513dbf37543c318b5eb1e;p=plomrogue Enemies have hitpoints (5 at start), disappear when those reach 0. --- diff --git a/README b/README index 39fb922..9f80b04 100644 --- a/README +++ b/README @@ -4,8 +4,9 @@ plomrogue plomlompom tries to build his own roguelike. Currently, it doesn't do much interesting, apart from managing some ncurses windows in a bizarre fashion. You can move around a player and meet a random number enemies; -they will occasionally hit you (and each other) on contact, but without -consequences (no hitpoints implemented yet). The map gets generated +they will occasionally hit you on contact, but without consequence to +you: The player has no hitpoints so far. Enemies have 5 hitpoints at +start and disappear if their hitpoints reach 0. The map gets generated randomly, too. There is only one save file (named "savefile"), and it gets overwritten each new turn. To start over with a new world, delete it. diff --git a/src/objects_on_map.c b/src/objects_on_map.c index eedf800..61efcde 100644 --- a/src/objects_on_map.c +++ b/src/objects_on_map.c @@ -32,7 +32,7 @@ extern void move_monster (struct World * world, struct Monster * monster) { if (other_monster == monster) continue; if (yx_uint16_cmp (t, other_monster->pos)) { - update_log (world, "\nMonster hits monster."); + update_log (world, "\nMonster bumps into monster."); return; } } if (is_passable(world->map, t)) monster->pos = t; } @@ -44,6 +44,16 @@ extern void move_player (struct World * world, char d) { for (monster = world->monster; monster != 0; monster = monster->next) if (yx_uint16_cmp (t, monster->pos)) { update_log (world, "\nYou hit the monster."); + monster->hitpoints--; + if (0 == monster->hitpoints) { + update_log (world, "\nYou kill the monster."); + if (world->monster == monster) + world->monster = world->monster->next; + else { + struct Monster * m_prev; + for (m_prev = world->monster; m_prev->next != monster; m_prev = m_prev->next); + m_prev->next = monster->next; } + free(monster); } turn_over (world, d); return; } char * msg = calloc(25, sizeof(char)); diff --git a/src/objects_on_map.h b/src/objects_on_map.h index 4966161..ccb9bcd 100644 --- a/src/objects_on_map.h +++ b/src/objects_on_map.h @@ -17,7 +17,8 @@ struct Item { struct Monster { struct Monster * next; unsigned char name; - struct yx_uint16 pos; }; + struct yx_uint16 pos; + unsigned char hitpoints; }; extern char is_passable (struct Map *, struct yx_uint16); extern struct yx_uint16 find_passable_pos (struct Map *); diff --git a/src/roguelike.c b/src/roguelike.c index 3604fbd..7cc50d6 100644 --- a/src/roguelike.c +++ b/src/roguelike.c @@ -100,7 +100,8 @@ void save_game(struct World * world) { for (monster = world->monster; monster != 0; monster = monster->next) { write_uint16_bigendian(monster->pos.y + 1, file); write_uint16_bigendian(monster->pos.x + 1, file); - fputc(monster->name, file); } + fputc(monster->name, file); + fputc(monster->hitpoints, file); } write_uint16_bigendian(0, file); struct Item * item; for (item = world->item; item != 0; item = item->next) { @@ -236,7 +237,8 @@ int main (int argc, char *argv[]) { monster = monster->next; } monster->pos.y = test - 1; monster->pos.x = read_uint16_bigendian(file) - 1; - monster->name = fgetc(file); } + monster->name = fgetc(file); + monster->hitpoints = fgetc(file); } if (!start) monster->next = 0; start = 1; @@ -293,7 +295,8 @@ int main (int argc, char *argv[]) { monster->next = malloc(sizeof(struct Monster)); monster = monster->next; } monster->pos = find_passable_pos(&map); - monster->name = 'A' + (rrand(0, 0) % 8); } + monster->name = 'A' + (rrand(0, 0) % 8); + monster->hitpoints = 5; } if (!start) monster->next = 0; start = 1;