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

TurtleAI, need help with handling userInput

Started by Conan1981m, 05 January 2014 - 10:21 AM
Conan1981m #1
Posted 05 January 2014 - 11:21 AM
Hey there ;-)

i watched some tutorial and
i wrote some code,
added a lot of own functions, some are working well already …


but i have a big problem with the
read()
i use so far …
i want to get a user Input with variables like
go 10
or
location 230 67 230


i want it in one line, with different amount of vars for different commands, thats actually the problem :wacko:/>

and thats what needs to change actually

<line 613>
function uInput()
	term.setCursorPos(1,8)
	write("what do you want me to do, Master ?")
	term.setCursorPos(1,9)
	write("											")
	term.setCursorPos(1,9)
	order = read()
end
and

if
    order == "down" then
    moveDown()
    reFuel() 
--other commands
  elseif
    order == "refill" then
    reFuel()
  elseif
    order == "base" then
    toBase()
you can see my code here, but be warned, its quite ugly so far :rolleyes:/>
but hey, its working :P/>
dont complain too much :D/>
http://pastebin.com/hae3quw8

its saving its location and direction in an external file,
can set a base, with fuelchest and dropchest on specific side if needed,
it can quarry, unload and resume
can get fuel and return to task,
it can temporarily host gps if needed ( for whatever you might need it)


If i solve this, it will be able to move to xyz coordinate,
dig its way there if neccesary,

I want to add some network functions as well, make it RC
Soon there will be some Brainbug - PC guiding its drones to do what it wants muahahahahaaa B)/> :lol:/> :lol:/>
Edited on 05 January 2014 - 12:27 PM
Moody #2
Posted 05 January 2014 - 12:40 PM
So you want to dismember a string into its parts?
use this api: http://computercraft...le=String_(API)

Example from the Lua wiki:

s = "hello world from Lua"
	 for w in string.gmatch(s, "%a+") do
	   print(w)
  end
prints every word in a single line.

you can just dump them into a table (t[#t+1] = w) and you can get the total count of words by using ( #t )

you have to be a bit carefull, because apparently some regEx s wont work in CC, but for your purpose it should be okay
Edited on 05 January 2014 - 11:49 AM
Conan1981m #3
Posted 05 January 2014 - 01:51 PM
Thanks for the quick reply ;-)

Well im not familiar with tables either so far …

hmm that doesnt help really …

I dont wanna print the words, i want to use them as sort of arguments.

Can you please explain more ?
Edited on 06 January 2014 - 06:02 AM
Moody #4
Posted 06 January 2014 - 08:12 AM
hey, what you did was kinda … strange
try to look at this code:


input = "gogo 3 4 6 to 9"
local t = { }
local argsT = { }

function process(ins)
	   local lookup = {   ["gogo"] = function(x,y,z) print("Hi! X: "..x.." Y: "..y.." Z: "..z) end,
								  ["to"] = function(x) print("Only one value here: "..x) end
							  }																											   -- lookup-table for easier processing
	   print("Current order: "..ins[1])																						 -- print current task/order
	   lookup[ins[1]](table.unpack(ins,2))																				 -- do the according function in the lookuptable
end

for w in string.gmatch(input,"%w+") do  -- this searches for every alphanumeric word in your string ( read "input" by using read() )
  if tonumber(w) == nil then					-- every word that is NOT a number (== a command): make a new Table to store its arguments
	t[#t+1] = argsT
	argsT = {}
	argsT[1] = w
  else													  -- if there are several arguments, store them all in the same table
	argsT[#argsT+1] = w
  end
end
t[#t+1] = argsT										-- add the last arguments to the table
table.remove(t,1)									-- remove the wrongly added empty table at the beginning

-- here you got a table that is something like that:
-- [1] = { gogo, 3, 4, 6 }
-- [2] = { to, 9 }


for i=1,#t,1 do
  process(t[i])										  -- process the orders
end


returns


Current order: gogo
Hi! X: 3 Y: 4 Z: 6
Current order: to
Only one value here: 9


maybe it is a bit hard to understand, but i just do it like this:

1. read the commandline inputs ( here "gogo 3 4 6 to 9" )
2. put each command block into a table ( here argsT = { "gogo", 3, 4, 6} and { "to", 9 } )
3. put each table into a bigger table so you can easily process them. Also it doesnt limit you to a certain amount of input commands ( thats the case when you use a construct like argsT1 = … argsT99 = … - you can only process a limited amount of commands)
4. look up every command block in the lookup table (i saved the command in argsT[1] = "gogo" / "to" ) and run its function

if you have any questions about the code, please ask, i know its kinda messy and not very elaborated, but i hope it will do
Edited on 06 January 2014 - 07:56 AM
Conan1981m #5
Posted 06 January 2014 - 12:57 PM
Well, what it returns to me is
Current order: gogo
input:12: attempt to call nil

which seems to be a mistake in
or maybe i run an old version with tekkt ..

		   lookup[ins[1]](table.unpack(ins,2))	 -- do the according function in the lookuptable

and why do we remove a wrongly added line in t ??
because of t[#t+1] , there is no t[1] declared right ?

i think i get the table thing slowly …
Moody #6
Posted 06 January 2014 - 03:03 PM
Well, what it returns to me is
Current order: gogo
input:12: attempt to call nil

okay, sorry, i cant test it on Computercraft right now, im using the Lua demo ( http://www.lua.org/cgi-bin/demo )
it works there, maybe something doesnt work in CC / Lua 5.1 - i will look into this tomorrow

which seems to be a mistake in
or maybe i run an old version with tekkt ..

		   lookup[ins[1]](table.unpack(ins,2))	 -- do the according function in the lookuptable

and why do we remove a wrongly added line in t ??
because of t[#t+1] , there is no t[1] declared right ?

i think i get the table thing slowly …

because when we first go into the for loop, the first word is a command ("gogo") which causes my code to make a new table argsT and store the old table in "t" - but at first the argsT is empty, so it inserts an empty table.
I dont need a empty table at the beginning, so i remove it.
if a table is empty, #t returns 0 . so 0 +1 = 1 –> t[1] exists, but it is an empty table.



EDIT:

Apparently before LUA 5.2 table.unpack() was just called unpack(), so i renamed that and it works for me

input = "gogo 3 4 6 to 9"
local t = { }
local argsT = { }
function process(ins)
		   local lookup = {   ["gogo"] = function(x,y,z) print("Hi! X: "..x.." Y: "..y.." Z: "..z) end,
																  ["to"] = function(x) print("Only one value here: "..x) end
														  }																																																					    -- lookup-table for easier processing
		   print("Current order: "..ins[1])																																											  -- print current task/order
		   lookup[ins[1]](unpack(ins,2))																																						   -- do the according function in the lookuptable
end
for w in string.gmatch(input,"%w+") do  -- this searches for every alphanumeric word in your string ( read "input" by using read() )
  if tonumber(w) == nil then								    -- every word that is NOT a number (== a command): make a new Table to store its arguments
	    t[#t+1] = argsT
	    argsT = {}
	    argsT[1] = w
  else																								    -- if there are several arguments, store them all in the same table
	    argsT[#argsT+1] = w
  end
end
t[#t+1] = argsT																		 -- add the last arguments to the table
table.remove(t,1)																	   -- remove the wrongly added empty table at the beginning
-- here you got a table that is something like that:
-- [1] = { gogo, 3, 4, 6 }
-- [2] = { to, 9 }

for i=1,#t,1 do
  process(t[i])																		   -- process the orders
end
Edited on 07 January 2014 - 03:57 AM