View difference between Paste ID: 07WCW6Dt and 6SZq8XGf
SHOW: | | - or go back to the newest paste.
1
-- TurbineMonitor
2
--  Made By: MrKMG <mrkmg.com>
3
--
4
-- This program will keep your turbines up to speed
5
-- and turn the coil on and off as needed.
6
--
7
-- To Use:
8
--
9
-- Attach as many Turbines and Monitors as you
10
-- would like via wired modems. Monitors will all
11
-- display the same information for each turbine:
12
--    Current Speed
13
--    Current Power Generation,
14
--    Coil On/Off
15
--    Flow On/Off
16
--
17
-- Monitors can be of any size
18
--
19
20
--Edit these to fit your turbines
21
22
-- Speed to keep rotors between. Recommend putting
23
-- just above the best your turbine can do.
24
minSpeed = 1800
25
maxSpeed = 1820
26
27
-- Range of RF to keep in turbine. Recommend to
28
-- keep low to avoid waste.
29
minEnergy = 1
30
maxEnergy = 200000
31
32
-- Colors for the monitor
33
uiColors = {}
34
uiColors["background"] = colors.gray
35
uiColors["border"] = colors.black
36
uiColors["headerBackground"] = colors.blue
37
uiColors["headerText"] = colors.black
38
uiColors["currentSpeedBackground"] = colors.black
39
uiColors["currentSpeedText"] = colors.white
40
uiColors["currentPowerGenBackground"] = colors.black
41
uiColors["currentPowerGenText"] = colors.white
42
uiColors["coilOnBackground"] = colors.green
43
uiColors["coilOnText"] = colors.white
44
uiColors["coilOffBackground"] = colors.red
45
uiColors["coilOffText"] = colors.white
46
uiColors["flowOnBackground"] = colors.green
47
uiColors["flowOnText"] = colors.white
48
uiColors["flowOffBackground"] = colors.red
49
uiColors["flowOffText"] = colors.white
50
51
-- Text for the monitor.
52
uiText = {}
53
uiText["name"] = "Turbine "
54
uiText["rpm"] = " rpm"
55
uiText["rft"] = " RF/t"
56
uiText["rfs"] = " RF/s"
57
uiText["/"] = " / "
58
uiText["flowOn"] = "Flow: On"
59
uiText["flowOff"] = "Flow: Off"
60
uiText["coilsOn"] = "Coils: On"
61
uiText["coilsOff"] = "Coils: Off"
62
uiText["title"] = "Turbine Monitor"
63
64
65
66
-- Edit below at your your own risk
67
-- !#!#!#!#!#!#!#!#!#!#!#!#!#!#!#!#
68
69
function drawPixel(x, y, col)
70
	mon.setTextColor(uiColors["coilOnText"])
71
	mon.setCursorPos(x, y)
72
	mon.setBackgroundColor(col)
73
	mon.write(" ")
74
end
75
76
function drawLine(x, y, length, col)
77
	for i = 0, length - 1 do
78
		drawPixel(x + i, y, col)
79
	end
80
end
81
82
-- Center Text
83
local function printCenter(mon, y, txt)
84
	local w, h = mon.getSize()
