#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use shortestpath::Gradient;
use crate::Color;
const FLOAT_PRECISION: f32 = 1e-6;
pub const MIN_GRADIENT_INCREMENT: f32 = 1.0;
pub fn min_gradient_increment_from_steps_count(steps_count: u64) -> f32 {
if steps_count.is_multiple_of(37) {
17.0
} else if steps_count.is_multiple_of(13) {
11.0
} else if steps_count.is_multiple_of(7) {
3.0
} else {
MIN_GRADIENT_INCREMENT
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Team {
gradient: Gradient,
color: Color,
name: String,
fighter_count: usize,
average_health: f32,
gradient_increment: f32,
}
impl PartialEq for Team {
fn eq(&self, other: &Self) -> bool {
self.gradient == other.gradient
&& self.color == other.color
&& self.name == other.name
&& self.fighter_count == other.fighter_count
&& (self.average_health - other.average_health).abs() < FLOAT_PRECISION
&& (self.gradient_increment - other.gradient_increment).abs() < FLOAT_PRECISION
}
}
impl Eq for Team {}
impl Team {
pub fn new(gradient: Gradient, color: Color, name: String) -> Self {
Self {
gradient,
color,
name,
fighter_count: 0,
average_health: 0.0,
gradient_increment: MIN_GRADIENT_INCREMENT,
}
}
pub fn gradient(&self) -> &Gradient {
&self.gradient
}
pub fn gradient_mut(&mut self) -> &mut Gradient {
&mut self.gradient
}
pub fn color(&self) -> &Color {
&self.color
}
pub fn set_color(&mut self, color: Color) {
self.color = color;
}
pub fn name(&self) -> &str {
&self.name
}
pub fn set_name(&mut self, name: String) {
self.name = name;
}
pub fn fighter_count(&self) -> usize {
self.fighter_count
}
pub fn set_fighter_count(&mut self, fighter_count: usize) {
self.fighter_count = fighter_count;
}
pub fn average_health(&self) -> f32 {
self.average_health
}
pub fn set_average_health(&mut self, average_health: f32) {
self.average_health = average_health;
}
pub fn gradient_increment(&self) -> f32 {
self.gradient_increment
}
pub fn set_gradient_increment(&mut self, gradient_increment: f32) {
self.gradient_increment = gradient_increment;
}
}
impl std::fmt::Display for Team {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Team \"{}\" ({}, {} fighters)",
self.name, self.color, self.fighter_count
)
}
}