Beispiel #1
0
/*
============
G_InitLua
============
*/
void G_InitLua()
{
	char            buf[MAX_STRING_CHARS];
	char			filename[MAX_QPATH];
	
	G_Printf("------- Lua Initialization -------\n");

	g_luaState = lua_open();

	// Lua standard lib
	luaopen_base(g_luaState);
	luaopen_string(g_luaState);

	// Quake lib
	luaopen_entity(g_luaState);
	luaopen_game(g_luaState);
	luaopen_qmath(g_luaState);
	luaopen_vector(g_luaState);
	
	// load map specific Lua script as default
	trap_Cvar_VariableStringBuffer("mapname", buf, sizeof(buf));
	Com_sprintf(filename, sizeof(filename), "scripts/lua/%s.lua", buf);
	
	G_LoadLuaScript(NULL, filename);

	G_Printf("-----------------------------------\n");
}
Beispiel #2
0
/*
============
G_InitLua
============
*/
void G_InitLua()
{
	char            buf[MAX_STRING_CHARS];
	
	G_Printf( "------- Game Lua Initialization -------\n" );
	
	g_luaState = lua_open();
	
	// Lua standard lib
	luaopen_base( g_luaState );
	luaopen_string( g_luaState );
	
	// Quake lib
	luaopen_entity( g_luaState );
	luaopen_game( g_luaState );
	luaopen_qmath( g_luaState );
	luaopen_vector( g_luaState );
	
	// Create the callback table (at location 1 in the registry)
	lua_pushnumber( g_luaState, 1 );
	lua_newtable( g_luaState );
	lua_settable( g_luaState, LUA_REGISTRYINDEX );
	
	// load global scripts
	G_Printf( "global lua scripts:\n" );
	G_InitLua_Global();
	
	// load map-specific lua scripts
	G_Printf( "map specific lua scripts:\n" );
	g_cvars->Cvar_VariableStringBuffer( "mapname", buf, sizeof( buf ) );
	G_InitLua_Local( buf );
	G_LoadLuaScript( va( "maps/%s.lua", buf ) );
	
	G_Printf( "-----------------------------------\n" );
	
	G_RunLuaFunction( g_luaState, "G_InitGame", "" );
	
}
Beispiel #3
0
/** G_LuaStartVM( vm )
 * Starts one individual virtual machine.
 */
