· 6 years ago · Apr 29, 2020, 03:00 PM
1--Darknifes Spawner controller
2function serialize(data, name)
3 if not fs.exists('/data') then
4 fs.makeDir('/data')
5 end
6 local f = fs.open('/data/'..name, 'w')
7 f.write(textutils.serialize(data))
8 f.close()
9end
10
11function unserialize(name)
12 if fs.exists('/data/'..name) then
13 local f = fs.open('/data/'..name, 'r')
14 data = textutils.unserialize(f.readAll())
15 f.close()
16 end
17 return data
18end
19
20--# load the touchpoint API
21os.loadAPI("touchpoint")
22
23--# intialize a new button set on the top monitor
24local MonSide = "back"
25local t = touchpoint.new(MonSide)
26local m = peripheral.wrap(MonSide)
27for i,v in ipairs(redstone.getSides()) do redstone.setBundledOutput(v,0) end
28m.setTextScale(1)
29local MonX, MonY = m.getSize()
30------------------------------------------------------------------------------------------------
31---------------------------- CONFIGS ---------------------------------------------------
32------------------------------------------------------------------------------------------------
33--Spawner Buttons
34
35-- Example entry for buttons --
36
37-- MobName = {"CableSide","RedstoneColour",initialstate,isInverted}
38
39--Edit this table to add mobs
40local Mobs = {
41 Enderman ={"top","orange",false,false},
42 Zombie = {"top","white",false,false},
43 Blaze = {"top","magenta",false,false},
44 Ghast = {"top","lightBlue",false,false},
45 Blizz = {"top","green",false,false},
46 Slime = {"top","red",false,false},
47 Basalz = {"top","lightGray",false,false},
48 WitherSkel ={"top","cyan",false,false}
49-- Skeleton = {"right","yellow",false,false},
50
51}
52--Controls for machines
53local Controls = {
54 Crushers = {"bottom","lightBlue",true,false},
55 Lights = {"bottom","white",true,false},
56 Spikes = {"bottom","magenta",true,false},
57 Access = {"bottom","yellow",false,true},
58 Slaughter = {"bottom","lime",false,false},
59 Duplicator = {"bottom","black",false,false}
60
61}
62
63
64
65local MobTitle = "Spawner Control"
66local ControlTitle = "System Control"
67
68
69-- all arrays in format [x,y]
70local MobGap = {3,2}
71local MobMin ={5,4}
72local MobMax = {MonX-6,18}
73local MobButtonSize = {12,2}
74
75local CtrlGap = {2,2}
76local CtrlMin ={3,22}
77local CtrlMax = {MonX-2,22}
78
79
80local ButtonSize={11,2}
81
82
83------------------------------------------------------------------------------------------------
84------------------------------------------------------------------------------------------------
85
86--Make Mob buttons
87local CurrentCoord = {MobMin[1],MobMin[2]}
88for key, value in pairs(Mobs) do
89
90
91
92
93 if CurrentCoord[1] > MobMax[1] then
94 CurrentCoord[2] = CurrentCoord[2]+MobButtonSize[2]+MobGap[2]
95 CurrentCoord[1] = MobMin[1]
96
97 end
98 if CurrentCoord[2] > MobMax[2] then
99 error("Too Many Buttons for Specified Dimentions")
100 end
101 t:add(key, nil, CurrentCoord[1], CurrentCoord[2], CurrentCoord[1]+MobButtonSize[1], CurrentCoord[2]+MobButtonSize[2], colors.red, colors.lime)
102 CurrentCoord[1] = CurrentCoord[1]+MobButtonSize[1]+MobGap[1]
103
104 if not fs.exists('/data/'..key) then
105 serialize(value[4],key)
106 end
107 RedstoneCol = 0
108 if unserialize(key) then
109 RedstoneCol = colors.combine(redstone.getBundledInput(value[1]),colors[value[2]])
110 redstone.setBundledOutput(value[1],RedstoneCol)
111 if (value[4] == false) then
112 t:toggleButton(key)
113 end
114 else if value[4] then
115 t:toggleButton(key)
116 end
117end
118end
119
120
121--Make Ctrl buttons
122local CurrentCoord = {CtrlMin[1],CtrlMin[2]}
123for key, value in pairs(Controls) do
124
125
126 if CurrentCoord[1] > CtrlMax[1] then
127 CurrentCoord[2] = CurrentCoord[2]+ButtonSize[2]+CtrlGap[2]
128 CurrentCoord[1] = CtrlMin[1]
129 end
130 if CurrentCoord[2] > CtrlMax[2] then
131 error("Too Many Buttons for Specified Dimentions")
132 end
133 t:add(key, nil, CurrentCoord[1], CurrentCoord[2], CurrentCoord[1]+ButtonSize[1], CurrentCoord[2]+ButtonSize[2], colors.red, colors.lime)
134 CurrentCoord[1] = CurrentCoord[1]+ButtonSize[1]+CtrlGap[1]
135 if not fs.exists('/data/'..key) then
136 if value[4] then
137 serialize(not value[3],key)
138 else
139 serialize(value[3],key)
140 end
141 end
142 RedstoneCol = 0
143 -- if (value[3] and (value[4] == false)) or (value[3] == false and (value[4])) then
144 -- RedstoneCol = colors.combine(redstone.getBundledInput(value[1]),colors[value[2]])
145 -- redstone.setBundledOutput(value[1],RedstoneCol)
146 -- if value[3] then
147 -- t:toggleButton(key)
148 -- end
149 -- else if value[4] then
150 -- t:toggleButton(key)
151 -- end
152 os.sleep(0.2)
153 if unserialize(key) then
154 RedstoneCol = colors.combine(redstone.getBundledInput(value[1]),colors[value[2]])
155 redstone.setBundledOutput(value[1],RedstoneCol)
156 if (value[4] == false) then
157 t:toggleButton(key)
158 end
159 else if value[4] then
160 t:toggleButton(key)
161 end
162end
163
164
165end
166
167
168
169--Fake buttons used as headers
170t:add(MobTitle, nil, 1, 1, MonX, 3, colors.black, colors.black)
171t:add(ControlTitle, nil, 1, 19, MonX, 21, colors.black, colors.black)
172
173--# draw the buttons
174t:draw()
175
176
177
178while true do
179 --# handleEvents will convert monitor_touch events to button_click if it was on a button
180 local event, p1 = t:handleEvents(os.pullEvent())
181 if event == "button_click" then
182 if Mobs[p1] or Controls[p1] then-- is a real button
183
184 if Mobs[p1] then --If its a mob
185 RSSide = Mobs[p1][1]
186 RSCol = Mobs[p1][2]
187 RSInv = Mobs[p1][4]
188 elseif Controls[p1] then --If its a control
189 RSSide = Controls[p1][1]
190 RSCol = Controls[p1][2]
191 RSInv = Controls[p1][4]
192 end
193 if (t.buttonList[p1].active and (RSInv == false)) or ((t.buttonList[p1].active == false) and RSInv) then
194 RedstoneCol = colors.subtract(redstone.getBundledInput(RSSide),colors[RSCol])
195 redstone.setBundledOutput(RSSide,RedstoneCol)
196
197 serialize(false,p1)
198 else
199 RedstoneCol = colors.combine(redstone.getBundledInput(RSSide),colors[RSCol])
200 redstone.setBundledOutput(RSSide,RedstoneCol)
201
202 serialize(true,p1)
203
204 end
205 t:toggleButton(p1)
206 os.sleep(0.15)
207 end
208 end
209 end