r/Tf2Scripts • u/prolvlwhale • Jul 26 '22
Script Conway's Game of Life script
I wrote a script that runs Conway's Game of Life in the console! I like this one more than the brainfuck interpreter since it it easier to play around with, and I had more fun writing it. There is some overlap between the techniques used in them though, so I'm glad I did this one with the brainfuck knowledge.
Anyways, if anyone has any questions, comments or suggestions on how to make the script or its documentation better, I would love to hear them!
14
Upvotes
View all comments
Show parent comment
1
u/prolvlwhale Jul 27 '22
Thanks, I think it's cool as well.
As for what I learned (besides some computer science stuff like how to make a fast incrementor/decrementor), the most important and interesting things that make both of these programs work are pointer aliases and arbitrary boolean aliases. (This next part will be really long and potentially confusing, so ask if you need anything clarified)
CW: Computer science
Pointer aliases are used to make functions (such as test_p in life.cfg or test_0 in brainf.cfg) MUCH more modular. Without them, each cell would have to have their own test function. Instead of doing this, the life program simply stores each cell in the pointer, then immediately tests the pointer. This is a life-saver, both to read and write.
Arbitrary boolean functions are the method that literally allows all of the step to work. They allow for bits to be set to true or false in a very simple way: by literally setting the bit alias to be 'true' or 'false'. This replaced my sv_allow_wait_command method (which was awful and it took me an embarassingly long amount of time to come up with fix, to the point where I had to rewrite the near-finished brainfuck compiler), and works on a simple principle: that if there is an alias guaranteed to run when a bit is tested, then that alias can be simply rebound right before testing based on the alias they are called from. For example, in brainf.cfg, the incrementor rebinds the true and false aliases for each bit (since each bit flips a different bit's state), and then calls (or tests) the bit.
These two techniques combined are what make these programs work, and can be most obviously seen in the incrementor/decrementor functions in brainf.cfg, and in the memory values/cells for both brainf and life.
In terms of how the life program works, it is actually pretty simple. Each generation the following happens in order:
This can then be looped through as many time as the user wants, or can be used without a loop function if the user wants to go through more slowly.
The output function starts at the first row, and uses the row pointer to point to each cell in that row in order. This is important because the output function works with a row pointer to make it more modular, and they are not grouped into rows normally. The output row function is probably one of the worst things in computer science history: it is essentially just a 13-layer full binary tree, representing the 4096 states each row can be in. I hate this part of the code so much, but I have not found a way around it since you have to end print statements with a newline in the console. The binary tree in navigated through repeated use of rebinding the arbitrary boolean functions to go to different branches, which is simple, if awful. I had a python script write this for me since I do not hate myself that much.
Rebinding the update function is also pretty simple, and looks much better than the output function. It relies a lot on the cell data structure (or what makes up each cell). Each cell has:
The cell file was also made via python.
With this cell structure, it is easy to rebind the update function: set up a cycle (like from the scripting page on the wiki) that calls the proper update-rebinding function each time (like rebinding update to kill when one cell is alive, and rebinding update to revive when two are alive), then set the arbitrary boolean 'true' to go to the next element in the cycle. Once this is done, when the surrounding spaces are tested, any time the true alias is called, the cycle will trigger and update will be rebound!
Instead of making one of these for each cell, I chose to make one for the pointer, and then store whichever cell I wanted to update in the pointer, since that makes it look much nicer.
And finally, the update function is very simple: simply call the update for each cell, since they have all been rebound. It is important to do this after rebinding update on every cell, since otherwise newly created or killed cells will be taken into account before they are supposed to.
I understand that probably didn't make a lot of sense since I'm not that good at explaining things, but if you have any questions feel free to ask and I will provide more helpful and specific information. If you REALLY want to understand how everything works, I highly suggest using this comment as a guide and looking at the code, since I think a lot of these specific examples make much more sense with context when you see how it all fits together.
That, or I've fully gone insane and no amount of reading the code will make it make sense to anyone.