init.lua 18 KB

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