Add lua object references
Whenever a lua script references a core struct obj, increment a counter in the obj struct. Core code will not free the obj, if there are any lua references pointing to it, just makes it free-floating. When lua script ends, the lua gc will free the free-floating objects. Also exposes u.inventory to lua. Breaks save and bones compat.
This commit is contained in:
16
src/nhlobj.c
16
src/nhlobj.c
@@ -43,8 +43,10 @@ lua_State *L;
|
||||
struct _lua_obj *lo = l_obj_check(L, 1);
|
||||
|
||||
if (lo && lo->obj) {
|
||||
/* free-floating objects are deallocated */
|
||||
if (lo->obj->where == OBJ_FREE) {
|
||||
if (lo->obj->lua_ref_cnt > 0)
|
||||
lo->obj->lua_ref_cnt--;
|
||||
/* free-floating objects with no other refs are deallocated. */
|
||||
if (lo->obj->where == OBJ_FREE && !lo->obj->lua_ref_cnt) {
|
||||
if (Has_contents(lo->obj)) {
|
||||
struct obj *otmp;
|
||||
while ((otmp = lo->obj->cobj) != 0) {
|
||||
@@ -71,10 +73,20 @@ struct obj *otmp;
|
||||
|
||||
lo->state = 0;
|
||||
lo->obj = otmp;
|
||||
if (otmp)
|
||||
otmp->lua_ref_cnt++;
|
||||
|
||||
return lo;
|
||||
}
|
||||
|
||||
void
|
||||
nhl_push_obj(L, otmp)
|
||||
lua_State *L;
|
||||
struct obj *otmp;
|
||||
{
|
||||
(void) l_obj_push(L, otmp);
|
||||
}
|
||||
|
||||
/* local o = obj.new("large chest");
|
||||
local cobj = o:contents(); */
|
||||
static int
|
||||
|
||||
Reference in New Issue
Block a user