Cleaned up code, made raising prices easier
[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.tooglelime 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("type")==0 then
63 meta:set_int("type",1)
64 minetest.chat_send_player(pname, "Your stock is limited")
65 else
66 meta:set_int("type",0)
67 minetest.chat_send_player(pname, "Your stock is unlimited")
68 end
69 elseif not pressed.quit then
70 local n=1
71 for i=1,4,1 do
72 n=i
73 if pressed["buy" .. i] then break end
74 end
75 local pos=smartshop.user[player:get_player_name()]
76 local meta=minetest.get_meta(pos)
77 local type=meta:get_int("type")
78 local inv=meta:get_inventory()
79 local pinv=player:get_inventory()
80 local pname=player:get_player_name()
81 if pressed["buy" .. n] then
82 local name=inv:get_stack("give" .. n,1):get_name()
83 local stack=name .." ".. inv:get_stack("give" .. n,1):get_count()
84 local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count()
85 if name~="" then
86 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
87 if meta:get_int("ghost") ~=1 then
88 -- transition shops to ghost inventory.
89 for i=1,4 do
90 if inv:room_for_item("main", "pay"..i) and inv:room_for_item("main", "give"..i) then
91 meta:set_int("ghost", 1)
92 inv:add_item("main", inv:get_stack("pay"..i,1))
93 inv:add_item("main", inv:get_stack("give"..i,1))
94 end
95 end
96 end
97 if type==1 and inv:contains_item("main", stack)==false then
98 minetest.chat_send_player(pname, "Error: The owners stock is end.")
99 if not meta:get_int("alerted") or meta:get_int("alerted") == 0 then
100 meta:set_int("alerted",1) -- Do not alert twice
101 smartshop.send_mail(meta:get_string("owner"), pos, stack)
102 end
103 return
104 end
105 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
106 if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return end
107 pinv:remove_item("main", pay)
108 pinv:add_item("main", stack)
109 if type==1 then
110 inv:remove_item("main", stack)
111 inv:add_item("main", pay)
112 if not inv:contains_item("main", stack) and (not meta:get_int("alerted") or meta:get_int("alerted") == 0) then
113 meta:set_int("alerted",1) -- Do not alert twice
114 smartshop.send_mail(meta:get_string("owner"), pos, stack)
115 end
116 end
117 end
118 end
119 else
120 local pos=smartshop.user[player:get_player_name()]
121 smartshop.update_info(pos)
122 if smartshop.user[player:get_player_name()] or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
123 local meta=minetest.get_meta(smartshop.user[player:get_player_name()])
124 if meta:get_string("owner")==player:get_player_name() then
125 smartshop.update(smartshop.user[player:get_player_name()],"update")
126 end
127 end
128 smartshop.user[player:get_player_name()]=nil
129 end
130 end
131
132 minetest.register_on_player_receive_fields(function(player, form, pressed)
133 if form=="smartshop.showform" then
134 smartshop.receive_fields(player,pressed)
135 end
136 end)
137
138
139
140
141 smartshop.update_info=function(pos)
142 local meta=minetest.get_meta(pos)
143 local inv = meta:get_inventory()
144 local owner=meta:get_string("owner")
145 if meta:get_int("type")==0 then
146 meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")
147 return false
148 end
149 local name=""
150 local count=0
151 local stuff={}
152 for i=1,4,1 do
153 stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()
154 stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()
155 stuff["stock" ..i]=stuff["count" ..i]
156 stuff["buy" ..i]=0
157 for ii=1,32,1 do
158 name=inv:get_stack("main",ii):get_name()
159 count=inv:get_stack("main",ii):get_count()
160 if name==stuff["name" ..i] then
161 stuff["stock" ..i]=stuff["stock" ..i]+count
162 end
163 end
164 local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""
165 nstr=nstr.split(nstr, ".")
166 stuff["buy" ..i]=tonumber(nstr[1])
167
168 if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then
169 stuff["buy" ..i]=""
170 stuff["name" ..i]=""
171 else
172 --if string.find(stuff["name" ..i],":")~=nil then
173 -- stuff["name" ..i]=stuff["name" ..i].split(stuff["name" ..i],":")[2]
174 --end
175 if core.registered_items[stuff["name"..i]] then
176 stuff["name"..i] = core.registered_items[stuff["name"..i]].description
177 else
178 stuff["name"..i] = "Unknown Object"
179 end
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 })