aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2016-04-28 20:39:37 -0400
committerFranklin Wei <git@fwei.tk>2016-04-28 20:39:37 -0400
commit7f3ba14388a586a946d721a71eb3a9862f7f6c02 (patch)
tree05c96d39815a3719131ad966a73d1ba40fd96d75 /src
parent0db524c05e0e077c06c7d02eaa2921159df04313 (diff)
downloadnetcosm-7f3ba14388a586a946d721a71eb3a9862f7f6c02.zip
netcosm-7f3ba14388a586a946d721a71eb3a9862f7f6c02.tar.gz
netcosm-7f3ba14388a586a946d721a71eb3a9862f7f6c02.tar.bz2
netcosm-7f3ba14388a586a946d721a71eb3a9862f7f6c02.tar.xz
fix build dependency checking, cleanup, simulation callback
Diffstat (limited to 'src')
-rw-r--r--src/client_reqs.h2
-rw-r--r--src/hash.c3
-rw-r--r--src/server.c55
-rw-r--r--src/world.c35
-rw-r--r--src/world.h7
5 files changed, 84 insertions, 18 deletions
diff --git a/src/client_reqs.h b/src/client_reqs.h
index 0d2a63c..a2146c4 100644
--- a/src/client_reqs.h
+++ b/src/client_reqs.h
@@ -26,7 +26,7 @@
enum room_id;
-enum reqdata_typespec { TYPE_NONE = 0, TYPE_USERDATA, TYPE_BOOLEAN } reqdata_type;
+enum reqdata_typespec { TYPE_NONE = 0, TYPE_USERDATA, TYPE_BOOLEAN };
union reqdata_t {
struct userdata_t userdata;
diff --git a/src/hash.c b/src/hash.c
index 1e7b732..a2938e1 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -200,6 +200,7 @@ static void hash_internal_insert_new(const void *key, const void *data, struct h
/* resize if load factor exceeds threshold */
if((map->used_buckets << 16) / (map->table_sz) >= RESIZE_THRESHOLD)
{
+ //printf("resizing map of %zu elements...\n", map->used_buckets);
hash_resize(map, map->table_sz * 2);
}
}
@@ -548,7 +549,7 @@ void hash_dump(void *ptr)
--used_buckets;
while(iter)
{
- printf(" --> `%s'", iter->key);
+ printf(" --> `%s'", (char*)iter->key);
++n_entries;
iter = iter->next;
}
diff --git a/src/server.c b/src/server.c
index 75c87f6..b736124 100644
--- a/src/server.c
+++ b/src/server.c
@@ -42,7 +42,7 @@ static uint16_t port = DEFAULT_PORT;
static int server_socket;
/* for debugging: */
-static char *world_module = "build/worlds/netcosm_default.so";
+static char *world_module = "build/worlds/dunnet.so";
static char *module_handle = NULL;
/* save after every X changes to the world state */
@@ -188,27 +188,48 @@ static void check_userfile(void)
static void load_worldfile(void)
{
/* load the world module */
+
module_handle = dlopen(world_module, RTLD_NOW);
if(!module_handle)
error("cannot load world module `%s' (%s)", world_module, dlerror());
/* load symbols */
- size_t *ptr;
-
- netcosm_verb_classes = dlsym(module_handle, "netcosm_verb_classes");
- ptr = dlsym(module_handle, "netcosm_verb_classes_sz");
- netcosm_verb_classes_sz = *ptr;
+ {
+ size_t *ptr;
- netcosm_obj_classes = dlsym(module_handle, "netcosm_obj_classes");
- ptr = dlsym(module_handle, "netcosm_obj_classes_sz");
- netcosm_obj_classes_sz = *ptr;
+ netcosm_verb_classes = dlsym(module_handle, "netcosm_verb_classes");
+ ptr = dlsym(module_handle, "netcosm_verb_classes_sz");
+ netcosm_verb_classes_sz = *ptr;
- netcosm_world = dlsym(module_handle, "netcosm_world");
- ptr = dlsym(module_handle, "netcosm_world_sz");
- netcosm_world_sz = *ptr;
+ netcosm_obj_classes = dlsym(module_handle, "netcosm_obj_classes");
+ ptr = dlsym(module_handle, "netcosm_obj_classes_sz");
+ netcosm_obj_classes_sz = *ptr;
- char **tmp = dlsym(module_handle, "netcosm_world_name");
- netcosm_world_name = *tmp;
+ netcosm_world = dlsym(module_handle, "netcosm_world");
+ ptr = dlsym(module_handle, "netcosm_world_sz");
+ netcosm_world_sz = *ptr;
+ }
+ {
+ char **ptr = dlsym(module_handle, "netcosm_world_name");
+ netcosm_world_name = *ptr;
+ }
+ {
+ void *ptr;
+ ptr = dlsym(module_handle, "netcosm_world_simulation_cb");
+ netcosm_world_simulation_cb = ptr;
+ }
+ {
+ unsigned *ptr;
+ ptr = dlsym(module_handle, "netcosm_world_simulation_interval");
+ if(ptr)
+ netcosm_world_simulation_interval = *ptr;
+ else
+ {
+ netcosm_world_simulation_interval = 0;
+ if(netcosm_world_simulation_cb)
+ error("have simulation callback, but no interval specified");
+ }
+ }
if(access(WORLDFILE, F_OK) < 0)
{
@@ -483,6 +504,12 @@ int server_main(int argc, char *argv[])
parse_args(argc, argv);
+ /* load default if none specified */
+ if(!world_module)
+ {
+ error("no world module specified");
+ }
+
/* this must be done before any world module data is used */
load_worldfile();
diff --git a/src/world.c b/src/world.c
index d7c3590..d0e1711 100644
--- a/src/world.c
+++ b/src/world.c
@@ -35,6 +35,10 @@ size_t netcosm_obj_classes_sz;
const struct roomdata_t *netcosm_world;
size_t netcosm_world_sz;
+/* simulation callback */
+void (*netcosm_world_simulation_cb)(void) = NULL;
+unsigned netcosm_world_simulation_interval = 0;
+
const char *netcosm_world_name;
/* processed world data */
@@ -157,6 +161,16 @@ void world_save(const char *fname)
close(fd);
}
+static ev_timer *sim_timer = NULL;
+
+static void sim_cb(EV_P_ ev_timer *w, int revents)
+{
+ (void) EV_A;
+ (void) w;
+ (void) revents;
+ netcosm_world_simulation_cb();
+}
+
void world_free(void)
{
if(world)
@@ -174,6 +188,20 @@ void world_free(void)
free(world);
world = NULL;
}
+ if(sim_timer)
+ free(sim_timer);
+}
+
+static void start_sim_callback(void)
+{
+ /* start callback */
+ if(netcosm_world_simulation_cb && netcosm_world_simulation_interval)
+ {
+ sim_timer = calloc(1, sizeof(ev_timer));
+ ev_timer_init(sim_timer, sim_cb, netcosm_world_simulation_interval/1000.0,
+ netcosm_world_simulation_interval/1000.0);
+ ev_timer_start(EV_DEFAULT_ sim_timer);
+ }
}
/**
@@ -208,8 +236,8 @@ bool world_load(const char *fname, const struct roomdata_t *data, size_t data_sz
world_name = read_string(fd);
if(strcmp(name, world_name))
{
- free(world_name);
debugf("Incompatible world state (%s %s).\n", name, world_name);
+ free(world_name);
return false;
}
@@ -277,6 +305,9 @@ bool world_load(const char *fname, const struct roomdata_t *data, size_t data_sz
}
close(fd);
+
+ start_sim_callback();
+
return true;
}
@@ -385,6 +416,8 @@ void world_init(const struct roomdata_t *data, size_t sz, const char *name)
}
hash_free(dir_map);
+
+ start_sim_callback();
}
static void *verb_map = NULL;
diff --git a/src/world.h b/src/world.h
index 5dcd85d..4ce253e 100644
--- a/src/world.h
+++ b/src/world.h
@@ -41,10 +41,15 @@ extern size_t netcosm_obj_classes_sz;
extern const struct roomdata_t *netcosm_world;
extern size_t netcosm_world_sz;
+/* simulation callback */
+extern void (*netcosm_world_simulation_cb)(void);
+extern unsigned netcosm_world_simulation_interval;
+
extern const char *netcosm_world_name;
#endif
-/*** loads the world into RAM for the first time, resets the game state ***/
+/* loads the world into RAM for the first time, resets the game
+ * state */
void world_init(const struct roomdata_t *data, size_t sz, const char *name);
void world_save(const char *fname);