summaryrefslogtreecommitdiff
path: root/init.lua
blob: b5426abc486678512c85ec757f5fbe753bebf9f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
-- feedlot mod Copyright (C) 2017,2020 Gabriel PĂ©rez-Cerezo
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.

-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.



local modpath = minetest.get_modpath( "feedlot" )
dofile(modpath.."/fakeplayer.lua")

local update_interval = 5

local function get_formspec(pos)
   local spos = pos.x .. "," ..pos.y .. "," .. pos.z
   local xbg = default.gui_bg .. default.gui_bg_img .. default.gui_slots
--   local detached_inv = minetest.create_detached_inventory("mailbox_"..owner)
   return "size[8,5.5]" .. xbg .. default.get_hotbar_bg(0, 1.5) ..
      "label[0,0;Feedlot]" ..
      "list[current_name;main;3.5,0;1,1;]" ..
      "list[current_player;main;0,1.5;8,1;]" ..
      "list[current_player;main;0,2.75;8,3;8]" ..
		"listring[current_name;main]" ..
      "listring[current_player;main]"
end
local function get_milker_formspec(pos)
   return    	"size[8,8.5]"..
      default.gui_bg..
      default.gui_bg_img..
      default.gui_slots..
      "label[0,0;Milker]"..
      "label[0,1;Buckets:]"..
      "label[3.5,1;Milk:]"..
      "list[current_name;bucket;1.5,1;1,1;]"..
      "list[current_name;main;4.75,0.96;3,2;]"..
      "list[current_player;main;0,4.25;8,1;]"..
      "list[current_player;main;0,5.5;8,3;8]"..
      "listring[current_player;main]"..
		"listring[current_name;bucket]"..
		"listring[current_player;main]"..
      "listring[current_name;main]"..
      "listring[current_player;main]"..
      default.get_hotbar_bg(0, 4.25)
end


local function on_construct(pos)
   local node = minetest.get_node(pos)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   if node.name == "feedlot:milker" then
      meta:set_string("formspec", get_milker_formspec(pos))
      inv:set_size("bucket", 1)
      inv:set_size("main", 6)
   else
      meta:set_string("formspec", get_formspec(pos))
      inv:set_size("main", 1)
   end
end

local function can_dig(pos)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   return inv:is_empty("main")
end

local function start_timer(pos)
		local timer = minetest.get_node_timer(pos)
		timer:start(update_interval)
end

local tube = {
   insert_object = function(pos, node, stack, direction)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
		start_timer(pos)
      if node.name == "feedlot:milker" then
	 if stack:get_name() == "bucket:bucket_empty" and inv:room_for_item("bucket", stack) then
	    inv:add_item("bucket", stack)
	    return ItemStack(nil)
	 else
	    return stack
	 end
      end
	 
      if inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty" then
	 inv:add_item("main", stack)
	 return ItemStack(nil)
      else
	 return stack
      end
   end,
   can_insert = function(pos, node, stack, direction)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      if node.name == "feedlot:milker" then
	 if stack:get_name() == "bucket:bucket_empty" then
	    return inv:room_for_item("bucket", stack)
	 else
	    return false
	 end
      else
			return inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty"
      end
   end,
   input_inventory = "main",
   connect_sides = {left = 0, right = 0, front = 0, back = 0, top = 0, bottom = 1}}


local function is_mob(obj)
   return obj.get_luaentity and obj:get_luaentity().name and string.sub(obj:get_luaentity().name,1,string.len("mob"))=="mob"
end


local function feed_animals (pos, elapsed)
	local node = minetest.get_node(pos)
	local meta = minetest.get_meta(pos)
	local inv = meta:get_inventory()


	local fake_player = feedlotFakePlayer.create(pos, "fake_player")

	local radius = 3
	if node.name == "feedlot:milker" then
		radius = 5
		if inv:is_empty("bucket") then
			return false
		end
	else
		if inv:is_empty("main") then
			return false
		end		
	end

	local objs = minetest.get_objects_inside_radius(pos, radius)
	for _,obj in ipairs(objs) do
		ent = obj:get_luaentity()
		if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 and is_mob(obj) then
			if ent.on_rightclick then
				if not (ent.horny and node.name == "feedlot:feedlot") then
					if node.name == "feedlot:milker" and not inv:room_for_item("main", "mobs:bucket_milk") then
						return false
					end
					obj:get_luaentity():on_rightclick(fake_player)					
				end
			end
		end
	end
	return true
end


minetest.register_node("feedlot:feedlot", {
			  description = "Feedlot",
			  tiles = {
			     "default_wood.png^feedlot_feed.png",
			     "default_wood.png",
			     "default_wood.png",
			     "default_wood.png",
			     "default_wood.png",
			     "default_wood.png"
			  },
			  drawtype = "nodebox",
			  paramtype = "light",
			  node_box = {
			     type = "fixed",
			     fixed = {
				{-0.5, -0.5, -0.5, 0.5, -0.1875, -0.4375}, -- NodeBox6
				{0.4375, -0.5, -0.5, 0.5, -0.1875, 0.5}, -- NodeBox7
				{-0.5, -0.5, 0.4375, 0.5, -0.1875, 0.5}, -- NodeBox8
				{-0.5, -0.5, -0.5, -0.4375, -0.1875, 0.5}, -- NodeBox9
				{-0.4375, -0.5, -0.4375, 0.4375, -0.375, 0.4375}, -- NodeBox10
			     }
			  },
			  on_metadata_inventory_put = function(pos, listname, index, stack, player)
				  start_timer(pos)
			  end,
			  on_rightclick = function(pos, node, player, itemstack, pointed_thing)
				  start_timer(pos)
			  end,
			  allow_metadata_inventory_put = function (_, _, _, stack, _)
			     if stack:get_name() == "bucket:bucket_empty" then
				return 0
			     else
				return stack:get_count()
			     end
			  end,
			  on_construct = on_construct,
			  can_dig = can_dig,
			  groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1}, 
			  tube = tube,
			  on_timer = feed_animals
})
minetest.register_node("feedlot:milker", {
			  description = "Milker",
			  tiles = {
			     "feedlot_milker_top.png",
			     "feedlot_milker_top.png",
			     "feedlot_milker_side.png",
			     "feedlot_milker_side.png",
			     "feedlot_milker_side.png",
			     "feedlot_milker_side.png",
			  },
			  paramtype = "light",
			  allow_metadata_inventory_put = function (_, listname, _, stack, _)			     
			     if listname == "bucket" and stack:get_name() == "bucket:bucket_empty" then
				return stack:get_count()
			     else
				return 0
			     end
			  end,
			  allow_metadata_inventory_move = function (_, _, _, dst, _, count)
				return 0
			  end,
			  on_construct = on_construct,
			  can_dig = can_dig,
			  groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1}, 
			  tube = tube,
			  on_timer = feed_animals,
			  on_metadata_inventory_put = function(pos, listname, index, stack, player)
				  start_timer(pos)
			  end,
			  on_rightclick = function(pos, node, player, itemstack, pointed_thing)
				  start_timer(pos)
			  end,

})




	   
minetest.register_craft({
      	output = "feedlot:feedlot",
	recipe = {
	   {"","",""},
	   {"default:wood", "farming:seed_wheat", "default:wood"},
	   {"default:wood", "default:wood", "default:wood"}
}})
minetest.register_craft({
      	output = "feedlot:milker",
	recipe = {
	   {"default:steel_ingot","","default:steel_ingot"},
	   {"default:steel_ingot", "bucket:bucket_empty", "default:steel_ingot"},
	   {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
}})