X-Git-Url: https://plomlompom.com/repos/test.html?a=blobdiff_plain;f=src%2Fserver%2Fmap_objects.c;h=ff062d7bee8e4995bd347132ce95a9dd2015e94d;hb=cc4ed0c49279f08a053a3e3a9a4acba22283a01f;hp=5b5bd975cbfe9d382c4313c36be51edc5221d222;hpb=dd9d65ee727ac7e95801da0f8b5bae7009811802;p=plomrogue
diff --git a/src/server/map_objects.c b/src/server/map_objects.c
index 5b5bd97..ff062d7 100644
--- a/src/server/map_objects.c
+++ b/src/server/map_objects.c
@@ -1,20 +1,17 @@
/* src/server/map_objects.c */
#include "map_objects.h"
-#include /* FILE typedef */
+#include /* NULL */
#include /* uint8_t, uint16_t */
-#include /* free(), atoi() */
-#include /* strlen(), memcpy(), strtok() */
-#include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
- * textfile_sizes()
- */
+#include /* free() */
+#include /* memset(), strlen() */
+#include "../common/rexit.h" /* exit_err() */
#include "../common/try_malloc.h" /* try_malloc() */
-#include "../common/yx_uint16.h" /* yx_uint16 struct */
-#include "cleanup.h" /* set_cleanup_flag() */
+#include "../common/yx_uint8.h" /* yx_uint8 struct */
#include "map.h" /* is_passable() */
#include "rrand.h" /* rrand() */
#include "world.h" /* global world */
-#include "yx_uint16.h" /* yx_uint16_cmp() */
+#include "yx_uint8.h" /* yx_uint8_cmp() */
@@ -22,7 +19,7 @@
static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
/* Return random passable (as by is_passable()) position on world.map. */
-static struct yx_uint16 find_passable_pos();
+static struct yx_uint8 find_passable_pos();
/* Add object of "type" to map on random position. Don't place actor on actor.*/
static void add_map_object(uint8_t type);
@@ -48,9 +45,9 @@ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
-static struct yx_uint16 find_passable_pos() // struct Map * map)
+static struct yx_uint8 find_passable_pos()
{
- struct yx_uint16 pos;
+ struct yx_uint8 pos;
for (pos.y = pos.x = 0; 0 == is_passable(pos);)
{
pos.y = rrand() % world.map.size.y;
@@ -66,17 +63,18 @@ static void add_map_object(uint8_t type)
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;
while (1)
{
- struct yx_uint16 pos = find_passable_pos(world.map);
+ struct yx_uint8 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)
{
- if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
+ if (yx_uint8_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
{
clear = 0;
break;
@@ -88,11 +86,6 @@ static void add_map_object(uint8_t type)
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;
@@ -100,34 +93,6 @@ static void add_map_object(uint8_t type)
-extern void init_map_object_defs(char * filename)
-{
- char * f_name = "init_map_object_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, 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));
- 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);
- * last_mod_ptr_ptr = mod;
- last_mod_ptr_ptr = &mod->next;
- }
- try_fclose(file, f_name);
- set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
-}
-
-
-
extern void free_map_object_defs(struct MapObjDef * mod_start)
{
if (NULL == mod_start)
@@ -137,7 +102,6 @@ extern void free_map_object_defs(struct MapObjDef * mod_start)
free_map_object_defs(mod_start->next);
free(mod_start->name);
free(mod_start);
- mod_start = NULL;
}
@@ -162,10 +126,10 @@ extern void free_map_objects(struct MapObj * mo_start)
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. */
}
@@ -211,13 +175,17 @@ extern struct MapObj * get_player()
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;
}
-extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
+extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos)
{
mo->pos = pos;
struct MapObj * owned = mo->owns;