home · contact · privacy
Add basic food clock (but no consumables yet to re-set it).
[plomrogue] / src / server / thing_actions.c
index 31b70af8ebc5f327cd4c4820d075f9dbf1bcd995..839a6e36d85eaa84644e723f576c9fa069c2cf6c 100644 (file)
@@ -7,7 +7,7 @@
 
 #include "thing_actions.h"
 #include <stddef.h> /* NULL */
-#include <stdint.h> /* uint8_t */
+#include <stdint.h> /* uint8_t, INT16_MIN */
 #include <stdio.h> /* sprintf() */
 #include <stdlib.h> /* free() */
 #include <string.h> /* strlen() */
@@ -20,6 +20,7 @@
                      * own_thing(), set_thing_position(), get_thing_type(),
                      */
 #include "map.h" /* mv_yx_in_dir_legal() */
+#include "rrand.h" /* rrand() */
 #include "run.h" /* send_to_outfile() */
 #include "world.h" /* global world */
 
 /* 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
@@ -55,6 +57,36 @@ static void update_log(char * text)
 
 
 
+static void decrement_lifepoints(struct Thing * t)
+{
+    struct Thing * player = get_player();
+    t->lifepoints--;
+    if (0 == t->lifepoints)
+    {
+        t->type = get_thing_type(t->type)->corpse_id;
+        if (player == t)
+        {
+            update_log("You die.");
+            memset(t->fov_map, ' ', world.map.length * world.map.length);
+            return;
+        }
+        else
+        {
+            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.");
+    }
+}
+
+
+
 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
 {
     struct ThingType * tt_hitter = get_thing_type(hitter->type);
@@ -78,29 +110,7 @@ static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
     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.");
-            memset(hitted->fov_map, ' ', world.map.length * world.map.length);
-            return;
-        }
-        else
-        {
-            free(hitted->fov_map);
-            hitted->fov_map = NULL;
-            free(hitted->mem_map);
-            hitted->mem_map = NULL;
-            free(hitted->mem_depth_map);
-            hitted->mem_depth_map = NULL;
-            free_things_in_memory(hitted->t_mem);
-            hitted->t_mem = NULL;
-        }
-        update_log("It dies.");
-    }
+    decrement_lifepoints(hitted);
 }
 
 
@@ -315,3 +325,38 @@ extern void actor_use(struct Thing * t)
         playerbonus_use(no_thing, wrong_thing);
     }
 }
+
+
+
+extern void hunger(struct Thing * t)
+{
+    struct ThingType * tt = get_thing_type(t->type);
+    if (!(tt->stomach))
+    {
+        return;
+    }
+    if (t->satiation > INT16_MIN)
+    {
+        t->satiation--;
+    }
+    uint16_t testbase = t->satiation < 0 ? -(t->satiation) : t->satiation;
+    uint16_t endurance = tt->stomach;
+    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);
+    }
+}