feat(m2): content-driven ruleset, balance cli and tuning findings

This commit is contained in:
2026-10-24 22:43:58 +02:00
parent e371c72a35
commit e77595eaf0
18 changed files with 844 additions and 120 deletions
+1
View File
@@ -14,5 +14,6 @@ default = []
serde = ["dep:serde", "dep:serde_json"]
[dependencies]
content.workspace = true
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
+5 -1
View File
@@ -23,5 +23,9 @@ fn main() {
sim.tick();
}
println!("seed={seed} sim_version={} hash={}", sim::SIM_VERSION, sim.state_hash());
println!(
"seed={seed} sim_version={} hash={}",
sim::SIM_VERSION,
sim.state_hash()
);
}
-1
View File
@@ -19,7 +19,6 @@ pub mod rng;
pub mod state;
pub mod terrain;
pub mod trig_table;
pub mod weapons;
/// Version of the simulation. Bumped on any formula/balance change.
pub const SIM_VERSION: &str = "0.1.0";
+12 -4
View File
@@ -102,7 +102,7 @@ pub struct ReplayV1 {
impl ReplayV1 {
/// Builds a replay from a finished (or in-flight) simulation.
pub fn capture(sim: &Sim, content_hash: &str, mode: &str, duration_ticks: u32) -> Self {
pub fn capture(sim: &Sim, mode: &str, duration_ticks: u32) -> Self {
let commands = sim
.recorded_commands()
.iter()
@@ -129,7 +129,7 @@ impl ReplayV1 {
Self {
v: 1,
sim_version: SIM_VERSION.to_string(),
content_hash: content_hash.to_string(),
content_hash: sim.content.hash(),
seed: sim.seed,
mode: mode.to_string(),
players,
@@ -167,7 +167,15 @@ impl ReplayV1 {
loadout: self.players[1].loadout,
},
];
let mut sim = Sim::new(self.seed, specs);
let content = content::Content::builtin();
if content.hash() != self.content_hash {
return Err(format!(
"content mismatch: replay {} vs server {}",
self.content_hash,
content.hash()
));
}
let mut sim = Sim::new(self.seed, specs, content);
let mut projected = 0u8;
for (i, rec) in self.commands.iter().enumerate() {
@@ -208,7 +216,7 @@ mod tests {
for _ in 0..300 {
sim.tick();
}
ReplayV1::capture(&sim, "b3:test", "hotseat", sim.tick)
ReplayV1::capture(&sim, "hotseat", sim.tick)
}
#[test]
+40 -10
View File
@@ -6,8 +6,8 @@ use crate::fixed::{self, from_int, from_ratio, mul, to_int, Fx};
use crate::hash::Hasher;
use crate::rng::Pcg32;
use crate::terrain::{Terrain, WORLD_H, WORLD_W};
use crate::weapons;
use crate::TICK_HZ;
use content::Content;
/// Maximum hit points per tank.
pub const MAX_HP: i32 = 100;
@@ -22,7 +22,7 @@ pub const SUDDEN_DEATH_TURN: u16 = 20;
/// Gravity in px/tick^2.
pub const GRAVITY: Fx = from_ratio(2, 10);
/// Wind acceleration per wind unit in px/tick^2.
pub const WIND_SCALE: Fx = from_ratio(2, 100);
pub const WIND_SCALE: Fx = from_ratio(4, 1000);
/// Muzzle offset above the tank in px.
pub const MUZZLE_LEN: i32 = 16;
/// Shield duration in ticks (2 turns).
@@ -114,11 +114,11 @@ pub struct Tank {
}
impl Tank {
fn new(spec: TankSpec, terrain: &Terrain) -> Self {
fn new(spec: TankSpec, terrain: &Terrain, content: &Content) -> Self {
let x = from_int(spec.x);
let mut ammo = [0u8; 3];
for (i, id) in spec.loadout.iter().enumerate() {
ammo[i] = weapons::get(*id).map(|w| w.ammo).unwrap_or(0);
ammo[i] = content.weapon(*id).map(|w| w.ammo).unwrap_or(0);
}
Self {
x,
@@ -165,6 +165,10 @@ pub struct Sim {
pub winner: Option<u8>,
pub lava_y: i32,
pub seed: u64,
/// Ruleset this match runs on.
pub content: Content,
/// Hash of the ruleset (part of the state hash).
pub content_hash: u64,
rng: Pcg32,
log: Vec<(u32, Command)>,
pending_end: bool,
@@ -172,11 +176,20 @@ pub struct Sim {
impl Sim {
/// Creates a match. Player 0 starts left, player 1 right.
pub fn new(seed: u64, specs: [TankSpec; 2]) -> Self {
pub fn new(seed: u64, specs: [TankSpec; 2], content: Content) -> Self {
Self::with_starter(seed, specs, content, 0)
}
/// Creates a match with an explicit starting player (0 or 1).
pub fn with_starter(seed: u64, specs: [TankSpec; 2], content: Content, starter: u8) -> Self {
let mut terrain = Terrain::generate(seed);
terrain.flatten_under(from_int(specs[0].x), 48);
terrain.flatten_under(from_int(specs[1].x), 48);
let tanks = [Tank::new(specs[0], &terrain), Tank::new(specs[1], &terrain)];
let tanks = [
Tank::new(specs[0], &terrain, &content),
Tank::new(specs[1], &terrain, &content),
];
let content_hash = content.hash_u64();
let mut rng = Pcg32::new(seed, 11);
let wind = (rng.below(17) as i32) - 8;
Self {
@@ -187,11 +200,13 @@ impl Sim {
tick: 0,
turn: 1,
phase: Phase::Aiming,
current: 0,
current: starter % 2,
turn_ticks_left: TURN_TICKS,
winner: None,
lava_y: WORLD_H + 200,
seed,
content,
content_hash,
rng,
log: Vec::new(),
pending_end: false,
@@ -212,6 +227,7 @@ impl Sim {
loadout: [1, 3, 4],
},
],
Content::builtin(),
)
}
@@ -251,7 +267,11 @@ impl Sim {
if power < from_ratio(5, 100) || power > from_int(1) {
return Err(SimError::BadPower);
}
let w = weapons::get(weapon).ok_or(SimError::UnknownWeapon)?;
let w = self
.content
.weapon(weapon)
.ok_or(SimError::UnknownWeapon)?
.clone();
let slot = self.tanks[self.current as usize]
.slot_of(weapon)
.ok_or(SimError::WeaponNotInLoadout)?;
@@ -348,7 +368,7 @@ impl Sim {
}
fn resolve_impact(&mut self, x: Fx, y: Fx, p: &Projectile, events: &mut Vec<Event>) {
let Some(w) = weapons::get(p.weapon) else {
let Some(w) = self.content.weapon(p.weapon).cloned() else {
self.end_turn(events);
return;
};
@@ -364,7 +384,8 @@ impl Sim {
if d >= radius {
continue;
}
let falloff = from_int(1) - fixed::div(d, radius);
// Near misses keep 25% damage so the game does not turn into pixel hunting.
let falloff = (from_int(1) - fixed::div(d, radius)).max(from_ratio(25, 100));
let mut dmg = ((w.damage as i64) * (falloff as i64)) >> 16;
dmg *= multiplier;
if tank.shield_ticks > 0 {
@@ -376,6 +397,14 @@ impl Sim {
}
}
// Tanks follow the terrain: if the ground under them was removed, they drop.
for tank in &mut self.tanks {
let surface = self.terrain.surface_y(tank.x);
if surface > tank.y {
tank.y = surface;
}
}
events.push(Event::Impact {
x,
y,
@@ -459,6 +488,7 @@ impl Sim {
});
h.i32(self.wind);
h.i32(self.lava_y);
h.u64(self.content_hash);
for tank in &self.tanks {
h.i32(tank.x);
h.i32(tank.y);
-83
View File
@@ -1,83 +0,0 @@
//! Weapon definitions (data table). Content-driven loading arrives at M2.
/// Static description of a weapon.
#[derive(Clone, Copy, Debug)]
pub struct WeaponDef {
/// Stable id used in replays.
pub id: u8,
/// Display name (English, localised by the client).
pub name: &'static str,
/// Base damage at the impact centre.
pub damage: i32,
/// Blast radius in pixels.
pub radius_px: i32,
/// Ammo per match (`255` means unlimited).
pub ammo: u8,
/// Muzzle speed in px/tick at full power.
pub speed: i32,
/// True if the terrain is dug instead of blasted.
pub digs: bool,
}
/// MVP-1 arsenal.
pub const WEAPONS: [WeaponDef; 6] = [
WeaponDef {
id: 1,
name: "Baby Shell",
damage: 22,
radius_px: 34,
ammo: 255,
speed: 20,
digs: false,
},
WeaponDef {
id: 2,
name: "Heavy Shell",
damage: 34,
radius_px: 48,
ammo: 3,
speed: 15,
digs: false,
},
WeaponDef {
id: 3,
name: "Roller",
damage: 26,
radius_px: 40,
ammo: 2,
speed: 18,
digs: false,
},
WeaponDef {
id: 4,
name: "Digger",
damage: 14,
radius_px: 30,
ammo: 2,
speed: 18,
digs: true,
},
WeaponDef {
id: 5,
name: "Shield",
damage: 0,
radius_px: 0,
ammo: 2,
speed: 0,
digs: false,
},
WeaponDef {
id: 6,
name: "MIRV",
damage: 18,
radius_px: 24,
ammo: 2,
speed: 19,
digs: false,
},
];
/// Looks up a weapon by id.
pub fn get(id: u8) -> Option<&'static WeaponDef> {
WEAPONS.iter().find(|w| w.id == id)
}