From bcc328c0576bfd912da9b074e10d8722a514aece Mon Sep 17 00:00:00 2001 From: shylie Date: Mon, 4 May 2026 12:16:16 -0400 Subject: [PATCH] Try to prevent sand loss --- src/main.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 09fc1e4..87af814 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,8 +11,8 @@ #include #endif -constexpr uint16_t WIDTH = 256; -constexpr uint16_t HEIGHT = 256; +constexpr uint16_t WIDTH = 64; +constexpr uint16_t HEIGHT = 64; constexpr int WINDOW_WIDTH = 1024; constexpr int WINDOW_HEIGHT = 1024; @@ -28,6 +28,7 @@ int main(int argc, char** argv) auto air = tb.assign(); auto sand1 = tb.assign(); auto sand2 = tb.assign(); + auto sand3 = tb.assign(); tb.set_default_conversion(sand1, sand2); tb.set_default_conversion(sand2, sand1); @@ -41,16 +42,52 @@ int main(int argc, char** argv) rb.add_rule(air, sand1) .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) .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) .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) .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); @@ -60,7 +97,7 @@ int main(int argc, char** argv) { return { 0x40, 0x60, 0x80 }; } - else if (t == sand1 || t == sand2) + else if (t == sand1 || t == sand2 || t == sand3) { return { 0xB0, 0x90, 0x50 }; } @@ -79,6 +116,7 @@ int main(int argc, char** argv) int prev_ms = SDL_GetTicks(); float time = 0; bool cont = true; + int prev_count = 0; while (cont) { SDL_Event e; @@ -94,7 +132,6 @@ int main(int argc, char** argv) float dt = (ms - prev_ms) / 1000.0f; prev_ms = ms; time += dt; - SDL_Log("%f\n", dt); float x, y; int state; @@ -103,6 +140,28 @@ int main(int argc, char** argv) time = 0; 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))) @@ -112,7 +171,14 @@ int main(int argc, char** argv) if (ix >= 0 && ix < WIDTH && iy >= 0 && iy < HEIGHT) { - s.set(ix, iy, sand1); + if (state == SDL_BUTTON_LEFT) + { + s.set(ix, iy, sand1); + } + else + { + s.set(ix, iy, sand3); + } } }