simulation done

This commit is contained in:
2026-03-18 01:02:07 +00:00
parent 23add2c55f
commit eebe39c241
4 changed files with 80 additions and 31 deletions

View File

@@ -33,8 +33,10 @@ typedef double f64;
#define HEIGHT 600
// Simulation constants
#define SIZEOF_PROGRAM 64
#define NUM_PROGRAMS 64
#define SIZEOF_PROGRAM_POW_2 8
#define SIZEOF_PROGRAM (1 << SIZEOF_PROGRAM_POW_2)
#define NUM_PROGRAMS_POW_2 8
#define NUM_PROGRAMS (1 << NUM_PROGRAMS_POW_2)
#endif

View File

@@ -15,17 +15,30 @@
#include "vec.h"
#include "bf.h"
#include "simulation.h"
void simulation_soup(simulation_t *sim)
{
for (size_t i = 0; i < sizeof(sim->memory); ++i)
{
sim->memory[i] = (rand() % 82) + 43;
}
}
int main(void)
{
simulation_t sim = {0};
simulation_soup(&sim);
srand(time(NULL));
InitWindow(WIDTH, HEIGHT, "CompLife");
SetTargetFPS(60);
for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
{
simulation_iterate(&sim);
BeginDrawing();
ClearBackground(BLACK);
simulation_draw(&sim);
EndDrawing();
}
CloseWindow();

View File

@@ -4,6 +4,7 @@
* License: See end of file
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -14,41 +15,33 @@
static char *VALID_OPS = "<>{}-+.,[]";
static const Color possible_colors[] = {
{0xb4, 0x60, 0x5f, 0xff}, {0xe, 0x7, 0x7, 0xff},
{0x2c, 0x10, 0x10, 0xff}, {0x28, 0x21, 0x21, 0xff},
{0xbc, 0x71, 0x70, 0xff}, {0x4a, 0x31, 0x31, 0xff},
{0xc2, 0x68, 0x67, 0xff}, {0x7e, 0x40, 0x3f, 0xff},
{0x21, 0x9, 0x8, 0xff}, {0xcf, 0x35, 0x34, 0xff},
{0x0, 0x0, 0x0, 0xff}, {0},
['<'] = {0x2f, 0x4f, 0x4f, 0xff}, ['>'] = {0x22, 0x8b, 0x22, 0xff},
['{'] = {0xb0, 0x30, 0x60, 0xff}, ['}'] = {0xff, 0x00, 0x00, 0xff},
['-'] = {0xff, 0xff, 0x00, 0xff}, ['+'] = {0x00, 0xff, 0x00, 0xff},
['.'] = {0x00, 0xff, 0xff, 0xff}, [','] = {0xa0, 0x20, 0xf0, 0xff},
['['] = {0x1e, 0x90, 0xff, 0xff}, [']'] = {0xff, 0xde, 0xad, 0xff},
['\0'] = {0x0, 0x0, 0x0, 0xff},
};
void simulation_pick(u64 *a, u64 *b)
{
// Pick two indices that don't intersect
while (*a == *b)
{
*a = rand() % NUM_PROGRAMS;
*b = rand() % NUM_PROGRAMS;
}
}
void simulation_iterate(simulation_t *sim)
{
u64 a = 0, b = 0;
simulation_pick(&a, &b);
// Change of basis from program indices to bf_token indices.
a *= SIZEOF_PROGRAM;
b *= SIZEOF_PROGRAM;
sim->a = 0;
sim->b = 0;
while (sim->a == sim->b)
{
sim->a = rand() % NUM_PROGRAMS;
sim->b = rand() % NUM_PROGRAMS;
}
// Perform the catalytic reaction
struct ProgramConcat a_b_concat = {0};
program_concat(&a_b_concat, sim->memory + a, sim->memory + b);
program_concat(&a_b_concat, sim->memory + (sim->a * SIZEOF_PROGRAM),
sim->memory + (sim->b * SIZEOF_PROGRAM));
program_execute(&a_b_concat);
program_split(&a_b_concat);
}
u8 get_op(const u8 cell)
bf_token get_op(const bf_token cell)
{
if (strchr(VALID_OPS, cell))
return cell;
@@ -56,14 +49,50 @@ u8 get_op(const u8 cell)
return '\0';
}
Color simulation_cell_color(const u8 cell)
Color simulation_cell_color(const bf_token cell)
{
return possible_colors[get_op(cell)];
}
void simulation_draw(simulation_t *sim)
{
assert(0 && "TODO: implement simulation_draw");
auto const PROGRAM_ROW = (1 << (SIZEOF_PROGRAM_POW_2 / 2));
auto const SIMULATION_ROW = (1 << (NUM_PROGRAMS_POW_2 / 2));
auto const CELL_WIDTH = WIDTH / ((f64)(PROGRAM_ROW * SIMULATION_ROW));
auto const CELL_HEIGHT = HEIGHT / ((f64)(PROGRAM_ROW * SIMULATION_ROW));
for (size_t i = 0; i < NUM_PROGRAMS; ++i)
{
auto s_x = (i / SIMULATION_ROW) * PROGRAM_ROW;
auto s_y = (i % SIMULATION_ROW) * PROGRAM_ROW;
// printf("%lu => (%lu, %lu)\n", i, s_x, s_y);
const bf_token *base = sim->memory + (i * SIZEOF_PROGRAM);
for (u64 j = 0; j < SIZEOF_PROGRAM; ++j)
{
auto p_x = j / PROGRAM_ROW;
auto p_y = j % PROGRAM_ROW;
p_x += s_x;
p_y += s_y;
// printf("\t%lu => (%lu, %lu)\n", j, p_x, p_y);
auto color = simulation_cell_color(base[j]);
DrawRectangleV((Vector2){CELL_WIDTH * p_x, CELL_HEIGHT * p_y},
(Vector2){CELL_WIDTH, CELL_HEIGHT}, color);
}
if (sim->a == i || sim->b == i)
{
DrawRectangleLinesEx((Rectangle){.x = s_x * CELL_WIDTH,
.y = s_y * CELL_HEIGHT,
.width = PROGRAM_ROW * CELL_WIDTH,
.height = PROGRAM_ROW * CELL_HEIGHT},
1, BLUE);
}
}
}
/* Copyright (C) 2026 Aryadev Chavali

View File

@@ -9,9 +9,12 @@
#include "bf.h"
#define SIMULATION_SIZE (NUM_PROGRAMS * SIZEOF_PROGRAM)
typedef struct
{
bf_token memory[NUM_PROGRAMS * SIZEOF_PROGRAM];
u64 a, b;
bf_token memory[SIMULATION_SIZE];
} simulation_t;
void simulation_iterate(simulation_t *sim);
@@ -22,12 +25,14 @@ void simulation_draw(simulation_t *sim);
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
* details.
* You may distribute and modify this code under the terms of the GNU General
* Public License Version 2, which you should have received a copy of along with
* Public License Version 2, which you should have received a copy of along
with
* this program. If not, please go to <https://www.gnu.org/licenses/>.
*/