/* src/server/map_objects.c */
#include "map_objects.h"
+#include <stddef.h> /* NULL */
#include <stdio.h> /* FILE typedef */
-#include <stdint.h> /* uint8_t, uint16_t */
+#include <stdint.h> /* uint8_t, uint16_t, UINT8_MAX */
#include <stdlib.h> /* free(), atoi() */
-#include <string.h> /* strlen(), memcpy(), strtok() */
-#include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
+#include <string.h> /* strlen(), memcpy(), memset() */
+#include "../common/err_try_fgets.h" /* err_try_fgets(), err_line(),
+ * reset_err_try_fgets_counter()
+ */
+#include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgetc(),
* textfile_sizes()
*/
+#include "../common/rexit.h" /* exit_err(), exit_trouble() */
#include "../common/try_malloc.h" /* try_malloc() */
#include "../common/yx_uint16.h" /* yx_uint16 struct */
#include "cleanup.h" /* set_cleanup_flag() */
-static struct yx_uint16 find_passable_pos() // struct Map * map)
+static struct yx_uint16 find_passable_pos()
{
struct yx_uint16 pos;
for (pos.y = pos.x = 0; 0 == is_passable(pos);)
char * f_name = "add_map_object()";
struct MapObjDef * mod = get_map_object_def(type);
struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
+ memset(mo, 0, sizeof(struct MapObj));
mo->id = world.map_obj_count++;
mo->type = mod->id;
mo->lifepoints = mod->lifepoints;
break;
}
}
- mo->progress = 0;
- 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;
extern void init_map_object_defs(char * filename)
{
char * f_name = "init_map_object_defs()";
+ char * context = "Failed reading map object definitions file. ";
+ char * err_toolarge = "Value is too large.";
+ char * err_uniq = "Declaration of ID already used.";
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, f_name))
+ reset_err_try_fgets_counter();
+ while (1)
{
+ int test_for_end = try_fgetc(file, f_name);
+ if (EOF == test_for_end || '\n' == test_for_end)
+ {
+ break;
+ }
+ exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
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));
- 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, f_name);
- memcpy(mod->name, name, strlen(name) + 1);
+ err_try_fgets(line, linemax, file, context, "nfi");
+ err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
+ mod->id = atoi(line);
+ struct MapObjDef * mod_test = world.map_obj_defs;
+ for (; NULL != mod_test; mod_test = mod_test->next)
+ {
+ err_line(mod->id == mod_test->id, line, context, err_uniq);
+ }
+ err_try_fgets(line, linemax, file, context, "0nfi");
+ err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
+ mod->corpse_id = atoi(line);
+ err_try_fgets(line, linemax, file, context, "0nfs");
+ mod->char_on_map = line[0];
+ err_try_fgets(line, linemax, file, context, "0nfi");
+ err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
+ mod->lifepoints = atoi(line);
+ err_try_fgets(line, linemax, file, context, "0nf");
+ line[strlen(line) - 1] = '\0';
+ mod->name = try_malloc(strlen(line) + 1, f_name);
+ memcpy(mod->name, line, strlen(line) + 1);
* last_mod_ptr_ptr = mod;
last_mod_ptr_ptr = &mod->next;
+ err_try_fgets(line, linemax, file, context, "d");
}
try_fclose(file, f_name);
set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
free_map_object_defs(mod_start->next);
free(mod_start->name);
free(mod_start);
- mod_start = NULL;
}
free_map_objects(mo_start->owns);
free_map_objects(mo_start->next);
free(mo_start);
- if (mo_start == world.map_objs)
- {
- world.map_objs = NULL;
- }
+ if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */
+ { /* map object iteration loop does not */
+ world.map_objs = NULL; /* iterate over freed memory when called */
+ } /* the 1st time after world re-seeding. */
}
extern struct MapObjDef * get_map_object_def(uint8_t id)
{
struct MapObjDef * mod = world.map_obj_defs;
- for (; id != mod->id; mod = mod->next);
+ for (; NULL != mod && id != mod->id; mod = mod->next);
+ char * err_intro = "Requested map object definition of unused ID ";
+ char err[strlen(err_intro) + 3 + 1 + 1];
+ sprintf(err, "%s%d.", err_intro, id);
+ exit_err(NULL == mod, err);
return mod;
}