96 lines
4.1 KiB
GDScript
96 lines
4.1 KiB
GDScript
class_name Economy
|
|
extends RefCounted
|
|
|
|
const DeterministicRngType = preload("res://scripts/domain/run/deterministic_rng.gd")
|
|
const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd")
|
|
const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd")
|
|
|
|
const SHOP_ACTIVE_FROM_FLOOR := 3
|
|
const SAFE_ROOM_PAID_FROM_FLOOR := 3
|
|
const SAFE_ROOM_BASE_REST_COST := 10
|
|
const DEFAULT_COLONY_SIZE: StringName = &"bourg"
|
|
const COLONY_SIZES := {
|
|
&"hameau": {"label": "Hameau", "shop_price_mult": 1.35, "rest_cost_mult": 0.6},
|
|
&"bourg": {"label": "Bourg", "shop_price_mult": 1.0, "rest_cost_mult": 1.0},
|
|
&"ville": {"label": "Ville-monde", "shop_price_mult": 0.8, "rest_cost_mult": 1.5},
|
|
}
|
|
const COLONY_SIZE_WEIGHTS := {
|
|
&"hameau": 45,
|
|
&"bourg": 40,
|
|
&"ville": 15,
|
|
}
|
|
|
|
static func colony_size_for_floor(seed: String, floor_number: int) -> StringName:
|
|
if floor_number < SHOP_ACTIVE_FROM_FLOOR:
|
|
return &""
|
|
var rng := DeterministicRngType.from_seed("%s-floor%d-colony" % [seed, floor_number])
|
|
var roll := rng.next_int() % 100
|
|
for size: StringName in COLONY_SIZE_WEIGHTS:
|
|
roll -= int(COLONY_SIZE_WEIGHTS[size])
|
|
if roll < 0:
|
|
return size
|
|
return DEFAULT_COLONY_SIZE
|
|
|
|
static func colony_info(colony_size: StringName) -> Dictionary:
|
|
return COLONY_SIZES.get(colony_size, COLONY_SIZES[DEFAULT_COLONY_SIZE]).duplicate(true)
|
|
|
|
static func buy_price(item: Dictionary, colony_size: StringName) -> int:
|
|
return maxi(1, roundi(float(item.get("value", 0)) * float(colony_info(colony_size).shop_price_mult)))
|
|
|
|
static func sell_price(item: Dictionary, colony_size: StringName) -> int:
|
|
return maxi(1, roundi(float(int(item.get("value", 0)) / 2) * float(colony_info(colony_size).shop_price_mult)))
|
|
|
|
static func rest_cost(floor_number: int, colony_size: StringName) -> int:
|
|
if floor_number < SAFE_ROOM_PAID_FROM_FLOOR:
|
|
return 0
|
|
return maxi(1, roundi(SAFE_ROOM_BASE_REST_COST * float(colony_info(colony_size).rest_cost_mult)))
|
|
|
|
static func pay_for_rest(floor_number: int, colony_size: StringName, gold: int) -> Dictionary:
|
|
var price := rest_cost(floor_number, colony_size)
|
|
if gold < price:
|
|
return _result(false, &"insufficient_gold", gold, price)
|
|
return _result(true, &"", gold - price, price)
|
|
|
|
static func shop_stock(catalog: ContentCatalog, npc: Dictionary) -> Array[Dictionary]:
|
|
var stock: Array[Dictionary] = []
|
|
if catalog == null:
|
|
return stock
|
|
for item_id_value in npc.get("shop_table", []):
|
|
var item := catalog.get_item(StringName(item_id_value))
|
|
if not item.is_empty():
|
|
stock.append(item)
|
|
return stock
|
|
|
|
static func buy(catalog: ContentCatalog, npc: Dictionary, inventory: InventoryState, gold: int, item_id: StringName, colony_size: StringName) -> Dictionary:
|
|
if catalog == null or inventory == null:
|
|
return _result(false, &"invalid_shop", gold)
|
|
var stock_ids: Array[StringName] = []
|
|
for item_data in shop_stock(catalog, npc):
|
|
stock_ids.append(StringName(item_data.id))
|
|
if not stock_ids.has(item_id):
|
|
return _result(false, &"not_in_stock", gold)
|
|
var definition_data := catalog.get_item(item_id)
|
|
var price := buy_price(definition_data, colony_size)
|
|
if gold < price:
|
|
return _result(false, &"insufficient_gold", gold, price)
|
|
var item := ItemInstanceType.new(ItemDefinitionType.new(definition_data))
|
|
var added := inventory.add_item(item)
|
|
if not bool(added.accepted):
|
|
return _result(false, StringName(added.reason), gold, price)
|
|
return _result(true, &"", gold - price, price, item)
|
|
|
|
static func sell(inventory: InventoryState, gold: int, instance_id: StringName, colony_size: StringName) -> Dictionary:
|
|
if inventory == null:
|
|
return _result(false, &"invalid_inventory", gold)
|
|
var item := inventory.item_by_id(instance_id)
|
|
if item == null:
|
|
return _result(false, &"missing_item", gold)
|
|
var price := sell_price(item.definition.raw, colony_size)
|
|
var removed := inventory.remove_item(instance_id)
|
|
if not bool(removed.accepted):
|
|
return _result(false, StringName(removed.reason), gold, price)
|
|
return _result(true, &"", gold + price, price, removed.item)
|
|
|
|
static func _result(accepted: bool, reason: StringName, gold: int, price: int = 0, item: ItemInstance = null) -> Dictionary:
|
|
return {"accepted": accepted, "reason": reason, "gold": gold, "price": price, "item": item}
|