home · contact · privacy
Replace uses of variable-length arrays with try_malloc()/free().
[plomrogue] / src / server / io.c
index 817d54253c40f69edc2225ef7f53e4769d15e78b..bc9d8773feab3b68cb9c4705aa20a80afb9bbac9 100644 (file)
@@ -1,26 +1,36 @@
 /* src/server/io.c */
 
+#define _POSIX_C_SOURCE 199309L
 #include "io.h"
 #include <errno.h> /* global errno */
-#include <fcntl.h> /* open(), O_RDONLY, O_NONBLOCK */
 #include <limits.h> /* PIPE_BUF */
 #include <stddef.h> /* size_t, NULL */
 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
-#include <stdio.h> /* define FILE, sprintf() */
+#include <stdio.h> /* defines EOF, FILE, sprintf() */
 #include <stdlib.h> /* free() */
-#include <string.h> /* strlen(), memset(), memcpy() */
-#include <unistd.h> /* read(), close() */
+#include <string.h> /* strlen(), memcpy(), memset() */
+#include <sys/types.h> /* time_t */
+#include <time.h> /* time(), nanosleep() */
 #include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
-                                  * try_fwrite(), try_fputc()
+                                  * try_fwrite(), try_fputc(), try_fgetc()
                                   */
 #include "../common/rexit.h" /* exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "cleanup.h" /* set_cleanup_flag() */
-#include "map_objects.h" /* structs MapObj, MapObjDef, get_map_obj_def() */
+#include "field_of_view.h" /* VISIBLE */
+#include "hardcoded_strings.h" /* s */
+#include "map.h" /* yx_to_map_pos() */
+#include "things.h" /* Thing, ThingType, get_thing_type(), get_player() */
 #include "world.h" /* global world  */
 
 
 
+/* Write to "file" god commands (one per line) to recreate thing "t". */
+static void write_key_value(FILE * file, char * key, uint32_t value);
+
+/* Write to "file" \n-delimited line of "key" + space + "value" as string. */
+static void write_thing(FILE * file, struct Thing * t);
+
 /* Cut out and return first \0-terminated string from world.queue and
  * appropriately reduce world.queue_size. Return NULL if queue is empty.
  * Superfluous \0 bytes after the string are also cut out. Should the queue
 */
 static char * get_message_from_queue();
 
-/* Read fifo input to put into queue. Only stop reading when bytes were received
- * and the receiving has stopped. Read max. PIPE_BUF-sized chunks for atomicity.
+/* Poll input file for world.queue input. Wait a few seconds until giving up;
+ * poll only every 0.03 seconds.. Translate '\n' chars in input file into '\0'.
  */
-static void read_fifo_into_queue();
+static void read_file_into_queue();
 
-/* Write to output file the world state as it is to be visible to clients. */
-static void update_out_file();
+/* Write world state as visible to clients to its file. Write single dot line to
+ * server output file to satisfy client ping mechanisms.
+ */
+static void update_worldstate_file();
 
 /* Write "value" to new \n-delimited line of "file". */
 static void write_value_as_line(uint32_t value, FILE * file);
 
 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
-static void write_inventory(struct MapObj * player, FILE * file);
+static void write_inventory(struct Thing * player, FILE * file);
 
-/* Write to "file" game map, with map objects super-imposed. Write one row per
- * \n-delimited line. Super-impose animated objects over inanimate objects.
+/* Return map cells sequence as visible to the "player", with invisible cells as
+ * whitespace. Super-impose over visible map cells things positioned there.
  */
-static void write_map(FILE * file);
+static char * build_visible_map(struct Thing * player);
 
