-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.lua
More file actions
434 lines (421 loc) · 13.5 KB
/
Copy pathbot.lua
File metadata and controls
434 lines (421 loc) · 13.5 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
--管理机器人的物品栏与位置
---@diagnostic disable: inject-field, undefined-field, assign-type-mismatch, duplicate-set-field, need-check-nil
local component = require("component")
local computer = require("computer")
local robot = require("robot")
local event = require("event")
local analyzeGenes = require("analyzeGenes")
local inventory_controller = component.inventory_controller
local upgrade_me = component.upgrade_me
local database = component.database
local M = {}
local inventorySize = robot.inventorySize()
if inventorySize < 32 then
error("物品栏升级过少")
end
--监听inventory_changed事件,维护物品栏映射表
M.inventory, M.inventoryLabel = {}, nil
function M.updateInventory(_, slot)
local stack = inventory_controller.getStackInInternalSlot(slot)
if stack then
local success, result = pcall(analyzeGenes, stack)
if success then
M.inventory[slot] = result
else
M.inventory[slot] = stack
M.inventory[slot].type = "others"
end
M.inventory[slot].inventoryLabel = M.inventoryLabel
else
M.inventory[slot] = nil
end
os.sleep(0)
return true
end
event.listen("inventory_changed", M.updateInventory)
--robot库物品栏操作函数重定向
local t, e = robot.transferTo, inventory_controller.equip
function M.equip()
local previousLabel = M.inventoryLabel
M.inventoryLabel = M.inventory[0] and M.inventory[0].inventoryLabel
M.inventory[0] = M.inventory[robot.select()]
e()
os.sleep(0)
M.inventoryLabel = previousLabel
end
function M.transfer(previousSlot, targetSlot)
if not M.inventory[previousSlot]then
return false
end
local previousSlotLabel, targetSlotLabel = M.inventory[previousSlot] and M.inventory[previousSlot].inventoryLabel, M.inventory[targetSlot] and M.inventory[targetSlot].inventoryLabel
robot.select(previousSlot)
local result = t(targetSlot)
os.sleep(0.1)
if M.inventory[targetSlot] then
M.inventory[targetSlot].inventoryLabel = previousSlotLabel
end
if M.inventory[previousSlot] then
M.inventory[previousSlot].inventoryLabel = targetSlotLabel
end
return result
end
robot.transferTo, inventory_controller.equip = M.transfer, M.equip
local inventoryChangerList = {
{robot, "drop"}, {robot, "dropDown"}, {robot, "dropUp"}, {robot, "suck"}, {robot, "suckDown"}, {robot, "suckUp"},
{inventory_controller, "dropIntoSlot"}, {inventory_controller, "suckFromSlot"}, {upgrade_me, "requestItems"}
}
for _, pack in pairs(inventoryChangerList) do--对于可能改变物品栏的函数,在结束后执行一个sleep(0.01)等待异步操作更新物品栏映射表
local lib, funcName = pack[1], pack[2]
local previousFunc = lib[funcName]
lib[funcName] = function(...)
local result = previousFunc(...)
os.sleep(0.01)
return result
end
end
--robot库移动函数重定向
local position,direction = {x=0,y=1,z=0},{x=0,z=-1}
local f, b, u, d, l, r = robot.forward, robot.back, robot.up, robot.down, robot.turnLeft, robot.turnRight
function M.up()
while not u() do
os.sleep(0)
end
position.y = position.y + 1
end
function M.down()
while not d() do
os.sleep(0)
end
position.y = position.y - 1
end
function M.forward()
while not f() do
os.sleep(0)
end
position.x=position.x+direction.x
position.z=position.z+direction.z
end
function M.back()
while not b() do
os.sleep(0)
end
position.x = position.x - direction.x
position.z = position.z - direction.z
end
robot.forward, robot.back, robot.up, robot.down = M.forward, M.back, M.up, M.down
function M.turnLeft()
l()
direction.x, direction.z = -direction.z, direction.x
end
function M.turnRight()
r()
direction.x, direction.z = direction.z, -direction.x
end
robot.turnLeft, robot.turnRight = M.turnLeft, M.turnRight
function M.turnTo(targetX, targetZ)
if math.abs(targetX) + math.abs(targetZ) ~= 1 or targetX * targetZ ~= 0 then
error("错误的调用bot.turnTo()")
end
if direction.x ~= targetX or direction.z ~= targetZ then
if (direction.x ~= 0 and direction.x+targetX == 0) or (direction.z ~= 0 and direction.z + targetZ == 0) then
M.turnRight() M.turnRight()
elseif direction.x == targetZ and direction.z + targetX == 0 then
M.turnLeft()
elseif direction.z == targetX and direction.x + targetZ == 0 then
M.turnRight()
end
end
end
function M.moveXZTo(targetX, targetZ)
local movedX = false
local function moveX()
if position.x > targetX then
M.turnTo(-1,0)
for i=1,position.x-targetX do
M.forward()
end
elseif position.x < targetX then
M.turnTo(1,0)
for i=1,targetX-position.x do
M.forward()
end
end
movedX = true
end
if direction.x * (targetX - position.x) > 0 or direction.z * (targetZ - position.z) < 0 then
moveX()
end
if position.z > targetZ then
M.turnTo(0,-1)
for i=1,position.z- targetZ do
M.forward()
end
elseif position.z < targetZ then
M.turnTo(0,1)
for i=1, targetZ -position.z do
M.forward()
end
end
if not movedX then
moveX()
end
end
function M.moveYTo(targetY)
if targetY > position.y then
for i=1,targetY-position.y do
M.up()
end
elseif targetY < position.y then
for i=1,position.y-targetY do
M.down()
end
end
end
--充电
function M.charge()
local previousPosition = {x=position.x, y=position.y, z=position.z}
local previousDirection = {x=direction.x, z=direction.z}
if computer.energy() < 5000 then
M.moveYTo(2)
M.moveXZTo(0,0)
M.moveYTo(1)
local targetEnergy = computer.maxEnergy() - 100
while computer.energy() < targetEnergy do
os.sleep(0.5)
end
M.moveYTo(2)
M.moveXZTo(previousPosition.x, previousPosition.z)
M.moveYTo(previousPosition.y)
M.turnTo(previousDirection.x, previousDirection.z)
end
return true
end
event.timer(120, M.charge, math.huge)
--物品栏管理函数
function M.getEmptySlotCount()
local count = 0
for i=1,inventorySize do
if not M.inventory[i] then
count = count + 1
end
end
return count
end
function M.selectEmptySlot()
for i=1,inventorySize do
if not M.inventory[i] and robot.count(i) == 0 then
return robot.select(i)
end
end
return nil
end
function M.selectUsedSlot()
for i=1,inventorySize do
if M.inventory[i] and robot.count(i) > 0 then
return robot.select(i)
end
end
return nil
end
function M.getItemsWithLabel(inventoryLabel)
local result = {}
for i=1,inventorySize do
if M.inventory[i] and M.inventory[i].inventoryLabel == inventoryLabel then
table.insert(result, i)
end
end
return result
end
function M.clearItem(inventoryLabel)
for i=1,inventorySize do
if M.inventory[i] and M.inventory[i].inventoryLabel == inventoryLabel then
if M.inventory[i].type == "beePrincess" or M.inventory[i].type == "beeDrone" then
robot.select(i)
robot.dropUp()
else
robot.select(i)
upgrade_me.sendItems()
end
end
end
end
function M.checkItem(filter, request)
if not filter or not filter.name then
error("错误的调用bot.checkItem()")
end
--[神秘小补丁
if filter.tag then
if filter.name == "Forestry:beeDroneGE" or filter.name == "Forestry:beePrincessGE" then
local speciesGenes = analyzeGenes({name=filter.name,tag=filter.tag,individual={}}).species
database.set(1, filter.name, 0, '{IsAnalyzed:1b,Genome:{Chromosomes:[0:{Slot:0b,UID0:"'..speciesGenes[1]..'",UID1:"'..speciesGenes[2]..'"}]}}')
local label = database.get(1).label
return M.checkItemByTag({tag=filter.tag,label=label}, request)
else
error("错误的调用bot.checkItem():不支持tag查询的物品")
end
end
--]]
local function isEqual(stack, _filter)
if (_filter.name and stack.name ~= _filter.name) or (_filter.damage and stack.damage ~= _filter.damage) or (_filter.tag and stack.tag ~= _filter.tag) then
return false
end
return true
end
if request then
request = type(request) == "number" and request or 1
else
request = 0
end
local slotList, count = {}, 0
for slot,item in pairs(M.inventory) do
if slot ~= 0 and isEqual(item, filter) then
table.insert(slotList, slot)
count = count + robot.count(slot)
end
end
local targetSlot
for _, slot in pairs(slotList) do
if targetSlot then
if not isEqual(M.inventory[targetSlot], M.inventory[slot]) then
error("错误的调用bot.checkItem()")
end
M.transfer(slot, targetSlot)
else
targetSlot = slot
request = math.min(request, M.inventory[slot].maxSize or 64)
end
end
if targetSlot and robot.count(targetSlot) >= request then
return targetSlot
end
local stackList = upgrade_me.getItemsInNetwork(filter)
if not stackList then
error("超出ME网络范围")
elseif not stackList[1] then
return nil
elseif request == 0 then
return true
end
if targetSlot then
robot.select(targetSlot)
else
targetSlot = M.selectEmptySlot()
if not targetSlot then
error("物品栏已满")
end
request = math.min(request, stackList[1].maxSize)
end
database.clear(1)
upgrade_me.store(filter, database.address, 1)
local stack = database.get(1)
if M.inventory[targetSlot] and not isEqual(M.inventory[targetSlot], stack) then
error("错误的调用bot.checkItem()")
end
if stack then
upgrade_me.requestItems(database.address, 1, request - count)
if M.inventory[targetSlot] then
return targetSlot
end
end
return nil
end
function M.checkItemByTag(filter, request)
if not filter or not filter.tag or not filter.label then
error("错误的调用bot.checkItemByTag()")
end
if request then
request = type(request) == "number" and request or 1
else
request = 0
end
local slotList, count = {}, 0
for slot, item in pairs(M.inventory) do
if slot ~= 0 and item.label == filter.label and item.tag == filter.tag then
table.insert(slotList, slot)
count = count + robot.count(slot)
end
end
local targetSlot
for _, slot in pairs(slotList) do
if targetSlot then
if M.inventory[targetSlot].tag ~= M.inventory[slot].tag then
error("错误的调用bot.checkItemByTag()")
end
M.transfer(slot, targetSlot)
else
targetSlot = slot
request = math.min(request, M.inventory[slot].maxSize or 64)
end
end
if targetSlot and robot.count(targetSlot) >= request then
return targetSlot
end
local i = 1
while database.clear(i) do
i = i + 1
end
local stackList = upgrade_me.getItemsInNetwork({label=filter.label})
local dbIdx
for i = 1, math.min(#stackList, 81) do
if stackList[i].tag == filter.tag then
dbIdx = i
break
end
end
if not dbIdx then
return nil
elseif request == 0 then
return true
end
if targetSlot then
robot.select(targetSlot)
else
targetSlot = M.selectEmptySlot()
if not targetSlot then
error("物品栏已满")
end
request = math.min(request, stackList[dbIdx].maxSize)
end
upgrade_me.store({label=filter.label}, database.address, 1)
local stack = database.get(dbIdx)
if stack and stack.tag == filter.tag then
upgrade_me.requestItems(database.address, dbIdx, request - count)
if M.inventory[targetSlot] then
return targetSlot
end
end
return nil
end
local crafterArea = {[1]=true,[2]=true,[3]=true,[5]=true,[6]=true,[7]=true,[9]=true,[10]=true,[11]=true}
function M.clearCrafterArea()
if M.getEmptySlotCount() < 9 then
return false
end
for slot,_ in pairs(crafterArea) do
if M.inventory[slot] then
for i=1,inventorySize do
if not crafterArea[i] and not M.inventory[i] then
M.transfer(slot, i)
os.sleep(0)
end
end
end
end
return true
end
--初始化物品栏映射表
for i=1,inventorySize do
M.updateInventory(nil, i)
end
M.equip()
M.equip()
return M
--[[
inventoryName
简易蜂房/蜂箱"tile.for.apiculture"
诱变机"tile.gendustry.mutatron"
基因压印机"tile.gendustry.imprinter"
基因采样机"tile.gendustry.sampler"
基因转换机"tile.gendustry.transposer"
基因复制机"tile.gendustry.replicator"
]]