Permalink
Browse files

Add sim.brush (#350)

  • Loading branch information...
LBPHacker authored and jacob1 committed Nov 7, 2016
1 parent 3a3254f commit 57f5d0f84f6870034d05dcde0bfa01573fc62f0d
Showing with 72 additions and 0 deletions.
  1. +71 −0 src/lua/LuaScriptInterface.cpp
  2. +1 −0 src/lua/LuaScriptInterface.h
@@ -741,6 +741,7 @@ void LuaScriptInterface::initSimulationAPI()
{"ambientAirTemp", simulation_ambientAirTemp},
{"elementCount", simulation_elementCount},
{"can_move", simulation_canMove},
{"brush", simulation_brush},
{"parts", simulation_parts},
{"pmap", simulation_pmap},
{"photons", simulation_photons},
@@ -1903,6 +1904,76 @@ int LuaScriptInterface::simulation_parts(lua_State * l)
return 1;
}
int BrushClosure(lua_State * l)
{
// see Simulation::ToolBrush
int positionX = lua_tointeger(l, lua_upvalueindex(1));
int positionY = lua_tointeger(l, lua_upvalueindex(2));
int radiusX = lua_tointeger(l, lua_upvalueindex(3));
int radiusY = lua_tointeger(l, lua_upvalueindex(4));
int sizeX = lua_tointeger(l, lua_upvalueindex(5));
int sizeY = lua_tointeger(l, lua_upvalueindex(6));
int x = lua_tointeger(l, lua_upvalueindex(7));
int y = lua_tointeger(l, lua_upvalueindex(8));
float strength = lua_tonumber(l, lua_upvalueindex(9));
unsigned char *bitmap = (unsigned char *)lua_touserdata(l, lua_upvalueindex(10));
int yield_x, yield_y;
while (true)
{
if (!(y < sizeY)) return 0;
if (x < sizeX)
{
bool yield_coords = false;
if(bitmap[(y*sizeX)+x] && (positionX+(x-radiusX) >= 0 && positionY+(y-radiusY) >= 0 && positionX+(x-radiusX) < XRES && positionY+(y-radiusY) < YRES))
{
yield_coords = true;
yield_x = positionX+(x-radiusX);
yield_y = positionY+(y-radiusY);
}
x++;
if (yield_coords) break;
}
else
{
x = 0;
y++;
}
}
lua_pushnumber(l, x);
lua_replace(l, lua_upvalueindex(7));
lua_pushnumber(l, y);
lua_replace(l, lua_upvalueindex(8));
lua_pushnumber(l, yield_x);
lua_pushnumber(l, yield_y);
lua_pushnumber(l, strength);
return 3;
}
int LuaScriptInterface::simulation_brush(lua_State * l)
{
// see Simulation::ToolBrush
int positionX = luaL_checkint(l, 1);
int positionY = luaL_checkint(l, 2);
Brush * cBrush = luacon_model->GetBrush();
int radiusX = cBrush->GetRadius().X, radiusY = cBrush->GetRadius().Y, sizeX = cBrush->GetSize().X, sizeY = cBrush->GetSize().Y;
lua_pushnumber(l, positionX);
lua_pushnumber(l, positionY);
lua_pushnumber(l, radiusX);
lua_pushnumber(l, radiusY);
lua_pushnumber(l, sizeX);
lua_pushnumber(l, sizeY);
lua_pushnumber(l, 0);
lua_pushnumber(l, 0);
lua_pushnumber(l, luacon_model->GetToolStrength());
lua_pushlightuserdata(l, cBrush->GetBitmap());
lua_pushcclosure(l, BrushClosure, 10);
return 1;
}
int LuaScriptInterface::simulation_pmap(lua_State * l)
{
int x = luaL_checkint(l, 1);
@@ -102,6 +102,7 @@ class LuaScriptInterface: public CommandInterface
static int simulation_elementCount(lua_State * l);
static int simulation_canMove(lua_State * l);
static int simulation_parts(lua_State * l);
static int simulation_brush(lua_State * l);
static int simulation_pmap(lua_State * l);
static int simulation_photons(lua_State * l);
static int simulation_neighbours(lua_State * l);

0 comments on commit 57f5d0f

Please sign in to comment.