Compare commits

...

2 Commits

Author SHA1 Message Date
Nate Bowman
b6c95cff3b Add market transactions 2026-11-07 19:59:11 -05:00
Nate Bowman
4d9da78842 Add trade 2026-11-07 19:59:11 -05:00
10 changed files with 257 additions and 6 deletions

View File

@@ -9,6 +9,26 @@ class Costs():
return self.costs[purchase] return self.costs[purchase]
def compute_market_price(self, resource, buy=True):
rate = self.market_rates[resource]
if buy:
cost = rate*(1 + self.market_fee)
else:
cost = rate*(1 - self.market_fee)
return cost
def adjust_market_price(self, resource, buy=True):
rate = self.market_rates[resource]
if buy:
new_rate = min(self.maximum_market_rate,
rate + self.market_increment_amount)
else:
new_rate = max(self.minimum_market_rate,
rate - self.market_increment_amount)
self.market_rates[resource] = new_rate
def __init__(self): def __init__(self):
self.costs = { self.costs = {
Ages.DARK: (0, 0, 0, 0), Ages.DARK: (0, 0, 0, 0),
@@ -140,4 +160,15 @@ class Costs():
Units.ARCHER: (0, 25, 45, 0), Units.ARCHER: (0, 25, 45, 0),
Units.MONK: (0, 0, 100, 0), Units.MONK: (0, 0, 100, 0),
Units.FISHINGSHIP: (0, 75, 0, 0), Units.FISHINGSHIP: (0, 75, 0, 0),
Units.TRADECOG: (0, 100, 50, 0),
} }
self.market_rates = {
Resources.FOOD: 115,
Resources.WOOD: 100,
Resources.STONE: 130,
}
self.market_fee = 0.3
self.minimum_market_rate = 20
self.maximum_market_rate = 9999
self.market_increment_amount = 2
self.market_transaction_amount = 100

View File

@@ -271,4 +271,5 @@ class Dependencies():
Units.ARCHER: [Ages.FEUDAL, Buildings.ARCHERYRANGE], Units.ARCHER: [Ages.FEUDAL, Buildings.ARCHERYRANGE],
Units.MONK: [Ages.CASTLE, Buildings.MONASTERY], Units.MONK: [Ages.CASTLE, Buildings.MONASTERY],
Units.FISHINGSHIP: [Ages.FEUDAL, Buildings.DOCK], Units.FISHINGSHIP: [Ages.FEUDAL, Buildings.DOCK],
Units.TRADECOG: [Ages.FEUDAL, Buildings.DOCK],
} }

View File

@@ -1,3 +1,8 @@
class InvalidMarketAmountException(Exception):
pass
class CannotChangeThePastException(Exception): class CannotChangeThePastException(Exception):
pass pass

View File

@@ -37,6 +37,18 @@ class Gameplay():
villagers_to,))) villagers_to,)))
def market_buy(self, action_time, resource, amount):
self._check_time(action_time)
self.actions[action_time].append((self.world.market_buy,
(resource, amount)))
def market_sell(self, action_time, resource, amount):
self._check_time(action_time)
self.actions[action_time].append((self.world.market_sell,
(resource, amount)))
def queue_research(self, action_time, research): def queue_research(self, action_time, research):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_research, self.actions[action_time].append((self.world.queue_research,
@@ -55,6 +67,16 @@ class Gameplay():
(initial_task,))) (initial_task,)))
def queue_trade_cog(self, action_time):
self._check_time(action_time)
self.actions[action_time].append((self.world.queue_trade_cog, ()))
def queue_trade_cart(self, action_time):
self._check_time(action_time)
self.actions[action_time].append((self.world.queue_trade_cart, ()))
def queue_unit(self, action_time, unit): def queue_unit(self, action_time, unit):
self._check_time(action_time) self._check_time(action_time)
self.actions[action_time].append((self.world.queue_unit, (unit,))) self.actions[action_time].append((self.world.queue_unit, (unit,)))

View File

@@ -171,6 +171,11 @@ class Gather():
self.resources[i] -= cost[i] self.resources[i] -= cost[i]
def acquire_resources_without_gather(self, gained):
for i in range(len(self.resources)):
self.resources[i] += gained[i]
def permanently_modify_gather_rate(self, task, rate_multiplier): def permanently_modify_gather_rate(self, task, rate_multiplier):
self.gather_rates[task] *= rate_multiplier self.gather_rates[task] *= rate_multiplier
self.original_gather_rates[task] *= rate_multiplier self.original_gather_rates[task] *= rate_multiplier

