ХМ-м-м

This commit is contained in:
Igor Timofeev 2016-04-06 09:55:21 +03:00
parent 786e0ae8e8
commit f158b5724b
6 changed files with 535 additions and 101 deletions

Binary file not shown.

View File

@ -1,5 +1,4 @@
local component = require("component")
local buffer = require("doubleBuffering")
local unicode = require("unicode")
@ -7,6 +6,7 @@ local serialization = require("serialization")
local context = require("context")
local event = require("event")
local keyboard = require("keyboard")
local ecs = require("ECSAPI")
local modem = component.modem
--------------------------------------------------------------------------------------------------------------------------------------
@ -16,13 +16,27 @@ local xStart = math.floor(buffer.screen.width / 2)
local yStart = math.floor(buffer.screen.height / 2 + 2)
local port = 1337
local topBarElements = { "Карта", "Инвентарь", "Редстоун", "Геоанализатор", "Бак" }
local currentTopBarElement = 2
local topBarElements = { "Карта", "Скрипт", "Инвентарь", "Редстоун", "Геоанализатор", "Бак" }
local currentTopBarElement = 1
local drawInventoryFrom = 1
local currentInventoryType = "internal"
local map = {
robotPosition = {
robot = {
x = 0, y = 0, z = 0,
rotation = 1,
status = "Ожидание",
energy = 1,
maxEnergy = 1,
redstone = {
[0] = 0,
[1] = 0,
[2] = 0,
[3] = 0,
[4] = 0,
[5] = 0,
}
},
currentLayer = 0,
{ type = "empty", x = 0, y = 0, z = 0 },
@ -31,6 +45,10 @@ local map = {
local robotPicture = {
"", "", "", ""
}
local robotFront = image.load("Robot.pic")
local robotSide = image.load("RobotSide.pic")
local robotTop = image.load("RobotTop.pic")
local chest = image.load("Chest.pic")
local colors = {
white = 0xFFFFFF,
@ -49,11 +67,53 @@ modem.open(port)
--------------------------------------------------------------------------------------------------------------------------------------
--OBJECTS, CYKA
local obj = {}
local function newObj(class, name, ...)
obj[class] = obj[class] or {}
obj[class][name] = {...}
end
local function isNumberInRange(n, min, max)
if n > min and n <= max then return true end
end
local function sendMessage(...)
modem.broadcast(port, "ECSRobotControl", ...)
end
local function drawMultiColorProgressBar(x, y, width, currentValue, maxValue, fat)
local percent = currentValue / maxValue
local valueWidth = math.ceil(percent * width)
local color = 0x33CC33
if isNumberInRange(percent, 0.5, 0.7) then
color = 0xFFFF33
elseif isNumberInRange(percent, 0.3, 0.5) then
color = 0xFFCC33
elseif isNumberInRange(percent, 0.1, 0.3) then
color = 0xFF3333
elseif isNumberInRange(percent, -10, 0.1) then
color = 0x663300
end
buffer.text(x, y, 0x000000, string.rep(fat and "" or "", width))
buffer.text(x, y, color, string.rep(fat and "" or "", valueWidth))
end
local function drawRobotStatus()
local width, height = 10, 7
local x, y = buffer.screen.width - width, buffer.screen.height - height
buffer.square(x, y, width, height, colors.lightGray)
buffer.text(x, y, colors.gray, map.robot.status or "N/A")
y = y + 2
buffer.image(x, y, robotFront)
y = y + robotFront.height
drawMultiColorProgressBar(x - 1, y, 10, map.robot.energy, map.robot.maxEnergy, true)
end
local function drawTopBar()
obj.TopBar = {}
buffer.square(1, 1, buffer.screen.width, 3, colors.gray)
local x = 1
for i = 1, #topBarElements do
@ -64,12 +124,23 @@ local function drawTopBar()
else
buffer.text(x + 2, 2, colors.lightGray, topBarElements[i])
end
newObj("TopBar", i, x, 1, x + textLength - 1, 3)
x = x + textLength
end
end
local function drawMap()
--Рисуем карту
local function drawMap()
buffer.setDrawLimit(1, 4, buffer.screen.width, buffer.screen.height - 3)
--Рисуем карту под роботом
for i = 1, #map do
if map[i].y < map.currentLayer then
buffer.set(xStart + map[i].x, yStart - map[i].z, colors.lightGray, colors.gray, "")
end
end
--Рисуем текущий слой
for i = 1, #map do
--Если слой совпадает с текущим Y
if map[i].y == map.currentLayer then
@ -81,7 +152,7 @@ local function drawMap()
elseif map[i].type == "passable" then
buffer.set(xStart + map[i].x, yStart - map[i].z, colors.lightGray, colors.passable, "")
elseif map[i].type == "entity" then
buffer.text(xStart + map[i].x, yStart - map[i].z, colors.entity, "")
buffer.set(xStart + map[i].x, yStart - map[i].z, colors.lightGray, colors.gray, "")
end
end
end
@ -97,41 +168,207 @@ local function drawMap()
end
--Рисуем робота
buffer.text(xStart + map.robotPosition.x, yStart - map.robotPosition.z, colors.robot, robotPicture[map.robotPosition.rotation])
buffer.text(xStart + map.robot.x, yStart - map.robot.z, colors.robot, robotPicture[map.robot.rotation])
buffer.resetDrawLimit()
drawRobotStatus()
end
local function requestInventoryInfo()
sendMessage("giveMeInfoAboutInventory")
local function requestInfoAboutInventory()
sendMessage("giveMeInfoAboutInventory", currentInventoryType)
end
local function requestInfoAboutRobot()
sendMessage("giveMeInfoAboutRobot")
end
local function highlight(x, y, width, height, color)
buffer.square(x, y + 1, width, height - 2, color)
buffer.text(x, y, color, string.rep("", width))
buffer.text(x, y + height - 1, color, string.rep("", width))
end
local function drawCloud(x, y, width, height, text)
local cloudColor = 0xFFFFFF
local textColor = colors.gray
buffer.square(x, y + 1, width, height - 2, cloudColor, textColor, " ")
buffer.square(x + 1, y, width - 2, height, cloudColor, textColor, " ")
buffer.text(x, y, cloudColor, "")
buffer.text(x + width - 1, y, cloudColor, "")
buffer.text(x, y + height - 1, cloudColor, "")
buffer.text(x + width - 1, y + height - 1, cloudColor, "")
local lines = {
"",
"██▄",
"████▄",
"████▀",
"██▀",
"",
}
local xLine, yLine = x + width, math.floor(y + height / 2 - #lines / 2)
for i = 1, #lines do buffer.text(xLine, yLine, cloudColor, lines[i]); yLine = yLine + 1 end
y = math.floor(y + height / 2)
x = math.floor(x + width / 2 - unicode.len(text) / 2)
buffer.text(x, y, textColor, text)
end
local function drawInventory()
-- sendMessage("giveMeInfoAboutInventory")
local x, y = 3, 5
local xPos, yPos = x, y
if map.robotInventory and #map.robotInventory > 0 then
for i = 1, #map.robotInventory do
--Квадратик
buffer.square(xPos, yPos, 8, 4, colors.gray)
--Имя шмотки
local name = unicode.sub(map.robotInventory[i].label, 1, 16)
local firstPart = unicode.sub(name, 1, 8)
local secondPart = unicode.sub(name, 9, 16) or ""
buffer.text(xPos, yPos, colors.lightGray, firstPart)
buffer.text(xPos, yPos + 1, colors.lightGray, secondPart)
--Колво шмотки
buffer.text(xPos, yPos + 2, 0xFFFFFF, tostring(map.robotInventory[i].size))
--Процент износа
if map.robotInventory[i].maxDamage ~= 0 then
local percentOfNotDamage = 1 - (map.robotInventory[i].damage / map.robotInventory[i].maxDamage)
local widthOfNotDamage = math.floor(percentOfNotDamage * 6)
buffer.text(xPos + 1, yPos + 3, 0x000000, "━━━━━━")
buffer.text(xPos + 1, yPos + 3, 0x33CC33, string.rep("", widthOfNotDamage))
end
xPos = xPos + 10
if i % 4 == 0 then xPos = x; yPos = yPos + 5 end
obj.InventorySlots = {}
local x, y = 3, 5
local inventoryWidth, inventoryHeight = 42, 19
local xPos, yPos = x, y
local counter = 1
if map.robotInventory then
if not map.robotInventory.noInventory then
-- highlight(x - 1, y - 1, inventoryWidth + 2, inventoryHeight + 2, 0xFFFFFF)
--Рисуем скроллбар
buffer.scrollBar(x + inventoryWidth - 2, y, 2, inventoryHeight, map.robotInventory.inventorySize, drawInventoryFrom, colors.gray, 0xFF3333)
--Рисуем слотики
for i = drawInventoryFrom, map.robotInventory.inventorySize do
--Выделение слота
if i == map.robotInventory.currentSlot then
highlight(xPos - 1, yPos - 1, 10, 6, 0xFF3333)
end
--Квадратик
buffer.square(xPos, yPos, 8, 4, colors.gray, colors.lightGray, " ")
--Записываем обжект, чо уж там
newObj("InventorySlots", i, xPos, yPos, xPos + 7, yPos + 3)
--Если такой слот ваще есть, то рисуем инфу о нем, а иначе рисуем пустой слот
if map.robotInventory[i] then
--Имя шмотки
local name = unicode.sub(map.robotInventory[i].label, 1, 16)
local firstPart = unicode.sub(name, 1, 8)
local secondPart = unicode.sub(name, 9, 16) or ""
buffer.text(xPos, yPos, colors.lightGray, firstPart)
buffer.text(xPos, yPos + 1, colors.lightGray, secondPart)
--Колво шмотки
local stringSize = tostring(map.robotInventory[i].size)
buffer.text(xPos + math.floor(4 - unicode.len(stringSize) / 2), yPos + 2, 0xFFFFFF, stringSize)
--Процент износа
if map.robotInventory[i].maxDamage ~= 0 then
drawMultiColorProgressBar(xPos + 1, yPos + 3, 6, map.robotInventory[i].damage, map.robotInventory[i].maxDamage)
end
else
buffer.text(xPos + 1, yPos + 1, colors.lightGray, "Пусто")
end
xPos = xPos + 10
counter = counter + 1
if i % 4 == 0 then xPos = x; yPos = yPos + 5 end
if counter > 16 then break end
end
else
drawCloud(x, y, inventoryWidth, inventoryHeight + 1, "Где инвентарь, сука? Кто ответственный?")
end
else
drawCloud(x, y, inventoryWidth, inventoryHeight + 1, "Запрашиваю у робота массив инвентаря")
end
--Рисуем выбор типа инвентаря
local width = 17
local height = 13
x = buffer.screen.width - width - 7
y = math.floor(4 + (buffer.screen.height - 4) / 2 - height / 2 )
--Коорды все нужные
local inventoryPositions = {
top = { x = x + 10, y = y },
front = { x = x, y = y + 5 },
bottom = { x = x + 10, y = y + 10 },
internal = { x = x + 10, y = y + 5}
}
--Подсветочка
highlight(inventoryPositions[currentInventoryType].x - 1, inventoryPositions[currentInventoryType].y - 1, 10, 6, 0xFF3333)
--Верхний сундук
buffer.image(inventoryPositions.top.x, inventoryPositions.top.y, chest)
--Средний сундук и роботСайд
buffer.image(inventoryPositions.front.x, inventoryPositions.front.y, chest)
buffer.image(inventoryPositions.internal.x, inventoryPositions.internal.y, robotSide)
--Нижний
buffer.image(inventoryPositions.bottom.x, inventoryPositions.bottom.y, chest)
--Обжекты
obj.InventoryTypeSelectors = {}
for key in pairs(inventoryPositions) do newObj("InventoryTypeSelectors", key, inventoryPositions[key].x, inventoryPositions[key].y, inventoryPositions[key].x + 9, inventoryPositions[key].y + 5) end
end
local function drawScript()
end
local function getSizeOfRedstoneWire(size, side)
local percent = (map.robot.redstone[side]) / 15
return math.floor(size * percent)
end
local function sendRedstoneRequest()
sendMessage("giveMeInfoAboutRedstone")
end
local function drawRedstone()
local x, y = 6, 5
local width, side
obj.Redstone = {}
--Левая гориз черта
side = 5
width = getSizeOfRedstoneWire(16, side)
newObj("Redstone", side, x, y + 9, x + 15, y + 10)
buffer.text(x, y + 9, 0x000000, string.rep("", 16))
buffer.text(x, y + 10, 0x000000, string.rep("", 16))
buffer.text(x + 16 - width, y + 9, 0xFF3333, string.rep("", width))
buffer.text(x + 16 - width, y + 10, 0xFF3333, string.rep("", width))
--Правая гориз черта
side = 4
newObj("Redstone", side, x + 24, y + 9, x + 39, y + 10)
width = getSizeOfRedstoneWire(16, side)
buffer.text(x + 24, y + 9, 0x000000, string.rep("", 16))
buffer.text(x + 24, y + 10, 0x000000, string.rep("", 16))
buffer.text(x + 24, y + 9, 0xFF3333, string.rep("", width))
buffer.text(x + 24, y + 10, 0xFF3333, string.rep("", width))
--Верхняя верт черта
side = 3
newObj("Redstone", side, x + 19, y, x + 20, y + 7)
width = getSizeOfRedstoneWire(8, side)
buffer.square(x + 19, y, 2, 8, 0x000000)
buffer.square(x + 19, y + 8 - width, 2, width, 0xFF3333)
--Нижняя верт черта
side = 2
newObj("Redstone", side, x + 19, y + 12, x + 20, y + 19)
buffer.square(x + 19, y + 12, 2, 8, 0x000000)
buffer.square(x + 19, y + 12, 2, getSizeOfRedstoneWire(8, side), 0xFF3333)
buffer.image(x + 16, y + 8, robotTop)
x = x + 41
--Верхняя верт черта
side = 1
newObj("Redstone", side, x + 19, y, x + 20, y + 7)
width = getSizeOfRedstoneWire(8, side)
buffer.square(x + 19, y, 2, 8, 0x000000)
buffer.square(x + 19, y + 8 - width, 2, width, 0xFF3333)
--Нижняя верт черта
side = 0
newObj("Redstone", side, x + 19, y + 12, x + 20, y + 19)
buffer.square(x + 19, y + 12, 2, 8, 0x000000)
buffer.square(x + 19, y + 12, 2, getSizeOfRedstoneWire(8, side), 0xFF3333)
buffer.image(x + 16, y + 8, robotFront)
end
local function drawMain()
@ -142,6 +379,8 @@ local function drawMain()
drawMap()
elseif topBarElements[currentTopBarElement] == "Инвентарь" then
drawInventory()
elseif topBarElements[currentTopBarElement] == "Редстоун" then
drawRedstone()
end
end
@ -153,29 +392,29 @@ end
local function getTargetCoords(direction)
if direction == "forward" then
if map.robotPosition.rotation == 1 then
return map.robotPosition.x, map.robotPosition.y, map.robotPosition.z + 1, xStart, yStart + 1
elseif map.robotPosition.rotation == 2 then
return map.robotPosition.x + 1, map.robotPosition.y, map.robotPosition.z, xStart - 1, yStart
elseif map.robotPosition.rotation == 3 then
return map.robotPosition.x, map.robotPosition.y, map.robotPosition.z - 1, xStart, yStart - 1
elseif map.robotPosition.rotation == 4 then
return map.robotPosition.x - 1, map.robotPosition.y, map.robotPosition.z, xStart + 1, yStart
if map.robot.rotation == 1 then
return map.robot.x, map.robot.y, map.robot.z + 1, xStart, yStart + 1
elseif map.robot.rotation == 2 then
return map.robot.x + 1, map.robot.y, map.robot.z, xStart - 1, yStart
elseif map.robot.rotation == 3 then
return map.robot.x, map.robot.y, map.robot.z - 1, xStart, yStart - 1
elseif map.robot.rotation == 4 then
return map.robot.x - 1, map.robot.y, map.robot.z, xStart + 1, yStart
end
elseif direction == "back" then
if map.robotPosition.rotation == 1 then
return map.robotPosition.x, map.robotPosition.y, map.robotPosition.z - 1, xStart, yStart - 1
elseif map.robotPosition.rotation == 2 then
return map.robotPosition.x - 1, map.robotPosition.y, map.robotPosition.z, xStart + 1, yStart
elseif map.robotPosition.rotation == 3 then
return map.robotPosition.x, map.robotPosition.y, map.robotPosition.z + 1, xStart, yStart + 1
elseif map.robotPosition.rotation == 4 then
return map.robotPosition.x + 1, map.robotPosition.y, map.robotPosition.z, xStart - 1, yStart
if map.robot.rotation == 1 then
return map.robot.x, map.robot.y, map.robot.z - 1, xStart, yStart - 1
elseif map.robot.rotation == 2 then
return map.robot.x - 1, map.robot.y, map.robot.z, xStart + 1, yStart
elseif map.robot.rotation == 3 then
return map.robot.x, map.robot.y, map.robot.z + 1, xStart, yStart + 1
elseif map.robot.rotation == 4 then
return map.robot.x + 1, map.robot.y, map.robot.z, xStart - 1, yStart
end
elseif direction == "up" then
return map.robotPosition.x, map.robotPosition.y + 1, map.robotPosition.z, xStart, yStart
return map.robot.x, map.robot.y + 1, map.robot.z, xStart, yStart
elseif direction == "down" then
return map.robotPosition.x, map.robotPosition.y - 1, map.robotPosition.z, xStart, yStart
return map.robot.x, map.robot.y - 1, map.robot.z, xStart, yStart
end
end
@ -192,46 +431,163 @@ local function getOptimalKeyPoint()
return optimalID
end
local function clicked(x, y, object)
if x >= object[1] and y >= object[2] and x <= object[3] and y <= object[4] then
return true
end
end
--------------------------------------------------------------------------------------------------------------------------------------
drawAll()
requestInfoAboutRobot()
while true do
local e = { event.pull() }
if e[1] == "touch" then
map.keyPoints = map.keyPoints or {}
table.insert(map.keyPoints, { x = e[3] - xStart, y = map.currentLayer, z = e[4] - yStart })
drawAll()
--СОздание ключевых точек
if e[4] >= 4 and topBarElements[currentTopBarElement] == "Карта" then
map.keyPoints = map.keyPoints or {}
table.insert(map.keyPoints, { x = e[3] - xStart, y = map.currentLayer, z = e[4] - yStart })
drawAll()
end
--Выбор верхнего тулбара
for key in pairs(obj.TopBar) do
if clicked(e[3], e[4], obj.TopBar[key]) then
currentTopBarElement = key
requestInfoAboutRobot()
if topBarElements[currentTopBarElement] == "Инвентарь" then
map.robotInventory = nil
currentInventoryType = "internal";
drawInventoryFrom = 1
requestInfoAboutInventory()
elseif topBarElements[currentTopBarElement] == "Редстоун" then
sendRedstoneRequest()
end
drawAll()
break
end
end
--Редстоун
if topBarElements[currentTopBarElement] == "Редстоун" then
if obj.Redstone then
for key in pairs(obj.Redstone) do
if clicked(e[3], e[4], obj.Redstone[key]) then
local newValue
--Если низ или жопка
if key == 0 or key == 2 then
newValue = (e[4] - obj.Redstone[key][2] + 1) * 2
if e[4] == obj.Redstone[key][2] then newValue = 0 end
--Ебало или верх
elseif key == 1 or key == 3 then
newValue = (obj.Redstone[key][4] - e[4] + 1) * 2
if e[4] == obj.Redstone[key][4] then newValue = 0 end
--Если лево
elseif key == 5 then
newValue = (obj.Redstone[key][3] - e[3])
if e[3] == obj.Redstone[key][3] then newValue = 0 end
elseif key == 4 then
newValue = (e[3] - obj.Redstone[key][1])
if e[3] == obj.Redstone[key][1] then newValue = 0 end
end
if newValue > 15 then newValue = 15 elseif newValue < 0 then newValue = 0 end
-- ecs.error(newValue)
sendMessage("changeRedstoneOutput", key, newValue)
break
end
end
end
end
--Выбор слотов
if topBarElements[currentTopBarElement] == "Инвентарь" then
--Тип инвентаря
if obj.InventoryTypeSelectors then
for key in pairs(obj.InventoryTypeSelectors) do
if clicked(e[3], e[4], obj.InventoryTypeSelectors[key]) then
map.robotInventory = nil
currentInventoryType = key
drawInventoryFrom = 1
requestInfoAboutInventory()
drawAll()
end
end
end
--Слотики
if obj.InventorySlots then
for key in pairs(obj.InventorySlots) do
if clicked(e[3], e[4], obj.InventorySlots[key]) then
if e[5] ~= 1 then
if currentInventoryType == "internal" then
sendMessage("selectSlot", key)
end
else
if currentInventoryType == "internal" then
if map.robotInventory[key] then
local action = context.menu(e[3], e[4], {"Экипировать"}, {"Выбросить"}, "-", {"Инфо", true})
if action == "Экипировать" then
sendMessage("equip", key)
elseif action == "Выбросить" then
sendMessage("drop", nil, key)
end
end
else
local action = context.menu(e[3], e[4], {"Соснуть", map.robotInventory[key] == nil}, {"Положить суда", map.robotInventory[key] ~= nil}, "-", {"Инфо", true})
if action == "Соснуть" then
sendMessage("suckFromSlot", currentInventoryType, key)
end
end
end
break
end
end
end
end
elseif e[1] == "key_down" then
--W
if e[4] == 17 then
sendMessage("forward")
--S
elseif e[4] == 31 then
sendMessage("back")
--SHIFT
elseif e[4] == 42 then
sendMessage("down")
--SPACE
elseif e[4] == 57 then
sendMessage("up")
--A
elseif e[4] == 30 then
sendMessage("turnLeft")
--D
elseif e[4] == 32 then
sendMessage("turnRight")
--E
elseif e[4] == 18 then
if keyboard.isControlDown() then sendMessage("use") else sendMessage("swing") end
--Q
elseif e[4] == 16 then
sendMessage("drop")
elseif e[4] == 28 then
-- if map.keyPoints and #map.keyPoints > 0 then
-- sendMessage("executeKeyPoints", serialization.serialize(map.keyPoints))
-- end
requestInventoryInfo()
if topBarElements[currentTopBarElement] == "Карта" then
--W
if e[4] == 17 then
sendMessage("forward")
--S
elseif e[4] == 31 then
sendMessage("back")
--SHIFT
elseif e[4] == 42 then
sendMessage("down")
--SPACE
elseif e[4] == 57 then
sendMessage("up")
--A
elseif e[4] == 30 then
sendMessage("turnLeft")
--D
elseif e[4] == 32 then
sendMessage("turnRight")
--E
elseif e[4] == 18 then
if keyboard.isControlDown() then sendMessage("use") else sendMessage("swing") end
--Q
elseif e[4] == 16 then
sendMessage("place", 1)
--C
elseif e[4] == 46 then
local color = require("palette").draw("auto", "auto")
sendMessage("changeColor", color or 0xFFFFFF)
elseif e[4] == 28 then
-- if map.keyPoints and #map.keyPoints > 0 then
-- sendMessage("executeKeyPoints", serialization.serialize(map.keyPoints))
-- end
end
end
elseif e[1] == "modem_message" then
if e[4] == port then
@ -247,16 +603,16 @@ while true do
elseif e[8] == "down" then
map.currentLayer = map.currentLayer - 1
end
map.robotPosition.x = x; map.robotPosition.y = y; map.robotPosition.z = z
map.robot.x = x; map.robot.y = y; map.robot.z = z
table.insert(map, { type = "empty", x = x, y = y, z = z })
drawAll()
elseif e[7] == "successfullyRotatedTo" then
local adder = -1; if e[8] == "turnRight" then adder = 1 end
map.robotPosition.rotation = map.robotPosition.rotation + adder
if map.robotPosition.rotation < 1 then
map.robotPosition.rotation = 4
elseif map.robotPosition.rotation > 4 then
map.robotPosition.rotation = 1
map.robot.rotation = map.robot.rotation + adder
if map.robot.rotation < 1 then
map.robot.rotation = 4
elseif map.robot.rotation > 4 then
map.robot.rotation = 1
end
drawAll()
elseif e[7] == "successfullySwingedTo" then
@ -267,9 +623,28 @@ while true do
elseif e[7] == "inventoryInfo" then
map.robotInventory = serialization.unserialize(e[8])
drawAll()
elseif e[7] == "selectedSlot" then
map.robotInventory.currentSlot = e[8]
if topBarElements[currentTopBarElement] == "Инвентарь" then drawAll() end
elseif e[7] == "infoAboutRobot" then
map.robot.energy = e[8]
map.robot.maxEnergy = e[9]
map.robot.status = e[10]
if topBarElements[currentTopBarElement] == "Карта" then drawRobotStatus(); buffer.draw() end
elseif e[7] == "infoAboutRedstone" then
map.robot.redstone = serialization.unserialize(e[8])
if topBarElements[currentTopBarElement] == "Редстоун" then drawAll() end
end
end
end
end
elseif e[1] == "scroll" then
if topBarElements[currentTopBarElement] == "Инвентарь" then
if e[5] == 1 then
if drawInventoryFrom > 4 then drawInventoryFrom = drawInventoryFrom - 4; drawAll() end
else
if drawInventoryFrom < (map.robotInventory.inventorySize - 3) then drawInventoryFrom = drawInventoryFrom + 4; drawAll() end
end
end
end
end

Binary file not shown.

View File

@ -2,12 +2,14 @@
local event = require("event")
local robot = require("robot")
local computer = require("computer")
local component = require("component")
local serialization = require("serialization")
local modem = component.modem
local inventoryController = component.inventory_controller
local port = 1337
local moveSleepDelay = 0.05
local sides = {front = 3, bottom = 0, top = 1}
modem.open(port)
@ -22,22 +24,55 @@ local function try(functionName, successMessage, errorMessage)
if success then
sendMessage(successMessage, functionName)
else
print("Ошибка try:", functionName, reason)
print("Ошибка try: " .. tostring(functionName) .. " " .. tostring(reason))
sendMessage(errorMessage, functionName, reason)
end
os.sleep(moveSleepDelay)
-- os.sleep(moveSleepDelay)
end
local function sendInventory()
local function sendInventoryInfo(type)
local inventory = {}
local inventorySize = robot.inventorySize()
local inventorySize
for slot = 1, inventorySize do
table.insert(inventory, inventoryController.getStackInInternalSlot(slot))
if type == "internal" then
inventorySize = robot.inventorySize()
elseif type == "front" or type == "bottom" or type == "top" then
inventorySize = inventoryController.getInventorySize(sides[type])
end
if inventorySize then
inventory.inventorySize = inventorySize
inventory.type = type
if type == "internal" then
inventory.currentSlot = robot.select()
for slot = 1, inventorySize do
-- if robot.count(i) > 0 then
inventory[slot] = inventoryController.getStackInInternalSlot(slot)
-- end
end
elseif type == "front" or type == "bottom" or type == "top" then
for slot = 1, inventorySize do
inventory[slot] = inventoryController.getStackInSlot(sides[type], slot)
end
end
else
inventory.noInventory = true
end
sendMessage("inventoryInfo", serialization.serialize(inventory))
print("Отправляю информацию об инвентаре")
end
local function sendInfoAboutRobot()
sendMessage("infoAboutRobot", computer.energy(), computer.maxEnergy(), "Статус")
end
local function sendInfoAboutRedstone()
local redstoneInfo = {}
for i = 0, 5 do
redstoneInfo[i] = component.redstone.getOutput(i)
end
sendMessage("infoAboutRedstone", serialization.serialize(redstoneInfo))
end
------------------------------------------------------------------------------------------
@ -50,15 +85,39 @@ while true do
if e[7] == "forward" or e[7] == "back" or e[7] == "up" or e[7] == "down" then
try(e[7], "successfullyMovedTo", "cantMoveTo")
elseif e[7] == "turnLeft" or e[7] == "turnRight" then
try(e[7], "successfullyRotatedTo", "cantRotateWTF")
try(e[7], "successfullyRotatedTo", "")
elseif e[7] == "swing" then
try(e[7], "successfullySwingedTo", "cantSwingTo")
elseif e[7] == "use" then
robot.use()
elseif e[7] == "place" then
try(e[7], "successfullyPlacedTo", "cantPlaceTo")
elseif e[7] == "changeColor" then
component.robot.setLightColor(e[8])
elseif e[7] == "drop" then
robot.drop(e[8] or 1)
local oldSlot
if e[9] then robot.select(e[9]); oldSlot = robot.select() end
robot.drop(e[8])
sendInventoryInfo("internal")
if oldSlot then robot.select(oldSlot) end
elseif e[7] == "giveMeInfoAboutInventory" then
sendInventory()
sendInventoryInfo(e[8])
elseif e[7] == "selectSlot" then
robot.select(e[8])
sendMessage("selectedSlot", e[8])
elseif e[7] == "equip" then
local success = inventoryController.equip(e[8])
if success then sendInventoryInfo("internal") end
elseif e[7] == "giveMeInfoAboutRobot" then
sendInfoAboutRobot()
elseif e[7] == "giveMeInfoAboutRedstone" then
sendInfoAboutRedstone()
elseif e[7] == "suckFromSlot" then
local success = inventoryController.suckFromSlot(sides[e[8]], e[9])
if success then sendInventoryInfo(e[8]) end
elseif e[7] == "changeRedstoneOutput" then
component.redstone.setOutput(e[8], e[9])
sendInfoAboutRedstone()
end
end
end

Binary file not shown.

Binary file not shown.