-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
223 lines (210 loc) · 5.67 KB
/
init.lua
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
--[[
## StreetsMod 2.0 ##
Submod: matrix_screen
Optional: true
]]
-- This submod was made by HybridDog and modified by Thomas S.
local matrix_px = 16
-- gets the object at pos
local function get_screen(pos)
local object
local objects = minetest.get_objects_inside_radius(pos, 0.5) or {}
for _, obj in pairs(objects) do
local ent = obj:get_luaentity()
if ent then
if ent.name == "streets:matrix_screen_lights" then
-- Remove duplicates
if object then
obj:remove()
else
object = obj
end
end
end
end
return object
end
-- used to get the texture for the object
local function generate_textures(data)
local texture, n = { "streets_matrix_screen_front.png^[combine:" .. matrix_px .. "x" .. matrix_px }, 2
for y = 1, matrix_px do
local xs = data[y]
for x = 1, matrix_px do
if xs[x] then
texture[n] = ":" .. x - 1 .. "," .. y - 1 .. "=streets_matrix_px.png"
n = n + 1
end
end
end
texture[n] = "^streets_matrix_screen_lines.png"
texture = table.concat(texture, "")
return { texture, texture, texture, texture, texture, texture }
end
-- updates texture of screen
local function update_screen(pos, data)
local obj = get_screen(pos) or minetest.add_entity(pos, "streets:matrix_screen_lights")
obj:set_properties({ textures = generate_textures(data) })
end
-- returns an empty toggleds table
local function new_toggleds()
local t = {}
for y = 1, matrix_px do
t[y] = {}
end
return t
end
-- gets the toggleds of the node from meta
local function get_data(meta)
local data = minetest.deserialize(minetest.decompress(meta:get_string("toggleds")))
if type(data) ~= "table" then
data = new_toggleds()
end
return data
end
-- update toggleds via a table sent by digiline
local function apply_changes(toggleds, t)
if type(t) ~= "table" then
return false, "matrix screen: got unsupported thing: " .. dump(t)
end
for y = 1, matrix_px do
toggleds[y] = {}
end
for y = 1, 16 do
local xs = t[y]
if type(xs) == "table" then
for x = 1, 16 do
local enabled = xs[x]
if enabled and enabled ~= 0 then
toggleds[y][x] = true
elseif enabled == false or enabled == 0 then
toggleds[y][x] = nil
end
end
end
end
return true
end
-- sets the toggleds of the node to meta
local function set_data(meta, toggleds)
meta:set_string("toggleds", minetest.compress(minetest.serialize(toggleds)))
end
minetest.register_node("streets:matrix_screen_base", {
description = "digiline controllable matrix screen",
tiles = { "streets_matrix_screen_front.png" },
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
groups = { cracky = 3, oddly_breakable_by_hand = 2 },
sounds = default.node_sound_stone_defaults(),
light_source = 14,
node_box = {
type = "fixed",
fixed = {
{ -0.5, -0.5, -0.49, 0.5, 0.5, 0.5 },
},
},
on_receive_fields = function(pos, formname, fields, sender)
local name = sender:get_player_name()
if minetest.is_protected(pos, name) and not minetest.check_player_privs(name, { protection_bypass = true }) then
minetest.record_protection_violation(pos, name)
return
end
if (fields.channel) then
minetest.get_meta(pos):set_string("channel", fields.channel)
end
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "field[channel;Channel;${channel}]")
meta:set_string("channel", "matrix_screen")
minetest.add_entity(pos, "streets:matrix_screen_lights")
end,
on_destruct = function(pos)
local obj = get_screen(pos)
if obj then
obj:remove()
end
end,
digiline = {
receptor = {},
effector = {
action = function(pos, node, channel, t)
local meta = minetest.get_meta(pos)
local toggleds = get_data(meta)
if channel == meta:get_string("channel") .. "_reset" then
for y = 1, matrix_px do
toggleds[y] = {}
end
set_data(meta, toggleds)
update_screen(pos, toggleds)
end
if channel ~= meta:get_string("channel") then
return
end
apply_changes(toggleds, t)
set_data(meta, toggleds)
update_screen(pos, toggleds)
end,
rules = {
{ x = 1, y = 0, z = 0 },
{ x = 1, y = 1, z = 0 },
{ x = 1, y = -1, z = 0 },
{ x = -1, y = 0, z = 0 },
{ x = -1, y = 1, z = 0 },
{ x = -1, y = -1, z = 0 },
{ x = 0, y = 1, z = 0 },
{ x = 0, y = 1, z = 1 },
{ x = 0, y = 1, z = -1 },
{ x = 0, y = -1, z = 0 },
{ x = 0, y = -1, z = 1 },
{ x = 0, y = -1, z = -1 },
{ x = 0, y = 0, z = 1 },
{ x = 0, y = 0, z = -1 },
}
},
},
})
-- ensure the screen existence
minetest.register_abm({
interval = 5,
chance = 1,
nodenames = { "streets:matrix_screen_base" },
action = function(pos, node)
if not get_screen(pos) then
local toggleds = get_data(minetest.get_meta(pos))
update_screen(pos, toggleds)
minetest.log("info", "[streets] matrix screen object was missing")
end
end,
})
-- the screen
minetest.register_entity("streets:matrix_screen_lights", {
collisionbox = { 0, 0, 0, 0, 0, 0 },
physical = false,
visual = "cube",
visual_size = { x = 0.99, y = 0.99 },
on_activate = function(self, staticdata)
local pos = self.object:getpos()
if not vector.equals(pos, vector.round(pos)) then
self.object:remove()
return
end
local node = minetest.get_node(pos)
if node.name ~= "streets:matrix_screen_base"
and node.name ~= "ignore" then
self.object:remove()
return
end
minetest.after(0, function(pos)
update_screen(pos, get_data(minetest.get_meta(pos)))
end, pos)
end,
})
minetest.register_craft({
output = "streets:matrix_screen_base",
recipe = {
{ "dye:yellow", "default:steel_ingot", "dye:yellow" },
{ "default:steel_ingot", "digilines:lcd", "default:steel_ingot" },
{ "dye:yellow", "default:steel_ingot", "dye:yellow" },
}
})