Try to prevent sand loss

This commit is contained in:
shylie 2026-05-04 12:16:16 -04:00
parent 5d63e04b17
commit bcc328c057
Signed by: shylie
GPG Key ID: 20C8D70580687D9D

View File

@ -11,8 +11,8 @@
#include <tracy/Tracy.hpp> #include <tracy/Tracy.hpp>
#endif #endif
constexpr uint16_t WIDTH = 256; constexpr uint16_t WIDTH = 64;
constexpr uint16_t HEIGHT = 256; constexpr uint16_t HEIGHT = 64;
constexpr int WINDOW_WIDTH = 1024; constexpr int WINDOW_WIDTH = 1024;
constexpr int WINDOW_HEIGHT = 1024; constexpr int WINDOW_HEIGHT = 1024;
@ -28,6 +28,7 @@ int main(int argc, char** argv)
auto air = tb.assign(); auto air = tb.assign();
auto sand1 = tb.assign(); auto sand1 = tb.assign();
auto sand2 = tb.assign(); auto sand2 = tb.assign();
auto sand3 = tb.assign();
tb.set_default_conversion(sand1, sand2); tb.set_default_conversion(sand1, sand2);
tb.set_default_conversion(sand2, sand1); tb.set_default_conversion(sand2, sand1);
@ -41,16 +42,52 @@ int main(int argc, char** argv)
rb.add_rule(air, sand1) rb.add_rule(air, sand1)
.top_right([=](auto t) { return t == sand2; }) .top_right([=](auto t) { return t == sand2; })
.right([=](auto t) { return t != air && t != sand::type::OFF_GRID; }); .right([=](auto t) { return t != air; })
.top_left([=](auto t) { return t != sand1; });
rb.add_rule(air, sand2) rb.add_rule(air, sand2)
.top_left([=](auto t) { return t == sand1; }) .top_left([=](auto t) { return t == sand1; })
.left([=](auto t) { return t != air && t != sand::type::OFF_GRID; }); .left([=](auto t) { return t != air; })
.top_right([=](auto t) { return t != sand2; });
rb.add_rule(air, sand1)
.top_right([=](auto t) { return t == sand2; })
.right([=](auto t) { return t != air; })
.left([=](auto t) { return t == air; })
.top([=](auto t) { return t == air; });
rb.add_rule(air, sand2)
.top_left([=](auto t) { return t == sand1; })
.left([=](auto t) { return t != air; })
.right([=](auto t) { return t == air; })
.top([=](auto t) { return t == air; });
rb.add_rule(air, sand3)
.top_right([=](auto t) { return t == sand2; })
.right([=](auto t) { return t != air; })
.top_left([=](auto t) { return t == sand1; })
.left([=](auto t) { return t != air; })
.top([=](auto t) { return t == air; });
rb.add_rule(sand3, sand1).top([=](auto t) { return t == air; });
rb.add_rule(air, sand1)
.bottom([=](auto t) { return t == sand3; })
.top([=](auto t) { return t != sand1 && t != sand2; });
rb.add_rule(air, sand3)
.bottom([=](auto t) { return t == sand3; })
.top([=](auto t) { return t == sand1 || t == sand2; });
rb.add_rule(sand1, air) rb.add_rule(sand1, air)
.bottom_right([=](auto t) { return t == air; }) .bottom_right([=](auto t) { return t == air; })
.bottom([=](auto t) { return t != air && t != sand::type::OFF_GRID; }); .bottom([=](auto t) { return t != air; })
.right([=](auto t) { return t == air; });
rb.add_rule(sand2, air) rb.add_rule(sand2, air)
.bottom_left([=](auto t) { return t == air; }) .bottom_left([=](auto t) { return t == air; })
.bottom([=](auto t) { return t != air && t != sand::type::OFF_GRID; }); .bottom([=](auto t) { return t != air; })
.left([=](auto t) { return t == air; });
rb.add_rule(sand1, sand3).bottom([=](auto t) { return t == sand3; });
rb.add_rule(sand2, sand3).bottom([=](auto t) { return t == sand3; });
rb.add_rule(sand3, sand1)
.top([=](auto t) { return t == sand1 || t == sand2; });
auto s = rb.build(WIDTH, HEIGHT, air); auto s = rb.build(WIDTH, HEIGHT, air);
@ -60,7 +97,7 @@ int main(int argc, char** argv)
{ {
return { 0x40, 0x60, 0x80 }; return { 0x40, 0x60, 0x80 };
} }
else if (t == sand1 || t == sand2) else if (t == sand1 || t == sand2 || t == sand3)
{ {
return { 0xB0, 0x90, 0x50 }; return { 0xB0, 0x90, 0x50 };
} }
@ -79,6 +116,7 @@ int main(int argc, char** argv)
int prev_ms = SDL_GetTicks(); int prev_ms = SDL_GetTicks();
float time = 0; float time = 0;
bool cont = true; bool cont = true;
int prev_count = 0;
while (cont) while (cont)
{ {
SDL_Event e; SDL_Event e;
@ -94,7 +132,6 @@ int main(int argc, char** argv)
float dt = (ms - prev_ms) / 1000.0f; float dt = (ms - prev_ms) / 1000.0f;
prev_ms = ms; prev_ms = ms;
time += dt; time += dt;
SDL_Log("%f\n", dt);
float x, y; float x, y;
int state; int state;
@ -103,6 +140,28 @@ int main(int argc, char** argv)
time = 0; time = 0;
s.tick(); s.tick();
int count = 0;
for (int i = 0; i < WIDTH; i++)
{
for (int j = 0; j < HEIGHT; j++)
{
if (s.get(i, j) == sand1 || s.get(i, j) == sand2)
{
count += 1;
}
else if (s.get(i, j) == sand3)
{
count += 2;
}
}
}
if (count != prev_count)
{
SDL_Log("%d\n", count);
}
prev_count = count;
} }
if ((state = SDL_GetMouseState(&x, &y))) if ((state = SDL_GetMouseState(&x, &y)))
@ -111,9 +170,16 @@ int main(int argc, char** argv)
int iy = y * HEIGHT / WINDOW_HEIGHT; int iy = y * HEIGHT / WINDOW_HEIGHT;
if (ix >= 0 && ix < WIDTH && iy >= 0 && iy < HEIGHT) if (ix >= 0 && ix < WIDTH && iy >= 0 && iy < HEIGHT)
{
if (state == SDL_BUTTON_LEFT)
{ {
s.set(ix, iy, sand1); s.set(ix, iy, sand1);
} }
else
{
s.set(ix, iy, sand3);
}
}
} }
SDL_LockSurface(surface); SDL_LockSurface(surface);