battery.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. --[[
  2. sl_controller
  3. =============
  4. Copyright (C) 2017-2020 Joachim Stolberg
  5. AGPL v3
  6. See LICENSE.txt for more information
  7. battery.lua:
  8. REPLACED BY SMARTLINE BATTERY !!!
  9. ]]--
  10. local function calc_percent(content)
  11. local val = (sl_controller.battery_capacity -
  12. math.min(content or 0, sl_controller.battery_capacity))
  13. return 100 - math.floor((val * 100.0 / sl_controller.battery_capacity))
  14. end
  15. local function on_timer(pos, elapsed)
  16. local meta = minetest.get_meta(pos)
  17. local percent = calc_percent(meta:get_int("content"))
  18. meta:set_string("infotext", "Battery ("..percent.."%)")
  19. if percent == 0 then
  20. local node = minetest.get_node(pos)
  21. node.name = "sl_controller:battery_empty"
  22. minetest.swap_node(pos, node)
  23. return false
  24. end
  25. return true
  26. end
  27. local function register_battery(ext, percent, nici)
  28. minetest.register_node("sl_controller:battery"..ext, {
  29. description = "Battery "..ext,
  30. inventory_image = 'sl_controller_battery_inventory.png',
  31. wield_image = 'sl_controller_battery_inventory.png',
  32. tiles = {
  33. -- up, down, right, left, back, front
  34. "smartline.png",
  35. "smartline.png",
  36. "smartline.png",
  37. "smartline.png",
  38. "smartline.png",
  39. "smartline.png^sl_controller_battery_green.png",
  40. },
  41. drawtype = "nodebox",
  42. node_box = {
  43. type = "fixed",
  44. fixed = {
  45. { -6/32, -6/32, 14/32, 6/32, 6/32, 16/32},
  46. },
  47. },
  48. after_place_node = function(pos, placer)
  49. local meta = minetest.get_meta(pos)
  50. meta:set_int("content", sl_controller.battery_capacity * percent)
  51. local node = minetest.get_node(pos)
  52. node.name = "sl_controller:battery"
  53. minetest.swap_node(pos, node)
  54. on_timer(pos, 1)
  55. minetest.get_node_timer(pos):start(30)
  56. end,
  57. on_timer = on_timer,
  58. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  59. local percent = calc_percent(tonumber(oldmetadata.fields.content))
  60. local stack
  61. if percent > 95 then
  62. stack = ItemStack("smartline:battery")
  63. elseif percent > 75 then
  64. stack = ItemStack("smartline:battery75")
  65. elseif percent > 50 then
  66. stack = ItemStack("smartline:battery50")
  67. elseif percent > 25 then
  68. stack = ItemStack("smartline:battery25")
  69. else
  70. return
  71. end
  72. local inv = minetest.get_inventory({type="player", name=digger:get_player_name()})
  73. inv:add_item("main", stack)
  74. end,
  75. paramtype = "light",
  76. sunlight_propagates = true,
  77. paramtype2 = "facedir",
  78. groups = {choppy=1, cracky=1, crumbly=1, not_in_creative_inventory=nici},
  79. drop = "",
  80. is_ground_content = false,
  81. sounds = default.node_sound_stone_defaults(),
  82. })
  83. end
  84. register_battery("", 1.0, 1)
  85. register_battery("75", 0.75, 1)
  86. register_battery("50", 0.5, 1)
  87. register_battery("25", 0.25, 1)
  88. minetest.register_node("sl_controller:battery_empty", {
  89. description = "Battery",
  90. tiles = {
  91. -- up, down, right, left, back, front
  92. "smartline.png",
  93. "smartline.png",
  94. "smartline.png",
  95. "smartline.png",
  96. "smartline.png",
  97. "smartline.png^sl_controller_battery_red.png",
  98. },
  99. drawtype = "nodebox",
  100. node_box = {
  101. type = "fixed",
  102. fixed = {
  103. { -6/32, -6/32, 14/32, 6/32, 6/32, 16/32},
  104. },
  105. },
  106. after_place_node = function(pos, placer)
  107. local meta = minetest.get_meta(pos)
  108. meta:set_int("content", 0)
  109. end,
  110. paramtype = "light",
  111. sunlight_propagates = true,
  112. paramtype2 = "facedir",
  113. groups = {choppy=1, cracky=1, crumbly=1, not_in_creative_inventory=1},
  114. drop = "",
  115. is_ground_content = false,
  116. sounds = default.node_sound_stone_defaults(),
  117. })
  118. --if minetest.global_exists("moreores") then
  119. -- minetest.register_craft({
  120. -- output = "sl_controller:battery 2",
  121. -- recipe = {
  122. -- {"", "moreores:silver_ingot", ""},
  123. -- {"", "default:copper_ingot", ""},
  124. -- {"", "moreores:silver_ingot", ""},
  125. -- }
  126. -- })
  127. --else
  128. -- minetest.register_craft({
  129. -- output = "sl_controller:battery 2",
  130. -- recipe = {
  131. -- {"", "default:tin_ingot", ""},
  132. -- {"", "default:copper_ingot", ""},
  133. -- {"", "default:tin_ingot", ""},
  134. -- }
  135. -- })
  136. --end