🐳 chore(工具): 更新 lua ecs

develop
xiaojin 5 years ago
parent 8be2062078
commit 1a6a4951cd

@ -448,6 +448,7 @@ static int grid_add_obj(World *w, Grid *grid, int id, int x, int y, int is_maker
hashtbl_insert(grid->watchers, obj->id, obj);
hashtbl_upsert(w->watcher_grids, grid->idx, NULL);
}
return 0;
}
static int grid_del_obj(World *w, Grid *grid, int id)
@ -497,6 +498,7 @@ static int grid_del_obj(World *w, Grid *grid, int id)
hashtbl_remove(w->watcher_grids, grid->idx);
}
}
return 0;
}
static void grid_update_obj(Grid *grid, int id)

@ -112,7 +112,7 @@ int hashtbl_insert(HashTable *tbl, uint64_t key, void *value)
{
if (p->key == key)
{
printf("table_insert fail, key<%lu>, already exist\n", key);
printf("table_insert fail, key<%llu>, already exist\n", key);
assert(0);
return 1;
}

@ -1549,12 +1549,65 @@ lremove(lua_State *L) {
static int
lsortkey(lua_State *L) {
struct entity_world *w = getW(L);
int oid = luaL_checkinteger(L, 2);
int cid = luaL_checkinteger(L, 3);
int oid = check_cid(L, w, 2);
int cid = check_cid(L, w, 3);
entity_sort_key_(w, oid, cid, L, 1);
return 0;
}
static int
lbsearch(lua_State *L) {
struct entity_world *w = getW(L);
int sorted_id = check_cid(L, w, 2);
int value_id = check_cid(L, w, 3);
int value = luaL_checkinteger(L, 4);
struct component_pool *c = &w->c[sorted_id];
int begin = 0, end = c->n;
if (sorted_id == value_id) {
while (begin < end) {
int mid = (begin + end)/2;
int * v = entity_iter_(w, value_id, mid);
if (*v == value) {
// found
lua_createtable(L, 1, 0);
lua_pushinteger(L, mid);
lua_seti(L, -2, 1);
return 1;
}
if (*v < value) {
begin = mid + 1;
} else {
end = mid;
}
}
} else {
while (begin < end) {
int mid = (begin + end)/2;
int index = entity_sibling_index_(w, sorted_id, mid, value_id);
if (index == 0) {
return luaL_error(L, "Invalid value component");
}
int * v = entity_iter_(w, value_id, index - 1);
if (*v == value) {
// found
lua_createtable(L, 1, 0);
lua_pushinteger(L, index);
lua_seti(L, -2, 1);
return 1;
}
if (*v < value) {
begin = mid + 1;
} else {
end = mid;
}
}
}
return 0;
}
static int
lobject(lua_State *L) {
struct group_iter *iter = luaL_checkudata(L, 1, "ENTITY_GROUPITER");
@ -1654,6 +1707,7 @@ luaopen_ecs_core(lua_State *L) {
{ "_object", lobject },
{ "_sync", lsync },
{ "_release", lrelease },
{ "_bsearch", lbsearch },
{ NULL, NULL },
};
luaL_setfuncs(L,l,0);

@ -49,7 +49,7 @@ local function cache_world(obj, k)
id = tc.id,
type = tc.type,
}
local n = #tc
-- local n = #tc
for i = 1, #tc do
a[i] = tc[i]
end
@ -155,8 +155,8 @@ do -- newtype
local function parse(s)
-- s is "name:typename"
local name, typename = s:match "^([%w_]+):(%w+)$"
local typeid = assert(typeid[typename])
return {typeid, name}
local tid = assert(typeid[typename])
return {tid, name}
end
local function align(c, field)
@ -222,17 +222,17 @@ do -- newtype
end
end
local function dump(obj)
for e, v in pairs(obj) do
if type(v) == "table" then
for k, v in pairs(v) do
print(e, k, v)
end
else
print(e, v)
end
end
end
-- local function dump(obj)
-- for e,v in pairs(obj) do
-- if type(v) == "table" then
-- for k,v in pairs(v) do
-- print(e,k,v)
-- end
-- else
-- print(e,v)
-- end
-- end
-- end
function M:new(obj)
-- dump(obj)
@ -312,7 +312,7 @@ function M:sort(sorted, name)
local ctx = context[self]
local typenames = ctx.typenames
local t = assert(typenames[name])
assert(t.type == typeid.int or (#t == 1 and t[1][1] == typeid.float))
assert(t.type == typeid.int or (#t == 1 and t[1][1] == typeid.int))
local stype = typenames[sorted]
if stype == nil then
local id = ctx.id + 1
@ -332,6 +332,13 @@ function M:sort(sorted, name)
self:_sortkey(stype.id, t.id)
end
function M:bsearch(sorted, name, value)
local typenames = context[self].typenames
local sorted_id = typenames[sorted].id
local value_id = typenames[name].id
return self:_bsearch(sorted_id, value_id, value)
end
do
local _object = M._object
function M:object(name, refid, v)

Loading…
Cancel
Save