68 lines
2.5 KiB
Text
68 lines
2.5 KiB
Text
# trade.ludic - standing at a counter, and the only ways anything changes hands
|
|
export function shop_visit(shop_st: mut ShopState, v: int) -> void {
|
|
if shop_vendor_ok(shop_st, v) { shop_st.shop_at = v } else { shop_st.shop_at = -1 }
|
|
}
|
|
|
|
export function shop_away(shop_st: mut ShopState) -> void { shop_st.shop_at = -1 }
|
|
|
|
# the vendor at the counter, -1 none
|
|
export function shop_vendor(shop_st: ShopState) -> int { return shop_st.shop_at }
|
|
|
|
export function shop_at_counter(shop_st: ShopState) -> bool { return shop_st.shop_at >= 0 }
|
|
|
|
# what `v` has on its shelves, in shelf order
|
|
export function shop_stock(shop_st: ShopState, v: int) -> []int {
|
|
let out = new []int
|
|
if not shop_vendor_ok(shop_st, v) or shop_st.shop_vendor_list[v].stock == null { return out }
|
|
let st = shop_st.shop_vendor_list[v].stock
|
|
for k in 0 .. len(st) { push(out, st[k]) }
|
|
return out
|
|
}
|
|
|
|
# what in the pack `v` would buy
|
|
export function shop_sellable(shop_st: ShopState, v: int) -> []int {
|
|
let out = new []int
|
|
for it in 0 .. shop_items(shop_st) { if ShopPack.count(it) > 0 and shop_buys(shop_st, v, it) { push(out, it) } }
|
|
return out
|
|
}
|
|
|
|
function shop_trade_fact(shop_st: ShopState, what: int, it: int, n: int, price: int) -> void {
|
|
var t = q_rec(shop_trades(shop_st))
|
|
if t == null {
|
|
@alloc_ok("past the ring: more of these in a frame than its records, then kept in it")
|
|
t = new ShopTrade
|
|
q_ring_add(shop_trades(shop_st), t)
|
|
}
|
|
t.what = what
|
|
t.vendor = shop_st.shop_at
|
|
t.item = it
|
|
t.n = n
|
|
t.price = price
|
|
q_push(shop_trades(shop_st), t)
|
|
}
|
|
|
|
export function shop_can_buy(shop_st: ShopState, it: int) -> bool {
|
|
return shop_item_ok(shop_st, it) and ShopPurse.money() >= shop_price_buy(shop_st, it) and ShopPack.room(it) > 0
|
|
}
|
|
|
|
# one, paid for and in the pack; false and nothing spent when it cannot be
|
|
export function shop_buy(shop_st: ShopState, it: int) -> bool {
|
|
if not shop_can_buy(shop_st, it) { return false }
|
|
let pr = shop_price_buy(shop_st, it)
|
|
if not ShopPurse.spend(pr) { return false }
|
|
ShopPack.add(it, 1)
|
|
shop_trade_fact(shop_st, SHOP_BOUGHT, it, 1, pr)
|
|
return true
|
|
}
|
|
|
|
# `n` to the vendor at the counter (as many as the pack has); how many went
|
|
export function shop_sell(shop_st: ShopState, it: int, n: int) -> int {
|
|
if not shop_buys(shop_st, shop_st.shop_at, it) { return 0 }
|
|
let k = Math.min(n, ShopPack.count(it))
|
|
if k <= 0 { return 0 }
|
|
let pr = shop_price_sell(shop_st, it)
|
|
if not ShopPack.take(it, k) { return 0 }
|
|
ShopPurse.earn(pr * k)
|
|
shop_trade_fact(shop_st, SHOP_SOLD, it, k, pr)
|
|
return k
|
|
}
|