blug/write/snad.md

7.7 KiB

cell thingy

2024-06-02 I made/am making some kind of asynchronous cellular automata or cell-based spatial programming environment. Idk, those are fancy words for a silly experiment. It is inspired by cellpond and Markov Junior. If you have rust installed, you can try it yourself! The source code is here and here.

The world consists of a grid of cells, and you can create new cell types and assign them names and colours. You can create rules, which are like the chemistry of this world. You make a pattern on the left and a pattern on the right. The engine randomly finds places where the one of your rules' input (left) picture matches a place in the world, and replaces it with the output (right) picture.

==video:"/media/snad/snake_in_maze.mp4"

groups

A rule can also have cells containing a group. On the input side, that cell will match anything in the group, and on the output side it will become a random cell from the group. A diagonal line through the group icon means that it can also match on positions outside the world, where there are no cells. ==image:"/media/snad/group_example.png"

copy cells

On the output side, a cell can be set to copy the contents of one cell from the input side, this is useful for applying the same rule to several cell types without randomly swapping between them on the output ==image:"/media/snad/copy_example.png"

random is easy, right?

The simplest way to make this engine run, and what I did at first, is to (many times per frame) pick a random position within the world, then pick a random rule and see if it matches at that position. This worked to get going but has some limitations and poor performance.

A rule can have blank cells. On the input side, this means it will match anything, and on the output side, the rule will leave whatever was already there.

worm

Let's say I make a rule for a little worm that moves to the right, this works fine. Its head disappears out of the world because the cell in front of the head in the input pattern is blank, which matches anything, including out of bounds. To make it stop just before the edge, you could set that rule cell to air. ==video:"/media/snad/worm_right.mp4" Now if I want it to do the same thing in but to the left, I can simply draw the rule and worm in the other direction, right? ==video:"/media/snad/worm_left.mp4"

Almost. It no longer moves past the edge.

When a rule and position is chosen, the top-left of the rule is aligned with that position. In the example above, the rule would have to start from outside the world to make the worm continue one more step. When it is going to the right, the top-left of the worm is always inside the world, which allows it to keep moving as long as the worm itself is intact

The solution is relatively straightforward, increase the area of possible positions by the size of the picked rule in the negative directions.

caching

Checking a random rule and random position over and over is quite inefficient. Often an area is checked again despite not having changed and therefore definitely still not matching. The solution to this is of course caching.

Once at the start, the engine checks every possible position and every rule, and stores a list of matching locations for each rule. Every time a rule is successfully executed, all cached matches that overlap the affected area are removed in case they are no longer valid, then the engine checks for new matches in the affected area.

Now, how should it even use that information? The first thing I tried was to randomly pick one of all the matches. This kind of works but leads to the simulation speed being dependent on the number of matches, so for example sand falls slower if there is a lot of sand that hasn't landed yet.

==video:"/media/snad/sand_fall_faster.mp4"

I got a slightly more even result by multiplying the speed (number of rule executions per frame) by the total number of matches, but this is not a perfect solution as it does not take into account overlapping matches. A cell contained in several rule matches would be more likely to be affected, which in some cases can lead to weird bias (see just one more implementation)

I wanted the old behavior back, where rule executions are evenly spread out over space and time. So now the engine picks a random position, then checks the cache and picks a random match at that position. This seemed to work well, until it didn't...

swarm

I want to simulate a swarm of bees, so I create a rule for a bee moving by one cell, and enable rotate so it works in all directions. That was easy,

==video:"/media/snad/bees_biased.mp4"

Oh no, the swarm is moving toward the top left instead of spreading equally. But I can see that the bees move in all four directions, so the rule seems to be rotated correctly.

I was stuck on this problem for quite a while, and it only happened with the new cached executor.

The movement rule has these variants: ==image:"/media/snad/bee_rules_rotated.png"

Each rule has its origin point in the top-left cell. In the diagram below, the bee would move left when the left (purple) cell is picked by the engine, and up when the top (green) cell is picked. ==image:"/media/snad/bee_movement_pattern.png"

But to move right or down, it needs the bees own position to be picked by the engine, since the rules always extend right-down. That means when the engine picks the bee's position, there are two possible rules to execute. Therefore, moving right and down both have half the probability overall compared to moving up or left.

the overlap checker

In this version, the cache is not only searched for matches at the chosen position, but also overlapping. Given that, it only needs to pick positions within the world.

Since the root issue was caused by asymmetry, I thought this implementation should be correct. This one was actually my first instinct when I created the cache, but as a friend suggested, an absolute-position based checker seemed (and indeed was) more performant and simpler, so I did that one first.

==video:"/media/snad/bees.mp4"

Now the bees are evenly distributed, just like real bees!(false)

just one more implementation

While I was trying to figure out the reason for the directional bias in the previous implementation, I realised that the overlap checker also has biases. The probability of any given cached match to be executed is dependent on its area, which is not very intuitive. Two objects of different mass fall at different speeds, and adding empty padding to a rule makes it run faster.

==video:"/media/snad/rule_size_bias.mp4"

Ideally the red, pink and blue cells should all fall at around the same rate, but the red sand always lands first and the pink last.

Alright, the Final (I hope) implementation stores an origin offset for rules. This is zero for normal rules, but is automatically modified by rotation and mirroring, so that the origin always falls on the same actual rule cell. The picked position now needs to extend on all four edges, since rules may happen to have their origin point on a blank input cell, making it possible to trigger partially outside the world bounds. The amount to extend by is determined by the maximum width and height of all the rule variants, minus one since at least one cell has to be inside the bounds for the rule to have any effect.

fin

If you somehow read through all that, congratulations I think? Now enjoy this fire: ==video:"/media/snad/fire.mp4"

Download the thing and make something fun: git.crispypin.cc/CrispyPin/snad

Or go back to my other garbage