Posted 27 January 2014 - 05:22 PM
I'm currently working on a turtle OS that will have some useful programs. So far, what I have works, but I can't help but think that the code could be shorter and a little less complicated. Any tips on simplification is appreciated. There's three codes so far that I have.
The startup code, which displays all the programs in a directory as multiple choice options, with configurable visibility:
Then the tower code, which just builds a square tower with configurable width and height:
And finally a program that will build a gregtech industrial grinder, and dig out the area for it if requested:
I'm relatively new to coding, so I haven't got the ability for aesthetically pleasing code yet. Any suggestions or tips are greatly appreciated!
The startup code, which displays all the programs in a directory as multiple choice options, with configurable visibility:
Spoiler
choice=fs.list('')
newTable=fs.list('')
hidden={'startup','rom','KeyNumb'}
function hideValues(array,hidden)
for i,v in ipairs(array) do
for i2,v2 in ipairs(hidden) do
if v==v2 then
table.remove(array, i)
return hideValues(array,hidden)
end
end
end
end
function lineClear()
x,y=term.getCursorPos()
term.clearLine()
term.setCursorPos(1,y)
end
function printCentered(str)
x,y=term.getSize()
x2,y2=term.getCursorPos()
newX=math.ceil(x/2)-math.ceil(string.len(str)/2)
term.setCursorPos(newX,y2)
print(str)
end
function replaceDelim(str, delim)
result=''
for i in string.gmatch(str, '%a+') do
result=result..i..delim
end
return result
end
function spacifyTable(array)
for i,v in ipairs(array) do
array[i]=replaceDelim(v,' ')
end
end
function getArraySize(array)
count=0
for _ in ipairs(array) do
count=count+1
end
return count
end
function multipleChoice(choices, initial)
size=getArraySize(choices)
if not initial then
sel=1
else
sel=initial
end
while true do
header()
for i,v in ipairs(choices) do
if i==sel then
printCentered('[ '..v..']')
else
printCentered(' '..v..' ')
end
end
event,key=os.pullEvent('key')
if (key==208 or key==31) and sel<=size then sel=sel+1 end
if (key==208 or key==31) and sel==size+1 then sel=1 end
if (key==200 or key==17) and sel>0 then sel=sel-1 end
if (key==200 or key==17) and sel==0 then sel=size end
if key==28 or key==57 then break end
sleep(0.01)
end
return sel
end
function header()
term.clear()
term.setCursorPos(1,1)
print("HimCo Industries' Turtley 2.0")
print()
printCentered('Select a template:')
print()
end
hideValues(choice,hidden)
spacifyTable(choice)
file=multipleChoice(choice)
hideValues(newTable,hidden)
shell.run(newTable[file])
Then the tower code, which just builds a square tower with configurable width and height:
Spoiler
--args={...}
--local width=tonumber(args[1])
--local height=tonumber(args[2])
term.clear()
term.setCursorPos(1,1)
function lineClear()
local x,y=term.getCursorPos()
term.setCursorPos(1,y)
term.clearLine()
end
function dichotomy(a,B)/>/>/>
sel=true
while true do
if sel then
write("--> "..a.." "..B)/>/>/>
else
write(" "..a.." --> "..B)/>/>/>
end
event,key=os.pullEvent()
if event=="key" and key==203 then sel=not sel end
if event=="key" and key==205 then sel=not sel end
if event=="key" and key==28 then break end
sleep(0.01)
lineClear()
end
print()
return sel
end
function numbersOnly(pre)
local x,y=term.getCursorPos()
if pre then write(pre) end
local num=read()
if not tonumber(num) then
term.setCursorPos(x,y)
term.clearLine()
write('Please insert a number only')
sleep(1.5)
term.setCursorPos(x,y)
term.clearLine()
return numbersOnly(pre)
end
return tonumber(num)
end
function returnToBase(height)
turtle.turnRight()
turtle.turnRight()
turtle.forward()
for i=1, height+1 do
turtle.down()
end
end
function getDimensions()
term.clear()
term.setCursorPos(1,1)
print('Give the values for the dimensions of the tower.')
print()
width=numbersOnly('Please give a value for the width: ')
height=numbersOnly('Please give a value for the height: ')
if width<0 or height<0 or height>256 then
print()
print('Check your numbers. The dimensions must be greater than 0, and the height cannot be greater than world heigh. (256)')
sleep(2)
return getDimensions()
end
return width,height
end
function tower(width, height)
for i1=1, height do
turtle.up()
for i2=1, 4 do
for i3=1, width-1 do
turtle.forward()
if not selectResource() then
print()
print('There are no available resources.')
print('Please add more to continue.')
while not selectResource() do
sleep(3)
end
term.clear()
term.setCursorPos(1,1)
print('Construction will continue')
end
turtle.placeDown()
end
turtle.turnRight()
end
end
print('Build Complete')
if toBase then
returnToBase(height)
end
end
function calculateBlocks(width, height)
local shell=(width^2*height)-((width-2)^2*height)
local stacks=(shell-(shell%64))/64
local extra=shell%64
term.clear()
term.setCursorPos(1,1)
print('Would you like the turtle to return to the tower base when finished?')
if dichotomy('Yes','No') then
toBase=true
else toBase=false
end
term.clear()
term.setCursorPos(1,1)
print('You will need 64x'..tostring(stacks)..'+'..tostring(extra)..' blocks for this tower.')
print()
print('When the turtle has depleted one slot, it will select the next available resource regardless of the type.')
print()
print('Add the required amount to the turtle, then select continue to proceed')
if dichotomy('Continue','Quit') then
print()
print('Construction will begin')
sleep(1.5)
term.clear()
term.setCursorPos(1,1)
else
os.reboot()
end
if countResources()<shell then
term.clear()
term.setCursorPos(1,1)
print('You still do not have enough resources to complete this build.')
print()
print('If the turtle runs out, it will stall until more are added.')
print()
print('Continue anyway?')
if dichotomy('Continue','Quit') then
print('Construction will begin')
sleep(1.5)
term.clear()
term.setCursorPos(1,1)
else
os.reboot()
end
end
return shell
end
function countResources()
local count=0
for i4=1, 16 do
count=count+turtle.getItemCount(i4)
end
return count
end
function selectResource()
for i5=1, 16 do
if turtle.getItemCount(i5)>0 then
turtle.select(i5)
return true
end
end
return false
end
width,height=getDimensions()
calculateBlocks(width,height)
tower(width,height)
os.reboot()
And finally a program that will build a gregtech industrial grinder, and dig out the area for it if requested:
Spoiler
diggingMode=false
function lineClear()
x,y=term.getCursorPos()
term.clearLine()
term.setCursorPos(1,y)
end
function dichotomy(a,B)/>/>/>
sel=true
while true do
if sel then
write("--> "..a.." "..B)/>/>/>
else
write(" "..a.." --> "..B)/>/>/>
end
event,key=os.pullEvent()
if event=="key" and key==203 then sel=not sel end
if event=="key" and key==205 then sel=not sel end
if event=="key" and key==28 then break end
sleep(0.01)
lineClear()
end
print()
return sel
end
function enableDiggingMode()
print('Do you wish to enable digging mode?')
print('If you do so, the turtle will clear a 3x3x3 area for the casing.')
print('Turtle with digging tool required. (Obviously)')
print()
print('Enable digging mode?')
if dichotomy('Yes','No') then
diggingMode=true
else
diggingMode=false
end
end
function placeSlot(side,slot)
if turtle.getItemCount(slot)==0 then
print('Insufficient resources to complete build. Please place an item in slot '..tostring(slot))
while turtle.getItemCount(slot)==0 do
sleep(0.01)
end
end
turtle.select(slot)
placeSide(side)
end
function placeSide(side)
if not side then
turtle.place()
elseif side=='up' then
turtle.placeUp()
elseif side=='down' then
turtle.placeDown()
elseif side=='left' then
turtle.turnLeft()
turtle.place()
turtle.turnRight()
elseif side=='right' then
turtle.turnRight()
turtle.place()
turtle.turnLeft()
elseif side=='front' then
turtle.place()
elseif side=='back' then
turtle.turnRight()
turtle.turnRight()
turtle.place()
turtle.turnRight()
turtle.turnRight()
end
end
function caveLayers(final)
layer1Reverse=false
for i=1, 3 do
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.dig()
turtle.forward()
turtle.digUp()
if not layer1Reverse and i~=3 then
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnRight()
elseif layer1Reverse and i~=3 then
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
end
layer1Reverse=not layer1Reverse
end
if not final then
turtle.digUp()
turtle.up()
turtle.turnRight()
turtle.turnRight()
turtle.digUp()
else
turtle.turnRight()
turtle.turnRight()
end
end
function layer1()
if not diggingMode then
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.up()
turtle.forward()
end
placeSlot('down',1)
for i2=1, 4 do
for i3=1, 2 do
turtle.forward()
placeSlot('down',1)
end
turtle.turnRight()
end
turtle.forward()
turtle.turnRight()
turtle.forward()
placeSlot('down',1)
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.turnRight()
end
function layer2()
turtle.up()
for i2=1, 4 do
for i3=1, 2 do
turtle.forward()
placeSlot('down',2)
end
turtle.turnRight()
end
turtle.forward()
turtle.turnRight()
turtle.forward()
placeSlot('down',3)
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.turnRight()
end
function layer3()
turtleInvert=false
turtle.forward()
for i2=1, 3 do
placeSlot('front',1)
for i=1, 2 do
if not turtleInvert then turtle.turnRight() else turtle.turnLeft() end
turtle.forward()
if not turtleInvert then turtle.turnLeft() else turtle.turnRight() end
placeSlot('front',1)
end
turtleInvert=not turtleInvert
if i2~=3 then
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.turnRight()
else
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.down()
placeSlot('back',4)
turtle.down()
turtle.turnRight()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
turtle.turnRight()
end
end
end
function info()
term.clear()
term.setCursorPos(1,1)
enableDiggingMode()
term.clear()
term.setCursorPos(1,1)
print('Please place 18 standard machine casings in slot 1, 8 reinforced casings in slot 2, water in slot 3, and an industrial grinder in slot 4.')
print()
print('Add the required materials to the turtle, then select continue to proceed')
if dichotomy('Continue','Quit') then
print()
print('Construction will begin')
sleep(1.5)
term.clear()
term.setCursorPos(1,1)
else
os.reboot()
end
if countResources()<28 then
term.clear()
term.setCursorPos(1,1)
print('There is a discrepency in the number of required materials.')
print()
print('If the turtle runs out, it will stall until more are added.')
print()
print('Continue anyway?')
if dichotomy('Continue','Quit') then
print('Construction will begin')
sleep(1.5)
term.clear()
term.setCursorPos(1,1)
else
os.reboot()
end
end
end
function countResources()
local count=0
for i4=1, 16 do
count=count+turtle.getItemCount(i4)
end
return count
end
info()
if diggingMode then
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.digUp()
caveLayers()
caveLayers(true)
end
layer1()
layer2()
layer3()
os.reboot()
I'm relatively new to coding, so I haven't got the ability for aesthetically pleasing code yet. Any suggestions or tips are greatly appreciated!