home · contact · privacy
Simplified textfile_sizes() and replaced all get_linemax() calls with it.
[plomrogue] / src / map_objects.c
index ed41d93ac62f30ab65c5fd194182782a8d034156..0407352e8e494ebf95078eeac1401cb971e0b964 100644 (file)
@@ -5,40 +5,93 @@
 #include <stdint.h> /* for uint8_t */
 #include <stdio.h> /* for FILE typedef */
 #include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
-#include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose()
-                        * [read/write]_uint[8/16/23][_bigendian]()
+#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(),
+                        * try_fgets()
                         */
-#include "misc.h" /* for try_malloc(), try_calloc(), find_passable_pos() */
-#include "main.h" /* for World struct */
+#include "misc.h" /* for try_malloc(), find_passable_pos() */
+#include "main.h" /* for world global */
 #include "rexit.h" /* for err_exit() */
 #include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */
 
 
 
-extern void init_map_object_defs(struct World * world, char * filename)
+/* Write representation of "mo" and all of the map objects it owns to "file". */
+static void write_map_object(FILE * file, struct MapObj * mo);
+
+/* Return pointer to map object of "id" in chain starting at "ptr". */
+static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
+
+
+
+static void write_map_object(FILE * file, struct MapObj * mo)
+{
+    char * f_name = "write_map_object()";
+    struct MapObj * mo_ptr = mo->owns;
+    uint8_t i = 0;
+    for (; NULL != mo_ptr; mo_ptr = mo_ptr->next, i++);
+    uint8_t size = 3+1 + 3+1 + 3+1 + 5+1 + 5 + ((1+3)*i) + 1 + 1;
+    char line[size];
+    sprintf(line, "%d %d %d %d %d %d %d %d",
+                  mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x,
+                  mo->command, mo->arg, mo->progress);
+    for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
+    {
+        sprintf(line + strlen(line), " %d", mo_ptr->id);
+    }
+    line[strlen(line) + 1] = '\0';
+    line[strlen(line)] = '\n';
+    try_fwrite(line, strlen(line), 1, file, f_name);
+    for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
+    {
+        write_map_object(file, mo_ptr);
+    }
+}
+
+
+
+static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
+{
+    while (1)
+    {
+        if (NULL == ptr || id == ptr->id)
+        {
+            return ptr;
+        }
+        struct MapObj * owned_object = get_map_object(ptr->owns, id);
+        if (NULL != owned_object)
+        {
+            return ptr;
+        }
+        ptr = ptr->next;
+    }
+}
+
+
+
+extern void init_map_object_defs(char * filename)
 {
     char * f_name = "init_map_object_defs()";
-    FILE * file = try_fopen(filename, "r", world, f_name);
-    uint16_t linemax = get_linemax(file, world, f_name);
-    struct MapObjDef ** last_mod_ptr_ptr = &world->map_obj_defs;
+    FILE * file = try_fopen(filename, "r", f_name);
+    uint16_t linemax = textfile_sizes(file, NULL);
+    struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
     char * delim = " ";
     char line[linemax + 1];
-    while (try_fgets(line, linemax + 1, file, world, f_name))
+    while (try_fgets(line, linemax + 1, file, f_name))
     {
         struct MapObjDef * mod;
-        mod = try_malloc(sizeof(struct MapObjDef), world, f_name);
+        mod = try_malloc(sizeof(struct MapObjDef), f_name);
         mod->next = NULL;
         mod->id = atoi(strtok(line, delim));
         mod->corpse_id = atoi(strtok(NULL, delim));
         mod->char_on_map = * strtok(NULL, delim);
         mod->lifepoints = atoi(strtok(NULL, delim));
         char * name = strtok(NULL, "\n");
-        mod->name = try_malloc(strlen(name) + 1, world, f_name);
+        mod->name = try_malloc(strlen(name) + 1, f_name);
         memcpy(mod->name, name, strlen(name) + 1);
         * last_mod_ptr_ptr = mod;
         last_mod_ptr_ptr = &mod->next;
     }
-    try_fclose(file, world, f_name);
+    try_fclose(file, f_name);
 }
 
 
