home · contact · privacy
Server/C: In actor_pick(), pick highest ID'd item in stack of many.
[plomrogue] / src / server / thing_actions.c
index bb8f585ef0d1cf60649da7c8476486ab0f276d1b..c9e8ddb30642d421f824487388a1f1fa75cbe5d0 100644 (file)
@@ -1,41 +1,38 @@
-/* src/server/thing_actions.c */
+/* src/server/thing_actions.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
 
 #include "thing_actions.h"
 #include <stddef.h> /* NULL */
-#include <stdint.h> /* uint8_t, uint16_t */
+#include <stdint.h> /* uint8_t, INT16_MIN, INT16_MAX */
 #include <stdio.h> /* sprintf() */
 #include <stdlib.h> /* free() */
-#include <string.h> /* strlen(), strcmp(), memcpy(), strncmp() */
-#include "../common/rexit.h" /* exit_err() */
+#include <string.h> /* strlen() */
+#include "../common/rexit.h" /* exit_err(), exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
-#include "../common/yx_uint8.h" /* struct yx_uint8 */
+#include "../common/yx_uint8.h" /* yx_uint8 */
 #include "field_of_view.h" /* build_fov_map() */
-#include "things.h" /* structs Thing, ThingType, get_player(), own_thing(),
-                     * set_thing_position(), get_thing_type()
+#include "hardcoded_strings.h" /* s */
+#include "things.h" /* Thing, ThingType, get_player(), free_things_in_memory(),
+                     * own_thing(), set_thing_position(), get_thing_type(),
                      */
-#include "map.h" /* is_passable() */
-#include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
+#include "map.h" /* mv_yx_in_dir_legal() */
+#include "rrand.h" /* rrand() */
+#include "run.h" /* send_to_outfile() */
 #include "world.h" /* global world */
 
 
 
-/* How many previous characters of the game log to keep on adding new text */
-#define MAX_BACKLOG_CHARS 3000
-
-
-
-/* If "text" is equal "log"'s last line, return 1, else 0. */
-static uint8_t text_equals_log_end(char * log, char * text);
-
-/* Append "text" to game log shortened to MAX_BACKLOG_CHARS characters, or
- * continuation period if "text" is the same as the (shortened) log's last line
- * minus continuation periods.
- */
+/* Send "text" as log message to server out file. */
 static void update_log(char * text);
 
-/* One actor "wounds" another actor, decrementing his lifepoints and, if they
- * reach zero in the process, killing it. Generates appropriate log message.
- */
+/* Decrement "t"'s lifepoints, and if to zero, kill it with log update. */
+static void decrement_lifepoints(struct Thing * t);
+
+/* One actor "wounds" another actor, decrementing his lifepoints. */
 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
 
 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
@@ -51,69 +48,47 @@ static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
 
 
 
-static uint8_t text_equals_log_end(char * log, char * text)
+static void update_log(char * text)
 {
-    uint16_t len_old = strlen(log);
-    uint16_t last_nl = len_old - 1;
-    while (last_nl != 0)
-    {
-        if ('\n' == log[last_nl])
-        {
-            break;
-        }
-        last_nl--;
-    }
-    uint16_t last_stop = len_old - 1;
-    while (last_stop != 0)
-    {
-        if ('.' == log[last_stop] && '.' != log[last_stop - 1])
-        {
-            break;
-        }
-        last_stop--;
-    }
-    if (   (last_stop + 1) - last_nl == (uint16_t) strlen(text)
-        && 0 == strncmp(log + last_nl, text, strlen(text)))
-    {
-        return 1;
-    }
-    return 0;
+    send_to_outfile("LOG ", 0);
+    send_to_outfile(text, 0);
+    send_to_outfile("\n", 1);
 }
 
 
 
