Skip to content

Commit eb29479

Browse files
committed
Change Team from enum to struct
1 parent d2e1247 commit eb29479

File tree

4 files changed

+59
-70
lines changed

4 files changed

+59
-70
lines changed

src/logic/vm/instructions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl Print {
414414
}
415415
LValue::String(string) => Cow::Borrowed(string),
416416
LValue::Content(content) => Cow::Borrowed(content.name()),
417-
LValue::Team(team) => Cow::from(team.as_ref()),
417+
LValue::Team(team) => Cow::from(team.name()),
418418
LValue::Building(position) => vm
419419
.building(*position)
420420
.map(|b| Cow::Borrowed(b.block.name.as_str()))
@@ -613,8 +613,8 @@ impl SimpleInstruction for Sensor {
613613
},
614614

615615
LValue::Team(team) => match sensor {
616-
Name => LString::Static(team.into()).into(),
617-
Id => team.id().into(),
616+
Name => LString::Static(team.name()).into(),
617+
Id => team.0.into(),
618618
Color => team.color().into(),
619619
_ => LValue::Null,
620620
},
@@ -626,7 +626,7 @@ impl SimpleInstruction for Sensor {
626626
Y => building.position.y.into(),
627627
Color => colors::TEAM_SHARDED.into(),
628628
Dead => false.into(),
629-
Team => crate::types::Team::Sharded.id().into(),
629+
Team => crate::types::Team::SHARDED.0.into(),
630630
Efficiency => 1.into(),
631631
Timescale => 1.into(),
632632
Range => building.block.range.into(),
@@ -853,7 +853,7 @@ impl SimpleInstruction for Lookup {
853853
.map(|v| Content::Unit(v))
854854
.into(),
855855

856-
ContentType::Team => id.try_into().ok().map(Team::from_id).into(),
856+
ContentType::Team => id.try_into().ok().map(Team).into(),
857857

858858
_ => LValue::Null,
859859
};

src/logic/vm/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,11 +2475,8 @@ mod tests {
24752475
// teams
24762476

24772477
assert_eq!(variables["team1"].get(&state), LValue::Null);
2478-
assert_eq!(variables["team2"].get(&state), LValue::Team(Team::Derelict));
2479-
assert_eq!(
2480-
variables["team3"].get(&state),
2481-
LValue::Team(Team::Unknown(255))
2482-
);
2478+
assert_eq!(variables["team2"].get(&state), LValue::Team(Team::DERELICT));
2479+
assert_eq!(variables["team3"].get(&state), LValue::Team(Team(255)));
24832480
assert_eq!(variables["team4"].get(&state), LValue::Null);
24842481
}
24852482

src/logic/vm/variables.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ impl LVar {
7979
"@unitCount": constant(content::units::FROM_LOGIC_ID.len()),
8080
};
8181

82-
globals.extend(Team::base_teams().map(|t| named_constant(t, t)));
82+
globals.extend(
83+
Team::BASE_TEAMS
84+
.iter()
85+
.map(|&t| named_constant(t.name(), t)),
86+
);
8387

8488
globals.extend(
8589
content::items::VALUES

src/types/enums.rs

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
use std::hash::Hash;
44

55
use binrw::prelude::*;
6+
use lazy_static::lazy_static;
67
use strum_macros::{AsRefStr, IntoStaticStr, VariantArray};
8+
use velcro::vec;
79

810
use super::colors;
911

@@ -118,74 +120,60 @@ pub enum LAccess {
118120
Color,
119121
}
120122

123+
lazy_static! {
124+
static ref TEAM_NAMES: Vec<&'static str> = vec![
125+
"derelict",
126+
"sharded",
127+
"crux",
128+
"malis",
129+
"green",
130+
"blue",
131+
"neoplastic",
132+
..(Team::BASE_TEAMS.len()..256).map(|i| -> &'static str { format!("team#{i}").leak() }),
133+
];
134+
}
135+
136+
const TEAM_COLORS: &[f64] = &[
137+
colors::TEAM_DERELICT_F64,
138+
colors::TEAM_SHARDED_F64,
139+
colors::TEAM_CRUX_F64,
140+
colors::TEAM_MALIS_F64,
141+
colors::TEAM_GREEN_F64,
142+
colors::TEAM_BLUE_F64,
143+
colors::TEAM_NEOPLASTIC_F64,
144+
// TODO: unnamed teams
145+
];
146+
121147
#[binrw]
122148
#[brw(big)]
123-
#[derive(
124-
Debug, Clone, Copy, PartialEq, Eq, Hash, AsRefStr, IntoStaticStr, strum_macros::Display,
125-
)]
126-
#[strum(serialize_all = "camelCase")]
127-
pub enum Team {
128-
#[brw(magic = 0u8)]
129-
Derelict,
130-
#[brw(magic = 1u8)]
131-
Sharded,
132-
#[brw(magic = 2u8)]
133-
Crux,
134-
#[brw(magic = 3u8)]
135-
Malis,
136-
#[brw(magic = 4u8)]
137-
Green,
138-
#[brw(magic = 5u8)]
139-
Blue,
140-
#[brw(magic = 6u8)]
141-
Neoplastic,
142-
#[strum(to_string = "team#{0}")]
143-
Unknown(u8),
144-
}
149+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
150+
pub struct Team(pub u8);
145151

