Skip to content

Commit

Permalink
contracts: use quadratic formula for scaling building cost
Browse files Browse the repository at this point in the history
  • Loading branch information
credence0x committed Sep 27, 2024
1 parent fec7fb3 commit 527aac6
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
15 changes: 12 additions & 3 deletions contracts/src/models/buildings.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/models/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ pub struct BuildingGeneralConfig {
#[key]
config_id: ID,
// cost scale percent
cost_scale_percent: u16,
base_cost_percent_increase: u16,
}


Expand Down
6 changes: 3 additions & 3 deletions contracts/src/systems/config/contracts.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -529,10 +529,10 @@ mod config_systems {

#[abi(embed_v0)]
impl BuildingConfigCustomImpl of super::IBuildingConfig<ContractState> {
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(
Expand Down
2 changes: 1 addition & 1 deletion sdk/packages/eternum/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}...`);
Expand Down
2 changes: 1 addition & 1 deletion sdk/packages/eternum/src/constants/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sdk/packages/eternum/src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/packages/eternum/src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 527aac6

Please sign in to comment.