Skip to content

Instantly share code, notes, and snippets.

@MacChuck
Last active November 22, 2023 10:12
Show Gist options
  • Save MacChuck/cb745bae0beef7a9f2b77c2e565b5e95 to your computer and use it in GitHub Desktop.
Save MacChuck/cb745bae0beef7a9f2b77c2e565b5e95 to your computer and use it in GitHub Desktop.
maxSize = 27
edgeGap = 2
midGap = 5
math.randomseed(os.time())
grid = {}
-- set up basic grid values
for x = 1,maxSize do
grid[x] = {}
for y = 1,maxSize do
if (x <= edgeGap) or (y <= edgeGap) or (x > maxSize-edgeGap) or (y > maxSize-edgeGap) then
grid[x][y] = math.random(0,2)
elseif (x <= midGap) or (y <= midGap) or (x > maxSize-midGap) or (y > maxSize-midGap) then
grid[x][y] = math.random(0,4)
else
len1 = math.random(0,11)
len2 = math.random(0,11)
if len1 < len2 then
grid[x][y] = len1
else
grid[x][y] = len2
end
end
end
end
-- create edging around longer spikes
for x = 1,maxSize do
for y = 1,maxSize do
if grid[x][y] >= 9 then
if grid[x-1][y] < (grid[x][y] - 2) then
grid[x-1][y] = math.random((grid[x][y]-5),(grid[x][y]-2))
end
if grid[x+1][y] < (grid[x][y] - 2) then
grid[x+1][y] = math.random((grid[x][y]-5),(grid[x][y]-2))
end
if grid[x][y-1] < (grid[x][y] - 2) then
grid[x][y-1] = math.random((grid[x][y]-5),(grid[x][y]-2))
end
if grid[x][y+1] < (grid[x][y] - 2) then
grid[x][y+1] = math.random((grid[x][y]-5),(grid[x][y]-2))
end
--[[
for xo = -1,1,1 do
for yo = -1,1,1 do
if grid[x+xo][y] < (grid[x][y] - 2) then
grid[x+xo][y] = math.random((grid[x][y]-5),(grid[x][y]-2))
elseif grid[x][y+yo] < (grid[x][y] - 2) then
grid[x][y+yo] = math.random((grid[x][y]-5),(grid[x][y]-2))
end
end
end
--]]
end
end
end
function invCheck()
empty = true
for i=1,16 do
if turtle.getItemCount(i) > 0 then
empty = false
break
end
end
if empty then
print("Waiting for blocks, press enter when refilled.")
read()
end
end
function selectBlock()
slot = math.random(16)
turtle.select(slot)
while turtle.getItemCount(slot) == 0 do
slot = slot + 1
if slot == 17 then slot = 1 end
turtle.select(slot)
end
end
function placeUp()
invCheck()
selectBlock()
turtle.placeUp()
end
function placeSpike(length)
if not turtle.detectUp() then -- don't start a spike where the island doesn't exist above
return false
elseif length == 0 then -- 0 length, do nothing
return false
else
for i = 1,length do
turtle.down()
placeUp()
end
return true
end
end
function displayGrid()
mon = peripheral.wrap("right")
mon.setTextScale(.5)
mon.clear()
for x=1,maxSize do
for y=1,maxSize do
os.sleep(.1)
mon.setTextColor(colors.white)
mon.setCursorPos(x*2,y)
if grid[x][y] == 11 then
mon.setTextColor(colors.red)
mon.write(".")
elseif grid[x][y] == 10 then
mon.setTextColor(colors.red)
mon.write("X")
else
if grid[x][y] == 9 then
mon.setTextColor(colors.red)
elseif grid[x][y] >= 6 then
mon.setTextColor(colors.orange)
elseif grid[x][y] <= 2 then
mon.setTextColor(colors.lightBlue)
end
mon.write(tostring(grid[x][y]))
end
end
end
end
function build()
for x=1,maxSize do
for y=1,maxSize do
length = grid[x][y]
if x%2 == 0 then -- turtle runs a zigzag, reverse Y on every other row
length = grid[x][maxSize-y+1]
end
placed = placeSpike(length)
turtle.forward()
if placed then
for i=1,length do
turtle.up()
end
end
end
if x%2 == 0 then
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.forward()
else
turtle.turnRight()
turtle.forward()
turtle.turnRight()
turtle.forward()
end
end
end
build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment