2014-07-12 18:47:39 -07:00
|
|
|
local shop = {}
|
|
|
|
|
|
|
|
local textbox = require "action.textbox"
|
|
|
|
|
|
|
|
local input = require "util.input"
|
|
|
|
local memory = require "util.memory"
|
|
|
|
local menu = require "util.menu"
|
|
|
|
local player = require "util.player"
|
|
|
|
|
|
|
|
local inventory = require "storage.inventory"
|
|
|
|
|
|
|
|
function shop.transaction(options)
|
|
|
|
local item, itemMenu, menuIdx, quantityMenu
|
2015-03-30 15:29:00 -07:00
|
|
|
if options.sell then
|
2014-07-12 18:47:39 -07:00
|
|
|
menuIdx = 1
|
|
|
|
itemMenu = 29
|
|
|
|
quantityMenu = 158
|
|
|
|
for i,sit in ipairs(options.sell) do
|
|
|
|
local idx = inventory.indexOf(sit.name)
|
2015-03-30 15:29:00 -07:00
|
|
|
if idx ~= -1 then
|
2014-07-12 18:47:39 -07:00
|
|
|
item = sit
|
|
|
|
item.index = idx
|
|
|
|
item.amount = inventory.count(sit.name)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-03-30 15:29:00 -07:00
|
|
|
if not item and options.buy then
|
2014-07-12 18:47:39 -07:00
|
|
|
menuIdx = 0
|
|
|
|
itemMenu = 123
|
|
|
|
quantityMenu = 161
|
|
|
|
for i,bit in ipairs(options.buy) do
|
|
|
|
local needed = (bit.amount or 1) - inventory.count(bit.name)
|
2015-03-30 15:29:00 -07:00
|
|
|
if needed > 0 then
|
2014-07-12 18:47:39 -07:00
|
|
|
item = bit
|
|
|
|
item.amount = needed
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-03-30 15:29:00 -07:00
|
|
|
if not item then
|
|
|
|
if not textbox.isActive() then
|
2014-07-12 18:47:39 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
input.press("B")
|
2015-03-30 15:29:00 -07:00
|
|
|
elseif player.isFacing(options.direction or "Left") then
|
|
|
|
if textbox.isActive() then
|
|
|
|
if menu.isCurrently(32, "shop") then
|
2014-07-12 18:47:39 -07:00
|
|
|
menu.select(menuIdx, true, false, "shop")
|
2015-03-30 15:29:00 -07:00
|
|
|
elseif menu.getCol() == 15 then
|
2014-07-12 18:47:39 -07:00
|
|
|
input.press("A")
|
2015-03-30 15:29:00 -07:00
|
|
|
elseif menu.isCurrently(itemMenu, "transaction") then
|
|
|
|
if menu.select(item.index, "accelerate", true, "transaction", true) then
|
|
|
|
if menu.isCurrently(quantityMenu, "shop") then
|
2014-07-12 18:47:39 -07:00
|
|
|
local currAmount = memory.value("shop", "transaction_amount")
|
2015-03-30 15:29:00 -07:00
|
|
|
if menu.balance(currAmount, item.amount, false, 99, true) then
|
2014-07-12 18:47:39 -07:00
|
|
|
input.press("A")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
input.press("A")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
input.press("B")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
input.press("A", 2)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
player.interact(options.direction or "Left")
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function shop.vend(options)
|
|
|
|
local item
|
|
|
|
menuIdx = 0
|
|
|
|
for i,bit in ipairs(options.buy) do
|
|
|
|
local needed = (bit.amount or 1) - inventory.count(bit.name)
|
2015-03-30 15:29:00 -07:00
|
|
|
if needed > 0 then
|
2014-07-12 18:47:39 -07:00
|
|
|
item = bit
|
|
|
|
item.buy = needed
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2015-03-30 15:29:00 -07:00
|
|
|
if not item then
|
|
|
|
if not textbox.isActive() then
|
2014-07-12 18:47:39 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
input.press("B")
|
2015-03-30 15:29:00 -07:00
|
|
|
elseif player.face(options.direction) then
|
|
|
|
if textbox.isActive() then
|
|
|
|
if memory.value("battle", "text") > 1 and memory.value("battle", "menu") ~= 95 then
|
2014-07-12 18:47:39 -07:00
|
|
|
menu.select(item.index, true)
|
|
|
|
else
|
|
|
|
input.press("A")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
input.press("A", 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return shop
|