Updated harvest and added movescript.
This commit is contained in:
parent
80be066744
commit
f9f2be629a
|
@ -0,0 +1,27 @@
|
|||
# movescript.lua
|
||||
A more convenient way to move a robot.
|
||||
|
||||
## Pastebin
|
||||
[4c2AN8Jw](https://pastebin.com/4c2AN8Jw)
|
||||
|
||||
## Module Requirements
|
||||
*None*
|
||||
|
||||
## Instructions
|
||||
Simply run `pastebin get 4c2AN8Jw /home/lib/movescript` to install movescript to the local computer. Then you can begin writing movescripts to tell your robots how to move. For example:
|
||||
|
||||
```
|
||||
local ms = require("movescript")
|
||||
|
||||
local my_script = "FFURFFL"
|
||||
ms.execute(my_script)
|
||||
```
|
||||
|
||||
The above code will make the robot move forward twice, then up once, then turn right, then move forward twice, and finally turn left.
|
||||
|
||||
It is also possible to specify the amount of times to perform an action, by giving an integer value before the character for the movement you want. For example:
|
||||
|
||||
```
|
||||
local my_long_script = "10D2R5B3U"
|
||||
ms.execute(my_long_script)
|
||||
```
|
|
@ -0,0 +1,71 @@
|
|||
--[[
|
||||
Author: Andrew Lalis
|
||||
File: movescript.lua
|
||||
Version: 1.0
|
||||
Last Modified: 27-09-2018
|
||||
|
||||
Description:
|
||||
This library enables string representation of robot movement, for easier
|
||||
robotic control without repeating functions many times.
|
||||
--]]
|
||||
|
||||
local r = require("robot")
|
||||
|
||||
local movescript = {}
|
||||
|
||||
local functionMap = {
|
||||
["U"] = r.up,
|
||||
["D"] = r.down,
|
||||
["L"] = r.turnLeft,
|
||||
["R"] = r.turnRight,
|
||||
["F"] = r.forward,
|
||||
["B"] = r.back
|
||||
}
|
||||
|
||||
--[[
|
||||
Executes a single instruction once.
|
||||
c - character: One uppercase character to translate into movement.
|
||||
--]]
|
||||
local function executeChar(c)
|
||||
local f = functionMap[c]
|
||||
if (f == nil) then
|
||||
return
|
||||
end
|
||||
local success = f()
|
||||
while (not success) do
|
||||
success = f()
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
Executes a single instruction, such as '15D'
|
||||
instruction - string: An integer followed by an uppercase character.
|
||||
--]]
|
||||
local function executeInstruction(instruction)
|
||||
local count = string.match(instruction, "%d+")
|
||||
local char = string.match(instruction, "%u")
|
||||
if (count == nil) then
|
||||
count = 1
|
||||
end
|
||||
if (char == nil) then
|
||||
return
|
||||
end
|
||||
for i=1,count do
|
||||
executeChar(char)
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
Executes a given script.
|
||||
script - string: The script to execute.
|
||||
--]]
|
||||
function movescript.execute(script)
|
||||
while (script ~= nil and script ~= "") do
|
||||
-- Matches the next instruction, possibly prefixed by an integer value.
|
||||
local next_instruction = string.match(script, "%d*%u")
|
||||
executeInstruction(next_instruction)
|
||||
script = string.sub(script, string.len(next_instruction) + 1)
|
||||
end
|
||||
end
|
||||
|
||||
return movescript
|
|
@ -25,16 +25,7 @@ local LEFT = 1
|
|||
local RIGHT = 0
|
||||
|
||||
-- List of crops which will be harvested.
|
||||
local crop_definitions = {
|
||||
["harvestcraft:pamsoybeancrop"] = {
|
||||
growth_limit = 0.42,
|
||||
item_name = "harvestcraft:soybeanitem"
|
||||
},
|
||||
["harvestcraft:pamspiceleafcrop"] = {
|
||||
growth_limit = 0.42,
|
||||
item_name = "harvestcraft:spiceleafitem"
|
||||
}
|
||||
}
|
||||
local crop_definitions = {}
|
||||
|
||||
-- Repeats the given function until it returns true.
|
||||
local function doUntilSuccess(func)
|
||||
|
@ -170,6 +161,7 @@ return - int: The total number of crops harvested.
|
|||
local function harvestField(rows, columns, start_location)
|
||||
goToStart(rows, columns)
|
||||
-- Begin harvesting.
|
||||
robot.select(1)
|
||||
local harvests = 0
|
||||
for i=1,rows do
|
||||
harvests = harvests + harvestRow(columns)
|
||||
|
@ -206,16 +198,15 @@ return - table|nil: The table defined in config, or nil if the file does not
|
|||
exist or another error occurs.
|
||||
--]]
|
||||
local function loadConfig(filename)
|
||||
if (fs.exists(filename) and not fs.isDirectory(filename)) then
|
||||
-- Config file exists.
|
||||
file = io.open(filename, "r")
|
||||
local t = serial.unserialize(file:read())
|
||||
file:close()
|
||||
return t
|
||||
else
|
||||
local f = io.open(filename, "r")
|
||||
if (f == nil) then
|
||||
print("No config file " .. filename .. " exists. Please create it before continuing.")
|
||||
return nil
|
||||
end
|
||||
local t = serial.unserialize(f:read())
|
||||
f:close()
|
||||
return t
|
||||
end
|
||||
|
||||
--[[
|
||||
|
|
Loading…
Reference in New Issue