View File

@@ -27,6 +27,7 @@ class Production():
Units.ARCHER: Buildings.ARCHERYRANGE, Units.ARCHER: Buildings.ARCHERYRANGE,
Units.MONK: Buildings.MONASTERY, Units.MONK: Buildings.MONASTERY,
Units.FISHINGSHIP: Buildings.DOCK, Units.FISHINGSHIP: Buildings.DOCK,
Units.TRADECOG: Buildings.DOCK,
Research.TOWNWATCH: Buildings.TOWNCENTER, Research.TOWNWATCH: Buildings.TOWNCENTER,
Research.TOWNPATROL: Buildings.TOWNCENTER, Research.TOWNPATROL: Buildings.TOWNCENTER,
Research.TREADMILLCRANE: Buildings.UNIVERSITY, Research.TREADMILLCRANE: Buildings.UNIVERSITY,

View File

@@ -137,4 +137,5 @@ class Times():
Units.ARCHER: 35, Units.ARCHER: 35,
Units.MONK: 51, Units.MONK: 51,
Units.FISHINGSHIP: 40, Units.FISHINGSHIP: 40,
Units.TRADECOG: 36,
} }

View File

@@ -173,3 +173,4 @@ class Units(Enum):
ARCHER = auto() ARCHER = auto()
MONK = auto() MONK = auto()
FISHINGSHIP = auto() FISHINGSHIP = auto()
TRADECOG = auto()

View File