qboolean G_LuaStartVM(lua_vm_t * vm)
{
	int             res = 0;
	char            homepath[MAX_QPATH], gamepath[MAX_QPATH];

	// Open a new lua state
	vm->L = luaL_newstate();
	if(!vm->L)
	{
		LOG("Lua API: Lua failed to initialise.\n");
		return qfalse;
	}

	// Initialise the lua state
	luaL_openlibs(vm->L);

	// set LUA_PATH and LUA_CPATH
	// TODO: add "fs_basepath/fs_game/?.lua;" to LUA_PATH
	//       and LUA_CPATH for linux machines
	trap_Cvar_VariableStringBuffer("fs_homepath", homepath, sizeof(homepath));
	trap_Cvar_VariableStringBuffer("fs_game", gamepath, sizeof(gamepath));

	lua_getglobal(vm->L, LUA_LOADLIBNAME);
	if(lua_istable(vm->L, -1))
	{
		lua_pushstring(vm->L, va("%s%s%s%s?.lua;%s%s%s%slualib%slua%s?.lua",
								 homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP,
								 homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP));
		lua_setfield(vm->L, -2, "path");
		lua_pushstring(vm->L, va("%s%s%s%s?.%s;%s%s%s%slualib%sclibs%s?.%s",
								 homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, EXTENSION,
								 homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP, EXTENSION));
		lua_setfield(vm->L, -2, "cpath");
	}
	lua_pop(vm->L, 1);

	// register globals
	lua_registerglobal(vm->L, "LUA_PATH", va("%s%s%s%s?.lua;%s%s%s%slualib%slua%s?.lua",
											 homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP,
											 homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP));
	lua_registerglobal(vm->L, "LUA_CPATH", va("%s%s%s%s?.%s;%s%s%s%slualib%sclibs%s?.%s",
											  homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, EXTENSION,
											  homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP, EXTENSION));
	lua_registerglobal(vm->L, "LUA_DIRSEP", LUA_DIRSEP);

	// register predefined constants
	lua_newtable(vm->L);
	lua_regconstinteger(vm->L, CS_PLAYERS);
	lua_regconstinteger(vm->L, EXEC_NOW);
	lua_regconstinteger(vm->L, EXEC_INSERT);
	lua_regconstinteger(vm->L, EXEC_APPEND);
	lua_regconstinteger(vm->L, FS_READ);
	lua_regconstinteger(vm->L, FS_WRITE);
	lua_regconstinteger(vm->L, FS_APPEND);
	lua_regconstinteger(vm->L, FS_APPEND_SYNC);
	lua_regconstinteger(vm->L, SAY_ALL);
	lua_regconstinteger(vm->L, SAY_TEAM);
	//xreal
	//lua_regconstinteger(vm->L, SAY_BUDDY);
	//lua_regconstinteger(vm->L, SAY_TEAMNL);
	lua_regconststring(vm->L, HOSTARCH);
	lua_setglobal(vm->L, "et");

	// register functions
	luaopen_et(vm->L);
	luaopen_game(vm->L);
	luaopen_qmath(vm->L);
	luaopen_mover(vm->L);
	luaopen_vector(vm->L);

	// Load the code
	res = luaL_loadbuffer(vm->L, vm->code, vm->code_size, vm->file_name);
	if(res == LUA_ERRSYNTAX)
	{
		LOG("Lua API: syntax error during pre-compilation: %s\n", lua_tostring(vm->L, -1));
		lua_pop(vm->L, 1);
		vm->err++;
		return qfalse;
	}
	else if(res == LUA_ERRMEM)
	{
		LOG("Lua API: memory allocation error #1 ( %s )\n", vm->file_name);
		vm->err++;
		return qfalse;
	}

	// Execute the code
	if(!G_LuaCall(vm, "G_LuaStartVM", 0, 0))
		return qfalse;

	LOG("Lua API: Loading %s\n", vm->file_name);
	return qtrue;
}
Beispiel #4
0
LuaScript::LuaScript(Kampf* kf) {
  this->L = lua_open();

  //load all of our libraries
  // luaopen_base(this->L);
  // luaopen_table(this->L);
  // luaopen_io(this->L);
  // luaopen_string(this->L);
  // luaopen_math(this->L);

  //standard libs
  luaL_openlibs(L);

  //custom libraries extending kampf

  //kf vectors
  luaopen_vector(L);
  
  //kf quaternions
  luaopen_quaternion(L);

  //SDLAssetManager
  luaopen_SDL_AM(L);
  luaopen_SDLDrawable(L);
  luaopen_SDLText(L);

  //SDLFontManager
  luaopen_SDL_FM(L);
  luaopen_SDLFont(L);

  //Entity-related
  luaopen_entity(L);
  luaopen_entityManager(L);
  luaopen_customAttribute(L);
  luaopen_abstractComponent(L);

  //components inheriting from abstract component
  luaopen_component(L);
  luaopen_collisionComponent(L);
  luaopen_physicsComponent(L);
  luaopen_graphicsComponent(L);

  //TimeManager
  luaopen_timeManager(L);
  
  //Physics
  luaopen_physicsregistry(L);
  luaopen_luaforcegenerator(L);
  luaopen_AbstractForceGenerator(L);

  if (kf != nullptr) {
    luaopen_kampf(L, kf);
    luaopen_messenger(L, kf->getMessenger());
    luaopen_message(L);
    luaopen_rulemachine(L, kf->getRuleMachine());
    luaopen_renderwindow(L, kf->getWindowContext());
  }
  else {
      std::cout << "Kampf not initialized, some functionality will not be available" << std::endl;
  }

  //hard coding the packages path to the current directory
  this->clearPath();
  this->addPath("?.lua");
  this->addPath("./scripts/?.lua");
  this->addPath("../scripts/?.lua");
}