station_stain.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. local function stain_formspec(pos)
  2. local meta = minetest.get_meta(pos)
  3. local chitin_level = tonumber(meta:get_string('chitin_level'))
  4. local grounds_level = tonumber(meta:get_string('grounds_level'))
  5. local water_level = tonumber(meta:get_string('water_level'))
  6. formspec =
  7. 'size[8,8.5]'..
  8. 'list[current_name;chitin;.5,0;1,1;]'..
  9. 'label[1.5,0.25;Input chitin]'..
  10. 'list[current_name;grounds;.5,1;1,1;]'..
  11. 'label[1.5,1.25;Input coffee grounds]'..
  12. 'list[current_name;water;.5,2;1,1;]'..
  13. 'label[1.5,2.25;Input water]'..
  14. 'list[current_name;brush;.5,3;1,1;]'..
  15. 'label[1.5,3.25;Input brush]'..
  16. 'list[current_name;output;4.5,3;1,1;]'..
  17. 'image[3,.25;3,.5;station_stain_bg.png^[lowpart:'..(chitin_level*5)..':station_stain_fg.png^[transformR270]'..
  18. 'image[3,1.25;3,.5;station_stain_bg.png^[lowpart:'..(grounds_level*5)..':station_stain_fg.png^[transformR270]'..
  19. 'image[3,2.25;3,.5;station_stain_bg.png^[lowpart:'..(water_level*5)..':station_stain_fg.png^[transformR270]'..
  20. 'item_image_button[6,0;1,1;furniture:stain_brush1;brush1; ]'..
  21. 'item_image_button[6,1;1,1;furniture:stain_brush2;brush2; ]'..
  22. 'item_image_button[6,2;1,1;furniture:stain_brush3;brush3; ]'..
  23. 'item_image_button[7,0;1,1;furniture:stain_brush4;brush4; ]'..
  24. 'item_image_button[7,1;1,1;furniture:stain_brush5;brush5; ]'..
  25. 'item_image_button[7,2;1,1;furniture:stain_brush6;brush6; ]'..
  26. 'item_image_button[7,3;1,1;furniture:stain_brush7;brush7; ]'..
  27. 'list[current_player;main;0,4.5;8,4;]'..
  28. 'listring[context;output]'..
  29. 'listring[current_player;main]'
  30. return formspec
  31. end
  32. minetest.register_node('stations:stain', {
  33. description = 'Stain Station',
  34. drawtype = 'mesh',
  35. mesh = 'stations_stain.obj',
  36. tiles = {'stations_stain.png'},
  37. sounds = default.node_sound_wood_defaults(),
  38. paramtype2 = 'facedir',
  39. paramtype = 'light',
  40. selection_box = {
  41. type = 'fixed',
  42. fixed = {-.5, -.5, -.5, .5, .5, .5}},
  43. collision_box = {
  44. type = 'fixed',
  45. fixed = {-.5, -.5, -.5, .5, .5, .5}},
  46. groups = {oddly_breakable_by_hand = 1, choppy=3},
  47. on_construct = function(pos)
  48. local meta = minetest.get_meta(pos)
  49. local inv = meta:get_inventory()
  50. inv:set_size('main', 8*4)
  51. inv:set_size('chitin', 1)
  52. inv:set_size('grounds', 1)
  53. inv:set_size('water', 1)
  54. inv:set_size('brush', 1)
  55. inv:set_size('output', 1)
  56. meta:set_string('chitin_level', 0)
  57. meta:set_string('grounds_level', 0)
  58. meta:set_string('water_level', 0)
  59. meta:set_string('infotext', 'Stain Station')
  60. meta:set_string('formspec', stain_formspec(pos))
  61. end,
  62. can_dig = function(pos,player)
  63. local meta = minetest.get_meta(pos);
  64. local inv = meta:get_inventory()
  65. if inv:is_empty('chitin') and inv:is_empty('grounds') and inv:is_empty('water')
  66. and inv:is_empty('brush') and inv:is_empty('output') then
  67. return true
  68. else
  69. return false
  70. end
  71. end,
  72. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  73. local input = stack:get_name()
  74. if listname == 'chitin' then
  75. if input == 'stations:chitin' then
  76. return 1
  77. else
  78. return 0
  79. end
  80. elseif listname == 'grounds' then
  81. if input == 'stations:coffee_grounds' then
  82. return 1
  83. else
  84. return 0
  85. end
  86. elseif listname == 'water' then
  87. if input == 'bucket:bucket_river_water' or input == 'bucket:bucket_water'
  88. or input == 'earthbuild:clay_pot_river_water' or input == 'earthbuild:clay_pot_water' then
  89. return 1
  90. else
  91. return 0
  92. end
  93. elseif listname == 'brush' then
  94. if minetest.get_item_group(input, 'stain_brush') > 0 then
  95. return 99
  96. else
  97. return 0
  98. end
  99. elseif listname == 'output' then
  100. return 0
  101. end
  102. end,
  103. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  104. local meta = minetest.get_meta(pos)
  105. local input = stack:get_name()
  106. local inv = meta:get_inventory()
  107. local chitin_level = tonumber(meta:get_string('chitin_level'))
  108. local grounds_level = tonumber(meta:get_string('grounds_level'))
  109. local water_level = tonumber(meta:get_string('water_level'))
  110. if listname == 'grounds' then
  111. if grounds_level + 2 <= 20 then
  112. meta:set_string('grounds_level', grounds_level+2)
  113. inv:set_stack('grounds', 1, '')
  114. end
  115. elseif listname == 'water' then
  116. if water_level + 10 <= 20 then
  117. meta:set_string('water_level', water_level+10)
  118. local vessel = string.sub(input, 1,3)
  119. if vessel == 'ear' then
  120. inv:set_stack('water', 1, 'earthbuild:clay_pot')
  121. elseif vessel == 'buc' then
  122. inv:set_stack('water', 1, 'bucket:bucket_empty')
  123. end
  124. end
  125. elseif listname == 'chitin' then
  126. if chitin_level + 2 <= 20 then
  127. meta:set_string('chitin_level', chitin_level+2)
  128. inv:set_stack('chitin', 1, '')
  129. end
  130. end
  131. meta:set_string('formspec', stain_formspec(pos))
  132. end,
  133. on_receive_fields = function(pos, formname, fields, sender)
  134. local meta = minetest.get_meta(pos)
  135. local inv = meta:get_inventory(pos)
  136. local chitin_level = tonumber(meta:get_string('chitin_level'))
  137. local grounds_level = tonumber(meta:get_string('grounds_level'))
  138. local water_level = tonumber(meta:get_string('water_level'))
  139. local brush = inv:get_stack('brush', 1)
  140. local brush_count = brush:get_count()
  141. local make_okay = false
  142. if chitin_level >= 1 and water_level >= 1 and brush_count >= 1 then
  143. if fields['brush1'] then
  144. if grounds_level >= 1 then
  145. inv:set_stack('output', 1, 'furniture:stain_brush1')
  146. meta:set_string('grounds_level', grounds_level-1)
  147. make_okay = true
  148. end
  149. elseif fields['brush2'] then
  150. if grounds_level >= 2 then
  151. inv:set_stack('output', 1, 'furniture:stain_brush2')
  152. meta:set_string('grounds_level', grounds_level-2)
  153. make_okay = true
  154. end
  155. elseif fields['brush3'] then
  156. if grounds_level >= 3 then
  157. inv:set_stack('output', 1, 'furniture:stain_brush3')
  158. meta:set_string('grounds_level', grounds_level-3)
  159. make_okay = true
  160. end
  161. elseif fields['brush4'] then
  162. if grounds_level >= 4 then
  163. inv:set_stack('output', 1, 'furniture:stain_brush4')
  164. meta:set_string('grounds_level', grounds_level-4)
  165. make_okay = true
  166. end
  167. elseif fields['brush5'] then
  168. if grounds_level >= 5 then
  169. inv:set_stack('output', 1, 'furniture:stain_brush5')
  170. meta:set_string('grounds_level', grounds_level-5)
  171. make_okay = true
  172. end
  173. elseif fields['brush6'] then
  174. if grounds_level >= 6 then
  175. inv:set_stack('output', 1, 'furniture:stain_brush6')
  176. meta:set_string('grounds_level', grounds_level-6)
  177. make_okay = true
  178. end
  179. elseif fields['brush7'] then
  180. if grounds_level >= 7 then
  181. inv:set_stack('output', 1, 'furniture:stain_brush7')
  182. meta:set_string('grounds_level', grounds_level-7)
  183. make_okay = true
  184. end
  185. end
  186. if make_okay == true then
  187. meta:set_string('chitin_level', chitin_level-1)
  188. meta:set_string('water_level', water_level-1)
  189. brush:take_item()
  190. inv:set_stack('brush',1,brush)
  191. meta:set_string('formspec', stain_formspec(pos))
  192. end
  193. end
  194. end,
  195. })
  196. minetest.register_node('stations:stain_locked', {
  197. description = 'Stain Station (locked)',
  198. drawtype = 'mesh',
  199. mesh = 'stations_stain.obj',
  200. tiles = {'stations_stain.png'},
  201. sounds = default.node_sound_wood_defaults(),
  202. paramtype2 = 'facedir',
  203. paramtype = 'light',
  204. selection_box = {
  205. type = 'fixed',
  206. fixed = {-.5, -.5, -.5, .5, .5, .5}},
  207. collision_box = {
  208. type = 'fixed',
  209. fixed = {-.5, -.5, -.5, .5, .5, .5}},
  210. groups = {oddly_breakable_by_hand = 1, choppy=3},
  211. on_construct = function(pos)
  212. local meta = minetest.get_meta(pos)
  213. local inv = meta:get_inventory()
  214. inv:set_size('main', 8*4)
  215. inv:set_size('chitin', 1)
  216. inv:set_size('grounds', 1)
  217. inv:set_size('water', 1)
  218. inv:set_size('brush', 1)
  219. inv:set_size('output', 1)
  220. meta:set_string('chitin_level', 0)
  221. meta:set_string('grounds_level', 0)
  222. meta:set_string('water_level', 0)
  223. meta:set_string('infotext', 'Stain Station (locked)')
  224. meta:set_string('formspec', stain_formspec(pos))
  225. end,
  226. can_dig = function(pos,player)
  227. local meta = minetest.get_meta(pos);
  228. local inv = meta:get_inventory()
  229. if inv:is_empty('chitin') and inv:is_empty('grounds') and inv:is_empty('water')
  230. and inv:is_empty('brush') and inv:is_empty('output') then
  231. return true
  232. else
  233. return false
  234. end
  235. end,
  236. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  237. local player_name = player:get_player_name()
  238. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  239. return 0
  240. else
  241. local input = stack:get_name()
  242. if listname == 'chitin' then
  243. if input == 'stations:chitin' then
  244. return 1
  245. else
  246. return 0
  247. end
  248. elseif listname == 'grounds' then
  249. if input == 'stations:coffee_grounds' then
  250. return 1
  251. else
  252. return 0
  253. end
  254. elseif listname == 'water' then
  255. if input == 'bucket:bucket_river_water' or input == 'bucket:bucket_water'
  256. or input == 'earthbuild:clay_pot_river_water' or input == 'earthbuild:clay_pot_water' then
  257. return 1
  258. else
  259. return 0
  260. end
  261. elseif listname == 'brush' then
  262. if minetest.get_item_group(input, 'stain_brush') > 0 then
  263. return 99
  264. else
  265. return 0
  266. end
  267. elseif listname == 'output' then
  268. return 0
  269. end
  270. end
  271. end,
  272. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  273. local meta = minetest.get_meta(pos)
  274. local input = stack:get_name()
  275. local inv = meta:get_inventory()
  276. local chitin_level = tonumber(meta:get_string('chitin_level'))
  277. local grounds_level = tonumber(meta:get_string('grounds_level'))
  278. local water_level = tonumber(meta:get_string('water_level'))
  279. if listname == 'grounds' then
  280. if grounds_level + 2 <= 20 then
  281. meta:set_string('grounds_level', grounds_level+2)
  282. inv:set_stack('grounds', 1, '')
  283. end
  284. elseif listname == 'water' then
  285. if water_level + 10 <= 20 then
  286. meta:set_string('water_level', water_level+10)
  287. local vessel = string.sub(input, 1,3)
  288. if vessel == 'ear' then
  289. inv:set_stack('water', 1, 'earthbuild:clay_pot')
  290. elseif vessel == 'buc' then
  291. inv:set_stack('water', 1, 'bucket:bucket_empty')
  292. end
  293. end
  294. elseif listname == 'chitin' then
  295. if chitin_level + 2 <= 20 then
  296. meta:set_string('chitin_level', chitin_level+2)
  297. inv:set_stack('chitin', 1, '')
  298. end
  299. end
  300. meta:set_string('formspec', stain_formspec(pos))
  301. end,
  302. on_receive_fields = function(pos, formname, fields, sender)
  303. local player_name = sender:get_player_name()
  304. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  305. minetest.chat_send_player(sender, 'You don\'t have access to this station.')
  306. else
  307. local meta = minetest.get_meta(pos)
  308. local inv = meta:get_inventory(pos)
  309. local chitin_level = tonumber(meta:get_string('chitin_level'))
  310. local grounds_level = tonumber(meta:get_string('grounds_level'))
  311. local water_level = tonumber(meta:get_string('water_level'))
  312. local brush = inv:get_stack('brush', 1)
  313. local brush_count = brush:get_count()
  314. local make_okay = false
  315. if chitin_level >= 1 and water_level >= 1 and brush_count >= 1 then
  316. if fields['brush1'] then
  317. if grounds_level >= 1 then
  318. inv:set_stack('output', 1, 'furniture:stain_brush1')
  319. meta:set_string('grounds_level', grounds_level-1)
  320. make_okay = true
  321. end
  322. elseif fields['brush2'] then
  323. if grounds_level >= 2 then
  324. inv:set_stack('output', 1, 'furniture:stain_brush2')
  325. meta:set_string('grounds_level', grounds_level-2)
  326. make_okay = true
  327. end
  328. elseif fields['brush3'] then
  329. if grounds_level >= 3 then
  330. inv:set_stack('output', 1, 'furniture:stain_brush3')
  331. meta:set_string('grounds_level', grounds_level-3)
  332. make_okay = true
  333. end
  334. elseif fields['brush4'] then
  335. if grounds_level >= 4 then
  336. inv:set_stack('output', 1, 'furniture:stain_brush4')
  337. meta:set_string('grounds_level', grounds_level-4)
  338. make_okay = true
  339. end
  340. elseif fields['brush5'] then
  341. if grounds_level >= 5 then
  342. inv:set_stack('output', 1, 'furniture:stain_brush5')
  343. meta:set_string('grounds_level', grounds_level-5)
  344. make_okay = true
  345. end
  346. elseif fields['brush6'] then
  347. if grounds_level >= 6 then
  348. inv:set_stack('output', 1, 'furniture:stain_brush6')
  349. meta:set_string('grounds_level', grounds_level-6)
  350. make_okay = true
  351. end
  352. elseif fields['brush7'] then
  353. if grounds_level >= 7 then
  354. inv:set_stack('output', 1, 'furniture:stain_brush7')
  355. meta:set_string('grounds_level', grounds_level-7)
  356. make_okay = true
  357. end
  358. end
  359. if make_okay == true then
  360. meta:set_string('chitin_level', chitin_level-1)
  361. meta:set_string('water_level', water_level-1)
  362. brush:take_item()
  363. inv:set_stack('brush',1,brush)
  364. meta:set_string('formspec', stain_formspec(pos))
  365. end
  366. end
  367. end
  368. end,
  369. })