Skip to content

Instantly share code, notes, and snippets.

@Dalethium
Last active August 24, 2019 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dalethium/d3169a1498a75c5c4c0d8bd8d3e2d0f6 to your computer and use it in GitHub Desktop.
Save Dalethium/d3169a1498a75c5c4c0d8bd8d3e2d0f6 to your computer and use it in GitHub Desktop.
lua
function sel()
turtle.select(5)
if turtle.getItemCount() < 1 then
turtle.select(6)
end
end
function down()
local result = turtle.detectDown()
while not result do
turtle.down()
result = turtle.detectDown()
end
end
function place()
sel()
turtle.placeDown()
end
function rim()
place()
turtle.forward()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.forward()
place()
turtle.forward()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.forward()
place()
turtle.forward()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.forward()
place()
turtle.forward()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.forward()
end
function lid()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
place()
turtle.forward()
place()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
end
function build()
down()
turtle.up()
lid()
turtle.forward()
turtle.forward()
turtle.turnLeft()
for i = 1, 5 do
rim()
turtle.up()
end
turtle.down()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
turtle.down()
turtle.down()
turtle.select(7)
turtle.placeDown()
turtle.up()
turtle.up()
lid()
for i = 1, 9 do
turtle.up()
end
turtle.select(8)
turtle.dropDown(1)
sleep(30)
end
function loop()
for x = 1, 64 do
turtle.select(5)
if turtle.getItemCount() > 63 then
turtle.select(6)
if turtle.getItemCount() > 33 then
build()
end
end
down()
turtle.up()
turtle.suckDown()
turtle.select(2)
turtle.placeDown()
turtle.up()
turtle.select(3)
turtle.placeDown()
for i = 1, 12 do
turtle.up()
end
turtle.select(4)
turtle.dropDown(1)
sleep(8)
for i = 1, 13 do
turtle.down()
end
turtle.select(5)
if turtle.getItemCount() > 63 then
turtle.select(6)
end
end
end
function quitProgram()
while true do
local event, param1 = os.pullEvent("char")
if param1 == "q" then
quit = true
end
sleep(0.05)
end
end
function run()
parallel.waitForAny(quitProgram, loop)
if quit then
print("Quitting....")
end
end
down()
for i = 1, 14 do
turtle.up()
end
run()
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function dumpFile(o, e)
local file = fs.open(e, "a")
file.write("\n" .. o)
file.flush()
file.close()
end
modules = peripheral.wrap("left")
while true do
sleep(1)
local o = modules.getInventory().list()
for k,v in pairs(o) do
if (k > 9) then
modules.getInventory().pushItems("right", k)
end
end
end
os.unloadAPI(shell.resolve("exceptional"))
os.loadAPI(shell.resolve("exceptional"))
modules = peripheral.wrap("left")
function main()
while true do
try {
function()
local o = modules.getInventory().list()
for k,v in pairs(o) do
if (v["name"] == "inventorypets:illuminati_pet") then
modules.getInventory().pushItems("back", k)
end
end
sleep(0.5)
modules.getInventory().pullItems("back", 1)
sleep(120.1)
end
} :catch {
function(e)
sleep(30)
os.reboot()
end
}
end
end
quit = false
function quitProgram()
while true do
local event, param1 = os.pullEvent("char")
if param1 == "q" then
quit = true
end
sleep(0.05)
end
end
function run()
parallel.waitForAny(quitProgram, main)
if quit then
print("Quitting....")
end
end
run()
local modules = peripheral.find("neuralInterface")
if not modules then error("Must have a neural interface", 0) end
if not modules.hasModule("plethora:laser") then error("Cannot find laser", 0) end
if not modules.hasModule("plethora:scanner") then error("Must have a scanner", 0) end
if not modules.hasModule("plethora:sensor") then error("Cannot find entity sensor", 0) end
local tx,ty = term.getSize()
local cx = 2
local cy = 2
function setPos(ex, ey)
cx = ex
cy = ey
--term.write(cy)
term.setCursorPos(ex, ey)
end
function writeLine(x, y, s)
setPos(x, y)
term.clearLine()
term.write(s)
end
local function fire(entity)
if entity.name ~= "Daleth" then
writeLine(1,1,entity.name)
end
local x1, y1, z1 = entity.x, entity.y, entity.z
local pitch = -math.atan2(y1, math.sqrt(x1 * x1 + z1 * z1))
local yaw = math.atan2(-x1, z1)
local meta = modules.getMetaOwner and modules.getMetaOwner()
local x0 = meta.x
local y0 = meta.y
local z0 = meta.z
local vec0 = vector.new(x1, y1, z1)
local vec1 = vector.new(x0, y0, z0)
local dist = vec0:sub(vec1):length()
writeLine(1,3,dist)
local scannerRange = 8
local scannerWidth = scannerRange * 2 + 1
local fire = true
--[[ local scanned_blocks = modules.scan()
for x = -scannerRange, scannerRange do
for y = -scannerRange, scannerRange do
for z = -scannerRange, scannerRange do
local scanned = scanned_blocks[scannerWidth ^ 2 * (x + scannerRange) + scannerWidth * (y + scannerRange) + (z + scannerRange) + 1]
--local vec2 = vector.new(ex, ey, ez)
--local d = (vec0:sub(vec1):cross(vec0:sub(vec2))):length() / (vec2:sub(vec1)):length()
if scanned ~= nil then
if scanned.name ~= undefined then
if scanned.name ~= "minecraft:air" then
if scanned.name ~= "minecraft:water" then
writeLine(1,2,scanned.name)
local dx = x1 - x0
local dy = y1 - y0
local dz = z1 - z0
local ex = x - x0
local ey = y - y0
local ez = z - z0
local q = dx * ex
local q = q + (dy * ey)
local q = q + (dz * ez)
if q < 0 then
fire = true
else
local q = q * q
local q = q / (dx * dx + dy * dy + dz * dz)
local q = q / (ex * ex + ey * ey + ez * ez)
writeLine(1,4,q)
if q <= 1.0 then
fire = false
break
else
fire = true
end
end
end
end
end
end
end
if fire == false then
break
end
end
if fire == false then
break
end
end--]]
if fire then
modules.fire(math.deg(yaw), math.deg(pitch), 5.00)
end
return fire
end
local mob_registry = {
["Creeper"] = {
["Threat"] = 12,
["Kill"] = true
},
["Zombie"] = {
["Threat"] = 10,
["Kill"] = true
},
["Husk"] = {
["Threat"] = 0,
["Kill"] = true
},
["Spider"] = {
["Threat"] = 10,
["Kill"] = true
},
["Skeleton"] = {
["Threat"] = 10,
["Kill"] = true
},
["abyzombie"] = {
["Threat"] = 11,
["Kill"] = true
},
["babyskeleton"] = {
["Threat"] = 11,
["Kill"] = true
},
["babycreeper"] = {
["Threat"] = 13,
["Kill"] = true
},
["babyspider"] = {
["Threat"] = 11,
["Kill"] = true
},
["zombiechicken"] = {
["Threat"] = 10,
["Kill"] = true
},
["aqua_creeper"] = {
["Threat"] = 15,
["Kill"] = true
},
["Guardian"] = {
["Threat"] = 7,
["Kill"] = true
},
["Squid"] = {
["Threat"] = 1,
["Kill"] = true
},
["babysquid"] = {
["Threat"] = 0,
["Kill"] = true
}
}
term.clear()
local candidates = {}
function a()
while true do
local mobs = modules.sense()
for i = 1, #mobs do
local mob = mobs[i]
if mob_registry[mob.name] ~= nil then
if mob_registry[mob.name]["Kill"] then
candidates[#candidates + 1] = mob
end
end
end
sleep(0.05)
if #candidates > 0 then
local mob = candidates[math.random(1, #candidates)]
local cdist = 99999
for i = 1, #candidates do
local x1, y1, z1 = candidates[i].x, candidates[i].y, candidates[i].z
local pitch = -math.atan2(y1, math.sqrt(x1 * x1 + z1 * z1))
local yaw = math.atan2(-x1, z1)
local meta = modules.getMetaOwner and modules.getMetaOwner()
local x0 = meta.x
local y0 = meta.y
local z0 = meta.z
local vec0 = vector.new(x1, y1, z1)
local vec1 = vector.new(x0, y0, z0)
local dist = vec0:sub(vec1):length()
if dist < cdist then
mob = candidates[i]
cdist = dist
end
end
if mob ~= nil then
fire(mob)
end
end
candidates = {}
sleep(0.05)
end
end
a()
quit = false
function quitProgram()
local event, param1 = os.pullEvent("char")
if param1 == "q" then
quit = true
end
end
function run()
while true do
parallel.waitForAny(quitProgram, main)
if quit then
print("Quitting....")
break
end
--Other code
sleep(0.05) --Sleep 1 tick
end
end
local modules = peripheral.find("neuralInterface")
if not modules then error("Must have a neural interface", 0) end
if not modules.hasModule("plethora:sensor") then error("Must have a sensor", 0) end
if not modules.hasModule("plethora:scanner") then error("Must have a scanner", 0) end
if not modules.hasModule("plethora:introspection") then error("Must have an introspection module", 0) end
if not modules.hasModule("plethora:kinetic", 0) then error("Must have a kinetic agument", 0) end
function tsave(table,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(table))
file.close()
end
local tx,ty = term.getSize()
local cx = 2
local cy = 2
function setPos(ex, ey)
cx = ex
cy = ey
--term.write(cy)
term.setCursorPos(ex, ey)
end
function writeLine(x, y, s)
setPos(x, y)
term.clearLine()
term.write(s)
end
function tload(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function tableMerge(t1, t2)
for k,v in pairs(t2) do
if type(v) == "table" then
if type(t1[k] or false) == "table" then
tableMerge(t1[k] or {}, t2[k] or {})
else
t1[k] = v
end
else
t1[k] = v
end
end
return t1
end
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
function empty(table)
for _, value in pairs(table) do
if value ~= nil then
return false
end
end
return true
end
-- Cache of the current turtle position and direction
local cachedX, cachedY, cachedZ, cachedDir = 0, 67, 0, 0
-- Directions
North, West, South, East, Up, Down = 0, 1, 2, 3, 4, 5
local shortNames = {[North] = "N", [West] = "W", [South] = "S",
[East] = "E", [Up] = "U", [Down] = "D" }
local deltas = {[North] = {0, 0, -1}, [West] = {-1, 0, 0}, [South] = {0, 0, 1},
[East] = {1, 0, 0}, [Up] = {0, 1, 0}, [Down] = {0, -1, 0}}
-- cache world geometry
local cachedWorld = {}
local cachedBlocks = {}
-- A* parameters
local stopAt, nilCost = 999999999, 1000
local protect_plants = true
function searchWorld(element)
for key, value in pairs(cachedBlocks) do
if value.name == element then
return key
end
end
return false
end
----------------------------------------
-- clearWorld
--
-- function: Clear the world cache
--
function clearWorld()
cachedWorld = {}
detectAll()
end
function getWorld()
return cachedWorld
end
function getBlocks()
return cachedBlocks
end
function setBlocks(b)
cachedBlocks = b
end
function setWorld(world)
cachedWorld = world
end
----------------------------------------
-- getLocation
--
-- function: Retrieve the cached turtle position and direction
-- return: cached X, Y, Z and direction of the turtle
--
function getLocation()
update()
return cachedX, cachedY, cachedZ
end
function saveAll()
tsave(getWorld(), "Dalethium/data/world/geometry")
--local loc = {}
--loc[1], loc[2], loc[3], loc[4] = getLocation()
---tsave(loc, "Dalethium/data/world/pos")
tsave(getBlocks(), "Dalethium/data/world/blocks")
end
function loadAll()
setWorld(tload("Dalethium/data/world/geometry"))
--local loc = tload("Dalethium/data/world/pos")
--setLocation(loc[1], loc[2], loc[3], loc[4])
setBlocks(tload("Dalethium/data/world/blocks"))
end
function update()
local meta = modules.getMetaOwner and modules.getMetaOwner()
cachedX = meta.x
cachedY = meta.y
cachedZ = meta.z
detectAll()
end
----------------------------------------
-- detectAll
--
-- function: Detect in all possible directions
--
local scannerRange = 8
local scannerWidth = scannerRange * 2 + 1
function detectAll()
local meta = modules.getMetaOwner and modules.getMetaOwner()
local zcachedX = meta.x
local zcachedY = meta.y
local zcachedZ = meta.z
local scanned_blocks = modules.scan()
for x = -scannerRange, scannerRange do
for z = -scannerRange, scannerRange do
for y = -scannerRange, scannerRange do
local scanned = scanned_blocks[scannerWidth ^ 2 * (x + scannerRange) + scannerWidth * (y + scannerRange) + (z + scannerRange) + 1]
if scanned then
cachedBlocks[(zcachedX + scanned.x)..":"..(zcachedY + scanned.y)..":"..(zcachedZ + scanned.z)] = {}
cachedBlocks[(zcachedX + scanned.x)..":"..(zcachedY + scanned.y)..":"..(zcachedZ + scanned.z)]["name"] = scanned.name
cachedBlocks[(zcachedX + scanned.x)..":"..(zcachedY + scanned.y)..":"..(zcachedZ + scanned.z)]["meta"] = modules.getBlockMeta(scanned.x, scanned.y, scanned.z);
if scanned.name == "minecraft:air" then
cachedWorld[scanned.x..":"..scanned.y..":"..scanned.z] = 0
end
writeLine(1,2, zcachedX + scanned.x .. " " .. scanned.y .. " " .. scanned.z .. " > " .. scanned.name)
end
sleep(0.05)
end
end
end
saveAll()
end
----------------------------------------
-- reconstruct_path
--
-- function: A* path reconstruction
-- input: A* visited nodes and goal
-- return: List of movement to be executed
--
local function reconstruct_path(_cameFrom, _currentNode)
if _cameFrom[_currentNode] ~= nil then
local dir, nextNode = _cameFrom[_currentNode][1], _cameFrom[_currentNode][2]
local path = reconstruct_path(_cameFrom, nextNode)
table.insert(path, dir)
return path
else
return {}
end
end
----------------------------------------
-- a_star
--
-- function: A* path finding
-- input: start and goal coordinates
-- return: List of movement to be executed
--
local function a_star(x1, y1, z1, x2, y2, z2, discover, closest)
discover = discover or 1
local start, idx_start = {x1, y1, z1}, x1..":"..y1..":"..z1
local goal, idx_goal = {x2, y2, z2}, x2..":"..y2..":"..z2
local idx_closest
if (cachedWorld[idx_goal] or 0) == 0 or closest then
local openset, closedset, cameFrom, g_score, tries = {}, {}, {}, {}, 0
openset[idx_start] = start
g_score[idx_start] = 0
local idx_current = idx_start
local i = 2
while not empty(openset) do
sleep(0.05)
idx_current = openset[idx_current]
local current = idx_current
i = i + 1
if i >= 2 then
idx_closest = idx_current
i = 0
end
if idx_current == idx_goal then
return reconstruct_path(cameFrom, idx_goal)
end
local x3, y3, z3 = current[1], current[2], current[3]
openset[idx_current] = nil
closedset[idx_current] = true
for dir = 0, 5 do -- for all direction find the neighbor of the current position
local D = deltas[dir]
local x4, y4, z4 = x3 + D[1], y3 + D[2], z3 + D[3]
local neighbor, idx_neighbor = {x4, y4, z4}, x4..":"..y4..":"..z4
if (cachedWorld[idx_neighbor] or 0) == 0 then -- if its free or unknow
if closedset[idx_neighbor] == nil then
local tentative_g_score = g_score[idx_current] + ((cachedWorld[idx_neighbor] == nil) and discover or 1)
if openset[idx_neighbor] == nil or tentative_g_score <= g_score[idx_neighbor] then
cameFrom[idx_neighbor] = {dir, idx_current}
g_score[idx_neighbor] = tentative_g_score
openset[idx_neighbor] = neighbor
end
end
end
end
end
if closest then
return reconstruct_path(cameFrom, idx_closest)
end
end
return {}
end
----------------------------------------
-- moveTo
--
-- function: Move the turtle to the choosen coordinates in the world
-- input: X, Y, Z and direction of the goal
-- return: boolean "success"
--
function moveTo(x, y, z)
for key, value in pairs(a_star(cachedX, cachedY, cachedZ, x, y, z, true, true)) do
modules.walk(value[1][0],value[1][1],value[1][2])
update()
end
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
local ores =
{
{
["name"] = "gregtech:gt.blockores",
["damage"] = 535
}
}
function loop()
while true do
detectAll()
end
end
function loopf()
while true do
local l = searchWorld("minecrat:bedrock")
if l ~= false then
writeLine(1,8,"k")
local s = split(l, "\:")
moveTo(s[1], s[2], s[3])
else
writeLine(1,8,"failed")
end
sleep(0.1)
end
end
--loadAll()
--saveAll()
--parallel.waitForAny(mine, loop)
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
term.clear()
args = {...}
detectAll()
saveAll()
--parallel.waitForAny(loopf, loop)
writeLine(1,1,"scanning...")
writeLine(1,2,dump(modules.getBlockMeta(-1, -3, -1)))
moveTo(args[1], args[2], args[3])
--loop()
--term.clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment