home · contact · privacy
Send log messages through server out file. Includes major refactoring.
[plomrogue] / src / server / run.c
index 334fe410fcf9200713725ee2f8adcc81b22f2c53..ba48d8ce3690392dacd39bcb4de5ee9bbd1888a7 100644 (file)
@@ -5,7 +5,7 @@
  * see the file NOTICE in the root directory of the PlomRogue source package.
  */
 
-#define _POSIX_C_SOURCE 200809L
+#define _POSIX_C_SOURCE 200809L /* strdup() */
 #include "run.h"
 #include <stddef.h> /* NULL */
 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t */
@@ -28,9 +28,9 @@
 #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */
 #include "hardcoded_strings.h" /* s */
 #include "io.h" /* io_round(), save_world() */
-#include "things.h" /* Thing, get_thing_action_id_by_name(), get_player(),
-                      * try_thing_proliferation()
-                      */
+#include "things.h" /* Thing, ThingType, get_thing_action_id_by_name(),
+                     * get_player(), try_thing_proliferation()
+                     */
 #include "world.h" /* world */
 
 
@@ -71,6 +71,9 @@ static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist);
  */
 static void turn_over();
 
+/* Try to read "msg" as meta command, act accordingly; on success, free it. */
+static uint8_t meta_commands(char * msg);
+
 
 
 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
@@ -221,7 +224,7 @@ static int16_t * build_whitelist()
     for (; t; t = t->next, i_things++);
     int16_t * whitelist = try_malloc(i_things * sizeof(int16_t), __func__);
     for (i_things = 0, t = world.things; t;
-         whitelist[i_things] = t->id, t = t->next, i_things++)
+         whitelist[i_things] = t->id, t = t->next, i_things++);
     whitelist[i_things] = -1;
     return whitelist;
 }
@@ -230,7 +233,7 @@ static int16_t * build_whitelist()
 
 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist)
 {
-    int16_t i;
+    uint16_t i;
     for (i = 0; -1 < whitelist[i]; i++)
     {
         if ((int16_t) id == whitelist[i])
@@ -247,18 +250,16 @@ static void turn_over()
 {
     struct Thing * player = get_player();
     struct Thing * thing = player;
-    uint16_t start_turn = world.turn;
     int16_t * whitelist = build_whitelist();
-    while (    0 < player->lifepoints
-           || (0 == player->lifepoints && start_turn == world.turn))
-    {             /* TODO: check meaning and refactorability of 2nd condition */
+    while (0 < player->lifepoints)
+    {
         if (!thing)
         {
             world.turn++;
             thing = world.things;
             free(whitelist);
-            whitelist = build_whitelist();
-        }
+            whitelist = build_whitelist();/* The whitelist excludes things    */
+        }                                 /* that appear only during the turn.*/
         if (thing_in_whitelist(thing->id, whitelist))
         {
             if (0 < thing->lifepoints)
@@ -289,6 +290,51 @@ static void turn_over()
 
 
 
+static uint8_t meta_commands(char * msg)
+{
+    if (!strcmp("QUIT", msg))
+    {
+        free(msg);
+        return 2;
+    }
+    if (!strcmp("PING", msg))
+    {
+        free(msg);
+        send_to_outfile("PONG\n");
+        return 1;
+    }
+    if (!strcmp("STACK", msg))
+    {
+        free(msg);
+        send_to_outfile("THINGS_BELOW_PLAYER START\n");
+        struct Thing * player = get_player();
+        struct Thing * t;
+        for (t = world.things; t; t = t->next)
+        {
+            if (   t->pos.y == player->pos.y && t->pos.x == player->pos.x
+                && t != player)
+            {
+                struct ThingType * tt = get_thing_type(t->type);
+                send_to_outfile(tt->name);
+                send_to_outfile("\n");
+            }
+        }
+        send_to_outfile("THINGS_BELOW_PLAYER END\n");
+        return 1;
+    }
+    return 0;
+}
+
+
+
+extern void send_to_outfile(char * answer)
+{
+    try_fwrite(answer, strlen(answer), 1, world.file_out, __func__);
+    fflush(world.file_out);
+}
+
+
+
 extern void record(char * msg, uint8_t force)
 {
     static FILE * file_tmp = NULL;
@@ -375,17 +421,13 @@ extern uint8_t io_loop()
         {
             exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
         }
-        if (!strcmp("QUIT", msg))
-        {
-            free(msg);
-            return 1;
-        }
-        if (!strcmp("PING", msg))
+        uint8_t test = meta_commands(msg);
+        if (test)
         {
-            free(msg);
-            char * pong = "PONG\n";
-            try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
-            fflush(world.file_out);
+            if (2 == test)
+            {
+                return 1;
+            }
             continue;
         }
         if (world.replay)