View difference between Paste ID: E4r2CTph and uLZu39jV
SHOW: | | - or go back to the newest paste.
1
rednet.open("right")
2
os.loadAPI("button")
3
local turtleID = 98
4
5
--  essentia is an array.  The array elements are defined by the name of the aspect.  The value of that element is the amount
6
--     of essentia in a jar.  For example:  essentia["ordo"]=21 means the ordo jar has 21 essentia
7
local essentia = {}
8
--  ejar is an array of the jars and essentia type for each jar location.  For example: ejar["tilejar_102"]="ordo"
9
local ejar = {}
10
11
--  Temp/testing var
12
local tmp
13
14
local jars = peripheral.getNames()
15-
local m = peripheral.wrap("monitor_0")
15+
local m = peripheral.wrap("top")
16
local monCoord = {}
17
local currEssentia
18
local fillAmt = 0
19
local rowsActive = true
20
21
function sortEss(t)
22
   local keys = {}
23
   for k in pairs(t) do keys[#keys+1] = k end
24
   table.sort(keys)
25
   
26
   local i = 0
27
   return function()
28
      i = i+1
29
      if keys[i] then
30
         return keys[i], t[keys[i]]
31
      end
32
   end
33
end
34
35
36
-- This function will do the initial scan all of the jars and store the values in the array essentia & ejar
37
function initScan()
38
  print("Starting inital scan")
39
  for i,j in ipairs(jars) do
40
    if peripheral.getType(j) == "tilejar" then
41
      pw = peripheral.wrap(j)
42
      asp = pw.getEssentiaType('front')
43
      ejar[j] = asp
44
      --print("    "..j.." = "..ejar[j])
45
      countasp = pw.getEssentiaAmount('front')
46
      if countasp > 0 then
47
        essentia[asp] = math.floor(countasp)
48
      else
49
        essentia[asp] = 0
50
      end
51
    end
52
  end
53
  --print("  tilejar_178 = "..ejar["tilejar_178"])
54
end
55
56
57
-- This function will scan all of the jars and store the value in the array essentia
58
function scanEssentia()
59
  print("Starting scan function")
60
  --print("  tilejar_183 = "..ejar["tilejar_183"])
61
  for i,j in pairs(ejar) do
62
    --print("    "..i.." = "..j)
63
    pw = peripheral.wrap(i)
64
    --asp = pw.getEssentiaType('front')
65
    countasp = pw.getEssentiaAmount('front')
66
    --print("    "..j.." = "..countasp)
67
    if countasp > 0 then
68
      essentia[j] = math.floor(countasp)
69
    else
70
      essentia[j] = 0
71
    end
72
  end
73
end
74
75
function printEssentia()
76
  m.setTextColor(colors.white)
77
  local x = 1
78
  local y = 1
79
  monCoord[x] = {}
80
  for i,j in sortEss(essentia) do
81
     if j<=20 then m.setTextColor(colors.red) end
82
     if j<40 and j>20 then m.setTextColor(colors.yellow) end
83-
     if j>=40 and j<64 then m.setTextColor(colors.lightBlue) end
83+
     if j>=40 then m.setTextColor(colors.green) end
84-
     if j==64 then m.setTextColor(colors.green) end
84+
85
     m.setCursorPos(x,y)
86
     m.write(i)
87
     m.setCursorPos(x+14,y)
88
     m.write(tostring(j))
89
     monCoord[x][y] = i
90
     if y < 17 then
91
        y = y+1
92
     else
93
        y = 1
94
        x = x+17
95
        monCoord[x] = {}   
96
     end
97
  end
98
 m.setTextColor(colors.white)
99
end
100
101
function getClick()
102
   local event,side,x,y = os.pullEvent()
103
   if event=="monitor_touch" then
104
     if button.checkxy(x,y) then
105
        print("button")
106
     else
107
        if rowsActive then
108
          fillAmt = 0
109
          print(x..":"..x-(x%17)+1)
110
          print(monCoord[x-(x%17)+1][y])
111
          currEssentia = monCoord[x-(x%17)+1][y]
112
          if currEssentia ~= nil then
113
          if essentia[currEssentia] < 64 then
114
             fillTable2()
115
          else
116
             m.clear()
117
             button.label(1,10, currEssentia.." is already full.  Please choose another.")
118
             sleep(3)
119
             refresh()
120
          end
121
          end
122
        end
123
     end
124
   end
125
end
126
127
function refresh()
128
   button.flash("Refresh")
129
   m.clear()
130
   scanEssentia()
131
   printEssentia()
132
   print("Refreshed")
133
   button.screen()
134
end
135
136
-- This function will fill all empty jars to 64
137
function fillAll()
138
  m.clear()
139
  button.label(7, 8,  "         Now filling all jars")
140
  button.label(7, 10, "Wait for aspects to finish cooking......")
141
  button.label(7, 12, "Don't forget to refresh the screen after")
142
  button.label(7, 13, "the golems are done moving the essentia.")
143
  local essData = {}
144
  button.flash("Fill All")
145
  for i,j in ipairs(jars) do
146
    if peripheral.getType(j) == "tilejar" then
147
      pw = peripheral.wrap(j)
148
      asp = ejar[j]
149
      countasp = pw.getEssentiaAmount('front')
150
      if countasp > 0 then
151
        essentia[asp] = math.floor(countasp)
152
      else
153
        essentia[asp] = 0
154
      end
155
      if essentia[asp] >= 64 then
156
        print(asp.." is full")
157
      else
158
        print(asp.." needs "..64-essentia[asp])
159
        essData[1] = asp
160
        essData[2] = 64-essentia[asp]
161
        local sendData = ""
162
        sendData = textutils.serialize(essData)
163
        print("turtleID = "..turtleID..", sendData = "..sendData)
164
        rednet.send(turtleID, sendData)
165
        rednet.receive()        
166
      end
167
    end
168
  end
169
  m.clear()
170
  fillTable()
171
  printEssentia()
172
  refresh()
173
end
174
175
function fillTable()
176
  rowsActive = true
177
  button.clearTable()
178
  button.setTable("Fill All", fillAll, "", 5, 23, 19, 19)
179
  button.setTable("Refresh", refresh, "", 27, 46, 19, 19)
180
  button.screen()
181
end
182
183
function addEss(num)
184
   fillAmt = fillAmt + num
185
   if fillAmt < 0 then fillAmt = 0 end
186
   if fillAmt > 64-essentia[currEssentia] then fillAmt = 64-essentia[currEssentia] end
187
   m.clear()
188
   fillTable2()
189
end
190
191
function fillEss()
192
   local essData = {}
193
   essData[1] = currEssentia
194
   essData[2] = fillAmt
195
   local sendData = ""
196
   sendData = textutils.serialize(essData)
197
   print("turtleID = "..turtleID..", sendData = "..sendData)
198
   rednet.send(turtleID, sendData)
199
   m.clear()
200
   button.label(7, 10, "Waiting for aspects to finish cooking....")
201
   button.label(7, 12, "Don't forget to refresh the screen after")
202
   button.label(7, 13, "the golem is done moving the essentia.")
203
   rednet.receive()
204
   m.clear()
205
   fillTable()
206
   printEssentia()
207
   --refresh()
208
end
209
210
function cancel()
211
   m.clear()
212
   fillTable()
213
   refresh()
214
end   
215
216
function fillTable2()
217
   rowsActive = false
218
   button.clearTable()
219
   m.clear()
220
   button.label(7, 1, "Essentia: "..currEssentia.." contains "..essentia[currEssentia])
221
   button.setTable("+1", addEss, 1, 8, 18, 6,6)
222
   button.setTable("+5", addEss, 5, 20, 30, 6, 6)
223
   button.setTable("+10", addEss, 10, 32, 42, 6, 6)
224
   button.setTable("-1", addEss, -1, 8, 18, 8, 8)
225
   button.setTable("-5", addEss, -5, 20, 30, 8, 8)
226
   button.setTable("-10", addEss, -10, 32, 42, 8 ,8)
227
   button.setTable("Refill Jar", addEss, 64-essentia[currEssentia], 8, 42, 10, 10)
228
   button.setTable("Execute Fill Request", fillEss, "", 8, 42, 16, 18)
229
   button.setTable("Cancel", cancel, "", 20, 30, 12, 14)
230
   button.label(7, 4, "Currently Adding "..fillAmt.." "..currEssentia.." essentia.")
231
   button.screen()
232
end
233
234
fillTable()
235
initScan()
236
refresh()
237
while true do getClick() end