#include "map_objects.h"
#include <stdlib.h> /* for free(), atoi() */
-#include <stdint.h> /* for uint8_t */
+#include <stdint.h> /* for uint8_t, uint16_t */
#include <stdio.h> /* for FILE typedef */
-#include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
+#include <string.h> /* for strlen(), memcpy(), strtok() */
#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(),
* try_fgets()
*/
#include "misc.h" /* for try_malloc(), find_passable_pos() */
#include "main.h" /* for world global */
-#include "rexit.h" /* for err_exit() */
+#include "rexit.h" /* for exit_err() */
#include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */
char line[linemax + 1];
while (try_fgets(line, linemax + 1, file, f_name))
{
- struct MapObjDef * mod;
- mod = try_malloc(sizeof(struct MapObjDef), f_name);
+ struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
mod->next = NULL;
mod->id = atoi(strtok(line, delim));
mod->corpse_id = 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;
- }
* mo_ptr_ptr = mo;
mo_ptr_ptr = &mo->next;
}
{
uint8_t id = atoi(strtok(line, delim));
uint8_t i;
- for (i = 0; i < 7; i++)
- {
- strtok(NULL, delim);
- }
+ for (i = 0; i < 7; i++, strtok(NULL, delim));
char * owned = strtok(NULL, "\n");
if (NULL != owned)
{
owned_id = strtok(owned, delim);
while (NULL != owned_id)
{
- own_map_object(&mo->owns, &world.map_objs,
- (uint8_t) atoi(owned_id));
+ own_map_object(&mo->owns, &world.map_objs, atoi(owned_id));
owned_id = strtok(NULL, delim);
}
}
{
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;
+ struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
+ mo->id = world.map_obj_count++;
+ mo->type = mod->id;
mo->lifepoints = mod->lifepoints;
while (1)
{
}
}
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 * mo_ptr;
- while (NULL != * last_ptr_ptr)
- {
- mo_ptr = * last_ptr_ptr;
- last_ptr_ptr = & mo_ptr->next;
- }
- * last_ptr_ptr = mo;
+ mo->command = 0;
+ mo->arg = 0;
+ mo->owns = NULL;
+ mo->next = NULL;
+ struct MapObj ** mo_ptr_ptr = &world.map_objs;
+ for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
+ * mo_ptr_ptr = mo;
}
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;
+ struct MapObj ** mo_ptr_ptr = target;
+ for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
+ * mo_ptr_ptr = mo;
mo->next = NULL;
}
extern struct MapObjDef * get_map_object_def(uint8_t id)
{
struct MapObjDef * mod = world.map_obj_defs;
- while (id != mod->id)
- {
- mod = mod->next;
- }
+ for (; id != mod->id; mod = mod->next);
return mod;
}
{
mo->pos = pos;
struct MapObj * owned = mo->owns;
- for (; owned != NULL; owned = owned->next)
- {
- set_object_position(owned, pos);
- }
+ for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);
}
+
uint8_t type; /* ID of appropriate map object definition */
uint8_t lifepoints; /* 0: object is inanimate; >0: hitpoints */
struct yx_uint16 pos; /* coordinate on map */
- uint8_t command; /* command map object tries to realize now*/
- uint8_t arg; /* optional field for command argument */
+ uint8_t command; /* map object's current action */
+ uint8_t arg; /* optional field for .command argument */
uint8_t progress; /* turns already passed to realize .command */
};
struct MapObjDef
{
struct MapObjDef * next;
- uint8_t id; /* unique identifier of map object type */
- uint8_t corpse_id; /* id of type to change into upon destruction */
+ uint8_t id; /* map object definition identifier / sets .type */
+ uint8_t corpse_id; /* type to change map object into upon destruction */
char char_on_map; /* map object symbol to appear on map */
- char * name; /* string to describe object in game log*/
- uint8_t lifepoints; /* default value for map object lifepoints member */
+ char * name; /* string to describe object in game log */
+ uint8_t lifepoints; /* default start value for map object's .lifepoints */
};
-/* Initialize map object defnitions chain from file at path "filename". */
+/* Initialize map object definitions chain from file at path "filename". */
extern void init_map_object_defs(char * filename);
/* Free map object definitions chain starting at "mod_start". */
extern void free_map_object_defs(struct MapObjDef * mod_start);
-/* Add new object(s) ("n": how many?) of "type" to map on random position(s).
- * New animate objects are never placed in the same square with other animated
- * ones.
- */
-extern void add_map_object(uint8_t type);
-extern void add_map_objects(uint8_t type, uint8_t n);
-
/* Write map objects chain to "file". */
extern void write_map_objects(FILE * file);
-/* Read from "file" map objects chain; use "line" as char array for fgets() and
- * expect strings of max. "linemax" length.
+/* Read map objects chain from "file"; use "line" as char array for fgets() and
+ * expect line strings of max. "linemax" length to be read by it.
*/
extern void read_map_objects(FILE * file, char * line, int linemax);
+/* Add object(s) ("n": how many?) of "type" to map on random position(s). New
+ * animate objects are never placed in the same square with other animate ones.
+ */
+extern void add_map_object(uint8_t type);
+extern void add_map_objects(uint8_t type, uint8_t n);
+
/* Free map objects in map object chain starting at "mo_start. */
extern void free_map_objects(struct MapObj * mo_start);
try_fwrite(line, strlen(line), 1, file, f_name);
sprintf(line, "%u\n", world.seed);
try_fwrite(line, strlen(line), 1, file, f_name);
+ sprintf(line, "%u\n", world.map_obj_count);
+ try_fwrite(line, strlen(line), 1, file, f_name);
sprintf(line, "%u\n", world.turn);
try_fwrite(line, strlen(line), 1, file, f_name);
sprintf(line, "%u\n", world.score);
try_fgets(line, linemax + 1, file, f_name);
world.seed = atoi(line);
try_fgets(line, linemax + 1, file, f_name);
+ world.map_obj_count = atoi(line);
+ try_fgets(line, linemax + 1, file, f_name);
world.turn = atoi(line);
try_fgets(line, linemax + 1, file, f_name);
world.score = atoi(line);