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