Skip to content

Commit

Permalink
add a digiline controllable matrix screen
Browse files Browse the repository at this point in the history
  • Loading branch information
HybridDog committed May 10, 2016
1 parent 37188dd commit 244dcde
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dofile(streets.conf.modpath .. "/global.lua")
streets.load_submod("streets_roadsurface")
streets.load_submod("streets_roadmarkings")
streets.load_submod("streets_roadworks")
streets.load_submod("matrix_screen")

-- Let the API register everything and finish the setup
dofile(streets.conf.modpath .. "/api_register_all.lua")
190 changes: 190 additions & 0 deletions matrix_screen/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
local matrix_px = 16

-- gets the object at pos
local function get_screen(pos)
for _,obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do
local ent = obj:get_luaentity()
if ent
and ent.name == "streets:matrix_screen_lights" then
return obj
end
end
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
if n == 2 then
-- entirely disabled
texture = "streets_matrix_screen_front.png"
else
texture[n] = "^streets_matrix_screen_lines.png"
texture = table.concat(texture, "")
end
--print(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
if t == "reset" then
for y = 1,matrix_px do
toggleds[y] = {}
end
return true
end
return false, "matrix screen: got unsupported thing: "..dump(t)
end
local changed
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
and not toggleds[y][x] then
toggleds[y][x] = true
changed = true
elseif (enabled == false
or enabled == 0)
and toggleds[y][x] then
toggleds[y][x] = nil
changed = true
end
end
end
end
if changed then
--return true, "successfully updated toggleds"
return true
end
return false, "nothing updated"
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 = 15,
node_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.49, 0.5, 0.5, 0.5},
},
},
on_construct = function(pos)
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)
if channel ~= "streets:matrix_screen" then
return
end
local meta = minetest.get_meta(pos)
local toggleds = get_data(meta)
local changed, msg = apply_changes(toggleds, t)
if changed then
set_data(meta, toggleds)
update_screen(pos, toggleds)
else
digiline:receptor_send(pos, digiline.rules.default, "streets:matrix_screen_error", msg)
end
end
},
},
})

-- ensure the screen existence
minetest.register_lbm({
name = "streets:matrix_screen_loading",
nodenames = {"streets:matrix_screen_base"},
run_at_every_load = true,
action = function(pos, node)
if not get_screen(pos) then
local toggleds = get_data(minetest.get_meta(pos))
update_screen(pos, toggleds)
minetest.log("error", "[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)
--[[
print(8)
self.object:set_properties(
{textures = generate_texture(
get_data(minetest.get_meta(pos)))})--]]
end,
})
Binary file added textures/streets_matrix_px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added textures/streets_matrix_screen_front.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added textures/streets_matrix_screen_lines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

4 comments on commit 244dcde

@HybridDog
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the streets_matrix_screen_lines.png is obviously not transparent enough, see that screenshot: https://forum.minetest.net/viewtopic.php?p=216808#p216808

@webD97
Copy link

@webD97 webD97 commented on 244dcde May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "toggleds" stand for? The name is confusing me...

@HybridDog
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a table which tells which pixels are activated, sorry for my bad english

@webD97
Copy link

@webD97 webD97 commented on 244dcde May 12, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.