little cleanup + new strat
This commit is contained in:
@@ -27,41 +27,74 @@ impl Default for Base {
|
||||
}
|
||||
|
||||
impl Base {
|
||||
pub fn population_in_n_ticks(&self, ticks: u32, config: &GameConfig, attacks: &Vec<BoardAction>) -> u32 {
|
||||
// get base population from base in n ticks with current config and attacks on the board
|
||||
fn raw_population_in_n_ticks(&self, ticks: u32, config: &GameConfig, attacks: &Vec<BoardAction>) -> i32 {
|
||||
// set the population in future to the current population
|
||||
let mut population_in_future: i32 = self.population as i32;
|
||||
if self.uid != 0 { population_in_future += (ticks + 1) as i32 * config.base_levels[self.level as usize].spawn_rate as i32; }
|
||||
for attack in attacks {
|
||||
|
||||
// check if the base has a passive generation rate
|
||||
if self.uid != 0 {
|
||||
// add the spawned bits to the population
|
||||
population_in_future += (ticks * config.base_levels[self.level as usize].spawn_rate) as i32;
|
||||
}
|
||||
|
||||
// iterate through all attacks
|
||||
attacks.iter().for_each(|attack| {
|
||||
// check if the attack will arrive in time
|
||||
if attack.arrival_in_ticks() >= ticks {
|
||||
// get the amount of bits in the attack on arrival
|
||||
let val_on_target: i32 = attack.amount_at_target(&config.paths) as i32;
|
||||
|
||||
// check if the attack is owned by the base
|
||||
if attack.player == self.player {
|
||||
// the attack will contribute to the population
|
||||
population_in_future += val_on_target;
|
||||
}
|
||||
else {
|
||||
// the attack will fight with the base
|
||||
population_in_future -= val_on_target;
|
||||
}
|
||||
}
|
||||
}
|
||||
return population_in_future.abs() as u32;
|
||||
});
|
||||
// return abs of population, because on negative population it is likely to be conquered by an opponent
|
||||
return population_in_future;
|
||||
}
|
||||
|
||||
pub fn population_in_n_ticks(&self, ticks: u32, config: &GameConfig, attacks: &Vec<BoardAction>) -> u32 {
|
||||
return self.raw_population_in_n_ticks(ticks, config, attacks).abs() as u32;
|
||||
}
|
||||
|
||||
pub fn will_die_within_in_n_ticks(&self, ticks: u32, config: &GameConfig, attacks: &Vec<BoardAction>) -> bool {
|
||||
return self.raw_population_in_n_ticks(ticks, config, attacks) < 0;
|
||||
}
|
||||
|
||||
// get the distance to another base
|
||||
pub fn distance_to(&self, base: &Base) -> u32 {
|
||||
return (((base.position.x - self.position.x).pow(2) + (base.position.y - self.position.y).pow(2) + (base.position.z - self.position.z).pow(2)) as f64).powf(1.0 / 2.0) as u32;
|
||||
// calculate the Euclidean distance between the bases
|
||||
return (( (base.position.x - self.position.x).pow(2)
|
||||
+ (base.position.y - self.position.y).pow(2)
|
||||
+ (base.position.z - self.position.z).pow(2)
|
||||
) as f64).sqrt() as u32;
|
||||
}
|
||||
|
||||
pub fn required_to_defeat(&self, base: &Base, attacks: &Vec<BoardAction>, game_config: &GameConfig) -> u32 {
|
||||
let d: u32 = self.distance_to(base);
|
||||
// get the amount of bits required to defeat another base
|
||||
pub fn required_to_defeat(&self, target_base: &Base, game_config: &GameConfig, attacks: &Vec<BoardAction>) -> u32 {
|
||||
// get the distance between the bases
|
||||
let d: u32 = self.distance_to(target_base);
|
||||
|
||||
let mut pop = base.population_in_n_ticks(d, game_config, attacks);
|
||||
// get the population of the target base when an own attack would arrive
|
||||
let population: u32 = target_base.population_in_n_ticks(d, game_config, attacks);
|
||||
|
||||
if base.uid == 0 && base.level == 4 {
|
||||
pop = 1;
|
||||
}
|
||||
else {
|
||||
pop += 3;
|
||||
// to defeat a base there has to be at least one more bit arriving than the current population is
|
||||
let mut requirement: u32 = population + 1;
|
||||
|
||||
// check if the distance is greater than the grace period
|
||||
if d > game_config.paths.grace_period {
|
||||
// add the loss over the additional distance
|
||||
requirement += (d - game_config.paths.grace_period) * game_config.paths.death_rate
|
||||
}
|
||||
|
||||
if d < game_config.paths.grace_period {return pop}
|
||||
|
||||
return pop + (d - game_config.paths.grace_period) * game_config.paths.death_rate
|
||||
// return required bits
|
||||
return requirement;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user