146152
impl Team {
147-
pub fn base_teams() -> impl Iterator<Item = Self> {
148-
(0u8..=6u8).map(Self::from_id)
149-
}
153+
pub const DERELICT: Self = Self(0);
154+
pub const SHARDED: Self = Self(1);
155+
pub const CRUX: Self = Self(2);
156+
pub const MALIS: Self = Self(3);
157+
pub const GREEN: Self = Self(4);
158+
pub const BLUE: Self = Self(5);
159+
pub const NEOPLASTIC: Self = Self(6);
150160

151-
pub fn from_id(id: u8) -> Self {
152-
match id {
153-
0 => Self::Derelict,
154-
1 => Self::Sharded,
155-
2 => Self::Crux,
156-
3 => Self::Malis,
157-
4 => Self::Green,
158-
5 => Self::Blue,
159-
6 => Self::Neoplastic,
160-
_ => Self::Unknown(id),
161-
}
162-
}
161+
pub const BASE_TEAMS: &[Self] = &[
162+
Self::DERELICT,
163+
Self::SHARDED,
164+
Self::CRUX,
165+
Self::MALIS,
166+
Self::GREEN,
167+
Self::BLUE,
168+
Self::NEOPLASTIC,
169+
];
163170

164-
pub fn id(&self) -> u8 {
165-
match self {
166-
Self::Derelict => 0,
167-
Self::Sharded => 1,
168-
Self::Crux => 2,
169-
Self::Malis => 3,
170-
Self::Green => 4,
171-
Self::Blue => 5,
172-
Self::Neoplastic => 6,
173-
Self::Unknown(id) => *id,
174-
}
171+
pub fn name(&self) -> &'static str {
172+
TEAM_NAMES[self.0 as usize]
175173
}
176174

177175
pub fn color(&self) -> f64 {
178-
match self {
179-
Self::Derelict => colors::TEAM_DERELICT_F64,
180-
Self::Sharded => colors::TEAM_SHARDED_F64,
181-
Self::Crux => colors::TEAM_CRUX_F64,
182-
Self::Malis => colors::TEAM_MALIS_F64,
183-
Self::Green => colors::TEAM_GREEN_F64,
184-
Self::Blue => colors::TEAM_BLUE_F64,
185-
Self::Neoplastic => colors::TEAM_NEOPLASTIC_F64,
186-
// TODO: implement
187-
Self::Unknown(_) => 0.,
188-
}
176+
TEAM_COLORS.get(self.0 as usize).copied().unwrap_or(0.)
189177
}
190178
}
191179

0 commit comments

Comments
 (0)