Skip to content

Commit

Permalink
Revise ctype-drawing (fixes #657)
Browse files Browse the repository at this point in the history
  • Loading branch information
LBPHacker committed Jul 27, 2019
1 parent a73d1f9 commit de1fc0f
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 64 deletions.
62 changes: 55 additions & 7 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -75,7 +75,7 @@ String *luacon_lastError;
String lastCode;

int *lua_el_mode;
LuaSmartRef *lua_el_func, *lua_gr_func;
LuaSmartRef *lua_el_func, *lua_gr_func, *lua_cd_func;

int getPartIndex_curIdx;
int tptProperties; //Table for some TPT properties
Expand Down Expand Up @@ -332,12 +332,14 @@ tpt.partsdata = nil");
}
lua_setfield(l, tptProperties, "eltransition");

lua_cd_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
lua_cd_func = &lua_cd_func_v[0];
lua_gr_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
lua_gr_func = &lua_gr_func_v[0];
lua_el_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
lua_el_func = &lua_el_func_v[0];
lua_el_mode = new int[PT_NUM];
std::fill(lua_el_mode, lua_el_mode + PT_NUM, 0);
lua_el_mode_v = std::vector<int>(PT_NUM, 0);
lua_el_mode = &lua_el_mode_v[0];

//make tpt.* a metatable
lua_newtable(l);
Expand Down Expand Up @@ -2426,7 +2428,6 @@ void LuaScriptInterface::initElementsAPI()
SETCONST(l, PROP_LIFE_KILL_DEC);
SETCONST(l, PROP_SPARKSETTLE);
SETCONST(l, PROP_NOAMBHEAT);
SETCONST(l, PROP_DRAWONCTYPE);
SETCONST(l, PROP_NOCTYPEDRAW);
SETCONST(l, FLAG_STAGNANT);
SETCONST(l, FLAG_SKIPMOVE);
Expand Down Expand Up @@ -2679,6 +2680,25 @@ int LuaScriptInterface::elements_allocate(lua_State * l)
return 1;
}

static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)
{
bool ret = false;
if (lua_cd_func[sim->parts[i].type])
{
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, lua_cd_func[sim->parts[i].type]);
lua_pushinteger(luacon_ci->l, i);
lua_pushinteger(luacon_ci->l, t);
lua_pushinteger(luacon_ci->l, v);
if (lua_pcall(luacon_ci->l, 3, 1, 0))
{
luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
}
ret = luaL_optinteger(luacon_ci->l, -1, 0);
lua_pop(luacon_ci->l, 1);
}
return ret;
}

