Split Simulation::IsValidElement into IsElement and IsElementOrNone

Allows finer control over whether you want none included or not.
Fixes some invalid things being allowed for element 0 in legacy lua api
Fixes {ctype} signs showing 0 instead of NONE
This commit is contained in:
jacob1 2021-04-11 23:20:21 -04:00
parent 1859125218
commit ffb88259d6
13 changed files with 42 additions and 39 deletions

View File

@ -2,4 +2,6 @@
# It may also be used by mod owners using the starcatcher update server to write changelogs for their mods
# Erase and replace the changelog for every release, do not just append changes to the end
Fix issue where Linux version couldn't be launched from some file managers
Fix air being able to go through walls for a frame after undoing
Show NONE in {ctype} signs instead of 0 when an element has no ctype
Fix some legacy lua api functions allowing you to set properties on element 0

View File

@ -943,7 +943,7 @@ void GameController::Update()
if (activeTool->GetIdentifier().BeginsWith("DEFAULT_PT_"))
{
int sr = activeTool->GetToolID();
if (sr && sim->IsValidElement(sr))
if (sr && sim->IsElementOrNone(sr))
rightSelected = sr;
}
@ -1564,7 +1564,7 @@ bool GameController::IsValidElement(int type)
{
if (gameModel && gameModel->GetSimulation())
{
return (type && gameModel->GetSimulation()->IsValidElement(type));
return (type && gameModel->GetSimulation()->IsElement(type));
}
else
return false;

View File

@ -171,7 +171,7 @@ int luacon_transitionread(lua_State* l)
lua_rawget(l, 1);
int i = lua_tointeger (l, lua_gettop(l));
lua_pop(l, 1);
if (!luacon_sim->IsValidElement(i))
if (!luacon_sim->IsElement(i))
{
return luaL_error(l, "Invalid index");
}
@ -194,7 +194,7 @@ int luacon_transitionwrite(lua_State* l)
lua_rawget(l, 1);
int i = lua_tointeger (l, lua_gettop(l));
lua_pop(l, 1);
if (!luacon_sim->IsValidElement(i))
if (!luacon_sim->IsElement(i))
{
return luaL_error(l, "Invalid index");
}
@ -202,7 +202,7 @@ int luacon_transitionwrite(lua_State* l)
if (prop.Type == StructProperty::TransitionType)
{
int type = luaL_checkinteger(l, 3);
if (!luacon_sim->IsValidElement(type) && type != NT && type != ST)
if (!luacon_sim->IsElementOrNone(type) && type != NT && type != ST)
{
return luaL_error(l, "Invalid element");
}
@ -226,7 +226,7 @@ int luacon_elementread(lua_State* l)
lua_rawget(l, 1);
int i = lua_tointeger (l, lua_gettop(l));
lua_pop(l, 1);
if (!luacon_sim->IsValidElement(i))
if (!luacon_sim->IsElement(i))
{
return luaL_error(l, "Invalid index");
}
@ -249,7 +249,7 @@ int luacon_elementwrite(lua_State* l)
lua_rawget(l, 1);
int i = lua_tointeger (l, lua_gettop(l));
lua_pop(l, 1);
if (!luacon_sim->IsValidElement(i))
if (!luacon_sim->IsElement(i))
{
return luaL_error(l, "Invalid index");
}
@ -302,7 +302,7 @@ int luatpt_getelement(lua_State *l)
if (lua_isnumber(l, 1))
{
t = luaL_optint(l, 1, 1);
if (!luacon_sim->IsValidElement(t))
if (!luacon_sim->IsElementOrNone(t))
{
return luaL_error(l, "Unrecognised element number '%d'", t);
}
@ -347,7 +347,7 @@ int luatpt_element_func(lua_State *l)
{
int element = luaL_optint(l, 2, 0);
int replace = luaL_optint(l, 3, 0);
if (luacon_sim->IsValidElement(element))
if (luacon_sim->IsElement(element))
{
lua_el_func[element].Assign(l, 1);
if (replace == 2)
@ -366,7 +366,7 @@ int luatpt_element_func(lua_State *l)
else if (lua_isnil(l, 1))
{
int element = luaL_optint(l, 2, 0);
if (luacon_sim->IsValidElement(element))
if (luacon_sim->IsElement(element))
{
lua_el_func[element].Clear();
lua_el_mode[element] = 0;
@ -427,7 +427,7 @@ int luatpt_graphics_func(lua_State *l)
if(lua_isfunction(l, 1))
{
int element = luaL_optint(l, 2, 0);
if (luacon_sim->IsValidElement(element))
if (luacon_sim->IsElement(element))
{
lua_gr_func[element].Assign(l, 1);
luacon_ren->graphicscache[element].isready = 0;
@ -441,7 +441,7 @@ int luatpt_graphics_func(lua_State *l)
else if (lua_isnil(l, 1))
{
int element = luaL_optint(l, 2, 0);
if (luacon_sim->IsValidElement(element))
if (luacon_sim->IsElement(element))
{
lua_gr_func[element].Clear();
luacon_ren->graphicscache[element].isready = 0;
@ -500,7 +500,7 @@ int luatpt_create(lua_State* l)
if (lua_isnumber(l, 3))
{
t = luaL_optint(l, 3, 0);
if (!luacon_sim->IsValidElement(t))
if (!luacon_sim->IsElement(t))
{
return luaL_error(l, "Unrecognised element number '%d'", t);
}
@ -730,7 +730,7 @@ int luatpt_set_property(lua_State* l)
else
t = luaL_optint(l, 2, 0);
if (!strcmp(prop, "type") && !luacon_sim->IsValidElement(t))
if (!strcmp(prop, "type") && !luacon_sim->IsElementOrNone(t))
return luaL_error(l, "Unrecognised element number '%d'", t);
}
else if (lua_isstring(l, 2))

View File

@ -2856,7 +2856,7 @@ static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)
int LuaScriptInterface::elements_element(lua_State * l)
{
int id = luaL_checkinteger(l, 1);
if (!luacon_sim->IsValidElement(id))
if (!luacon_sim->IsElementOrNone(id))
{
return luaL_error(l, "Invalid element");
}
@ -3007,7 +3007,7 @@ int LuaScriptInterface::elements_element(lua_State * l)
int LuaScriptInterface::elements_property(lua_State * l)
{
int id = luaL_checkinteger(l, 1);
if (!luacon_sim->IsValidElement(id))
if (!luacon_sim->IsElementOrNone(id))
{
return luaL_error(l, "Invalid element");
}
@ -3027,7 +3027,7 @@ int LuaScriptInterface::elements_property(lua_State * l)
if (prop->Type == StructProperty::TransitionType)
{
int type = luaL_checkinteger(l, 3);
if (!luacon_sim->IsValidElement(type) && type != NT && type != ST)
if (!luacon_sim->IsElementOrNone(type) && type != NT && type != ST)
{
return luaL_error(l, "Invalid element");
}
@ -3188,7 +3188,7 @@ int LuaScriptInterface::elements_property(lua_State * l)
int LuaScriptInterface::elements_free(lua_State * l)
{
int id = luaL_checkinteger(l, 1);
if (!luacon_sim->IsValidElement(id))
if (!luacon_sim->IsElement(id))
{
return luaL_error(l, "Invalid element");
}

View File

@ -85,7 +85,7 @@ String sign::getDisplayText(Simulation *sim, int &x0, int &y0, int &w, int &h, b
}
else if (between_curlies == "ctype")
{
formatted_text << (part ? ((part->ctype && sim->IsValidElement(part->ctype)) ? sim->ElementResolve(part->ctype, -1) : String::Build(part->ctype)) : (formatted_text.Size() ? String::Build("empty") : String::Build("Empty")));
formatted_text << (part ? (sim->IsElementOrNone(part->ctype) ? sim->ElementResolve(part->ctype, -1) : String::Build(part->ctype)) : (formatted_text.Size() ? String::Build("empty") : String::Build("Empty")));
if (v95)
*v95 = true;
}

View File

@ -3651,16 +3651,16 @@ void Simulation::UpdateParticles(int start, int end)
ctemph = ctempl = pt;
// change boiling point with pressure
if (((elements[t].Properties&TYPE_LIQUID) && IsValidElement(elements[t].HighTemperatureTransition) && (elements[elements[t].HighTemperatureTransition].Properties&TYPE_GAS))
if (((elements[t].Properties&TYPE_LIQUID) && IsElementOrNone(elements[t].HighTemperatureTransition) && (elements[elements[t].HighTemperatureTransition].Properties&TYPE_GAS))
|| t==PT_LNTG || t==PT_SLTW)
ctemph -= 2.0f*pv[y/CELL][x/CELL];
else if (((elements[t].Properties&TYPE_GAS) && IsValidElement(elements[t].LowTemperatureTransition) && (elements[elements[t].LowTemperatureTransition].Properties&TYPE_LIQUID))
else if (((elements[t].Properties&TYPE_GAS) && IsElementOrNone(elements[t].LowTemperatureTransition) && (elements[elements[t].LowTemperatureTransition].Properties&TYPE_LIQUID))
|| t==PT_WTRV)
ctempl -= 2.0f*pv[y/CELL][x/CELL];
s = 1;
//A fix for ice with ctype = 0
if ((t==PT_ICEI || t==PT_SNOW) && (!parts[i].ctype || !IsValidElement(parts[i].ctype) || parts[i].ctype==PT_ICEI || parts[i].ctype==PT_SNOW))
if ((t==PT_ICEI || t==PT_SNOW) && (!IsElement(parts[i].ctype) || parts[i].ctype==PT_ICEI || parts[i].ctype==PT_SNOW))
parts[i].ctype = PT_WATR;
if (elements[t].HighTemperatureTransition>-1 && ctemph>=elements[t].HighTemperature)
@ -5317,9 +5317,7 @@ String Simulation::ElementResolve(int type, int ctype)
return SerialiseGOLRule(ctype);
}
else if (type >= 0 && type < PT_NUM)
{
return elements[type].Name;
}
return "Empty";
}
@ -5329,13 +5327,13 @@ String Simulation::BasicParticleInfo(Particle const &sample_part)
int type = sample_part.type;
int ctype = sample_part.ctype;
int pavg1int = (int)sample_part.pavg[1];
if (type == PT_LAVA && ctype && IsValidElement(ctype))
if (type == PT_LAVA && IsElement(ctype))
{
sampleInfo << "Molten " << ElementResolve(ctype, -1);
}
else if ((type == PT_PIPE || type == PT_PPIP) && ctype && IsValidElement(ctype))
else if ((type == PT_PIPE || type == PT_PPIP) && IsElement(ctype))
{
if (ctype == PT_LAVA && pavg1int && IsValidElement(pavg1int))
if (ctype == PT_LAVA && IsElement(pavg1int))
{
sampleInfo << ElementResolve(type, -1) << " with molten " << ElementResolve(pavg1int, -1);
}

View File

@ -134,7 +134,10 @@ public:
int eval_move(int pt, int nx, int ny, unsigned *rr);
void init_can_move();
bool IsWallBlocking(int x, int y, int type);
bool IsValidElement(int type) {
bool IsElement(int type) {
return (type > 0 && type < PT_NUM && elements[type].Enabled);
}
bool IsElementOrNone(int type) {
return (type >= 0 && type < PT_NUM && elements[type].Enabled);
}
void create_cherenkov_photon(int pp);

View File

@ -75,7 +75,7 @@ static int update(UPDATE_FUNC_ARGS)
}
else
{
int restrictElement = sim->IsValidElement(parts[i].tmp) ? parts[i].tmp : 0;
int restrictElement = sim->IsElement(parts[i].tmp) ? parts[i].tmp : 0;
for (rx=-1; rx<2; rx++)
for (ry=-1; ry<2; ry++)
if (x+rx>=0 && y+ry>=0 && x+rx<XRES && y+ry<YRES)

View File

@ -5,7 +5,7 @@ static VideoBuffer *iconGen(int wallID, int width, int height);
void Element::Element_NONE()
{
Identifier = "DEFAULT_PT_NONE";
Name = "";
Name = "NONE";
Colour = PIXPACK(0x000000);
MenuVisible = 1;
MenuSection = SC_SPECIAL;

View File

@ -263,7 +263,7 @@ int Element_PIPE_update(UPDATE_FUNC_ARGS)
transfer_part_to_pipe(parts+(ID(r)), parts+i);
sim->kill_part(ID(r));
}
else if (!TYP(parts[i].ctype) && TYP(r)==PT_STOR && parts[ID(r)].tmp>0 && sim->IsValidElement(parts[ID(r)].tmp) && (sim->elements[parts[ID(r)].tmp].Properties & (TYPE_PART | TYPE_LIQUID | TYPE_GAS | TYPE_ENERGY)))
else if (!TYP(parts[i].ctype) && TYP(r)==PT_STOR && sim->IsElement(parts[ID(r)].tmp) && (sim->elements[parts[ID(r)].tmp].Properties & (TYPE_PART | TYPE_LIQUID | TYPE_GAS | TYPE_ENERGY)))
{
// STOR stores properties in the same places as PIPE does
transfer_pipe_to_pipe(parts+(ID(r)), parts+i, true);

View File

@ -94,7 +94,7 @@ static int update(UPDATE_FUNC_ARGS)
{
if (TYP(r) == PT_STOR)
{
if (sim->IsValidElement(parts[ID(r)].tmp) && (sim->elements[parts[ID(r)].tmp].Properties & (TYPE_PART | TYPE_LIQUID | TYPE_GAS | TYPE_ENERGY)))
if (sim->IsElement(parts[ID(r)].tmp) && (sim->elements[parts[ID(r)].tmp].Properties & (TYPE_PART | TYPE_LIQUID | TYPE_GAS | TYPE_ENERGY)))
{
// STOR uses same format as PIPE, so we can use this function to do the transfer
Element_PIPE_transfer_pipe_to_part(sim, parts+(ID(r)), &sim->portalp[parts[i].tmp][count][nnx], true);

View File

@ -112,7 +112,7 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS)
float rocketBootsHeadEffectV = 0.3f;// stronger acceleration vertically, to counteract gravity
float rocketBootsFeetEffectV = 0.45f;
if (!playerp->fan && parts[i].ctype && sim->IsValidElement(parts[i].ctype))
if (!playerp->fan && parts[i].ctype && sim->IsElementOrNone(parts[i].ctype))
Element_STKM_set_element(sim, playerp, parts[i].ctype);
playerp->frames++;

View File

@ -53,7 +53,7 @@ void Element::Element_STOR()
static int update(UPDATE_FUNC_ARGS)
{
int r, rx, ry, np, rx1, ry1;
if (!sim->IsValidElement(parts[i].tmp))
if (!sim->IsElementOrNone(parts[i].tmp))
parts[i].tmp = 0;
if(parts[i].life && !parts[i].tmp)
parts[i].life--;