-static void update_log(char * text)
+static void decrement_lifepoints(struct Thing * t)
 {
-    char * f_name = "update_log()";
-    uint16_t len_new = strlen(text);
-    uint16_t len_old = 0;
-    uint16_t offset = 0;
-    if (world.log)
+    struct Thing * player = get_player();
+    t->lifepoints--;
+    if (0 == t->lifepoints)
     {
-        len_old = strlen(world.log);
-        if (len_old > MAX_BACKLOG_CHARS)
+        t->type = get_thing_type(t->type)->corpse_id;
+        if (player == t)
         {
-            offset = len_old - MAX_BACKLOG_CHARS;
-            len_old = MAX_BACKLOG_CHARS;
+            update_log("You die.");
+            memset(t->fov_map, ' ', world.map.length * world.map.length);
+            return;
         }
-        if (text_equals_log_end(world.log + offset, text))
+        else
         {
-            text = ".";
+            free(t->fov_map);
+            t->fov_map = NULL;
+            free(t->mem_map);
+            t->mem_map = NULL;
+            free(t->mem_depth_map);
+            t->mem_depth_map = NULL;
+            free_things_in_memory(t->t_mem);
+            t->t_mem = NULL;
         }
+        update_log("It dies.");
     }
-    uint16_t len_whole = len_old + len_new + 1;
-    char * new_text = try_malloc(len_whole, f_name);
-    memcpy(new_text, world.log + offset, len_old);
-    sprintf(new_text + len_old, "%s", text);
-    free(world.log);
-    world.log = new_text;
 }
 
 
 
 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
 {
-    char * f_name = "actor_hits_actor()";
     struct ThingType * tt_hitter = get_thing_type(hitter->type);
     struct ThingType * tt_hitted = get_thing_type(hitted->type);
     struct Thing * player = get_player();
@@ -129,29 +104,20 @@ static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
     {
         msg3 = tt_hitted->name;
     }
-    uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
-    char * msg = try_malloc(len, f_name);
-    sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
+    uint8_t len = strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
+    char * msg = try_malloc(len, __func__);
+    int test = sprintf(msg, "%s %s %s.", msg1, msg2, msg3);
+    exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
     update_log(msg);
     free(msg);
-    hitted->lifepoints--;
-    if (0 == hitted->lifepoints)
-    {
-        hitted->type = tt_hitted->corpse_id;
-        if (player == hitted)
-        {
-            update_log(" You die.");
-            return;
-        }
-        update_log(" It dies.");
-    }
+    decrement_lifepoints(hitted);
 }
 
 
 
 static void playerbonus_wait()
 {
-        update_log("\nYou wait.");
+        update_log("You wait.");
 }
 
 
@@ -170,7 +136,6 @@ static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
 
 static void playerbonus_move(char d, uint8_t passable)
 {
-    char * f_name = "playerbonus_move()";
     char * dsc_dir = "north-east";
     if (   match_dir(d, &dsc_dir, 'd', "east")
         || match_dir(d, &dsc_dir, 'c', "south-east")
@@ -185,8 +150,9 @@ static void playerbonus_move(char d, uint8_t passable)
     {
         dsc_move = "You fail to move ";
     }
-    char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name);
-    sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
+    char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 2, __func__);
+    int test = sprintf(msg, "%s%s.", dsc_move, dsc_dir);
+    exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
     update_log(msg);
     free(msg);
 }
@@ -197,10 +163,10 @@ static void playerbonus_drop(uint8_t owns_none)
 {
     if (0 != owns_none)
     {
-        update_log("\nYou try to drop an object, but you own none.");
+        update_log("You try to drop an object, but you own none.");
         return;
     }
-    update_log("\nYou drop an object.");
+    update_log("You drop an object.");
 }
 
 
@@ -209,10 +175,10 @@ static void playerbonus_pick(uint8_t picked)
 {
     if (picked)
     {
-        update_log("\nYou pick up an object.");
+        update_log("You pick up an object.");
         return;
     }
-    update_log("\nYou try to pick up an object, but there is none.");
+    update_log("You try to pick up an object, but there is none.");
 }
 
 
@@ -221,45 +187,15 @@ static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
 {
     if      (no_thing)
     {
-        update_log("\nYou try to use an object, but you own none.");
+        update_log("You try to use an object, but you own none.");
         return;
     }
     else if (wrong_thing)
     {
-        update_log("\nYou try to use this object, but fail.");
-        return;
-    }
-    update_log("\nYou consume MAGIC MEAT.");
-}
-
-
-
-extern void free_thing_actions(struct ThingAction * ta)
-{
-    if (NULL == ta)
-    {
+        update_log("You try to use this object, but fail.");
         return;
     }
-    free(ta->name);
-    free_thing_actions(ta->next);
-    free(ta);
-}
-
-
-
-extern uint8_t get_thing_action_id_by_name(char * name)
-{
-    struct ThingAction * ta = world.thing_actions;
-    while (NULL != ta)
-    {
-        if (0 == strcmp(ta->name, name))
-        {
-            break;
-        }
-        ta = ta->next;
-    }
-    exit_err(NULL == ta, "get_thing_action_id_by_name() did not find action.");
-    return ta->id;
+    update_log("You consume this object.");
 }
 
 
