Skip to content

Commit 711d65b

Browse files
krawthekrowjacob1
authored andcommitted
rewrite ldtc to be less buggy
1 parent 1c1bcb0 commit 711d65b

File tree

1 file changed

+69
-61
lines changed

1 file changed

+69
-61
lines changed

src/simulation/elements/LDTC.cpp

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ Element_LDTC::Element_LDTC()
4646
Update = &Element_LDTC::update;
4747
}
4848

49-
const int mask_invert_filter = 0x1;
50-
const int mask_ignore_energy = 0x2;
51-
const int mask_no_copy_color = 0x4;
52-
const int mask_keep_searching = 0x8;
49+
//#TPT-Directive ElementHeader Element_LDTC static const int FLAG_INVERT_FILTER
50+
//#TPT-Directive ElementHeader Element_LDTC static const int FLAG_IGNORE_ENERGY
51+
//#TPT-Directive ElementHeader Element_LDTC static const int FLAG_NO_COPY_COLOR
52+
//#TPT-Directive ElementHeader Element_LDTC static const int FLAG_KEEP_SEARCHING
53+
const int Element_LDTC::FLAG_INVERT_FILTER = 0x1;
54+
const int Element_LDTC::FLAG_IGNORE_ENERGY = 0x2;
55+
const int Element_LDTC::FLAG_NO_COPY_COLOR = 0x4;
56+
const int Element_LDTC::FLAG_KEEP_SEARCHING = 0x8;
5357

5458
//NOTES:
5559
// 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;
6266
// 0x08: Keep searching even after finding a particle
6367

6468

69+
//#TPT-Directive ElementHeader Element_LDTC static bool phot_data_type(int rt);
6570
/* Returns true for particles that activate the special FILT color copying mode */
66-
bool phot_data_type(int rt)
71+
bool Element_LDTC::phot_data_type(int rt)
6772
{
68-
if (rt == PT_FILT || rt == PT_PHOT || rt == PT_BRAY)
69-
return true;
70-
return false;
73+
return rt == PT_FILT || rt == PT_PHOT || rt == PT_BRAY;
7174
}
7275

76+
//#TPT-Directive ElementHeader Element_LDTC static bool accepted_conductor(Simulation *sim, int rt);
7377
/* Returns true for particles that start a ray search ("dtec" mode)
7478
*/
75-
bool accepted_type(Simulation* sim, int r)
79+
bool Element_LDTC::accepted_conductor(Simulation* sim, int r)
7680
{
7781
int rt = TYP(r);
78-
if ((sim->elements[rt].Properties & PROP_CONDUCTS) && !(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT || rt == PT_PTCT || rt == PT_INWR))
79-
{
80-
if (sim->parts[ID(r)].life == 0)
81-
return true;
82-
}
83-
return false;
82+
return (sim->elements[rt].Properties & PROP_CONDUCTS) &&
83+
!(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT ||
84+
rt == PT_PTCT || rt == PT_INWR) &&
85+
sim->parts[ID(r)].life == 0;
8486
}
8587

