Bugfix and code cleanup
[smartshop.git] / init.lua
1 smartshop={user={},tmp={},dir={{x=0,y=0,z=-1},{x=-1,y=0,z=0},{x=0,y=0,z=1},{x=1,y=0,z=0}},dpos={
2 {{x=0.2,y=0.2,z=0},{x=-0.2,y=0.2,z=0},{x=0.2,y=-0.2,z=0},{x=-0.2,y=-0.2,z=0}},
3 {{x=0,y=0.2,z=0.2},{x=0,y=0.2,z=-0.2},{x=0,y=-0.2,z=0.2},{x=0,y=-0.2,z=-0.2}},
4 {{x=-0.2,y=0.2,z=0},{x=0.2,y=0.2,z=0},{x=-0.2,y=-0.2,z=0},{x=0.2,y=-0.2,z=0}},
5 {{x=0,y=0.2,z=-0.2},{x=0,y=0.2,z=0.2},{x=0,y=-0.2,z=-0.2},{x=0,y=-0.2,z=0.2}}}
6 }
7
8 minetest.register_craft({
9 output = "smartshop:shop",
10 recipe = {
11 {"default:chest_locked", "default:chest_locked", "default:chest_locked"},
12 {"default:sign_wall_wood", "default:chest_locked", "default:sign_wall_wood"},
13 {"default:sign_wall_wood", "default:torch", "default:sign_wall_wood"},
14 }
15 })
16 smartshop.get_human_name = function(item)
17 if core.registered_items[item] then
18 return core.registered_items[item].description
19 else
20 return "Unknown Item"
21 end
22 end
23
24 smartshop.use_offer=function(pos,player,n)
25 local pressed={}
26 pressed["buy" .. n]=true
27 smartshop.user[player:get_player_name()]=pos
28 smartshop.receive_fields(player,pressed)
29 smartshop.user[player:get_player_name()]=nil
30 smartshop.update(pos)
31
32
33 end
34
35 smartshop.get_offer=function(pos)
36 if not pos or not minetest.get_node(pos) then return end
37 if minetest.get_node(pos).name~="smartshop:shop" then return end
38 local meta=minetest.get_meta(pos)
39 local inv=meta:get_inventory()
40 local offer={}
41 for i=1,4,1 do
42 offer[i]={
43 give=inv:get_stack("give" .. i,1):get_name(),
44 give_count=inv:get_stack("give" .. i,1):get_count(),
45 pay=inv:get_stack("pay" .. i,1):get_name(),
46 pay_count=inv:get_stack("pay" .. i,1):get_count(),
47 }
48 end
49 return offer
50 end
51
52 smartshop.send_mail=function(owner, pos, item)
53 if not minetest.get_modpath( "mail" ) then
54 return
55 end
56 local spos = "("..pos.x..", "..pos.y..", "..pos.z..")"
57 mail.send("DO NOT REPLY", owner, "Out of "..smartshop.get_human_name(item).." at "..spos, "Your smartshop at "..spos.." is out of "..smartshop.get_human_name(item)..". Please restock")
58 end
59
60
61 smartshop.receive_fields=function(player,pressed)
62 if pressed.customer then
63 return smartshop.showform(smartshop.user[player:get_player_name()],player,true)
64 elseif pressed.tooglelime then
65 local pos=smartshop.user[player:get_player_name()]
66 local meta=minetest.get_meta(pos)
67 local pname=player:get_player_name()
68 if meta:get_int("type")==0 then
69 meta:set_int("type",1)
70 minetest.chat_send_player(pname, "Your stock is limited")
71 else
72 meta:set_int("type",0)
73 minetest.chat_send_player(pname, "Your stock is unlimited")
74 end
75 elseif not pressed.quit then
76 local n=1
77 for i=1,4,1 do
78 n=i
79 if pressed["buy" .. i] then break end
80 end
81 local pos=smartshop.user[player:get_player_name()]
82 local meta=minetest.get_meta(pos)
83 local type=meta:get_int("type")
84 local inv=meta:get_inventory()
85 local pinv=player:get_inventory()
86 local pname=player:get_player_name()
87 if pressed["buy" .. n] then
88 local name=inv:get_stack("give" .. n,1):get_name()
89 local stack=name .." ".. inv:get_stack("give" .. n,1):get_count()
90 local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count()
91 if name~="" then
92 if type==1 and inv:room_for_item("main", pay)==false then minetest.chat_send_player(pname, "Error: The owner's stock is full, can't receive, exchange aborted.") return end
93 if meta:get_int("ghost") ~=1 then
94 -- transition shops to ghost inventory.
95 for i=1,4 do
96 if inv:room_for_item("main", "pay"..i) and inv:room_for_item("main", "give"..i) then
97 meta:set_int("ghost", 1)
98 inv:add_item("main", inv:get_stack("pay"..i,1))
99 inv:add_item("main", inv:get_stack("give"..i,1))
100 end
101 end
102 end
103 if type==1 and inv:contains_item("main", stack)==false then
104 minetest.chat_send_player(pname, "Error: "..smartshop.get_human_name(name).." is sold out.")
105 if not meta:get_int("alerted") or meta:get_int("alerted") == 0 then
106 meta:set_int("alerted",1) -- Do not alert twice
107 smartshop.send_mail(meta:get_string("owner"), pos, name)
108 end
109 return
110 end
111 if not pinv:contains_item("main", pay) then minetest.chat_send_player(pname, "Error: You don't have enough in your inventory to buy this, exchange aborted.") return end
112 if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return end
113 pinv:remove_item("main", pay)
114 pinv:add_item("main", stack)
115 if type==1 then
116 inv:remove_item("main", stack)
117 inv:add_item("main", pay)
118 if not inv:contains_item("main", stack) and (not meta:get_int("alerted") or meta:get_int("alerted") == 0) then
119 meta:set_int("alerted",1) -- Do not alert twice
120 smartshop.send_mail(meta:get_string("owner"), pos, name)
121 end
122 end
123 end
124 end
125 else
126 local pos=smartshop.user[player:get_player_name()]
127 smartshop.update_info(pos)
128 if smartshop.user[player:get_player_name()] or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
129 local meta=minetest.get_meta(smartshop.user[player:get_player_name()])
130 if meta:get_string("owner")==player:get_player_name() then
131 smartshop.update(smartshop.user[player:get_player_name()],"update")
132 end
133 end
134 smartshop.user[player:get_player_name()]=nil
135 end
136 end
137
138 minetest.register_on_player_receive_fields(function(player, form, pressed)
139 if form=="smartshop.showform" then
140 smartshop.receive_fields(player,pressed)
141 end
142 end)
143
144
145
146
147 smartshop.update_info=function(pos)
148 local meta=minetest.get_meta(pos)
149 local inv = meta:get_inventory()
150 local owner=meta:get_string("owner")
151 if meta:get_int("type")==0 then
152 meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")
153 return false
154 end
155 local name=""
156 local count=0
157 local stuff={}
158 for i=1,4,1 do
159 stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()
160 stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()
161 stuff["stock" ..i]=0 -- stuff["count" ..i]
162 stuff["buy" ..i]=0
163 for ii=1,32,1 do
164 name=inv:get_stack("main",ii):get_name()
165 count=inv:get_stack("main",ii):get_count()
166 if name==stuff["name" ..i] then
167 stuff["stock" ..i]=stuff["stock" ..i]+count
168 end
169 end
170 local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""
171 nstr=nstr.split(nstr, ".")
172 stuff["buy" ..i]=tonumber(nstr[1])
173
174 if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then
175 stuff["buy" ..i]=""
176 stuff["name" ..i]=""
177 else
178
179 stuff["name"..i] = smartshop.get_human_name(stuff["name"..i])
180 stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "
181 stuff["name" ..i]=stuff["name" ..i] .."\n"
182 end
183 end
184 meta:set_string("infotext",
185 "(Smartshop by " .. owner ..") Purchases left:\n"
186 .. stuff.buy1 .. stuff.name1
187 .. stuff.buy2 .. stuff.name2
188 .. stuff.buy3 .. stuff.name3
189 .. stuff.buy4 .. stuff.name4
190 )
191 end
192
193
194
195
196 smartshop.update=function(pos,stat)
197 --clear
198 local spos=minetest.pos_to_string(pos)
199 for _, ob in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
200 if ob and ob:get_luaentity() and ob:get_luaentity().smartshop and ob:get_luaentity().pos==spos then
201 ob:remove()
202 end
203 end
204 if stat=="clear" then return end
205 --update
206 local meta=minetest.get_meta(pos)
207 local inv = meta:get_inventory()
208 local node=minetest.get_node(pos)
209 local dp = smartshop.dir[node.param2+1]
210 if not dp then return end
211 pos.x = pos.x + dp.x*0.01
212 pos.y = pos.y + dp.y*6.5/16
213 pos.z = pos.z + dp.z*0.01
214 for i=1,4,1 do
215 local item=inv:get_stack("give" .. i,1):get_name()
216 local pos2=smartshop.dpos[node.param2+1][i]
217 if item~="" then
218 smartshop.tmp.item=item
219 smartshop.tmp.pos=spos
220 local e = minetest.env:add_entity({x=pos.x+pos2.x,y=pos.y+pos2.y,z=pos.z+pos2.z},"smartshop:item")
221 e:setyaw(math.pi*2 - node.param2 * math.pi/2)
222 end
223 end
224 end
225
226
227 minetest.register_entity("smartshop:item",{
228 hp_max = 1,
229 visual="wielditem",
230 visual_size={x=.20,y=.20},
231 collisionbox = {0,0,0,0,0,0},
232 physical=false,
233 textures={"air"},
234 smartshop=true,
235 type="",
236 on_activate = function(self, staticdata)
237 if smartshop.tmp.item ~= nil then
238 self.item=smartshop.tmp.item
239 self.pos=smartshop.tmp.pos
240 smartshop.tmp={}
241 else
242 if staticdata ~= nil and staticdata ~= "" then
243 local data = staticdata:split(';')
244 if data and data[1] and data[2] then
245 self.item = data[1]
246 self.pos = data[2]
247 end
248 end
249 end
250 if self.item ~= nil then
251 self.object:set_properties({textures={self.item}})
252 else
253 self.object:remove()
254 end
255 end,
256 get_staticdata = function(self)
257 if self.item ~= nil and self.pos ~= nil then
258 return self.item .. ';' .. self.pos
259 end
260 return ""
261 end,
262 })
263
264
265 smartshop.showform=function(pos,player,re)
266 local meta=minetest.get_meta(pos)
267 local creative=meta:get_int("creative")
268 local inv = meta:get_inventory()
269 local gui=""
270 local spos=pos.x .. "," .. pos.y .. "," .. pos.z
271 local owner=meta:get_string("owner")==player:get_player_name()
272 if minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then owner=true end
273 if re then owner=false end
274 smartshop.user[player:get_player_name()]=pos
275 if owner then
276 meta:set_int("alerted",0) -- Player has been there to refill
277 gui=""
278 .."size[8,10]"
279 .."button_exit[6,0;1.5,1;customer;Customer]"
280 .."label[0,0.2;Item:]"
281 .."label[0,1.2;Price:]"
282 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
283 .."list[nodemeta:" .. spos .. ";pay1;2,1;1,1;]"
284 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
285 .."list[nodemeta:" .. spos .. ";pay2;3,1;1,1;]"
286 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
287 .."list[nodemeta:" .. spos .. ";pay3;4,1;1,1;]"
288 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
289 .."list[nodemeta:" .. spos .. ";pay4;5,1;1,1;]"
290 if creative==1 then
291 gui=gui .."label[0.5,-0.4;Your stock is unlimited becaouse you have creative or give]"
292 .."button[6,1;2.2,1;tooglelime;Toggle limit]"
293 end
294 gui=gui
295 .."list[nodemeta:" .. spos .. ";main;0,2;8,4;]"
296 .."list[current_player;main;0,6.2;8,4;]"
297 .."listring[nodemeta:" .. spos .. ";main]"
298 .."listring[current_player;main]"
299 else
300 gui=""
301 .."size[8,6]"
302 .."list[current_player;main;0,2.2;8,4;]"
303 .."label[0,0.2;Item:]"
304 .."label[0,1.2;Price:]"
305 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
306 .."item_image_button[2,1;1,1;".. inv:get_stack("pay1",1):get_name() ..";buy1;\n\n\b\b\b\b\b" .. inv:get_stack("pay1",1):get_count() .."]"
307 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
308 .."item_image_button[3,1;1,1;".. inv:get_stack("pay2",1):get_name() ..";buy2;\n\n\b\b\b\b\b" .. inv:get_stack("pay2",1):get_count() .."]"
309 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
310 .."item_image_button[4,1;1,1;".. inv:get_stack("pay3",1):get_name() ..";buy3;\n\n\b\b\b\b\b" .. inv:get_stack("pay3",1):get_count() .."]"
311 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
312 .."item_image_button[5,1;1,1;".. inv:get_stack("pay4",1):get_name() ..";buy4;\n\n\b\b\b\b\b" .. inv:get_stack("pay4",1):get_count() .."]"
313 end
314 minetest.after((0.1), function(gui)
315 return minetest.show_formspec(player:get_player_name(), "smartshop.showform",gui)
316 end, gui)
317 end
318
319 minetest.register_node("smartshop:shop", {
320 description = "Smartshop",
321 tiles = {"default_chest_top.png^[colorize:#ffffff77^default_obsidian_glass.png"},
322 groups = {choppy = 2, oddly_breakable_by_hand = 1,tubedevice = 1, tubedevice_receiver = 1},
323 drawtype="nodebox",
324 node_box = {type="fixed",fixed={-0.5,-0.5,-0.0,0.5,0.5,0.5}},
325 paramtype2="facedir",
326 paramtype = "light",
327 sunlight_propagates = true,
328 light_source = 10,
329 tube = {insert_object = function(pos, node, stack, direction)
330 local meta = minetest.get_meta(pos)
331 local inv = meta:get_inventory()
332 local added = inv:add_item("main", stack)
333 return added
334 end,
335 can_insert = function(pos, node, stack, direction)
336 local meta = minetest.get_meta(pos)
337 local inv = meta:get_inventory()
338 for i=1,4 do
339 local sellitem = inv:get_stack("give"..i,1):get_name()
340 if sellitem == stack:get_name() then
341 return inv:room_for_item("main", stack)
342 end
343 -- minetest.chat_send_all(sellitem)
344 end
345 --
346 return false
347 end,
348 input_inventory = "main",
349 connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
350 after_place_node = function(pos, placer)
351 local meta=minetest.get_meta(pos)
352 meta:set_string("owner",placer:get_player_name())
353 meta:set_string("infotext", "Shop by: " .. placer:get_player_name())
354 meta:set_int("type",1)
355 if minetest.check_player_privs(placer:get_player_name(), {creative=true}) or minetest.check_player_privs(placer:get_player_name(), {give=true}) then
356 meta:set_int("creative",1)
357 meta:set_int("type",0)
358 end
359 end,
360 on_construct = function(pos)
361 local meta=minetest.get_meta(pos)
362 meta:set_int("state", 0)
363 meta:get_inventory():set_size("main", 32)
364 meta:get_inventory():set_size("give1", 1)
365 meta:get_inventory():set_size("pay1", 1)
366 meta:get_inventory():set_size("give2", 1)
367 meta:get_inventory():set_size("pay2", 1)
368 meta:get_inventory():set_size("give3", 1)
369 meta:get_inventory():set_size("pay3", 1)
370 meta:get_inventory():set_size("give4", 1)
371 meta:get_inventory():set_size("pay4", 1)
372 meta:set_int("ghost", 1)
373 end,
374 on_rightclick = function(pos, node, player, itemstack, pointed_thing)
375 smartshop.showform(pos,player)
376 end,
377 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
378 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
379 local meta = minetest.get_meta(pos)
380 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
381 local inv = minetest.get_inventory({type="node", pos=pos})
382 -- minetest.chat_send_all( inv:get_stack(listname, index):get_name()..stack:get_name())
383 if inv:get_stack(listname, index):get_name() == stack:get_name() then
384 inv:add_item(listname, stack)
385 else
386 inv:set_stack(listname, index, stack)
387 end
388 return 0
389 end
390 return stack:get_count()
391 end
392 return 0
393 end,
394 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
395 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
396 local meta = minetest.get_meta(pos)
397 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
398 local inv = minetest.get_inventory({type="node", pos=pos})
399 inv:set_stack(listname, index, ItemStack(""))
400 return 0
401 end
402 return stack:get_count()
403 end
404 return 0
405 end,
406 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
407 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
408 local meta = minetest.get_meta(pos)
409 local inv = minetest.get_inventory({type="node", pos=pos})
410 if meta:get_int("ghost") ~= 1 then
411 return count
412 end
413 if (string.find(from_list, "pay") or string.find(from_list, "give")) and to_list == "main" then
414 inv:set_stack(from_list, from_index, ItemStack(""))
415 return 0
416 elseif (string.find(to_list, "pay") or string.find(to_list, "give")) and from_list == "main" then
417 if inv:get_stack(to_list, to_index):get_name() == inv:get_stack(from_list, from_index):get_name() then
418 inv:add_item(to_list, inv:get_stack(from_list, from_index))
419 else
420 inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index))
421 inv:set_stack(from_list, from_index, inv:get_stack(from_list, from_index))
422 end
423 return 0
424 else
425 return count
426 end
427 end
428 return 0
429 end,
430 can_dig = function(pos, player)
431 local meta=minetest.get_meta(pos)
432 local inv=meta:get_inventory()
433 if ((meta:get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true})) and inv:is_empty("main") and inv:is_empty("pay1") and inv:is_empty("pay2") and inv:is_empty("pay3") and inv:is_empty("pay4") and inv:is_empty("give1") and inv:is_empty("give2") and inv:is_empty("give3") and inv:is_empty("give4")) or meta:get_string("owner")=="" then
434 smartshop.update(pos,"clear")
435 return true
436 end
437 end,
438 })