From 527aac6c767d2a8005bf98cde0ec2dfbb203ed90 Mon Sep 17 00:00:00 2001 From: Credence Date: Fri, 27 Sep 2024 07:21:43 +0100 Subject: [PATCH] contracts: use quadratic formula for scaling building cost --- contracts/src/models/buildings.cairo | 15 ++++++++++++--- contracts/src/models/config.cairo | 2 +- contracts/src/systems/config/contracts.cairo | 6 +++--- sdk/packages/eternum/src/config/index.ts | 2 +- sdk/packages/eternum/src/constants/global.ts | 2 +- sdk/packages/eternum/src/provider/index.ts | 4 ++-- sdk/packages/eternum/src/types/provider.ts | 2 +- 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/contracts/src/models/buildings.cairo b/contracts/src/models/buildings.cairo index 59b729ac1..8e5e2d994 100644 --- a/contracts/src/models/buildings.cairo +++ b/contracts/src/models/buildings.cairo @@ -594,12 +594,21 @@ impl BuildingCustomImpl of BuildingCustomTrait { break; } + // calculate cost of building based on the formula: + // Cost = Base + (Base * Rate * (N - 1)²) + // Where: + // Base = The cost of the first building + // Rate = How quickly the cost goes up (a small number like 0.1 or 0.2) + // N = Which number building this is (1st, 2nd, 3rd, etc.) + // let resource_cost: ResourceCost = get!(world, (building_config.resource_cost_id, index), ResourceCost); let mut resource = ResourceCustomImpl::get(world, (self.outer_entity_id, resource_cost.resource_type)); - let additional_cost = PercentageImpl::get( - resource_cost.amount, building_general_config.cost_scale_percent.into() + let percentage_additional_cost = PercentageImpl::get( + resource_cost.amount, building_general_config.base_cost_percent_increase.into() ); - let total_cost = resource_cost.amount + ((building_quantity.value.into() - 1) * additional_cost); + let scale_factor = building_quantity.value - 1; + let total_cost = resource_cost.amount + + (scale_factor.into() * scale_factor.into() * percentage_additional_cost); resource.burn(total_cost); resource.save(world); index += 1; diff --git a/contracts/src/models/config.cairo b/contracts/src/models/config.cairo index fbe919b19..7038cdb5f 100644 --- a/contracts/src/models/config.cairo +++ b/contracts/src/models/config.cairo @@ -350,7 +350,7 @@ pub struct BuildingGeneralConfig { #[key] config_id: ID, // cost scale percent - cost_scale_percent: u16, + base_cost_percent_increase: u16, } diff --git a/contracts/src/systems/config/contracts.cairo b/contracts/src/systems/config/contracts.cairo index f905d41c2..fa72ae385 100644 --- a/contracts/src/systems/config/contracts.cairo +++ b/contracts/src/systems/config/contracts.cairo @@ -107,7 +107,7 @@ trait ITroopConfig { #[dojo::interface] trait IBuildingConfig { - fn set_building_general_config(ref world: IWorldDispatcher, cost_scale_percent: u16); + fn set_building_general_config(ref world: IWorldDispatcher, base_cost_percent_increase: u16); fn set_building_config( ref world: IWorldDispatcher, building_category: BuildingCategory, @@ -529,10 +529,10 @@ mod config_systems { #[abi(embed_v0)] impl BuildingConfigCustomImpl of super::IBuildingConfig { - fn set_building_general_config(ref world: IWorldDispatcher, cost_scale_percent: u16) { + fn set_building_general_config(ref world: IWorldDispatcher, base_cost_percent_increase: u16) { assert_caller_is_admin(world); - set!(world, BuildingGeneralConfig { config_id: WORLD_CONFIG_ID, cost_scale_percent }); + set!(world, BuildingGeneralConfig { config_id: WORLD_CONFIG_ID, base_cost_percent_increase }); } fn set_building_config( diff --git a/sdk/packages/eternum/src/config/index.ts b/sdk/packages/eternum/src/config/index.ts index 8247f76f3..9919a2e98 100644 --- a/sdk/packages/eternum/src/config/index.ts +++ b/sdk/packages/eternum/src/config/index.ts @@ -76,7 +76,7 @@ export const setBuildingCategoryPopConfig = async (account: Account, provider: E export const setBuildingGeneralConfig = async (account: Account, provider: EternumProvider) => { const tx = await provider.set_building_general_config({ signer: account, - cost_scale_percent: BUILDING_FIXED_COST_SCALE_PERCENT, + base_cost_percent_increase: BUILDING_FIXED_COST_SCALE_PERCENT, }); console.log(`Configuring building general config ${tx.statusReceipt}...`); diff --git a/sdk/packages/eternum/src/constants/global.ts b/sdk/packages/eternum/src/constants/global.ts index 99af8ecad..3fda6cdc1 100644 --- a/sdk/packages/eternum/src/constants/global.ts +++ b/sdk/packages/eternum/src/constants/global.ts @@ -102,7 +102,7 @@ export const ONE_MONTH = 2628000; // Buildings export const BASE_POPULATION_CAPACITY = 5; -export const BUILDING_FIXED_COST_SCALE_PERCENT = 1_000; // 1_000/10_000 = 10% +export const BUILDING_FIXED_COST_SCALE_PERCENT = 5_000; // 5_000/10_000 = 50% // Points export const HYPERSTRUCTURE_POINTS_PER_CYCLE = 10; export const HYPERSTRUCTURE_POINTS_ON_COMPLETION = 2_000_000; // about the amount of points generated by the structure in 2 days diff --git a/sdk/packages/eternum/src/provider/index.ts b/sdk/packages/eternum/src/provider/index.ts index ab3578e4f..444291a22 100644 --- a/sdk/packages/eternum/src/provider/index.ts +++ b/sdk/packages/eternum/src/provider/index.ts @@ -879,12 +879,12 @@ export class EternumProvider extends EnhancedDojoProvider { } public async set_building_general_config(props: SystemProps.SetBuildingGeneralConfigProps) { - const { cost_scale_percent, signer } = props; + const { base_cost_percent_increase, signer } = props; return await this.executeAndCheckTransaction(signer, { contractAddress: getContractByName(this.manifest, `${NAMESPACE}-config_systems`), entrypoint: "set_building_general_config", - calldata: [cost_scale_percent], + calldata: [base_cost_percent_increase], }); } diff --git a/sdk/packages/eternum/src/types/provider.ts b/sdk/packages/eternum/src/types/provider.ts index 7c5aa1d1d..f42d5c76d 100644 --- a/sdk/packages/eternum/src/types/provider.ts +++ b/sdk/packages/eternum/src/types/provider.ts @@ -445,7 +445,7 @@ export interface SetBuildingCategoryPopConfigProps extends SystemSigner { } export interface SetBuildingGeneralConfigProps extends SystemSigner { - cost_scale_percent: num.BigNumberish; + base_cost_percent_increase: num.BigNumberish; } export interface SetPopulationConfigProps extends SystemSigner {