blug/site/cellthing.html

118 lines
No EOL
7.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>something silly - cellthing</title>
<link rel="stylesheet" href="/style.css">
<link rel="icon" type="image/x-icon" href="/cretin.png">
</head>
<body>
<main>
<h1 id="cell-thingy">cell thingy</h1>
<p>
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>
<p>
<video src="snad/snake_in_maze.mp4" controls></video><br>
</p>
<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>
<img src="snad/group_example.png" alt=""><br>
</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>
<img src="snad/copy_example.png" alt=""><br>
</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>
<video src="snad/worm_right.mp4" controls></video><br>
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>
<video src="snad/worm_left.mp4" controls></video><br>
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>
<p>
<video src="snad/sand_fall_faster.mp4" controls></video><br>
</p>
<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>
<p>
<video src="snad/bees.mp4" controls></video><br>
</p>
<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>
<img src="snad/bee_rules_rotated.png" alt=""><br>
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>
<img src="snad/bee_movement_pattern.png" alt=""><br>
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>
<p>
TODO explain overlap checker<br>
</p>
<h2 id="just-one-more-implementation">just one more implementation</h2>
<p>
I wasn't happy with the performance difference<br>
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<br>
</p>
<p>
TODO video different sized blocks falling<br>
</p>
<p>
TODO explain rule origin shifting<br>
</p>
<p>
go back to my <a href="/">other garbag</a><br>
</main>
</body>
</html>