This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
robbyrit's profile picture

Help Cleaning Up Formatting/Program

Started by robbyrit, 07 October 2016 - 10:52 PM
robbyrit #1
Posted 08 October 2016 - 12:52 AM
Hello,

I'm very new to computercraft and am attempting to learn LUA. I've written this simple program to create and store gas in a chemistry lab i've built in my world. The program works currently, though I haven't attempted it with the "refuel()" functionality yet. Mostly I'd like suggestions in terms of properly formatting my code (I have read up on this a bit on the forums/internet), and/or using loops or other aspects of lua i may not be familiar with to make the code better.

Thank you in advance for any constructive criticism.

Spoiler

local fuelLevel = turtle.getFuelLevel()

local function getfuel()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  for i = 1, 16, 1
  do
	turtle.select(i)
	turtle.suck(64)
  end
end

local function refuel()
if (fuelLevel < 5000) then
	do
	  getfuel()
	  shell.run("refuel all")
	  turtle.turnRight()
	  turtle.forward()
	  turtle.turnLeft()
	end
end

local function forward(blocks)
  for i = 1, blocks do
	turtle.forward()
  end
end

local function carbon()
  turtle.select(1)
  turtle.suck(2)
end

local function chlorine()
  turtle.select(3)
  turtle.suck(4)
end

local function oxygen()
  turtle.select(2)
  turtle.suck(2)
end

local function move()
  turtle.turnRight()
  forward(3)
  turtle.turnLeft()
end

local function tosynth()
  turtle.turnRight()
  turtle.back()
  turtle.turnRight()
  turtle.up()
  turtle.forward()
  turtle.turnLeft()
end

local function dumbmachine()
  turtle.select(4)
  turtle.suck(1)
  turtle.select(5)
  turtle.dig()
  turtle.place()
  turtle.select(4)
  turtle.drop()
end

local function phosgene()
  for i = 1,3,1
  do
	turtle.select(i)
	turtle.drop()
  end
end

local function fromsynth()
	turtle.down()
	turtle.forward()
	turtle.select(16)
	turtle.suckUp(1)
	os.sleep(1)
	turtle.suckUp(1)
end

local function transpose()
  turtle.back()
  turtle.back()
  turtle.turnRight()
  forward(3)
  turtle.up()
  turtle.select(16)
  turtle.drop(2)
  turtle.down()
end

local function tostart()
  turtle.turnRight()
  forward(4)
  turtle.turnRight()
  forward(4)
  turtle.down()
end

while true
  do
  refuel()
  carbon()
  move()
  chlorine()
  move()
  oxygen()
  tosynth()
  dumbmachine()
  os.sleep(1)
  phosgene()
  os.sleep(5)
  fromsynth()
  transpose()
  tostart()
  os.sleep(5)
end
ebernerd #2
Posted 08 October 2016 - 01:11 AM
The only thing I would suggest changing is the "do -> end" statements within the if statements.


if (1>2) then
  return true
end

is just fine, no need for

if (1>2) then
  do
    return true
  end
end
Bomb Bloke #3
Posted 08 October 2016 - 01:42 AM
Note that you do require "do" when constructing a "for" loop - it's just redundant in most other cases.