Advertisement
Guest User

tst

a guest
Oct 20th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.30 KB | None | 0 0
  1. --Usage: open and edit priceSet to set the prices of your items to be listed.
  2. os.loadAPI("buttonAPI")
  3. os.loadAPI("priceSet")
  4.  
  5. local modem = peripheral.wrap("top")
  6. local monitor = peripheral.wrap("right")
  7. local chestStock = peripheral.wrap("container_chest_12")
  8. local chestBank = peripheral.wrap("container_chest_11")
  9. local chestPay = peripheral.wrap("container_chest_13")
  10. local chestOut = peripheral.wrap("container_chest_10")
  11.  
  12. chestStock.condenseItems()
  13. monitor.clear()
  14. modem.open(1)
  15.  
  16. local items = chestStock.getAllStacks()
  17. local itemNames = {}
  18. local itemQtys = {}
  19. local functions = {}
  20. local itemPrices = {}
  21. local itemQtyPerPurchase = {}
  22. local buyQty = {}
  23. local correctNames = {}
  24. local width = nil
  25. local height = nil
  26. width, height = monitor.getSize()
  27. local xDraw = 3
  28. local yDraw = 2
  29.  
  30. function indexChest(chestName)
  31.   items = chestName.getAllStacks()
  32.   itemNames = {}
  33.   itemQtys = {}
  34.   currItemNum = 1
  35.   chestName.condenseItems()
  36.  
  37.   for i = 1, #items do
  38.     itemName = nameFix(items, i)
  39.     dupCheck = checkForDuplicates(itemNames, itemName)
  40.     if dupCheck ~= nil then
  41.       itemQtys[dupCheck] = itemQtys[dupCheck] + items[i].qty
  42.     else
  43.       itemNames[currItemNum] = itemName
  44.       itemQtys[currItemNum] = items[i].qty
  45.       currItemNum = currItemNum + 1
  46.     end
  47.   end
  48. end
  49.  
  50. function nameFix(item, i)
  51.   itemName = item[i].name
  52.   if string.find(itemName, string.char(167)) ~= nil then
  53.     itemName = string.sub(itemName, 4)
  54.   end
  55.  
  56.   if string.find(itemName, "tile.extrautils:") ~= nil then
  57.     itemName = string.sub(itemName, 17)
  58.   end
  59.  
  60.   if string.find(itemName, "Infinity Orb") ~= nil then
  61.     if item[i].dmg == 10 then
  62.       itemName = "B. Infinity Orb"
  63.     elseif item[i].dmg == 11 then
  64.       itemName = "G. Infinity Orb"
  65.     elseif item[i].dmg == 12 then
  66.       itemName = "R. Infinity Orb"
  67.     end
  68.   end
  69.  
  70.   if correctNames[itemName] ~= nil then
  71.     itemName = correctNames[itemName]
  72.   end
  73.  
  74.   return itemName
  75. end
  76.  
  77. function checkForDuplicates(itemNames, newItem)
  78.   for j = 1, #itemNames do
  79.     if newItem == itemNames[j] then
  80.       return j
  81.     end
  82.   end
  83.   return nil
  84. end
  85.  
  86. function findSlotNumber(items, name)
  87.   for j = 1, #items do
  88.     if name == items[j].name then
  89.       return j
  90.     end
  91.   end
  92.   return nil
  93. end
  94.  
  95. function fillTable()
  96.   j = 1
  97.   yDraw = 2
  98.   xDraw = 2
  99.   buttonAPI.setTableNormal("Twilight Forest (1 Iron)", openDoor, xDraw, xDraw + 24, yDraw, yDraw + 2)
  100.   yDraw = 6
  101.   for i = 1, #itemNames do
  102.     buttonAPI.setTable(itemNames[i] .. ", " .. itemQtys[i] .. " ($" .. itemPrices[itemNames[i]] .. ")", printItem, {i}, xDraw, xDraw + 24, yDraw, yDraw + 2)
  103.     yDraw = 2 + (j+1)*4
  104.     j = j + 1
  105.     if yDraw >= height then
  106.       yDraw = 2
  107.       xDraw = xDraw + 28
  108.       j = 1
  109.     end
  110.   end
  111.   buttonAPI.screen()
  112. end
  113.  
  114. function purchaseButtons(id)
  115.   buttonAPI.setTable("Confirm", confirm, id, 2, 10, 20, 22)
  116.   buttonAPI.setTableNormal("Cancel", cancel, 47, 55, 20, 22)
  117.   buttonAPI.screen()
  118. end
  119.  
  120. function quantityButtons(id)
  121.   buttonAPI.setTable("+", addQuantity, id, 2, 6, 20, 22)
  122.   buttonAPI.setTable("Continue", confirmScreen, id, 16, 24, 20, 22)
  123.   buttonAPI.setTableNormal("Cancel", cancel, 29, 37, 20, 22)
  124.   buttonAPI.setTable("-", subQuantity, id, 51, 55, 20, 22)
  125.   buttonAPI.screen()
  126. end
  127.  
  128. function addQuantity(id)
  129.   if buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]] <= itemQtys[id] then
  130.     buyQty[itemNames[id]] = buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]]
  131.   end
  132.   buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ".    ")
  133.   buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  134. end
  135.  
  136. function subQuantity(id)
  137.   if buyQty[itemNames[id]] >= itemQtyPerPurchase[itemNames[id]] then
  138.     buyQty[itemNames[id]] = buyQty[itemNames[id]] - itemQtyPerPurchase[itemNames[id]]
  139.   end
  140.   buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ".    ")
  141.   buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  142. end
  143.  
  144. function confirmScreen(id)
  145.   buttonAPI.clearTable()
  146.   if (buyQty[itemNames[id]] > 1 and string.sub(itemNames[id], string.len(itemNames[id]) ) ~= "s") then
  147.     buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. "s.")
  148.   else
  149.     buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. ".")
  150.   end
  151.   buttonAPI.label(3, 6, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  152.   buttonAPI.label(3, 10, "Please confirm to continue or cancel to leave.")
  153.   purchaseButtons(id)
  154. end
  155.  
  156. function getClick()
  157.   event,side,x,y = os.pullEvent("monitor_touch")
  158.   buttonAPI.checkxy(x, y)
  159. end
  160.  
  161. function confirm(id)
  162.   slot = checkForDuplicates(items, itemNames[id])
  163.   i = 1
  164.   leave = false
  165.   remaining = 0
  166.  
  167.   sendMessage("getPayment", id, slot, remaining)
  168.   evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  169.   while (message == "atPayment" and leave ~= true) do
  170.     if i > 10 then
  171.       leave = true
  172.       remaining = 0
  173.     elseif message == "noChange" then
  174.       leave = true
  175.       buttonAPI.label(15, 6, "Correct change could not be found. Please contact the shop owner or try a smaller currency.")
  176.     else
  177.       sendMessage("collectPayment", id, slot, remaining)
  178.       evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  179.       evnt, side, sendCh, recCh, remaining, sendDistance = os.pullEvent("modem_message")
  180.       sleep(2)
  181.       i = i + 1
  182.     end
  183.   end
  184.   if leave ~= true then
  185.     sendMessage("collectItem", id, slot, remaining)
  186.     evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  187.     evnt, side, sendCh, recCh, extra, sendDistance = os.pullEvent("modem_message")
  188.     sendMessage("depositPayment", id, slot, extra)
  189.     evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  190.     sendMessage("backToStart", id, slot, remaining)
  191.   end
  192.   cancel()
  193. end
  194.  
  195. function cancel()
  196.   buttonAPI.clearTable()
  197.   buyQty = priceSet.setQPP()
  198.   indexChest(chestStock)
  199.   fillTable()
  200. end
  201.  
  202. function printItem(id)
  203.   local i = id[1]
  204.   local name = itemNames[i]
  205.   local qty = itemQtys[i]
  206.   buyQty[name] = itemQtyPerPurchase[name]
  207.  
  208.   buttonAPI.clearTable()
  209.   if itemQtys[i] > 1 then
  210.     if string.sub(name, string.len(name)) ~= "s" then
  211.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .."s.")
  212.     else
  213.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .. ".")
  214.     end
  215.     buttonAPI.label(3, 6, "To increase the quantity press the \"+\" button,")
  216.     buttonAPI.label(3, 8, "\"-\" to decrease.")
  217.     buttonAPI.label(3, 12, "Once the quantity is correct press \"Continue\"")
  218.     buttonAPI.label(3, 14, "or press \"Cancel\" to leave.")
  219.     buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[name] .. ".    ")
  220.     buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[name] * buyQty[name])/itemQtyPerPurchase[name] .. ".    ")
  221.     quantityButtons(i)
  222.   else
  223.     confirmScreen(i)
  224.   end
  225. end
  226.  
  227. function openDoor()
  228.   redstone.setOutput("back", false)
  229.   sleep(2)
  230.   redstone.setOutput("back", true)
  231. end
  232.  
  233. function findChange(changeAmount)
  234.   indexChest(chestBank)
  235.  
  236.   totalChestValue = 0
  237.  
  238.   for i = 1, #itemNames do
  239.     totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  240.   end
  241.  
  242.   if totalChestValue >= changeAmount then
  243.     return true
  244.   else
  245.     return false
  246.   end
  247. end
  248.  
  249. function sendMessage(type, id, slot, remaining)
  250.   indexChest(chestStock)
  251.   if #itemNames > 0 then
  252.     name = itemNames[id]
  253.     qty = itemQtys[id]
  254.     price = itemPrices[name]
  255.     qtyBuying = buyQty[name]
  256.     itemQPP = itemQtyPerPurchase[name]
  257.     if remaining == 0 then
  258.       cost = (price * qtyBuying)/itemQPP
  259.     else
  260.       cost = remaining
  261.     end
  262.   end
  263.  
  264.   totalChestValue = 0
  265.   bioChestValue = 0
  266.   gioChestValue = 0
  267.   rioChestValue = 0
  268.  
  269.   if type == "getPayment" then
  270.     modem.transmit(1, 1, "getPayment")
  271.     print("1 -> 1 : getPayment")
  272.   elseif type == "collectPayment" then
  273.     indexChest(chestPay)
  274.     for i = 1, #itemNames do
  275.       totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  276.       if itemNames[i] == "B. Infinity Orb" then
  277.         bioLocation = i
  278.         bioChestValue = bioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  279.       elseif itemNames[i] == "G. Infinity Orb" then
  280.         gioLocation = i
  281.         gioChestValue = gioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  282.       elseif itemNames[i] == "R. Infinity Orb" then
  283.         rioLocation = i
  284.         rioChestValue = rioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  285.       end
  286.     end
  287.     if totalChestValue < cost then
  288.       if #items == 0 then
  289.         buttonAPI.label(15, 6, "No payment has been found. Try again.")
  290.         modem.transmit(1, 1, "wrongPayment")
  291.         print("1 -> 1 : items = 0, wrongPayment")
  292.       else
  293.         buttonAPI.label(15, 6, "The amount entered is not enough! Missing: $" .. cost)
  294.         modem.transmit(1, 1, "wrongPayment")
  295.         print("1 -> 1 : tCV < " .. cost .. ", wrongPayment")
  296.       end
  297.     elseif totalChestValue == cost then
  298.       for i = 1, #items do
  299.         chestPay.pushItem("down", i, itemQtys[i])
  300.       end
  301.       modem.transmit(1, 1, "collectPayment")
  302.       print("1 -> 1 : rem = 0, collectPayment")
  303.       modem.transmit(1, 1, 0)
  304.     elseif totalChestValue > cost then
  305.     print("Total chest value = " .. totalChestValue)
  306.     print("B. IO. chest value = " .. bioChestValue)
  307.     print("G. IO. chest value = " .. gioChestValue)
  308.     print("R. IO. chest value = " .. rioChestValue)
  309.       if bioChestValue >= cost then
  310.         costDiff = bioChestValue - cost
  311.         chestPay.pushItem("down", bioLocation, itemQtys[bioLocation] - costDiff)
  312.         modem.transmit(1, 1, "collectPayment")
  313.         print("1 -> 1 : bio >= cost, " .. bioChestValue .. " >= " .. cost .. ", collectPayment")
  314.         modem.transmit(1, 1, 0)
  315.       elseif gioChestValue >= cost then
  316.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  317.         rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  318.         if rem > 0 then
  319.           if bioChestValue >= rem then
  320.             chestPay.pushItem("down", bioLocation, rem)
  321.             chestPay.pushItem("down", gioLocation, qtyToGet)
  322.             modem.transmit(1, 1, "collectPayment")
  323.             print("1 -> 1 : gio >= cost, bio >= rem, collectPayment")
  324.             modem.transmit(1, 1, 0)
  325.           else
  326.             extra = itemPrices[itemNames[gioLocation]] - rem
  327.             if findChange(extra) then
  328.               chestPay.pushItem("down", gioLocation, qtyToGet + 1)
  329.               modem.transmit(1, 1, "collectPayment")
  330.               print("1 -> 1 : gio >= cost, extra = " .. extra .. ", collectPayment")
  331.               modem.transmit(1, 1, extra)
  332.             end
  333.             else
  334.               modem.transmit(1, 1, "noChange")
  335.               print("1 -> 1 : gio >= cost, noChange")
  336.               break
  337.             end        
  338.         else
  339.           chestPay.pushItem("down", gioLocation, qtyToGet)
  340.           modem.transmit(1, 1, "collectPayment")
  341.           print("1 -> 1 : gio >= cost, collectPayment")
  342.           modem.transmit(1, 1, 0)
  343.         end
  344.       elseif bioChestValue + gioChestValue >= cost then
  345.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  346.         rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  347.         chestPay.pushItem("down", bioLocation, rem)
  348.         chestPay.pushItem("down", gioLocation, qtyToGet)
  349.         modem.transmit(1, 1, "collectPayment")
  350.         print("1 -> 1 : bio + gio >= cost, collectPayment")
  351.         modem.transmit(1, 1, 0)
  352.       elseif rioChestValue >= cost then
  353.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrices[itemNames[rioLocation]] ))
  354.         rem = cost - itemPrices[itemNames[rioLocation]] * qtyToGet
  355.         if rem > 0 then
  356.           if bioChestValue >= rem then
  357.             chestPay.pushItem("down", bioLocation, rem)
  358.             chestPay.pushItem("down", rioLocation, qtyToGet)
  359.             modem.transmit(1, 1, "collectPayment")
  360.             print("1 -> 1 : rio >= cost, rem > 0, bio >= rem, collectPayment")
  361.             modem.transmit(1, 1, 0)
  362.           elseif gioChestValue >= rem then
  363.             qtyToGet = math.floor(extra/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  364.             rem = extra - itemPrices[itemNames[gioLocation]] * qtyToGet
  365.             if rem > 0 then
  366.               if bioChestValue >= rem then
  367.                 chestPay.pushItem("down", bioLocation, rem)
  368.                 chestPay.pushItem("down", gioLocation, qtyToGet)
  369.                 modem.transmit(1, 1, "collectPayment")
  370.                 print("1 -> 1 : rio >= cost, rem > 0, gio >= rem, bio >= rem2, collectPayment")
  371.                 modem.transmit(1, 1, 0)
  372.               end
  373.             else
  374.               chestBank.pullItem("up", gioLocation, qtyToGet)
  375.               modem.transmit(1, 1, "collectPayment")
  376.               print("1 -> 1 : rio >= extra, rem > 0, gio >= rem, collectPayment")
  377.               modem.transmit(1, 1, 0)
  378.             end
  379.           end
  380.         else
  381.           extra = (qtyToGet+1)*itemPrices[itemNames[rioLocation]] - cost
  382.           if findChange(extra) then
  383.             chestPay.pushItem("down", rioLocation, qtyToGet+1)
  384.             modem.transmit(1, 1, "collectPayment")
  385.             print("1 -> 1 : rio >= cost, extra = " .. extra .. ", collectPayment")
  386.             modem.transmit(1, 1, extra)
  387.           else
  388.             modem.transmit(1, 1, "noChange")
  389.             print("1 -> 1 : rio >= cost, noChange")
  390.             break
  391.           end
  392.         end
  393.       end
  394.     end
  395.   elseif type == "collectItem" then
  396.     targetSlot = findSlotNumber(items, name)
  397.     print(targetSlot)
  398.     nextSlot = 0
  399.     while qtyBuying >= 64 do
  400.       chestStock.pushItem("up", targetSlot + nextSlot, 64)
  401.       qtyBuying = qtyBuying - 64
  402.       nextSlot = nextSlot + 1
  403.     end
  404.     chestStock.pushItem("up", targetSlot + nextSlot, qtyBuying)
  405.     modem.transmit(1, 1, "dropOffPayment")
  406.     print("1 -> 1 : qtyBuying = " .. qtyBuying .. ", dropOffPayment")
  407.     modem.transmit(1, 1, qtyBuying)
  408.   elseif type == "depositPayment" then
  409.     extra = remaining
  410.     indexChest(chestBank)
  411.     for i = 1, #itemNames
  412.       totalChestValue = totalChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  413.       if itemNames[i] == "B. Infinity Orb" then
  414.         bioLocation = i
  415.         bioChestValue = bioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  416.       elseif itemNames[i] == "G. Infinity Orb" then
  417.         gioLocation = i
  418.         gioChestValue = gioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  419.       elseif itemNames[i] == "R. Infinity Orb" then
  420.         rioLocation = i
  421.         rioChestValue = rioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  422.       end
  423.     end
  424.  
  425.     if bioChestValue >= extra then
  426.       extraDiff = bioChestValue - extra
  427.       chestBank.pullItem("up", bioLocation, extraDiff)
  428.       modem.transmit(1, 1, "dropOffItem")
  429.       print("1 -> 1 : bio >= extra, dropOffItem")
  430.     elseif gioChestValue >= extra then
  431.       qtyToGet = math.floor(extra/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  432.       rem = extra - itemPrices[itemNames[gioLocation]] * qtyToGet
  433.       if rem > 0 then
  434.         if bioChestValue >= rem then
  435.           chestBank.pullItem("up", bioLocation, rem)
  436.           chestBank.pullItem("up", gioLocation, qtyToGet)
  437.           modem.transmit(1, 1, "dropOffItem")
  438.           print("1 -> 1 : gio >= extra, rem > 0, bio >= extra, dropOffItem")
  439.         end
  440.       else
  441.         chestBank.pullItem("up", gioLocation, qtyToGet)
  442.         modem.transmit(1, 1, "dropOffItem")
  443.         print("1 -> 1 : gio >= cost, dropOffItem")
  444.       end
  445.     elseif bioChestValue+gioChestValue >= extra then
  446.       qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  447.       rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  448.       chestPay.pushItem("down", bioLocation, rem)
  449.       chestPay.pushItem("down", gioLocation, qtyToGet)
  450.       modem.transmit(1, 1, "dropOffItem")
  451.       print("1 -> 1 : bio + gio >= extra, dropOffItem")
  452.     elseif rioChestValue >= extra
  453.       qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrices[itemNames[rioLocation]] ))
  454.       rem = cost - itemPrices[itemNames[rioLocation]] * qtyToGet
  455.       if rem > 0 then
  456.         if bioChestValue >= rem then
  457.           chestBank.pullItem("up", bioLocation, rem)
  458.           chestBank.pullItem("up", rioLocation, qtyToGet)
  459.           modem.transmit(1, 1, "dropOffItem")
  460.           print("1 -> 1 : rio >= extra, rem > 0, bio >= rem, dropOffItem")
  461.         elseif gioChestValue >= rem then
  462.           qtyToGet = math.floor(extra/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  463.           rem = extra - itemPrices[itemNames[gioLocation]] * qtyToGet
  464.           if rem > 0 then
  465.             if bioChestValue >= rem then
  466.               chestBank.pullItem("up", bioLocation, rem)
  467.               chestBank.pullItem("up", gioLocation, qtyToGet)
  468.               modem.transmit(1, 1, "dropOffItem")
  469.               print("1 -> 1 : rio >= extra, rem > 0, gio >= rem, bio >= rem2, dropOffItem")
  470.             end
  471.           else
  472.             chestBank.pullItem("up", gioLocation, qtyToGet)
  473.             modem.transmit(1, 1, "dropOffItem")
  474.             print("1 -> 1 : rio >= extra, rem > 0, gio >= rem, dropOffItem")
  475.           end
  476.         end
  477.       end
  478.     end
  479.   elseif type == "backToStart" then
  480.     modem.transmit(1, 2, "backToStart")
  481.     print("1 -> 1 : backToStart")
  482.   end  
  483. end
  484.  
  485. correctNames["tile.TFTowerDevice.0.name"] = "Reappearing Block"
  486. correctNames["cursedearthside.name"] = "Cursed Earth"
  487. correctNames["cobblestone_compressed.0.name"] = "Compr. Cobble"
  488. correctNames["Bucket of Liquid Essence"] = "Liquid Essence"
  489.  
  490. itemPrices = priceSet.setPrices()
  491. itemQtyPerPurchase = priceSet.setQPP()
  492. buyQty = priceSet.setQPP()
  493.  
  494. indexChest(chestStock)
  495.  
  496. fillTable()
  497.  
  498. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement