-
Notifications
You must be signed in to change notification settings - Fork 2
/
startup.lua
142 lines (131 loc) · 3.41 KB
/
startup.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
if not turtle then return end
-- install
if not fs.exists("act-install") then
shell.run("pastebin", "get", "5CuUMxqr", "act-install")
end
shell.run("act-install")
os.loadAPI("apis/act")
-- get act script
local plan
local actFile
local formanName
for i, filename in ipairs(fs.list("disk")) do
if filename:find("%.act$") then
print("initializing "..filename)
local f = fs.open(fs.combine("disk",filename), "r")
plan = f.readAll()
f.close()
actFile = filename
formanName = filename:sub(1, #filename - 4)
break
end
end
local ast = act.parse(plan)
-- get next offline worker
local workers = act.getWorkers(ast)
local modem
for _, side in ipairs(rs.getSides()) do
if peripheral.getType(side) == "modem" then
modem = peripheral.wrap(side)
end
end
if not modem then
print("Not a wireless turtle")
return
end
if not modem.isOpen(220) then
modem.open(220)
end
local nextWorker
for worker, _ in pairs(workers) do
modem.transmit(284, 220, worker.."@"..worker..":If")
local timer = os.startTimer(1)
while true do
local event, modemSide, senderChannel, replyChannel, returnMessage, senderDistance = os.pullEvent()
if event == "modem_message" then
print(" worker '"..worker.."' online")
break
elseif event == "timer" and modemSide == timer then
print(" worker '"..worker.."' offline")
nextWorker = worker
break
end
end
if nextWorker then
break
end
end
-- fuel and orient turtle
while true do
local pType = peripheral.getType("front")
if pType and pType:find("chest") then
if turtle.getFuelLevel() == 0 then
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
turtle.select(i)
turtle.suck()
turtle.refuel()
print("getting fuel: "..turtle.getFuelLevel())
break
end
end
end
break
end
turtle.turnRight()
end
turtle.turnLeft()
turtle.facing = 0
if turtle.gps(true) then
print("gps synced")
end
-- send to startup position
function sendToStartup(theWorker)
function getStartup(ast)
if ast.actions then
for i, v in ipairs(ast.actions) do
local startup = getStartup(v)
if startup then
return startup
end
end
elseif ast.action and type(ast.action) == "table" then
return getStartup(ast.action)
elseif ast.extension and ast.extension == "startup" then
return ast.params
end
end
local params = getStartup(ast)
tFacing = {south=0, west=1, north=2, east=3}
for i = 1, #params, 5 do
local worker, x, y, z, facing = params[i], params[i+1], params[i+2], params[i+3], params[i+4]
facing = tFacing[facing] or facing
if worker and worker == theWorker and x and y and z and facing then
local plan = "G<"..x..","..y..","..z..","..facing..">"
print(plan)
act.act(plan)
break
end
end
end
if nextWorker then
os.setComputerLabel(nextWorker)
print("label set to "..nextWorker)
-- create startup
f = fs.open("startup", "w")
f.write("shell.run('worker','"..nextWorker.."')")
f.close()
print("startup file created")
sendToStartup(nextWorker)
-- start worker program
shell.run("worker", nextWorker)
else
-- setup forman
os.setComputerLabel("forman"..formanName)
fs.copy(fs.combine("disk",actFile), actFile)
f = fs.open("start", "w")
f.write("shell.run('forman', '"..actFile.."')")
f.close()
sendToStartup("forman")
print("forman ready, run 'start'")
end