+/* Write to "file" game map as visible to "player", build_visible_map()-drawn.
+ * Write one row per \n-delimited line.
+ */
+static void write_map(struct Thing * player, FILE * file);
 
 
-static char * get_message_from_queue()
+
+static void write_key_value(FILE * file, char * key, uint32_t value)
 {
-    char * f_name = "get_message_from_queue()";
-    char * message = NULL;
-    size_t cutout_len = strlen(world.queue);
-    if (0 < cutout_len)
+    char * f_name = "write_key_value()";
+    try_fwrite(key, strlen(key), 1, file, f_name);
+    try_fputc(' ', file, f_name);
+    char * line = try_malloc(11, f_name);
+    exit_trouble(-1 == sprintf(line, "%u", value), f_name, "sprintf()");
+    try_fwrite(line, strlen(line), 1, file, f_name);
+    free(line);
+    try_fputc('\n', file, f_name);
+}
+
+
+
+static void write_thing(FILE * file, struct Thing * t)
+{
+    char * f_name = "write_thing()";
+    struct Thing * o;
+    for (o = t->owns; o; o = o->next)
     {
-        cutout_len++;
-        message = try_malloc(cutout_len, f_name);
-        memcpy(message, world.queue, cutout_len);
+        write_thing(file, o);
     }
-    for (;
-         cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
-         cutout_len++);
-    world.queue_size = world.queue_size - cutout_len;
-    if (0 == world.queue_size)
+    write_key_value(file, s[CMD_THING], t->id);
+    write_key_value(file, s[CMD_TYPE], t->type);
+    write_key_value(file, s[CMD_POS_Y], t->pos.y);
+    write_key_value(file, s[CMD_POS_X], t->pos.x);
+    write_key_value(file, s[CMD_COMMAND], t->command);
+    write_key_value(file, s[CMD_ARGUMENT], t->arg);
+    write_key_value(file, s[CMD_PROGRESS], t->progress);
+    write_key_value(file, s[CMD_LIFEPOINTS], t->lifepoints);
+    for (o = t->owns; o; o = o->next)
     {
-        free(world.queue);  /* NULL so read_fifo_into_queue() may free() this */
-        world.queue = NULL; /* every time, even when it's un-allocated first. */
+        write_key_value(file, s[CMD_CARRIES], o->id);
     }
-    else
-    {
-        char * new_queue = try_malloc(world.queue_size, f_name);
-        memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
-        free(world.queue);
-        world.queue = new_queue;
-    }
-    return message;
+    try_fputc('\n', file, f_name);
 }
 
 
 
-static void read_fifo_into_queue()
+static char * get_message_from_queue()
 {
-    char * f_name = "read_fifo_into_queue()";
-    uint32_t buf_size = PIPE_BUF;
-    int fdesc_infile = open(world.path_in, O_RDONLY | O_NONBLOCK);
-    exit_trouble(-1 == fdesc_infile, "open()", f_name);
-    char buf[buf_size];
-    memset(buf, 0, buf_size);
-    int bytes_read;
-    uint8_t read_state = 0; /* 0: waiting for input. 1: started receiving it. */
-    while (1)
+    char * f_name = "get_message_from_queue()";
+    char * message = NULL;
+    if (world.queue_size)
     {
-        bytes_read = read(fdesc_infile, buf, buf_size);
-        if (bytes_read > 0)
+        size_t cutout_len = strlen(world.queue);
+        if (0 < cutout_len)
+        {
+            cutout_len++;
+            message = try_malloc(cutout_len, f_name);
+            memcpy(message, world.queue, cutout_len);
+        }
+        for (;
+             cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
+             cutout_len++);
+        world.queue_size = world.queue_size - cutout_len;
+        if (0 == world.queue_size)
+        {
+            free(world.queue);   /* NULL so read_file_into_queue() may free() */
+            world.queue = NULL;  /* this every time, even when it's           */
+        }                        /* un-allocated first. */
+        else
         {
-            read_state = 1;
-            uint32_t old_queue_size = world.queue_size;
-            world.queue_size = world.queue_size + bytes_read;
             char * new_queue = try_malloc(world.queue_size, f_name);
-            memcpy(new_queue, world.queue, old_queue_size);
-            memcpy(&(new_queue[old_queue_size]), buf, bytes_read);
+            memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
             free(world.queue);
             world.queue = new_queue;
-            memset(buf, 0, buf_size);
-            continue;
         }
-        exit_trouble(-1 == bytes_read && errno != EAGAIN, "read()", f_name);
-        if (1 == read_state)
+    }
+    return message;
+}
+
+
+
+static void read_file_into_queue()
+{
+    char * f_name = "read_file_into_queue()";
+    uint8_t wait_seconds = 5;
+    time_t now = time(0);
+    struct timespec dur;
+    dur.tv_sec = 0;
+    dur.tv_nsec = 33333333;
+    int test;
+    while (EOF == (test = try_fgetc(world.file_in, f_name)))
+    {
+        nanosleep(&dur, NULL);
+        if (time(0) > now + wait_seconds)
+        {
+            return;
+        }
+    }
+    do
+    {
+        char c = (char) test;
+        if ('\n' == c)
         {
-            break;
+            c = '\0';
         }
+        char * new_queue = try_malloc(world.queue_size + 1, f_name);
+        memcpy(new_queue, world.queue, world.queue_size);
+        char * new_pos = new_queue + world.queue_size;
+        * new_pos = c;
+        world.queue_size++;
+        free(world.queue);
+        world.queue = new_queue;
     }
-    exit_trouble(close(fdesc_infile), f_name, "close()");
+    while (EOF != (test = try_fgetc(world.file_in, f_name)));
 }
 
 
 
