From 0564f6c3a2b877683080586b57830520a7de516d Mon Sep 17 00:00:00 2001 From: Linus_Probert Date: Thu, 1 Feb 2018 09:55:12 +0100 Subject: [PATCH] Info print from lua and new hash function in hashtable --- data/mapgen.lua | 2 +- src/hashtable.c | 13 +++++-------- src/map_lua.c | 11 +++++++++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/data/mapgen.lua b/data/mapgen.lua index f8bd401..7ba72bd 100644 --- a/data/mapgen.lua +++ b/data/mapgen.lua @@ -42,7 +42,7 @@ local function generate_path () local cx, cy = 1, 1 local seed = time(); - print("[**] Map generation seed: " .. seed) + info("Map generation seed: " .. seed) randomseed(seed) local direction = 0 local lastDirection = 0 diff --git a/src/hashtable.c b/src/hashtable.c index 2b660f7..fd76cb2 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -26,20 +26,17 @@ ht_create(unsigned int size) return table; } +/* D. J. Bernstein hash function */ static unsigned int hash(Hashtable *table, const char *key) { - unsigned long int hashval = 0; - unsigned int i = 0; + unsigned int hash = 5381; - // TODO(Linus): This isn't very good, - // bad distribution on similar strings - while (hashval < ULONG_MAX && i < strlen(key)) { - hashval += key[i++]; - hashval = hashval << 8; + while (*key) { + hash = 33 * hash ^ (unsigned char) *key++; } - return hashval % table->size; + return hash % table->size; } static Entry* diff --git a/src/map_lua.c b/src/map_lua.c index 86d569b..760683c 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -27,6 +27,14 @@ int l_create_map(lua_State *L) return 1; } +static int +l_print_info(lua_State *L) +{ + const char *str = luaL_checkstring(L, 1); + info(str); + return 0; +} + static Map* luaL_checkmap(lua_State *L, int index) { @@ -238,6 +246,9 @@ Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer) lua_pushcfunction(L, l_create_map); lua_setglobal(L, "create_map"); + lua_pushcfunction(L, l_print_info); + lua_setglobal(L, "info"); + lua_pushcfunction(L, l_add_tile); lua_setglobal(L, "add_tile");