Skip to content

Commit

Permalink
rewrite ldtc to be less buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
krawthekrow authored and jacob1 committed Jul 22, 2018
1 parent 1c1bcb0 commit 711d65b
Showing 1 changed file with 69 additions and 61 deletions.
130 changes: 69 additions & 61 deletions src/simulation/elements/LDTC.cpp
Expand Up @@ -46,10 +46,14 @@ Element_LDTC::Element_LDTC()
Update = &Element_LDTC::update;
}

const int mask_invert_filter = 0x1;
const int mask_ignore_energy = 0x2;
const int mask_no_copy_color = 0x4;
const int mask_keep_searching = 0x8;
//#TPT-Directive ElementHeader Element_LDTC static const int FLAG_INVERT_FILTER
//#TPT-Directive ElementHeader Element_LDTC static const int FLAG_IGNORE_ENERGY
//#TPT-Directive ElementHeader Element_LDTC static const int FLAG_NO_COPY_COLOR
//#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:
// ctype is used to store the target element, if any. (NONE is treated as a wildcard)
Expand All @@ -62,31 +66,37 @@ const int mask_keep_searching = 0x8;
// 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 */
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 true;
return false;
return rt == PT_FILT || rt == PT_PHOT || rt == PT_BRAY;
}

//#TPT-Directive ElementHeader Element_LDTC static bool accepted_conductor(Simulation *sim, int rt);
/* 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);
if ((sim->elements[rt].Properties & PROP_CONDUCTS) && !(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT || rt == PT_PTCT || rt == PT_INWR))
{
if (sim->parts[ID(r)].life == 0)
return true;
}
return false;
return (sim->elements[rt].Properties & PROP_CONDUCTS) &&
!(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT ||
rt == PT_PTCT || rt == PT_INWR) &&
sim->parts[ID(r)].life == 0;
}

//#TPT-Directive ElementHeader Element_LDTC static int 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 ry = -1; ry <= 1; ry++)
Expand All @@ -96,70 +106,68 @@ int Element_LDTC::update(UPDATE_FUNC_ARGS)
int r = pmap[y+ry][x+rx];
if (!r)
continue;

if (!accepted_type(sim, r) && ((parts[i].tmp2 & mask_no_copy_color) || !phot_data_type(TYP(r))))
bool boolMode = Element_LDTC::accepted_conductor(sim, r);
bool filtMode = copyColor && TYP(r) == PT_FILT;
if (!boolMode && !filtMode)
continue;

// Stolen from DRAY, does the ray searching
int maxRange = parts[i].life + parts[i].tmp;
int xStep = rx * -1, yStep = ry * -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))
{
break; // We're out of bounds! Oops!
}
if (!rr)
{
int rr = pmap[yCurrent][xCurrent];
if (!rr && !ignoreEnergy)
rr = sim->photons[yCurrent][xCurrent];
if (!(rr && !(parts[i].tmp2 & mask_ignore_energy)))
{
continue;
}
}
if (!rr)
continue;

// Usual DTEC-like mode
if (!phot_data_type(TYP(r)))
// If ctype isn't set (no type restriction), or ctype matches what we found
// 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
// Can use .tmp2 flag to invert this
if (parts[i].ctype == 0 || (parts[i].ctype == TYP(rr)) ^ (parts[i].tmp2 & mask_invert_filter))
{
parts[ID(r)].life = 4;
parts[ID(r)].ctype = TYP(r);
sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
if (keepSearching)
continue;
else
break;
}
// room for more conditions here.
}
// FILT color copying mode
else
// room for more conditions here.

if (boolMode)
{
parts[ID(r)].life = 4;
parts[ID(r)].ctype = TYP(r);
sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
break;
}

if (filtMode)
{
// If ctype isn't set (no type restriction), or ctype matches what we found
// Can use .tmp2 flag to invert this
if (parts[i].ctype == 0 || (parts[i].ctype == TYP(rr)) ^ (parts[i].tmp2 & mask_invert_filter))
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)
{
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];
}
parts[ID(r)].ctype = photonWl;
nx += rx;
ny += ry;
if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES)
break;
}
r = pmap[ny][nx];
}
}
if (!(parts[i].tmp2 & mask_keep_searching))
break;
}
}
}
}
Expand Down

0 comments on commit 711d65b

Please sign in to comment.