blug/site/snad.html

135 lines
9.4 KiB
HTML
Raw Permalink Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2024-08-29 21:19:07 +02:00
<title>something silly - snad</title>
<link rel="stylesheet" href="/style.css">
2024-07-06 15:41:31 +02:00
<link rel="icon" type="image/x-icon" href="/creacher.png">
</head>
<body>
<main>
<h1 id="cell-thingy">cell thingy</h1>
<p>
<code>2024-06-02</code><br>
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 <a href="https://www.youtube.com/watch?v=xvlsJ3FqNYU">cellpond</a> and <a href="https://github.com/mxgmn/MarkovJunior">Markov Junior</a>. If you have <a href="https://www.rust-lang.org/">rust</a> installed, you can try it yourself! The source code is <a href="https://git.crispypin.cc/CrispyPin/snad">here</a> and <a href="https://github.com/CrispyPin/snad">here</a>.<br>
</p>
<p>
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.<br>
The engine randomly finds places where the one of your rules' <em>input (left)</em> picture matches a place in the world, and replaces it with the <em>output (right)</em> picture.<br>
</p>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/snake_in_maze.mp4" alt="" title="" controls ></video>
<h2 id="groups">groups</h2>
<p>
A rule can also have cells containing a <em>group</em>. 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.<br>
2024-11-03 22:45:17 +01:00
<img loading="lazy" src="/media/snad/group_example.png" alt="" title="">
</p>
<h2 id="copy-cells">copy cells</h2>
<p>
On the output side, a cell can be set to <em>copy</em> 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<br>
2024-11-03 22:45:17 +01:00
<img loading="lazy" src="/media/snad/copy_example.png" alt="" title="">
</p>
<h1 id="random-is-easy-right">random is easy, right?</h1>
<p>
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.<br>
</p>
<p>
A rule can have <em>blank</em> cells. On the input side, this means it will match anything, and on the output side, the rule will leave whatever was already there.<br>
</p>
<h2 id="worm">worm</h2>
<p>
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 <em>input</em> 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.<br>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/worm_right.mp4" alt="" title="" controls ></video>
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?<br>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/worm_left.mp4" alt="" title="" controls ></video>
</p>
<p>
Almost. It no longer moves past the edge.<br>
</p>
<p>
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.<br>
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<br>
</p>
<p>
The solution is relatively straightforward, increase the area of possible positions by the size of the picked rule in the negative directions.<br>
</p>
<h2 id="caching">caching</h2>
<p>
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.<br>
</p>
<p>
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.<br>
</p>
<p>
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.<br>
</p>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/sand_fall_faster.mp4" alt="" title="" controls ></video>
<p>
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 <a href="#just-one-more-implementation">just one more implementation</a>)<br>
</p>
<p>
I wanted the old behavior back, where rule executions are evenly spread out over space and time.<br>
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...<br>
</p>
<h2 id="swarm">swarm</h2>
<p>
I want to simulate a swarm of bees, so I create a rule for a bee moving by one cell, and enable <code>rotate</code> so it works in all directions. That was easy,<br>
</p>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/bees_biased.mp4" alt="" title="" controls ></video>
<p>
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.<br>
</p>
<p>
I was stuck on this problem for quite a while, and it only happened with the new cached executor.<br>
</p>
<p>
The movement rule has these variants:<br>
2024-11-03 22:45:17 +01:00
<img loading="lazy" src="/media/snad/bee_rules_rotated.png" alt="" title="">
</p>
<p>
Each rule has its origin point in the top-left cell.<br>
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.<br>
2024-11-03 22:45:17 +01:00
<img loading="lazy" src="/media/snad/bee_movement_pattern.png" alt="" title="">
</p>
<p>
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 <em>two</em> possible rules to execute. Therefore, moving right and down both have half the probability overall compared to moving up or left.<br>
</p>
<h3 id="the-overlap-checker">the overlap checker</h3>
<p>
In this version, the cache is not only searched for matches <em>at</em> the chosen position, but also <em>overlapping</em>. Given that, it only needs to pick positions within the world.<br>
</p>
<p>
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 <a href="https://gaussian.dev/">friend</a> suggested, an absolute-position based checker seemed (and indeed was) more performant and simpler, so I did that one first.<br>
</p>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/bees.mp4" alt="" title="" controls ></video>
<p>
Now the bees are evenly distributed, just like real bees!<sup>(false)</sup><br>
</p>
<h2 id="just-one-more-implementation">just one more implementation</h2>
<p>
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.<br>
</p>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/rule_size_bias.mp4" alt="" title="" controls ></video>
<p>
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.<br>
</p>
<p>
Alright, the Final (I <em>hope</em>) 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.<br>
</p>
<h2 id="fin">fin</h2>
<p>
If you somehow read through all that, congratulations I think? Now enjoy this fire:<br>
2024-08-29 21:19:07 +02:00
<video loading="lazy" src="/media/snad/fire.mp4" alt="" title="" controls ></video>
</p>
<p>
Download the thing and make something fun: <a href="https://git.crispypin.cc/CrispyPin/snad">git.crispypin.cc/CrispyPin/snad</a><br>
</p>
<p>
Or go back to my <a href="/">other garbage</a><br>
</main>
</body>
</html>