@@ -277,26 +213,31 @@ extern void actor_wait(struct Thing * t)
 extern void actor_move(struct Thing * t)
 {
     char d = t->arg;
-    struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
     struct Thing * other_t;
-    for (other_t = world.things; other_t != 0; other_t = other_t->next)
+    struct yx_uint8 target = t->pos;
+    uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
+    mv_yx_in_dir_legal(0, NULL);
+    uint8_t passable = 0;
+    if (legal_move)
     {
-        if (0 == other_t->lifepoints || other_t == t)
-        {
-            continue;
-        }
-        if (yx_uint8_cmp(&target, &other_t->pos))
+        passable = '.' == world.map.cells[target.y*world.map.length + target.x];
+        for (other_t = world.things; other_t != 0; other_t = other_t->next)
         {
-            actor_hits_actor(t, other_t);
-            return;
+            if (0 == other_t->lifepoints || other_t == t)
+            {
+                continue;
+            }
+            if (target.y == other_t->pos.y && target.x == other_t->pos.x)
+            {
+               actor_hits_actor(t, other_t);
+               return;
+            }
         }
     }
-    uint8_t passable = is_passable(target);
     if (passable)
     {
         set_thing_position(t, target);
-        free(t->fov_map);
-        t->fov_map = build_fov_map(t);
+        build_fov_map(t);
     }
     if (t == get_player())
     {
@@ -308,7 +249,7 @@ extern void actor_move(struct Thing * t)
 
 extern void actor_drop(struct Thing * t)
 {
-    uint8_t owns_none = (NULL == t->owns);
+    uint8_t owns_none = (!t->owns);
     if (!owns_none)
     {
         uint8_t select = t->arg;
@@ -329,21 +270,26 @@ extern void actor_pick(struct Thing * t)
 {
     struct Thing * picked = NULL;
     struct Thing * t_i;
-    for (t_i = world.things; NULL != t_i; t_i = t_i->next)
+    uint8_t highest_id = 0;
+    for (t_i = world.things; t_i; t_i = t_i->next)
     {
-        if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
+        if (t_i != t && t_i->pos.y == t->pos.y && t_i->pos.x == t->pos.x)
         {
-            picked = t_i;
+            if (t_i->id >= highest_id)   /* With several Things to pick,      */
+            {                            /* pick the one with the highest ID. */
+                highest_id = t_i->id;
+                picked = t_i;
+            }
         }
     }
-    if (NULL != picked)
+    if (picked)
     {
         own_thing(&t->owns, &world.things, picked->id);
         set_thing_position(picked, t->pos);
     }
     if (t == get_player())
     {
-        playerbonus_pick(NULL != picked);
+        playerbonus_pick(!(!picked));
     }
 }
 
@@ -352,7 +298,7 @@ extern void actor_pick(struct Thing * t)
 extern void actor_use(struct Thing * t)
 {
     uint8_t wrong_thing = 1;
-    uint8_t no_thing = (NULL == t->owns);
+    uint8_t no_thing = (!t->owns);
     if (!no_thing)
     {
         uint8_t select = t->arg;
@@ -376,6 +322,8 @@ extern void actor_use(struct Thing * t)
             {
                 t->owns = next;
             }
+            t->satiation = t->satiation + tt->consumable > INT16_MAX ?
+                           INT16_MAX : t->satiation + tt->consumable;
             t->lifepoints = t->lifepoints + tt->consumable;
         }
     }
@@ -384,3 +332,63 @@ extern void actor_use(struct Thing * t)
         playerbonus_use(no_thing, wrong_thing);
     }
 }
+
+
+
+extern void try_healing(struct Thing * t)
+{
+    struct ThingType * tt = get_thing_type(t->type);
+    if (   t->satiation > 0 && t->lifepoints < tt->lifepoints
+        && 0 == (rrand() % 31)
+        && get_thing_action_id_by_name(s[S_CMD_WAIT]) == t->command)
+    {
+        t->lifepoints++;
+        t->satiation = t->satiation - 32;
+        if (get_player() == t)
+        {
+            update_log("You heal.");
+        }
+        else
+        {
+            char * msg_part = " heals.";
+            uint8_t len = strlen(tt->name) + strlen(msg_part) + 1;
+            char * msg = try_malloc(len, __func__);
+            int test = sprintf(msg, "%s%s", tt->name, msg_part);
+            exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
+            update_log(msg);
+            free(msg);
+        }
+    }
+}
+
+
+
+extern void hunger(struct Thing * t)
+{
+    if (t->satiation > INT16_MIN)
+    {
+        t->satiation--;
+    }
+    struct ThingType * tt = get_thing_type(t->type);
+    uint16_t testbase = t->satiation < 0 ? -(t->satiation) : t->satiation;
+    exit_err(!(tt->lifepoints), "A thing that should not hunger is hungering.");
+    uint16_t endurance = INT16_MAX / tt->lifepoints;
+    if ((testbase / endurance) / ((rrand() % endurance) + 1))
+    {
+        if (get_player() == t)
+        {
+            update_log("You suffer from hunger.");
+        }
+        else
+        {
+            char * msg_part = " suffers from hunger.";
+            uint8_t len = strlen(tt->name) + strlen(msg_part) + 1;
+            char * msg = try_malloc(len, __func__);
+            int test = sprintf(msg, "%s%s", tt->name, msg_part);
+            exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
+            update_log(msg);
+            free(msg);
+        }
+        decrement_lifepoints(t);
+    }
+}