lcd.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. --* parts are currently not possible because you cannot set the pitch of an entity from lua
  2. -- Font: 04.jp.org
  3. -- load characters map
  4. local chars_file = io.open(minetest.get_modpath("digilines").."/characters", "r")
  5. local charmap = {}
  6. local max_chars = 12
  7. if not chars_file then
  8. print("[digilines] E: LCD: character map file not found")
  9. else
  10. while true do
  11. local char = chars_file:read("*l")
  12. if char == nil then
  13. break
  14. end
  15. local img = chars_file:read("*l")
  16. chars_file:read("*l")
  17. charmap[char] = img
  18. end
  19. end
  20. -- CONSTANTS
  21. local LCD_WITH = 100
  22. local LCD_PADDING = 8
  23. local LINE_LENGTH = 12
  24. local NUMBER_OF_LINES = 5
  25. local LINE_HEIGHT = 14
  26. local CHAR_WIDTH = 5
  27. local create_lines = function(text)
  28. local line = ""
  29. local line_num = 1
  30. local tab = {}
  31. for word in string.gmatch(text, "%S+") do
  32. if string.len(line)+string.len(word) < LINE_LENGTH and word ~= "|" then
  33. if line ~= "" then
  34. line = line.." "..word
  35. else
  36. line = word
  37. end
  38. else
  39. table.insert(tab, line)
  40. if word ~= "|" then
  41. line = word
  42. else
  43. line = ""
  44. end
  45. line_num = line_num+1
  46. if line_num > NUMBER_OF_LINES then
  47. return tab
  48. end
  49. end
  50. end
  51. table.insert(tab, line)
  52. return tab
  53. end
  54. local generate_line = function(s, ypos)
  55. local i = 1
  56. local parsed = {}
  57. local width = 0
  58. local chars = 0
  59. while chars < max_chars and i <= #s do
  60. local file = nil
  61. if charmap[s:sub(i, i)] ~= nil then
  62. file = charmap[s:sub(i, i)]
  63. i = i + 1
  64. elseif i < #s and charmap[s:sub(i, i + 1)] ~= nil then
  65. file = charmap[s:sub(i, i + 1)]
  66. i = i + 2
  67. else
  68. print("[digilines] W: LCD: unknown symbol in '"..s.."' at "..i)
  69. i = i + 1
  70. end
  71. if file ~= nil then
  72. width = width + CHAR_WIDTH
  73. table.insert(parsed, file)
  74. chars = chars + 1
  75. end
  76. end
  77. width = width - 1
  78. local texture = ""
  79. local xpos = math.floor((LCD_WITH - 2 * LCD_PADDING - width) / 2 + LCD_PADDING)
  80. for ii = 1, #parsed do
  81. texture = texture..":"..xpos..","..ypos.."="..parsed[ii]..".png"
  82. xpos = xpos + CHAR_WIDTH + 1
  83. end
  84. return texture
  85. end
  86. local generate_texture = function(lines)
  87. local texture = "[combine:"..LCD_WITH.."x"..LCD_WITH
  88. local ypos = 16
  89. for i = 1, #lines do
  90. texture = texture..generate_line(lines[i], ypos)
  91. ypos = ypos + LINE_HEIGHT
  92. end
  93. return texture
  94. end
  95. local lcds = {
  96. -- on ceiling
  97. --* [0] = {delta = {x = 0, y = 0.4, z = 0}, pitch = math.pi / -2},
  98. -- on ground
  99. --* [1] = {delta = {x = 0, y =-0.4, z = 0}, pitch = math.pi / 2},
  100. -- sides
  101. [2] = {delta = {x = 0.437, y = 0, z = 0}, yaw = math.pi / -2},
  102. [3] = {delta = {x = -0.437, y = 0, z = 0}, yaw = math.pi / 2},
  103. [4] = {delta = {x = 0, y = 0, z = 0.437}, yaw = 0},
  104. [5] = {delta = {x = 0, y = 0, z = -0.437}, yaw = math.pi},
  105. }
  106. local reset_meta = function(pos)
  107. minetest.get_meta(pos):set_string("formspec", "field[channel;Channel;${channel}]")
  108. end
  109. local clearscreen = function(pos)
  110. local objects = minetest.get_objects_inside_radius(pos, 0.5)
  111. for _, o in ipairs(objects) do
  112. local o_entity = o:get_luaentity()
  113. if o_entity and o_entity.name == "digilines_lcd:text" then
  114. o:remove()
  115. end
  116. end
  117. end
  118. local set_texture = function(ent)
  119. local meta = minetest.get_meta(ent.object:get_pos())
  120. local text = meta:get_string("text")
  121. ent.object:set_properties({
  122. textures = {
  123. generate_texture(create_lines(text))
  124. }
  125. })
  126. end
  127. local get_entity = function(pos)
  128. local lcd_entity
  129. local objects = minetest.get_objects_inside_radius(pos, 0.5)
  130. for _, o in ipairs(objects) do
  131. local o_entity = o:get_luaentity()
  132. if o_entity and o_entity.name == "digilines_lcd:text" then
  133. if not lcd_entity then
  134. lcd_entity = o_entity
  135. else
  136. -- Remove extras, if any
  137. o:remove()
  138. end
  139. end
  140. end
  141. return lcd_entity
  142. end
  143. local rotate_text = function(pos, param)
  144. local entity = get_entity(pos)
  145. if not entity then
  146. return
  147. end
  148. local lcd_info = lcds[param or minetest.get_node(pos).param2]
  149. if not lcd_info then
  150. return
  151. end
  152. entity.object:set_pos(vector.add(pos, lcd_info.delta))
  153. entity.object:set_yaw(lcd_info.yaw or 0)
  154. end
  155. local prepare_writing = function(pos)
  156. local entity = get_entity(pos)
  157. if entity then
  158. set_texture(entity)
  159. rotate_text(pos)
  160. end
  161. end
  162. local spawn_entity = function(pos)
  163. if not get_entity(pos) then
  164. minetest.add_entity(pos, "digilines_lcd:text")
  165. rotate_text(pos)
  166. end
  167. end
  168. local on_digiline_receive = function(pos, _, channel, msg)
  169. local meta = minetest.get_meta(pos)
  170. local setchan = meta:get_string("channel")
  171. if setchan ~= channel then return end
  172. meta:set_string("text", msg)
  173. meta:set_string("infotext", msg)
  174. if msg ~= "" then
  175. prepare_writing(pos)
  176. end
  177. end
  178. local lcd_box = {
  179. type = "wallmounted",
  180. wall_top = {-8/16, 7/16, -8/16, 8/16, 8/16, 8/16}
  181. }
  182. minetest.register_alias("digilines_lcd:lcd", "digilines:lcd")
  183. minetest.register_node("digilines:lcd", {
  184. drawtype = "nodebox",
  185. description = "Digiline LCD",
  186. inventory_image = "lcd_lcd.png",
  187. wield_image = "lcd_lcd.png",
  188. tiles = {"lcd_anyside.png"},
  189. paramtype = "light",
  190. sunlight_propagates = true,
  191. light_source = 6,
  192. paramtype2 = "wallmounted",
  193. node_box = lcd_box,
  194. selection_box = lcd_box,
  195. groups = {choppy = 3, dig_immediate = 2},
  196. after_place_node = function(pos)
  197. local param2 = minetest.get_node(pos).param2
  198. if param2 == 0 or param2 == 1 then
  199. minetest.add_node(pos, {name = "digilines:lcd", param2 = 3})
  200. end
  201. spawn_entity(pos)
  202. prepare_writing(pos)
  203. end,
  204. on_construct = reset_meta,
  205. on_destruct = clearscreen,
  206. on_punch = function(pos, node, puncher, pointed_thing)
  207. if minetest.is_player(puncher) then
  208. spawn_entity(pos)
  209. end
  210. end,
  211. on_rotate = function(pos, node, user, mode, new_param2)
  212. if mode ~= screwdriver.ROTATE_FACE then
  213. return false
  214. end
  215. rotate_text(pos, new_param2)
  216. end,
  217. on_receive_fields = function(pos, _, fields, sender)
  218. local name = sender:get_player_name()
  219. if minetest.is_protected(pos, name) and not minetest.check_player_privs(name, {protection_bypass=true}) then
  220. minetest.record_protection_violation(pos, name)
  221. return
  222. end
  223. if (fields.channel) then
  224. minetest.get_meta(pos):set_string("channel", fields.channel)
  225. end
  226. end,
  227. digiline = {
  228. receptor = {},
  229. effector = {
  230. action = on_digiline_receive
  231. },
  232. },
  233. })
  234. minetest.register_lbm({
  235. label = "Replace Missing Text Entities",
  236. name = "digilines:replace_text",
  237. nodenames = {"digilines:lcd"},
  238. run_at_every_load = true,
  239. action = spawn_entity,
  240. })
  241. minetest.register_entity(":digilines_lcd:text", {
  242. collisionbox = { 0, 0, 0, 0, 0, 0 },
  243. visual = "upright_sprite",
  244. textures = {},
  245. on_activate = set_texture,
  246. })
  247. minetest.register_craft({
  248. output = "digilines:lcd 2",
  249. recipe = {
  250. {"default:steel_ingot", "digilines:wire_std_00000000", "default:steel_ingot"},
  251. {"mesecons_lightstone:lightstone_green_off","mesecons_lightstone:lightstone_green_off","mesecons_lightstone:lightstone_green_off"},
  252. {"default:glass","default:glass","default:glass"}
  253. }
  254. })