Created
January 8, 2021 22:53
-
-
Save Powersaurus/e318ef94ad044027c1eaf58f4d4ea899 to your computer and use it in GitHub Desktop.
Diamond Squared algorithm in #pico8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- not actually a cart, but paste it into the pico-8 code section | |
| -- diamond squared implementation | |
| -- by ben jones/@powersaurus | |
| -- based on this wikipedia image https://en.wikipedia.org/wiki/diamond-square_algorithm#/media/file:diamond_square.svg | |
| -- https://en.wikipedia.org/wiki/diamond-square_algorithm | |
| -- it is probably wrong. looks nice though | |
| function _init() | |
| printh("\n ███████████████") | |
| printh(" █new board!!!!█") | |
| printh(" ███████████████") | |
| grid={} | |
| height=129 | |
| width=129 | |
| step=height-1 | |
| rand=255 | |
| randomness=1.3 | |
| iter=0 | |
| -- everything starts at same height | |
| for i=0,height-1 do | |
| grid[i]={} | |
| for j=0,width-1 do | |
| grid[i][j]=64--rnd(128) | |
| end | |
| end | |
| end | |
| function _update() | |
| if btnp(❎) then | |
| do_step() | |
| iter+=1 | |
| printh("iteration "..iter) | |
| end | |
| end | |
| -- iterate on the heightmap | |
| function do_step() | |
| printh(step) | |
| if(step<1)step=1 | |
| printh("\n\n>>>") | |
| for y=-height*2,height*2,step do | |
| for x=-width*2,width*2,step do | |
| half_step=flr(step/2) | |
| vx=(x+half_step)%width | |
| vy=(y+half_step)%height | |
| printh("visit "..vx..", "..vy) | |
| xx=x%width | |
| yy=y%height | |
| a=grid[yy][xx] | |
| b=grid[yy][(xx+step)%width] | |
| c=grid[(yy+step)%height][xx] | |
| d=grid[(yy+step)%height][(xx+step)%width] | |
| grid[vy][vx]= | |
| flr( | |
| (a+b+ | |
| c+d) | |
| /4) | |
| new_h=grid[vy][vx] | |
| grid[(vy-half_step)%height][vx]=randomise(new_h) | |
| grid[vy][(vx+half_step)%width]=randomise(new_h) | |
| grid[(vy+half_step)%width][vx]=randomise(new_h) | |
| grid[vy][(vx-half_step)%width]=randomise(new_h) | |
| end | |
| end | |
| step/=2 | |
| rand/=randomness | |
| step=flr(step) | |
| printh("---") | |
| end | |
| -- add some randomness to a value | |
| function randomise(v) | |
| return new_h+flr(rnd(rand))-rand/2 | |
| end | |
| -- heightmap, lighter colours for | |
| -- higher points, darker colours for | |
| -- lower points | |
| function hm(v) | |
| if v>90 then | |
| return 7 | |
| elseif v>70 then | |
| return 6 | |
| elseif v>40 then | |
| return 13 | |
| elseif v>10 then | |
| return 5 | |
| else | |
| return 1 | |
| end | |
| end | |
| function cloud(v) | |
| if v>40 then | |
| return 7 | |
| else | |
| return 1 | |
| end | |
| end | |
| function _draw() | |
| -- draw grid of pixels | |
| for i=0,0 do | |
| for j=0,0 do | |
| for y=0,height-1 do | |
| for x=0,width-1 do | |
| pset( | |
| i*(width-1)+x, | |
| j*(height-1)+y, | |
| hm(grid[y][x])) | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment