From 00ddc1f8be769c75758296e80779aa8ca198860a Mon Sep 17 00:00:00 2001 From: Cracker1000 <39940063+cracker1000@users.noreply.github.com> Date: Wed, 7 Oct 2020 14:19:31 +0530 Subject: [PATCH] Add VSNS (#707) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tamás Bálint Misius --- src/simulation/ElementNumbers.h | 1 + src/simulation/elements/VSNS.cpp | 171 +++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 src/simulation/elements/VSNS.cpp diff --git a/src/simulation/ElementNumbers.h b/src/simulation/ElementNumbers.h index 6c92f23c3..e9f1141bd 100644 --- a/src/simulation/ElementNumbers.h +++ b/src/simulation/ElementNumbers.h @@ -197,5 +197,6 @@ ELEMENT_DEFINE(LSNS, 185); ELEMENT_DEFINE(LDTC, 186); ELEMENT_DEFINE(SLCN, 187); ELEMENT_DEFINE(PTNM, 188); +ELEMENT_DEFINE(VSNS, 189); #undef ELEMENT_DEFINE diff --git a/src/simulation/elements/VSNS.cpp b/src/simulation/elements/VSNS.cpp new file mode 100644 index 000000000..53ac536f2 --- /dev/null +++ b/src/simulation/elements/VSNS.cpp @@ -0,0 +1,171 @@ +#include "simulation/ElementCommon.h" + +static int update(UPDATE_FUNC_ARGS); + +void Element::Element_VSNS() +{ + Identifier = "DEFAULT_PT_VSNS"; + Name = "VSNS"; + Colour = PIXPACK(0x7C9C00); + MenuVisible = 1; + MenuSection = SC_SENSOR; + Enabled = 1; + + Advection = 0.0f; + AirDrag = 0.00f * CFDS; + AirLoss = 0.96f; + Loss = 0.00f; + Collision = 0.0f; + Gravity = 0.0f; + Diffusion = 0.00f; + HotAir = 0.000f * CFDS; + Falldown = 0; + + Flammable = 0; + Explosive = 0; + Meltable = 0; + Hardness = 1; + + Weight = 100; + + DefaultProperties.temp = 4.0f + 273.15f; + HeatConduct = 0; + Description = "Velocity sensor, creates a spark when there's a nearby particle with velocity higher than its temperature."; + + Properties = TYPE_SOLID; + + LowPressure = IPL; + LowPressureTransition = NT; + HighPressure = IPH; + HighPressureTransition = NT; + LowTemperature = ITL; + LowTemperatureTransition = NT; + HighTemperature = ITH; + HighTemperatureTransition = NT; + + DefaultProperties.tmp2 = 2; + + Update = &update; +} + +static int update(UPDATE_FUNC_ARGS) +{ + int rd = parts[i].tmp2; + if (rd > 25) parts[i].tmp2 = rd = 25; + if (parts[i].life) + { + parts[i].life = 0; + for (int rx = -2; rx <= 2; rx++) + for (int ry = -2; ry <= 2; ry++) + if (BOUNDS_CHECK && (rx || ry)) + { + int r = pmap[y + ry][x + rx]; + if (!r) + continue; + int rt = TYP(r); + if (sim->parts_avg(i, ID(r), PT_INSL) != PT_INSL) + { + 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) + { + parts[ID(r)].life = 4; + parts[ID(r)].ctype = rt; + sim->part_change_type(ID(r), x + rx, y + ry, PT_SPRK); + } + } + } + } + bool doSerialization = false; + bool doDeserialization = false; + float Vs = 0; + for (int rx = -rd; rx < rd + 1; rx++) + for (int ry = -rd; ry < rd + 1; ry++) + if (x + rx >= 0 && y + ry >= 0 && x + rx < XRES && y + ry < YRES && (rx || ry)) + { + int r = pmap[y + ry][x + rx]; + if (!r) + r = sim->photons[y + ry][x + rx]; + if (!r) + continue; + float Vx = parts[ID(r)].vx; + float Vy = parts[ID(r)].vy; + float Vm = sqrt(Vx*Vx + Vy*Vy); + + switch (parts[i].tmp) + { + case 1: + // serialization + if (TYP(r) != PT_VSNS && TYP(r) != PT_FILT && !(sim->elements[TYP(r)].Properties & TYPE_SOLID)) + { + doSerialization = true; + Vs = Vm; + } + break; + case 3: + // deserialization + if (TYP(r) == PT_FILT) + { + int vel = parts[ID(r)].ctype - 0x10000000; + if (vel >= 0 && vel < SIM_MAXVELOCITY) + { + doDeserialization = true; + Vs = vel; + } + } + break; + case 2: + // Invert mode + if (!(sim->elements[TYP(r)].Properties & TYPE_SOLID) && Vm <= parts[i].temp - 273.15) + parts[i].life = 1; + break; + default: + // Normal mode + if (!(sim->elements[TYP(r)].Properties & TYPE_SOLID) && Vm > parts[i].temp - 273.15) + parts[i].life = 1; + break; + } + } + + for (int rx = -1; rx <= 1; rx++) + for (int ry = -1; ry <= 1; ry++) + if (BOUNDS_CHECK && (rx || ry)) + { + int r = pmap[y + ry][x + rx]; + if (!r) + r = sim->photons[y + ry][x + rx]; + if (!r) + continue; + int nx = x + rx; + int ny = y + ry; + //Serialization. + if (doSerialization) + { + while (TYP(r) == PT_FILT) + { + parts[ID(r)].ctype = 0x10000000 + (int)(Vs + 0.5f); + nx += rx; + ny += ry; + if (nx < 0 || ny < 0 || nx >= XRES || ny >= YRES) + break; + r = pmap[ny][nx]; + } + } + //Deserialization. + if (doDeserialization) + { + if (TYP(r) != PT_FILT && !(sim->elements[TYP(r)].Properties & TYPE_SOLID)) + { + float Vx = parts[ID(r)].vx; + float Vy = parts[ID(r)].vy; + float Vm = sqrt(Vx*Vx + Vy*Vy); + if (Vm > 0) + { + parts[ID(r)].vx *= Vs / Vm; + parts[ID(r)].vy *= Vs / Vm; + } + break; + } + } + } + + return 0; +}