85
	local x = math.floor((w / 2) - (#txt / 2))
86
87
	mon.setBackgroundColor(uiColors["flowOffBackground"])
88
	mon.setTextColor(uiColors["coilOnText"])
89
	mon.setCursorPos(x, y)
90
	mon.write(txt)
91
end
92
93
-- Global Variables
94
mon = peripheral.wrap("right")
95
96
t = {} -- Holds turbines
97
m = {} -- Holds monitor
98
w = {} -- Holds windows
99
100
-- Other Functions
101
102
function round(num)
103
    return math.floor(num + 0.5)
104
end
105
106
-- Program Functions
107
108
-- Finds all devices. Puts turbines in `t` table
109
-- and monitors in `m` table
110
function getDevices()
111
    for _, name in pairs(peripheral.getNames()) do
112
        if peripheral.getType(name) == "BigReactors-Turbine" then
113
            table.insert(t, peripheral.wrap(name))
114
        end
115
        if peripheral.getType(name) == "monitor" then
116
            table.insert(m, peripheral.wrap(name))
117
        end
118
    end
119
end
120
121
-- Determines if the coil should be turned on
122
function needCoilOn(i)
123
    if t[i].getEnergyStored() < minEnergy then
124
        return true
125
    else
126
        return false
127
    end
128
end
129
130
-- Determines if the coil should be turned off
131
function needCoilOff(i)
132
    if t[i].getEnergyStored() > maxEnergy then
133
        return true
134
    else
135
        return false
136
    end
137
end
138
139
-- Turns coil on
140
function coilOn(i)
141
    t[i].setInductorEngaged(true)
142
end
143
144
-- Tuns coil off
145
function coilOff(i)
146
    t[i].setInductorEngaged(false)
147
end
148
149
-- Checks if coil in on or off
150
function isCoilOn(i)
151
    return t[i].getInductorEngaged()
152
end
153
154
-- Determines if flow should turn on
155
function needFlowOn(i)
156
    if t[i].getRotorSpeed() < minSpeed then
157
        return true
158
    else
159
        return false
160
    end
161
end
162
163
-- Determines if flow should turn off
164
function needFlowOff(i)
165
    if t[i].getRotorSpeed() > maxSpeed then
166
        return true
167
    else
168
        return false
169
    end
170
end
171
172
-- Turns flow on
173
-- (Sets flow to MAX)
174
function flowOn(i)
175
    t[i].setFluidFlowRateMax(t[i].getFluidFlowRateMaxMax())
176
end
177
178
-- Turns flow off
179
-- (Sets flow to 0)
180
function flowOff(i)
181
    t[i].setFluidFlowRateMax(0)
182
end
183
184
-- Checks if flow is on or off
185
-- (Assumes MAX is on, other is off)
186
function isFlowOn(i)
187
    return t[i].getFluidFlowRateMax() == t[i].getFluidFlowRateMaxMax()
188
end
189
190
-- Get Rotor Speed
191
function getRotorSpeed(i)
192
    return math.floor(t[i].getRotorSpeed())
193
end
194
195
-- Get Power Generation
196
function getPowerGeneration(i)
197
    return math.floor(t[i].getEnergyProducedLastTick())
198
end
199
200
-- Get Sum Power Generation
201
function getSumPowerGeneration()
202
    local sum = 0
203
	for i = 1, #t ,1 do
204
		sum = sum + getPowerGeneration(i)
205
	end
206
	return sum
207
end
208
209
function getSumPowerGeneration20()
210
    local sum = 0
211
	for i = 1, #t ,1 do
212
		sum = getSumPowerGeneration() * 20
213
	end
214
	return sum
215
end
216
217
-- Returns text scale for best fit
218
-- TODO Figure out if there is a better way than
219
-- brute forcing
220
function getScaleForBox(baseWidth, baseHeight, fitWidth, fitHeight, count)
221
    local best = 0.5
222
223
    for i = best, 5, 0.5 do
224
        local cWidth = baseWidth / i
225
        local cHeight = baseHeight / i
226
227
        local cPerRow = math.floor(cWidth / fitWidth)
228
        local cPerCol = math.floor(cHeight / fitHeight)
229
230
        if cPerRow * cPerCol >= count then
231
            best = i
232
        else
233
            break
234
        end
235
    end
236
237
    return best
238
end
239
240
-- Determins offset to center the windows in the
241
-- monitor.
242
function getOffsetForWindow(monitorWidth, monitorHeight, desiredWidth, desiredHeight, count)
243
    local columnsNeeded = math.min(math.floor(monitorWidth / desiredWidth), count)
244
    local rowsNeeded = math.ceil(count / columnsNeeded)
245
246
    local widthUsed = desiredWidth * columnsNeeded
247
    local heightUsed = desiredHeight * rowsNeeded
248
249
    local offsetLeft = math.ceil((monitorWidth - widthUsed) / 2)
250
    local offsetRight = math.ceil((monitorHeight - heightUsed) / 2)
251
252
    return offsetLeft, offsetRight
253
end
254
255
-- Clears each connected monitor
256
function clearMonitors()
257
    for i = 1, #m do
258
        m[i].setBackgroundColor(uiColors["border"])
259
        m[i].clear()
260
    end
261
end
262
263
-- Setup each monitor
264
-- (Put a window in each monitor for each turbine)
265
function setupMonitors()
266
    if #t > 0 then
267
        local monitorWidth
268
        local monitorHeight
269
        local desiredW = 14
270
        local desiredH = 8
271
        local currentX
272
        local currentY
273
274
        for i = 1, #m do
275
            print("Setting Up " .. i)
276
277
            table.insert(w, {})
278
279
            m[i].setTextScale(1)
280
            monitorWidth, monitorHeight = m[i].getSize()
281
282
            print("Initial Size: " .. monitorWidth .. " " .. monitorHeight)
283
284
            scale = getScaleForBox(monitorWidth, monitorHeight, desiredW, desiredH, #t)
285
286
            print("Scale: " .. scale)
287
288
            m[i].setTextScale(scale)
289
            monitorWidth, monitorHeight = m[i].getSize()
290
291
            offsetX, offsetY = getOffsetForWindow(monitorWidth, monitorHeight, desiredW, desiredH, #t)
292
293
            print("Offset: " .. offsetX .. " " .. offsetY)
294
295
            currentX = offsetX
296
            currentY = offsetY
297
298
            for ii = 1, #t do
299
                print(currentX .. " " .. currentY .. " " .. desiredW .. " " .. desiredH)
300
                table.insert(w[i], window.create(m[i], currentX, currentY, desiredW, desiredH))
301
302
                currentX = currentX + desiredW
303
                if (currentX + desiredW > monitorWidth) then
304
                    currentX = offsetX
305
                    currentY = currentY + desiredH
306
                end
307
            end
308
        end
309
    end
310
end
311
312
-- Write status of turn to window
313
function writeStatus(turbine, screen)
314
    screen.setBackgroundColor(uiColors["border"])
315
    screen.clear()
316
317
    -- Write Title
318
    screen.setBackgroundColor(uiColors["headerBackground"])
319
    screen.setTextColor(uiColors["headerText"])
320
    screen.setCursorPos(2, 2)
321
    screen.clearLine()
322
    screen.write(uiText["name"] .. turbine)
323
324
    --Filler
325
    screen.setBackgroundColor(uiColors["background"])
326
    screen.setCursorPos(1, 3)
327
    screen.clearLine()
328
329
    -- Write Rotor Speed
330
    screen.setBackgroundColor(uiColors["currentSpeedBackground"])
331
    screen.setTextColor(uiColors["currentSpeedText"])
332
    screen.setCursorPos(2, 4)
333
    screen.clearLine()
334
    screen.write(getRotorSpeed(turbine) .. uiText["rpm"])
335
336
    -- Write Power Generation
337
    screen.setBackgroundColor(uiColors["currentPowerGenBackground"])
338
    screen.setTextColor(uiColors["currentPowerGenText"])
339
    screen.setCursorPos(2, 5)
340
    screen.clearLine()
341
    screen.write(getPowerGeneration(turbine) .. uiText["rft"])
342
343
    --Filler
344
    screen.setBackgroundColor(uiColors["background"])
345
    screen.setCursorPos(1, 6)
346
    screen.clearLine()
347
	
348
    -- Write Coil Status
349
    if isCoilOn(turbine) then
350
        screen.setBackgroundColor(uiColors["coilOnBackground"])
351
        screen.setTextColor(uiColors["coilOnText"])
352
        screen.setCursorPos(2, 7)
353
        screen.clearLine()
354
        screen.write(uiText["coilsOn"])
355
    else
356
        screen.setBackgroundColor(uiColors["coilOffBackground"])
357
        screen.setTextColor(uiColors["coilOffText"])
358
        screen.setCursorPos(2, 7)
359
        screen.clearLine()
360
        screen.write(uiText["coilsOff"])
361
    end
362
363
    -- Write Flow Status
364
    if isFlowOn(turbine) then
365
        screen.setBackgroundColor(uiColors["flowOnBackground"])
366
        screen.setTextColor(uiColors["flowOnText"])
367
        screen.setCursorPos(2, 8)
368
        screen.clearLine()
369
        screen.write(uiText["flowOn"])
370
    else
371
        screen.setBackgroundColor(uiColors["flowOffBackground"])
372
        screen.setTextColor(uiColors["flowOffText"])
373
        screen.setCursorPos(2, 8)
374
        screen.clearLine()
375
        screen.write(uiText["flowOff"])
376
    end
377
378
    -- Draw Right Border
379
    screen.setBackgroundColor(uiColors["border"])
380
    for i = 1, 8 do
381
        screen.setCursorPos(14, i)
382
        screen.write(" ")
383
    end
384
	
385
end
386
387
-- Write status of each turbine to appropiate windows
388
function writeStatuses()
389
    for i = 1, #m do
390
        for ii = 1, #t do
391
            writeStatus(ii, w[i][ii])
392
        end
393
    end
394
end
395
396
-- Setup
397
function setup()
398
    w = {}
399
    m = {}
400
    t = {}
401
402
    getDevices()
403
404
    if (#t == 0) then
405
        error("No Turbines. You did something wrong")
406
    end
407
408
    if (#m > 0) then
409
        clearMonitors()
410
        setupMonitors()
411
    end
412
end
413
414
-- Main Loop
415
function main()
416
    for i = 1, #t do
417
        if isCoilOn(i) then
418
            if needCoilOff(i) then
419
                print("turning off " .. i)
420
                coilOff(i)
421
            end
422
        else
423
            if needCoilOn(i) then
424
                print("turning on " .. i)
425
                coilOn(i)
426
            end
427
        end
428
        if isFlowOn(i) then
429
            if needFlowOff(i) then
430
                print("stopping flow " .. i)
431
                flowOff(i)
432
            end
433
        else
434
            if needFlowOn(i) then
435
                print("starting flow " .. i)
436
                flowOn(i)
437
            end
438
        end
439
    end
440
    writeStatuses()
441
end
442
443
-- Program Run
444
setup()
445
while true do
446
    main()
447
	
448
	drawLine(1, 2, 4, colors.black)
449
	drawLine(5, 2, 69, colors.red)
450
	drawLine(74, 2, 6, colors.black)
451
	
452
	drawLine(1, 3, 4, colors.black)
453
	drawLine(5, 3, 69, colors.red)
454
	printCenter(mon, 3, uiText["title"])
455
	drawLine(74, 3, 6, colors.black)
456
	
457
	drawLine(1, 4, 4, colors.black)
458
	drawLine(5, 4, 69, colors.red)
459
	drawLine(74, 4, 6, colors.black)
460
	
461
	drawLine(1, 48, 4, colors.black)
462
	drawLine(5, 48, 69, colors.red)
463
	drawLine(74, 48, 6, colors.black)
464
	
465
	drawLine(1, 49, 4, colors.black)
466
	drawLine(5, 49, 69, colors.red)
467
	printCenter(mon, 49, getSumPowerGeneration() .. uiText["rft"] .. uiText["/"] .. getSumPowerGeneration20() .. uiText["rfs"])
468
	drawLine(74, 49, 6, colors.black)
469
	
470
	drawLine(1, 50, 4, colors.black)
471
	drawLine(5, 50, 69, colors.red)
472
	drawLine(74, 50, 6, colors.black)
473
	
474
    os.sleep(1)
475
end