8688
//#TPT-Directive ElementHeader Element_LDTC static int update(UPDATE_FUNC_ARGS)
8789
int Element_LDTC::update(UPDATE_FUNC_ARGS)
8890
{
89-
int max = parts[i].tmp + parts[i].life;
91+
int ctype = TYP(parts[i].ctype), ctypeExtra = ID(parts[i].ctype), detectLength = parts[i].tmp, detectSpaces = parts[i].tmp2;
92+
bool copyColor = !(parts[i].tmp2 & Element_LDTC::FLAG_NO_COPY_COLOR);
93+
bool ignoreEnergy = parts[i].tmp2 & Element_LDTC::FLAG_IGNORE_ENERGY;
94+
bool invertFilter = parts[i].tmp2 & Element_LDTC::FLAG_INVERT_FILTER;
95+
bool keepSearching = parts[i].tmp2 & Element_LDTC::FLAG_KEEP_SEARCHING;
96+
if (detectSpaces < 0)
97+
detectSpaces = parts[i].tmp2 = 0;
98+
if (detectLength < 0)
99+
detectLength = parts[i].tmp = 0;
90100
for (int rx = -1; rx <= 1; rx++)
91101
{
92102
for (int ry = -1; ry <= 1; ry++)
@@ -96,70 +106,68 @@ int Element_LDTC::update(UPDATE_FUNC_ARGS)
96106
int r = pmap[y+ry][x+rx];
97107
if (!r)
98108
continue;
99-
100-
if (!accepted_type(sim, r) && ((parts[i].tmp2 & mask_no_copy_color) || !phot_data_type(TYP(r))))
109+
bool boolMode = Element_LDTC::accepted_conductor(sim, r);
110+
bool filtMode = copyColor && TYP(r) == PT_FILT;
111+
if (!boolMode && !filtMode)
101112
continue;
102113

103-
// Stolen from DRAY, does the ray searching
114+
int maxRange = parts[i].life + parts[i].tmp;
104115
int xStep = rx * -1, yStep = ry * -1;
105116
int xCurrent = x + (xStep * (parts[i].life + 1)), yCurrent = y + (yStep * (parts[i].life + 1));
106-
for (;(parts[i].tmp == 0) || !(xCurrent - x >= max) || (yCurrent-y >= max); xCurrent += xStep, yCurrent += yStep)
117+
for (; !parts[i].tmp ||
118+
(xStep * (xCurrent - x) <= maxRange &&
119+
yStep * (yCurrent - y) <= maxRange);
120+
xCurrent += xStep, yCurrent += yStep)
107121
{
108-
int rr = pmap[yCurrent][xCurrent];
109122
if (!(xCurrent>=0 && yCurrent>=0 && xCurrent<XRES && yCurrent<YRES))
110-
{
111123
break; // We're out of bounds! Oops!
112-
}
113-
if (!rr)
114-
{
124+
int rr = pmap[yCurrent][xCurrent];
125+
if (!rr && !ignoreEnergy)
115126
rr = sim->photons[yCurrent][xCurrent];
116-
if (!(rr && !(parts[i].tmp2 & mask_ignore_energy)))
117-
{
118-
continue;
119-
}
120-
}
121127
if (!rr)
122128
continue;
123129

124-
// Usual DTEC-like mode
125-
if (!phot_data_type(TYP(r)))
130+
// If ctype isn't set (no type restriction), or ctype matches what we found
131+
// Can use .tmp2 flag to invert this
132+
bool matchesCtype = parts[i].ctype == TYP(rr) && (ctype != PT_LIFE || parts[ID(rr)].ctype == ctypeExtra);
133+
bool matchesFilter = !ctype || (invertFilter ^ (int)matchesCtype);
134+
if (!matchesFilter)
126135
{
127-
// If ctype isn't set (no type restriction), or ctype matches what we found
128-
// Can use .tmp2 flag to invert this
129-
if (parts[i].ctype == 0 || (parts[i].ctype == TYP(rr)) ^ (parts[i].tmp2 & mask_invert_filter))
130-
{
131-
parts[ID(r)].life = 4;
132-
parts[ID(r)].ctype = TYP(r);
133-
sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
136+
if (keepSearching)
137+
continue;
138+
else
134139
break;
135-
}
136-
// room for more conditions here.
137140
}
138-
// FILT color copying mode
139-
else
141+
// room for more conditions here.
142+
143+
if (boolMode)
144+
{
145+
parts[ID(r)].life = 4;
146+
parts[ID(r)].ctype = TYP(r);
147+
sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
148+
break;
149+
}
150+
151+
if (filtMode)
140152
{
141-
// If ctype isn't set (no type restriction), or ctype matches what we found
142-
// Can use .tmp2 flag to invert this
143-
if (parts[i].ctype == 0 || (parts[i].ctype == TYP(rr)) ^ (parts[i].tmp2 & mask_invert_filter))
153+
if (!Element_LDTC::phot_data_type(TYP(rr)))
154+
continue;
155+
156+
int nx = x + rx, ny = y + ry;
157+
int photonWl = TYP(rr) == PT_FILT ?
158+
Element_FILT::getWavelengths(&parts[ID(rr)]) :
159+
parts[ID(rr)].ctype;
160+
while (TYP(r) == PT_FILT)
144161
{
145-
if (phot_data_type(TYP(rr)))
146-
{
147-
int nx = x + rx, ny = y + ry;
148-
while (TYP(r) == PT_FILT)
149-
{
150-
parts[ID(r)].ctype = Element_FILT::getWavelengths(&parts[ID(rr)]);
151-
nx += rx;
152-
ny += ry;
153-
if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES)
154-
break;
155-
r = pmap[ny][nx];
156-
}
162+
parts[ID(r)].ctype = photonWl;
163+
nx += rx;
164+
ny += ry;
165+
if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES)
157166
break;
158-
}
167+
r = pmap[ny][nx];
159168
}
160-
}
161-
if (!(parts[i].tmp2 & mask_keep_searching))
162169
break;
170+
}
163171
}
164172
}
165173
}

0 commit comments

Comments
 (0)