Jump to content


Molinko's Content

There have been 2 items by Molinko (Search limited from 25-September 20)


By content type

See this member's

Sort by                Order  

#259346 KdControl - A somewhat advanced Turtle Controller

Posted by Molinko on 13 September 2016 - 11:42 PM in Turtle Programs

@TerraEnvy -

Sounds like someone needs to fuel the turtle.... :) Bomb Bloke is being subtle...



#259345 KdControl - A somewhat advanced Turtle Controller

Posted by Molinko on 13 September 2016 - 11:26 PM in Turtle Programs

I'd like to recommend a lookup table for the remote turtle program commands. Lookup tables are easier to read and easier to expand with more commands later in dev for your ease of programming :)

--...
--------CONFIGURATION--------
channel = ... -- <- this will take a string argument at the start of the program. Now people wont have to edit the file, just start with 'program uuid'
--------ACTUAL CODE--------
os.loadAPI("ender")--Created by Sxw on the Computercraft Forums
-- os.loadAPI("commands") ?YAY?
ender.connect(channel)

-- This commands table could even be loaded with os.loadAPI  and stored separately in another file...
commands = {
  forward = function(n)
	n = tonumber(n) -- still a string from being sent over modem and parsed. Maybe parseCmd() can convert string arguments to the proper type when parsing...
	for i = 1, n do
	  turtle.forward()
	end
  end,
  up = ...,
  down = ...,
  select = function(slot) -- this helps readability x16 ;)/>
    turtle.select(tonumber(slot))
  end
  -- ... and more
  dig3 = function() -- custom commands are easier to deal with here in my opinion
	turtle.digUp()
	turtle.digDown()
	turtle.dig()
  end
}

function parseCmd(message)
  local f, args, cmd = string.gmatch(message, "%S+"), {}, nil
  cmd = f()
  for arg_ in f do
	args[#args+1] = arg_
  end
  return cmd, args
end

function slot(num)
  turtle.select(num)
end

while true do
  worked = false
  while worked == false do
	worked, command = ender.receive(channel)
	command, args = parseCmd(command)
	sleep(0.1)
  end
  if commands[command] then
	commands[command](unpack(args))
  else
	print('invalid command')
  end
end



Thanks for listening.. Hope this helps with some more ideas!