functions.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. grinder.get_formspec_defaults = function()
  2. local str =
  3. default.gui_bg ..
  4. default.gui_bg_img ..
  5. default.gui_slots
  6. return str
  7. end
  8. grinder.formspec_active = function(fuel_percent, item_percent)
  9. local formspec =
  10. "size[8,8.5]" ..
  11. grinder.get_formspec_defaults() ..
  12. "label[3.5,0;Fuel & Input]" ..
  13. "list[context;src;3.5,0.5;1,1;]" ..
  14. "list[context;fuel;3.5,2.5;1,1;]" ..
  15. "image[3.5,1.5;1,1;machine_progress_bg.png^[lowpart:" ..
  16. (100-fuel_percent) .. ":machine_progress_fg.png]" ..
  17. "image[4.5,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:" ..
  18. (item_percent) .. ":gui_furnace_arrow_fg.png^[transformR270]" ..
  19. "label[5.5,0.46;Destination]" ..
  20. "list[context;dst;5.5,0.96;2,2;]" ..
  21. "list[current_player;main;0,4.25;8,1;]" ..
  22. "list[current_player;main;0,5.5;8,3;8]" ..
  23. "label[0.75,0;Configuration]" ..
  24. "list[context;cfg;0.75,0.5;2,1;]" ..
  25. "label[0.75,2;Upgrades]" ..
  26. "list[context;upg;0.75,2.5;2,1;]" ..
  27. "listring[context;dst]" ..
  28. "listring[current_player;main]" ..
  29. "listring[context;src]" ..
  30. "listring[current_player;main]" ..
  31. "listring[context;fuel]"..
  32. "listring[current_player;main]"..
  33. default.get_hotbar_bg(0, 4.25)
  34. return formspec
  35. end
  36. grinder.formspec_inactive = function()
  37. return grinder.formspec_active(100, 0)
  38. end
  39. grinder.on_punch =
  40. function(pos, node, puncher, pointed_thing)
  41. machines.initialize_typedata(pos, "grinder:inactive", "mv")
  42. grinder.trigger_update(pos)
  43. end
  44. grinder.trigger_update =
  45. function(pos)
  46. local timer = minetest.get_node_timer(pos)
  47. if not timer:is_started() then
  48. timer:start(1.0)
  49. end
  50. end
  51. grinder.can_dig = function(pos, player)
  52. local meta = minetest.get_meta(pos);
  53. local inv = meta:get_inventory()
  54. return inv:is_empty("fuel") and
  55. inv:is_empty("dst") and
  56. inv:is_empty("src") and
  57. inv:is_empty("cfg") and
  58. inv:is_empty("upg")
  59. end
  60. grinder.allow_metadata_inventory_put =
  61. function(pos, listname, index, stack, player)
  62. return machines.allow_metadata_inventory_put(
  63. pos, listname, index, stack, player,
  64. "mesefuel", "fuel", "src", "dst", "cfg", "upg")
  65. end
  66. grinder.allow_metadata_inventory_move =
  67. function(pos, from_list, from_index, to_list, to_index, count, player)
  68. local meta = minetest.get_meta(pos)
  69. local inv = meta:get_inventory()
  70. local stack = inv:get_stack(from_list, from_index)
  71. return grinder.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  72. end
  73. grinder.allow_metadata_inventory_take =
  74. function(pos, listname, index, stack, player)
  75. local pname = player:get_player_name()
  76. if minetest.test_protection(pos, pname) then
  77. return 0
  78. end
  79. return stack:get_count()
  80. end
  81. local MACHINE_DATA = {
  82. name = "Grinder",
  83. method = "grinding",
  84. demand = 200,
  85. swap = {
  86. inactive = "grinder:inactive",
  87. active = "grinder:active",
  88. },
  89. form = {
  90. inactive = grinder.formspec_inactive,
  91. active = grinder.formspec_active,
  92. },
  93. fuel = "mesefuel",
  94. processable = "Grindable",
  95. }
  96. grinder.on_timer = function(pos, elapsed)
  97. machines.log_update(pos, "Grinder")
  98. return machines.on_machine_timer(pos, elapsed, MACHINE_DATA)
  99. end
  100. grinder.on_construct =
  101. function(pos)
  102. end
  103. grinder.after_place_node =
  104. function(pos, placer, itemstack, pointed_thing)
  105. machines.initialize_typedata(pos, "grinder:inactive", "mv")
  106. machines.after_place_machine(pos, placer, "Grinder", 1, grinder.formspec_inactive)
  107. end
  108. grinder.on_metadata_inventory_move =
  109. function(pos)
  110. grinder.trigger_update(pos)
  111. end
  112. grinder.on_metadata_inventory_put =
  113. function(pos)
  114. grinder.trigger_update(pos)
  115. end
  116. grinder.on_metadata_inventory_take =
  117. function(pos)
  118. grinder.trigger_update(pos)
  119. end
  120. grinder.on_blast = function(pos)
  121. local drops = {}
  122. default.get_inventory_drops(pos, "src", drops)
  123. default.get_inventory_drops(pos, "fuel", drops)
  124. default.get_inventory_drops(pos, "dst", drops)
  125. default.get_inventory_drops(pos, "cfg", drops)
  126. default.get_inventory_drops(pos, "upg", drops)
  127. drops[#drops+1] = "grinder:inactive"
  128. minetest.remove_node(pos)
  129. return drops
  130. end