@@ -43,16 +43,11 @@ class World():
# to farms with the highest capacity. # to farms with the highest capacity.
# We keep track of only the closest upcoming farm deletion. # We keep track of only the closest upcoming farm deletion.
# TODO fishing ships # TODO fishtraps
# TODO relics
# TODO trade carts
# TODO allow automation: # TODO allow automation:
# reseeding of farms # reseeding of farms
# queuing of villagers # queuing of villagers
# possibly other things # possibly other things
# TODO gathering boars and hunting
# spend villager time in exchange for adding gatherable resource
# TODO food spoilage
# TODO use market for buy/sell # TODO use market for buy/sell
@@ -65,6 +60,35 @@ class World():
self.gather.spend_resources(cost) self.gather.spend_resources(cost)
def market_buy(self, resource, amount):
self._use_market(resource, amount, buy=True)
def market_sell(self, resource, amount):
self._use_market(resource, amount, buy=False)
def _use_market(self, resource, amount, buy=True):
if amount % self.costs.market_transaction_amount != 0:
raise InvalidMarketAmountException
for i in range(amount//self.costs.market_transaction_amount):
cost = self.costs.compute_market_price(resource, buy=buy)
gold_amount = [0, 0, cost, 0]
resource_amount = [0, 0, 0, 0]
resource_amount[int(resource)] = self.costs.market_transaction_amount
if buy:
self.gather.spend_resources(gold_amount)
self.gather.acquire_resources_without_gather(resource_amount)
else:
self.gather.spend_resources(resource_amount)
self.gather.acquire_resources_without_gather(gold_amount)
self.costs.adjust_market_price(resource, buy=buy)
def has_building(self, building): def has_building(self, building):
return building in self.buildings return building in self.buildings
@@ -419,6 +443,10 @@ class World():
if not initial_task: if not initial_task:
initial_task = Tasks.FISH_SHIP_IDLE initial_task = Tasks.FISH_SHIP_IDLE
self.villager_tasks[initial_task] += 1 self.villager_tasks[initial_task] += 1
elif thing == Units.TRADECOG:
self.villager_tasks[Tasks.TRADECOG] += 1
elif thing == Units.TRADECART:
self.villager_tasks[Tasks.TRADECART] += 1
def _start_producing(self, thing, initial_task): def _start_producing(self, thing, initial_task):
@@ -455,6 +483,14 @@ class World():
self._queue_thing(Units.FISHINGSHIP, initial_task) self._queue_thing(Units.FISHINGSHIP, initial_task)
def queue_trade_cog(self):
self._queue_thing(Units.TRADECOG)
def queue_trade_cart(self):
self._queue_thing(Units.TRADECART)
def _handle_events(self): def _handle_events(self):
for func, args in self.events[self.current_time]: for func, args in self.events[self.current_time]:
func(*args) func(*args)

View File

@@ -307,3 +307,151 @@ class TestWorld(unittest.TestCase):
game.play_until(training + 60) game.play_until(training + 60)
self.assertResourcesMatch([500 + gathered, 425, 500, 500], game.world) self.assertResourcesMatch([500 + gathered, 425, 500, 500], game.world)
def test_trade_cog(self):
game = Gameplay()
# just cheat
game.world.age = Ages.FEUDAL
game.world.gather.resources = [500, 500, 500, 500]
game.world._add_building(Buildings.DOCK, {}, {})
game.queue_trade_cog(0)
training = 36
gathered = 25.9
game.play_until(training + 60)
self.assertResourcesMatch([500, 400, 450 + gathered, 500], game.world)
def test_trade_cart(self):
game = Gameplay()
# just cheat
game.world.age = Ages.FEUDAL
game.world.gather.resources = [500, 500, 500, 500]
game.world._add_building(Buildings.MARKET, {}, {})
game.queue_trade_cart(0)
training = 51
gathered = 25.9
game.play_until(training + 60)
self.assertResourcesMatch([500, 400, 450 + gathered, 500], game.world)
def test_market_buy(self):
game = Gameplay()
game.world.age = Ages.FEUDAL
game.world.gather.resources = [500, 500, 1000, 500]
game.world._add_building(Buildings.MARKET, {}, {})
food_rate = 115
wood_rate = 100
new_gold = game.world.gather.resources[2]
fee = 0.3
inc = 2
game.market_buy(0, Resources.FOOD, 100)
new_gold -= food_rate*(1 + fee)
food_rate += 2
game.play_until(1)
self.assertResourcesMatch([600, 500, new_gold, 500], game.world)
game.market_buy(5, Resources.FOOD, 100)
new_gold -= food_rate*(1 + fee)
food_rate += 2
game.play_until(6)
self.assertResourcesMatch([700, 500, new_gold, 500], game.world)
game.market_buy(7, Resources.FOOD, 300)
for i in range(3):
new_gold -= food_rate*(1 + fee)
food_rate += 2
game.play_until(8)
self.assertResourcesMatch([1000, 500, new_gold, 500], game.world)
game.market_buy(9, Resources.WOOD, 100)
new_gold -= wood_rate*(1 + fee)
wood_rate += 2
game.play_until(10)
self.assertResourcesMatch([1000, 600, new_gold, 500], game.world)
def test_market_sell(self):
game = Gameplay()
game.world.age = Ages.FEUDAL
game.world.gather.resources = [500, 500, 1000, 500]
game.world._add_building(Buildings.MARKET, {}, {})
food_rate = 115
wood_rate = 100
new_gold = game.world.gather.resources[2]
fee = 0.3
inc = 2
game.market_sell(0, Resources.FOOD, 100)
new_gold += food_rate*(1 - fee)
food_rate -= 2
game.play_until(1)
self.assertResourcesMatch([400, 500, new_gold, 500], game.world)
game.market_sell(5, Resources.FOOD, 100)
new_gold += food_rate*(1 - fee)
food_rate -= 2
game.play_until(6)
self.assertResourcesMatch([300, 500, new_gold, 500], game.world)
game.market_sell(7, Resources.FOOD, 300)
for i in range(3):
new_gold += food_rate*(1 - fee)
food_rate -= 2
game.play_until(8)
self.assertResourcesMatch([0, 500, new_gold, 500], game.world)
game.market_sell(9, Resources.WOOD, 100)
new_gold += wood_rate*(1 - fee)
wood_rate -= 2
game.play_until(10)
self.assertResourcesMatch([0, 400, new_gold, 500], game.world)
def test_market_buy_too_much(self):
game = Gameplay()
game.world.age = Ages.FEUDAL
game.world.gather.resources = [500, 500, 1000, 500]
game.world._add_building(Buildings.MARKET, {}, {})
with self.assertRaises(NotEnoughResourcesException):
game.market_buy(0, Resources.FOOD, 1000)
game.play_until(1)
def test_market_sell_too_much(self):
game = Gameplay()
game.world.age = Ages.FEUDAL
game.world.gather.resources = [500, 500, 1000, 500]
game.world._add_building(Buildings.MARKET, {}, {})
with self.assertRaises(NotEnoughResourcesException):
game.market_sell(0, Resources.FOOD, 1000)
game.play_until(1)
def test_market_sell_bad_amount(self):
game = Gameplay()
game.world.age = Ages.FEUDAL
game.world.gather.resources = [500, 500, 1000, 500]
game.world._add_building(Buildings.MARKET, {}, {})
with self.assertRaises(InvalidMarketAmountException):
game.market_sell(0, Resources.FOOD, 50)
game.play_until(1)