init.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --[===================================================================[--
  2. Digiplay - Copyright © 2017 Pedro Gimeno Fortea.
  3. This file is part of the package Digiplay. Both copying and distribution
  4. of this package are permitted, with or without modification, under the
  5. sole condition that any copyright notices and this notice are preserved.
  6. This package is offered as-is, without any warranty express or implied.
  7. --]===================================================================]--
  8. -- Informative note: The textures included in the package are under the
  9. -- same license; the files include a valid tEXt chunk with the copyright
  10. -- and license notice.
  11. -------------------------------------------------------------------------
  12. -- Minetest 0.4.16+ required for colour changing.
  13. -------------------------------------------------------------------------
  14. local function multicolour_light_on_punch(pos, node, puncher, pointed_thing)
  15. local channel = minetest.get_meta(pos):get_string("digich")
  16. if channel == "" then
  17. return
  18. end
  19. digiline:receptor_send(pos, digiline.rules.default, channel, "L " .. puncher:get_player_name())
  20. end
  21. local function multicolour_light_on_rightclick(pos, node, clicker, itemstack,
  22. pointed_thing)
  23. local channel = minetest.get_meta(pos):get_string("digich")
  24. if channel == "" then
  25. return
  26. end
  27. digiline:receptor_send(pos, digiline.rules.default, channel, "R " .. clicker:get_player_name())
  28. end
  29. local function multicolour_light_on_construct(pos)
  30. local meta = minetest.get_meta(pos)
  31. meta:set_string("formspec", "field[channel;Digiline Channel;]")
  32. end
  33. local function multicolour_light_on_receive_fields(pos, formname, fields, sender)
  34. if fields.channel and fields.channel ~= "" then
  35. local meta = minetest.get_meta(pos)
  36. meta:set_string("formspec", "")
  37. meta:set_string("digich", fields.channel)
  38. end
  39. end
  40. local function multicolour_light_effector_action(pos, node, channel, msg)
  41. local configured_channel = minetest.get_meta(pos):get_string("digich")
  42. if channel == configured_channel and type(msg) == "string" then
  43. local node = minetest.get_node(pos)
  44. local onoff, colour = msg:match("^([^#]*)#?(%x-)$")
  45. if onoff == "off" and node.name == "digiplay:multicolour_light_on" then
  46. node.name = "digiplay:multicolour_light"
  47. elseif onoff == "on" and node.name == "digiplay:multicolour_light" then
  48. node.name = "digiplay:multicolour_light_on"
  49. end
  50. if colour ~= "" and colour ~= nil then
  51. local base
  52. if colour:match("^%x%x%x%x%x%x$") then
  53. base = 256
  54. elseif colour:match("^%x%x%x$") then
  55. base = 16
  56. end
  57. if base then
  58. local r = tonumber(colour, 16)
  59. local b = r % base
  60. r = (r - b) / base
  61. local g = r % base
  62. r = (r - g) / base
  63. node.param2 = math.floor(g*5/base)*25 + math.floor(r*5/base)*5 + math.floor(b*5/base) + 1
  64. if node.param2 == 125 then node.param2 = 0 end
  65. end
  66. end
  67. minetest.swap_node(pos, node)
  68. end
  69. end
  70. for i = 0, 1 do
  71. local onoff = i == 0 and "" or "_on"
  72. local node_definition = {
  73. description = "Multi-coloured light";
  74. tiles = {"digiplay_multicolour_light" .. onoff .. ".png"};
  75. groups = {dig_immediate = 2};
  76. drawtype = "color";
  77. paramtype2 = "color";
  78. palette = "digiplay_palette.png";
  79. place_param2 = 0;
  80. on_receive_fields = multicolour_light_on_receive_fields;
  81. on_punch = multicolour_light_on_punch;
  82. on_construct = multicolour_light_on_construct;
  83. on_rightclick = multicolour_light_on_rightclick;
  84. digiline = {
  85. receptor = {
  86. rules = digiline.rules.default;
  87. };
  88. effector = {
  89. rules = digiline.rules.default;
  90. action = multicolour_light_effector_action;
  91. };
  92. };
  93. }
  94. if i == 1 then
  95. node_definition.light_source = 12
  96. node_definition.drop = "digiplay:multicolour_light"
  97. node_definition.groups.not_in_creative_inventory = 1
  98. end
  99. minetest.register_node("digiplay:multicolour_light" .. onoff, node_definition)
  100. end
  101. minetest.register_craft {
  102. output = "digiplay:multicolour_light";
  103. recipe = {
  104. {"", "digilines:wire_std_00000000", ""},
  105. {"default:clay", "default:mese_crystal_fragment", "default:clay"},
  106. {"dye:red", "dye:green", "dye:blue"}
  107. };
  108. }
  109. print("[mod] digiplay successfully loaded!")