todo
Last active
August 29, 2017 01:54
-
-
Save InfinitiesLoop/532dab0b849e580cb883b7f03ab1dbc9 to your computer and use it in GitHub Desktop.
OpenComputers Quary Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[*] | |
indent_style = space | |
indent_size = 2 | |
trim_trailing_whitespace = true | |
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local init = {} | |
function init.getfiles() | |
print("initializing files...") | |
local repo = nil | |
for line in io.lines("init.files") do | |
if repo == nil do | |
repo = line | |
print("repo " .. repo) | |
else | |
print("getting " .. line) | |
os.execute("wget https://raw.githubusercontent.com/" .. repo .. "/master/" .. line .. " " .. line) | |
end | |
end | |
print("done") | |
end | |
function init.clone(repo) | |
os.execute("wget https://raw.githubusercontent.com/" .. repo .. "/master/init.files init.files") | |
end | |
if arg[1] ~= nil then | |
init.clone(arg[1]) | |
end | |
return init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local inventory = {} | |
local function inventory.dropAll(robot, side) | |
-- tries to drop all the robot's inventory into the storage on the given side | |
-- returns true if all if it could be unloaded, false if none or only some could | |
--robot.drop([number]) -- Returns true if at least one item was dropped, false otherwise. | |
local couldDropAll = true | |
for i=0,robot.inventorySize() do | |
robot.select(i) | |
local c = robot.count() | |
if c > 0 then | |
robot.drop() | |
-- see if all the items were successfully dropped | |
c = robot.count() | |
if c > 0 then | |
-- at least one item couldn't be dropped. | |
-- but we keep trying to drop all so we still drop as much as we can. | |
couldDropAll = false | |
end | |
end | |
end | |
return couldDropAll | |
end | |
return inventory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local quary = require("quary") | |
local robot = require("robot") | |
local q = quary.new({robot = robot, depth = 30, width = 20}) | |
q:start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function trunc(num, numDecimalPlaces) | |
local mult = 10^(numDecimalPlaces or 0) | |
return math.floor(num * mult) / mult | |
end | |
return { | |
trunc: trunc | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local math = require("math") | |
local smartmove = require("smartmove") | |
local quary = {} | |
Quary = { | |
} | |
function Quary:canMine() | |
local d, dcurrent, dmax = self.robot.durability() | |
d = math.trunc(d or 0, 2) | |
if d <= 0 then | |
print("lost durability on tool!") | |
return false | |
end | |
return true | |
end | |
function Quary:_mineAhead() | |
if not self:canMine() then | |
return false | |
end | |
self.robot.swing() | |
if not self.move:forward() then | |
return false | |
end | |
if not self:canMine() then | |
return false | |
end | |
self.robot.swingUp() | |
if not self:canMine() then | |
return false | |
end | |
self.robot.swingDown() | |
return true | |
end | |
function Quary:_mineAroundCorner() | |
local orient = self.move.orient | |
if orient == 1 then | |
self.move:turnLeft() | |
else | |
self.move:turnRight() | |
end | |
if not self:_mineAhead() then | |
return false | |
end | |
if orient == 1 then | |
self.move:turnLeft() | |
else | |
self.move:turnRight() | |
end | |
return true | |
end | |
function Quary:_findStartingPoint() | |
-- at the start of a quary, it might be a quary in progress. | |
-- navigate the lanes to the left until we find where we left off. | |
self.move:turnLeft() | |
local moved = false | |
while self.move:forward() do | |
moved = true | |
self.stepsWidth = self.stepsWidth + 1 | |
if self.stepsWidth >= self.width then | |
print("looks like this quary is done, I couldn't find the starting point!") | |
self.move:turnRight() | |
return false | |
end | |
end | |
-- found it, and it's the very beginning | |
if not moved then | |
print("looks like a new quary! may the diamonds be plentiful!") | |
end | |
-- there was a block up or down so we're already in the starting spot | |
print("found starting point.") | |
self.move:turnRight() | |
return true | |
end | |
function Quary:mineNextLane() | |
local steps = 0 | |
while (steps < (self.depth - 1)) do | |
if not self:_mineAhead() then | |
print("could not mine main part of lane") | |
return false | |
end | |
steps = steps + 1 | |
end | |
if not self:_mineAroundCorner() then | |
print("could not turn corner") | |
return false | |
end | |
steps = 0 | |
while (steps < (self.depth - 1)) do | |
if not self:_mineAhead() then | |
print("could not mine return part of lane") | |
return false | |
end | |
steps = steps + 1 | |
end | |
return true | |
end | |
function Quary:backToStart() | |
self.move:moveTo(0, 0) | |
self.move:faceDirection(1) | |
end | |
function Quary:start() | |
self.stepsWidth = 0 | |
if not self:_mineAhead() then | |
print("could not enter quary area.") | |
self:backToStart() | |
return false | |
end | |
if not self:_findStartingPoint() then | |
print("could not find starting point.") | |
self:backToStart() | |
return false | |
end | |
while self.stepsWidth < self.width do | |
local result = self:mineNextLane() | |
if not result then | |
print("failed to mine lane") | |
self:backToStart() | |
return false | |
end | |
-- move to next lane | |
if not self:_mineAroundCorner() then | |
print("failed to turn corner into new lane.") | |
self:backToStart() | |
return false | |
end | |
self.stepsWidth = self.stepsWidth + 2 | |
end | |
self:backToStart() | |
return true | |
end | |
function quary.new(o) | |
o = o or {} | |
setmetatable(o, { __index = Quary }) | |
self.move = self.move or smartmove.new({robot=o.robot}) | |
return o | |
end | |
return quary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local smartmove = {} | |
SmartMove = { | |
posX = 0, | |
posY = 0, | |
orient = 1 | |
} | |
function SmartMove:_move(direction) | |
local result | |
if direction == 1 then | |
result = self.robot.forward() | |
else | |
result = self.robot.back() | |
end | |
if result then | |
if self.orient == 1 or self.orient == -1 then | |
self.posX = self.posX + (direction*self.orient) | |
else | |
self.posY = self.posY + (direction*self.orient/2) | |
end | |
end | |
return result | |
end | |
function SmartMove:forward() | |
return self:_move(1) | |
end | |
function SmartMove:backward() | |
return self:_move(-1) | |
end | |
function SmartMove:_turn(direction) | |
local result | |
if direction == 1 then | |
result = self.robot.turnRight() | |
else | |
result = self.robot.turnLeft() | |
end | |
if result then | |
if self.orient == 1 then | |
self.orient = direction * 2 | |
elseif self.orient == -1 then | |
self.orient = direction * -2 | |
elseif self.orient == 2 then | |
self.orient = direction * -1 | |
elseif self.orient == -2 then | |
self.orient = direction * 1 | |
end | |
end | |
end | |
function SmartMove:turnRight() | |
return self:_turn(1) | |
end | |
function SmartMove:turnLeft() | |
return self:_turn(-1) | |
end | |
function SmartMove:forwardUntilBlocked() | |
while self:forward() do | |
end | |
end | |
function SmartMove:faceDirection(o) | |
-- makes the robot oriented in the desired direction | |
-- by turning in the appropriate direction | |
if self.orient == o then | |
return true | |
end | |
if self.orient == -o then | |
-- 180 | |
self:turnRight() | |
self:turnRight() | |
-- probably could be more clever | |
elseif o == -1 and self.orient == -2 then | |
self:turnLeft() | |
elseif o == -1 and self.orient == 2 then | |
self:turnRight() | |
elseif o == 1 and self.orient == -2 then | |
self:turnRight() | |
elseif o == 1 and self.orient == 2 then | |
self:turnLeft() | |
elseif o == -2 and self.orient == -1 then | |
self:turnRight() | |
elseif o == -2 and self.orient == 1 then | |
self:turnLeft() | |
elseif o == 2 and self.orient == -1 then | |
self:turnLeft() | |
elseif o == 2 and self.orient == 1 then | |
self:turnRight() | |
end | |
end | |
function SmartMove:moveTo(x, y) | |
local moved = false | |
-- lets do X first, gotta reorient if necessary | |
if self.posX ~= x then | |
local direction | |
if self.posX < x then | |
direction = 1 | |
else | |
direction = -1 | |
end | |
self:faceDirection(direction) | |
while self.posX ~= x and self:forward() do | |
moved = true | |
end | |
end | |
if self.posY ~= y then | |
if self.posY < y then | |
direction = 2 | |
else | |
direction = -2 | |
end | |
self:faceDirection(direction) | |
while self.posY ~= y and self:forward() do | |
moved = true | |
end | |
end | |
-- try again | |
if moved and (self.posY ~= y or self.posX ~= x) then | |
self:moveTo(x, y) | |
end | |
end | |
local function smartmove.new(o) | |
o = o or {} | |
setmetatable(o, { __index = SmartMove }) | |
return o | |
end | |
return smartmove |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment