forked from blunty666/CC-Pathfinding-and-Mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
if not rednet.isOpen() then | ||
for _, side in ipairs({"left", "right"}) do | ||
if peripheral.getType(side) == "modem" then | ||
rednet.open(side) | ||
end | ||
end | ||
end | ||
if not rednet.isOpen() then | ||
printError("Could not open rednet") | ||
return | ||
end | ||
function save(table,name) | ||
local file = fs.open(name,"w") | ||
file.write(textutils.serialize(table)) | ||
file.close() | ||
end | ||
function load(name) | ||
local file = fs.open(name,"r") | ||
if not file then return false end | ||
local data = file.readAll() | ||
file.close() | ||
return textutils.unserialize(data) | ||
end | ||
local items = load("data") | ||
if not items then | ||
items = {} | ||
end | ||
while true do | ||
local senderId, s, protocol = rednet.receive() | ||
if s.call == "set" then | ||
items[s.itemname] = s.item | ||
save(items, "data") | ||
elseif s.call == "get" then | ||
if not items[s.item] then | ||
rednet.broadcast({["return"] = "none"}) | ||
else | ||
rednet.broadcast({["return"] = "success", ["location"] = items[s.item], ["item"] = s.item}) | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
if not rednet.isOpen() then | ||
for _, side in ipairs({"left", "right"}) do | ||
if peripheral.getType(side) == "modem" then | ||
rednet.open(side) | ||
end | ||
end | ||
end | ||
if not rednet.isOpen() then | ||
printError("Could not open rednet") | ||
return | ||
end | ||
local dirs = { | ||
["south"] = 0, | ||
["west"] = 1, | ||
["north"] = 2, | ||
["east"] = 3 | ||
} | ||
local call = {} | ||
--[[ | ||
rednet.broadcast({["call"] = "set", ["itemname"] = "minecraft:cobblestone", ["item"] = { | ||
x = 762, | ||
y = 6, | ||
z = 267, | ||
dir = 3 | ||
}}) | ||
]]-- | ||
function serializeTable(val, name, skipnewlines, depth) | ||
skipnewlines = skipnewlines or false | ||
depth = depth or 0 | ||
|
||
local tmp = string.rep(" ", depth) | ||
|
||
if name then tmp = tmp .. name .. " = " end | ||
|
||
if type(val) == "table" then | ||
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "") | ||
|
||
for k, v in pairs(val) do | ||
tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "") | ||
end | ||
|
||
tmp = tmp .. string.rep(" ", depth) .. "}" | ||
elseif type(val) == "number" then | ||
tmp = tmp .. tostring(val) | ||
elseif type(val) == "string" then | ||
tmp = tmp .. string.format("%q", val) | ||
elseif type(val) == "boolean" then | ||
tmp = tmp .. (val and "true" or "false") | ||
else | ||
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\"" | ||
end | ||
|
||
return tmp | ||
end | ||
function get(itemname) | ||
rednet.broadcast({["call"] = "get", ["item"] = itemname}) | ||
local senderId, s, protocol = rednet.receive() | ||
return s | ||
end | ||
local tArgs = {...} | ||
local method = tArgs[1] | ||
if not method then return end | ||
if method == "set" then | ||
if #tArgs < 6 then return end | ||
call["call"] = "set" | ||
call["itemname"] = tArgs[2] | ||
local item = {} | ||
item["x"] = tArgs[3] | ||
item["y"] = tArgs[4] | ||
item["z"] = tArgs[5] | ||
item["dir"] = dirs[tArgs[6]] | ||
call["item"] = item | ||
rednet.broadcast(call) | ||
print(serializeTable(get(tArgs[2]))) | ||
elseif method == "get" then | ||
print(serializeTable(get(tArgs[2]))) | ||
end | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
-- LOAD NETNAV API | ||
if not netNav then | ||
if not os.loadAPI("netNav") then | ||
error("could not load netNav API") | ||
end | ||
end | ||
|
||
-- OPEN REDNET | ||
for _, side in ipairs({"left", "right"}) do | ||
if peripheral.getType(side) == "modem" then | ||
rednet.open(side) | ||
end | ||
end | ||
if not rednet.isOpen() then | ||
printError("Could not open rednet") | ||
return | ||
end | ||
function serializeTable(val, name, skipnewlines, depth) | ||
skipnewlines = skipnewlines or false | ||
depth = depth or 0 | ||
|
||
local tmp = string.rep(" ", depth) | ||
|
||
if name then tmp = tmp .. name .. " = " end | ||
|
||
if type(val) == "table" then | ||
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "") | ||
|
||
for k, v in pairs(val) do | ||
tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "") | ||
end | ||
|
||
tmp = tmp .. string.rep(" ", depth) .. "}" | ||
elseif type(val) == "number" then | ||
tmp = tmp .. tostring(val) | ||
elseif type(val) == "string" then | ||
tmp = tmp .. string.format("%q", val) | ||
elseif type(val) == "boolean" then | ||
tmp = tmp .. (val and "true" or "false") | ||
else | ||
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\"" | ||
end | ||
|
||
return tmp | ||
end | ||
-- SET NETNAV MAP | ||
netNav.setMap("test", 15) -- the second argument determines how frequently the turtle will check with the server for newer map data | ||
function get(itemname) | ||
rednet.broadcast({["call"] = "get", ["item"] = itemname}) | ||
local senderId, s, protocol = rednet.receive() | ||
if not s["return"] then return false elseif s["return"] == "none" then return false end | ||
print(serializeTable(s)) | ||
return s | ||
end | ||
|
||
|
||
while true do | ||
for i = 1, 15 do | ||
turtle.select(i) | ||
sleep(0.5) | ||
if turtle.getItemDetail(i) then | ||
local cont = true | ||
if turtle.getItemDetail(i).name == "minecraft:coal" then | ||
if not turtle.getItemDetail(16) then | ||
turtle.transferTo(16) | ||
cont = false | ||
end | ||
end | ||
if cont then | ||
local ret = get(turtle.getItemDetail(i).name) | ||
if ret then | ||
if turtle.getFuelLevel() < 1000 then | ||
turtle.select(16) | ||
turtle.refuel() | ||
turtle.select(i) | ||
end | ||
|
||
netNav.goto(ret.location.x, ret.location.y, ret.location.z) | ||
netNav.setHeading(ret.location.dir) | ||
turtle.drop() | ||
end | ||
end | ||
end | ||
end | ||
netNav.goto(775, 6, 282) | ||
netNav.setHeading(2) | ||
for i = 1, 15 do | ||
turtle.select(i) | ||
sleep(0.5) | ||
turtle.dropUp() | ||
end | ||
|
||
local found = false | ||
while not found do | ||
for i = 1, 15 do | ||
turtle.select(i) | ||
local a = turtle.suckUp() | ||
sleep(0.5) | ||
if not found then | ||
if a then | ||
found = true | ||
end | ||
end | ||
end | ||
if not found then sleep(5) end | ||
end | ||
end |