aboutsummaryrefslogtreecommitdiff
path: root/src/world.c
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2016-03-28 14:11:22 -0400
committerFranklin Wei <git@fwei.tk>2016-03-31 17:09:36 -0400
commited95a5621ac9c4f5002e68a981f8b24d5caaedf4 (patch)
treeaee87cf74280e8f9dd29aca88d12a638f37b2493 /src/world.c
parent13052597a19fd2212efb7f51f19ed73b9f4b6ba4 (diff)
downloadnetcosm-ed95a5621ac9c4f5002e68a981f8b24d5caaedf4.zip
netcosm-ed95a5621ac9c4f5002e68a981f8b24d5caaedf4.tar.gz
netcosm-ed95a5621ac9c4f5002e68a981f8b24d5caaedf4.tar.bz2
netcosm-ed95a5621ac9c4f5002e68a981f8b24d5caaedf4.tar.xz
kludge things to compile on old linux
Diffstat (limited to 'src/world.c')
-rw-r--r--src/world.c50
1 files changed, 43 insertions, 7 deletions
diff --git a/src/world.c b/src/world.c
index a854e49..8c25d7d 100644
--- a/src/world.c
+++ b/src/world.c
@@ -29,6 +29,9 @@ static struct room_t *world;
static size_t world_sz;
static char *world_name;
+/* map of room names -> rooms */
+static void *world_map = NULL;
+
struct room_t *room_get(room_id id)
{
return world + id;
@@ -124,8 +127,22 @@ void world_save(const char *fname)
world[i].data.hook_serialize(i, fd);
}
+ /* write the object counter so future objects will have sequential ids */
write_uint64(fd, obj_get_idcounter());
+ /* now write the map of room names to ids */
+ void *ptr = world_map, *save;
+ for(unsigned int i = 0; i < world_sz; ++i)
+ {
+ void *key;
+ struct room_t *room = hash_iterate(ptr, &save, &key);
+ if(!room)
+ break;
+ ptr = NULL;
+ write_string(fd, key);
+ write_roomid(fd, &room->id);
+ }
+
close(fd);
}
@@ -230,6 +247,18 @@ bool world_load(const char *fname, const struct roomdata_t *data, size_t data_sz
obj_set_idcounter(read_uint64(fd));
+ /* read in the room name -> room map */
+
+ world_map = hash_init(world_sz * 2, hash_djb, compare_strings);
+
+ for(unsigned int i = 0; i < world_sz; ++i)
+ {
+ const char *key = read_string(fd);
+ room_id id = read_roomid(fd);
+ debugf("'%s' -> %d\n", key, id);
+ hash_insert(world_map, key, world + id);
+ }
+
close(fd);
return true;
}
@@ -240,12 +269,12 @@ static SIMP_EQUAL(enum direction_t, dir_equal);
/* loads room data (supplied by the world module) into our internal format */
void world_init(const struct roomdata_t *data, size_t sz, const char *name)
{
- debugf("Loading world with %lu rooms.\n", sz);
+ debugf("Loading world with %zu rooms.\n", sz);
world = calloc(sz, sizeof(struct room_t));
world_sz = 0;
world_name = strdup(name);
- void *map = hash_init(sz / 2 + 1, hash_djb, compare_strings);
+ world_map = hash_init(sz * 2, hash_djb, compare_strings);
for(size_t i = 0; i < sz; ++i)
{
@@ -258,7 +287,7 @@ void world_init(const struct roomdata_t *data, size_t sz, const char *name)
world[i].data.desc = strdup(world[i].data.desc);
debugf("Loading room '%s'\n", world[i].data.uniq_id);
- if(hash_insert(map, world[i].data.uniq_id, world + i))
+ if(hash_insert(world_map, world[i].data.uniq_id, world + i))
error("Duplicate room ID '%s'", world[i].data.uniq_id);
for(int dir = 0; dir < NUM_DIRECTIONS; ++dir)
@@ -266,7 +295,7 @@ void world_init(const struct roomdata_t *data, size_t sz, const char *name)
const char *adjacent_room = world[i].data.adjacent[dir];
if(adjacent_room)
{
- struct room_t *room = hash_lookup(map, adjacent_room);
+ struct room_t *room = hash_lookup(world_map, adjacent_room);
if(room)
world[i].adjacent[dir] = room->id;
}
@@ -284,7 +313,7 @@ void world_init(const struct roomdata_t *data, size_t sz, const char *name)
const char *adjacent_room = world[i].data.adjacent[dir];
if(adjacent_room)
{
- struct room_t *room = hash_lookup(map, adjacent_room);
+ struct room_t *room = hash_lookup(world_map, adjacent_room);
if(room)
world[i].adjacent[dir] = room->id;
else
@@ -339,8 +368,6 @@ void world_init(const struct roomdata_t *data, size_t sz, const char *name)
}
hash_free(dir_map);
-
- hash_free(map);
}
static void *verb_map = NULL;
@@ -368,3 +395,12 @@ void *world_verb_map(void)
init_map();
return verb_map;
}
+
+room_id room_get_id(const char *uniq_id)
+{
+ struct room_t *room = hash_lookup(world_map, uniq_id);
+ if(!room)
+ return ROOM_NONE;
+ else
+ return room->id;
+}