This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
The-Powder-Toy/src/simulation/elements/DRAY.cpp
cracker64 a115e78952 More element optimizations!
ACEL, changed a range check
DRAY, Fixed repetitive checking
GOLD, less rand()
PLNT, removed range checks, less rand()
PROT, used a switch()
VIBR, less rand()
VINE, less rand(), fixed growing along WOOD
VIRS, less rand()
2014-12-06 15:43:11 -05:00

123 lines
3.5 KiB
C++

#include "simulation/Elements.h"
//#TPT-Directive ElementClass Element_DRAY PT_DRAY 178
Element_DRAY::Element_DRAY()
{
Identifier = "DEFAULT_PT_DRAY";
Name = "DRAY";
Colour = PIXPACK(0xFFAA22);
MenuVisible = 1;
MenuSection = SC_ELEC;
Enabled = 1;
Advection = 0.0f;
AirDrag = 0.00f * CFDS;
AirLoss = 0.90f;
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;
Temperature = R_TEMP + 273.15f;
HeatConduct = 0;
Description = "Duplicator ray. Replicates a line of particles in front of it.";
State = ST_SOLID;
Properties = TYPE_SOLID|PROP_LIFE_DEC;
LowPressure = IPL;
LowPressureTransition = NT;
HighPressure = IPH;
HighPressureTransition = NT;
LowTemperature = ITL;
LowTemperatureTransition = NT;
HighTemperature = ITH;
HighTemperatureTransition = NT;
Update = &Element_DRAY::update;
Graphics = NULL;
}
//should probably be in Simulation.h
bool InBounds(int x, int y)
{
return (x>=0 && y>=0 && x<XRES && y<YRES);
}
//#TPT-Directive ElementHeader Element_DRAY static int update(UPDATE_FUNC_ARGS)
int Element_DRAY::update(UPDATE_FUNC_ARGS)
{
int ctype = parts[i].ctype&0xFF, ctypeExtra = parts[i].ctype>>8, copyLength = parts[i].tmp, copySpaces = parts[i].tmp2;
if (copySpaces < 0)
copySpaces = parts[i].tmp2 = 0;
if (copyLength < 0)
copyLength = parts[i].tmp = 0;
else
copySpaces++; //strange hack
if (!parts[i].life) // only fire when life is 0, but nothing sets the life right now
{
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&0xFF) == PT_SPRK && parts[r>>8].life == 3) //spark found, start creating
{
bool overwrite = parts[r>>8].ctype == PT_PSCN;
int partsRemaining = copyLength, xCopyTo, yCopyTo; //positions where the line will start being copied at
if (parts[r>>8].ctype == PT_INWR && rx && ry) // INWR doesn't spark from diagonals
continue;
//figure out where the copying will start/end
for (int xStep = rx*-1, yStep = ry*-1, xCurrent = x+xStep, yCurrent = y+yStep; ; xCurrent+=xStep, yCurrent+=yStep)
{
int rr = pmap[yCurrent][xCurrent];
if ((!copyLength && (rr&0xFF) == ctype && (ctype != PT_LIFE || parts[rr>>8].ctype == ctypeExtra))
|| !(--partsRemaining && InBounds(xCurrent+xStep, yCurrent+yStep)))
{
copyLength -= partsRemaining;
xCopyTo = xCurrent + xStep*copySpaces;
yCopyTo = yCurrent + yStep*copySpaces;
break;
}
}
//now, actually copy the particles
partsRemaining = copyLength + 1;
for (int xStep = rx*-1, yStep = ry*-1, xCurrent = x+xStep, yCurrent = y+yStep; InBounds(xCopyTo, yCopyTo) && --partsRemaining; xCurrent+=xStep, yCurrent+=yStep, xCopyTo+=xStep, yCopyTo+=yStep)
{
int type = pmap[yCurrent][xCurrent]&0xFF, p;
if (overwrite)
sim->delete_part(xCopyTo, yCopyTo);
if (type == PT_SPRK) //hack
p = sim->create_part(-1, xCopyTo, yCopyTo, PT_METL);
else if (type)
p = sim->create_part(-1, xCopyTo, yCopyTo, type);
else
continue;
if (p >= 0)
{
if (type == PT_SPRK)
sim->part_change_type(p, xCopyTo, yCopyTo, PT_SPRK);
parts[p] = parts[pmap[yCurrent][xCurrent]>>8];
parts[p].x = xCopyTo;
parts[p].y = yCopyTo;
}
}
}
}
}
return 0;
}
Element_DRAY::~Element_DRAY() {}