rewrite ldtc to be less buggy

This commit is contained in:
krawthekrow
2018-07-10 22:18:36 -04:00
committed by jacob1
parent 1c1bcb0cdc
commit 711d65bbd5

View File

@@ -46,10 +46,14 @@ Element_LDTC::Element_LDTC()
Update = &Element_LDTC::update; Update = &Element_LDTC::update;
} }
const int mask_invert_filter = 0x1; //#TPT-Directive ElementHeader Element_LDTC static const int FLAG_INVERT_FILTER
const int mask_ignore_energy = 0x2; //#TPT-Directive ElementHeader Element_LDTC static const int FLAG_IGNORE_ENERGY
const int mask_no_copy_color = 0x4; //#TPT-Directive ElementHeader Element_LDTC static const int FLAG_NO_COPY_COLOR
const int mask_keep_searching = 0x8; //#TPT-Directive ElementHeader Element_LDTC static const int FLAG_KEEP_SEARCHING
const int Element_LDTC::FLAG_INVERT_FILTER = 0x1;
const int Element_LDTC::FLAG_IGNORE_ENERGY = 0x2;
const int Element_LDTC::FLAG_NO_COPY_COLOR = 0x4;
const int Element_LDTC::FLAG_KEEP_SEARCHING = 0x8;
//NOTES: //NOTES:
// ctype is used to store the target element, if any. (NONE is treated as a wildcard) // ctype is used to store the target element, if any. (NONE is treated as a wildcard)
@@ -62,31 +66,37 @@ const int mask_keep_searching = 0x8;
// 0x08: Keep searching even after finding a particle // 0x08: Keep searching even after finding a particle
//#TPT-Directive ElementHeader Element_LDTC static bool phot_data_type(int rt);
/* Returns true for particles that activate the special FILT color copying mode */ /* Returns true for particles that activate the special FILT color copying mode */
bool phot_data_type(int rt) bool Element_LDTC::phot_data_type(int rt)
{ {
if (rt == PT_FILT || rt == PT_PHOT || rt == PT_BRAY) return rt == PT_FILT || rt == PT_PHOT || rt == PT_BRAY;
return true;
return false;
} }
//#TPT-Directive ElementHeader Element_LDTC static bool accepted_conductor(Simulation *sim, int rt);
/* Returns true for particles that start a ray search ("dtec" mode) /* Returns true for particles that start a ray search ("dtec" mode)
*/ */
bool accepted_type(Simulation* sim, int r) bool Element_LDTC::accepted_conductor(Simulation* sim, int r)
{ {
int rt = TYP(r); int rt = TYP(r);
if ((sim->elements[rt].Properties & PROP_CONDUCTS) && !(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT || rt == PT_PTCT || rt == PT_INWR)) return (sim->elements[rt].Properties & PROP_CONDUCTS) &&
{ !(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT ||
if (sim->parts[ID(r)].life == 0) rt == PT_PTCT || rt == PT_INWR) &&
return true; sim->parts[ID(r)].life == 0;
}
return false;
} }
//#TPT-Directive ElementHeader Element_LDTC static int update(UPDATE_FUNC_ARGS) //#TPT-Directive ElementHeader Element_LDTC static int update(UPDATE_FUNC_ARGS)
int Element_LDTC::update(UPDATE_FUNC_ARGS) int Element_LDTC::update(UPDATE_FUNC_ARGS)
{ {
int max = parts[i].tmp + parts[i].life; int ctype = TYP(parts[i].ctype), ctypeExtra = ID(parts[i].ctype), detectLength = parts[i].tmp, detectSpaces = parts[i].tmp2;
bool copyColor = !(parts[i].tmp2 & Element_LDTC::FLAG_NO_COPY_COLOR);
bool ignoreEnergy = parts[i].tmp2 & Element_LDTC::FLAG_IGNORE_ENERGY;
bool invertFilter = parts[i].tmp2 & Element_LDTC::FLAG_INVERT_FILTER;
bool keepSearching = parts[i].tmp2 & Element_LDTC::FLAG_KEEP_SEARCHING;
if (detectSpaces < 0)
detectSpaces = parts[i].tmp2 = 0;
if (detectLength < 0)
detectLength = parts[i].tmp = 0;
for (int rx = -1; rx <= 1; rx++) for (int rx = -1; rx <= 1; rx++)
{ {
for (int ry = -1; ry <= 1; ry++) for (int ry = -1; ry <= 1; ry++)
@@ -96,70 +106,68 @@ int Element_LDTC::update(UPDATE_FUNC_ARGS)
int r = pmap[y+ry][x+rx]; int r = pmap[y+ry][x+rx];
if (!r) if (!r)
continue; continue;
bool boolMode = Element_LDTC::accepted_conductor(sim, r);
if (!accepted_type(sim, r) && ((parts[i].tmp2 & mask_no_copy_color) || !phot_data_type(TYP(r)))) bool filtMode = copyColor && TYP(r) == PT_FILT;
if (!boolMode && !filtMode)
continue; continue;
// Stolen from DRAY, does the ray searching int maxRange = parts[i].life + parts[i].tmp;
int xStep = rx * -1, yStep = ry * -1; int xStep = rx * -1, yStep = ry * -1;
int xCurrent = x + (xStep * (parts[i].life + 1)), yCurrent = y + (yStep * (parts[i].life + 1)); int xCurrent = x + (xStep * (parts[i].life + 1)), yCurrent = y + (yStep * (parts[i].life + 1));
for (;(parts[i].tmp == 0) || !(xCurrent - x >= max) || (yCurrent-y >= max); xCurrent += xStep, yCurrent += yStep) for (; !parts[i].tmp ||
(xStep * (xCurrent - x) <= maxRange &&
yStep * (yCurrent - y) <= maxRange);
xCurrent += xStep, yCurrent += yStep)
{ {
int rr = pmap[yCurrent][xCurrent];
if (!(xCurrent>=0 && yCurrent>=0 && xCurrent<XRES && yCurrent<YRES)) if (!(xCurrent>=0 && yCurrent>=0 && xCurrent<XRES && yCurrent<YRES))
{
break; // We're out of bounds! Oops! break; // We're out of bounds! Oops!
} int rr = pmap[yCurrent][xCurrent];
if (!rr) if (!rr && !ignoreEnergy)
{
rr = sim->photons[yCurrent][xCurrent]; rr = sim->photons[yCurrent][xCurrent];
if (!(rr && !(parts[i].tmp2 & mask_ignore_energy)))
{
continue;
}
}
if (!rr) if (!rr)
continue; continue;
// Usual DTEC-like mode // If ctype isn't set (no type restriction), or ctype matches what we found
if (!phot_data_type(TYP(r))) // Can use .tmp2 flag to invert this
bool matchesCtype = parts[i].ctype == TYP(rr) && (ctype != PT_LIFE || parts[ID(rr)].ctype == ctypeExtra);
bool matchesFilter = !ctype || (invertFilter ^ (int)matchesCtype);
if (!matchesFilter)
{ {
// If ctype isn't set (no type restriction), or ctype matches what we found if (keepSearching)
// Can use .tmp2 flag to invert this continue;
if (parts[i].ctype == 0 || (parts[i].ctype == TYP(rr)) ^ (parts[i].tmp2 & mask_invert_filter)) else
{
parts[ID(r)].life = 4;
parts[ID(r)].ctype = TYP(r);
sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
break; break;
}
// room for more conditions here.
} }
// FILT color copying mode // room for more conditions here.
else
if (boolMode)
{ {
// If ctype isn't set (no type restriction), or ctype matches what we found parts[ID(r)].life = 4;
// Can use .tmp2 flag to invert this parts[ID(r)].ctype = TYP(r);
if (parts[i].ctype == 0 || (parts[i].ctype == TYP(rr)) ^ (parts[i].tmp2 & mask_invert_filter)) sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
{
if (phot_data_type(TYP(rr)))
{
int nx = x + rx, ny = y + ry;
while (TYP(r) == PT_FILT)
{
parts[ID(r)].ctype = Element_FILT::getWavelengths(&parts[ID(rr)]);
nx += rx;
ny += ry;
if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES)
break;
r = pmap[ny][nx];
}
break;
}
}
}
if (!(parts[i].tmp2 & mask_keep_searching))
break; break;
}
if (filtMode)
{
if (!Element_LDTC::phot_data_type(TYP(rr)))
continue;
int nx = x + rx, ny = y + ry;
int photonWl = TYP(rr) == PT_FILT ?
Element_FILT::getWavelengths(&parts[ID(rr)]) :
parts[ID(rr)].ctype;
while (TYP(r) == PT_FILT)
{
parts[ID(r)].ctype = photonWl;
nx += rx;
ny += ry;
if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES)
break;
r = pmap[ny][nx];
}
break;
}
} }
} }
} }