2007-06-14
This commit is contained in:
1 parent
e20673c2cd
commit
1ae34ae340
96 files changed
+5680
-1020
No files matched your search
@@ -1,3 +1,4 @@
|
||||
#include <cassert>
|
||||
#include "lua_ini_bridge.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include "lua_map.h"
|
||||
#include "m_exceptions.h"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
int Block::l_getBlockType(lua_State *L) {
|
||||
lua_pushinteger(L, blockType());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_setBlockType(lua_State *L) {
|
||||
uint8_t v = luaL_checkinteger(L, 1);
|
||||
setBlockType(v);
|
||||
return 0;
|
||||
}
|
||||
int Block::l_getSlopeType(lua_State *L) {
|
||||
lua_pushinteger(L, slopeType());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_setSlopeType(lua_State *L) {
|
||||
uint8_t v = luaL_checkinteger(L, 1);
|
||||
setSlopeType(v);
|
||||
return 0;
|
||||
}
|
||||
int Block::l_getRotation(lua_State *L) {
|
||||
lua_pushinteger(L, rotation());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_setRotation(lua_State *L) {
|
||||
uint8_t v = luaL_checkinteger(L, 1);
|
||||
setRotation(v);
|
||||
return 0;
|
||||
}
|
||||
int Block::l_getUpOk(lua_State *L) {
|
||||
lua_pushboolean(L, upOk());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getDownOk(lua_State *L) {
|
||||
lua_pushboolean(L, downOk());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getLeftOk(lua_State *L) {
|
||||
lua_pushboolean(L, leftOk());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getRightOk(lua_State *L) {
|
||||
lua_pushboolean(L, rightOk());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getIsFlat(lua_State *L) {
|
||||
lua_pushboolean(L, isFlat());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getRailway(lua_State *L) {
|
||||
lua_pushboolean(L, railway());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getRailStation(lua_State *L) {
|
||||
lua_pushboolean(L, railStation());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getRailStationTrain(lua_State *L) {
|
||||
lua_pushboolean(L, railStationTrain());
|
||||
return 1;
|
||||
}
|
||||
int Block::l_getTextureId(lua_State *L) {
|
||||
int which = luaL_checkinteger(L, 1);
|
||||
int v = 0;
|
||||
if ((which < 0) || (which > 4)) {
|
||||
std::ostringstream ostr;
|
||||
ostr << "Quad id " << which << " is invalid";
|
||||
throw E_OUTOFRANGE(ostr.str());
|
||||
}
|
||||
switch(which) {
|
||||
case 0:
|
||||
v = lid;
|
||||
break;
|
||||
case 1:
|
||||
v = top;
|
||||
break;
|
||||
case 2:
|
||||
v = bottom;
|
||||
break;
|
||||
case 3:
|
||||
v = left;
|
||||
break;
|
||||
case 4:
|
||||
v = right;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
// handled above
|
||||
}
|
||||
lua_pushinteger(L, v);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LMap::l_getBlockAt(lua_State *L) {
|
||||
int x, y, z;
|
||||
x = luaL_checkinteger(L, 1);
|
||||
y = luaL_checkinteger(L, 2);
|
||||
z = luaL_checkinteger(L, 3);
|
||||
OpenGTA::Map::BlockInfo* bi = getBlockAtNew(x, y, z);
|
||||
assert(bi);
|
||||
Block* b = static_cast<Block*>(bi);
|
||||
Lunar<Block>::push(L, b, false);
|
||||
return 1;
|
||||
}
|
||||
int LMap::l_getNumBlocksAt(lua_State *L) {
|
||||
int x, y;
|
||||
x = luaL_checkinteger(L, 1);
|
||||
y = luaL_checkinteger(L, 2);
|
||||
lua_pushinteger(L, getNumBlocksAtNew(x, y));
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char Block::className[] = "Block";
|
||||
#define method(name) {#name, &Block::l_##name}
|
||||
Lunar<Block>::RegType Block::methods[] = {
|
||||
method(getBlockType),
|
||||
method(getSlopeType),
|
||||
method(getRotation),
|
||||
method(getIsFlat),
|
||||
method(getLeftOk),
|
||||
method(getRightOk),
|
||||
method(getUpOk),
|
||||
method(getDownOk),
|
||||
method(getRailway),
|
||||
method(getRailStation),
|
||||
method(getRailStationTrain),
|
||||
method(getTextureId),
|
||||
|
||||
method(setBlockType),
|
||||
method(setRotation),
|
||||
method(setSlopeType),
|
||||
{0, 0}
|
||||
};
|
||||
#undef method
|
||||
#define method(name) {#name, &LMap::l_##name}
|
||||
const char LMap::className[] = "Map";
|
||||
Lunar<LMap>::RegType LMap::methods[] = {
|
||||
method(getNumBlocksAt),
|
||||
method(getBlockAt),
|
||||
{0, 0}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#ifndef LUA_MAP_H
|
||||
#define LUA_MAP_H
|
||||
|
||||
#include "lunar.h"
|
||||
#include "opengta.h"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
class Block : public OpenGTA::Map::BlockInfo {
|
||||
public:
|
||||
int l_getBlockType(lua_State *L);
|
||||
int l_getUpOk(lua_State *L);
|
||||
int l_getDownOk(lua_State *L);
|
||||
int l_getLeftOk(lua_State *L);
|
||||
int l_getRightOk(lua_State *L);
|
||||
int l_getIsFlat(lua_State *L);
|
||||
int l_getSlopeType(lua_State *L);
|
||||
int l_getRotation(lua_State *L);
|
||||
int l_getRailway(lua_State *L);
|
||||
int l_getRailStation(lua_State *L);
|
||||
int l_getRailStationTrain(lua_State *L);
|
||||
int l_getTextureId(lua_State *L);
|
||||
|
||||
int l_setBlockType(lua_State *L);
|
||||
int l_setUpOk(lua_State *L);
|
||||
int l_setDownOk(lua_State *L);
|
||||
int l_setLeftOk(lua_State *L);
|
||||
int l_setRightOk(lua_State *L);
|
||||
int l_setIsFlat(lua_State *L);
|
||||
int l_setSlopeType(lua_State *L);
|
||||
int l_setRotation(lua_State *L);
|
||||
//...
|
||||
int l_setTextureId(lua_State *L);
|
||||
|
||||
|
||||
static const char className[];
|
||||
static Lunar<Block>::RegType methods[];
|
||||
};
|
||||
class LMap : public OpenGTA::Map {
|
||||
public:
|
||||
LMap() : OpenGTA::Map("") {}
|
||||
int l_getNumBlocksAt(lua_State *L);
|
||||
int l_getBlockAt(lua_State *L);
|
||||
|
||||
static const char className[];
|
||||
static Lunar<LMap>::RegType methods[];
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+82
-6
@@ -11,6 +11,8 @@
|
||||
|
||||
using namespace Util;
|
||||
|
||||
extern int global_Done;
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
LuaVM::LuaVM() : L(NULL) {
|
||||
@@ -20,6 +22,7 @@ namespace OpenGTA {
|
||||
|
||||
luaopen_base(L);
|
||||
luaopen_math(L);
|
||||
luaopen_table(L);
|
||||
_registered = false;
|
||||
lua_settop(L, 0);
|
||||
prepare();
|
||||
@@ -31,17 +34,24 @@ namespace OpenGTA {
|
||||
L = NULL;
|
||||
}
|
||||
|
||||
int vm_quit(lua_State *L) {
|
||||
global_Done = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LuaVM::prepare() {
|
||||
LGUARD(L);
|
||||
if (!_registered) {
|
||||
Lunar<Block>::Register2(L);
|
||||
Lunar<LMap>::Register2(L);
|
||||
/*
|
||||
#ifndef LUA_MAP_ONLY
|
||||
Lunar<CityView>::Register2(L);
|
||||
luaL_openlib(L, "camera", Camera::methods, 0);
|
||||
luaL_openlib(L, "screen", Screen::methods, 0);
|
||||
luaL_openlib(L, "spritecache", SpriteCache::methods, 0);
|
||||
*/
|
||||
#endif
|
||||
lua_pushcfunction(L, vm_quit);
|
||||
lua_setglobal(L, "quit");
|
||||
}
|
||||
_registered = true;
|
||||
}
|
||||
@@ -61,15 +71,15 @@ namespace OpenGTA {
|
||||
}
|
||||
|
||||
void LuaVM::setCityView(OpenGTA::CityView & cv) {
|
||||
#ifndef LUA_MAP_ONLY
|
||||
LGUARD(L);
|
||||
/*
|
||||
CityView *scv = static_cast<CityView*>(&cv);
|
||||
lua_gettable(L, LUA_GLOBALSINDEX);
|
||||
int scv_ref = Lunar<CityView>::push(L, scv, false);
|
||||
lua_pushliteral(L, "city_view");
|
||||
lua_pushvalue(L, scv_ref);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
*/
|
||||
#endif
|
||||
}
|
||||
|
||||
void LuaVM::runString(const char* _str) {
|
||||
@@ -103,7 +113,7 @@ namespace OpenGTA {
|
||||
lua_getglobal(L, key);
|
||||
if (!lua_isnumber(L, -1))
|
||||
throw E_SCRIPTERROR("Expected int value for key: " + std::string(key));
|
||||
int v = int(lua_tonumber(L, -1));
|
||||
int v = int(lua_tointeger(L, -1));
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -125,9 +135,69 @@ namespace OpenGTA {
|
||||
return v;
|
||||
}
|
||||
|
||||
bool LuaVM::getGlobalBool(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getglobal(L, key);
|
||||
if (!lua_isboolean(L, -1))
|
||||
throw E_SCRIPTERROR("Expected boolean value for key: " + std::string(key));
|
||||
return lua_toboolean(L, -1);
|
||||
}
|
||||
|
||||
void LuaVM::setInt(const char* key, int v) {
|
||||
lua_pushinteger(L, v);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
int LuaVM::getInt(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getfield(L, -1, key);
|
||||
if (!lua_isnumber(L, -1))
|
||||
throw E_SCRIPTERROR("Expected int value for key: " + std::string(key));
|
||||
return luaL_checkinteger(L, -1);
|
||||
}
|
||||
|
||||
void LuaVM::setFloat(const char* key, float v) {
|
||||
lua_pushnumber(L, v);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
float LuaVM::getFloat(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getfield(L, -1, key);
|
||||
if (!lua_isnumber(L, -1))
|
||||
throw E_SCRIPTERROR("Expected float value for key: " + std::string(key));
|
||||
return luaL_checknumber(L, -1);
|
||||
}
|
||||
|
||||
void LuaVM::setString(const char* key, const char* v) {
|
||||
lua_pushstring(L, v);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
const char* LuaVM::getString(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getfield(L, -1, key);
|
||||
if (!lua_isstring(L, -1))
|
||||
throw E_SCRIPTERROR("Expected string value for key: " + std::string(key));
|
||||
return luaL_checkstring(L, -1);
|
||||
}
|
||||
|
||||
void LuaVM::setBool(const char* key, bool v) {
|
||||
lua_pushboolean(L, v);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
bool LuaVM::getBool(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getfield(L, -1, key);
|
||||
if (!lua_isboolean(L, -1))
|
||||
throw E_SCRIPTERROR("Expected boolean value for key: " + std::string(key));
|
||||
return lua_toboolean(L, -1);
|
||||
}
|
||||
|
||||
void LuaVM::setGlobalInt(const char* key, int v) {
|
||||
LGUARD(L);
|
||||
lua_pushnumber(L, v);
|
||||
lua_pushinteger(L, v);
|
||||
lua_setglobal(L, key);
|
||||
}
|
||||
|
||||
@@ -143,5 +213,11 @@ namespace OpenGTA {
|
||||
lua_setglobal(L, key);
|
||||
}
|
||||
|
||||
void LuaVM::setGlobalBool(const char* key, bool v) {
|
||||
LGUARD(L);
|
||||
lua_pushboolean(L, v);
|
||||
lua_setglobal(L, key);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+21
-9
@@ -11,17 +11,29 @@ namespace OpenGTA {
|
||||
public:
|
||||
LuaVM();
|
||||
~LuaVM();
|
||||
void runString(const char*);
|
||||
void runFile(const char*);
|
||||
void callSimpleFunction(const char*);
|
||||
void setCityView(OpenGTA::CityView &);
|
||||
void setMap(OpenGTA::Map &);
|
||||
int getGlobalInt(const char*);
|
||||
void runString(const char*);
|
||||
void runFile(const char*);
|
||||
void callSimpleFunction(const char*);
|
||||
void setCityView(OpenGTA::CityView &);
|
||||
void setMap(OpenGTA::Map &);
|
||||
int getGlobalInt(const char*);
|
||||
float getGlobalFloat(const char*);
|
||||
const char* getGlobalString(const char*);
|
||||
void setGlobalInt(const char*, int);
|
||||
void setGlobalFloat(const char*, float);
|
||||
void setGlobalString(const char*, const char*);
|
||||
bool getGlobalBool(const char*);
|
||||
void setGlobalInt(const char*, int);
|
||||
void setGlobalFloat(const char*, float);
|
||||
void setGlobalString(const char*, const char*);
|
||||
void setGlobalBool(const char*, bool);
|
||||
|
||||
void setInt(const char*, int);
|
||||
int getInt(const char*);
|
||||
void setFloat(const char*, float);
|
||||
float getFloat(const char*);
|
||||
void setString(const char*, const char*);
|
||||
const char* getString(const char*);
|
||||
void setBool(const char*, bool);
|
||||
bool getBool(const char*);
|
||||
|
||||
lua_State *getInternalState();
|
||||
protected:
|
||||
lua_State *L;
|
||||
|
||||
Reference in new issue
Block a user