Info print from lua and new hash function in hashtable

This commit is contained in:
Linus_Probert 2018-02-01 09:55:12 +01:00
parent 9550840a7a
commit 0564f6c3a2
3 changed files with 17 additions and 9 deletions

View File

@ -42,7 +42,7 @@ local function generate_path ()
local cx, cy = 1, 1 local cx, cy = 1, 1
local seed = time(); local seed = time();
print("[**] Map generation seed: " .. seed) info("Map generation seed: " .. seed)
randomseed(seed) randomseed(seed)
local direction = 0 local direction = 0
local lastDirection = 0 local lastDirection = 0

View File

@ -26,20 +26,17 @@ ht_create(unsigned int size)
return table; return table;
} }
/* D. J. Bernstein hash function */
static unsigned int static unsigned int
hash(Hashtable *table, const char *key) hash(Hashtable *table, const char *key)
{ {
unsigned long int hashval = 0; unsigned int hash = 5381;
unsigned int i = 0;
// TODO(Linus): This isn't very good, while (*key) {
// bad distribution on similar strings hash = 33 * hash ^ (unsigned char) *key++;
while (hashval < ULONG_MAX && i < strlen(key)) {
hashval += key[i++];
hashval = hashval << 8;
} }
return hashval % table->size; return hash % table->size;
} }
static Entry* static Entry*

View File

@ -27,6 +27,14 @@ int l_create_map(lua_State *L)
return 1; return 1;
} }
static int
l_print_info(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
info(str);
return 0;
}
static static
Map* luaL_checkmap(lua_State *L, int index) 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_pushcfunction(L, l_create_map);
lua_setglobal(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_pushcfunction(L, l_add_tile);
lua_setglobal(L, "add_tile"); lua_setglobal(L, "add_tile");