aboutsummaryrefslogtreecommitdiff
path: root/src/server/main.cpp
blob: b326e5f216cad3be79ba61e7f084ae273c7561f0 (plain)
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
#include <stdio.h>
#include <stdlib.h>
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}

//C++ things
#include <vector>
#include <thread>
#include <list>

#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 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;
}