Bitops for Lua, incomplete elements API

This commit is contained in:
Simon Robertshaw 2012-08-29 20:50:18 +01:00
parent 6760d15be7
commit 3e78f64da8
4 changed files with 365 additions and 10 deletions

192
src/cat/LuaBit.cpp Normal file
View File

@ -0,0 +1,192 @@
/*
** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
** http://bitop.luajit.org/
**
** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
*/
#define LUA_BITOP_VERSION "1.0.2"
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#ifdef _MSC_VER
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
typedef int32_t SBits;
typedef uint32_t UBits;
typedef union {
lua_Number n;
#ifdef LUA_NUMBER_DOUBLE
uint64_t b;
#else
UBits b;
#endif
} BitNum;
/* Convert argument to bit type. */
static UBits barg(lua_State *L, int idx)
{
BitNum bn;
UBits b;
#if LUA_VERSION_NUM < 502
bn.n = lua_tonumber(L, idx);
#else
bn.n = luaL_checknumber(L, idx);
#endif
#if defined(LUA_NUMBER_DOUBLE)
bn.n += 6755399441055744.0; /* 2^52+2^51 */
#ifdef SWAPPED_DOUBLE
b = (UBits)(bn.b >> 32);
#else
b = (UBits)bn.b;
#endif
#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
defined(LUA_NUMBER_LLONG)
if (sizeof(UBits) == sizeof(lua_Number))
b = bn.b;
else
b = (UBits)(SBits)bn.n;
#elif defined(LUA_NUMBER_FLOAT)
#error "A 'float' lua_Number type is incompatible with this library"
#else
#error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
#endif
#if LUA_VERSION_NUM < 502
if (b == 0 && !lua_isnumber(L, idx)) {
luaL_typerror(L, idx, "number");
}
#endif
return b;
}
/* Return bit type. */
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
#define BIT_OP(func, opr) \
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
BIT_OP(bit_band, &=)
BIT_OP(bit_bor, |=)
BIT_OP(bit_bxor, ^=)
#define bshl(b, n) (b << n)
#define bshr(b, n) (b >> n)
#define bsar(b, n) ((SBits)b >> n)
#define brol(b, n) ((b << n) | (b >> (32-n)))
#define bror(b, n) ((b << (32-n)) | (b >> n))
#define BIT_SH(func, fn) \
static int func(lua_State *L) { \
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
BIT_SH(bit_lshift, bshl)
BIT_SH(bit_rshift, bshr)
BIT_SH(bit_arshift, bsar)
BIT_SH(bit_rol, brol)
BIT_SH(bit_ror, bror)
static int bit_bswap(lua_State *L)
{
UBits b = barg(L, 1);
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
BRET(b)
}
static int bit_tohex(lua_State *L)
{
UBits b = barg(L, 1);
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
const char *hexdigits = "0123456789abcdef";
char buf[8];
int i;
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
if (n > 8) n = 8;
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
lua_pushlstring(L, buf, (size_t)n);
return 1;
}
static const struct luaL_Reg bit_funcs[] = {
{ "tobit", bit_tobit },
{ "bnot", bit_bnot },
{ "band", bit_band },
{ "bor", bit_bor },
{ "bxor", bit_bxor },
{ "lshift", bit_lshift },
{ "rshift", bit_rshift },
{ "arshift", bit_arshift },
{ "rol", bit_rol },
{ "ror", bit_ror },
{ "bswap", bit_bswap },
{ "tohex", bit_tohex },
{ NULL, NULL }
};
/* Signed right-shifts are implementation-defined per C89/C99.
** But the de facto standard are arithmetic right-shifts on two's
** complement CPUs. This behaviour is required here, so test for it.
*/
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)
int luaopen_bit(lua_State *L)
{
UBits b;
lua_pushnumber(L, (lua_Number)1437217655L);
b = barg(L, -1);
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
const char *msg = "compiled with incompatible luaconf.h";
#ifdef LUA_NUMBER_DOUBLE
#ifdef _WIN32
if (b == (UBits)1610612736L)
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
#endif
if (b == (UBits)1127743488L)
msg = "not compiled with SWAPPED_DOUBLE";
#endif
if (BAD_SAR)
msg = "arithmetic right-shift broken";
luaL_error(L, "bit library self-test failed (%s)", msg);
}
#if LUA_VERSION_NUM < 502
luaL_register(L, "bit", bit_funcs);
#else
luaL_newlib(L, bit_funcs);
#endif
return 1;
}

32
src/cat/LuaBit.h Normal file
View File

@ -0,0 +1,32 @@
/*
** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
** http://bitop.luajit.org/
**
** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
*/
#define LUA_BITOP_VERSION "1.0.2"
int luaopen_bit(lua_State *L);

View File