int LuaScriptInterface::elements_element(lua_State * l)
{
int id = luaL_checkinteger(l, 1);
Expand Down Expand Up @@ -2712,7 +2732,7 @@ int LuaScriptInterface::elements_element(lua_State * l)
{
lua_el_func[id].Clear();
lua_el_mode[id] = 0;
luacon_sim->elements[id].Update = NULL;
luacon_sim->elements[id].Update = nullptr;
}
lua_pop(l, 1);

Expand All @@ -2724,7 +2744,20 @@ int LuaScriptInterface::elements_element(lua_State * l)
else if (lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
{
lua_gr_func[id].Clear();
luacon_sim->elements[id].Graphics = NULL;
luacon_sim->elements[id].Graphics = nullptr;
}
lua_pop(l, 1);

lua_getfield(l, -1, "CtypeDraw");
if (lua_type(l, -1) == LUA_TFUNCTION)
{
lua_cd_func[id].Assign(-1);
luacon_sim->elements[id].CtypeDraw = luaCtypeDrawWrapper;
}
else if (lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
{
lua_cd_func[id].Clear();
luacon_sim->elements[id].CtypeDraw = nullptr;
}
lua_pop(l, 1);

Expand Down Expand Up @@ -2830,6 +2863,20 @@ int LuaScriptInterface::elements_property(lua_State * l)
luacon_ren->graphicscache[id].isready = 0;
return 0;
}
else if (propertyName == "CtypeDraw")
{
if (lua_type(l, 3) == LUA_TFUNCTION)
{
lua_cd_func[id].Assign(3);
luacon_sim->elements[id].CtypeDraw = luaCtypeDrawWrapper;
}
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
{
lua_cd_func[id].Clear();
luacon_sim->elements[id].CtypeDraw = nullptr;
}
return 0;
}
else
{
return luaL_error(l, "Invalid element property");
Expand Down Expand Up @@ -3732,9 +3779,10 @@ LuaScriptInterface::~LuaScriptInterface() {
component_and_ref.second.Clear();
component_and_ref.first->owner_ref = component_and_ref.second;
}
delete[] lua_el_mode;
lua_el_mode_v.clear();
lua_el_func_v.clear();
lua_gr_func_v.clear();
lua_cd_func_v.clear();
lua_close(l);
delete legacy;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lua/LuaScriptInterface.h
Expand Up @@ -180,7 +180,8 @@ class LuaScriptInterface: public CommandInterface
static int event_unregister(lua_State * l);
static int event_getmodifiers(lua_State * l);

std::vector<LuaSmartRef> lua_el_func_v, lua_gr_func_v;
std::vector<LuaSmartRef> lua_el_func_v, lua_gr_func_v, lua_cd_func_v;
std::vector<int> lua_el_mode_v;

public:
int tpt_index(lua_State *l);
Expand Down
4 changes: 3 additions & 1 deletion src/simulation/ElementDefs.h
Expand Up @@ -30,7 +30,6 @@
#define PROP_LIFE_KILL_DEC 0x10000 //2^16 Kill when life value is decremented to <= zero
#define PROP_SPARKSETTLE 0x20000 //2^17 Allow Sparks/Embers to settle
#define PROP_NOAMBHEAT 0x40000 //2^18 Don't transfer or receive heat from ambient heat.
#define PROP_DRAWONCTYPE 0x80000 //2^19 Set its ctype to another element if the element is drawn upon it (like what CLNE does)
#define PROP_NOCTYPEDRAW 0x100000 // 2^20 When this element is drawn upon with, do not set ctype (like BCLN for CLNE)

#define FLAG_STAGNANT 0x1
Expand All @@ -46,6 +45,9 @@
#define GRAPHICS_FUNC_ARGS Renderer * ren, Particle *cpart, int nx, int ny, int *pixel_mode, int* cola, int *colr, int *colg, int *colb, int *firea, int *firer, int *fireg, int *fireb
#define GRAPHICS_FUNC_SUBCALL_ARGS ren, cpart, nx, ny, pixel_mode, cola, colr, colg, colb, firea, firer, fireg, fireb

#define CTYPEDRAW_FUNC_ARGS Simulation *sim, int i, int t, int v
#define CTYPEDRAW_FUNC_SUBCALL_ARGS sim, i, t, v

#define BOUNDS_CHECK true

#define OLD_PT_WIND 147
Expand Down
43 changes: 2 additions & 41 deletions src/simulation/Simulation.cpp
Expand Up @@ -3113,11 +3113,6 @@ int Simulation::create_part(int p, int x, int y, int t, int v)
parts[index].ctype = PT_DUST;
return index;
}
if (p==-2 && ((elements[type].Properties & PROP_DRAWONCTYPE) || type==PT_CRAY))
{
parts[index].ctype = PT_SPRK;
return index;
}
if (!(type == PT_INST || (elements[type].Properties&PROP_CONDUCTS)) || parts[index].life!=0)
return -1;
if (p == -2 && type == PT_INST)
Expand Down Expand Up @@ -3163,44 +3158,10 @@ int Simulation::create_part(int p, int x, int y, int t, int v)
{
if (pmap[y][x])
{
//If an element has the PROP_DRAWONCTYPE property, and the element being drawn to it does not have PROP_NOCTYPEDRAW (Also some special cases), set the element's ctype
int drawOn = TYP(pmap[y][x]);
if (drawOn == t)
return -1;
if (((elements[drawOn].Properties & PROP_DRAWONCTYPE) ||
(drawOn == PT_STOR && !(elements[t].Properties & TYPE_SOLID)) ||
(drawOn == PT_PCLN && t != PT_PSCN && t != PT_NSCN) ||
(drawOn == PT_PBCN && t != PT_PSCN && t != PT_NSCN))
&& (!(elements[t].Properties & PROP_NOCTYPEDRAW)))
{
parts[ID(pmap[y][x])].ctype = t;
if (t == PT_LIFE && v >= 0 && v < NGOL)
{
if (drawOn == PT_CONV)
parts[ID(pmap[y][x])].ctype |= PMAPID(v);
else if (drawOn != PT_STOR)
parts[ID(pmap[y][x])].tmp = v;
}
}
else if (drawOn == PT_DTEC || (drawOn == PT_PSTN && t != PT_FRME) || drawOn == PT_DRAY)
{
parts[ID(pmap[y][x])].ctype = t;
if (t == PT_LIFE && v >= 0 && v < NGOL)
{
if (drawOn == PT_DTEC)
parts[ID(pmap[y][x])].tmp = v;
else if (drawOn == PT_DRAY)
parts[ID(pmap[y][x])].ctype |= PMAPID(v);
}
}
else if (drawOn == PT_CRAY)
if (elements[drawOn].CtypeDraw)
{
parts[ID(pmap[y][x])].ctype = t;
if (t == PT_LIFE && v >= 0 && v < NGOL)
parts[ID(pmap[y][x])].ctype |= PMAPID(v);
if (t == PT_LIGH)
parts[ID(pmap[y][x])].ctype |= PMAPID(30);
parts[ID(pmap[y][x])].temp = elements[t].Temperature;
elements[drawOn].CtypeDraw(this, ID(pmap[y][x]), t, v);
}
return -1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/simulation/elements/BCLN.cpp
Expand Up @@ -30,7 +30,7 @@ Element_BCLN::Element_BCLN()
HeatConduct = 251;
Description = "Breakable Clone.";

Properties = TYPE_SOLID|PROP_LIFE_DEC|PROP_LIFE_KILL_DEC|PROP_DRAWONCTYPE|PROP_NOCTYPEDRAW;
Properties = TYPE_SOLID | PROP_LIFE_DEC | PROP_LIFE_KILL_DEC | PROP_NOCTYPEDRAW;

LowPressure = IPL;
LowPressureTransition = NT;
Expand All @@ -42,6 +42,7 @@ Element_BCLN::Element_BCLN()
HighTemperatureTransition = NT;

Update = &Element_BCLN::update;
CtypeDraw = &Element::ctypeDrawVInTmp;
}

#define ADVECTION 0.1f
Expand Down
4 changes: 2 additions & 2 deletions src/simulation/elements/CLNE.cpp
Expand Up @@ -30,7 +30,7 @@ Element_CLNE::Element_CLNE()
HeatConduct = 251;
Description = "Clone. Duplicates any particles it touches.";

Properties = TYPE_SOLID|PROP_DRAWONCTYPE|PROP_NOCTYPEDRAW;
Properties = TYPE_SOLID | PROP_NOCTYPEDRAW;

LowPressure = IPL;
LowPressureTransition = NT;
Expand All @@ -42,6 +42,7 @@ Element_CLNE::Element_CLNE()
HighTemperatureTransition = NT;

Update = &Element_CLNE::update;
CtypeDraw = &Element::ctypeDrawVInTmp;
}

//#TPT-Directive ElementHeader Element_CLNE static int update(UPDATE_FUNC_ARGS)
Expand Down Expand Up @@ -87,5 +88,4 @@ int Element_CLNE::update(UPDATE_FUNC_ARGS)
return 0;
}


Element_CLNE::~Element_CLNE() {}
4 changes: 2 additions & 2 deletions src/simulation/elements/CONV.cpp
Expand Up @@ -30,7 +30,7 @@ Element_CONV::Element_CONV()
HeatConduct = 251;
Description = "Converter. Converts everything into whatever it first touches.";

Properties = TYPE_SOLID|PROP_DRAWONCTYPE|PROP_NOCTYPEDRAW;
Properties = TYPE_SOLID | PROP_NOCTYPEDRAW;

LowPressure = IPL;
LowPressureTransition = NT;
Expand All @@ -42,6 +42,7 @@ Element_CONV::Element_CONV()
HighTemperatureTransition = NT;

Update = &Element_CONV::update;
CtypeDraw = &Element::ctypeDrawVInCtype;
}

