Make some free-floating arrays sim data

This commit is contained in:
Tamás Bálint Misius
2025-03-21 19:21:52 +01:00
parent 5e0fc38b8f
commit 2babec6d2b
11 changed files with 33 additions and 42 deletions

View File

@@ -1473,10 +1473,6 @@ void Renderer::DrawBlob(Vec2<int> pos, RGB colour)
BlendPixel(pos + Vec2{ -1, +1 }, colour.WithAlpha(64)); BlendPixel(pos + Vec2{ -1, +1 }, colour.WithAlpha(64));
} }
float temp[CELL*3][CELL*3];
float fire_alphaf[CELL*3][CELL*3];
float glow_alphaf[11][11];
float blur_alphaf[7][7];
void Renderer::prepare_alpha(int size, float intensity) void Renderer::prepare_alpha(int size, float intensity)
{ {
fireIntensity = intensity; fireIntensity = intensity;
@@ -1484,6 +1480,7 @@ void Renderer::prepare_alpha(int size, float intensity)
int x,y,i,j; int x,y,i,j;
float multiplier = 255.0f*fireIntensity; float multiplier = 255.0f*fireIntensity;
float temp[CELL*3][CELL*3];
memset(temp, 0, sizeof(temp)); memset(temp, 0, sizeof(temp));
for (x=0; x<CELL; x++) for (x=0; x<CELL; x++)
for (y=0; y<CELL; y++) for (y=0; y<CELL; y++)

View File

@@ -1023,6 +1023,10 @@ void Simulation::clear_sim(void)
memset(fighters, 0, sizeof(fighters)); memset(fighters, 0, sizeof(fighters));
memset(&player, 0, sizeof(player)); memset(&player, 0, sizeof(player));
memset(&player2, 0, sizeof(player2)); memset(&player2, 0, sizeof(player2));
memset(&Element_LOLZ_lolz, 0, sizeof(Element_LOLZ_lolz));
memset(&Element_LOVE_love, 0, sizeof(Element_LOVE_love));
memset(&Element_PSTN_tempParts, 0, sizeof(Element_PSTN_tempParts));
Element_PPIP_ppip_changed = 0;
std::fill(elementCount, elementCount+PT_NUM, 0); std::fill(elementCount, elementCount+PT_NUM, 0);
elementRecount = true; elementRecount = true;
fighcount = 0; fighcount = 0;

View File

@@ -128,6 +128,10 @@ public:
float fvx[YCELLS][XCELLS]; float fvx[YCELLS][XCELLS];
float fvy[YCELLS][XCELLS]; float fvy[YCELLS][XCELLS];
int Element_LOLZ_lolz[XRES/9][YRES/9];
int Element_LOVE_love[XRES/9][YRES/9];
int Element_PSTN_tempParts[std::max(XRES, YRES)];
int Element_PPIP_ppip_changed;
unsigned int pmap_count[YRES][XRES]; unsigned int pmap_count[YRES][XRES];

View File

@@ -2,7 +2,6 @@
#include "ETRD.h" #include "ETRD.h"
#include <algorithm> #include <algorithm>
static void initDeltaPos();
static void changeType(ELEMENT_CHANGETYPE_FUNC_ARGS); static void changeType(ELEMENT_CHANGETYPE_FUNC_ARGS);
void Element::Element_ETRD() void Element::Element_ETRD()
@@ -46,8 +45,6 @@ void Element::Element_ETRD()
HighTemperatureTransition = NT; HighTemperatureTransition = NT;
ChangeType = &changeType; ChangeType = &changeType;
initDeltaPos();
} }
static void changeType(ELEMENT_CHANGETYPE_FUNC_ARGS) static void changeType(ELEMENT_CHANGETYPE_FUNC_ARGS)
@@ -75,12 +72,10 @@ public:
int length; int length;
}; };
const int maxLength = 12; static const std::vector<ETRD_deltaWithLength> InitDeltaPos()
std::vector<ETRD_deltaWithLength> deltaPos;
static void initDeltaPos()
{ {
deltaPos.clear(); const int maxLength = 12;
std::vector<ETRD_deltaWithLength> deltaPos;
for (int ry = -maxLength; ry <= maxLength; ry++) for (int ry = -maxLength; ry <= maxLength; ry++)
for (int rx = -maxLength; rx <= maxLength; rx++) for (int rx = -maxLength; rx <= maxLength; rx++)
{ {
@@ -91,7 +86,9 @@ static void initDeltaPos()
std::stable_sort(deltaPos.begin(), deltaPos.end(), [](const ETRD_deltaWithLength &a, const ETRD_deltaWithLength &b) { std::stable_sort(deltaPos.begin(), deltaPos.end(), [](const ETRD_deltaWithLength &a, const ETRD_deltaWithLength &b) {
return a.length < b.length; return a.length < b.length;
}); });
return deltaPos;
} }
static const auto deltaPos = InitDeltaPos();
int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId) int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId)
{ {
@@ -118,9 +115,8 @@ int Element_ETRD_nearestSparkablePart(Simulation *sim, int targetId)
// TODO: probably not optimal if excessive stacking is used // TODO: probably not optimal if excessive stacking is used
if (sim->parts.lastActiveIndex > (int)deltaPos.size()*2) if (sim->parts.lastActiveIndex > (int)deltaPos.size()*2)
{ {
for (std::vector<ETRD_deltaWithLength>::iterator iter = deltaPos.begin(), end = deltaPos.end(); iter != end; ++iter) for (auto &delta : deltaPos)
{ {
ETRD_deltaWithLength delta = (*iter);
ui::Point checkPos = targetPos + delta.d; ui::Point checkPos = targetPos + delta.d;
int checkDistance = delta.length; int checkDistance = delta.length;
if (parts[targetId].tmp >= checkDistance) // tmp sets min distance if (parts[targetId].tmp >= checkDistance) // tmp sets min distance

View File

@@ -43,7 +43,7 @@ void Element::Element_LOLZ()
HighTemperatureTransition = NT; HighTemperatureTransition = NT;
} }
int Element_LOLZ_RuleTable[9][9] = extern const int Element_LOLZ_RuleTable[9][9] =
{ {
{0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,1,0,0}, {1,0,0,0,0,0,1,0,0},
@@ -55,5 +55,3 @@ int Element_LOLZ_RuleTable[9][9] =
{0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0},
}; };
int Element_LOLZ_lolz[XRES/9][YRES/9];

View File

@@ -1,7 +1,5 @@
#pragma once #pragma once
#include "simulation/ElementDefs.h" #include "simulation/ElementDefs.h"
extern int Element_LOLZ_RuleTable[9][9]; extern const int Element_LOLZ_RuleTable[9][9];
extern int Element_LOLZ_lolz[XRES/9][YRES/9]; extern const int Element_LOVE_RuleTable[9][9];
extern int Element_LOVE_RuleTable[9][9];
extern int Element_LOVE_love[XRES/9][YRES/9];

View File

@@ -42,7 +42,7 @@ void Element::Element_LOVE()
HighTemperatureTransition = NT; HighTemperatureTransition = NT;
} }
int Element_LOVE_RuleTable[9][9] = extern const int Element_LOVE_RuleTable[9][9] =
{ {
{0,0,1,1,0,0,0,0,0}, {0,0,1,1,0,0,0,0,0},
{0,1,0,0,1,1,0,0,0}, {0,1,0,0,1,1,0,0,0},
@@ -54,5 +54,3 @@ int Element_LOVE_RuleTable[9][9] =
{0,1,0,0,1,1,0,0,0}, {0,1,0,0,1,1,0,0,0},
{0,0,1,1,0,0,0,0,0}, {0,0,1,1,0,0,0,0,0},
}; };
int Element_LOVE_love[XRES/9][YRES/9];

View File

@@ -24,5 +24,4 @@ constexpr int PFLAG_COLOR_GREEN = 0x00080000;
constexpr int PFLAG_COLOR_BLUE = 0x000C0000; constexpr int PFLAG_COLOR_BLUE = 0x000C0000;
constexpr int PFLAG_COLORS = 0x000C0000; constexpr int PFLAG_COLORS = 0x000C0000;
extern int Element_PPIP_ppip_changed;
extern const std::array<Vec2<int>, 8> Element_PIPE_offsets; extern const std::array<Vec2<int>, 8> Element_PIPE_offsets;

View File

@@ -61,8 +61,6 @@ constexpr int PPIP_TMPFLAG_TRIGGER_REVERSE = 0x04000000;
// 0x00002000 will transfer like a single pixel pipe when in reverse mode // 0x00002000 will transfer like a single pixel pipe when in reverse mode
// 0x0001C000 reverse single pixel pipe direction // 0x0001C000 reverse single pixel pipe direction
int Element_PPIP_ppip_changed = 0;
void Element_PPIP_flood_trigger(Simulation * sim, int x, int y, int sparkedBy) void Element_PPIP_flood_trigger(Simulation * sim, int x, int y, int sparkedBy)
{ {
int coord_stack_limit = XRES*YRES; int coord_stack_limit = XRES*YRES;
@@ -120,7 +118,7 @@ void Element_PPIP_flood_trigger(Simulation * sim, int x, int y, int sparkedBy)
for (x=x1; x<=x2; x++) for (x=x1; x<=x2; x++)
{ {
if (!(parts[ID(pmap[y][x])].tmp & prop) && sparkedBy != PT_HEAC) if (!(parts[ID(pmap[y][x])].tmp & prop) && sparkedBy != PT_HEAC)
Element_PPIP_ppip_changed = 1; sim->Element_PPIP_ppip_changed = 1;
parts[ID(pmap[y][x])].tmp |= prop; parts[ID(pmap[y][x])].tmp |= prop;
} }

View File

@@ -67,8 +67,6 @@ struct StackData
} }
}; };
int tempParts[XRES];
constexpr int PISTON_INACTIVE = 0x00; constexpr int PISTON_INACTIVE = 0x00;
constexpr int PISTON_RETRACT = 0x01; constexpr int PISTON_RETRACT = 0x01;
constexpr int PISTON_EXTEND = 0x02; constexpr int PISTON_EXTEND = 0x02;
@@ -221,14 +219,14 @@ static StackData CanMoveStack(Simulation * sim, int stackX, int stackY, int dire
if (!r) if (!r)
{ {
spaces++; spaces++;
tempParts[currentPos++] = -1; sim->Element_PSTN_tempParts[currentPos++] = -1;
if (spaces >= amount) if (spaces >= amount)
break; break;
} }
else else
{ {
if (currentPos - spaces < maxSize && (!retract || (TYP(r) == PT_FRME && posX == stackX && posY == stackY))) if (currentPos - spaces < maxSize && (!retract || (TYP(r) == PT_FRME && posX == stackX && posY == stackY)))
tempParts[currentPos++] = ID(r); sim->Element_PSTN_tempParts[currentPos++] = ID(r);
else else
return StackData(currentPos - spaces, spaces); return StackData(currentPos - spaces, spaces);
} }
@@ -306,13 +304,13 @@ static int MoveStack(Simulation * sim, int stackX, int stackY, int directionX, i
break; break;
} else { } else {
foundParts = true; foundParts = true;
tempParts[currentPos++] = ID(r); sim->Element_PSTN_tempParts[currentPos++] = ID(r);
} }
} }
if(foundParts) { if(foundParts) {
//Move particles //Move particles
for(int j = 0; j < currentPos; j++) { for(int j = 0; j < currentPos; j++) {
int jP = tempParts[j]; int jP = sim->Element_PSTN_tempParts[j];
int srcX = (int)(sim->parts[jP].x + 0.5f), srcY = (int)(sim->parts[jP].y + 0.5f); int srcX = (int)(sim->parts[jP].x + 0.5f), srcY = (int)(sim->parts[jP].y + 0.5f);
int destX = srcX-directionX*amount, destY = srcY-directionY*amount; int destX = srcX-directionX*amount, destY = srcY-directionY*amount;
sim->pmap[srcY][srcX] = 0; sim->pmap[srcY][srcX] = 0;
@@ -329,7 +327,7 @@ static int MoveStack(Simulation * sim, int stackX, int stackY, int directionX, i
//Move particles //Move particles
int possibleMovement = 0; int possibleMovement = 0;
for(int j = currentPos-1; j >= 0; j--) { for(int j = currentPos-1; j >= 0; j--) {
int jP = tempParts[j]; int jP = sim->Element_PSTN_tempParts[j];
if(jP < 0) { if(jP < 0) {
possibleMovement++; possibleMovement++;
continue; continue;

View File

@@ -3,7 +3,6 @@
static int update(UPDATE_FUNC_ARGS); static int update(UPDATE_FUNC_ARGS);
static int graphics(GRAPHICS_FUNC_ARGS); static int graphics(GRAPHICS_FUNC_ARGS);
static void create(ELEMENT_CREATE_FUNC_ARGS); static void create(ELEMENT_CREATE_FUNC_ARGS);
static void init_graphics();
static int trymovetron(Simulation * sim, int x, int y, int dir, int i, int len); static int trymovetron(Simulation * sim, int x, int y, int dir, int i, int len);
static bool canmovetron(Simulation * sim, int r, int len); static bool canmovetron(Simulation * sim, int r, int len);
static int new_tronhead(Simulation * sim, int x, int y, int i, int direction); static int new_tronhead(Simulation * sim, int x, int y, int i, int direction);
@@ -52,8 +51,6 @@ void Element::Element_TRON()
Update = &update; Update = &update;
Graphics = &graphics; Graphics = &graphics;
Create = &create; Create = &create;
init_graphics();
} }
/* TRON element is meant to resemble a tron bike (or worm) moving around and trying to avoid obstacles itself. /* TRON element is meant to resemble a tron bike (or worm) moving around and trying to avoid obstacles itself.
@@ -80,20 +77,24 @@ constexpr auto TRON_WAIT = UINT32_C(0x00000004); //it was just created, so W
constexpr auto TRON_NODIE = UINT32_C(0x00000008); constexpr auto TRON_NODIE = UINT32_C(0x00000008);
constexpr auto TRON_DEATH = UINT32_C(0x00000010); //Crashed, now dying constexpr auto TRON_DEATH = UINT32_C(0x00000010); //Crashed, now dying
constexpr auto TRON_NORANDOM = UINT32_C(0x00010000); constexpr auto TRON_NORANDOM = UINT32_C(0x00010000);
int tron_rx[4] = {-1, 0, 1, 0}; constexpr int tron_rx[4] = {-1, 0, 1, 0};
int tron_ry[4] = { 0,-1, 0, 1}; constexpr int tron_ry[4] = { 0,-1, 0, 1};
unsigned int tron_colours[32];
static void init_graphics() static const std::array<unsigned int, 32> MakeTronColors()
{ {
std::array<unsigned int, 32> tron_colours;
int i; int i;
int r, g, b; int r, g, b;
for (i=0; i<32; i++) for (i=0; i<32; i++)
{ {
// funny almost-bug: if (i<<4) > 360(ish), HSV_to_RGB does nothing with r/g/b,
// but since the variables are reused across iterations of the loop, they will still have sane values
HSV_to_RGB(i<<4,255,255,&r,&g,&b); HSV_to_RGB(i<<4,255,255,&r,&g,&b);
tron_colours[i] = r<<16 | g<<8 | b; tron_colours[i] = r<<16 | g<<8 | b;
} }
return tron_colours;
} }
static const auto tron_colours = MakeTronColors();
static int update(UPDATE_FUNC_ARGS) static int update(UPDATE_FUNC_ARGS)
{ {