init.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. --[[
  2. More Blocks: circular saw
  3. Copyright (c) 2011-2015 Calinou and contributors.
  4. Licensed under the zlib license. See LICENSE.md for more information.
  5. --]]
  6. local S = function(str) return str end
  7. circular_saw = circular_saw or {}
  8. -- This is populated by stairsplus:register_all:
  9. circular_saw.known_nodes = circular_saw.known_nodes or {}
  10. -- 3rd parameter: how many microblocks does this shape cost:
  11. -- It may cause slight loss, but no gain.
  12. -- 1st and 2nd parameters are nodename prefix/postfixes.
  13. circular_saw.names = {
  14. {"micro", "_1", 1},
  15. {"panel", "_1", 1},
  16. {"micro", "_2", 1},
  17. {"panel", "_2", 1},
  18. {"micro", "_4", 1},
  19. {"panel", "_4", 1},
  20. {"micro", "", 1},
  21. {"panel", "", 2},
  22. {"micro", "_12", 2},
  23. {"panel", "_12", 2},
  24. {"micro", "_14", 3},
  25. {"panel", "_14", 2},
  26. {"micro", "_15", 4},
  27. {"panel", "_15", 2},
  28. {"micro", "_16", 4},
  29. {"panel", "_16", 4},
  30. {"stair", "_outer", 5},
  31. {"stair", "", 6},
  32. {"stair", "_inner", 7},
  33. {"slab", "_1", 1},
  34. {"slab", "_2", 1},
  35. {"slab", "_quarter", 2},
  36. {"slab", "", 4},
  37. {"slab", "_three_quarter", 6},
  38. {"slab", "_14", 7},
  39. {"slab", "_15", 8},
  40. {"stair", "_half", 3},
  41. {"stair", "_right_half", 3},
  42. {"stair", "_alt_1", 1},
  43. {"stair", "_alt_2", 1},
  44. {"stair", "_alt_4", 2},
  45. {"stair", "_alt_5", 1},
  46. {"stair", "_alt_6", 2},
  47. {"stair", "_alt", 4},
  48. {"slope", "", 4},
  49. {"slope", "_half", 2},
  50. {"slope", "_half_raised", 6},
  51. {"slope", "_inner", 7},
  52. {"slope", "_inner_half", 3},
  53. {"slope", "_inner_half_raised", 7},
  54. {"slope", "_inner_cut", 7},
  55. {"slope", "_inner_cut2", 8},
  56. {"slope", "_inner_cut3", 8},
  57. {"slope", "_inner_cut4", 4},
  58. {"slope", "_inner_cut5", 4},
  59. {"slope", "_inner_cut6", 7},
  60. {"slope", "_inner_cut7", 8},
  61. {"slope", "_inner_cut_half", 4},
  62. {"slope", "_inner_cut_half_raised", 8},
  63. {"slope", "_outer", 3},
  64. {"slope", "_outer_half", 2},
  65. {"slope", "_outer_half_raised", 6},
  66. {"slope", "_outer_cut", 2},
  67. {"slope", "_outer_cut_half", 1},
  68. {"slope", "_outer_cut_half_raised", 3},
  69. {"slope", "_cut", 4},
  70. {"slope", "_xslope_quarter", 2},
  71. {"slope", "_xslope_quarter2", 2},
  72. {"slope", "_xslope_three_quarter", 6},
  73. {"slope", "_xslope_three_quarter_half", 4},
  74. {"slope", "_xslope_cut", 4},
  75. {"slope", "_xslope_slope", 1},
  76. {"slab", "_two_sides", 1},
  77. {"slab", "_three_sides", 2},
  78. {"slab", "_three_sides_u", 2},
  79. {"slab", "_four_sides", 3},
  80. {"slab", "_hole", 3},
  81. {"slab", "_two_opposite", 1},
  82. {"slab", "_pit", 3},
  83. {"slab", "_pit_half", 2},
  84. {"stair", "_half_1", 1},
  85. {"stair", "_right_half_1", 1},
  86. {"slope", "_xslope_peak", 4},
  87. {"slope", "_xslope_peak_half", 2},
  88. {"slope", "_lh", 2},
  89. {"slope", "_half_lh", 1},
  90. {"slope", "_half_raised_lh", 3},
  91. {"slope", "_xslope_slope_lh", 1},
  92. {"slope", "_xslope_peak_lh", 2},
  93. {"slope", "_xslope_peak_half_lh", 1},
  94. {"slope", "_rh", 2},
  95. {"slope", "_half_rh", 1},
  96. {"slope", "_half_raised_rh", 3},
  97. {"slope", "_xslope_slope_rh", 1},
  98. {"slab", "_hole_half", 2},
  99. {"slope", "_astair_1", 6},
  100. {"slope", "_astair_2", 5},
  101. {"slope", "_astair_3", 6},
  102. {"slope", "_astair_4", 6},
  103. {"slope", "_astair_5", 8},
  104. }
  105. function circular_saw:get_cost(inv, stackname)
  106. for i, item in pairs(inv:get_list("output")) do
  107. if item:get_name() == stackname then
  108. return circular_saw.names[i][3]
  109. end
  110. end
  111. end
  112. function circular_saw:get_output_inv(modname, material, amount, max)
  113. if (not max or max < 1 or max > 64) then max = 64 end
  114. local list = {}
  115. local pos = #list
  116. -- If there is nothing inside, display empty inventory:
  117. if amount < 1 then
  118. return list
  119. end
  120. for i = 1, #circular_saw.names do
  121. local t = circular_saw.names[i]
  122. local cost = t[3]
  123. local balance = math.min(math.floor(amount/cost), max)
  124. local nodename = modname .. ":" .. t[1] .. "_" .. material .. t[2]
  125. if minetest.registered_nodes[nodename] then
  126. pos = pos + 1
  127. list[pos] = nodename .. " " .. balance
  128. end
  129. end
  130. return list
  131. end
  132. -- Reset empty circular_saw after last full block has been taken out
  133. -- (or the circular_saw has been placed the first time)
  134. -- Note: max_offered is not reset:
  135. function circular_saw:reset(pos)
  136. local meta = minetest.get_meta(pos)
  137. local inv = meta:get_inventory()
  138. local owner = meta:get_string("owner")
  139. local dname = rename.gpn(owner)
  140. inv:set_list("input", {})
  141. inv:set_list("micro", {})
  142. inv:set_list("output", {})
  143. meta:set_int("anz", 0)
  144. meta:set_string("infotext",
  145. S("Circular Saw is Empty (Owned by <%s>!)")
  146. :format(dname))
  147. end
  148. -- Player has taken something out of the box or placed something inside
  149. -- that amounts to count microblocks:
  150. function circular_saw:update_inventory(pos, amount)
  151. local meta = minetest.get_meta(pos)
  152. local inv = meta:get_inventory()
  153. amount = meta:get_int("anz") + amount
  154. -- The material is recycled automaticly.
  155. inv:set_list("recycle", {})
  156. if amount < 1 then -- If the last block is taken out.
  157. self:reset(pos)
  158. return
  159. end
  160. local stack = inv:get_stack("input", 1)
  161. -- At least one "normal" block is necessary to see what kind of stairs are requested.
  162. if stack:is_empty() then
  163. -- Any microblocks not taken out yet are now lost.
  164. -- (covers material loss in the machine)
  165. self:reset(pos)
  166. return
  167. end
  168. local node_name = stack:get_name() or ""
  169. local name_parts = circular_saw.known_nodes[node_name] or ""
  170. local modname = name_parts[1] or ""
  171. local material = name_parts[2] or ""
  172. inv:set_list("input", { -- Display as many full blocks as possible:
  173. node_name.. " " .. math.floor(amount / 8)
  174. })
  175. -- The stairnodes made of default nodes use moreblocks namespace, other mods keep own:
  176. if modname == "default" then
  177. modname = "circular_saw"
  178. end
  179. -- print("circular_saw set to " .. modname .. " : "
  180. -- .. material .. " with " .. (amount) .. " microblocks.")
  181. -- 0-7 microblocks may remain left-over:
  182. inv:set_list("micro", {
  183. modname .. ":micro_" .. material .. "_bottom " .. (amount % 8)
  184. })
  185. -- Display:
  186. inv:set_list("output",
  187. self:get_output_inv(modname, material, amount,
  188. meta:get_int("max_offered")))
  189. -- Store how many microblocks are available:
  190. meta:set_int("anz", amount)
  191. local material_desc = node_name
  192. local def = minetest.registered_items[node_name]
  193. if def and def.description then
  194. material_desc = utility.get_short_desc(def.description)
  195. end
  196. local owner = meta:get_string("owner")
  197. local dname = rename.gpn(owner)
  198. meta:set_string("infotext",
  199. S("Circular Saw is Working on \"%s\" (Owned by <%s>!)")
  200. :format(material_desc, dname))
  201. end
  202. -- The amount of items offered per shape can be configured:
  203. function circular_saw.on_receive_fields(pos, formname, fields, sender)
  204. local meta = minetest.get_meta(pos)
  205. local max = tonumber(fields.max_offered)
  206. if max and max > 0 then
  207. meta:set_string("max_offered", max)
  208. -- Update to show the correct number of items:
  209. circular_saw:update_inventory(pos, 0)
  210. end
  211. end
  212. local function has_saw_privilege(meta, player)
  213. if not meta then return false end
  214. if not player then return false end
  215. if minetest.check_player_privs(player, "protection_bypass") then
  216. return true
  217. end
  218. local owner = (meta:get_string("owner") or "")
  219. if player:get_player_name() == owner then
  220. return true
  221. end
  222. return false
  223. end
  224. -- Moving the inventory of the circular_saw around is not allowed because it
  225. -- is a fictional inventory. Moving inventory around would be rather
  226. -- impractical and make things more difficult to calculate:
  227. function circular_saw.allow_metadata_inventory_move(
  228. pos, from_list, from_index, to_list, to_index, count, player)
  229. return 0
  230. end
  231. -- Only input- and recycle-slot are intended as input slots:
  232. function circular_saw.allow_metadata_inventory_put(pos, listname, index, stack, player)
  233. local meta = minetest.get_meta(pos)
  234. if not has_saw_privilege(meta, player) then return 0 end
  235. if listname == "fuel" then
  236. if stack:get_name() == "default:mese_crystal_fragment" then
  237. return stack:get_count()
  238. else
  239. return 0
  240. end
  241. end
  242. -- The player is not allowed to put something in there:
  243. if listname == "output" or listname == "micro" then
  244. return 0
  245. end
  246. local inv = meta:get_inventory()
  247. local stackname = stack:get_name()
  248. local count = stack:get_count()
  249. -- Only alow those items that are offered in the output inventory to be recycled:
  250. if listname == "recycle" then
  251. if not inv:contains_item("output", stackname) then
  252. return 0
  253. end
  254. local stackmax = stack:get_stack_max()
  255. local instack = inv:get_stack("input", 1)
  256. local microstack = inv:get_stack("micro", 1)
  257. local incount = instack:get_count()
  258. local incost = (incount * 8) + microstack:get_count()
  259. local maxcost = (stackmax * 8) + 7
  260. local cost = circular_saw:get_cost(inv, stackname)
  261. if (incost + cost) > maxcost then
  262. return math.max((maxcost - incost) / cost, 0)
  263. end
  264. return count
  265. end
  266. -- Only accept certain blocks as input which are known to be craftable into stairs:
  267. if listname == "input" then
  268. if not inv:is_empty("input") then
  269. if inv:get_stack("input", index):get_name() ~= stackname then
  270. return 0
  271. end
  272. end
  273. if not inv:is_empty("micro") then
  274. local microstackname = inv:get_stack("micro", 1):get_name():gsub("^.+:micro_", "", 1)
  275. local cutstackname = stackname:gsub("^.+:", "", 1)
  276. if microstackname ~= cutstackname then
  277. return 0
  278. end
  279. end
  280. for name, t in pairs(circular_saw.known_nodes) do
  281. if name == stackname and inv:room_for_item("input", stack) then
  282. return count
  283. end
  284. end
  285. return 0
  286. end
  287. end
  288. -- Taking is allowed from all slots (even the internal microblock slot).
  289. -- Putting something in is slightly more complicated than taking anything
  290. -- because we have to make sure it is of a suitable material:
  291. function circular_saw.on_metadata_inventory_put(
  292. pos, listname, index, stack, player)
  293. -- We need to find out if the circular_saw is already set to a
  294. -- specific material or not:
  295. local meta = minetest.get_meta(pos)
  296. local inv = meta:get_inventory()
  297. local stackname = stack:get_name()
  298. local count = stack:get_count()
  299. -- Putting something into the input slot is only possible if that had
  300. -- been empty before or did contain something of the same material:
  301. if listname == "input" then
  302. -- Each new block is worth 8 microblocks:
  303. circular_saw:update_inventory(pos, 8 * count)
  304. elseif listname == "recycle" then
  305. -- Lets look which shape this represents:
  306. local cost = circular_saw:get_cost(inv, stackname)
  307. local input_stack = inv:get_stack("input", 1)
  308. -- check if this would not exceed input itemstack max_stacks
  309. if input_stack:get_count() + ((cost * count) / 8) <= input_stack:get_stack_max() then
  310. circular_saw:update_inventory(pos, cost * count)
  311. end
  312. end
  313. end
  314. local mese_to_cut_ratio = 32
  315. function circular_saw.allow_metadata_inventory_take(pos, listname, index, stack, player)
  316. local meta = minetest.get_meta(pos)
  317. if not has_saw_privilege(meta, player) then return 0 end
  318. if listname == "output" then
  319. local inv = meta:get_inventory()
  320. if inv:is_empty("fuel") then
  321. minetest.chat_send_player(player:get_player_name(), "# Server: No power to saw!")
  322. easyvend.sound_error(player:get_player_name())
  323. return 0
  324. else
  325. -- We do know how much each block at each position costs:
  326. local cost = circular_saw.names[index][3] * stack:get_count()
  327. local fuel = math.ceil(cost / mese_to_cut_ratio)
  328. if fuel < 1 then fuel = 1 end
  329. local fstack = inv:get_stack("fuel", 1)
  330. if fstack:get_count() < fuel then
  331. minetest.chat_send_player(player:get_player_name(), "# Server: Not enough energy!")
  332. easyvend.sound_error(player:get_player_name())
  333. return 0
  334. end
  335. end
  336. end
  337. return stack:get_count()
  338. end
  339. function circular_saw.on_metadata_inventory_take(
  340. pos, listname, index, stack, player)
  341. -- Prevent (inbuilt) swapping between inventories with different blocks
  342. -- corrupting player inventory or Saw with 'unknown' items.
  343. local meta = minetest.get_meta(pos)
  344. local inv = meta:get_inventory()
  345. local input_stack = inv:get_stack(listname, index)
  346. if not input_stack:is_empty() and input_stack:get_name()~=stack:get_name() then
  347. local player_inv = player:get_inventory()
  348. if player_inv:room_for_item("main", input_stack) then
  349. player_inv:add_item("main", input_stack)
  350. end
  351. circular_saw:reset(pos)
  352. return
  353. end
  354. -- If it is one of the offered stairs: find out how many
  355. -- microblocks have to be substracted:
  356. if listname == "output" then
  357. -- We do know how much each block at each position costs:
  358. local cost = circular_saw.names[index][3] * stack:get_count()
  359. local fuel = math.ceil(cost / mese_to_cut_ratio)
  360. if fuel < 1 then fuel = 1 end
  361. inv:remove_item("fuel", ItemStack("default:mese_crystal_fragment " .. fuel))
  362. circular_saw:update_inventory(pos, -cost)
  363. elseif listname == "micro" then
  364. -- Each microblock costs 1 microblock:
  365. circular_saw:update_inventory(pos, -stack:get_count())
  366. elseif listname == "input" then
  367. -- Each normal (= full) block taken costs 8 microblocks:
  368. circular_saw:update_inventory(pos, 8 * -stack:get_count())
  369. end
  370. -- The recycle field plays no role here since it is processed immediately.
  371. end
  372. function circular_saw.on_construct(pos)
  373. local meta = minetest.get_meta(pos)
  374. local fancy_inv = default.gui_bg..default.gui_bg_img..default.gui_slots
  375. -- Modify formspec size and inventory size in order to make room for more blocks.
  376. meta:set_string("formspec", "size[16,10]"..fancy_inv..
  377. "label[0,0;" ..S("Input\nMaterial").. "]" ..
  378. "list[context;input;1.5,0;1,1;]" ..
  379. "label[0,1;" ..S("Left-Over").. "]" ..
  380. "list[context;micro;1.5,1;1,1;]" ..
  381. "label[0,2;" ..S("Recycle\nOutput").. "]" ..
  382. "list[context;recycle;1.5,2;1,1;]" ..
  383. "field[0.3,4.0;1,1;max_offered;" ..S("Max").. ":;${max_offered}]" ..
  384. "button[1,3.7;1,1;Set;" ..S("Set").. "]" ..
  385. "list[context;output;2.8,0;13,6;]" ..
  386. "list[context;output;8.8,6;7,4;78]" ..
  387. "list[current_player;main;0.5,6.25;8,4;]" ..
  388. "label[0,5;Mese Fuel\nStorage]" ..
  389. "list[context;fuel;1.5,5;1,1;]"
  390. )
  391. meta:set_int("anz", 0) -- No microblocks inside yet.
  392. meta:set_string("max_offered", 64) -- How many items of this kind are offered by default?
  393. meta:set_string("infotext", S("Circular Saw is Empty"))
  394. local inv = meta:get_inventory()
  395. inv:set_size("input", 1) -- Input slot for full blocks of material x.
  396. inv:set_size("micro", 1) -- Storage for 1-7 surplus microblocks.
  397. inv:set_size("recycle", 1) -- Surplus partial blocks can be placed here.
  398. inv:set_size("output", 6*13+7*4) -- Many versions of stair-parts of material x.
  399. inv:set_size("fuel", 1)
  400. circular_saw:reset(pos)
  401. end
  402. function circular_saw.can_dig(pos,player)
  403. local meta = minetest.get_meta(pos)
  404. local inv = meta:get_inventory()
  405. if not inv:is_empty("input") or
  406. not inv:is_empty("micro") or
  407. not inv:is_empty("recycle") or
  408. not inv:is_empty("fuel") then
  409. return false
  410. end
  411. -- Can be dug by anyone when empty, not only by the owner:
  412. return true
  413. end
  414. minetest.register_node("circular_saw:circular_saw", {
  415. description = "Circular Table Saw\n\nRequires mese fragments to power the saw wheel.\nDo not allow children to use.",
  416. drawtype = "nodebox",
  417. node_box = {
  418. type = "fixed",
  419. fixed = {
  420. {-0.4, -0.5, -0.4, -0.25, 0.25, -0.25}, -- Leg
  421. {0.25, -0.5, 0.25, 0.4, 0.25, 0.4}, -- Leg
  422. {-0.4, -0.5, 0.25, -0.25, 0.25, 0.4}, -- Leg
  423. {0.25, -0.5, -0.4, 0.4, 0.25, -0.25}, -- Leg
  424. {-0.5, 0.25, -0.5, 0.5, 0.375, 0.5}, -- Tabletop
  425. {-0.01, 0.4375, -0.125, 0.01, 0.5, 0.125}, -- Saw blade (top)
  426. {-0.01, 0.375, -0.1875, 0.01, 0.4375, 0.1875}, -- Saw blade (bottom)
  427. {-0.25, -0.0625, -0.25, 0.25, 0.25, 0.25}, -- Motor case
  428. },
  429. },
  430. dumpnodes_tile = {"default_wood.png"},
  431. tiles = {
  432. "moreblocks_circular_saw_top.png",
  433. "moreblocks_circular_saw_bottom.png",
  434. "moreblocks_circular_saw_side.png"
  435. },
  436. paramtype = "light",
  437. sunlight_propagates = true,
  438. paramtype2 = "facedir",
  439. on_rotate = function(...)
  440. return screwdriver.rotate_simple(...)
  441. end,
  442. groups = utility.dig_groups("furniture", {
  443. immovable = 1,
  444. }),
  445. sounds = default.node_sound_wood_defaults(),
  446. on_construct = circular_saw.on_construct,
  447. can_dig = circular_saw.can_dig,
  448. -- Set the owner of this circular saw.
  449. after_place_node = function(pos, placer)
  450. local meta = minetest.get_meta(pos)
  451. local owner = placer and placer:get_player_name() or ""
  452. local dname = rename.gpn(owner)
  453. meta:set_string("owner", owner)
  454. meta:set_string("rename", dname)
  455. meta:set_string("infotext",
  456. S("Circular Saw is Empty (Owned by <%s>!)")
  457. :format(dname))
  458. end,
  459. -- The amount of items offered per shape can be configured:
  460. on_receive_fields = circular_saw.on_receive_fields,
  461. allow_metadata_inventory_move = circular_saw.allow_metadata_inventory_move,
  462. -- Only input- and recycle-slot are intended as input slots:
  463. allow_metadata_inventory_put = circular_saw.allow_metadata_inventory_put,
  464. allow_metadata_inventory_take = circular_saw.allow_metadata_inventory_take,
  465. -- Taking is allowed from all slots (even the internal microblock slot). Moving is forbidden.
  466. -- Putting something in is slightly more complicated than taking anything because we have to make sure it is of a suitable material:
  467. on_metadata_inventory_put = circular_saw.on_metadata_inventory_put,
  468. on_metadata_inventory_take = circular_saw.on_metadata_inventory_take,
  469. -- Called by rename LBM.
  470. _on_rename_check = function(pos)
  471. local meta = minetest.get_meta(pos)
  472. local owner = meta:get_string("owner")
  473. -- Nobody placed this block.
  474. if owner == "" then
  475. return
  476. end
  477. local dname = rename.gpn(owner)
  478. meta:set_string("rename", dname)
  479. -- Update circular saw.
  480. circular_saw:update_inventory(pos, 0)
  481. end,
  482. })
  483. minetest.register_craft({
  484. output = "circular_saw:circular_saw",
  485. recipe = {
  486. {'', 'gem_cutter:blade', ''},
  487. {'group:wood', 'group:wood', 'group:wood'},
  488. {'cast_iron:ingot', 'techcrafts:electric_motor', 'cast_iron:ingot'},
  489. }
  490. })