From 0d2de2ba9c616862d7881f089382db772d034f89 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 27 Oct 2019 17:25:16 -0400 Subject: Various updates --- src/client/callbackhandeler.cpp | 24 ++++++++--- src/client/initdevice.cpp | 35 ++++++++-------- src/client/lua_api/gui/iguibutton.cpp | 10 ++--- src/client/lua_api/gui/iguifiledialog.cpp | 16 +++++--- src/client/lua_api/gui/iguiimage.cpp | 14 +++++++ src/client/lua_api/load_cphys.cpp | 13 +++++- src/client/lua_api/load_video.cpp | 20 ++++++++- src/client/lua_api/phys/cbcharactercontroller.cpp | 2 - src/client/lua_api/phys/cbphysbox.cpp | 13 +++--- src/client/lua_api/phys/cbphysmodel.cpp | 12 ++++-- src/client/lua_api/phys/cbphysmodel.hpp | 2 +- src/client/lua_api/scene/igeneric.cpp | 18 +++++++++ src/client/lua_api/scene/ilight.cpp | 23 +++++++++-- src/client/lua_api/video/draw.cpp | 29 ++++++++++++++ src/client/lua_api/video/draw.hpp | 8 ++++ src/client/lua_api/video/smaterial.cpp | 14 +++++++ src/client/main.cpp | 49 ++++++++++++++++++----- src/server/main.cpp | 21 +++++++++- src/shared/lua_api/common.cpp | 2 + src/shared/lua_api/load_common.cpp | 26 +++++++++++- src/shared/lua_api/load_common.hpp | 11 +---- src/shared/lua_api/load_net.cpp | 49 ++++++++++++----------- src/shared/lua_api/load_phys.cpp | 1 + src/shared/lua_api/load_scene.cpp | 1 - src/shared/lua_api/phys/bphysbuffer.cpp | 10 ++--- src/shared/lua_api/phys/bphysmodel.cpp | 19 ++++++--- src/shared/phys/physcommon.cpp | 49 ++++++++++++++++++++++- 27 files changed, 381 insertions(+), 110 deletions(-) create mode 100644 src/client/lua_api/video/draw.cpp create mode 100644 src/client/lua_api/video/draw.hpp (limited to 'src') diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp index 0f1f87f..d522e84 100644 --- a/src/client/callbackhandeler.cpp +++ b/src/client/callbackhandeler.cpp @@ -1,6 +1,7 @@ #include #include #include +#include extern "C" { #include #include @@ -38,8 +39,10 @@ GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d){ //device = d; } + int callMouse(lua_State* L, const char* funcname, double x, double y, double event){ int consume = 0; + assert(lua_gettop(L) == 0); lua_getglobal(L,"GAME");//GAME pusherrorfunc(L);//{GAME},errfun lua_getfield(L,-2,funcname);//{GAME},errfunc,funcname? @@ -53,18 +56,22 @@ int callMouse(lua_State* L, const char* funcname, double x, double y, double eve }else{ if(!lua_isnil(L,-1)){ consume = lua_toboolean(L,-1); - lua_pop(L,-1); } } - lua_pop(L,2); + lua_pop(L,3); + assert(lua_gettop(L) == 0); }else{ //{GAME},errfunc,nil lua_pop(L,3);// + assert(lua_gettop(L) == 0); } + assert(lua_gettop(L) == 0); return consume; } bool GlobalEventReceiver::OnEvent(const SEvent& e){ + //printf("Onevent triggered when top was %d\n", lua_gettop(L)); + assert(lua_gettop(L) == 0); //lua_State* L = this->L; EEVENT_TYPE type = e.EventType; SEvent::SMouseInput se = e.MouseInput; @@ -95,7 +102,7 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ case EGET_LISTBOX_SELECTED_AGAIN: fieldname = "onSame"; break; case EGET_FILE_SELECTED: fieldname = "onFileSelect"; break; case EGET_DIRECTORY_SELECTED: fieldname = "onDirectorySelect"; break; - case EGET_FILE_CHOOSE_DIALOG_CANCELLED: fieldname = "onFileChooserCancel"; break; + case EGET_FILE_CHOOSE_DIALOG_CANCELLED: fieldname = "onCanceled"; break; case EGET_MESSAGEBOX_YES: fieldname = "onYes"; break; case EGET_MESSAGEBOX_NO: fieldname = "onNo"; break; case EGET_MESSAGEBOX_OK: fieldname = "onOk"; break; @@ -122,6 +129,7 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ if(lua_isnil(L,-1)){ printf("Element did not have a function %s, returning\n",fieldname); lua_pop(L,3);// + assert(lua_gettop(L) == 0); return false; } lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//{guielement},errfunc(),func(),{guielement} @@ -130,9 +138,12 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ if(!lua_isnil(L,-1)){ printf("Got an argument back!\n"); int ans = lua_toboolean(L,-1); + lua_pop(L,3); + assert(lua_gettop(L) == 0); return ans; } lua_pop(L,3);// + assert(lua_gettop(L) == 0); return false; } break; @@ -199,6 +210,7 @@ Detects any key presses from the game. case EET_KEY_INPUT_EVENT:{ //printf("Got input event\n"); SEvent::SKeyInput se = e.KeyInput; + assert(lua_gettop(L) == 0); lua_getglobal(L,"GAME");//{} lua_getfield(L,-1,"onKeyDown");//{},()|nil if(!lua_isnil(L,-1)){ @@ -210,10 +222,9 @@ Detects any key presses from the game. lua_pushboolean(L,se.Control);//{GAME},GAME.onKeyDown(),errfunc,onKeyDown(),key_number,is_down,is_ctrl lua_pushboolean(L,se.Shift);//{GAME},GAME.onKeyDown(),errfunc,onKeyDown(),key_number,is_down,is_ctrl,is_shift lua_pcall(L,4,0,-6);//GAME,GAME.onKeyDown() - lua_pop(L,2); - }else{ - lua_pop(L,2); } + lua_pop(L,lua_gettop(L)); + assert(lua_gettop(L) == 0); return false; break; } @@ -222,5 +233,6 @@ Detects any key presses from the game. return false; } } + assert(lua_gettop(L) == 0); return true; } diff --git a/src/client/initdevice.cpp b/src/client/initdevice.cpp index 3720d49..40a1e30 100644 --- a/src/client/initdevice.cpp +++ b/src/client/initdevice.cpp @@ -144,7 +144,7 @@ void parseSetting(const char* settingname, lua_State* L, settings* set){ } void settingsFromTable(lua_State *L, SIrrlichtCreationParameters* p){ - lua_pushnil(L); + lua_pushnil(L);//nil settings* set = (settings*)malloc(sizeof(settings)); printf("Loading settings..."); while(lua_next(L,-2) != 0){ @@ -172,20 +172,21 @@ void settingsFromTable(lua_State *L, SIrrlichtCreationParameters* p){ } IrrlichtDevice* spawnIrrDevice(lua_State* L, char *path){ - //printf("Attempting to load settings...\n"); - char initname[] = "deviceinit.lua"; - size_t pathlen = strlen(initname) + strlen(path); - char filename[pathlen + 1]; - sprintf(filename,"%s/%s",path,initname); - int iErr = luaL_dofile(L,filename); - SIrrlichtCreationParameters p = SIrrlichtCreationParameters(); - if(iErr != 0){ - printf("Failed to open lua file:%s\n", filename); - } - settingsFromTable(L,&p); - IrrlichtDevice* dev = createDeviceEx(p); - if(!dev) - exit(1); - dev->setWindowCaption(L"Borkengin"); - return dev; + //printf("Attempting to load settings...\n"); + char initname[] = "deviceinit.lua"; + size_t pathlen = strlen(initname) + strlen(path); + char filename[pathlen + 1]; + sprintf(filename,"%s/%s",path,initname); + int iErr = luaL_dofile(L,filename);//ret + SIrrlichtCreationParameters p = SIrrlichtCreationParameters(); + if(iErr != 0){ + printf("Failed to open lua file:%s\n", filename); + } + settingsFromTable(L,&p); + IrrlichtDevice* dev = createDeviceEx(p); + if(!dev) + exit(1); + dev->setWindowCaption(L"Brokengine"); + lua_pop(L,1); + return dev; } diff --git a/src/client/lua_api/gui/iguibutton.cpp b/src/client/lua_api/gui/iguibutton.cpp index 42d2846..b8ff517 100644 --- a/src/client/lua_api/gui/iguibutton.cpp +++ b/src/client/lua_api/gui/iguibutton.cpp @@ -30,11 +30,11 @@ char lhashkey[20]; /*** Creates a new button. Buttons may have the following fields set for callbacks: -`.onClick(self)` -`.onFocus(self)` -`.onUnfocus(self)` -`.onHover(self)` -`.onLeave(self)` + .onClick(self) + .onFocus(self) + .onUnfocus(self) + .onHover(self) + .onLeave(self) @function newbutton() @tparam rect dimensions The rectangle to place the button at. If the box has a parent, it is offset from the upper-left of the parent element. diff --git a/src/client/lua_api/gui/iguifiledialog.cpp b/src/client/lua_api/gui/iguifiledialog.cpp index 6303230..d717673 100644 --- a/src/client/lua_api/gui/iguifiledialog.cpp +++ b/src/client/lua_api/gui/iguifiledialog.cpp @@ -28,7 +28,11 @@ extern IrrlichtDevice* device; /*** @function newfileopendialog() -Creates a new dialog to open a file +Creates a new dialog to open a file. +The file creation window may have the following fields set for callbacks: + .onDirectorySelect(self) + .onFileSelect(self) + .onCanceled(self) @tparam ?string title The rectangle to place the button at. If the box has a parent, it is offset from the upper-left of the parent element. @tparam ?string path The path to open the file dialog to @@ -66,10 +70,10 @@ static int newfileopendialog(lua_State* L){ mbstowcs(title,titlec,titlecslen); title[titlecslen] = L'\0'; } - + //printf("Got all arguments\n"); IGUIEnvironment *env = device->getGUIEnvironment(); IGUIFileOpenDialog *but = env->addFileOpenDialog(title,modal,parent,-1,false,path); - + //printf("Created file open dialog\n"); //printf("Added file open dialog\n"); lua_newtable(L);//{} lua_pushlightuserdata(L,but);//{},ud_iguibutton @@ -78,13 +82,13 @@ static int newfileopendialog(lua_State* L){ lua_setmetatable(L,-2);//{guielement} //printf("Created lua representation\n"); - setelementcallback(L,EGET_DIRECTORY_SELECTED,"onDirectory");// - setelementcallback(L,EGET_FILE_SELECTED,"onFileSelected"); + setelementcallback(L,EGET_DIRECTORY_SELECTED,"onDirectorySelect");// + setelementcallback(L,EGET_FILE_SELECTED,"onFileSelect"); setelementcallback(L,EGET_FILE_CHOOSE_DIALOG_CANCELLED,"onCanceled"); //printf("Finished registering callback\n"); free(title); - free(path); + //printf("Freed everything\n"); return 1; } diff --git a/src/client/lua_api/gui/iguiimage.cpp b/src/client/lua_api/gui/iguiimage.cpp index 1d0f4dc..267cbd2 100644 --- a/src/client/lua_api/gui/iguiimage.cpp +++ b/src/client/lua_api/gui/iguiimage.cpp @@ -21,6 +21,7 @@ extern "C" { @module gui */ using namespace irr; +using namespace video; using namespace gui; extern IrrlichtDevice* device; @@ -80,8 +81,21 @@ int setcolor(lua_State* L){ return 0; } +//setimage(self,itexture) +int setimage(lua_State *L){ + lua_getfield(L,-1,"texture");//{iguiimg},{itex} + ITexture* img = (ITexture*)lua_touserdata(L,-1);//{iguiimg},{itex},ud_itexture + lua_pop(L,2);//{iguiimg} + lua_getfield(L,-1,"guielement");//{iguiimg},ud_guiimg + IGUIImage *gimg = (IGUIImage*)lua_touserdata(L,-1);//{iguiimg},ud_guiimg + lua_pop(L,2);// + gimg->setImage(img); + return 0; +} + static const luaL_reg iguiimage_m[] = { {"setcolor", setcolor}, + {"setimage", setimage}, {0, 0}, }; diff --git a/src/client/lua_api/load_cphys.cpp b/src/client/lua_api/load_cphys.cpp index 92eaff0..2e2d18a 100644 --- a/src/client/lua_api/load_cphys.cpp +++ b/src/client/lua_api/load_cphys.cpp @@ -2,6 +2,7 @@ #include #include #include +#include extern "C" { #include #include @@ -31,8 +32,8 @@ extern btBroadphaseInterface *BroadPhase; int raytest(lua_State *L){ double fx,fy,fz; double tx,ty,tz; - popvector3d(L, &fx, &fy, &fz); popvector3d(L, &tx, &ty, &tz); + popvector3d(L, &fx, &fy, &fz); btVector3 from(fx, fy, fz); btVector3 to(tx, ty, tz); @@ -44,15 +45,23 @@ int raytest(lua_State *L){ } void load_cphysfuncs(lua_State* L){ - + assert(lua_gettop(L) == 0); + printf("Registering cphysbox\n"); //phys things + assert(lua_gettop(L) == 0); cbphysbox_register(L); + assert(lua_gettop(L) == 0); cbcharactercontroller_register(L); + assert(lua_gettop(L) == 0); cbphysmodel_register(L); + assert(lua_gettop(L) == 0); bghostobject_register(L); + assert(lua_gettop(L) == 0); + assert(lua_gettop(L) == 0); lua_getglobal(L,"phys");//{} lua_pushcfunction(L,raytest);//{},raytest() lua_setfield(L,-2,"raytest");//{} lua_pop(L,1); + assert(lua_gettop(L) == 0); } diff --git a/src/client/lua_api/load_video.cpp b/src/client/lua_api/load_video.cpp index f309180..b3487ca 100644 --- a/src/client/lua_api/load_video.cpp +++ b/src/client/lua_api/load_video.cpp @@ -8,6 +8,7 @@ extern "C" { #include "video/smaterial.hpp" #include "video/itexture.hpp" #include "video/iimage.hpp" +#include "video/draw.hpp" #include @@ -79,6 +80,20 @@ int draw2dline(lua_State* L){ return 0; } +//{sx,sy,sz},{ex,ey,ez},{color} +int draw3dline(lua_State* L){ + double sx,sy,sz; + double ex,ey,ez; + long r,g,b,a; + popvector4i(L,&r,&g,&b,&a); + popvector3d(L,&ex,&ey,&ez); + popvector3d(L,&sx,&sy,&sz); + + driver->draw3DLine(vector3df(sx,sy,sz),vector3df(ex,ey,ez),SColor(a,r,g,b)); + + return 0; +} + void load_videofuncs(lua_State* L){ //printf("Loading video libraries...\n"); lua_newtable(L);//{} @@ -88,11 +103,14 @@ void load_videofuncs(lua_State* L){ lua_pushcfunction(L,draw2dimage);//{},draw2dimage() lua_setfield(L,-2,"drawtexture");//{} lua_pushcfunction(L,draw2dline);//{},draw2dline() - lua_setfield(L,-2,"drawline");//{} + lua_setfield(L,-2,"draw2dline");//{} + lua_pushcfunction(L,draw3dline); + lua_setfield(L,-2,"draw3dline"); lua_pop(L,1);// smaterial_register(L); itexture_register(L); iimage_register(L); + draw_register(L); } diff --git a/src/client/lua_api/phys/cbcharactercontroller.cpp b/src/client/lua_api/phys/cbcharactercontroller.cpp index 6a6da96..16ad1ad 100644 --- a/src/client/lua_api/phys/cbcharactercontroller.cpp +++ b/src/client/lua_api/phys/cbcharactercontroller.cpp @@ -115,6 +115,4 @@ void cbcharactercontroller_register(lua_State* L){ //printf("When registering physbox, new() is %p\n",newcbphysbox); //printf("setpos is %p\n",cbphyssetpos); - lua_pop(L,1); - } diff --git a/src/client/lua_api/phys/cbphysbox.cpp b/src/client/lua_api/phys/cbphysbox.cpp index 3c0f97d..e675a04 100644 --- a/src/client/lua_api/phys/cbphysbox.cpp +++ b/src/client/lua_api/phys/cbphysbox.cpp @@ -2,6 +2,7 @@ #include #include #include +#include extern "C" { #include #include @@ -163,12 +164,15 @@ static const luaL_reg cbphysbox_m[] = { }; void cbphysbox_register(lua_State* L){ + assert(lua_gettop(L) == 0); bphysbox_register(L);// + assert(lua_gettop(L) == 0); lua_getglobal(L,"phys");//{} lua_pushcfunction(L,newcbphysbox);//{},newcbphysbox() - lua_setfield(L,-2,"newcphysbox");//{} + lua_setfield(L,-2,"newphysbox");//{phys} lua_pop(L,1);// + assert(lua_gettop(L) == 0); luaL_getmetatable(L,"phys.physbox");//phys.physbox lua_newtable(L);//phys.physbox,{} @@ -176,15 +180,14 @@ void cbphysbox_register(lua_State* L){ luaL_register(L,NULL,brigidbody_m); luaL_register(L,NULL,igeneric_m); luaL_register(L,NULL,cbphysbox_m);//phys.physbox,{} - lua_pushstring(L,"rigidbody"); - lua_setfield(L,-2,"type"); + lua_pushstring(L,"rigidbody");//phys.physbox,{},"rigidbody" + lua_setfield(L,-2,"type");//phys.physbox,{} lua_setfield(L,-2,"__index");//phys.physbox lua_pop(L,1); - //printf("When registering physbox, new() is %p\n",newcbphysbox); //printf("setpos is %p\n",cbphyssetpos); - lua_pop(L,1); + assert(lua_gettop(L) == 0); } diff --git a/src/client/lua_api/phys/cbphysmodel.cpp b/src/client/lua_api/phys/cbphysmodel.cpp index ac27288..ae2559e 100644 --- a/src/client/lua_api/phys/cbphysmodel.cpp +++ b/src/client/lua_api/phys/cbphysmodel.cpp @@ -6,6 +6,7 @@ #include #include #include +#include extern "C" { #include #include @@ -260,9 +261,12 @@ static const luaL_reg bphysmodel_m[] = { {0, 0}, }; -int cbphysmodel_register(lua_State* L){ +void cbphysmodel_register(lua_State* L){ + assert(lua_gettop(L) == 0); bphysmodel_register(L); + assert(lua_gettop(L) == 0); + assert(lua_gettop(L) == 0); luaL_getmetatable(L,"phys.physmodel");//{physmodel_m} lua_getfield(L,-1,"__index");//{physmodel_m},{} luaL_register(L,NULL,igeneric_m); //Inherit all the things to do with scene nodes @@ -270,9 +274,11 @@ int cbphysmodel_register(lua_State* L){ luaL_register(L,NULL,brigidbody_m);//Add rigidbody things luaL_register(L,NULL,bphysmodel_m);//anything specific? lua_pop(L,2); + assert(lua_gettop(L) == 0); + assert(lua_gettop(L) == 0); lua_getglobal(L,"phys"); luaL_register(L,NULL,bphysmodel_f); - - return 1; + lua_pop(L,1); + assert(lua_gettop(L) == 0); } diff --git a/src/client/lua_api/phys/cbphysmodel.hpp b/src/client/lua_api/phys/cbphysmodel.hpp index 5ea42a8..d8495d2 100644 --- a/src/client/lua_api/phys/cbphysmodel.hpp +++ b/src/client/lua_api/phys/cbphysmodel.hpp @@ -8,5 +8,5 @@ extern "C" { } #include -int cbphysmodel_register(lua_State* L); +void cbphysmodel_register(lua_State* L); #endif diff --git a/src/client/lua_api/scene/igeneric.cpp b/src/client/lua_api/scene/igeneric.cpp index 63bf4bc..dcac022 100644 --- a/src/client/lua_api/scene/igeneric.cpp +++ b/src/client/lua_api/scene/igeneric.cpp @@ -160,6 +160,23 @@ int iscenesetscale(lua_State *L){ return 1; } +/*** +Sets the visibility of this scene element +@function iscenenode:setvisible(bool) +@tparam boolean visible Sets the visibility for this element +*/ +//setvisible(true|false) +int iscenesetvisible(lua_State *L){ + int visible = lua_toboolean(L,-1); + lua_pop(L,1); + lua_getfield(L, -1, "node"); + ISceneNode *i = (ISceneNode*)lua_touserdata(L,-1);//{node=ud_ISceneNode}, ud_ISceneNode + lua_pop(L,2); + + i->setVisible(visible == 1); + return 0; +} + /*** Get the scale of this scene element. @function iscenenode:getscale() @@ -208,5 +225,6 @@ extern const luaL_reg igeneric_m[] = { {"setscale", iscenesetscale}, {"getscale", iscenegetscale}, {"remove", isceneremove}, + {"setvisible", iscenesetvisible}, {0, 0}, }; diff --git a/src/client/lua_api/scene/ilight.cpp b/src/client/lua_api/scene/ilight.cpp index 972de83..1e292ee 100644 --- a/src/client/lua_api/scene/ilight.cpp +++ b/src/client/lua_api/scene/ilight.cpp @@ -17,6 +17,7 @@ extern "C" { #include using namespace irr; +using namespace video; using namespace scene; using namespace core; @@ -27,9 +28,9 @@ static int newiscenelight(lua_State* L){ printf("Createing light!\n"); double x,y,z; popvector3d(L,&x,&y,&z);//radius - double radius = lua_tonumber(L,-1); + double radius = lua_tonumber(L,-1); lua_pop(L,1);// - + //Create the mesh ISceneManager* smgr = device->getSceneManager(); ILightSceneNode* light = smgr->addLightSceneNode(0,vector3df(x,y,z),video::SColor(1.0f,1.0f,1.0f,1.0f),(f32)radius,(s32)-1); @@ -45,11 +46,23 @@ static int newiscenelight(lua_State* L){ return 1; - } +} + +//settype(self,const) +int settype(lua_State *L){ + video::E_LIGHT_TYPE type = (video::E_LIGHT_TYPE)lua_tonumber(L,-1);//self,type + lua_pop(L,1);//self + lua_getfield(L,-1,"node");//self,ud_ILightSceneNode + ILightSceneNode *light = (ILightSceneNode*)lua_touserdata(L,-1); + lua_pop(L,2);// + light->setLightType(type); + return 0; +} static const luaL_reg ilight_m[] = { {"getpos", iscenegetpos}, {"setpos", iscenesetpos}, + {"settype", settype}, {0, 0}, }; @@ -59,6 +72,10 @@ void ilight_register(lua_State* L){ lua_pushcfunction(L,newiscenelight);//{scene},newiscenelight() lua_setfield(L,-2,"newlight");//{scene} + set_const(L,ELT_POINT); + set_const(L,ELT_SPOT); + set_const(L,ELT_DIRECTIONAL); + lua_pop(L,1);// luaL_newmetatable(L,"scene.ilight");//scene.ilight diff --git a/src/client/lua_api/video/draw.cpp b/src/client/lua_api/video/draw.cpp new file mode 100644 index 0000000..1f38f34 --- /dev/null +++ b/src/client/lua_api/video/draw.cpp @@ -0,0 +1,29 @@ + +#include +#include +#include "draw.hpp" + +using namespace irr; +using namespace video; +using namespace core; + +extern IrrlichtDevice* device; +extern IVideoDriver* driver; +//drawline2d {startx,starty},{endx,endy},{r,g,b,a} +int drawline2d(lua_State *L){ + long r,g,b,a; + popvector4i(L, &r, &g, &b, &a); + long endx, endy; + popvector2i(L, &endx, &endy); + long startx, starty; + popvector2i(L, &startx, &starty); + driver->draw2DLine(position2d(startx, starty), position2d(endx,endy), SColor(r,g,b,a)); + return 0; +} + +void draw_register(lua_State *L){ + lua_getglobal(L,"video");//{video} + lua_pushcfunction(L,drawline2d);//{video},drawline2d() + lua_setfield(L,-2,"drawline2d");//{video} + lua_pop(L,1);// +} diff --git a/src/client/lua_api/video/draw.hpp b/src/client/lua_api/video/draw.hpp new file mode 100644 index 0000000..eb5a9a7 --- /dev/null +++ b/src/client/lua_api/video/draw.hpp @@ -0,0 +1,8 @@ + +extern "C" { + #include + #include + #include +} + +void draw_register(lua_State* L); diff --git a/src/client/lua_api/video/smaterial.cpp b/src/client/lua_api/video/smaterial.cpp index 2ef7053..510748c 100644 --- a/src/client/lua_api/video/smaterial.cpp +++ b/src/client/lua_api/video/smaterial.cpp @@ -38,8 +38,22 @@ int setTexture(lua_State* L){ return 0; } +//{Material},flag,state +int setFlag(lua_State* L){ + int b = lua_toboolean(L,-1);//{material},flag,state + lua_pop(L,1);//{material},flag + int flag = lua_tonumber(L,-1);//{material},flag + lua_pop(L,1);//{material} + lua_getfield(L,-1,"smaterial");//{material},ud_material + SMaterial* mat = (SMaterial*)lua_touserdata(L,-1); + lua_pop(L,2); + mat->setFlag((E_MATERIAL_FLAG)flag,b == 1 ? true : false); + return 0; +} + static const luaL_reg smaterial_m[] = { {"setTexture", setTexture}, + {"setFlag", setFlag}, {0,0}, }; diff --git a/src/client/main.cpp b/src/client/main.cpp index f08862c..98b2f61 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -1,5 +1,6 @@ #include #include +#include extern "C" { #include #include @@ -134,7 +135,9 @@ int main(int argc, char *argv[]){ L = state; printf("Created lua state at %p\n",L); //Load the lua libraries + assert(lua_gettop(L) == 0); loadLLibs(state); + assert(lua_gettop(L) == 0); //Defined in initdevice.cpp, creates the irrlicht device printf("Argc: %d\n",argc); char *path; @@ -143,7 +146,9 @@ int main(int argc, char *argv[]){ }else{ path = (char*)"../data"; } + assert(lua_gettop(L) == 0); device = spawnIrrDevice(state,path); + assert(lua_gettop(L) == 0); if (!device){ printf("Failed to initalize device\n"); return 1; @@ -152,19 +157,24 @@ int main(int argc, char *argv[]){ //ILogger* log = device->getLogger(); //log->setLogLevel(ELL_NONE); //Loads libraries for interfaceing with irrlicht + assert(lua_gettop(L) == 0); luaL_openlibs(state); + assert(lua_gettop(L) == 0); loadIrrLibs(state,device); + assert(lua_gettop(L) == 0); loadNetLibs(state); + assert(lua_gettop(L) == 0); loadCommonLibs(state); + assert(lua_gettop(L) == 0); printf("Loadded irr libs...\n"); //Sets the global event handeler printf("Creating event receiver\n"); GlobalEventReceiver ger = GlobalEventReceiver(device); printf("Created event receiver\n"); device->setEventReceiver(&ger); - + assert(lua_gettop(L) == 0); pusherrorfunc(L);//errfunc - int err = luaL_loadfile(state,"init.lua"); + int err = luaL_loadfile(state,"init.lua");//errfunc,init() printf("Error loading init.lua: %d\n",err); switch(err){//errmsg or nothing case 0: @@ -181,8 +191,10 @@ int main(int argc, char *argv[]){ } //errfunc,initfile() printf("Loaded file\n"); - lua_pcall(state,0,0,-2); + lua_pcall(state,0,0,-2);//errfunc() + lua_pop(L,1);// printf("Finished running init.lua"); + assert(lua_gettop(L) == 0); //int iErr = luaL_dofile(state,"init.lua"); //if(iErr != 0){ //printf("Failed to open lua file:%s/init.lua\n",path); @@ -205,14 +217,21 @@ int main(int argc, char *argv[]){ printf("Device is %p\n",device); while(device->run()){ //printf("Start gameloop net\n"); + assert(lua_gettop(L) == 0); gameloop_net(L); + assert(lua_gettop(L) == 0); //printf("End gameloop net\n"); //printf("Start gameloop phys\n"); + assert(lua_gettop(L) == 0); gameloop_phys(UpdateElement); + assert(lua_gettop(L) == 0); //printf("End gameloop phys\n"); if(device->isWindowActive()){ + assert(lua_gettop(L) == 0); driver->beginScene(true, true, background); + assert(lua_gettop(L) == 0); //printf("Device active, began scene\n"); + assert(lua_gettop(L) == 0); pusherrorfunc(state); lua_getglobal(state,"GAME");//err(),{GAME} lua_getfield(state,-1,"draw");//err(),{GAME},GAME.draw() @@ -222,10 +241,12 @@ int main(int argc, char *argv[]){ }else{ lua_pop(state,3); } + assert(lua_gettop(L) == 0); //printf("Finished calling GAME.draw()\n"); smgr->drawAll(); //printf("Scene manager drew all\n"); + assert(lua_gettop(L) == 0); lua_getglobal(state,"GAME"); lua_getfield(state,-1,"drawPostScene"); if(!lua_isnil(state,-1)){ @@ -234,9 +255,11 @@ int main(int argc, char *argv[]){ }else{ lua_pop(state,2); } + assert(lua_gettop(L) == 0); //printf("Post draw scene completed\n"); guienv->drawAll(); //printf("Gui draw all completed\n"); + assert(lua_gettop(L) == 0); lua_getglobal(state,"GAME"); lua_getfield(state,-1,"drawPostGui"); if(!lua_isnil(state,-1)){ @@ -245,23 +268,29 @@ int main(int argc, char *argv[]){ }else{ lua_pop(state,2); } + assert(lua_gettop(L) == 0); //printf("GAME.drawPostGui completed\n"); driver->endScene(); }else{ + assert(lua_gettop(L) == 0); device->yield(); + assert(lua_gettop(L) == 0); } - lua_getglobal(state,"GAME");//{} - lua_getfield(state,-1,"tick");//{},function_tick() + assert(lua_gettop(L) == 0); + pusherrorfunc(L);//errfunc() + lua_getglobal(state,"GAME");//errfunc(),{} + lua_getfield(state,-1,"tick");//errfunc(),{},function_tick() if(!lua_isnil(state,-1)){ - lua_call(state,0,0); - lua_pop(state,1); - }else{ - lua_pop(state,2); + lua_pcall(state,0,0,-3);//errfunc(),{} + lua_pop(state,2);// + }else{//errfunc(),{},nil + lua_pop(state,3);// } + assert(lua_gettop(L) == 0); } + assert(lua_gettop(L) == 0); //phys_shutdown(RemoveISceneNode); phys_shutdown(); - device->drop(); printf("Goodbye\n"); return 0; diff --git a/src/server/main.cpp b/src/server/main.cpp index c4c4d8e..8817329 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -1,6 +1,7 @@ #include #include +#include extern "C" { #include #include @@ -66,24 +67,34 @@ int main (int argc, char *argv[]){ putenv(envstr); //printf("Put lua path\n"); L = luaL_newstate(); + assert(lua_gettop(L) == 0); //printf("Created lua state\n"); //lua_newtable(L);//{} //lua_setglobal(L,"GAME");// + assert(lua_gettop(L) == 0); load_gamefuncs(L); + assert(lua_gettop(L) == 0); printf("Created global table\n"); + assert(lua_gettop(L) == 0); phys_genesis(); printf("Started phys\n"); + assert(lua_gettop(L) == 0); luaL_openlibs(L); printf("Opened standard libs\n"); + assert(lua_gettop(L) == 0); loadLLibs(L); printf("Opened aux libs\n"); + assert(lua_gettop(L) == 0); loadNetLibs(L); printf("Opened net libs\n"); + assert(lua_gettop(L) == 0); load_physfuncs(L); printf("Opened phys libs\n"); + assert(lua_gettop(L) == 0); load_iofuncs(L); printf("About to push error func\n"); + assert(lua_gettop(L) == 0); pusherrorfunc(L);//errfunc printf("pushed error func\n"); size_t init_file_path_len = snprintf(NULL,0,"%s/init.lua",path); @@ -104,14 +115,19 @@ int main (int argc, char *argv[]){ } //errfunc,initfile() printf("Loaded file\n"); - lua_pcall(L,0,0,-2); + lua_pcall(L,0,0,-2);//errfunc + lua_pop(L,1); + assert(lua_gettop(L) == 0); do{ + assert(lua_gettop(L) == 0); //printf("Start of server gameloop\n"); gameloop(); + assert(lua_gettop(L) == 0); //printf("Gameloop\n"); //std::this_thread::yield(); std::this_thread::sleep_for(std::chrono::milliseconds(100)); //printf("Thread yeild\n"); + assert(lua_gettop(L) == 0); pusherrorfunc(L);//errfunc() lua_getglobal(L,"GAME");//errfunc(),{} lua_getfield(L,-1,"tick");//errfunc(),{},function_tick()? @@ -123,9 +139,12 @@ int main (int argc, char *argv[]){ //printf("Did not find tick function\n"); lua_pop(L,3); } + assert(lua_gettop(L) == 0); //printf("End of server gameloop\n"); }while(game_active); + assert(lua_gettop(L) == 0); phys_shutdown(); + assert(lua_gettop(L) == 0); printf("Goodbye\n"); return 0; diff --git a/src/shared/lua_api/common.cpp b/src/shared/lua_api/common.cpp index 9b833fe..b26c3cc 100644 --- a/src/shared/lua_api/common.cpp +++ b/src/shared/lua_api/common.cpp @@ -8,6 +8,8 @@ extern "C" { #include #include "common.hpp" +#define set_const(l,x) lua_pushstring(l,#x);lua_pushinteger(l,x);lua_settable(l,-3); + //Expose things to the lua state void loadLLibs(lua_State* L){ diff --git a/src/shared/lua_api/load_common.cpp b/src/shared/lua_api/load_common.cpp index 66afe67..dc61ef1 100644 --- a/src/shared/lua_api/load_common.cpp +++ b/src/shared/lua_api/load_common.cpp @@ -1,8 +1,30 @@ +#include +#include extern "C" { #include #include #include } +using namespace std::chrono; -void loadCommonLibs(lua_State* L); -void gameloop_common(lua_State* L); +//Gets the time +int get_time(lua_State* L){ + std::chrono::high_resolution_clock::time_point now = high_resolution_clock::now(); + std::chrono::high_resolution_clock::duration since_epoch = now.time_since_epoch(); + double dc = std::chrono::duration_cast(since_epoch).count(); + lua_pushnumber(L,dc); + return 1; +} + +void loadCommonLibs(lua_State* L){ + lua_getglobal(L,"GAME"); + lua_pushcfunction(L,make_crashy); + lua_setfield(L,-2,"crashy"); + lua_pop(L,1); + lua_pushcfunction(L,get_time); + lua_setglobal(L,"get_time"); +} + +void gameloop_common(lua_State* L){ + +} diff --git a/src/shared/lua_api/load_common.hpp b/src/shared/lua_api/load_common.hpp index 5be5de1..759ba85 100644 --- a/src/shared/lua_api/load_common.hpp +++ b/src/shared/lua_api/load_common.hpp @@ -1,11 +1,4 @@ #include -void loadCommonLibs(lua_State* L){ - lua_getglobal(L,"GAME"); - lua_pushcfunction(L,make_crashy); - lua_setfield(L,-2,"crashy"); -} - -void gameloop_common(lua_State* L){ - -} +void loadCommonLibs(lua_State* L); +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 d8d321a..3452aa2 100644 --- a/src/shared/lua_api/load_net.cpp +++ b/src/shared/lua_api/load_net.cpp @@ -30,34 +30,35 @@ ect. */ extern "C" { - #include - #include - #include +# include +# include +# include } #include - +#include #include #include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - +extern "C" { +# include + +# include +# include +# include +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +} #include "load_net.hpp" #include @@ -336,6 +337,7 @@ int block_recv(lua_State *L){//{socket} } void gameloop_net(lua_State* L){ + assert(lua_gettop(L) == 0); //printf("Doing net of gameloop,starting with %d args\n",lua_gettop(L)); //printf("Got net\n"); lua_getglobal(L,"net");//{net} @@ -423,6 +425,7 @@ void gameloop_net(lua_State* L){ //printf("There are %d items left on the lua stack\n",lua_gettop(L)); lua_pop(L,2); //printf("Done with net game loop\n"); + assert(lua_gettop(L) == 0); } /*** diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp index be5529c..7002a09 100644 --- a/src/shared/lua_api/load_phys.cpp +++ b/src/shared/lua_api/load_phys.cpp @@ -6,6 +6,7 @@ #include void load_physfuncs(lua_State* L){ + printf("Loading phys things\n"); lua_newtable(L);//{} lua_setglobal(L,"phys");// lua_getglobal(L,"phys");//{phys} diff --git a/src/shared/lua_api/load_scene.cpp b/src/shared/lua_api/load_scene.cpp index 633fcb1..ef22653 100644 --- a/src/shared/lua_api/load_scene.cpp +++ b/src/shared/lua_api/load_scene.cpp @@ -7,5 +7,4 @@ extern "C" { void register_scene(lua_State* L){ - } diff --git a/src/shared/lua_api/phys/bphysbuffer.cpp b/src/shared/lua_api/phys/bphysbuffer.cpp index 111b159..074f506 100644 --- a/src/shared/lua_api/phys/bphysbuffer.cpp +++ b/src/shared/lua_api/phys/bphysbuffer.cpp @@ -27,11 +27,11 @@ using namespace video; extern btDiscreteDynamicsWorld* World; extern core::list Objects; -static LBPhysNode* checkisbphysmodel(lua_State* L, int index){ - void* ud = luaL_checkudata(L,index,"phys.physmodel"); - luaL_argcheck(L,ud != NULL, index, "'phys.physmodel' expected"); - return (LBPhysNode*) ud; -} +//static LBPhysNode* checkisbphysmodel(lua_State* L, int index){ + //void* ud = luaL_checkudata(L,index,"phys.physmodel"); + //luaL_argcheck(L,ud != NULL, index, "'phys.physmodel' expected"); + //return (LBPhysNode*) ud; +//} //iscenecamera.new(Vector position, Vector lookat, parrent) // {} {} 0 1 diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp index 0cff0ed..2c9a02d 100644 --- a/src/shared/lua_api/phys/bphysmodel.cpp +++ b/src/shared/lua_api/phys/bphysmodel.cpp @@ -12,6 +12,8 @@ extern "C" { #include } #include +#include +#include #include "bphysmodel.hpp" #include #include @@ -69,19 +71,22 @@ void makebphysmodel(lua_State *L){ //__mingw_printf("attrib.num_vertices: %u\n",attrib.num_vertices); //__mingw_printf("attrib.num_faces: %u\n",attrib.num_faces); btTriangleMesh* trimesh = new btTriangleMesh(); + btVector3 vertexes[attrib.num_vertices]; for(size_t i = 0; i < attrib.num_vertices; i++){ //0 - x, so num_vertices - 1 - //__mingw_printf("Looking at vertice %llu/%u\n",i,attrib.num_vertices); - //DO NOT MULTIPLY THIS BY SIEZEOF(FLOAT) float *vs = attrib.vertices + (3*i);//3 floats per vertex //printf("Got start\n"); - float v1 = vs[0]; + //For some reason irrlicht and bullet disagree with which direction +x is, + //negate the x in the physics engine so the graphical stuff lines up + //with the physics stuff + float v1 = -vs[0]; //printf("Got 1\n"); float v2 = vs[1]; //printf("Got 2\n"); float v3 = vs[2]; //printf("Got all 3 vertexees\n"); //printf("Adding vertex: (%f %f %f)\n",v1, v2, v3); - trimesh->findOrAddVertex(btVector3(v1,v2,v3),true); + //trimesh->findOrAddVertex(btVector3(v1,v2,v3),true); + vertexes[i] = btVector3(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 @@ -89,7 +94,8 @@ void makebphysmodel(lua_State *L){ i1 = attrib.faces[i]; i2 = attrib.faces[i+1]; i3 = attrib.faces[i+2]; - trimesh->addTriangleIndices(i1.v_idx,i2.v_idx,i3.v_idx); + trimesh->addTriangle(vertexes[i1.v_idx],vertexes[i2.v_idx],vertexes[i3.v_idx],true); + //trimesh->addTriangleIndices(i1.v_idx,i2.v_idx,i3.v_idx); } printf("Finished adding triangle indicies\n"); //size_t numverts = attrib.num_face_num_verts; @@ -120,7 +126,8 @@ void makebphysmodel(lua_State *L){ //face_offset += (size_t)attrib.face_num_verts[i]; //} printf("Done building trimesh\n"); - btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true); + //btCollisionShape *shape = new btGImpactMeshShape(trimesh); + btCollisionShape *shape = new btConvexTriangleMeshShape(trimesh); btTransform tr; tr.setIdentity(); tr.setOrigin(btVector3(x,y,z)); diff --git a/src/shared/phys/physcommon.cpp b/src/shared/phys/physcommon.cpp index 379ad55..45374d2 100644 --- a/src/shared/phys/physcommon.cpp +++ b/src/shared/phys/physcommon.cpp @@ -2,8 +2,14 @@ #define __shared_physcommon_h #include #include +#include #include - +extern "C" { + #include + #include + #include +} +#include #include "physcommon.hpp" using namespace std::chrono; @@ -11,6 +17,7 @@ using namespace std::chrono; btDiscreteDynamicsWorld* World; std::list Objects; std::list Chars; +extern lua_State *L; extern void dropCollisionObject(btCollisionObject* b); @@ -45,7 +52,7 @@ btCollisionDispatcher* Dispatcher; btSequentialImpulseConstraintSolver* Solver; void phys_genesis(){ - broadphase = new btAxisSweep3(btVector3(-1000,-1000,-1000),btVector3(1000,1000,1000)); + broadphase = new btAxisSweep3(btVector3(-100000,-100000,-100000),btVector3(100000,100000,100000)); broadphase ->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback()); //printf("Broadphase\n"); CollisionConfiguration = new btDefaultCollisionConfiguration(); @@ -84,6 +91,7 @@ void UpdatePhysics(double TDeltaTime, void(*f)(btCollisionObject *)) { //printf("Pre step simulation\n"); World->stepSimulation(TDeltaTime * 0.2f, 60); //printf("Done step simulation\n"); + assert(lua_gettop(L) == 0); if(f){ //printf("Updating the position of %llu objects\n", Objects.size()); // Relay the object's orientation to irrlicht @@ -91,15 +99,52 @@ void UpdatePhysics(double TDeltaTime, void(*f)(btCollisionObject *)) { (*f)(*it); } } + assert(lua_gettop(L) == 0); + + //Call phy.oncollide(obj1, obj2, point1, point2, normal2) + lua_getglobal(L,"phys");//phys + lua_getfield(L,-1,"colliders");//phys,colliders + int nummanifolds = World->getDispatcher()->getNumManifolds(); + pusherrorfunc(L);//{phys},{colliders},errfunc() + for(int i = 0; i < nummanifolds; i++){ + //printf("Looking at manifold %d, top is %d\n", i, lua_gettop(L)); + + lua_getfield(L,-3,"oncollide");//{phys},{colliders},errfunc(),oncollide() + if(lua_isnil(L,-1)){ + lua_pop(L,4); + return; + } + btPersistentManifold *mf = World->getDispatcher()->getManifoldByIndexInternal(i); + btCollisionObject *oa = (btCollisionObject*)mf->getBody0(); + btCollisionObject *ob = (btCollisionObject*)mf->getBody1(); + btManifoldPoint mp = mf->getContactPoint(i); + btVector3 pa = mp.getPositionWorldOnA(); + btVector3 pb = mp.getPositionWorldOnB(); + btVector3 pn = mp.m_normalWorldOnB; + lua_pushlightuserdata(L,oa);//{phys},{colliders},errfunc(),oncollide(),ud_oa + lua_gettable(L,-4);//{phys},{colliders},errfun(),concollide(),{oa} + lua_pushlightuserdata(L,ob);//{phys},{colliders},errfunc(),oncollide(),{oa},ud_ob + lua_gettable(L,-5);//{phys},{colliders},errfunc(),oncollide(),{oa},{ob} + pushvector3d(L,pa.x(),pa.y(),pa.z()); + pushvector3d(L,pb.x(),pb.y(),pb.z()); + pushvector3d(L,pn.x(),pn.y(),pn.z());//{phys},{colliders},errfunc(),oncollide(),{oa},{ob},{pa},{pb},{normal} + int err = lua_pcall(L,5,0,-7);//{phys},{colliders},errfunc() + if(err) + printf("Failed to call oncollide\n"); + } + lua_pop(L,3); + assert(lua_gettop(L) == 0); } high_resolution_clock::time_point t1 = high_resolution_clock::now(); void gameloop_phys(void(*f)(btCollisionObject *)){ + assert(lua_gettop(L) == 0); //printf("Doing phys gameloop\n"); high_resolution_clock::time_point now = high_resolution_clock::now(); duration delta = now-t1; double steps = delta.count() * 10; UpdatePhysics(steps,f); t1 = now; + assert(lua_gettop(L) == 0); } #endif -- cgit v1.2.3-70-g09d2