From 80789508b9655d25629223b9dcc84b4cfb77ce45 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 29 Jun 2020 15:29:03 -0400 Subject: Updates for mdoc Also more tests --- src/shared/lua_api/load_common.cpp | 51 ++++++++++++++++++++++++++++++++ src/shared/lua_api/load_net.cpp | 28 +++++++++--------- src/shared/lua_api/phys/bcollider.cpp | 7 +++++ src/shared/lua_api/phys/bghostobject.cpp | 15 ++++++---- src/shared/lua_api/phys/bphysbox.cpp | 2 +- src/shared/lua_api/phys/bphysgeneric.cpp | 15 ++++++---- src/shared/lua_api/phys/bphysmodel.cpp | 19 ++++++++---- 7 files changed, 106 insertions(+), 31 deletions(-) (limited to 'src/shared/lua_api') diff --git a/src/shared/lua_api/load_common.cpp b/src/shared/lua_api/load_common.cpp index 1eca598..188cd30 100644 --- a/src/shared/lua_api/load_common.cpp +++ b/src/shared/lua_api/load_common.cpp @@ -5,6 +5,13 @@ extern "C" { #include #include } +//Open group +#include //for io.dir("file/path") +#include //required before sys/stat.h on windows +#include //for io.time("path/to.file") +#include +#include //For better errors + using namespace std::chrono; //Gets the time @@ -16,6 +23,45 @@ int get_time(lua_State* L){ return 1; } +char *epath; +size_t epathlen; +/*Add an io.dir(path) function, which lists all the files in (path)*/ +int dirpath(lua_State *L){ + if(!lua_isstring(L,-1)){ + lua_pushstring(L,"io.dir() requires a string as argument #1"); + lua_error(L); + } + size_t pathstrlen; + const char *pathstr = lua_tolstring(L,-1,&pathstrlen); + char tpathstr[pathstrlen + epathlen + 1 + 1]; //+1 for null, +1 for / + memcpy(tpathstr,epath,epathlen); + tpathstr[epathlen] = '/'; + memcpy(tpathstr+epathlen+1,pathstr,pathstrlen); + tpathstr[pathstrlen + epathlen + 1] = '\0'; + lua_pop(L,1); + DIR *dir; + struct dirent *ent; + dir = opendir(tpathstr); + if(dir == NULL){ + perror("Cannot open"); + lua_pushstring(L,"Failed to open directory: "); + lua_pushstring(L,tpathstr); + lua_concat(L,2); + lua_error(L); + } + int i = 1; + ent = readdir(dir); + lua_newtable(L); + while( (ent = readdir(dir)) != NULL){ + lua_pushinteger(L,i); + lua_pushstring(L,ent->d_name); + lua_settable(L,-3); + i++; + } + closedir(dir); + return 1; +} + void loadCommonLibs(lua_State* L){ lua_getglobal(L,"GAME"); lua_pushcfunction(L,make_crashy); @@ -23,6 +69,11 @@ void loadCommonLibs(lua_State* L){ lua_pop(L,1); lua_pushcfunction(L,get_time); lua_setglobal(L,"get_time"); + + lua_getglobal(L,"io"); + lua_pushcfunction(L,dirpath); + lua_setfield(L,-2,"dir"); + lua_pop(L,1); } void gameloop_common(lua_State* L){ diff --git a/src/shared/lua_api/load_net.cpp b/src/shared/lua_api/load_net.cpp index b377cc5..bff55cf 100644 --- a/src/shared/lua_api/load_net.cpp +++ b/src/shared/lua_api/load_net.cpp @@ -1,11 +1,11 @@ -/*** +/* +TODO:Fix documentation or find a better home for this example code. The net library Exposes various structs, constants, and functions for passing messages A list of protocols that can be used when creating sockets. The number values below are only for refrence. You should use net.PAIR, net.BUS, ect. -@module net @usage --Server local s = net.newsocket(net.REP) @@ -67,7 +67,7 @@ extern "C" { /*** 1 to 1 protocol. -@field PAIR 1 +@field net.PAIR */ #define PAIR 1 @@ -75,63 +75,63 @@ extern "C" { Many to many protocol. When this socket sends messages, it does not receive a copy of the message it just sent. -@field BUS 2 +@field net.BUS */ #define BUS 2 /*** Publish protocol. The first half of the pub/sub protocol. -@field PUB 3 +@field net.PUB */ #define PUB 3 /*** Subscribe protocol The second half of the pub/sub protocol. -@field SUB 4 +@field net.SUB */ #define SUB 4 /*** Pull protocol. The first half of the push/pull protocol. -@field PULL 5 +@field net.PULL */ #define PULL 5 /*** Push protocol. The second half of the push/pull protocol. -@field PUSH 6 +@field net.PUSH */ #define PUSH 6 /*** Request protocol. The first half of the request/reply protocol. -@field REQ 7 +@field net.REQ */ #define REQ 7 /*** Reply protocol. The second half of the request/reply protocol. -@field REP 8 +@field net.REP */ #define REP 8 /*** Respond protocol. The second half of the survey/respond protocol. -@field RESPOND 9 +@field net.RESPOND */ #define RESPOND 9 /*** Survey protocol. The first half of the survey/respond protocol. -@field SURVEY 10 +@field net.SURVEY */ #define SURVEY 10 @@ -597,8 +597,8 @@ int netclose(lua_State* L){//{socket} } /*** -@field fd -@table net.socket +The opaque socket descriptor for this socket. +@field socket.fd The descriptor @domain shared */ diff --git a/src/shared/lua_api/phys/bcollider.cpp b/src/shared/lua_api/phys/bcollider.cpp index 91c34fd..caefd3e 100644 --- a/src/shared/lua_api/phys/bcollider.cpp +++ b/src/shared/lua_api/phys/bcollider.cpp @@ -1,3 +1,10 @@ +/* +A wrapper for the btCollisionObject class. +Most of the methods that bullet exposes for the btCollisionObject are wrapped +here, any subclass of btCollisionObject should register methods defined +here in it's metatable's __index. +*/ + extern "C" { #include #include diff --git a/src/shared/lua_api/phys/bghostobject.cpp b/src/shared/lua_api/phys/bghostobject.cpp index 63f790c..5657b5f 100644 --- a/src/shared/lua_api/phys/bghostobject.cpp +++ b/src/shared/lua_api/phys/bghostobject.cpp @@ -109,7 +109,7 @@ int bghostconvexsweep(lua_State *L){ btVector3 hw, hn; hw = cb->m_hitPointWorld; hn = cb->m_hitNormalWorld; - btCollisionObject *co = cb->m_hitCollisionObject; + const btCollisionObject *co = cb->m_hitCollisionObject; lua_newtable(L);//{} lua_pushboolean(L,cb->hasHit() ? 1 : 0); @@ -118,12 +118,17 @@ int bghostconvexsweep(lua_State *L){ lua_setfield(L,-2,"pos"); pushvector3d(L,hn.x(),hn.y(),hn.z()); lua_setfield(L,-2,"normal"); + lua_getglobal(L,"phys");//{},{phys} lua_getfield(L,-1,"colliders");//{},{phys},{phys.colliders} - lua_pushlightuserdata(L,co);//{},{phys},{phys.colliders},ud_collisionobject - lua_gettable(L,-2);//{},{phys},{phys.colliders},ud_collisionobject,{rb} or nil - lua_setfield(L,-5,"what");//{},{phys},{phys.colliders},ud_collisionobject - lua_pop(L,3);//{} + lua_pushlightuserdata(L,(void*)co);//{},{phys},{phys.colliders},ud_collisionobject + lua_gettable(L,-2);//{},{phys},{phys.colliders},{rb} or nil + if(lua_isnil(L,-1)){ + lua_pop(L,3);//{} + }else{ + lua_setfield(L,-4,"what");//{},{phys},{phys.colliders} + lua_pop(L,2);//{} + } delete cb; return 1; diff --git a/src/shared/lua_api/phys/bphysbox.cpp b/src/shared/lua_api/phys/bphysbox.cpp index 569f3f9..66f112d 100644 --- a/src/shared/lua_api/phys/bphysbox.cpp +++ b/src/shared/lua_api/phys/bphysbox.cpp @@ -107,7 +107,7 @@ int newbphysbox(lua_State* L){ //Set it's metatable luaL_getmetatable(L, "phys.physbox");//{},{phys.physbox} lua_setmetatable(L, -2);//{} - + return 1; } diff --git a/src/shared/lua_api/phys/bphysgeneric.cpp b/src/shared/lua_api/phys/bphysgeneric.cpp index 4500ce0..eed9112 100644 --- a/src/shared/lua_api/phys/bphysgeneric.cpp +++ b/src/shared/lua_api/phys/bphysgeneric.cpp @@ -8,11 +8,6 @@ extern "C" { #include #include -/*** -@module phys -*/ - - /*Physics things from lua have the form of: { type = "rigidbody" @@ -243,6 +238,7 @@ int applyimpulse(lua_State *L){ popvector3d(L,&x,&y,&z); btRigidBody *r = popRigidBody(L); r->applyImpulse(btVector3(x,y,z),btVector3(ox,oy,oz)); + r->activate(); return 0; } @@ -265,6 +261,14 @@ int setrotation(lua_State *L){ return 0; } +//rigidbody:getmass() :: number +int getmass(lua_State *L){ + btRigidBody *r = popRigidBody(L); + double m = r->getMass(); + lua_pushnumber(L,m); + return 1; +} + /* @@ -310,6 +314,7 @@ extern const luaL_reg brigidbody_m[] = { {"setangfactor", setangfactor}, {"setflags", setflags}, {"setrotation", setrotation}, + {"getmass", getmass}, //{"testcontact", testcontact}, {NULL, NULL} }; diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp index 0c8e3bf..375d68b 100644 --- a/src/shared/lua_api/phys/bphysmodel.cpp +++ b/src/shared/lua_api/phys/bphysmodel.cpp @@ -24,6 +24,10 @@ extern "C" { extern btDiscreteDynamicsWorld* World; extern std::list Objects; +//btRigidBody* load_obj(tinyobj_attrib_t attrib, tinyobj_shape_t *shapes, size_t num){ + //btTriangleMesh *trimesh = new btTriangleMesh(); +//} + //TODO: This will break in the future, see github.com/syoyo/tinyobjloader-c/issues/16 //"physicfile",mass[,position][,lookat] :: ud_rigidbody void makebphysmodel(lua_State *L){ @@ -67,6 +71,9 @@ void makebphysmodel(lua_State *L){ if(err != TINYOBJ_SUCCESS){ printf("Tinyobj failed to load model:%s\n",ppath); } + //for(size_t s = 0; s < meshcount; s++){ + //btRigidBody *rb = load_obj(attrib,shapes,s); + //} //u32 meshcount = pmesh->getMeshBufferCount(); //__mingw_printf("attrib.num_vertices: %u\n",attrib.num_vertices); //__mingw_printf("attrib.num_faces: %u\n",attrib.num_faces); @@ -81,7 +88,7 @@ void makebphysmodel(lua_State *L){ float v2 = vs[1]; float v3 = vs[2]; vertexes[i] = btVector3(v1,v2,v3); - printf("Adding vertex %lld at (%f,%f,%f)\n",i,v1,v2,v3); + //printf("Adding vertex %lld at (%f,%f,%f)\n",i,v1,v2,v3); } //printf("Finished finding or adding vertexes\n"); for(size_t i = 0; i < attrib.num_faces - 1; i+= 3){ //0 - y to num_faces - 1 @@ -94,14 +101,14 @@ void makebphysmodel(lua_State *L){ v2 = vertexes[i2.v_idx]; v3 = vertexes[i3.v_idx]; trimesh->addTriangle(vertexes[i1.v_idx],vertexes[i2.v_idx],vertexes[i3.v_idx],false);//Some triangles are "the wrong way round", - printf("Adding triangle:(%d,%d,%d)\n",i1.v_idx,i2.v_idx,i3.v_idx); - //trimesh->addTriangle(vertexes[i3.v_idx],vertexes[i2.v_idx],vertexes[i1.v_idx],true);//double-side all triangles + //printf("Adding triangle:(%d,%d,%d)\n",i1.v_idx,i2.v_idx,i3.v_idx); + trimesh->addTriangle(vertexes[i3.v_idx],vertexes[i2.v_idx],vertexes[i1.v_idx],true);//double-side all triangles } //printf("Finished adding triangle indicies\n"); //printf("Done building trimesh\n"); - //btGImpactShapeInterface *shape = new btGImpactMeshShape(trimesh); - btConvexTriangleMeshShape *shape = new btConvexTriangleMeshShape(trimesh); - //shape->updateBound(); + //btConvexTriangleMeshShape *shape = new btConvexTriangleMeshShape(trimesh); + btGImpactShapeInterface *shape = new btGImpactMeshShape(trimesh); + shape->updateBound(); //btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true); btTransform tr; tr.setIdentity(); -- cgit v1.2.3-70-g09d2