home · contact · privacy
Server/AI: Explore map for (long-time) unexplored cells.
[plomrogue] / src / server / things.c
index 81d59318cf8e36b19a7cebe0d8ebc68571cc5493..85503e9a8a874d556ae38a669945e5e819b36870 100644 (file)
-/* src/server/things.c */
-
+/* src/server/things.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.
+ */
+
+#define _POSIX_C_SOURCE 200809L /* strdup() */
 #include "things.h"
-#include <stddef.h> /* NULL */
-#include <stdint.h> /* uint8_t, uint16_t, UINT8_MAX, UINT16_MAX */
+#include <stddef.h> /* NULL, size_t */
+#include <stdint.h> /* uint8_t, uint16_t, int16_t, UINT8_MAX, UINT16_MAX */
 #include <stdlib.h> /* free() */
-#include <string.h> /* memset(), strlen() */
+#include <string.h> /* memset(), strcmp(), strdup(), strlen() */
 #include "../common/rexit.h" /* exit_err() */
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "../common/yx_uint8.h" /* yx_uint8 */
-#include "map.h" /* is_passable() */
+#include "cleanup.h" /* set_cleanup_flag() */
+#include "hardcoded_strings.h" /* s */
+#include "field_of_view.h" /* build_fov_map() */
+#include "map.h" /* mv_yx_in_dir_legal() */
 #include "rrand.h" /* rrand() */
-#include "world.h" /* global world */
-#include "yx_uint8.h" /* yx_uint8_cmp() */
+#include "thing_actions.h" /* actor_wait */
+#include "world.h" /* world */
+
+
 
+/* Used to treat structs Thing, ThingType and ThingAction the same. */
+struct NextAndId
+{
+    struct NextAndId * next;
+    uint8_t id;
+};
 
 
 
-/* Return lowest unused id for new thing. */
-static uint8_t get_lowest_unused_id();
+/* To linked list of NextAndId structs (or rather structs whose start region is
+ * compatible to it) starting at "start", add newly allocated element of
+ * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
+ * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
+ * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
+ */
+static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
+                                             int16_t id, uint8_t struct_id,
+                                             struct NextAndId ** start);
 
+/* Return 1 if cell at "test_pos" is proliferable by "t", i.e. it is passable,
+ * it is not inhabited by another thing of "t"'s type, and, if "t" is animate,
+ * neither by any other animate thing; else return 0.
+ */
+static uint8_t cell_is_proliferable(struct yx_uint8 test_pos, struct Thing * t);
 
 
-static uint8_t get_lowest_unused_id()
+static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
+                                             int16_t id, uint8_t struct_id,
+                                             struct NextAndId ** start)
 {
-    uint8_t i = 0;
-    while (1)
+    struct NextAndId * nai  = try_malloc(n_size, __func__);
+    memset(nai, 0, n_size);
+    if (start_id <= id && id <= UINT8_MAX)
     {
-        if (!get_thing(world.things, i, 1))
+        nai->id = id;
+    }
+    else
+    {
+        while (1)
         {
-            return i;
+            if (   (0 == struct_id && !get_thing(world.things, start_id, 1))
+                || (1 == struct_id && !get_thing_type(start_id))
+                || (2 == struct_id && !get_thing_action(start_id)))
+            {
+                nai->id = start_id;
+                break;
+            }
+            char * err =  "No unused ID available to add to ID list.";
+            exit_err(start_id == UINT8_MAX, err);
+            start_id++;
         }
-        exit_err(i == UINT8_MAX, "No unused ID available to add new thing.");
-        i++;
     }
+    struct NextAndId ** nai_ptr_ptr = start;
+    for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
+    *nai_ptr_ptr = nai;
+    return nai;
 }
 
 
 
-extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
+static uint8_t cell_is_proliferable(struct yx_uint8 test_pos, struct Thing * t)
 {
-    while (1)
+    if ('.' == world.map.cells[test_pos.y * world.map.length + test_pos.x])
     {
-        if (NULL == ptr || id == ptr->id)
-        {
-            return ptr;
-        }
-        if (deep)
+        struct Thing * t_test;
+        for (t_test = world.things; t_test; t_test = t_test->next)
         {
-            struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
-            if (NULL != owned_thing)
+            if (t_test->pos.y == test_pos.y && t_test->pos.x == test_pos.x)
             {
-                return ptr;
+                if (t_test->type == t->type)
+                {
+                    return 0;
+                }
+                if (t_test->lifepoints && t->lifepoints)
+                {
+                    return 0;
+                }
             }
         }
-        ptr = ptr->next;
+        return 1;
     }
+    return 0;
 }
 
 
 
