From 0629b042ad8f4a3d1ed6ee292478cded3e1a3932 Mon Sep 17 00:00:00 2001 From: mid <> Date: Sat, 10 May 2025 19:18:09 +0300 Subject: [PATCH] Many API additions 2 --- src/luaapi.c | 101 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 23 deletions(-) diff --git a/src/luaapi.c b/src/luaapi.c index 6aeb16e..46f8abe 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -15,6 +15,8 @@ #include"k3menu.h" #include #include"net_hi.h" +#include"k3particles.h" +#include /* * This is by far the least clean or well-maintained source in the @@ -170,6 +172,24 @@ error: return 0; } +struct mixitem { + void *thing; +#define MIXITEM_SOURCE 0 +#define MIXITEM_SOUND 1 +#define MIXITEM_QUEUE 2 +#define MIXITEM_POWER 3 + int type; +}; + +struct Animator { + struct k3Mdl *mdl; + struct k3Animator animator; + struct k3AnimationBone *bones; + + bool playing; + double playStartTime; +}; + static int game_addentity(lua_State *L) { lua_Integer li; int i; @@ -666,15 +686,6 @@ static int game_water_disturb(lua_State *L) { return 0; } -struct mixitem { - void *thing; -#define MIXITEM_SOURCE 0 -#define MIXITEM_SOUND 1 -#define MIXITEM_QUEUE 2 -#define MIXITEM_POWER 3 - int type; -}; - static int dagame_mixsound(lua_State *L) { struct mixitem *i = lua_touserdata(L, 1); @@ -866,13 +877,15 @@ static int game_ref(lua_State *L) { } static int game_batch(lua_State *L) { - void **r = lua_touserdata(L, 1); + void *r = lua_touserdata(L, 1); struct k3Mdl *mdl; if(luaL_testudata(L, 1, "k3water")) { - mdl = ((struct k3Water*) *r)->mdl; + mdl = (*(struct k3Water**) r)->mdl; + } else if(luaL_testudata(L, 1, "k3cpuparticles")) { + mdl = (*(struct k3CPUQuadParticles**) r)->mdl; } else if(luaL_testudata(L, 1, "k3mdl")) { - mdl = *r; + mdl = *(struct k3Mdl**) r; } else return 0; lua_rawgeti(L, 2, 1); @@ -884,7 +897,28 @@ static int game_batch(lua_State *L) { lua_pop(L, 3); - if(lua_gettop(L) == 3) { + struct k3AnimationBone *bones = NULL; + + if(lua_gettop(L) >= 4) { + struct Animator *anim = lua_touserdata(L, 4); + + if(anim->playing) { + k3AnimatorSet(&anim->animator, glfwGetTime() - anim->playStartTime); + } + + bones = anim->bones; + + size_t boneCount = k3MdlGetBoneCount(anim->mdl); + + for(size_t m = 0; m < boneCount; m++) { + vec3 scaleignore; + mat4 rot; + glm_decompose(anim->animator.inter[m], bones[m].translation, rot, scaleignore); + glm_mat4_quat(anim->animator.inter[m], bones[m].rotation); + } + } + + if(lua_gettop(L) >= 3) { mat4 anchor; for(int i = 0; i < 16; i++) { lua_rawgeti(L, 3, i + 1); @@ -895,7 +929,7 @@ static int game_batch(lua_State *L) { glm_mat4_mul(anchor, m, m); } - k3Batch(mdl, m, NULL); + k3Batch(mdl, m, bones); return 0; } @@ -986,6 +1020,22 @@ static int game_submissive_send(lua_State *L) { return 0; } +static int dagame_cphysics_add_force(lua_State *L) { + struct CPhysics *c = *(struct CPhysics**) lua_touserdata(L, 1); + + dBodyID bid = dGeomGetBody(c->geom); + + if(!bid) { + lua_pushboolean(L, false); + return 1; + } + + dBodyAddForce(bid, lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4)); + + lua_pushboolean(L, true); + return 1; +} + static int game_cphysics_get(lua_State *L) { struct CPhysics *c = *(struct CPhysics**) lua_touserdata(L, 1); @@ -1052,7 +1102,7 @@ static int game_cphysics_get(lua_State *L) { lua_pushnumber(L, c->speed); return 1; } else if(!strcmp(k, "add_force")) { - //lua_pushcfunction(L, ); + lua_pushcfunction(L, dagame_cphysics_add_force); return 1; } @@ -1083,7 +1133,9 @@ static int game_cphysics_set(lua_State *L) { lua_rawgeti(L, 3, 3); lua_rawgeti(L, 3, 4); - dGeomSetQuaternion(c->geom, (dQuaternion) {lua_tonumber(L, -1), lua_tonumber(L, -4), lua_tonumber(L, -3), lua_tonumber(L, -2)}); + dQuaternion q = {lua_tonumber(L, -1), lua_tonumber(L, -4), lua_tonumber(L, -3), lua_tonumber(L, -2)}; + + dGeomSetQuaternion(c->geom, q); lua_pop(L, 4); } else if(!strcmp(k, "vel")) { @@ -1280,7 +1332,11 @@ static int game_get(lua_State *L) { } else if(!strcmp(i, "authority")) { lua_pushboolean(L, Game.isAuthority); } else if(!strcmp(i, "spectated")) { - lua_pushinteger(L, Game.spectated); + if(Game.spectated == ENT_ID_INVALID) { + lua_pushnil(L); + } else { + lua_pushinteger(L, Game.spectated); + } } else if(!strcmp(i, "fov")) { lua_pushnumber(L, LuaapiFov); } else if(!strcmp(i, "rate")) { @@ -1491,7 +1547,6 @@ static int game_load(lua_State *L) { static int dagame_cleanup(lua_State *L) { luaapi_cleanup(); - LuaapiCamFocus = false; return 0; } @@ -1597,7 +1652,7 @@ static int dagame_mdl(lua_State *L) { vertexCount = lua_tointeger(L, -1) / 3; lua_pop(L, 1); - positions = malloc(sizeof(*positions) * vertexCount); + positions = calloc(vertexCount, sizeof(*positions)); for(size_t i = 0; i < vertexCount; i++) { lua_geti(L, -1, i * 3 + 1); @@ -1615,7 +1670,7 @@ static int dagame_mdl(lua_State *L) { indexCount = lua_tointeger(L, -1); lua_pop(L, 1); - indices = malloc(sizeof(*indices) * indexCount); + indices = calloc(indexCount, sizeof(*indices)); for(size_t i = 0; i < indexCount; i++) { lua_geti(L, -1, i + 1); @@ -1637,7 +1692,7 @@ static int dagame_mdl(lua_State *L) { return 0; } - normals = malloc(sizeof(*normals) * 3 * vertexCount); + normals = calloc(vertexCount, sizeof(*normals) * 3); for(size_t i = 0; i < vertexCount; i++) { lua_geti(L, -1, i * 3 + 1); @@ -1660,7 +1715,7 @@ static int dagame_mdl(lua_State *L) { return 0; } - uvs = malloc(sizeof(*uvs) * vertexCount); + uvs = calloc(vertexCount, sizeof(*uvs)); for(size_t i = 0; i < vertexCount; i++) { lua_geti(L, -1, i * 2 + 1); @@ -1858,7 +1913,7 @@ static int dagame_draw(lua_State *L) { float b = lua_type(L, -2) == LUA_TNUMBER ? lua_tonumber(L, -2) : 1; float a = lua_type(L, -1) == LUA_TNUMBER ? lua_tonumber(L, -1) : 1; - k3BatchAdd(tex, (struct k3RectF) {.x = sx, .y = sy, .w = sw, .h = sh}, (struct k3RectF) {.x = x, .y = y, .w = w, .h = h}, 0, (vec4) {r, g, b, a}); + k3BatchAdd(tex, (struct k3RectF) {.x = sx, .y = sy, .w = sw, .h = sh}, (struct k3RectF) {.x = x, .y = y, .w = w, .h = h}, 0, (vec4) {r, g, b, a}, 0); lua_pop(L, 13); } else {