-static void update_out_file()
+static void update_worldstate_file()
 {
-    char * f_name = "update_out_file()";
-    char path_tmp[strlen(world.path_out) + strlen(world.tmp_suffix) + 1];
-    sprintf(path_tmp, "%s%s", world.path_out, world.tmp_suffix);
+    char * f_name = "update_worldstate_file()";
+    uint16_t size = strlen(s[PATH_WORLDSTATE]) + strlen(s[PATH_SUFFIX_TMP]) + 1;
+    char * path_tmp = try_malloc(size, f_name);
+    sprintf(path_tmp, "%s%s", s[PATH_WORLDSTATE], s[PATH_SUFFIX_TMP]);
     FILE * file = try_fopen(path_tmp, "w", f_name);
-    struct MapObj * player = get_player();
+    struct Thing * player = get_player();
     write_value_as_line(world.turn, file);
-    write_value_as_line(world.score, file);
     write_value_as_line(player->lifepoints, file);
     write_inventory(player, file);
     write_value_as_line(player->pos.y, file);
     write_value_as_line(player->pos.x, file);
-    write_value_as_line(world.map.size.y, file);
-    write_value_as_line(world.map.size.x, file);
-    write_map(file);
+    write_value_as_line(world.map.length, file);
+    write_map(player, file);
     if (world.log)
     {
         try_fwrite(world.log, strlen(world.log), 1, file, f_name);
     }
-    try_fclose_unlink_rename(file, path_tmp, world.path_out, f_name);
-    set_cleanup_flag(CLEANUP_OUTFILE);
+    try_fclose_unlink_rename(file, path_tmp, s[PATH_WORLDSTATE], f_name);
+    free(path_tmp);
+    set_cleanup_flag(CLEANUP_WORLDSTATE);
+    char * dot = ".\n";;
+    try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
+    fflush(world.file_out);
 }
 
 
@@ -154,10 +216,10 @@ static void write_value_as_line(uint32_t value, FILE * file)
 
 
 
