49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
// Package market prices ore and settles deliveries. Prices are integers
|
|
// (credits per kg) that fall as supply accumulates and recover over time.
|
|
package market
|
|
|
|
import "wh/world"
|
|
|
|
var basePrice = [world.NumOre]int64{world.Iron: 2, world.Nickel: 6, world.Ice: 3, world.Platinum: 300}
|
|
|
|
// saturation is the supply (kg) at which an ore's price has halved.
|
|
var saturation = [world.NumOre]int64{world.Iron: 400_000, world.Nickel: 200_000, world.Ice: 300_000, world.Platinum: 5_000}
|
|
|
|
type Market struct {
|
|
Supply [world.NumOre]int64 // decayed running total of kg sold
|
|
Prices [world.NumOre]int64 // credits per kg, fixed for the day
|
|
}
|
|
|
|
func New() Market {
|
|
m := Market{}
|
|
m.reprice()
|
|
return m
|
|
}
|
|
|
|
func (m *Market) reprice() {
|
|
for o := range m.Prices {
|
|
p := basePrice[o] * saturation[o] / (saturation[o] + m.Supply[o])
|
|
if p < 1 {
|
|
p = 1
|
|
}
|
|
m.Prices[o] = p
|
|
}
|
|
}
|
|
|
|
// Value returns the payout for a cargo at today's prices.
|
|
func (m *Market) Value(cargo [world.NumOre]int64) int64 {
|
|
var v int64
|
|
for o, kg := range cargo {
|
|
v += kg * m.Prices[o]
|
|
}
|
|
return v
|
|
}
|
|
|
|
// EndOfDay folds the day's sales into supply (with 10% decay) and reprices.
|
|
func (m *Market) EndOfDay(sold [world.NumOre]int64) {
|
|
for o := range m.Supply {
|
|
m.Supply[o] = m.Supply[o]*9/10 + sold[o]
|
|
}
|
|
m.reprice()
|
|
}
|