1 #include "simulation/ElementCommon.h"
2 
3 static int update(UPDATE_FUNC_ARGS);
4 
Element_LSNS()5 void Element::Element_LSNS()
6 {
7 	Identifier = "DEFAULT_PT_LSNS";
8 	Name = "LSNS";
9 	Colour = PIXPACK(0x336699);
10 	MenuVisible = 1;
11 	MenuSection = SC_SENSOR;
12 	Enabled = 1;
13 
14 	Advection = 0.0f;
15 	AirDrag = 0.00f * CFDS;
16 	AirLoss = 0.96f;
17 	Loss = 0.00f;
18 	Collision = 0.0f;
19 	Gravity = 0.0f;
20 	Diffusion = 0.00f;
21 	HotAir = 0.000f	* CFDS;
22 	Falldown = 0;
23 
24 	Flammable = 0;
25 	Explosive = 0;
26 	Meltable = 0;
27 	Hardness = 1;
28 
29 	Weight = 100;
30 
31 	DefaultProperties.temp = 4.0f + 273.15f;
32 	HeatConduct = 0;
33 	Description = "Life sensor, creates a spark when there's a nearby particle with a life higher than its temperature.";
34 
35 	Properties = TYPE_SOLID;
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 	DefaultProperties.tmp2 = 2;
47 
48 	Update = &update;
49 }
50 
update(UPDATE_FUNC_ARGS)51 static int update(UPDATE_FUNC_ARGS)
52 {
53 	int rd = parts[i].tmp2;
54 	if (rd > 25) parts[i].tmp2 = rd = 25;
55 	if (parts[i].life)
56 	{
57 		parts[i].life = 0;
58 		for (int rx = -2; rx <= 2; rx++)
59 			for (int ry = -2; ry <= 2; ry++)
60 				if (BOUNDS_CHECK && (rx || ry))
61 				{
62 					int r = pmap[y + ry][x + rx];
63 					if (!r)
64 						continue;
65 					int rt = TYP(r);
66 					if (sim->parts_avg(i, ID(r), PT_INSL) != PT_INSL)
67 					{
68 						if ((sim->elements[rt].Properties&PROP_CONDUCTS) && !(rt == PT_WATR || rt == PT_SLTW || rt == PT_NTCT || rt == PT_PTCT || rt == PT_INWR) && parts[ID(r)].life == 0)
69 						{
70 							parts[ID(r)].life = 4;
71 							parts[ID(r)].ctype = rt;
72 							sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK);
73 						}
74 					}
75 
76 				}
77 	}
78 	bool doSerialization = false;
79 	bool doDeserialization = false;
80 	int life = 0;
81 	for (int rx = -rd; rx < rd + 1; rx++)
82 		for (int ry = -rd; ry < rd + 1; ry++)
83 			if (x + rx >= 0 && y + ry >= 0 && x + rx < XRES && y + ry < YRES && (rx || ry))
84 			{
85 				int r = pmap[y + ry][x + rx];
86 				if (!r)
87 					r = sim->photons[y + ry][x + rx];
88 				if (!r)
89 					continue;
90 
91 				switch (parts[i].tmp)
92 				{
93 				case 1:
94 					// .life serialization into FILT
95 					if (TYP(r) != PT_LSNS && TYP(r) != PT_FILT && parts[ID(r)].life >= 0)
96 					{
97 						doSerialization = true;
98 						life = parts[ID(r)].life;
99 					}
100 					break;
101 				case 3:
102 					// .life deserialization
103 					if (TYP(r) == PT_FILT)
104 					{
105 						doDeserialization = true;
106 						life = parts[ID(r)].ctype;
107 					}
108 					break;
109 				case 2:
110 					// Invert mode
111 					if (TYP(r) != PT_METL && parts[ID(r)].life <= parts[i].temp - 273.15)
112 						parts[i].life = 1;
113 					break;
114 				default:
115 					// Normal mode
116 					if (TYP(r) != PT_METL && parts[ID(r)].life > parts[i].temp - 273.15)
117 						parts[i].life = 1;
118 					break;
119 				}
120 			}
121 
122 	for (int rx = -1; rx <= 1; rx++)
123 		for (int ry = -1; ry <= 1; ry++)
124 			if (BOUNDS_CHECK && (rx || ry))
125 			{
126 				int r = pmap[y + ry][x + rx];
127 				if (!r)
128 					continue;
129 				int nx = x + rx;
130 				int ny = y + ry;
131 				// .life serialization.
132 				if (doSerialization)
133 				{
134 					while (TYP(r) == PT_FILT)
135 					{
136 						parts[ID(r)].ctype = 0x10000000 + life;
137 						nx += rx;
138 						ny += ry;
139 						if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES)
140 							break;
141 						r = pmap[ny][nx];
142 					}
143 				}
144 				// .life deserialization.
145 				if (doDeserialization)
146 				{
147 					if (TYP(r) != PT_FILT)
148 					{
149 						parts[ID(r)].life = life - 0x10000000;
150 						break;
151 					}
152 				}
153 			}
154 
155 	return 0;
156 }
157