//#TPT-Directive ElementHeader Element_CONV static int update(UPDATE_FUNC_ARGS)
Expand Down Expand Up @@ -93,5 +94,4 @@ int Element_CONV::update(UPDATE_FUNC_ARGS)
return 0;
}


Element_CONV::~Element_CONV() {}
15 changes: 15 additions & 0 deletions src/simulation/elements/CRAY.cpp
Expand Up @@ -42,6 +42,7 @@ Element_CRAY::Element_CRAY()
HighTemperatureTransition = NT;

Update = &Element_CRAY::update;
CtypeDraw = &Element_CRAY::ctypeDraw;
}

//#TPT-Directive ElementHeader Element_CRAY static int update(UPDATE_FUNC_ARGS)
Expand Down Expand Up @@ -154,5 +155,19 @@ unsigned int Element_CRAY::wavelengthToDecoColour(int wavelength)
return (255<<24) | (colr<<16) | (colg<<8) | colb;
}

//#TPT-Directive ElementHeader Element_CRAY static bool ctypeDraw(CTYPEDRAW_FUNC_ARGS)
bool Element_CRAY::ctypeDraw(CTYPEDRAW_FUNC_ARGS)
{
if (!Element::ctypeDrawVInCtype(CTYPEDRAW_FUNC_SUBCALL_ARGS))
{
return false;
}
if (t == PT_LIGH)
{
sim->parts[i].ctype |= PMAPID(30);
}
sim->parts[i].temp = sim->elements[t].Temperature;
return true;
}

