1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
//C++ things
#include <vector>
#include <thread>
#include <list>
#include <chrono>
#include <thread>
#include <string.h> // for strstr()
#include <btBulletDynamicsCommon.h>
#include <cstdlib>
#include <shared/lua_api/common.hpp>
#include <shared/lua_api/load_net.hpp>
#include <shared/lua_api/load_phys.hpp>
#include <shared/phys/physcommon.hpp>
#include <shared/util/hashmap.hpp>
#include <server/lua_api/load_game.hpp>
#include <server/lua_api/load_io.hpp>
using namespace std;
using namespace chrono;
bool game_active = true;
void dropRigidBody(btRigidBody* rb){
}
void dropCollisionObject(btCollisionObject* obj){
}
void dropChar(btKinematicCharacterController *a){
}
void dropGhostObject(btGhostObject *ghost){
}
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();
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);
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);//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()?
if(!lua_isnil(L,-1)){
//printf("Found game tick\n");
lua_pcall(L,0,0,-3);//errfunc(),{}
lua_pop(L,2);
}else{
//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;
}
|