Switch to Nodetimers, Enable shift+click
[feedlot.git] / init.lua
1 -- feedlot mod Copyright (C) 2017 Gabriel PĂ©rez-Cerezo
2 -- This program is free software: you can redistribute it and/or modify
3 -- it under the terms of the GNU Affero General Public License as published by
4 -- the Free Software Foundation, either version 3 of the License, or
5 -- (at your option) any later version.
6
7 -- This program is distributed in the hope that it will be useful,
8 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
9 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 -- GNU Affero General Public License for more details.
11
12 -- You should have received a copy of the GNU Affero General Public License
13 -- along with this program. If not, see <https://www.gnu.org/licenses/>.
14
15
16
17 local modpath = minetest.get_modpath( "feedlot" )
18 dofile(modpath.."/fakeplayer.lua")
19
20 local function get_formspec(pos)
21 local spos = pos.x .. "," ..pos.y .. "," .. pos.z
22 local xbg = default.gui_bg .. default.gui_bg_img .. default.gui_slots
23 -- local detached_inv = minetest.create_detached_inventory("mailbox_"..owner)
24 return "size[8,5.5]" .. xbg .. default.get_hotbar_bg(0, 1.5) ..
25 "label[0,0;Feedlot]" ..
26 "list[current_name;main;3.5,0;1,1;]" ..
27 "list[current_player;main;0,1.5;8,1;]" ..
28 "list[current_player;main;0,2.75;8,3;8]" ..
29 -- "listring[detached:mailbox_" .. owner .. ";drop]" ..
30 "listring[current_player;main]"
31 end
32 local function get_milker_formspec(pos)
33 return "size[8,8.5]"..
34 default.gui_bg..
35 default.gui_bg_img..
36 default.gui_slots..
37 "label[0,0;Milker]"..
38 "label[0,1;Buckets:]"..
39 "label[3.5,1;Milk:]"..
40 "list[current_name;bucket;1.5,1;1,1;]"..
41 "list[current_name;main;4.75,0.96;3,2;]"..
42 "list[current_player;main;0,4.25;8,1;]"..
43 "list[current_player;main;0,5.5;8,3;8]"..
44 "listring[current_name;dst]"..
45 "listring[current_player;main]"..
46 "listring[current_name;src]"..
47 "listring[current_player;main]"..
48 default.get_hotbar_bg(0, 4.25)
49 end
50
51
52 local function on_construct(pos)
53 local node = minetest.get_node(pos)
54 local meta = minetest.get_meta(pos)
55 local inv = meta:get_inventory()
56 if node.name == "feedlot:milker" then
57 meta:set_string("formspec", get_milker_formspec(pos))
58 inv:set_size("bucket", 1)
59 inv:set_size("main", 6)
60 else
61 meta:set_string("formspec", get_formspec(pos))
62 inv:set_size("main", 1)
63 end
64 end
65
66 local function can_dig(pos)
67 local meta = minetest.get_meta(pos)
68 local inv = meta:get_inventory()
69 return inv:is_empty("main")
70 end
71 local tube = {
72 insert_object = function(pos, node, stack, direction)
73 local meta = minetest.get_meta(pos)
74 local inv = meta:get_inventory()
75 if node.name == "feedlot:milker" then
76 if stack:get_name() == "bucket:bucket_empty" and inv:room_for_item("bucket", stack) then
77 inv:add_item("bucket", stack)
78 return ItemStack(nil)
79 else
80 return stack
81 end
82 end
83
84 if inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty" then
85 inv:add_item("main", stack)
86 return ItemStack(nil)
87 else
88 return stack
89 end
90 end,
91 can_insert = function(pos, node, stack, direction)
92 local meta = minetest.get_meta(pos)
93 local inv = meta:get_inventory()
94 if node.name == "feedlot:milker" then
95 if stack:get_name() == "bucket:bucket_empty" then
96 return inv:room_for_item("bucket", stack)
97 else
98 return false
99 end
100 else
101 return inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty"
102 end
103 end,
104 input_inventory = "main",
105 connect_sides = {left = 0, right = 0, front = 0, back = 0, top = 0, bottom = 1}}
106
107 minetest.register_node("feedlot:feedlot", {
108 description = "Feedlot",
109 tiles = {
110 "default_wood.png^feedlot_feed.png",
111 "default_wood.png",
112 "default_wood.png",
113 "default_wood.png",
114 "default_wood.png",
115 "default_wood.png"
116 },
117 drawtype = "nodebox",
118 paramtype = "light",
119 node_box = {
120 type = "fixed",
121 fixed = {
122 {-0.5, -0.5, -0.5, 0.5, -0.1875, -0.4375}, -- NodeBox6
123 {0.4375, -0.5, -0.5, 0.5, -0.1875, 0.5}, -- NodeBox7
124 {-0.5, -0.5, 0.4375, 0.5, -0.1875, 0.5}, -- NodeBox8
125 {-0.5, -0.5, -0.5, -0.4375, -0.1875, 0.5}, -- NodeBox9
126 {-0.4375, -0.5, -0.4375, 0.4375, -0.375, 0.4375}, -- NodeBox10
127 }
128 },
129 allow_metadata_inventory_put = function (_, _, _, stack, _)
130 if stack:get_name() == "bucket:bucket_empty" then
131 return 0
132 else
133 return stack:get_count()
134 end
135 end,
136 on_construct = on_construct,
137 can_dig = can_dig,
138 groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1},
139 tube = tube
140 })
141 minetest.register_node("feedlot:milker", {
142 description = "Milker",
143 tiles = {
144 "feedlot_milker_top.png",
145 "feedlot_milker_top.png",
146 "feedlot_milker_side.png",
147 "feedlot_milker_side.png",
148 "feedlot_milker_side.png",
149 "feedlot_milker_side.png",
150 },
151 paramtype = "light",
152 allow_metadata_inventory_put = function (_, listname, _, stack, _)
153 if listname == "bucket" and stack:get_name() == "bucket:bucket_empty" then
154 return stack:get_count()
155 else
156 return 0
157 end
158 end,
159 allow_metadata_inventory_move = function (_, _, _, dst, _, count)
160 return 0
161 end,
162 on_construct = on_construct,
163 can_dig = can_dig,
164 groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1},
165 tube = tube
166 })
167
168
169
170 local function is_mob(obj)
171 return obj.get_luaentity and obj:get_luaentity().name and string.sub(obj:get_luaentity().name,1,string.len("mob"))=="mob"
172 end
173
174
175 minetest.register_abm({
176 label = "Feeding and milking",
177 nodenames = {"feedlot:feedlot", "feedlot:milker"},
178 interval = 5,
179 chance = 1,
180
181 action = function (pos, node)
182 local fake_player = feedlotFakePlayer.create(pos, "fake_player")
183 local radius = 3
184 if node.name == "feedlot:milker" then
185 radius = 5
186 end
187 local objs = minetest.get_objects_inside_radius(pos, radius)
188 for _,obj in ipairs(objs) do
189 ent = obj:get_luaentity()
190 if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 and is_mob(obj) then
191 if ent.on_rightclick then
192 if ent.horny and node.name == "feedlot:feedlot" then
193 return
194 end
195 obj:get_luaentity():on_rightclick(fake_player)
196 end
197 -- minetest.chat_send_all("animal fed")
198 end
199 end
200 end,
201 })
202
203 minetest.register_craft({
204 output = "feedlot:feedlot",
205 recipe = {
206 {"","",""},
207 {"default:wood", "farming:seed_wheat", "default:wood"},
208 {"default:wood", "default:wood", "default:wood"}
209 }})
210 minetest.register_craft({
211 output = "feedlot:milker",
212 recipe = {
213 {"default:steel_ingot","","default:steel_ingot"},
214 {"default:steel_ingot", "bucket:bucket_empty", "default:steel_ingot"},
215 {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
216 }})