Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh/modular config #1139

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion client/src/dojo/contractComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,23 @@ export function defineContractComponents(world: World) {
},
);
})(),
TravelStaminaCostConfig: (() => {
return defineComponent(
world,
{
config_id: RecsType.Number,
travel_type: RecsType.Number,
cost: RecsType.Number,
},
{
metadata: {
name: "eternum-TravelStaminaCostConfig",
types: ["u32", "u8", "u16"],
customTypes: [],
},
},
);
})(),
TroopConfig: (() => {
return defineComponent(
world,
Expand All @@ -1183,12 +1200,13 @@ export function defineContractComponents(world: World) {
army_extra_per_building: RecsType.Number,
battle_leave_slash_num: RecsType.Number,
battle_leave_slash_denom: RecsType.Number,
max_troop_count: RecsType.Number,
},
{
metadata: {
namespace: "eternum",
name: "TroopConfig",
types: ["u32", "u32", "u8", "u8", "u16", "u16", "u16", "u8", "u8", "u8", "u8", "u8"],
types: ["u32", "u32", "u8", "u8", "u16", "u16", "u16", "u8", "u8", "u8", "u8", "u8", "u64"],
customTypes: [],
},
},
Expand Down
31 changes: 23 additions & 8 deletions client/src/dojo/modelManager/ArmyMovementManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { FELT_CENTER } from "@/ui/config";
import { getEntityIdFromKeys } from "@/ui/utils/utils";
import {
ContractAddress,
EternumGlobalConfig,
ID,
ResourcesIds,
TravelTypes,
getNeighborHexes,
neighborOffsetsEven,
neighborOffsetsOdd,
} from "@bibliothecadao/eternum";
import { Entity, getComponentValue } from "@dojoengine/recs";
import { uuid } from "@latticexyz/utils";
import { SetupResult } from "../setup";
import { ClientConfigManager } from "./ClientConfigManager";
import { ProductionManager } from "./ProductionManager";
import { StaminaManager } from "./StaminaManager";
import { getRemainingCapacity } from "./utils/ArmyMovementUtils";
Expand Down Expand Up @@ -88,30 +89,38 @@ export class ArmyMovementManager {
}

private _canExplore(currentDefaultTick: number, currentArmiesTick: number): boolean {
const config = ClientConfigManager.instance();
const exploreCost = config.getTravelStaminaCost(TravelTypes.Explore);
const exploreFishCost = config.getExploreResourceCost(ResourcesIds.Fish);
const exploreWheatCost = config.getExploreResourceCost(ResourcesIds.Wheat);
const exploreReward = config.getExploreReward();

const stamina = this.staminaManager.getStamina(currentArmiesTick);

if (stamina.amount < EternumGlobalConfig.stamina.exploreCost) {
if (stamina.amount < exploreCost) {
return false;
}
const { wheat, fish } = this.getFood(currentDefaultTick);

if (fish < EternumGlobalConfig.exploration.fishBurn) {
if (fish < exploreFishCost) {
return false;
}
if (wheat < EternumGlobalConfig.exploration.wheatBurn) {
if (wheat < exploreWheatCost) {
return false;
}

if (this._getArmyRemainingCapacity() < EternumGlobalConfig.exploration.reward) {
if (this._getArmyRemainingCapacity() < exploreReward) {
return false;
}

return true;
}

private _calculateMaxTravelPossible = (currentArmiesTick: number) => {
const config = ClientConfigManager.instance();
const travelCost = config.getTravelStaminaCost(TravelTypes.Travel);
const stamina = this.staminaManager.getStamina(currentArmiesTick);
return Math.floor((stamina.amount || 0) / EternumGlobalConfig.stamina.travelCost);
return Math.floor((stamina.amount || 0) / travelCost);
};

private _getCurrentPosition = () => {
Expand Down Expand Up @@ -229,9 +238,12 @@ export class ArmyMovementManager {
};

private _optimisticExplore = (col: number, row: number, currentArmiesTick: number) => {
const config = ClientConfigManager.instance();
const exploreCost = config.getTravelStaminaCost(TravelTypes.Explore);

let overrideId = uuid();

this._optimisticStaminaUpdate(overrideId, EternumGlobalConfig.stamina.exploreCost, currentArmiesTick);
this._optimisticStaminaUpdate(overrideId, exploreCost, currentArmiesTick);
this._optimisticTileUpdate(overrideId, col, row);
this._optimisticPositionUpdate(overrideId, col, row);

Expand Down Expand Up @@ -274,9 +286,12 @@ export class ArmyMovementManager {
};

private _optimisticTravelHex = (col: number, row: number, pathLength: number, currentArmiesTick: number) => {
const config = ClientConfigManager.instance();
const travelCost = config.getTravelStaminaCost(TravelTypes.Travel);

let overrideId = uuid();

this._optimisticStaminaUpdate(overrideId, EternumGlobalConfig.stamina.travelCost * pathLength, currentArmiesTick);
this._optimisticStaminaUpdate(overrideId, travelCost * pathLength, currentArmiesTick);

this.setup.components.Position.addOverride(overrideId, {
entity: this.entity,
Expand Down
14 changes: 8 additions & 6 deletions client/src/dojo/modelManager/BattleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { DojoResult } from "@/hooks/context/DojoContext";
import { ArmyInfo } from "@/hooks/helpers/useArmies";
import { Structure } from "@/hooks/helpers/useStructures";
import { Health } from "@/types";
import { BattleSide, EternumGlobalConfig, ID } from "@bibliothecadao/eternum";
import { BattleSide, ID, RESOURCE_PRECISION, TROOP_HEALTH_PRECISION } from "@bibliothecadao/eternum";
import { ComponentValue, Components, Has, HasValue, getComponentValue, runQuery } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { ClientComponents } from "../createClientComponents";
import { ClientConfigManager } from "./ClientConfigManager";
import { StaminaManager } from "./StaminaManager";

export enum BattleType {
Expand Down Expand Up @@ -324,16 +325,17 @@ export class BattleManager {
}

private getTroopFullHealth(troops: ComponentValue<ClientComponents["Army"]["schema"]["troops"]>): bigint {
const health = EternumGlobalConfig.troop.health;
const config = ClientConfigManager.instance();
const troopConfig = config.getTroopConfig();

let total_knight_health = health * Number(troops.knight_count);
let total_paladin_health = health * Number(troops.paladin_count);
let total_crossbowman_health = health * Number(troops.crossbowman_count);
let total_knight_health = troopConfig.health * Number(troops.knight_count);
let total_paladin_health = troopConfig.health * Number(troops.paladin_count);
let total_crossbowman_health = troopConfig.health * Number(troops.crossbowman_count);

return BigInt(
Math.floor(
(total_knight_health + total_paladin_health + total_crossbowman_health) /
(EternumGlobalConfig.resources.resourceMultiplier * Number(EternumGlobalConfig.troop.healthPrecision)),
(RESOURCE_PRECISION * Number(TROOP_HEALTH_PRECISION)),
),
);
}
Expand Down
Loading
Loading