-extern void free_thing_types(struct ThingType * tt_start)
+extern struct ThingAction * add_thing_action(uint8_t id)
 {
-    if (NULL == tt_start)
-    {
-        return;
-    }
-    free_thing_types(tt_start->next);
-    free(tt_start->name);
-    free(tt_start);
+    struct ThingAction * ta;
+    ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
+                                                   1, (int16_t) id, 2,
+                                                   (struct NextAndId **)
+                                                   &world.thing_actions);
+    set_cleanup_flag(CLEANUP_THING_ACTIONS);
+    ta->name = strdup(s[S_CMD_WAIT]);
+    ta->effort = 1;
+    ta->func = actor_wait;
+    return ta;
 }
 
 
 
-extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t find_pos)
+extern struct ThingType * add_thing_type(int16_t id)
 {
-    char * f_name = "add_thing()";
+    struct ThingType * tt;
+    tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
+                                                 0, id, 1,
+                                                 (struct NextAndId **)
+                                                 &world.thing_types);
+    set_cleanup_flag(CLEANUP_THING_TYPES);
+    tt->name = strdup("(none)");
+    tt->corpse_id = tt->id;
+    return tt;
+}
+
+
+
+extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
+{
+    struct Thing * t;
+    t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
+                                            (struct NextAndId **)&world.things);
     struct ThingType * tt = get_thing_type(type);
-    struct Thing *     t  = try_malloc(sizeof(struct Thing), f_name);
-    memset(t, 0, sizeof(struct Thing));
-    t->id = (0 <= id && id <= UINT8_MAX) ? id : get_lowest_unused_id();
+    set_cleanup_flag(CLEANUP_THINGS);
     t->type       = tt->id;
     t->lifepoints = tt->lifepoints;
-    char * err = "Space to put thing on too hard to find. Map too small?";
-    uint16_t i = 0;
-    memset(&(t->pos), 0, sizeof(struct yx_uint8));
-    while (find_pos)
+    t->pos.y      = y;
+    t->pos.x      = x;
+    if (t->lifepoints && world.exists)
     {
-        struct yx_uint8 pos;
-        for (pos.y = pos.x = 0; 0 == is_passable(pos); i++)
+        build_fov_map(t);
+    }
+    return t;
+}
+
+
+
+extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
+                                    uint8_t y, uint8_t x)
+{
+    struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
+    tm->type = type;
+    tm->pos.y = y;
+    tm->pos.x = x;
+    tm->next = t->t_mem;
+    t->t_mem = tm;
+}
+
+
+
+extern void free_thing_actions(struct ThingAction * ta)
+{
+    if (!ta)
+    {
+        return;
+    }
+    free_thing_actions(ta->next);
+    free(ta->name);
+    free(ta);
+}
+
+
+
+extern void free_thing_types(struct ThingType * tt)
+{
+    if (!tt)
+    {
+        return;
+    }
+    free_thing_types(tt->next);
+    free(tt->name);
+    free(tt);
+}
+
+
+
+extern void free_things(struct Thing * t)
+{
+    if (!t)
+    {
+        return;
+    }
+    free_things(t->owns);
+    free_things(t->next);
+    free(t->fov_map);
+    free(t->mem_map);
+    free(t->mem_depth_map);
+    free_things_in_memory(t->t_mem);
+    free(t);
+    if (t == world.things)         /* So add_things()' NULL-delimited thing   */
+    {                              /* iteration loop does not iterate over    */
+        world.things = NULL;       /* freed memory when called the first time */
+    }                              /* after world re-seeding.                 */
+}
+
+
+
+extern void free_things_in_memory(struct ThingInMemory * tm)
+{
+    if (!tm)
+    {
+        return;
+    }
+    free_things_in_memory(tm->next);
+    free(tm);
+}
+
+
+
+extern struct ThingAction * get_thing_action(uint8_t id)
+{
+    struct ThingAction * ta = world.thing_actions;
+    for (; ta && id != ta->id; ta = ta->next);
+    return ta;
+}
+
+
+
+extern struct ThingType * get_thing_type(uint8_t id)
+{
+    struct ThingType * tt = world.thing_types;
+    for (; tt && id != tt->id; tt = tt->next);
+    return tt;
+}
+
+
+
+extern uint8_t get_thing_action_id_by_name(char * name)
+{
+    struct ThingAction * ta = world.thing_actions;
+    while (ta)
+    {
+        if (0 == strcmp(ta->name, name))
         {
-            exit_err(UINT16_MAX == i, err);
-            pos.y = rrand() % world.map.length;
-            pos.x = rrand() % world.map.length;
+            break;
         }
-        struct Thing * t_ptr;
-        uint8_t clear = 1;
-        for (t_ptr = world.things; t_ptr != NULL; t_ptr = t_ptr->next)
+        ta = ta->next;
+    }
+    if (!ta)
+    {
+        return 0;
+    }
+    return ta->id;
+}
+
+
+
+extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
+{
+    while (1)
+    {
+        if (!ptr || id == ptr->id)
         {
-            if (yx_uint8_cmp(&pos, &t_ptr->pos) && 0 != t_ptr->lifepoints)
-            {
-                clear = 0;
-                break;
-            }
+            return ptr;
         }
-        if (1 == clear)
+        if (deep)
         {
-            t->pos = pos;
-            break;
+            struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
+            if (owned_thing)
+            {
+                return ptr;
+            }
         }
+        ptr = ptr->next;
     }
-    struct Thing ** t_ptr_ptr = &world.things;
-    for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
-    * t_ptr_ptr = t;
-    return t;
 }
 
 
 
