home · contact · privacy
Added -Wextra to compiler flags, found out about redundant test.
[plomrogue] / src / server / run.c
index 3be073a70170654e89f4a5c6a3b0e6639eacd544..82e0aaefb866ce80fd3f5d4f63a7f281c598ac19 100644 (file)
@@ -1,6 +1,7 @@
 /* src/server/run.c */
 
 #include "run.h"
+#include <stddef.h> /* NULL */
 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
 #include <stdio.h> /* FILE, sprintf() */
 #include <stdlib.h> /* free() */
@@ -8,10 +9,10 @@
 #include <unistd.h> /* access() */
 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
                                   * try_fgets(), try_fclose_unlink_rename(),
-                                  * textfile_sizes(), try_fputc()
+                                  * textfile_width(), try_fputc()
                                   */
 #include "../common/rexit.h" /* exit_trouble() */
-#include "ai.h" /* pretty_dumb_ai() */
+#include "ai.h" /* ai() */
 #include "init.h" /* remake_world() */
 #include "io.h" /* io_round() */
 #include "map_object_actions.h" /* get_moa_id_by_name() */
  */
 static void turn_over();
 
+/* Helper to turn_over() to determine whether a map object's action effort has
+ * reached its end. The simplicity of just comparing map_object->progress to
+ * moa->effort is suspended for actor movement, for in this case the effort
+ * depends on the diagonal movement penalty expressed in the ratio of
+ * world.map.dist_diagonal / world.map.dist_orthogonal. (Movement being diagonal
+ * or orthogonal is determined by the ->arg char encoding an even or un-even
+ * number digit).
+ */
+static uint8_t is_effort_finished(struct MapObjAct * moa,
+                                  struct MapObj * map_object);
+
 /* If "msg"'s first part matches "command_name", set player's MapObj's .command
  * to the command's id and its .arg to a numerical value following in the latter
  * part of "msg" (if no digits are found, use 0); then finish player's turn and
@@ -39,7 +51,6 @@ static void turn_over()
     struct MapObj * player = get_player();
     struct MapObj * map_object = player;
     uint16_t start_turn = world.turn;
-    uint8_t first_round = 1;
     while (    0 < player->lifepoints
            || (0 == player->lifepoints && start_turn == world.turn))
     {
@@ -50,24 +61,24 @@ static void turn_over()
         }
         if (0 < map_object->lifepoints)
         {
-            if (0 == first_round && 0 == map_object->progress)
+            if (0 == map_object->command)
             {
                 if (map_object == player)
                 {
                     break;
                 }
-                pretty_dumb_ai(map_object);
+                ai(map_object);
             }
-            first_round = 0;
             map_object->progress++;
             struct MapObjAct * moa = world.map_obj_acts;
             while (moa->id != map_object->command)
             {
                 moa = moa->next;
             }
-            if (map_object->progress == moa->effort)
+            if (is_effort_finished(moa, map_object))
             {
                 moa->func(map_object);
+                map_object->command = 0;
                 map_object->progress = 0;
             }
         }
@@ -77,6 +88,37 @@ static void turn_over()
 
 
 
+static uint8_t is_effort_finished(struct MapObjAct * moa,
+                                  struct MapObj * map_object)
+{
+    if (moa->func != actor_move)
+    {
+        if (map_object->progress == moa->effort)
+        {
+            return 1;
+        }
+    }
+    else if (strchr("8624", map_object->arg))
+    {
+        if (map_object->progress == moa->effort)
+        {
+            return 1;
+        }
+    }
+    else if (strchr("1379", map_object->arg))
+    {
+        uint16_t diagonal_effort =   (moa->effort * world.map.dist_diagonal)
+                                   / world.map.dist_orthogonal;
+        if (map_object->progress == diagonal_effort)
+        {
+            return 1;
+        }
+    }
+    return 0;
+}
+
+
+
 static uint8_t apply_player_command(char * msg, char * command_name)
 {
     if (!strncmp(msg, command_name, strlen(command_name)))
@@ -116,7 +158,7 @@ extern void obey_msg(char * msg, uint8_t do_record)
         if (!access(world.path_record, F_OK))
         {
             FILE * file_read = try_fopen(world.path_record, "r", f_name);
-            uint32_t linemax = textfile_sizes(file_read, NULL);
+            uint32_t linemax = textfile_width(file_read);
             char line[linemax + 1];
             while (try_fgets(line, linemax + 1, file_read, f_name))
             {