api.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. local has_value=basic_functions.has_value
  2. function kiosk.demangle_for_formspec(str)
  3. -- copied from unified_inventory
  4. return string.gsub(str, "_([0-9]+)_", function (v) return string.char(v) end)
  5. end
  6. kiosk.add_inv=function(name,def)
  7. if name == nil then return end
  8. def.name = name
  9. if def.buy == nil then
  10. def.buy=1
  11. end
  12. if def.sell == nil then
  13. def.sell = 1
  14. end
  15. if def.sell > def.buy then
  16. def.sell = kiosk.sell_factor * def.buy
  17. end
  18. if def.stock==nil then def.stock=0 end
  19. if def.dug==nil then def.dug=0 end
  20. if def.crafted==nil then def.crafted=0 end
  21. if kiosk.inventar[name] ~= nil then kiosk.inventar[name] = nil end
  22. local dn = name:split(":")[1]
  23. if dn == "group" then
  24. print(name)
  25. def.is_group=1
  26. end
  27. kiosk.inventar[name] = def
  28. end
  29. kiosk.add_inventar=function(name,buy_value,sell_value,stock,fixed)
  30. if name == nil then
  31. return
  32. end
  33. if buy_value == nil then
  34. buy_value=1
  35. end
  36. if buy_value == 0 then
  37. buy_value=1
  38. end
  39. if sell_value == nil then
  40. sell_value=kiosk.sell_factor * buy_value
  41. end
  42. if sell_value > buy_value then
  43. sell_value=kiosk.sell_factor * buy_value
  44. end
  45. if stock == nil then
  46. stock = 0
  47. end
  48. if kiosk.inventar[name] == nil then
  49. local kinv = {name = name,
  50. sell = sell_value,
  51. buy = buy_value,
  52. stock=stock,
  53. dug=0,
  54. craft=0,
  55. }
  56. if fixed ~= nil then kinv.fixed=fixed end
  57. kiosk.inventar[name] = kinv
  58. else
  59. local kiosk_inv=kiosk.inventar[name]
  60. kiosk_inv.sell=sell_value
  61. kiosk_inv.buy=buy_value
  62. if fixed ~= nil then
  63. kiosk_inv.fixed = fixed
  64. end
  65. end
  66. end
  67. kiosk.is_drop_item=function(name)
  68. if kiosk.inventar[name] == nil then
  69. kiosk.add_inv(name,{})
  70. end
  71. kiosk.inventar[name].is_drop = 1
  72. end
  73. kiosk.register_craft_relation=function(input,output)
  74. if kiosk.inventar[input] == nil then
  75. kiosk.add_inv(input,{})
  76. end
  77. if kiosk.inventar[output] == nil then
  78. kiosk.add_inv(output,{})
  79. end
  80. local source=kiosk.inventar[input]
  81. local dest=kiosk.inventar[output]
  82. if source.dest == nil then
  83. source.dest = {output}
  84. else
  85. if not has_value(source.dest,output) then
  86. table.insert(source.dest,output)
  87. end
  88. end
  89. if dest.source == nil then
  90. dest.source = {input}
  91. else
  92. if not has_value(dest.source,input) then
  93. table.insert(dest.source,input)
  94. end
  95. end
  96. end
  97. kiosk.is_inventar=function(name)
  98. return kiosk.inventar[name] ~= nil
  99. end
  100. kiosk.get_value=function(name,value)
  101. if name == nil then return end
  102. if kiosk.inventar[name] == nil then
  103. kiosk.add_inventar(name,1,0.9,0)
  104. end
  105. local kinv=kiosk.inventar[name]
  106. local output = 0
  107. if kinv[value] ~= nil then
  108. output = kinv[value]
  109. end
  110. return output
  111. end
  112. kiosk.get_buy=function(name)
  113. return kiosk.get_value(name,"buy")
  114. end
  115. kiosk.get_sell=function(name)
  116. return kiosk.get_value(name,"sell")
  117. end
  118. kiosk.get_stock=function(name)
  119. return kiosk.get_value(name,"stock")
  120. end
  121. kiosk.player_sell=function(player)
  122. if player == nil then return end
  123. local playername=player:get_player_name()
  124. if playername == nil then return end
  125. if playername == "" then return end
  126. local player_inv=minetest.get_inventory({type="detached",name=playername.."_sell"})
  127. if player_inv == nil then return end
  128. local sell_stack=player_inv:get_stack("main",1)
  129. if sell_stack == nil then return end
  130. local sell_item=sell_stack:get_name()
  131. if sell_item == nil then return end
  132. if sell_item == "" then return end
  133. local sell_count = sell_stack:get_count()
  134. if sell_count < 1 then return end
  135. local price_single = kiosk.get_sell(sell_item)
  136. local price=price_single * sell_count
  137. xpfw.player_add_attribute(player,kiosk.account,price)
  138. local kinv=kiosk.inventar[sell_item]
  139. kinv.stock=kinv.stock+sell_count
  140. player_inv:set_stack("main",1,nil)
  141. xpfw.player_add_attribute(player,"kiosk_sold",sell_count)
  142. end