From f4959adf7af7d391feb42f2194c541cd6d86bf83 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Wed, 5 Oct 2022 01:17:26 +0300 Subject: [PATCH] feat: getUnitModifiers --- src/config/military.ts | 25 +++++++++---------- .../pack/military/generateMilitary.ts | 4 +-- .../pack/military/getUnitModifiers.ts | 25 +++++++++++-------- src/types/globals.d.ts | 2 +- src/types/pack/states.d.ts | 6 ++--- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/config/military.ts b/src/config/military.ts index 43649bb2..c9e6e804 100644 --- a/src/config/military.ts +++ b/src/config/military.ts @@ -1,4 +1,4 @@ -export const getDefaultMilitaryOptions: () => IMilitaryOption[] = function () { +export const getDefaultMilitaryOptions: () => IMilitaryUnit[] = function () { return [ {icon: "⚔️", name: "infantry", rural: 0.25, urban: 0.2, crew: 1, power: 1, type: "melee", separate: 0}, {icon: "🏹", name: "archers", rural: 0.12, urban: 0.2, crew: 1, power: 1, type: "ranged", separate: 0}, @@ -21,19 +21,18 @@ export const relationsAlertRate: {[key in TRelation]: number} = { Enemy: 1 }; -type TCulture = Exclude; -export const stateModifier: {[key in TMilitaryType]: {[key in TCulture]: number}} = { - melee: {Nomadic: 0.5, Highland: 1.2, Lake: 1, Naval: 0.7, Hunting: 1.2, River: 1.1}, - ranged: {Nomadic: 0.9, Highland: 1.3, Lake: 1, Naval: 0.8, Hunting: 2, River: 0.8}, - mounted: {Nomadic: 2.3, Highland: 0.6, Lake: 0.7, Naval: 0.3, Hunting: 0.7, River: 0.8}, - machinery: {Nomadic: 0.8, Highland: 1.4, Lake: 1.1, Naval: 1.4, Hunting: 0.4, River: 1.1}, - naval: {Nomadic: 0.5, Highland: 0.5, Lake: 1.2, Naval: 1.8, Hunting: 0.7, River: 1.2}, - armored: {Nomadic: 1, Highland: 0.5, Lake: 1, Naval: 1, Hunting: 0.7, River: 1.1}, - aviation: {Nomadic: 0.5, Highland: 0.5, Lake: 1.2, Naval: 1.2, Hunting: 0.6, River: 1.2}, - magical: {Nomadic: 1, Highland: 2, Lake: 1, Naval: 1, Hunting: 1, River: 1} +export const stateModifier: {[key in TMilitaryUnitType]: {[key in TCultureType]: number}} = { + melee: {Generic: 1, Nomadic: 0.5, Highland: 1.2, Lake: 1, Naval: 0.7, Hunting: 1.2, River: 1.1}, + ranged: {Generic: 1, Nomadic: 0.9, Highland: 1.3, Lake: 1, Naval: 0.8, Hunting: 2, River: 0.8}, + mounted: {Generic: 1, Nomadic: 2.3, Highland: 0.6, Lake: 0.7, Naval: 0.3, Hunting: 0.7, River: 0.8}, + machinery: {Generic: 1, Nomadic: 0.8, Highland: 1.4, Lake: 1.1, Naval: 1.4, Hunting: 0.4, River: 1.1}, + naval: {Generic: 1, Nomadic: 0.5, Highland: 0.5, Lake: 1.2, Naval: 1.8, Hunting: 0.7, River: 1.2}, + armored: {Generic: 1, Nomadic: 1, Highland: 0.5, Lake: 1, Naval: 1, Hunting: 0.7, River: 1.1}, + aviation: {Generic: 1, Nomadic: 0.5, Highland: 0.5, Lake: 1.2, Naval: 1.2, Hunting: 0.6, River: 1.2}, + magical: {Generic: 1, Nomadic: 1, Highland: 2, Lake: 1, Naval: 1, Hunting: 1, River: 1} }; -export const cellTypeModifier: {[key: string]: {[key in TMilitaryType]: number}} = { +export const cellTypeModifier: {[key: string]: {[key in TMilitaryUnitType]: number}} = { nomadic: { melee: 0.2, ranged: 0.5, @@ -66,7 +65,7 @@ export const cellTypeModifier: {[key: string]: {[key in TMilitaryType]: number}} } }; -export const burgTypeModifier: {[key: string]: {[key in TMilitaryType]: number}} = { +export const burgTypeModifier: {[key: string]: {[key in TMilitaryUnitType]: number}} = { nomadic: { melee: 0.3, ranged: 0.8, diff --git a/src/scripts/generation/pack/military/generateMilitary.ts b/src/scripts/generation/pack/military/generateMilitary.ts index eb45a2b6..63cda4e7 100644 --- a/src/scripts/generation/pack/military/generateMilitary.ts +++ b/src/scripts/generation/pack/military/generateMilitary.ts @@ -7,9 +7,9 @@ export function generateMilitary(states: TStates) { if (!options.military) options.military = getDefaultMilitaryOptions(); - // const unitModifiers = getUnitModifiers(states); + const unitModifiers = getUnitModifiers(states); - console.log(states); + console.log({states, unitModifiers}); TIME && console.timeEnd("generateMilitaryForces"); } diff --git a/src/scripts/generation/pack/military/getUnitModifiers.ts b/src/scripts/generation/pack/military/getUnitModifiers.ts index fed50616..14de6fc4 100644 --- a/src/scripts/generation/pack/military/getUnitModifiers.ts +++ b/src/scripts/generation/pack/military/getUnitModifiers.ts @@ -3,19 +3,22 @@ import {isState} from "utils/typeUtils"; // calculate overall state modifiers for unit types based on state features export function getUnitModifiers(states: TStates) { - const validStates = states.filter(isState); + return states.map(state => { + if (!isState(state)) return {}; - for (const state of validStates) { - const military = {platoons: []}; - const {i: stateId, relations, expansionism, area, neighbors, alert} = state; + const unitModifiers: Dict = {}; + const {type: stateType, formName, form, alert} = state; - for (const unit of options.military) { - if (!stateModifier[unit.type]) continue; + for (const {type, name} of options.military) { + if (!stateModifier[type]) continue; - let modifier = stateModifier[unit.type][s.type] || 1; - if (unit.type === "mounted" && s.formName.includes("Horde")) modifier *= 2; - else if (unit.type === "naval" && s.form === "Republic") modifier *= 1.2; - military[unit.name] = modifier * alert; + let modifier = stateModifier[type][stateType] || 1; + if (type === "mounted" && formName.includes("Horde")) modifier *= 2; + else if (type === "naval" && form === "Republic") modifier *= 1.2; + + unitModifiers[name] = modifier * alert; } - } + + return unitModifiers; + }); } diff --git a/src/types/globals.d.ts b/src/types/globals.d.ts index 8c8ea66f..a1d42c50 100644 --- a/src/types/globals.d.ts +++ b/src/types/globals.d.ts @@ -26,7 +26,7 @@ interface IOptions { winds: [number, number, number, number, number, number]; stateLabelsMode: "auto" | "short" | "full"; year: number; - military: IMilitaryOption[]; + military: IMilitaryUnit[]; } declare let populationRate: number; diff --git a/src/types/pack/states.d.ts b/src/types/pack/states.d.ts index 647da3d7..4149f836 100644 --- a/src/types/pack/states.d.ts +++ b/src/types/pack/states.d.ts @@ -53,15 +53,15 @@ type TRelation = | "Enemy" | "x"; -interface IMilitaryOption { +interface IMilitaryUnit { name: string; icon: string; crew: number; power: number; rural: number; urban: number; - type: TMilitaryType; + type: TMilitaryUnitType; separate: Logical; } -type TMilitaryType = "melee" | "ranged" | "mounted" | "machinery" | "naval" | "armored" | "aviation" | "magical"; +type TMilitaryUnitType = "melee" | "ranged" | "mounted" | "machinery" | "naval" | "armored" | "aviation" | "magical";