1 #include "simulation/ElementCommon.h"
2 #include <iostream>
3 
4 static int update(UPDATE_FUNC_ARGS);
5 
Element_LDTC()6 void Element::Element_LDTC()
7 {
8 	Identifier = "DEFAULT_PT_LDTC";
9 	Name = "LDTC";
10 	Colour = PIXPACK(0x66ff66);
11 	MenuVisible = 1;
12 	MenuSection = SC_SENSOR;
13 	Enabled = 1;
14 
15 	Advection = 0.0f;
16 	AirDrag = 0.00f * CFDS;
17 	AirLoss = 0.96f;
18 	Loss = 0.00f;
19 	Collision = 0.0f;
20 	Gravity = 0.0f;
21 	Diffusion = 0.00f;
22 	HotAir = 0.000f	* CFDS;
23 	Falldown = 0;
24 
25 	Flammable = 0;
26 	Explosive = 0;
27 	Meltable = 0;
28 	Hardness = 0;
29 
30 	Weight = 100;
31 
32 	HeatConduct = 0;
33 	Description = "Linear detector. Scans in 8 directions for particles with its ctype and creates a spark on the opposite side.";
34 
35 	Properties = TYPE_SOLID | PROP_NOCTYPEDRAW;
36 
37 	LowPressure = IPL;
38 	LowPressureTransition = NT;
39 	HighPressure = IPH;
40 	HighPressureTransition = NT;
41 	LowTemperature = ITL;
42 	LowTemperatureTransition = NT;
43 	HighTemperature = ITH;
44 	HighTemperatureTransition = NT;
45 
46 	Update = &update;
47 	CtypeDraw = &Element::ctypeDrawVInTmp;
48 }
49 
50 constexpr int FLAG_INVERT_FILTER =  0x1;
51 constexpr int FLAG_IGNORE_ENERGY =  0x2;
52 constexpr int FLAG_NO_COPY_COLOR =  0x4;
53 constexpr int FLAG_KEEP_SEARCHING = 0x8;
54 
55 //NOTES:
56 // ctype is used to store the target element, if any. (NONE is treated as a wildcard)
57 // life is used for the amount of pixels to skip before starting the scan. Starts just in front of the LDTC if 0.
58 // tmp is the number of particles that will be scanned before scanning stops. Unbounded if 0.
59 // tmp2 is used for settings (binary flags). The flags are as follows:
60 // 0x01: Inverts the CTYPE filter so that the element in ctype is the only thing that doesn't trigger LDTC, instead of the opposite.
61 // 0x02: Ignore energy particles
62 // 0x04: Ignore FILT (do not use color copying mode)
63 // 0x08: Keep searching even after finding a particle
64 
65 
66 /* Returns true for particles that activate the special FILT color copying mode */
phot_data_type(int rt)67 static bool phot_data_type(int rt)
68 {
69 	return rt == PT_FILT || rt == PT_PHOT || rt == PT_BRAY;
70 }
71 
72 /* Returns true for particles that start a ray search ("dtec" mode)
73  */
accepted_conductor(Simulation * sim,int r)74 static bool accepted_conductor(Simulation* sim, int r)
75 {
76 	int rt = TYP(r);
77 	return (sim->elements[rt].Properties & PROP_CONDUCTS) &&
78 		!(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT ||
79 		rt == PT_PTCT || rt == PT_INWR) &&
80 		sim->parts[ID(r)].life == 0;
81 }
82 
update(UPDATE_FUNC_ARGS)83 static int update(UPDATE_FUNC_ARGS)
84 {
85 	int ctype = TYP(parts[i].ctype), ctypeExtra = ID(parts[i].ctype), detectLength = parts[i].tmp, detectSpaces = parts[i].tmp2;
86 	bool copyColor = !(parts[i].tmp2 & FLAG_NO_COPY_COLOR);
87 	bool ignoreEnergy = parts[i].tmp2 & FLAG_IGNORE_ENERGY;
88 	bool invertFilter = parts[i].tmp2 & FLAG_INVERT_FILTER;
89 	bool keepSearching = parts[i].tmp2 & FLAG_KEEP_SEARCHING;
90 	if (detectSpaces < 0)
91 		detectSpaces = parts[i].tmp2 = 0;
92 	if (detectLength < 0)
93 		detectLength = parts[i].tmp = 0;
94 	for (int rx = -1; rx <= 1; rx++)
95 	{
96 		for (int ry = -1; ry <= 1; ry++)
97 		{
98 			if (BOUNDS_CHECK && (rx || ry))
99 			{
100 				int r = pmap[y+ry][x+rx];
101 				if (!r)
102 					continue;
103 				bool boolMode = accepted_conductor(sim, r);
104 				bool filtMode = copyColor && TYP(r) == PT_FILT;
105 				if (!boolMode && !filtMode)
106 					continue;
107 
108 				int maxRange = parts[i].life + parts[i].tmp;
109 				int xStep = rx * -1, yStep = ry * -1;
110 				int xCurrent = x + (xStep * (parts[i].life + 1)), yCurrent = y + (yStep * (parts[i].life + 1));
111 				for (; !parts[i].tmp ||
112 					(xStep * (xCurrent - x) <= maxRange &&
113 					yStep * (yCurrent - y) <= maxRange);
114 					xCurrent += xStep, yCurrent += yStep)
115 				{
116 					if (!(xCurrent>=0 && yCurrent>=0 && xCurrent<XRES && yCurrent<YRES))
117 						break; // We're out of bounds! Oops!
118 					int rr = pmap[yCurrent][xCurrent];
119 					if (!rr && !ignoreEnergy)
120 						rr = sim->photons[yCurrent][xCurrent];
121 					if (!rr)
122 						continue;
123 
124 					// If ctype isn't set (no type restriction), or ctype matches what we found
125 					// Can use .tmp2 flag to invert this
126 					bool matchesCtype = parts[i].ctype == TYP(rr) && (ctype != PT_LIFE || parts[ID(rr)].ctype == ctypeExtra);
127 					bool matchesFilter = !ctype || (invertFilter ^ (int)matchesCtype);
128 					if (!matchesFilter)
129 					{
130 						if (keepSearching)
131 							continue;
132 						else
133 							break;
134 					}
135 					// room for more conditions here.
136 
137 					if (boolMode)
138 					{
139 						parts[ID(r)].life = 4;
140 						parts[ID(r)].ctype = TYP(r);
141 						sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
142 						break;
143 					}
144 
145 					if (filtMode)
146 					{
147 						if (!phot_data_type(TYP(rr)))
148 							continue;
149 
150 						int nx = x + rx, ny = y + ry;
151 						int Element_FILT_getWavelengths(Particle* cpart);
152 						int photonWl = TYP(rr) == PT_FILT ?
153 							Element_FILT_getWavelengths(&parts[ID(rr)]) :
154 							parts[ID(rr)].ctype;
155 						while (TYP(r) == PT_FILT)
156 						{
157 							parts[ID(r)].ctype = photonWl;
158 							nx += rx;
159 							ny += ry;
160 							if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES)
161 								break;
162 							r = pmap[ny][nx];
163 						}
164 						break;
165 					}
166 				}
167 			}
168 		}
169 	}
170 	return 0;
171 }
172