home · contact · privacy
Enemies have hitpoints (5 at start), disappear when those reach 0.
[plomrogue] / src / roguelike.c
index f20bfe18e1b123ce7cb5d4ef592b59e30923a96c..7cc50d6443c00b3ab606c599697042acc9ce14ed 100644 (file)
@@ -9,7 +9,7 @@
 #include "draw_wins.h"
 #include "keybindings.h"
 #include "readwrite.h"
-#include "actors.h"
+#include "objects_on_map.h"
 
 uint16_t rrand(char use_seed, uint32_t new_seed) {
 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
@@ -99,12 +99,15 @@ void save_game(struct World * world) {
   struct Monster * monster;
   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); }
+    write_uint16_bigendian(monster->pos.x + 1, 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) {
     write_uint16_bigendian(item->pos.y + 1, file);
-    write_uint16_bigendian(item->pos.x + 1, file); }
+    write_uint16_bigendian(item->pos.x + 1, file);
+    fputc(item->name, file); }
   write_uint16_bigendian(0, file);
   fclose(file); }
 
@@ -232,9 +235,10 @@ int main (int argc, char *argv[]) {
       else {
         monster->next = malloc(sizeof(struct Monster));
         monster = monster->next; }
-      monster->name = 'M';
       monster->pos.y = test - 1;
-      monster->pos.x = read_uint16_bigendian(file) - 1; }
+      monster->pos.x = read_uint16_bigendian(file) - 1;
+      monster->name = fgetc(file);
+      monster->hitpoints = fgetc(file); }
     if (!start)
       monster->next = 0;
     start = 1;
@@ -250,9 +254,9 @@ int main (int argc, char *argv[]) {
       else {
         item->next = malloc(sizeof(struct Item));
         item = item->next; }
-      item->name = '#';
       item->pos.y = test - 1;
-      item->pos.x = read_uint16_bigendian(file) - 1; }
+      item->pos.x = read_uint16_bigendian(file) - 1;
+      item->name = fgetc(file); }
     if (!start)
       item->next = 0;
     fclose(file); }
@@ -276,9 +280,7 @@ int main (int argc, char *argv[]) {
   struct Map map = init_map();
   world.map = ↦
   if (1 == world.turn) {
-    for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos);) {
-      player.pos.y = rrand(0, 0) % map.size.y;
-      player.pos.x = rrand(0, 0) % map.size.x; }
+    player.pos = find_passable_pos(&map);
     unsigned char n_monsters = rrand(0, 0) % 16;
     unsigned char n_items    = rrand(0, 0) % 48;
     unsigned char i;
@@ -292,10 +294,9 @@ int main (int argc, char *argv[]) {
       else {
         monster->next = malloc(sizeof(struct Monster));
         monster = monster->next; }
-      for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos);) {
-        monster->pos.y = rrand(0, 0) % map.size.y;
-        monster->pos.x = rrand(0, 0) % map.size.x; }
-      monster->name = 'M'; }
+      monster->pos = find_passable_pos(&map);
+      monster->name = 'A' + (rrand(0, 0) % 8);
+      monster->hitpoints = 5; }
     if (!start)
       monster->next = 0;
     start = 1;
@@ -308,10 +309,8 @@ int main (int argc, char *argv[]) {
       else {
         item->next = malloc(sizeof(struct Item));
         item = item->next; }
-      for (item->pos.y = item->pos.x = 0; 0 == is_passable(&map, item->pos);) {
-        item->pos.y = rrand(0, 0) % map.size.y;
-        item->pos.x = rrand(0, 0) % map.size.x; }
-      item->name = '#'; }
+      item->pos = find_passable_pos(&map);
+      item->name = '#' + (rrand(0, 0) % 4); }
     if (!start)
       item->next = 0; }