-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobfarm.lua
More file actions
228 lines (189 loc) · 6.25 KB
/
mobfarm.lua
File metadata and controls
228 lines (189 loc) · 6.25 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
-- Monitors and controls different parts of a mobfarm
-- Assumes that a monitor is attached to the top of the computer
-- fans are attached via redstone on the bottom
-- and that a modem is attached on the back
local monitorSide = "top"
local fanSide = "bottom"
local modemSide = "back"
local apiPath = "/usr/apis/touchpoint"
-- Moving global objects to local for optimization
-- Should only be done for objects used in the hot path
local stringSub = string.sub
local osPullEventRaw = os.pullEventRaw
local colorsBlack = colors.black
local colorsGreen = colors.green
local colorsGray = colors.gray
local tostring = tostring
local mathFloor = math.floor
local rsSetOutput = rs.setOutput
local rednetBroadcast = rednet.broadcast
local tableInsert = table.insert
local tableSort = table.sort
-- Constants
local tankWidth = 19
local tankBarHeight = 20
local topItemsWidth = 49
local topItemsHeight = 15
-- String constants
local tankLabel = "ESSENSE"
local tankLabelLen = #tankLabel
local tankLabelBackground = string.rep("=", tankWidth)
local tankBlank = string.rep(" ", tankWidth)
local topItemsLabel = "TOP ITEMS"
local topItemsLabelLen = #topItemsLabel
local topItemsLabelBackground = string.rep("=", topItemsWidth)
local topItemsBlank = string.rep(" ", topItemsWidth)
local topItemsItemsSuffix = " items"
local topItemsItemsSuffixLen = #topItemsItemsSuffix
-- Check if touchpoint is installed
if not fs.exists(apiPath) then
print("touchpoint is not installed; installing...")
shell.run("pastebin run 4zyreNZy")
shell.run("packman install touchpoint")
end
os.loadAPI("/usr/apis/touchpoint")
-- Check peripherals are where we expect them to be
if not peripheral.isPresent(monitorSide) then
error("Monitor is not present")
elseif not peripheral.isPresent(modemSide) then
error("Modem is not present")
end
local mon = peripheral.wrap(monitorSide)
local monSetBackgroundColor = mon.setBackgroundColor
local monSetCursorPos = mon.setCursorPos
local monWrite = mon.write
local tankLevel = 0
local function drawGraph()
-- Draw essense label
monSetBackgroundColor(colorsBlack)
monSetCursorPos(52, 2)
monWrite(tankLabelBackground)
monSetCursorPos(52 + ((tankWidth / 2) - (tankLabelLen / 2)), 2)
monWrite(tankLabel)
-- Draw percentage
monSetCursorPos(52, 3)
monWrite(tankBlank)
local percentage = stringSub(tostring(tankLevel * 100), 1, 5)
monSetCursorPos(52 + ((tankWidth / 2) - ((#percentage + 1) / 2)), 3)
monWrite(percentage)
monWrite("%")
-- Draw bar
local h = mathFloor(tankLevel * tankBarHeight)
for i = 0, tankBarHeight do
if i >= tankBarHeight - h then
monSetBackgroundColor(colorsGreen)
else
monSetBackgroundColor(colorsGray)
end
monSetCursorPos(52, i + 5)
monWrite(tankBlank)
end
end
local totalItems = 0
local items = {}
local itemsLen = 0
local function drawTopItems()
-- Draw top items label
monSetBackgroundColor(colorsBlack)
monSetCursorPos(2, 2)
monWrite(topItemsLabelBackground)
monSetCursorPos(2 + ((topItemsWidth / 2) - (topItemsLabelLen / 2)), 2)
monWrite(topItemsLabel)
-- Draw total items
local totalItemsStr = tostring(totalItems)
monSetCursorPos(2 +
((topItemsWidth / 2) -
((#totalItemsStr + topItemsItemsSuffixLen) / 2)), 3)
monWrite(totalItemsStr)
monWrite(topItemsItemsSuffix)
-- Draw top items
for i = 1, topItemsHeight do
monSetCursorPos(2, i + 4)
monWrite(topItemsBlank)
end
local itemsToDraw = topItemsHeight * 2
if itemsLen < itemsToDraw then itemsToDraw = itemsLen end
for i = 1, itemsToDraw do
if i > topItemsHeight then
monSetCursorPos(2 + math.ceil(topItemsWidth / 2),
i + 4 - topItemsHeight)
else
monSetCursorPos(2, i + 4)
end
monWrite(tostring(items[i].count))
monWrite(" - ")
monWrite(items[i].name)
end
end
rednet.open(modemSide)
if not fs.exists("items") then
local file = fs.open("items", "w")
file.write("{}")
file.close()
end
local itemsFile = fs.open("items", "r")
items = textutils.unserialize(itemsFile.readAll()) or {}
itemsLen = #items
for i = 1, itemsLen do
local item = items[i]
totalItems = totalItems + item.count
end
itemsFile.close()
local t = touchpoint.new(monitorSide)
t:add("fan", nil, 2, 21, 25, 25, colors.red, colors.lime)
t:add("lantern", nil, 27, 21, 50, 25, colors.red, colors.lime)
t:draw()
drawGraph()
drawTopItems()
-- Event loop
while true do
local event = {t:handleEvents(osPullEventRaw())}
if event[1] == "terminate" then
itemsFile = fs.open("items", "w")
itemsFile.write(textutils.serialize(items))
itemsFile.close()
return
elseif event[1] == "button_click" then
local buttonName = event[2]
t:toggleButton(buttonName)
if buttonName == "fan" then
rsSetOutput(fanSide, t.buttonList[buttonName].active)
elseif buttonName == "lantern" then
rednetBroadcast({
type = "lantern",
state = t.buttonList[buttonName].active
})
end
drawGraph()
drawTopItems()
elseif event[1] == "rednet_message" then
local message = event[3]
if message.type == "tank" then
tankLevel = message.amount
drawGraph()
elseif message.type == "items" then
local messageItem = message.item
local count = messageItem.qty
local name = messageItem.display_name
totalItems = totalItems + count
local found = false
for i = 1, itemsLen do
local item = items[i]
if item.name == name then
item.count = item.count + count
found = true
break
end
end
if not found then
tableInsert(items, {count = count, name = name})
itemsLen = itemsLen + 1
end
tableSort(items, function(a, b)
if a.count == b.count then return a.name < b.name end
return a.count > b.count
end)
drawTopItems()
end
end
end