@ -8,6 +8,8 @@
#include <string> #include <string>
#include <iomanip> #include <iomanip>
#include <vector> #include <vector>
#include <algorithm>
#include <locale>
#include "Config.h" #include "Config.h"
#include "Format.h" #include "Format.h"
#include "LuaScriptInterface.h" #include "LuaScriptInterface.h"
@ -21,6 +23,8 @@
#include "LuaScriptHelper.h" #include "LuaScriptHelper.h"
#include "client/HTTP.h" #include "client/HTTP.h"
#include "LuaBit.h"
#ifdef WIN #ifdef WIN
#include <direct.h> #include <direct.h>
#else #else
@ -42,8 +46,10 @@ LuaScriptInterface::LuaScriptInterface(GameModel * m):
//New TPT API //New TPT API
l = lua_open(); l = lua_open();
luaL_openlibs(l); luaL_openlibs(l);
luaopen_bit(l);
initRendererAPI(); initRendererAPI();
initElementsAPI();
//Old TPT API //Old TPT API
int i = 0, j; int i = 0, j;
@ -255,10 +261,10 @@ void LuaScriptInterface::initRendererAPI()
{ {
//Methods //Methods
struct luaL_reg rendererAPIMethods [] = { struct luaL_reg rendererAPIMethods [] = {
{"renderModes", luatpt_renderer_renderModes}, {"renderModes", renderer_renderModes},
{"displayModes", luatpt_renderer_displayModes}, {"displayModes", renderer_displayModes},
{"colourMode", luatpt_renderer_colourMode}, {"colourMode", renderer_colourMode},
{"colorMode", luatpt_renderer_colourMode}, //Duplicate of above to make americans happy {"colorMode", renderer_colourMode}, //Duplicate of above to make americans happy
{NULL, NULL} {NULL, NULL}
}; };
luaL_register(l, "renderer", rendererAPIMethods); luaL_register(l, "renderer", rendererAPIMethods);
@ -314,7 +320,7 @@ void LuaScriptInterface::initRendererAPI()
} }
//get/set render modes list //get/set render modes list
int LuaScriptInterface::luatpt_renderer_renderModes(lua_State * l) int LuaScriptInterface::renderer_renderModes(lua_State * l)
{ {
int args = lua_gettop(l); int args = lua_gettop(l);
if(args) if(args)
@ -347,7 +353,7 @@ int LuaScriptInterface::luatpt_renderer_renderModes(lua_State * l)
} }
} }
int LuaScriptInterface::luatpt_renderer_displayModes(lua_State * l) int LuaScriptInterface::renderer_displayModes(lua_State * l)
{ {
int args = lua_gettop(l); int args = lua_gettop(l);
if(args) if(args)
@ -380,7 +386,7 @@ int LuaScriptInterface::luatpt_renderer_displayModes(lua_State * l)
} }
} }
int LuaScriptInterface::luatpt_renderer_colourMode(lua_State * l) int LuaScriptInterface::renderer_colourMode(lua_State * l)
{ {
int args = lua_gettop(l); int args = lua_gettop(l);
if(args) if(args)
@ -398,6 +404,126 @@ int LuaScriptInterface::luatpt_renderer_colourMode(lua_State * l)
return luaL_error(l, "Not implemented"); return luaL_error(l, "Not implemented");
} }
void LuaScriptInterface::initElementsAPI()
{
//Methods
struct luaL_reg elementsAPIMethods [] = {
{"allocate", elements_allocate},
{"free", elements_free},
{NULL, NULL}
};
luaL_register(l, "elements", elementsAPIMethods);
int elementsAPI = lua_gettop(l);
//Static values
//Element types/properties/states
lua_pushinteger(l, TYPE_PART); lua_setfield(l, elementsAPI, "TYPE_PART");
lua_pushinteger(l, TYPE_LIQUID); lua_setfield(l, elementsAPI, "TYPE_LIQUID");
lua_pushinteger(l, TYPE_SOLID); lua_setfield(l, elementsAPI, "TYPE_SOLID");
lua_pushinteger(l, TYPE_GAS); lua_setfield(l, elementsAPI, "TYPE_GAS");
lua_pushinteger(l, TYPE_ENERGY); lua_setfield(l, elementsAPI, "TYPE_ENERGY");
lua_pushinteger(l, PROP_CONDUCTS); lua_setfield(l, elementsAPI, "PROP_CONDUCTS");
lua_pushinteger(l, PROP_BLACK); lua_setfield(l, elementsAPI, "PROP_BLACK");
lua_pushinteger(l, PROP_NEUTPENETRATE); lua_setfield(l, elementsAPI, "PROP_NEUTPENETRATE");
lua_pushinteger(l, PROP_NEUTABSORB); lua_setfield(l, elementsAPI, "PROP_NEUTABSORB");
lua_pushinteger(l, PROP_NEUTPASS); lua_setfield(l, elementsAPI, "PROP_NEUTPASS");
lua_pushinteger(l, PROP_DEADLY); lua_setfield(l, elementsAPI, "PROP_DEADLY");
lua_pushinteger(l, PROP_HOT_GLOW); lua_setfield(l, elementsAPI, "PROP_HOT_GLOW");
lua_pushinteger(l, PROP_LIFE); lua_setfield(l, elementsAPI, "PROP_LIFE");
lua_pushinteger(l, PROP_RADIOACTIVE); lua_setfield(l, elementsAPI, "PROP_RADIOACTIVE");
lua_pushinteger(l, PROP_LIFE_DEC); lua_setfield(l, elementsAPI, "PROP_LIFE_DEC");
lua_pushinteger(l, PROP_LIFE_KILL); lua_setfield(l, elementsAPI, "PROP_LIFE_KILL");
lua_pushinteger(l, PROP_LIFE_KILL_DEC); lua_setfield(l, elementsAPI, "PROP_LIFE_KILL_DEC");
lua_pushinteger(l, PROP_SPARKSETTLE); lua_setfield(l, elementsAPI, "PROP_SPARKSETTLE");
lua_pushinteger(l, PROP_NOAMBHEAT); lua_setfield(l, elementsAPI, "PROP_NOAMBHEAT");
lua_pushinteger(l, FLAG_STAGNANT); lua_setfield(l, elementsAPI, "FLAG_STAGNANT");
lua_pushinteger(l, FLAG_SKIPMOVE); lua_setfield(l, elementsAPI, "FLAG_SKIPMOVE");
lua_pushinteger(l, FLAG_MOVABLE); lua_setfield(l, elementsAPI, "FLAG_MOVABLE");
lua_pushinteger(l, ST_NONE); lua_setfield(l, elementsAPI, "ST_NONE");
lua_pushinteger(l, ST_SOLID); lua_setfield(l, elementsAPI, "ST_SOLID");
lua_pushinteger(l, ST_LIQUID); lua_setfield(l, elementsAPI, "ST_LIQUID");
lua_pushinteger(l, ST_GAS); lua_setfield(l, elementsAPI, "ST_GAS");
//Element identifiers
for(int i = 0; i < PT_NUM; i++)
{
if(luacon_sim->elements[i].Enabled)
{
lua_pushinteger(l, i);
lua_setfield(l, elementsAPI, luacon_sim->elements[i].Identifier);
}
}
}
int LuaScriptInterface::elements_allocate(lua_State * l)
{
std::string group, id, identifier;
luaL_checktype(l, 1, LUA_TSTRING);
luaL_checktype(l, 2, LUA_TSTRING);
group = std::string(lua_tostring(l, 1));
std::transform(group.begin(), group.end(), group.begin(), ::toupper);
id = std::string(lua_tostring(l, 2));
std::transform(id.begin(), id.end(), id.begin(), ::toupper);
if(group == "DEFAULT")
return luaL_error(l, "You cannot create elements in the 'default' group.");
identifier = group + "_PT_" + id;
for(int i = 0; i < PT_NUM; i++)
{
if(luacon_sim->elements[i].Enabled && std::string(luacon_sim->elements[i].Identifier) == identifier)
return luaL_error(l, "Element identifier already in use");
}
int newID = -1;
for(int i = PT_NUM-1; i >= 0; i--)
{
if(!luacon_sim->elements[i].Enabled)
{
newID = i;
luacon_sim->elements[i] = Element();
luacon_sim->elements[i].Enabled = true;
luacon_sim->elements[i].Identifier = strdup(identifier.c_str());
break;
}
}
if(newID != -1)
{
lua_getglobal(l, "elements");
lua_pushinteger(l, newID);
lua_setfield(l, -2, identifier.c_str());
lua_pop(l, 1);
}
lua_pushinteger(l, newID);
return 1;
}
int LuaScriptInterface::elements_free(lua_State * l)
{
int id;
luaL_checktype(l, 1, LUA_TNUMBER);
id = lua_tointeger(l, 1);
if(id < 0 || id >= PT_NUM || !luacon_sim->elements[id].Enabled)
return luaL_error(l, "Invalid element");
std::string identifier = luacon_sim->elements[id].Identifier;
if(identifier.length()>7 && identifier.substr(0, 7) == "DEFAULT")
return luaL_error(l, "Cannot free default elements");
luacon_sim->elements[id].Enabled = false;
lua_getglobal(l, "elements");
lua_pushnil(l);
lua_setfield(l, -2, identifier.c_str());
lua_pop(l, 1);
return 0;
}
bool LuaScriptInterface::OnBrushChanged(int brushType, int rx, int ry) bool LuaScriptInterface::OnBrushChanged(int brushType, int rx, int ry)
{ {

View File

@ -42,9 +42,14 @@ class LuaScriptInterface: public CommandInterface {
//Renderer //Renderer
void initRendererAPI(); void initRendererAPI();
static int luatpt_renderer_renderModes(lua_State * l); static int renderer_renderModes(lua_State * l);
static int luatpt_renderer_displayModes(lua_State * l); static int renderer_displayModes(lua_State * l);
static int luatpt_renderer_colourMode(lua_State * l); static int renderer_colourMode(lua_State * l);
//Elements
void initElementsAPI();
static int elements_allocate(lua_State * l);
static int elements_free(lua_State * l);
public: public:
lua_State *l; lua_State *l;
LuaScriptInterface(GameModel * m); LuaScriptInterface(GameModel * m);