-static void write_inventory(struct MapObj * player, FILE * file)
+static void write_inventory(struct Thing * player, FILE * file)
 {
     char * f_name = "write_inventory()";
-    struct MapObj * owned = player->owns;
+    struct Thing * owned = player->owns;
     if (NULL == owned)
     {
         char * empty = "(none)\n";
@@ -168,8 +230,8 @@ static void write_inventory(struct MapObj * player, FILE * file)
         uint8_t q;
         for (q = 0; NULL != owned; q++)
         {
-            struct MapObjDef * mod = get_map_object_def(owned->type);
-            try_fwrite(mod->name, strlen(mod->name), 1, file, f_name);
+            struct ThingType * tt = get_thing_type(owned->type);
+            try_fwrite(tt->name, strlen(tt->name), 1, file, f_name);
             try_fputc('\n', file, f_name);
             owned = owned->next;
         }
@@ -180,38 +242,60 @@ static void write_inventory(struct MapObj * player, FILE * file)
 
 
 
-static void write_map(FILE * file)
+static char * build_visible_map(struct Thing * player)
 {
-    char * f_name = "write_map()";
-    uint32_t map_size = world.map.size.y * world.map.size.x;
-    char visible_map[map_size];
-    memcpy(visible_map, world.map.cells, map_size);
-    struct MapObj * o;
-    struct MapObjDef * d;
-    char c;
-    uint8_t i;
-    for (i = 0; i < 2; i++)
-    {
-        for (o = world.map_objs; o != 0; o = o->next)
+    char * f_name = "build_visible_map()";
+    uint32_t map_size = world.map.length * world.map.length;
+    char * visible_map = try_malloc(map_size, f_name);
+    memset(visible_map, ' ', map_size);
+    if (player->fov_map) /* May fail if player thing was created / positioned */
+    {                    /* by god command after turning off FOV building.    */
+        uint32_t pos_i;
+        for (pos_i = 0; pos_i < map_size; pos_i++)
+        {
+            if (player->fov_map[pos_i] & VISIBLE)
+            {
+                visible_map[pos_i] = world.map.cells[pos_i];
+            }
+        }
+        struct Thing * t;
+        struct ThingType * tt;
+        char c;
+        uint8_t i;
+        for (i = 0; i < 2; i++)
         {
-            if ((   (0 == i && 0 == o->lifepoints)
-                 || (1 == i && 0 < o->lifepoints)))
+            for (t = world.things; t != 0; t = t->next)
             {
-                d = get_map_object_def(o->type);
-                c = d->char_on_map;
-                visible_map[(o->pos.y * world.map.size.x) + o->pos.x] = c;
+                if (   player->fov_map[yx_to_map_pos(&t->pos)] & VISIBLE
+                    && (   (0 == i && 0 == t->lifepoints)
+                        || (1 == i && 0 < t->lifepoints)))
+                {
+                    tt = get_thing_type(t->type);
+                    c = tt->char_on_map;
+                    visible_map[yx_to_map_pos(&t->pos)] = c;
+                }
             }
         }
     }
+    return visible_map;
+}
+
+
+
+static void write_map(struct Thing * player, FILE * file)
+{
+    char * f_name = "write_map()";
+    char * visible_map = build_visible_map(player);
     uint16_t x, y;
-    for (y = 0; y < world.map.size.y; y++)
+    for (y = 0; y < world.map.length; y++)
     {
-        for (x = 0; x < world.map.size.x; x++)
+        for (x = 0; x < world.map.length; x++)
         {
-            try_fputc(visible_map[(y * world.map.size.x) + x], file, f_name);
+            try_fputc(visible_map[(y * world.map.length) + x], file, f_name);
         }
         try_fputc('\n', file, f_name);
     }
+    free(visible_map);
 }
 
 
@@ -225,11 +309,11 @@ extern char * io_round()
     }
     if (world.turn != world.last_update_turn)
     {
-        update_out_file();
+        update_worldstate_file();
         world.last_update_turn = world.turn;
     }
-    read_fifo_into_queue();
-    if ('\0' != world.queue[world.queue_size - 1])
+    read_file_into_queue();
+    if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
     {
         char * new_queue = try_malloc(world.queue_size + 1, f_name);
         memcpy(new_queue, world.queue, world.queue_size);
@@ -240,3 +324,25 @@ extern char * io_round()
     }
     return get_message_from_queue();
 }
+
+
+
+extern void save_world()
+{
+    char * f_name = "save_world()";
+    char * path = s[PATH_SAVE];
+    FILE * file = try_fopen(path, "w", f_name);
+    write_key_value(file, s[CMD_DO_FOV], 0);
+    try_fputc('\n', file, f_name);
+    write_key_value(file, s[CMD_SEED_MAP], world.seed_map);
+    write_key_value(file, s[CMD_SEED_RAND], world.seed);
+    write_key_value(file, s[CMD_TURN], world.turn);
+    try_fputc('\n', file, f_name);
+    struct Thing * t;
+    for (t = world.things; t; t = t->next)
+    {
+        write_thing(file, t);
+    }
+    write_key_value(file, s[CMD_DO_FOV], 1);
+    try_fclose(file, f_name);
+}