#include #include extern "C" { #include #include #include } //C++ things #include #include #include #include // for strstr() #include #include #include #include #include #include #include #include #include using namespace std; using namespace chrono; bool game_active = true; void dropRigidBody(btRigidBody* rb){ } void dropChar(btKinematicCharacterController *a){ } lua_State* L; void gameloop(){ gameloop_phys(NULL); //printf("done physics\n"); gameloop_net(L); //printf("done net\n"); } int main (int argc, char *argv[]){ printf("Brok[en]gine Server\n"); game_active = true; char *path; if(argc == 2){ path = argv[1]; }else{ path = (char*)"../data"; } size_t envstrsize = snprintf(NULL,0,"LUA_PATH=%s/?.lua",path); char envstr[envstrsize]; sprintf(envstr,"LUA_PATH=%s/?.lua",path); putenv(envstr); //printf("Put lua path\n"); L = luaL_newstate(); //printf("Created lua state\n"); //lua_newtable(L);//{} //lua_setglobal(L,"GAME");// load_gamefuncs(L); printf("Created global table\n"); phys_genesis(); printf("Started phys\n"); luaL_openlibs(L); printf("Opened standard libs\n"); loadLLibs(L); printf("Opened aux libs\n"); loadNetLibs(L); printf("Opened net libs\n"); load_physfuncs(L); printf("Opened phys libs\n"); load_iofuncs(L); printf("About to push error func\n"); pusherrorfunc(L);//errfunc printf("pushed error func\n"); size_t init_file_path_len = snprintf(NULL,0,"%s/init.lua",path); char init_file_path[init_file_path_len]; sprintf(init_file_path,"%s/init.lua",path); switch(luaL_loadfile(L,init_file_path)){ case 0: break; //no error case LUA_ERRSYNTAX: printf("Syntax error, failed to load: %s\n%s",init_file_path,lua_tostring(L,-1)); break; case LUA_ERRMEM: printf("Failed to allocate memroy\n"); break; case LUA_ERRFILE: printf("Could not find file: %s\n",init_file_path); break; } //errfunc,initfile() printf("Loaded file\n"); lua_pcall(L,0,0,-2); do{ //printf("Start of server gameloop\n"); gameloop(); //printf("Gameloop\n"); //std::this_thread::yield(); std::this_thread::sleep_for(std::chrono::milliseconds(100)); //printf("Thread yeild\n"); lua_getglobal(L,"GAME");//{} lua_getfield(L,-1,"tick");//{},function_tick() if(!lua_isnil(L,-1)){ //printf("Found game tick\n"); lua_call(L,0,0); lua_pop(L,1); }else{ //printf("Did not find tick function\n"); lua_pop(L,2); } //printf("End of server gameloop\n"); }while(game_active); phys_shutdown(); printf("Goodbye\n"); return 0; }