nodes_straw.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. ---------------------------------------------------------------------------------------
  2. -- straw - a very basic material
  3. ---------------------------------------------------------------------------------------
  4. -- * straw mat - for animals and very poor NPC; also basis for other straw things
  5. -- * straw bale - well, just a good source for building and decoration
  6. local S = cottages.S
  7. -- an even simpler from of bed - usually for animals
  8. -- it is a nodebox and not wallmounted because that makes it easier to replace beds with straw mats
  9. minetest.register_node("cottages:straw_mat", {
  10. description = S("layer of straw"),
  11. drawtype = 'nodebox',
  12. tiles = { cottages.straw_texture }, -- done by VanessaE
  13. wield_image = cottages.straw_texture,
  14. inventory_image = cottages.straw_texture,
  15. sunlight_propagates = true,
  16. paramtype = 'light',
  17. paramtype2 = "facedir",
  18. walkable = false,
  19. groups = { hay = 3, snappy = 2, oddly_breakable_by_hand = 2, flammable=3 },
  20. sounds = cottages.sounds.leaves,
  21. node_box = {
  22. type = "fixed",
  23. fixed = {
  24. {-0.48, -0.5,-0.48, 0.48, -0.45, 0.48},
  25. }
  26. },
  27. selection_box = {
  28. type = "fixed",
  29. fixed = {
  30. {-0.48, -0.5,-0.48, 0.48, -0.25, 0.48},
  31. }
  32. },
  33. is_ground_content = false,
  34. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  35. return cottages.sleep_in_bed( pos, node, clicker, itemstack, pointed_thing );
  36. end
  37. })
  38. -- straw bales are a must for farming environments; if you for some reason do not have the darkage mod installed, this here gets you a straw bale
  39. minetest.register_node("cottages:straw_bale", {
  40. drawtype = "nodebox",
  41. description = S("straw bale"),
  42. tiles = {"cottages_darkage_straw_bale.png"},
  43. paramtype = "light",
  44. groups = { hay = 3, snappy = 2, oddly_breakable_by_hand = 2, flammable=3 },
  45. sounds = cottages.sounds.leaves,
  46. -- the bale is slightly smaller than a full node
  47. node_box = {
  48. type = "fixed",
  49. fixed = {
  50. {-0.45, -0.5,-0.45, 0.45, 0.45, 0.45},
  51. }
  52. },
  53. selection_box = {
  54. type = "fixed",
  55. fixed = {
  56. {-0.45, -0.5,-0.45, 0.45, 0.45, 0.45},
  57. }
  58. },
  59. is_ground_content = false,
  60. })
  61. -- just straw
  62. if( not(minetest.registered_nodes["farming:straw"])) then
  63. minetest.register_node("cottages:straw", {
  64. drawtype = "normal",
  65. description = S("straw"),
  66. tiles = {cottages.straw_texture},
  67. groups = { hay = 3, snappy = 2, oddly_breakable_by_hand = 2, flammable=3 },
  68. sounds = cottages.sounds.leaves,
  69. -- the bale is slightly smaller than a full node
  70. is_ground_content = false,
  71. })
  72. else
  73. minetest.register_alias("cottages:straw", "farming:straw")
  74. end
  75. local cottages_formspec_treshing_floor =
  76. "size[8,8]"..
  77. "image[1.5,0;1,1;"..cottages.texture_stick.."]"..
  78. "image[0,1;1,1;farming_wheat.png]"..
  79. "button_exit[6.8,0.0;1.5,0.5;public;"..S("Public?").."]"..
  80. "list[current_name;harvest;1,1;2,1;]"..
  81. "list[current_name;straw;5,0;2,2;]"..
  82. "list[current_name;seeds;5,2;2,2;]"..
  83. "label[1,0.5;"..S("Harvested wheat:").."]"..
  84. "label[4,0.0;"..S("Straw:").."]"..
  85. "label[4,2.0;"..S("Seeds:").."]"..
  86. "label[0,-0.5;"..S("Threshing floor").."]"..
  87. "label[0,2.5;"..S("Punch threshing floor with a stick").."]"..
  88. "label[0,3.0;"..S("to get straw and seeds from wheat.").."]"..
  89. "list[current_player;main;0,4;8,4;]";
  90. minetest.register_node("cottages:threshing_floor", {
  91. drawtype = "nodebox",
  92. description = S("threshing floor"),
  93. -- TODO: stone also looks pretty well for this
  94. tiles = {"cottages_junglewood.png^farming_wheat.png","cottages_junglewood.png","cottages_junglewood.png^"..cottages.texture_stick},
  95. paramtype = "light",
  96. paramtype2 = "facedir",
  97. -- can be digged with axe and pick
  98. groups = {cracky=2, choppy=2},
  99. is_ground_content = false,
  100. node_box = {
  101. type = "fixed",
  102. fixed = {
  103. {-0.50, -0.5,-0.50, 0.50, -0.40, 0.50},
  104. {-0.50, -0.4,-0.50,-0.45, -0.20, 0.50},
  105. { 0.45, -0.4,-0.50, 0.50, -0.20, 0.50},
  106. {-0.45, -0.4,-0.50, 0.45, -0.20,-0.45},
  107. {-0.45, -0.4, 0.45, 0.45, -0.20, 0.50},
  108. }
  109. },
  110. selection_box = {
  111. type = "fixed",
  112. fixed = {
  113. {-0.50, -0.5,-0.50, 0.50, -0.20, 0.50},
  114. }
  115. },
  116. on_construct = function(pos)
  117. local meta = minetest.get_meta(pos);
  118. meta:set_string("infotext", S("Public threshing floor"));
  119. local inv = meta:get_inventory();
  120. inv:set_size("harvest", 2);
  121. inv:set_size("straw", 4);
  122. inv:set_size("seeds", 4);
  123. meta:set_string("formspec", cottages_formspec_treshing_floor );
  124. meta:set_string("public", "public")
  125. end,
  126. after_place_node = function(pos, placer)
  127. local meta = minetest.get_meta(pos);
  128. meta:set_string("owner", placer:get_player_name() or "");
  129. meta:set_string("infotext", S("Private threshing floor (owned by %s)"):format(meta:get_string("owner") or ""));
  130. meta:set_string("formspec",
  131. cottages_formspec_treshing_floor..
  132. "label[2.5,-0.5;"..S("Owner: %s"):format(meta:get_string("owner") or "").."]" );
  133. meta:set_string("public", "private")
  134. end,
  135. on_receive_fields = function(pos, formname, fields, sender)
  136. cottages.switch_public(pos, formname, fields, sender, 'threshing floor')
  137. end,
  138. can_dig = function(pos,player)
  139. local meta = minetest.get_meta(pos);
  140. local inv = meta:get_inventory();
  141. local owner = meta:get_string('owner');
  142. if( not( inv:is_empty("harvest"))
  143. or not( inv:is_empty("straw"))
  144. or not( inv:is_empty("seeds"))
  145. or not( player )
  146. or ( owner and owner ~= '' and player:get_player_name() ~= owner )) then
  147. return false;
  148. end
  149. return true;
  150. end,
  151. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  152. local meta = minetest.get_meta(pos)
  153. if( not( cottages.player_can_use( meta, player ))) then
  154. return 0
  155. end
  156. return count;
  157. end,
  158. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  159. local meta = minetest.get_meta(pos)
  160. -- only accept input the threshing floor can use/process
  161. if( listname=='straw'
  162. or listname=='seeds'
  163. or (listname=='harvest' and stack and stack:get_name() ~= 'farming:wheat' )) then
  164. return 0;
  165. end
  166. if( not( cottages.player_can_use( meta, player ))) then
  167. return 0
  168. end
  169. return stack:get_count()
  170. end,
  171. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  172. local meta = minetest.get_meta(pos)
  173. if( not( cottages.player_can_use( meta, player ))) then
  174. return 0
  175. end
  176. return stack:get_count()
  177. end,
  178. on_punch = function(pos, node, puncher)
  179. if( not( pos ) or not( node ) or not( puncher )) then
  180. return;
  181. end
  182. -- only punching with a normal stick is supposed to work
  183. local wielded = puncher:get_wielded_item();
  184. if( not( wielded )
  185. or not( wielded:get_name() )
  186. or not( minetest.registered_items[ wielded:get_name() ])
  187. or not( minetest.registered_items[ wielded:get_name() ].groups )
  188. or not( minetest.registered_items[ wielded:get_name() ].groups.stick )) then
  189. return;
  190. end
  191. local name = puncher:get_player_name();
  192. local meta = minetest.get_meta(pos);
  193. local inv = meta:get_inventory();
  194. local input = inv:get_list('harvest');
  195. -- we have two input slots
  196. local stack1 = inv:get_stack( 'harvest', 1);
  197. local stack2 = inv:get_stack( 'harvest', 2);
  198. if( ( stack1:is_empty() and stack2:is_empty())
  199. or( not( stack1:is_empty()) and stack1:get_name() ~= 'farming:wheat')
  200. or( not( stack2:is_empty()) and stack2:get_name() ~= 'farming:wheat')) then
  201. -- minetest.chat_send_player( name, 'One of the input slots contains something else than wheat, or there is no wheat at all.');
  202. -- update the formspec
  203. meta:set_string("formspec",
  204. cottages_formspec_treshing_floor..
  205. "label[2.5,-0.5;"..S("Owner: %s"):format(meta:get_string("owner") or "").."]" );
  206. return;
  207. end
  208. -- on average, process 25 wheat at each punch (10..40 are possible)
  209. local anz_wheat = 10 + math.random( 0, 30 );
  210. -- we already made sure there is only wheat inside
  211. local found_wheat = stack1:get_count() + stack2:get_count();
  212. -- do not process more wheat than present in the input slots
  213. if( found_wheat < anz_wheat ) then
  214. anz_wheat = found_wheat;
  215. end
  216. local overlay1 = "^farming_wheat.png";
  217. local overlay2 = "^"..cottages.straw_texture;
  218. local overlay3 = "^"..cottages.texture_wheat_seed;
  219. -- this can be enlarged by a multiplicator if desired
  220. local anz_straw = anz_wheat;
  221. local anz_seeds = anz_wheat;
  222. if( inv:room_for_item('straw','cottages:straw_mat '..tostring( anz_straw ))
  223. and inv:room_for_item('seeds',cottages.craftitem_seed_wheat..' '..tostring( anz_seeds ))) then
  224. -- the player gets two kind of output
  225. inv:add_item("straw",'cottages:straw_mat '..tostring( anz_straw ));
  226. inv:add_item("seeds",cottages.craftitem_seed_wheat..' '..tostring( anz_seeds ));
  227. -- consume the wheat
  228. inv:remove_item("harvest", 'farming:wheat '..tostring( anz_wheat ));
  229. local anz_left = found_wheat - anz_wheat;
  230. if( anz_left > 0 ) then
  231. -- minetest.chat_send_player( name, S('You have threshed %s wheat (%s are left).'):format(anz_wheat,anz_left));
  232. else
  233. -- minetest.chat_send_player( name, S('You have threshed the last %s wheat.'):format(anz_wheat));
  234. overlay1 = "";
  235. end
  236. end
  237. local hud0 = puncher:hud_add({
  238. hud_elem_type = "image",
  239. scale = {x = 38, y = 38},
  240. text = "cottages_junglewood.png^[colorize:#888888:128",
  241. position = {x = 0.5, y = 0.5},
  242. alignment = {x = 0, y = 0}
  243. });
  244. local hud1 = puncher:hud_add({
  245. hud_elem_type = "image",
  246. scale = {x = 15, y = 15},
  247. text = "cottages_junglewood.png"..overlay1,
  248. position = {x = 0.4, y = 0.5},
  249. alignment = {x = 0, y = 0}
  250. });
  251. local hud2 = puncher:hud_add({
  252. hud_elem_type = "image",
  253. scale = {x = 15, y = 15},
  254. text = "cottages_junglewood.png"..overlay2,
  255. position = {x = 0.6, y = 0.35},
  256. alignment = {x = 0, y = 0}
  257. });
  258. local hud3 = puncher:hud_add({
  259. hud_elem_type = "image",
  260. scale = {x = 15, y = 15},
  261. text = "cottages_junglewood.png"..overlay3,
  262. position = {x = 0.6, y = 0.65},
  263. alignment = {x = 0, y = 0}
  264. });
  265. local hud4 = puncher:hud_add({
  266. hud_elem_type = "text",
  267. text = tostring( found_wheat-anz_wheat ),
  268. number = 0x00CC00,
  269. alignment = {x = 0, y = 0},
  270. scale = {x = 100, y = 100}, -- bounding rectangle of the text
  271. position = {x = 0.4, y = 0.5},
  272. });
  273. if( not( anz_straw )) then
  274. anz_straw = "0";
  275. end
  276. if( not( anz_seed )) then
  277. anz_seed = "0";
  278. end
  279. local hud5 = puncher:hud_add({
  280. hud_elem_type = "text",
  281. text = '+ '..tostring( anz_straw )..' straw',
  282. number = 0x00CC00,
  283. alignment = {x = 0, y = 0},
  284. scale = {x = 100, y = 100}, -- bounding rectangle of the text
  285. position = {x = 0.6, y = 0.35},
  286. });
  287. local hud6 = puncher:hud_add({
  288. hud_elem_type = "text",
  289. text = '+ '..tostring( anz_seed )..' seeds',
  290. number = 0x00CC00,
  291. alignment = {x = 0, y = 0},
  292. scale = {x = 100, y = 100}, -- bounding rectangle of the text
  293. position = {x = 0.6, y = 0.65},
  294. });
  295. minetest.after(2, function()
  296. if( puncher ) then
  297. if(hud1) then puncher:hud_remove(hud1); end
  298. if(hud2) then puncher:hud_remove(hud2); end
  299. if(hud3) then puncher:hud_remove(hud3); end
  300. if(hud4) then puncher:hud_remove(hud4); end
  301. if(hud5) then puncher:hud_remove(hud5); end
  302. if(hud6) then puncher:hud_remove(hud6); end
  303. if(hud0) then puncher:hud_remove(hud0); end
  304. end
  305. end)
  306. end,
  307. })
  308. local cottages_handmill_formspec = "size[8,8]"..
  309. "image[0,1;1,1;"..cottages.texture_wheat_seed.."]"..
  310. "button_exit[6.0,0.0;1.5,0.5;public;"..S("Public?").."]"..
  311. "list[current_name;seeds;1,1;1,1;]"..
  312. "list[current_name;flour;5,1;2,2;]"..
  313. "label[0,0.5;"..S("Wheat seeds:").."]"..
  314. "label[4,0.5;"..S("Flour:").."]"..
  315. "label[0,-0.3;"..S("Mill").."]"..
  316. "label[0,2.5;"..S("Punch this hand-driven mill").."]"..
  317. "label[0,3.0;"..S("to convert wheat seeds into flour.").."]"..
  318. "list[current_player;main;0,4;8,4;]";
  319. minetest.register_node("cottages:handmill", {
  320. description = S("mill, powered by punching"),
  321. drawtype = "mesh",
  322. mesh = "cottages_handmill.obj",
  323. tiles = {"cottages_stone.png"},
  324. paramtype = "light",
  325. paramtype2 = "facedir",
  326. groups = {cracky=2},
  327. is_ground_content = false,
  328. selection_box = {
  329. type = "fixed",
  330. fixed = {
  331. {-0.50, -0.5,-0.50, 0.50, 0.25, 0.50},
  332. }
  333. },
  334. collision_box = {
  335. type = "fixed",
  336. fixed = {
  337. {-0.50, -0.5,-0.50, 0.50, 0.25, 0.50},
  338. }
  339. },
  340. on_construct = function(pos)
  341. local meta = minetest.get_meta(pos);
  342. meta:set_string("infotext", S("Public mill, powered by punching"));
  343. local inv = meta:get_inventory();
  344. inv:set_size("seeds", 1);
  345. inv:set_size("flour", 4);
  346. meta:set_string("formspec", cottages_handmill_formspec );
  347. meta:set_string("public", "public")
  348. end,
  349. after_place_node = function(pos, placer)
  350. local meta = minetest.get_meta(pos);
  351. meta:set_string("owner", placer:get_player_name() or "");
  352. meta:set_string("infotext", S("Private mill, powered by punching (owned by %s)"):format(meta:get_string("owner") or ""));
  353. meta:set_string("formspec",
  354. cottages_handmill_formspec..
  355. "label[2.5,-0.5;"..S("Owner: %s"):format(meta:get_string('owner') or "").."]" );
  356. meta:set_string("public", "private")
  357. end,
  358. on_receive_fields = function(pos, formname, fields, sender)
  359. cottages.switch_public(pos, formname, fields, sender, 'mill, powered by punching')
  360. end,
  361. can_dig = function(pos,player)
  362. local meta = minetest.get_meta(pos);
  363. local inv = meta:get_inventory();
  364. local owner = meta:get_string('owner');
  365. if( not( inv:is_empty("flour"))
  366. or not( inv:is_empty("seeds"))
  367. or not( player )
  368. or ( owner and owner ~= '' and player:get_player_name() ~= owner )) then
  369. return false;
  370. end
  371. return true;
  372. end,
  373. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  374. local meta = minetest.get_meta(pos)
  375. if( not( cottages.player_can_use( meta, player ))) then
  376. return 0
  377. end
  378. return count;
  379. end,
  380. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  381. local meta = minetest.get_meta(pos)
  382. -- only accept input the threshing floor can use/process
  383. if( listname=='flour'
  384. or (listname=='seeds' and stack and not( cottages.handmill_product[ stack:get_name()] ))) then
  385. return 0;
  386. end
  387. if( not( cottages.player_can_use( meta, player ))) then
  388. return 0
  389. end
  390. return stack:get_count()
  391. end,
  392. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  393. local meta = minetest.get_meta(pos)
  394. if( not( cottages.player_can_use( meta, player ))) then
  395. return 0
  396. end
  397. return stack:get_count()
  398. end,
  399. -- this code is very similar to the threshing floor; except that it has only one input- and output-slot
  400. -- and does not require the usage of a stick
  401. on_punch = function(pos, node, puncher)
  402. if( not( pos ) or not( node ) or not( puncher )) then
  403. return;
  404. end
  405. local name = puncher:get_player_name();
  406. local meta = minetest.get_meta(pos);
  407. local inv = meta:get_inventory();
  408. local input = inv:get_list('seeds');
  409. local stack1 = inv:get_stack( 'seeds', 1);
  410. if( ( stack1:is_empty())
  411. or( not( stack1:is_empty())
  412. and not( cottages.handmill_product[ stack1:get_name() ] ))) then
  413. if not( stack1:is_empty() ) then
  414. minetest.chat_send_player(name,"Nothing happens...")
  415. end
  416. -- update the formspec
  417. meta:set_string("formspec",
  418. cottages_handmill_formspec..
  419. "label[2.5,-0.5;"..S("Owner: %s"):format(meta:get_string('owner') or "").."]" );
  420. return;
  421. end
  422. -- turning the mill is a slow process; 1-21 flour are generated per turn
  423. local anz = 1 + math.random( cottages.handmill_min_per_turn, cottages.handmill_max_per_turn );
  424. -- we already made sure there is only wheat inside
  425. local found = stack1:get_count();
  426. -- do not process more wheat than present in the input slots
  427. if( found < anz ) then
  428. anz = found;
  429. end
  430. local product_stack = ItemStack( cottages.handmill_product[ stack1:get_name() ]);
  431. local anz_result = anz;
  432. -- items that produce more
  433. if( product_stack:get_count()> 1 ) then
  434. anz_result = anz * product_stack:get_count();
  435. end
  436. if( inv:room_for_item('flour', product_stack:get_name()..' '..tostring( anz_result ))) then
  437. inv:add_item( 'flour', product_stack:get_name()..' '..tostring( anz_result ));
  438. inv:remove_item( 'seeds', stack1:get_name()..' '..tostring( anz ));
  439. local anz_left = found - anz;
  440. if( anz_left > 0 ) then
  441. minetest.chat_send_player( name, S('You have ground a %s (%s are left).'):format(stack1:get_definition().description,(anz_left)));
  442. else
  443. minetest.chat_send_player( name, S('You have ground the last %s.'):format(stack1:get_definition().description));
  444. end
  445. -- if the version of MT is recent enough, rotate the mill a bit
  446. if( minetest.swap_node ) then
  447. node.param2 = node.param2 + 1;
  448. if( node.param2 > 3 ) then
  449. node.param2 = 0;
  450. end
  451. minetest.swap_node( pos, node );
  452. end
  453. end
  454. end,
  455. })
  456. ---------------------------------------------------------------------------------------
  457. -- crafting receipes
  458. ---------------------------------------------------------------------------------------
  459. -- this returns corn as well
  460. -- the replacements work only if the replaced slot gets empty...
  461. minetest.register_craft({
  462. output = "cottages:straw_mat 6",
  463. recipe = {
  464. {cottages.craftitem_stone,'',''},
  465. {"farming:wheat", "farming:wheat", "farming:wheat", },
  466. },
  467. replacements = {{ cottages.craftitem_stone, cottages.craftitem_seed_wheat.." 3" }},
  468. })
  469. -- this is a better way to get straw mats
  470. minetest.register_craft({
  471. output = "cottages:threshing_floor",
  472. recipe = {
  473. {cottages.craftitem_junglewood, cottages.craftitem_chest_locked, cottages.craftitem_junglewood, },
  474. {cottages.craftitem_junglewood, cottages.craftitem_stone, cottages.craftitem_junglewood, },
  475. },
  476. })
  477. -- and a way to turn wheat seeds into flour
  478. minetest.register_craft({
  479. output = "cottages:handmill",
  480. recipe = {
  481. {cottages.craftitem_stick, cottages.craftitem_stone, "", },
  482. {"", cottages.craftitem_steel, "", },
  483. {"", cottages.craftitem_stone, "", },
  484. },
  485. })
  486. minetest.register_craft({
  487. output = "cottages:straw_bale",
  488. recipe = {
  489. {"cottages:straw_mat"},
  490. {"cottages:straw_mat"},
  491. {"cottages:straw_mat"},
  492. },
  493. })
  494. minetest.register_craft({
  495. output = "cottages:straw",
  496. recipe = {
  497. {"cottages:straw_bale"},
  498. },
  499. })
  500. minetest.register_craft({
  501. output = "cottages:straw_bale",
  502. recipe = {
  503. {"cottages:straw"},
  504. },
  505. })
  506. minetest.register_craft({
  507. output = "cottages:straw_mat 3",
  508. recipe = {
  509. {"cottages:straw_bale"},
  510. },
  511. })