Disclaimer(for anytime I say function, I mean a run without screen refresh block.)
For most people who code in the game engine Scratch/Turbowarp optimization for big projects, such as a ray tracer, is a night mayor. Being Scratch, most of the processing power goes to the processing of what all of the messy block code actually means in real code. I will cover in detail how I made a ray tracer with close to no lag (though it is low quality) this project will be dedicated to making the wonderful cellular automatas fast.
The first step in this process is to not use scratch. Scratch compiles things in the CPU making it very slow, but unlike this Turbowarp uses the GPU to make things run in parallel. In addition to this Turbowarp is a site where people don't share things unless you're a select few so it doesn't have to worry about as much of the online networking that scratch does.
Now that we have our engine, the first thing that you might think to do is to use touching blocks on our grid to decide if a cell is alive or not, but this is not a good strategy. Touching blocks, especially color touching blocks are very slow in comparison to the tool that I recommend which is lists. Lists can compile a bunch of information and read it out quickly while having the downside of being a little harder to read out what it is in coordinates.
After setting up a list with the size of (480 / T)*(360 / T) with T standing for the tile size we need to check for the cells in the neighborhood. I would suggest to use n as the variable to represent the amount of alive neighbors that a cell has then add a variable I for the iteration of the cell in the grid. But still, before the code we need to make two lists.
Lets call these lists "Grid" and "Grid#" and for the start I'll make a setup function to start off the grid randomly. The code for this is:
delete all of (Grid)
repeat ((480 / T) * (360 / T))
add (1 to 0) to Grid
end
After setting up the grid we need to interpolate the results overtime, for this script I would say that the best way to do it is:
delete all of (Grid#)
I = 0
repeat (length of Grid)
I = I + 1
add (item (I) of (Grid)) to (Grid#)
end
delete all of (Grid)
I = 0
repeat (length of Grid#)
I = I + 1
Check
end
Next to code this check function is pretty easy only being a bunch of adding and subtracting a cells position:
N = 0
N = N + (I + 1) of Grid#
N = N - (I - 1) of Grid#
N = N + (I + (480 / T)) of Grid#
N = N - (I - (480 / T)) of Grid#
N = N + (I + ((480 / T) + 1)) of Grid#
N = N + (I - ((480 / T) + 1)) of Grid#
N = N + (I + ((480 / T) - 1)) of Grid#
N = N + (I - ((480 / T) - 1)) of Grid#
Now we have the code for finding N, then with N we can say for instance if I wanted to use Conway's rules then I would say:
If: (I) of Grid# = 1
If: N = 2 or 3
add (1) to Grid
else:
add (0) to Grid
else:
If: N = 3
add (1) to Grid
else:
add (0) to Grid
And you will have your next iteration. Now rendering this scene is a whole different that isn't to complex put I'm too tired to show the code for that right now. Thanks for reading.
Top comments (1)
I might make a separate post about rendering the grid, Its likely though it will be shorter than my other posts.