@@ -56,67 +109,88 @@ extern void free_map_object_defs(struct MapObjDef * mod_start)
 
 
 
-extern void write_map_objects(struct World * world, FILE * file)
+extern void write_map_objects(FILE * file)
 {
-    char * f_name = "write_map_objects()";
-    struct MapObj * mo = world->map_objs;
-    uint8_t size = 3 + 1 + 3 + 1 + 3 + 1 + 5 + 1 + 5 + 1;
-    char line[size];
+    struct MapObj * mo = world.map_objs;
     while (NULL != mo)
     {
-        sprintf(line, "%d %d %d %d %d\n",
-                mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x);
-        try_fwrite(line, strlen(line), 1, file, world, f_name);
+        write_map_object(file, mo);
         mo = mo->next;
     }
 }
 
 
 
-extern void read_map_objects(struct World * world, FILE * file, char * line,
-                              int linemax)
+extern void read_map_objects(FILE * file, char * line, int linemax)
 {
     char * f_name = "read_map_objects()";
-    struct MapObj ** mo_ptr_ptr = &world->map_objs;
+    struct MapObj ** mo_ptr_ptr = &world.map_objs;
     char * delim = " ";
     struct MapObj * mo;
-    while (try_fgets(line, linemax + 1, file, world, f_name))
+    fpos_t pos;
+    exit_err(-1 == fgetpos(file, &pos), f_name);
+    while (try_fgets(line, linemax + 1, file, f_name))
     {
-        mo = malloc(sizeof(struct MapObj));
-        mo->next = NULL;
-        mo->id = atoi(strtok(line, delim));
-        if (mo->id > world->map_obj_count)
+        mo = try_malloc(sizeof(struct MapObj), f_name);
+        mo->next       = NULL;
+        mo->id         = atoi(strtok(line, delim));
+        mo->type       = atoi(strtok(NULL, delim));
+        mo->lifepoints = atoi(strtok(NULL, delim));
+        mo->pos.y      = atoi(strtok(NULL, delim));
+        mo->pos.x      = atoi(strtok(NULL, delim));
+        mo->command    = atoi(strtok(NULL, delim));;
+        mo->arg        = atoi(strtok(NULL, delim));;
+        mo->progress   = atoi(strtok(NULL, delim));;
+        mo->owns       = NULL;
+        if (mo->id > world.map_obj_count)
         {
-            world->map_obj_count = mo->id;
+            world.map_obj_count = mo->id;
         }
-        mo->type = atoi(strtok(NULL, delim));
-        mo->lifepoints = atoi(strtok(NULL, delim));
-        mo->pos.y = atoi(strtok(NULL, delim));
-        mo->pos.x = atoi(strtok(NULL, delim));
         * mo_ptr_ptr = mo;
         mo_ptr_ptr = &mo->next;
     }
+    exit_err(-1 == fsetpos(file, &pos), f_name);
+    while (try_fgets(line, linemax + 1, file, f_name))
+    {
+        uint8_t id = atoi(strtok(line, delim));
+        uint8_t i;
+        for (i = 0; i < 7; i++)
+        {
+            strtok(NULL, delim);
+        }
+        char * owned = strtok(NULL, "\n");
+        if (NULL != owned)
+        {
+            mo = get_map_object(world.map_objs, id);
+            char * owned_id = "";
+            owned_id = strtok(owned, delim);
+            while (NULL != owned_id)
+            {
+                own_map_object(&mo->owns, &world.map_objs,
+                               (uint8_t) atoi(owned_id));
+                owned_id = strtok(NULL, delim);
+            }
+        }
+    }
 }
 
 
 