-extern void add_things(uint8_t type, uint8_t n)
+extern struct Thing * get_player()
 {
-    uint8_t i;
-    for (i = 0; i < n; i++)
+    return get_thing(world.things, 0, 0);
+}
+
+
+
+extern void try_thing_proliferation(struct Thing * t)
+{
+    struct ThingType * tt = get_thing_type(t->type);
+    if (tt->proliferate)
     {
-        add_thing(-1, type, 1);
+        if (1 == tt->proliferate || 1 == (rrand() % tt->proliferate))
+        {
+            struct yx_uint8 candidates[6];
+            uint8_t n_candidates = 0;
+            char dirs[7] = "dxswed";
+            struct yx_uint8 test = t->pos;
+            uint8_t i;
+            for (i = 0; i < strlen(dirs); i++)
+            {
+                if (   mv_yx_in_dir_legal(dirs[i], &test)
+                    && cell_is_proliferable(test, t))
+                {
+                        candidates[n_candidates] = test;
+                        n_candidates++;
+                }
+            }
+            if (!n_candidates)
+            {
+                return;
+            }
+            i = rrand() % n_candidates;
+            add_thing(-1, tt->id, candidates[i].y, candidates[i].x);
+        }
     }
 }
 
 
 
-extern void free_things(struct Thing * t_start)
+extern void add_things(uint8_t type, uint8_t n)
 {
-    if (NULL == t_start)
+    uint8_t i;
+    for (i = 0; i < n; i++)
     {
-        return;
+        struct yx_uint8 pos;
+        while (1)
+        {
+            char * err="Space to put thing on too hard to find. Map too small?";
+            uint16_t i_pos = 0;
+            for (pos.y = pos.x = 0;
+                 '.' != world.map.cells[pos.y * world.map.length + pos.x];
+                 i_pos++)
+            {
+                exit_err(UINT16_MAX == i_pos, err);
+                pos.y = rrand() % world.map.length;
+                pos.x = rrand() % world.map.length;
+            }
+            struct Thing * t;
+            uint8_t clear = 1;
+            for (t = world.things; t; t = t->next)
+            {
+                if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
+                {
+                    clear = 0;
+                    break;
+                }
+            }
+            if (1 == clear)
+            {
+                break;
+            }
+        }
+        add_thing(-1, type, pos.y, pos.x);
     }
-    free_things(t_start->owns);
-    free_things(t_start->next);
-    free(t_start->fov_map);
-    free(t_start);
-    if (t_start == world.things)   /* So add_things()' NULL-delimited thing   */
-    {                              /* iteration loop does not iterate over    */
-        world.things = NULL;       /* freed memory when called the first time */
-    }                              /* after world re-seeding.                 */
 }
 
 
@@ -170,39 +396,16 @@ extern void own_thing(struct Thing ** target, struct Thing ** source,
         penult->next = t->next;
     }
     struct Thing ** t_ptr_ptr = target;
-    for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
+    for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
     * t_ptr_ptr = t;
     t->next = NULL;
 }
 
 
 
-extern struct Thing * get_player()
-{
-    return get_thing(world.things, 0, 1);
-}
-
-
-
-extern struct ThingType * get_thing_type(uint8_t id)
-{
-    char * f_name = "get_thing_type()";
-    struct ThingType * tt = world.thing_types;
-    for (; NULL != tt && id != tt->id; tt = tt->next);
-    char * err_intro = "Requested thing type of unused ID ";
-    uint16_t size = strlen(err_intro) + 3 + 1 + 1;
-    char * err = try_malloc(size, f_name);
-    sprintf(err, "%s%d.", err_intro, id);
-    exit_err(NULL == tt, err);
-    free(err);
-    return tt;
-}
-
-
-
 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
 {
     t->pos = pos;
     struct Thing * owned = t->owns;
-    for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);
+    for (; owned; set_thing_position(owned, pos), owned = owned->next);
 }