books/board2.lua

by hugeblank
448 days agolua
COPY
1
local big = require("bigfont")
2
local m = peripheral.find("monitor")
3
local ok = true
4

5
local shades = {
6
    colors.toBlit(colors.red),
7
    colors.toBlit(colors.purple),
8
    colors.toBlit(colors.magenta),
9
    colors.toBlit(colors.pink)
10
}
11

12
local book = {
13
    {},
14
    { "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "c", "c", "c", "c", "c", "c", "c", "c"},
15
    { "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c"},
16
    { "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "e", "e", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c"},
17
    { "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "c", "c", "e", "e", "e", "e", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c"},
18
    { "f", "f", "f", "f", "f", "f", "f", "f", "c", "c", "c", "c", "c", "c", "e", "e", "e", "e", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c"},
19
    { "f", "f", "f", "f", "f", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "e", "e", "e", "e", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c"},
20
    { "f", "f", "f", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "e", "e", "e", "e", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c"},
21
    { "f", "f", "c", "c", "0", "0", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "e", "e", "e", "e", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "0", "0"},
22
    { "f", "c", "c", "0", "0", "0", "0", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "e", "e", "e", "e", "c", "c", "c", "c", "c", "c", "c", "0", "0", "0", "0", "0"},
23
    { "f", "f", "c", "c", "0", "0", "0", "0", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "e", "e", "4", "4", "c", "c", "c", "0", "0", "0", "0", "0", "0", "0", "0"},
24
    { "f", "f", "f", "c", "c", "0", "0", "0", "0", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "4", "4", "4", "4", "0", "0", "0", "0", "0", "0", "0", "0", "c", "c", "c"},
25
    { "f", "f", "f", "f", "c", "c", "0", "0", "0", "0", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "4", "4", "4", "4", "0", "0", "0", "0", "0", "c", "c", "c"},
26
    { "f", "f", "f", "f", "f", "c", "c", "0", "0", "0", "0", "c", "c", "c", "c", "c", "c", "0", "0", "0", "0", "4", "4", "e", "0", "0", "c", "c", "c"},
27
    { "f", "f", "f", "f", "f", "f", "c", "c", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "e", "e", "e", "c", "c"},
28
    { "f", "f", "f", "f", "f", "f", "f", "c", "c", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "c", "e", "e"},
29
    { "f", "f", "f", "f", "f", "f", "f", "f", "c", "c", "0", "0", "0", "0", "0", "0", "0", "c", "c", "c"},
30
    { "f", "f", "f", "f", "f", "f", "f", "f", "f", "c", "c", "c", "c", "c", "c", "c", "c"},
31
}
32

33
local function getBook()
34
    local out = {}
35
    for i = 1, #book do
36
        out[i] = {}
37
        for j = 1, #book[i] do
38
            out[i][j] = book[i][j]
39
        end
40
    end
41
    return out
42
end
43

44
local frames = {}
45
local period = 1886
46

47
local sins = function(x, y, offset)
48
    return math.sin((x+(y*(math.cos(offset/100)))+offset)/4)-0.5, (math.sin(((x*(math.cos(offset/150)+1))-y+(offset/2))/5)-0.5)
49
end
50
local indices = function(sin1, sin2)
51
    return math.floor(sin1*7.2)+1, math.floor(sin2*5)+2
52
end
53

54
local function makeFrame(p)
55
    local book = getBook()
56
    frames[p] = {}
57
    for i = 1, #book do
58
        for j = 1, #book[i] do
59
            local sin1, sin2 = sins(i, j, p)
60
            local shi1, shi2 = indices(sin1, sin2)
61
            if sin1 > 0 and book[i][j] ~= "f" and shi1 > 1 and shi1 >= shi2 then
62
                book[i][j] = shades[shi1]
63
            end
64
            if sin2 > 0 and book[i][j] ~= "f" and shi2 >= shi1 then
65
                book[i][j] = shades[shi2]
66
            end
67
        end
68
        frames[p][i] = table.concat(book[i], "")
69
    end
70
end
71

72
local function renderFrame(frame)
73
    local book = frames[frame]
74
    for j = 1, #book do
75
        m.setCursorPos(1, j)
76
        local blank = (" "):rep(#(book[j]))
77
        m.blit(blank, blank, book[j])
78
    end
79
end
80

81
-- setup frames
82
local frac = math.floor(period/3)
83
for p = 1, period do
84
    makeFrame(p)
85
    if p%frac == 0 then
86
        sleep() -- be nice
87
    end
88
end
89

90
local function run()
91
    while true do
92
        for i = 1, #frames do
93
            if ok then
94
                renderFrame(i)
95
                sleep()
96
            else
97
                sleep(1)
98
            end
99
        end
100
    end
101
end
102

103
local function esc()
104
    local exit = false
105
    while not exit do
106
        local _, k = os.pullEvent("key_up")
107
        if k == keys.space then
108
            return
109
        end
110
    end
111
end
112

113
local function text()
114
    local function init()
115
        m.setTextScale(1)
116
        m.setCursorBlink(false)
117
        m.setBackgroundColor(colors.black)
118
        m.clear()
119

120
        local sx, sy = m.getSize()
121
        local edge = 36
122

123
        local function center(y, text, offset)
124
            m.setCursorPos(math.ceil(((offset or 0)/2)+(sx/2)-(#text/2)), y)
125
            m.write(text)
126
        end
127

128
        if sx < 82 then
129
            ok = false
130
            printError("Must be displayed on a monitor of size 8x3 or 8x4.")
131
            big.writeOn(m, 2, "\2 NT.")
132
            return
133
        else
134
            ok = true
135
        end
136

137
        m.setTextColor(colors.lime)
138
        big.writeOn(m, 1, "Home of the \1641", edge+2, 2)
139
        big.writeOn(m, 1, "Enchanted Book.", edge+2, 5)
140
        local nameX, nameY
141
        local descriptionY
142
        if sy == 19 then
143
            nameX, nameY = edge+9, 9
144
            descriptionY = 13
145
        else
146
            nameX, nameY = edge-6, 21
147
            descriptionY = 9
148
        end
149
        m.setBackgroundColor(colors.gray)
150
        big.writeOn(m, 1, "books.kst", nameX, nameY)
151
        for y = nameY, nameY+2 do
152
            m.setCursorPos(nameX-1, y)
153
            m.write(" ")
154
        end
155
        m.setBackgroundColor(colors.black)
156

157
        local texts = {
158
            [0] = "Tools, Armor, and MORE",
159
            [2] = "--- Locations ---",
160
            [3] = "2F Coconut Mall | -30 80 -280",
161
            [4] = "Nether | 50 62 -490",
162
            [5] = "SC Public Library | 190 69 -170",
163
            [6] = "More coming soon! \2"
164
        }
165
        local max = 0
166
        for offset, text in pairs(texts) do
167
            max = math.max(offset, max)
168
        end
169
        if sy > 19 then
170
            texts[max+2] = "Don't forget to `/sethome books`!"
171
            m.setTextColor(colors.gray)
172
            center(sy, "books.kst is a supporter of open waterways and a member of the SCWPS.")
173
        end
174
        m.setTextColor(colors.lime)
175
        for offset, text in pairs(texts) do
176
            center(descriptionY+offset, text, edge)
177
        end
178
    end
179
    init()
180
    while true do
181
        local _, side = os.pullEvent("monitor_resize")
182
        if side == peripheral.getName(m) then
183
            init()
184
        end
185
    end
186
end
187

188
parallel.waitForAny(text, esc, run)
189