Element_CRAY::~Element_CRAY() {}
5 changes: 3 additions & 2 deletions src/simulation/elements/DRAY.cpp
Expand Up @@ -30,7 +30,7 @@ Element_DRAY::Element_DRAY()
HeatConduct = 0;
Description = "Duplicator ray. Replicates a line of particles in front of it.";

Properties = TYPE_SOLID|PROP_LIFE_DEC;
Properties = TYPE_SOLID | PROP_LIFE_DEC;

LowPressure = IPL;
LowPressureTransition = NT;
Expand All @@ -42,7 +42,8 @@ Element_DRAY::Element_DRAY()
HighTemperatureTransition = NT;

Update = &Element_DRAY::update;
Graphics = NULL;
Graphics = nullptr;
CtypeDraw = &Element::ctypeDrawVInCtype;
}

//should probably be in Simulation.h
Expand Down
1 change: 1 addition & 0 deletions src/simulation/elements/DTEC.cpp
Expand Up @@ -42,6 +42,7 @@ Element_DTEC::Element_DTEC()
HighTemperatureTransition = NT;

Update = &Element_DTEC::update;
CtypeDraw = &Element::ctypeDrawVInTmp;
}

//#TPT-Directive ElementHeader Element_DTEC static int update(UPDATE_FUNC_ARGS)
Expand Down
41 changes: 39 additions & 2 deletions src/simulation/elements/Element.cpp
Expand Up @@ -42,9 +42,10 @@ Element::Element():
HighTemperature(ITH),
HighTemperatureTransition(NT),

Update(NULL),
Update(nullptr),
Graphics(&Element::defaultGraphics),
IconGenerator(NULL)
CtypeDraw(nullptr),
IconGenerator(nullptr)
{
}

Expand Down Expand Up @@ -229,3 +230,39 @@ int Element::defaultGraphics(GRAPHICS_FUNC_ARGS)
}
return 1;
}

bool Element::basicCtypeDraw(CTYPEDRAW_FUNC_ARGS)
{
if (sim->parts[i].type == t || sim->elements[t].Properties & PROP_NOCTYPEDRAW)
{
return false;
}
sim->parts[i].ctype = t;
return true;
}

bool Element::ctypeDrawVInTmp(CTYPEDRAW_FUNC_ARGS)
{
if (!Element::basicCtypeDraw(CTYPEDRAW_FUNC_SUBCALL_ARGS))
{
return false;
}
if (t == PT_LIFE && v >= 0 && v < NGOL)
{
sim->parts[i].tmp = v;
}
return true;
}

bool Element::ctypeDrawVInCtype(CTYPEDRAW_FUNC_ARGS)
{
if (!Element::basicCtypeDraw(CTYPEDRAW_FUNC_SUBCALL_ARGS))
{
return false;
}
if (t == PT_LIFE && v >= 0 && v < NGOL)
{
sim->parts[i].ctype |= PMAPID(v);
}
return true;
}
5 changes: 5 additions & 0 deletions src/simulation/elements/Element.h
Expand Up @@ -51,12 +51,17 @@ class Element

int (*Update) (UPDATE_FUNC_ARGS);
int (*Graphics) (GRAPHICS_FUNC_ARGS);
bool (*CtypeDraw) (CTYPEDRAW_FUNC_ARGS);

VideoBuffer * (*IconGenerator)(int, int, int);

Element();
virtual ~Element() {}
static int defaultGraphics(GRAPHICS_FUNC_ARGS);
static int legacyUpdate(UPDATE_FUNC_ARGS);
static bool basicCtypeDraw(CTYPEDRAW_FUNC_ARGS);
static bool ctypeDrawVInTmp(CTYPEDRAW_FUNC_ARGS);
static bool ctypeDrawVInCtype(CTYPEDRAW_FUNC_ARGS);

/** Returns a list of properties, their type and offset within the structure that can be changed
by higher-level processes referring to them by name such as Lua or the property tool **/
Expand Down

0 comments on commit de1fc0f

Please sign in to comment.