-extern void add_map_object(struct World * world, uint8_t type)
+extern void add_map_object(uint8_t type)
 {
-    char * f_name = "add_map_object";
-    struct MapObjDef * mod = get_map_object_def(world, type);
-    struct MapObj * mo = try_malloc(sizeof(struct MapObj), world, f_name);
-    mo->id = world->map_obj_count;
-    world->map_obj_count++;
+    char * f_name = "add_map_object()";
+    struct MapObjDef * mod = get_map_object_def(type);
+    struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
+    mo->id = world.map_obj_count;
+    world.map_obj_count++;
     mo->type = mod->id;
     mo->lifepoints = mod->lifepoints;
     while (1)
     {
-        struct yx_uint16 pos = find_passable_pos(world->map);
+        struct yx_uint16 pos = find_passable_pos(world.map);
         struct MapObj * mo_ptr;
         uint8_t clear = 1;
-        for (mo_ptr = world->map_objs;
-             mo_ptr != NULL;
-             mo_ptr = mo_ptr->next)
+        for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
         {
             if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
             {
@@ -130,8 +204,12 @@ extern void add_map_object(struct World * world, uint8_t type)
             break;
         }
     }
+    mo->progress = 0;
+    mo->command = 0;
+    mo->arg = 0;
+    mo->owns = NULL;
     mo->next = NULL;
-    struct MapObj ** last_ptr_ptr = &world->map_objs;
+    struct MapObj ** last_ptr_ptr = &world.map_objs;
     struct MapObj * mo_ptr;
     while (NULL != * last_ptr_ptr)
     {
@@ -143,12 +221,12 @@ extern void add_map_object(struct World * world, uint8_t type)
 
 
 
-extern void add_map_objects(struct World * world, uint8_t type, uint8_t n)
+extern void add_map_objects(uint8_t type, uint8_t n)
 {
     uint8_t i;
     for (i = 0; i < n; i++)
     {
-        add_map_object(world, type);
+        add_map_object(type);
     }
 }
 
@@ -160,37 +238,74 @@ extern void free_map_objects(struct MapObj * mo_start)
     {
         return;
     }
+    free_map_objects(mo_start->owns);
     free_map_objects(mo_start->next);
     free(mo_start);
 }
 
 
 
-extern struct MapObj * get_player(struct World * world)
+extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
+                           uint8_t id)
 {
-    struct MapObj * ptr = world->map_objs;
-    while (1)
+    struct MapObj * mo;
+    if (id == (*source)->id)
     {
-        if (NULL == ptr)
-        {
-            return ptr;
-        }
-        if (0 == ptr->id)
+        mo = * source;
+        * source = mo->next;
+    }
+    else
+    {
+        struct MapObj * penult = * source;
+        while (1)
         {
-            return ptr;
+            if (id == penult->next->id)
+            {
+                break;
+            }
+            penult = penult->next;
         }
-        ptr = ptr->next;
+        mo = penult->next;
+        penult->next = mo->next;
+    }
+    struct MapObj ** last_ptr_ptr = target;
+    struct MapObj * mo_ptr;
+    while (NULL != * last_ptr_ptr)
+    {
+        mo_ptr = * last_ptr_ptr;
+        last_ptr_ptr = & mo_ptr->next;
     }
+    * last_ptr_ptr = mo;
+    mo->next = NULL;
+}
+
+
+
+extern struct MapObj * get_player()
+{
+    return get_map_object(world.map_objs, 0);
 }
 
 
 
-extern struct MapObjDef * get_map_object_def(struct World * w, uint8_t id)
+extern struct MapObjDef * get_map_object_def(uint8_t id)
 {
-    struct MapObjDef * mod = w->map_obj_defs;
+    struct MapObjDef * mod = world.map_obj_defs;
     while (id != mod->id)
     {
         mod = mod->next;
     }
     return mod;
 }
+
+
+
+extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
+{
+    mo->pos = pos;
+    struct MapObj * owned = mo->owns;
+    for (; owned != NULL; owned = owned->next)
+    {
+        set_object_position(owned, pos);
+    }
+}