Initial commit
[feedlot.git] / init.lua
1 -- The purpose of this class is to have something that can be passed into callbacks that
2 -- demand a "Player" object as a parameter and hopefully prevent the mods that have
3 -- registered with those callbacks from crashing on a nil dereference or bad function
4 -- call. This is not supposed to be a remotely functional thing, it's just supposed
5 -- to provide dummy methods and return values of the correct data type for anything that
6 -- might ignore the false "is_player()" return and go ahead and try to use this thing
7 -- anyway.
8
9 -- I'm trying to patch holes in bad mod programming, essentially. If a mod is so badly
10 -- programmed that it crashes anyway there's not a lot else I can do on my end of things.
11
12 DigtronFakePlayer = {}
13 DigtronFakePlayer.__index = DigtronFakePlayer
14
15 local function return_value(x)
16 return (function() return x end)
17 end
18
19 local function return_nil()
20 return nil
21 end
22
23 local function return_empty_string()
24 return ""
25 end
26
27 local function return_zero()
28 return 0
29 end
30
31 local function return_empty_table()
32 return {}
33 end
34
35 function DigtronFakePlayer.update(self, pos, player_name)
36 self.is_fake_player = ":digtron " .. player_name
37 self.get_pos = return_value(pos)
38 end
39 local function get_feed (pos)
40 return function ()
41 local meta = minetest.get_meta(pos)
42 local inv = meta:get_inventory()
43 if not inv or not inv:get_list("main") then
44 return ItemStack(nil)
45 end
46 -- minetest.chat_send_all(inv:get_stack("main", 1):get_name())
47 return inv:get_stack("main", 1)
48 end
49 end
50 local function set_feed (pos)
51 return function (_, stack)
52 local meta = minetest.get_meta(pos)
53 local inv = meta:get_inventory()
54 inv:set_stack("main", 1, stack)
55 return
56 end
57 end
58
59 function DigtronFakePlayer.create(pos, player_name)
60 local self = {}
61 setmetatable(self, DigtronFakePlayer)
62
63 self.is_fake_player = ":digtron " .. player_name
64
65 -- ObjectRef
66 self.get_pos = return_value(pos)
67 self.set_pos = return_nil
68 self.move_to = return_nil
69 self.punch = return_nil
70 self.right_click = return_nil
71 self.get_hp = return_value(10)
72 self.set_hp = return_nil
73 self.get_inventory = return_nil -- returns an `InvRef`
74 self.get_wield_list = return_empty_string
75 self.get_wield_index = return_value(1)
76 -- self.get_wielded_item = return_value(ItemStack(nil))
77 self.get_wielded_item = get_feed(pos)
78 self.set_wielded_item = set_feed(pos)
79
80 self.set_armor_groups = return_nil
81 self.get_armor_groups = return_empty_table
82 self.set_animation = return_nil
83 self.get_animation = return_nil -- a set of values, maybe important?
84 self.set_attach = return_nil
85 self.get_attach = return_nil
86 self.set_detach = return_nil
87 self.set_bone_position = return_nil
88 self.get_bone_position = return_nil
89 self.set_properties = return_nil
90 self.get_properties = return_empty_table
91
92 self.is_player = return_value(false)
93
94 self.get_nametag_attributes = return_empty_table
95 self.set_nametag_attributes = return_nil
96
97 --LuaEntitySAO
98 self.set_velocity = return_nil
99 self.get_velocity = return_value({x=0,y=0,z=0})
100 self.set_acceleration = return_nil
101 self.get_acceleration = return_value({x=0,y=0,z=0})
102 self.set_yaw = return_nil
103 self.get_yaw = return_zero
104 self.set_texture_mod = return_nil
105 self.get_texture_mod = return_nil -- maybe important?
106 self.set_sprite = return_nil
107 --self.get_entity_name` (**Deprecated**: Will be removed in a future version)
108 self.get_luaentity = return_nil
109
110 -- Player object
111
112 self.get_player_name = return_empty_string
113 self.get_player_velocity = return_nil
114 self.get_look_dir = return_value({x=0,y=1,z=0})
115 self.get_look_horizontal = return_zero
116 self.set_look_horizontal = return_nil
117 self.get_look_vertical = return_zero
118 self.set_look_vertical = return_nil
119
120 --self.get_look_pitch`: pitch in radians - Deprecated as broken. Use `get_look_vertical`
121 --self.get_look_yaw`: yaw in radians - Deprecated as broken. Use `get_look_horizontal`
122 --self.set_look_pitch(radians)`: sets look pitch - Deprecated. Use `set_look_vertical`.
123 --self.set_look_yaw(radians)`: sets look yaw - Deprecated. Use `set_look_horizontal`.
124 self.get_breath = return_value(10)
125 self.set_breath = return_nil
126 self.get_attribute = return_nil
127 self.set_attribute = return_nil
128
129 self.set_inventory_formspec = return_nil
130 self.get_inventory_formspec = return_empty_string
131 self.get_player_control = return_value({jump=false, right=false, left=false, LMB=false, RMB=false, sneak=false, aux1=false, down=false, up=false} )
132 self.get_player_control_bits = return_zero
133
134 self.set_physics_override = return_nil
135 self.get_physics_override = return_value({speed = 1, jump = 1, gravity = 1, sneak = true, sneak_glitch = false, new_move = true,})
136
137
138 self.hud_add = return_nil
139 self.hud_remove = return_nil
140 self.hud_change = return_nil
141 self.hud_get = return_nil -- possibly important return value?
142 self.hud_set_flags = return_nil
143 self.hud_get_flags = return_value({ hotbar=true, healthbar=true, crosshair=true, wielditem=true, breathbar=true, minimap=true })
144 self.hud_set_hotbar_itemcount = return_nil
145 self.hud_get_hotbar_itemcount = return_zero
146 self.hud_set_hotbar_image = return_nil
147 self.hud_get_hotbar_image = return_empty_string
148 self.hud_set_hotbar_selected_image = return_nil
149 self.hud_get_hotbar_selected_image = return_empty_string
150 self.set_sky = return_nil
151 self.get_sky = return_empty_table -- may need members on this table
152
153 self.set_clouds = return_nil
154 self.get_clouds = return_value({density = 0, color = "#fff0f0e5", ambient = "#000000", height = 120, thickness = 16, speed = {x=0, y=-2}})
155
156 self.override_day_night_ratio = return_nil
157 self.get_day_night_ratio = return_nil
158
159 self.set_local_animation = return_nil
160 self.get_local_animation = return_empty_table
161
162 self.set_eye_offset = return_nil
163 self.get_eye_offset = return_value({x=0,y=0,z=0},{x=0,y=0,z=0})
164
165 return self
166 end
167
168
169 local function get_formspec(pos)
170 local spos = pos.x .. "," ..pos.y .. "," .. pos.z
171 local xbg = default.gui_bg .. default.gui_bg_img .. default.gui_slots
172 -- local detached_inv = minetest.create_detached_inventory("mailbox_"..owner)
173 return "size[8,5.5]" .. xbg .. default.get_hotbar_bg(0, 1.5) ..
174 "label[0,0;Feedlot]" ..
175 "list[nodemeta:" .. spos .. ";main;3.5,0;1,1;]" ..
176 "list[current_player;main;0,1.5;8,1;]" ..
177 "list[current_player;main;0,2.75;8,3;8]" ..
178 -- "listring[detached:mailbox_" .. owner .. ";drop]" ..
179 "listring[current_player;main]"
180 end
181
182 minetest.register_node("feedlot:feedlot", {
183 description = "Feedlot",
184 tiles = {
185 "default_wood.png^feedlot_feed.png",
186 "default_wood.png",
187 "default_wood.png",
188 "default_wood.png",
189 "default_wood.png",
190 "default_wood.png"
191 },
192 drawtype = "nodebox",
193 paramtype = "light",
194 node_box = {
195 type = "fixed",
196 fixed = {
197 {-0.5, -0.5, -0.5, 0.5, -0.1875, -0.4375}, -- NodeBox6
198 {0.4375, -0.5, -0.5, 0.5, -0.1875, 0.5}, -- NodeBox7
199 {-0.5, -0.5, 0.4375, 0.5, -0.1875, 0.5}, -- NodeBox8
200 {-0.5, -0.5, -0.5, -0.4375, -0.1875, 0.5}, -- NodeBox9
201 {-0.4375, -0.5, -0.4375, 0.4375, -0.375, 0.4375}, -- NodeBox10
202 }
203 },
204
205 on_construct = function (pos)
206 local meta = minetest.get_meta(pos)
207 local inv = meta:get_inventory()
208 meta:set_string("formspec", get_formspec(pos))
209 inv:set_size("main", 1)
210 end,
211 can_dig = function (pos)
212 local meta = minetest.get_meta(pos)
213 local inv = meta:get_inventory()
214 return inv:is_empty("main")
215 end,
216 groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1},
217 tube = {
218 insert_object = function(pos, node, stack, direction)
219 local meta = minetest.get_meta(pos)
220 local inv = meta:get_inventory()
221 local added = inv:add_item("main", stack)
222 return added
223 end,
224 can_insert = function(pos, node, stack, direction)
225 local meta = minetest.get_meta(pos)
226 local inv = meta:get_inventory()
227 return inv:room_for_item("main", stack)
228 end,
229 input_inventory = "main",
230 connect_sides = {left = 0, right = 0, front = 0, back = 0, top = 0, bottom = 1}},
231 })
232
233 local function is_mob(obj)
234 return obj.get_luaentity and obj:get_luaentity().name and string.sub(obj:get_luaentity().name,1,string.len("mob"))=="mob"
235 end
236
237
238 minetest.register_abm({
239 label = "lava cooling",
240 nodenames = {"feedlot:feedlot"},
241 interval = 5,
242 chance = 1,
243
244 action = function (pos)
245 local fake_player = DigtronFakePlayer.create(pos, "fake_player")
246 local objs = minetest.get_objects_inside_radius(pos, 3)
247 for _,obj in ipairs(objs) do
248 if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 and is_mob(obj) then
249 -- obj:on_rightclick(fake_player)
250 obj:get_luaentity():on_rightclick(fake_player)
251 -- minetest.chat_send_all("animal fed")
252 end
253 end
254 end,
255 })
256
257 minetest.register_craft({
258 output = "feedlot:feedlot",
259 recipe = {
260 {"","",""},
261 {"default:wood", "farming:seed_wheat", "default:wood"},
262 {"default:wood", "default:wood", "default:wood"}
263 }})