-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.lua
More file actions
381 lines (316 loc) · 12.9 KB
/
robot.lua
File metadata and controls
381 lines (316 loc) · 12.9 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
----------------ROBOT----------------
local robot = require('robot')
local computer = require('computer')
local event = require('event')
local fs = require("filesystem")
local term = require('term')
local serial = require('serialization')
local com = require('component')
local sides = require('sides')
local port = 87
local rssignal
-- Безопасная подгрузка компонентов
local function safeLoadComponents(name)
if com.isAvailable(name) then
return com.getPrimary(name), true
else
return ('Внимание! Компонент %s не найден!'):format(name), false
end
end
-- ===== Всякие принтилки, писалки на экран разной кучи инфы и всякие проверки =====
-- Проверка компонентов
local modem, state_tun = safeLoadComponents('modem')
local rs, state_redstone = safeLoadComponents('redstone')
local icontroller, state_icontroller = safeLoadComponents('inventory_controller')
local magnet, state_magnet = safeLoadComponents('tractor_beam')
local sign, state_sign = safeLoadComponents('sign')
modem.open(port)
modem.setWakeMessage("start")
term.clear()
print('============= Проверка компонентов ===========')
if not state_tun then
print(modem)
print('ВНИМАНИЕ! Аппаратные возможности Вашего робота')
print('не удовлетворяют миним. требованиям программы!')
print('Установите в робота плату беспроводной сети.')
print('=================================================')
print('Программа завершена.')
os.exit()
end
if not state_icontroller then
print(icontroller)
print('ВНИМАНИЕ! Аппаратные возможности Вашего робота')
print('не удовлетворяют миним. требованиям программы!')
print('Установите в робота контроллер инвентаря!')
print('=================================================')
print('Программа завершена.')
os.exit()
end
if not state_redstone then
print(rs)
print('Аппаратные возможности робота ограничены!')
print('Вы не сможете управлять редстоун сигналами!')
rssignal = 'редстоун НЕ УСТ.'
else
rssignal = 'редстоун выкл.'
rs.setOutput(sides.front, 0)
rs.setOutput(sides.bottom, 0)
rs.setOutput(sides.top, 0)
end
if not state_magnet then
print(magnet)
print('Аппаратные возможности робота ограничены!')
print('Вы не сможете подбирать лут с земли!')
end
if not state_sign then
print(sign)
print('Аппаратные возможности робота ограничены!')
print('Вы не сможете писать на табличках!')
end
-- Константы
local MAXPACKET = modem.maxPacketSize()
local INVSIZE = robot.inventorySize()
local MAX_EU = computer.maxEnergy()
local TOTAL_MEMORY = computer.totalMemory() / 1024
local neon = {0x0000ff, 0x00ff00, 0xffff00, 0x00ffff, 0xff00c0, 0xff0000, 0x000000}
-- Переменные
local DYNAMIC_VIEW = true
local current_neon = 1
local robot_status = 'Ожидаю команд...'
local function sendData(array)
local data = serial.serialize(array)
modem.broadcast(port, data)
end
local function getEU()
return math.floor(computer.energy())
end
local function freeMemory()
return math.floor(computer.freeMemory() / 1024)
end
local function printStatus()
term.clear()
print('==================== Статус =====================')
print(('Имя робота: %s'):format(robot.name()))
print(('Уровень прокачки: %s'):format(robot.level()))
print(('Всего/свободно слотов: %s/нет данных'):format(INVSIZE))
print(('Ёмкость батареи: %s EU'):format(MAX_EU))
print(('Текущий заряд: %s EU'):format(getEU()))
print(('Всего RAM: %s КБ'):format(TOTAL_MEMORY))
print(('Свободно RAM: %s КБ'):format(freeMemory()))
print(('Время работы/RS: %s мин./%s'):format(math.floor(computer.uptime() / 60), rssignal))
print(('================================================='))
print(('Данные Wi-Fi: %s'):format(robot_status))
end
-- Формирование и отправка данных о роботе (параметры, инвентарь и прочее)
local function infoStatSend()
-- local full_info = {
-- 'infoRobot',
-- 'Имя робота/уровень прокачки: ' .. robot.name() .. '/' .. robot.level(),
-- 'Всего/свободно слотов: ' .. INVSIZE .. '/нет данных',
-- 'Ёмкость батареи/текущий заряд: ' .. MAX_EU .. '/' .. getEU() .. ' EU',
-- 'Всего/cвободно RAM: ' .. TOTAL_MEMORY .. '/' .. freeMemory() .. ' KБ',
-- 'Время работы/редстоун: ' .. math.floor(computer.uptime() / 60) .. ' мин./' .. rssignal,
-- ' --- Перечень предметов в слотах (слот, имя, кол-во) ---'
-- }
local full_info = {
'infoRobot',
('Имя робота/уровень прокачки: %s/%s'):format(robot.name(), robot.level()),
('Всего/свободно слотов: %s/нет данных'):format(INVSIZE),
('Ёмкость батареи/текущий заряд: %s/%s EU'):format(MAX_EU, getEU()),
('Всего/cвободно RAM: %s/%s КБ'):format(TOTAL_MEMORY, freeMemory()),
('Время работы/редстоун: %s мин./%s'):format(math.floor(computer.uptime() / 60), rssignal),
' --- Перечень предметов в слотах (слот, имя, кол-во) ---'
}
for i = 1, INVSIZE do
local amount = robot.count(i)
if amount > 0 then
robot.select(i)
stack = icontroller.getStackInInternalSlot(i) -- local?
local data_item = ("%d: %s - %d шт."):format(i, stack.label, amount)
table.insert(full_info, data_item)
end
end
sendData(full_info)
end
local function infoStatSendDynamicView()
if DYNAMIC_VIEW == true then
infoStatSend()
end
end
-- ===== таблица действий робота на сетевые команды ======
local commands = {
-- Движение робота
['w'] = function() robot.forward() end,
['s'] = function() robot.back() end,
['d'] = function() robot.turnRight() end,
['a'] = function() robot.turnLeft() end,
['lshift'] = function() robot.up() end,
['lcontrol'] = function() robot.down() end,
-- Дейстия сверху ЛКМ, ПКМ, Shift-ПКМ
['i'] = function() robot.swingUp() end,
['o'] = function() robot.useUp() end,
['p'] = function() robot.useUp(sides.top, true) end,
-- Дейстия спереди ЛКМ, ПКМ, Shift-ПКМ
['q'] = function() robot.swing() end,
['e'] = function() robot.use() end,
['r'] = function() robot.use(sides.front, true) end,
-- Дейстия снизу ЛКМ, ПКМ, Shift-ПКМ
['j'] = function() robot.swingDown() end,
['k'] = function() robot.useDown() end,
['l'] = function() robot.useDown(sides.bottom, true) end,
--======команды 0-9=================
-- ВЫБРОСИТЬ ВСЁ и выбрать 1-й слот
['1'] = function()
for i = 1, INVSIZE do
if robot.count(i) > 0 then
robot.select(i)
robot.drop()
infoStatSendDynamicView()
end
end
robot.select(1)
end,
-- Собрать весь дроп с земли, пока могу это делать!
['2'] = function()
if not state_magnet then
return
end
robot.select(1)
while magnet.suck() do
infoStatSendDynamicView()
end
end,
-- ВЗЯТЬ из сундука СВЕРХУ
['3'] = function()
robot.select(1)
robot.suckUp()
infoStatSendDynamicView()
end,
-- ВЗЯТЬ из сундука СПЕРЕДИ
['4'] = function()
robot.select(1)
robot.suck()
infoStatSendDynamicView()
end,
-- ВЗЯТЬ из сундука СНИЗУ
['5'] = function()
robot.select(1)
robot.suckDown()
infoStatSendDynamicView()
end,
-- Что вокруг меня?
['6'] = function()
local scan = {'scan'}
if robot.detect() then
table.insert(scan, ' Cпереди - БЛОК')
else
table.insert(scan, ' Cпереди - 0')
end
if robot.detectUp() then
table.insert(scan, ' Cверху - БЛОК')
else
table.insert(scan, ' Cверху - 0')
end
if robot.detectDown() then
table.insert(scan, ' Cнизу - БЛОК')
else
table.insert(scan, ' Cнизу - 0')
end
robot.turnRight()
if robot.detect() then
table.insert(scan, ' Cправа - БЛОК')
else
table.insert(scan, ' Cправа - 0')
end
robot.turnRight()
if robot.detect() then
table.insert(scan, ' Сзади - БЛОК')
else
table.insert(scan, ' Сзади - 0')
end
robot.turnRight()
if robot.detect() then
table.insert(scan, ' Слева - БЛОК')
else
table.insert(scan, ' Слева - 0')
end
robot.turnRight()
sendData(scan)
end,
-- Выбрать слот N
['7'] = function(data)
pcall(robot.select, tonumber(data[2]))
end,
-- Дроп текущего слота
['8'] = function() robot.drop() end,
-- Показать статистику
['9'] = function() infoStatSend() end,
-- Поставить блок Сверху
['z'] = function() robot.placeUp() end,
-- Поставить блок Спереди
['x'] = function() robot.place() end,
-- Поставить блок Снизу
['c'] = function() robot.placeDown() end,
-- Включить/выключить редстоун излучение
['f'] = function()
if not state_redstone then
return
end
if rssignal == 'редстоун выкл.' then
rs.setOutput(sides.front, 15)
rs.setOutput(sides.bottom, 15)
rs.setOutput(sides.top, 15)
rssignal = 'редстоун вкл.'
else
rs.setOutput(sides.front, 0)
rs.setOutput(sides.bottom, 0)
rs.setOutput(sides.top, 0)
rssignal = 'редстоун выкл.'
end
end,
-- Цикличная смена цвета неоновой подсветки робота
['n'] = function()
if current_neon > #neon then
current_neon = 1
end
robot.setLightColor(neon[current_neon])
current_neon = current_neon + 1
end,
-- Вкл/выкл показа динамического изменения инвентаря робота
['v'] = function() DYNAMIC_VIEW = not DYNAMIC_VIEW end,
-- Меняем инструмент на инструмент в активном слоте инвентаря
['y'] = function()
if not state_icontroller then
return
end
icontroller.equip()
end,
-- Пишем текст на табличках
['t'] = function(data)
if not state_sign then
return
end
sign.setValue(tostring(data[2]))
end,
}
local function main()
robot.select(1)
printStatus()
while true do
-- Ожидаем команд (события) по wi-fi, каждые 30 сек - срыв ожидания и принт статуса
e, _, _, _, _, msg = event.pull(30, 'modem_message')
if e then
robot_status = msg
printStatus()
data = serial.unserialize(msg)
-- Если существует ключ, вызываем действие из массива и передаем все данные из сообщения
if commands[data[1]] then
commands[data[1]](data)
end
else
-- Принтим на экран инфу о разряжающейся батарейке
printStatus()
end
end
end
main()