gizgame/strategy
Universal Grand Strategy Engine: Factions, Regions, 5-Stat Officers, Event-Driven Diplomacy (Treaty SSOT + Relation), Influence Maps + 5-Goal GOAP Strategic AI, Declarative Macro Rules, Dual Strategic/Hex Combat, and Multi-Branch Tech Tree. Designed as a pure, orthogonal, zero-duplication module composable into any Gizlang 3D/2D game (Voyage, Three Kingdoms, Studio).
import * as Strategy from "gizgame/strategy";Types
StrategyRegion
type StrategyRegion = { id: i64; nameEn: string; nameKo: string; factionId: i64; lat: f64; lon: f64; economy: i64; industry: i64; defense: i64; troops: i64; morale: i64; connections: Vec<i64>; }A geopolitical region or maritime hub in the strategy graph.
id:i64nameEn:stringnameKo:stringfactionId:i64lat:f64lon:f64economy:i64industry:i64defense:i64troops:i64morale:i64connections:Vec<i64>
StrategyFaction
type StrategyFaction = { id: i64; nameEn: string; nameKo: string; color: Vec<f64>; gold: i64; food: i64; prestige: i64; capitalId: i64; personality: string; isPlayer: bool; }A sovereign faction or maritime empire with personality-weighted strategic AI.
id:i64nameEn:stringnameKo:stringcolor:Vec<f64>gold:i64food:i64prestige:i64capitalId:i64personality:stringisPlayer:bool
StrategyOfficer
type StrategyOfficer = { id: i64; nameEn: string; nameKo: string; factionId: i64; lea: i64; war: i64; intl: i64; pol: i64; cha: i64; loyalty: i64; office: string; }A 5-stat historical officer or council advisor (lea/war/int/pol/cha).
id:i64nameEn:stringnameKo:stringfactionId:i64lea:i64war:i64intl:i64pol:i64cha:i64loyalty:i64office:string
DiplomacyEdge
type DiplomacyEdge = { a: i64; b: i64; treaty: string; relation: i64; expiryTurn: i64; }Diplomatic treaty separating formal treaty state from scalar attitude (-100..+100).
a:i64b:i64treaty:stringrelation:i64expiryTurn:i64
InfluenceCell
type InfluenceCell = { regionId: i64; myInfluence: f64; enemyInfluence: f64; contestedLevel: f64; strategicValue: f64; }Spatial Influence Map metrics computed per region for Strategic AI & GOAP.
regionId:i64myInfluence:f64enemyInfluence:f64contestedLevel:f64strategicValue:f64
TechNode
type TechNode = { id: string; branch: string; nameEn: string; nameKo: string; descEn: string; descKo: string; level: i64; maxLevel: i64; baseCost: i64; bonusPerLv: f64; }A multi-level researchable technology node across military / civil / special branches.
id:stringbranch:stringnameEn:stringnameKo:stringdescEn:stringdescKo:stringlevel:i64maxLevel:i64baseCost:i64bonusPerLv:f64
BattleForce
type BattleForce = { unitType: string; formation: string; troops: i64; morale: i64; officerWar: i64; officerLea: i64; officerInt: i64; techBonus: f64; }Unit force specification for Strategic Auto-Resolve or Axial-Hex Tactical Combat.
unitType:stringformation:stringtroops:i64morale:i64officerWar:i64officerLea:i64officerInt:i64techBonus:f64
BattleResult
type BattleResult = { winner: i64; attackerLosses: i64; defenderLosses: i64; attackerMorale: i64; defenderMorale: i64; summaryEn: string; summaryKo: string; }Outcome of a strategic or tactical battle engagement.
winner:i64attackerLosses:i64defenderLosses:i64attackerMorale:i64defenderMorale:i64summaryEn:stringsummaryKo:string
StrategyWorld
class StrategyWorld { isKo: bool = false; turn: i64 = 1; factions: Vec<StrategyFaction> = []; regions: Vec<StrategyRegion> = []; officers: Vec<StrategyOfficer> = []; diplomacy: Vec<DiplomacyEdge> = []; techs: Vec<TechNode> = []; chronicle: Vec<string> = []; private ruleCooldowns = new Map<string, i64>(); constructor(isKo: bool = false) { this.isKo = isKo; } edgeIndex(a: i64, b: i64): i64 { const lo = Math.minI64(a, b); const hi = Math.maxI64(a, b); for (const [i, e] of this.diplomacy.entries()) { if (e.a === lo && e.b === hi) { return i; } } return -1; } diplomacyOf(a: i64, b: i64): DiplomacyEdge { if (a === b) { return { a, b, treaty: "ally", relation: 100, expiryTurn: 9999 }; } const idx = this.edgeIndex(a, b); if (idx> = 0) { return this.diplomacy[idx]; } const lo = Math.minI64(a, b); const hi = Math.maxI64(a, b); const edge: DiplomacyEdge = { a: lo, b: hi, treaty: "neutral", relation: 0, expiryTurn: 0 }; this.diplomacy.push(edge); return edge; } shiftRelation(a: i64, b: i64, delta: i64): void { if (a === b) { return; } const idx = this.edgeIndex(a, b); if (idx <0) { const lo = Math.minI64(a, b); const hi = Math.maxI64(a, b); this.diplomacy.push({ a: lo, b: hi, treaty: "neutral", relation: Math.maxI64(-100, Math.minI64(100, delta)), expiryTurn: 0 }); return; } let e = this.diplomacy[idx]; e.relation = Math.maxI64(-100, Math.minI64(100, e.relation + delta)); this.diplomacy[idx] = e; } setTreaty(a: i64, b: i64, treaty: string, duration: i64 = 12): void { if (a === b) { return; } void(this.diplomacyOf(a, b)); const idx = this.edgeIndex(a, b); let e = this.diplomacy[idx]; e.treaty = treaty; e.expiryTurn = treaty === "neutral" || treaty === "war" ? 0: this.turn + duration; if (treaty === "ally") { e.relation = Math.maxI64(e.relation + 25, 60); } else if (treaty === "trade") { e.relation = Math.maxI64(e.relation + 15, 35); } else if (treaty === "war") { e.relation = Math.minI64(e.relation - 45, -60); } this.diplomacy[idx] = e; if (treaty === "war") { for (const f of this.factions) { if (f.id === a || f.id === b) { continue; } const withA = this.diplomacyOf(f.id, a); if (withA.treaty === "war") { this.shiftRelation(f.id, b, 15); } if (withA.treaty === "ally") { this.shiftRelation(f.id, b, -15); } } } const fa = this.factions[a]; const fb = this.factions[b]; if (treaty === "ally") { this.record(this.isKo ? `[외교] ${fa.nameKo } ↔ ${ fb.nameKo } 동맹 조약 체결!`: `[Diplomacy] ${fa.nameEn } & ${ fb.nameEn } signed an Alliance!`); } else if (treaty === "trade") { this.record(this.isKo ? `[외교] ${fa.nameKo } ↔ ${ fb.nameKo } 통상 협정 체결!`: `[Diplomacy] ${fa.nameEn } & ${ fb.nameEn } signed a Trade Pact!`); } else if (treaty === "war") { this.record(this.isKo ? `[전쟁] ${fa.nameKo }, ${ fb.nameKo }에 선전포고!`: `[War] ${fa.nameEn } declared war on ${ fb.nameEn }!`); } } record(line: string): void { this.chronicle = [line, ...this.chronicle.slice(0, 23)]; } techBonus(branch: string): f64 { let sum = 0.0; for (const t of this.techs) { if (t.branch === branch) { sum + = f64(t.level)*t.bonusPerLv; } } return sum; } researchTech(id: string, goldAvailable: i64): i64 { for (const [i, t] of this.techs.entries()) { if (t.id !== id) { continue; } if (t.level> = t.maxLevel) { return 0; } const cost = t.baseCost*(t.level + 1); if (goldAvailable <cost) { return 0; } let nt = t; nt.level + = 1; this.techs[i] = nt; this.record(this.isKo ? `[기술] ${nt.nameKo } Lv.${ nt.level } 연구 완료!`: `[Tech] Researched ${nt.nameEn } Lv.${ nt.level }!`); return cost; } return 0; } computeInfluenceMap(factionId: i64): Vec<InfluenceCell> { let cells: Vec<InfluenceCell> = []; for (const r of this.regions) { let myInf = r.factionId === factionId ? f64(r.troops): 0.0; let enInf = r.factionId !== factionId && this.diplomacyOf(factionId, r.factionId).treaty === "war" ? f64(r.troops): 0.0; for (const nid of r.connections) { if (nid <0 || nid> = this.regions.length) { continue; } const nb = this.regions[nid]; if (nb.factionId === factionId) { myInf + = f64(nb.troops)*0.45; } else if (this.diplomacyOf(factionId, nb.factionId).treaty === "war") { enInf + = f64(nb.troops)*0.45; } } const contested = Math.min(myInf, enInf)/Math.max(1.0, myInf + enInf); const value = f64(r.economy*12 + r.industry*10 + r.defense) + contested*60.0; cells.push({ regionId: r.id, myInfluence: myInf, enemyInfluence: enInf, contestedLevel: contested, strategicValue: value }); } return cells; } stepMonth(): void { this.turn + = 1; for (const i of range(0, this.diplomacy.length)) { let e = this.diplomacy[i]; if (e.expiryTurn> 0 && this.turn> = e.expiryTurn) { e.treaty = "neutral"; e.expiryTurn = 0; } if (e.relation> 5) { e.relation - = 1; } else if (e.relation <-5) { e.relation + = 1; } this.diplomacy[i] = e; } let regionCounts: Vec<i64> = []; for (const _f of this.factions) { regionCounts.push(0); } for (const r of this.regions) { if (r.factionId> = 0 && r.factionId <regionCounts.length) { regionCounts[r.factionId] + = 1; } } let hegemon = -1; let maxReg = 0; for (const [fi, cnt] of regionCounts.entries()) { if (cnt> maxReg) { maxReg = cnt; hegemon = fi; } } if (hegemon> = 0 && maxReg*2> = this.regions.length) { for (const f of this.factions) { if (f.id !== hegemon && this.diplomacyOf(f.id, hegemon).treaty !== "ally") { this.shiftRelation(f.id, hegemon, -3); } } } for (const fi of range(0, this.factions.length)) { let fac = this.factions[fi]; if (fac.isPlayer) { continue; } const influence = this.computeInfluenceMap(fac.id); let income = 0; for (const r of this.regions) { if (r.factionId === fac.id) { income + = r.economy*45 + r.industry*25; } } fac.gold + = income; let bestDef = -1; let bestThreat = 0.0; let bestGrow = -1; let lowestEcon = 99; for (const c of influence) { const rg = this.regions[c.regionId]; if (rg.factionId !== fac.id) { continue; } if (c.enemyInfluence> bestThreat) { bestThreat = c.enemyInfluence; bestDef = rg.id; } if (rg.economy <lowestEcon) { lowestEcon = rg.economy; bestGrow = rg.id; } } if (bestDef> = 0 && bestThreat> 50.0 && fac.gold> = 300) { let rg = this.regions[bestDef]; rg.troops + = 30; rg.defense = Math.minI64(100, rg.defense+4); this.regions[bestDef] = rg; fac.gold - = 300; } else if (bestGrow> = 0 && fac.gold> = 400 && this.regions[bestGrow].economy <10) { let rg = this.regions[bestGrow]; rg.economy + = 1; this.regions[bestGrow] = rg; fac.gold - = 400; } this.factions[fi] = fac; } const lastBoom = this.ruleCooldowns.get("trade_boom") ?? -10; if (this.turn - lastBoom> = 3 && this.regions.length> 0) { this.ruleCooldowns.set("trade_boom", this.turn); const rid = (this.turn*7) % this.regions.length; let rg = this.regions[rid]; rg.morale = Math.minI64(100, rg.morale + 6); this.regions[rid] = rg; this.record(this.isKo ? `[세계 동향] ${rg.nameKo } 항구에 무역 호황이 도래했습니다!`: `[World Event] Trade boom arrives in ${rg.nameEn }!`); } } }Orthogonal Grand Strategy Simulation World composable into any Gizlang game.
isKo:boolturn:i64factions:Vec<M248.StrategyFaction>regions:Vec<M248.StrategyRegion>officers:Vec<M248.StrategyOfficer>diplomacy:Vec<M248.DiplomacyEdge>techs:Vec<M248.TechNode>chronicle:Vec<string>ruleCooldowns:_gizlang_sdk_collections.Map<string, i64>
Functions
hexDistance
function hexDistance(q1: i64, r1: i64, q2: i64, r2: i64): i64Axial hex distance (q, r) for tactical hex grid battles.
Effects: fail.
unitAdvantage
function unitAdvantage(attacker: string, defender: string): f64Rock-paper-scissors unit advantage multiplier across land and naval arms.
formationMods
function formationMods(formation: string): Vec<f64>Formation attack/defense modifier pair [atkMult, defMult].
Effects: fail, alloc.
resolveStrategicBattle
function resolveStrategicBattle(atk: BattleForce, def: BattleForce): BattleResultResolves a strategic engagement using unit counters, formations, officer 5-stats, morale breaks, and tech bonuses.
Effects: fail, alloc.
constructor
constructor(isKo: bool = false)Effects: fail, read, write, alloc.
createMaritimeStrategy
function createMaritimeStrategy(isKo: bool, ports: Vec<{ id: i64; nameEn: string; nameKo: string; nation: string; lat: f64; lon: f64; economy: i64; industry: i64; }>): StrategyWorldBuilds the canonical 16th-Century Maritime Grand Strategy world (8 powers, 18 Earth ports, 6 researchable techs).
Effects: fail, read, write, alloc.
Methods
StrategyWorld.edgeIndex
edgeIndex(a: i64, b: i64): i64Looks up the canonical DiplomacyEdge between factions A and B (order-independent SSOT).
Effects: fail, read, alloc.
StrategyWorld.diplomacyOf
diplomacyOf(a: i64, b: i64): DiplomacyEdgeReturns the diplomatic state between factions A and B.
Effects: fail, read, write, alloc.
StrategyWorld.shiftRelation
shiftRelation(a: i64, b: i64, delta: i64): voidAdjusts scalar diplomatic attitude between factions A and B, clamped to [-100, +100].
Effects: fail, read, write, alloc.
StrategyWorld.setTreaty
setTreaty(a: i64, b: i64, treaty: string, duration: i64 = 12): voidSets formal treaty state between factions A and B and runs emergent geopolitical hooks.
Effects: fail, read, write, alloc.
StrategyWorld.record
record(line: string): voidAppends an event line to the historical chronicle (capped to latest 24 entries).
Effects: fail, read, write, alloc.
StrategyWorld.techBonus
techBonus(branch: string): f64Returns the sum of active technology bonuses for BRANCH ("military" | "civil" | "special").
Effects: fail, read.
StrategyWorld.researchTech
researchTech(id: string, goldAvailable: i64): i64Researches next level of technology ID if player has sufficient gold. Returns cost paid (0 if unable).
Effects: fail, read, write, alloc.
StrategyWorld.computeInfluenceMap
computeInfluenceMap(factionId: i64): Vec<InfluenceCell>Computes a spatial Influence Map across all regions for FACTION_ID.
Effects: fail, read, write, alloc.
StrategyWorld.stepMonth
stepMonth(): voidRuns one monthly Grand Strategy cycle: treaty expiry, Hegemon Balancing, GOAP Strategic AI, and Macro Rule Engine!
Effects: fail, read, write, alloc.