2022-10-08 19:47:47 +00:00
|
|
|
use crate::{
|
|
|
|
asteroids::{Asteroid, AsteroidSize},
|
2023-01-12 09:55:26 +00:00
|
|
|
nn::{ActivationFunc, NN},
|
2022-10-08 19:47:47 +00:00
|
|
|
player::Player,
|
|
|
|
};
|
2022-10-08 15:27:05 +00:00
|
|
|
use macroquad::{prelude::*, rand::gen_range};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct World {
|
2023-01-04 19:57:59 +00:00
|
|
|
pub player: Player,
|
2022-10-08 15:27:05 +00:00
|
|
|
asteroids: Vec<Asteroid>,
|
2023-01-04 19:57:59 +00:00
|
|
|
pub score: f32,
|
2022-10-09 05:40:56 +00:00
|
|
|
pub over: bool,
|
2022-10-22 19:40:19 +00:00
|
|
|
pub fitness: f32,
|
2023-01-09 19:25:18 +00:00
|
|
|
pub track: bool,
|
2023-01-11 18:56:03 +00:00
|
|
|
color: Color,
|
2022-10-08 15:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl World {
|
2023-01-12 09:55:26 +00:00
|
|
|
pub fn new(
|
|
|
|
hlayers: Option<Vec<usize>>,
|
|
|
|
mut_rate: Option<f32>,
|
|
|
|
activ: Option<ActivationFunc>,
|
|
|
|
) -> Self {
|
2022-10-09 20:11:24 +00:00
|
|
|
Self {
|
2023-01-12 20:02:58 +00:00
|
|
|
color: Color::new(1., 1., 1., if hlayers.is_none() { 0.8 } else { 0.4 }),
|
2023-01-12 09:55:26 +00:00
|
|
|
player: Player::new(hlayers, mut_rate, activ),
|
2023-01-05 20:10:35 +00:00
|
|
|
score: 1.,
|
2022-10-23 19:07:18 +00:00
|
|
|
asteroids: vec![
|
|
|
|
Asteroid::new_to(vec2(0., 0.), 1.5, AsteroidSize::Large),
|
|
|
|
Asteroid::new(AsteroidSize::Large),
|
|
|
|
],
|
2022-10-09 20:11:24 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 21:31:03 +00:00
|
|
|
pub fn simulate(brain: NN) -> Self {
|
2023-01-12 09:55:26 +00:00
|
|
|
let mut w = World::new(None, None, None);
|
2023-01-11 21:31:03 +00:00
|
|
|
w.player.brain = Some(brain);
|
2023-01-12 20:02:58 +00:00
|
|
|
w.color = Color::new(1., 1., 1., 0.4);
|
2023-01-11 21:31:03 +00:00
|
|
|
w
|
|
|
|
}
|
2022-10-09 20:11:24 +00:00
|
|
|
|
2023-01-09 19:25:18 +00:00
|
|
|
pub fn track(&mut self, track: bool) {
|
2023-01-11 18:56:03 +00:00
|
|
|
self.track = track;
|
|
|
|
self.color = if track {
|
|
|
|
Color::new(0., 0.8, 0., 0.8)
|
|
|
|
} else {
|
|
|
|
Color::new(1., 1., 1., 0.4)
|
|
|
|
};
|
2022-10-22 19:40:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 18:36:14 +00:00
|
|
|
pub fn see_brain(&self) -> &NN {
|
|
|
|
self.player.brain.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
|
2023-01-08 20:09:10 +00:00
|
|
|
pub fn export_brain(&self, path: &str) {
|
2023-01-06 09:10:29 +00:00
|
|
|
let json = self.player.brain.as_ref().unwrap().export();
|
2023-01-08 20:09:10 +00:00
|
|
|
std::fs::write(path, json).expect("Unable to write file");
|
2023-01-06 09:10:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 15:27:05 +00:00
|
|
|
pub fn update(&mut self) {
|
2023-01-09 19:25:18 +00:00
|
|
|
self.player.update();
|
2022-10-08 20:17:06 +00:00
|
|
|
let mut to_add: Vec<Asteroid> = Vec::new();
|
2022-10-08 15:27:05 +00:00
|
|
|
for asteroid in &mut self.asteroids {
|
|
|
|
asteroid.update();
|
2022-10-08 20:17:06 +00:00
|
|
|
if self.player.check_bullet_collisions(asteroid) {
|
2023-01-04 19:57:59 +00:00
|
|
|
self.score += 1.;
|
2022-10-08 20:17:06 +00:00
|
|
|
match asteroid.size {
|
|
|
|
AsteroidSize::Large => {
|
2022-10-10 10:13:06 +00:00
|
|
|
let rand = vec2(gen_range(-0.8, 0.8), gen_range(-0.8, 0.8));
|
2022-10-08 20:17:06 +00:00
|
|
|
to_add.push(Asteroid::new_from(
|
|
|
|
asteroid.pos,
|
|
|
|
asteroid.vel + rand,
|
|
|
|
AsteroidSize::Medium,
|
|
|
|
));
|
|
|
|
to_add.push(Asteroid::new_from(
|
|
|
|
asteroid.pos,
|
|
|
|
asteroid.vel - rand,
|
|
|
|
AsteroidSize::Medium,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
AsteroidSize::Medium => {
|
2022-10-10 10:13:06 +00:00
|
|
|
let rand = vec2(gen_range(-0.6, 0.6), gen_range(-0.6, 0.6));
|
2022-10-08 20:17:06 +00:00
|
|
|
to_add.push(Asteroid::new_from(
|
|
|
|
asteroid.pos,
|
|
|
|
asteroid.vel + rand,
|
|
|
|
AsteroidSize::Small,
|
|
|
|
));
|
|
|
|
to_add.push(Asteroid::new_from(
|
|
|
|
asteroid.pos,
|
|
|
|
asteroid.vel - rand,
|
|
|
|
AsteroidSize::Small,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2023-01-05 20:33:25 +00:00
|
|
|
if self.player.check_player_collision(asteroid) {
|
2023-01-05 16:46:08 +00:00
|
|
|
self.over = true;
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 03:55:59 +00:00
|
|
|
self.fitness = self.score;
|
2022-10-08 20:17:06 +00:00
|
|
|
self.asteroids.append(&mut to_add);
|
2022-10-08 19:47:47 +00:00
|
|
|
self.asteroids.retain(|asteroid| asteroid.alive);
|
2022-10-23 19:07:18 +00:00
|
|
|
// if self.asteroids.iter().fold(0, |acc, x| {
|
|
|
|
// acc + match x.size {
|
|
|
|
// AsteroidSize::Large => 4,
|
|
|
|
// AsteroidSize::Medium => 2,
|
|
|
|
// AsteroidSize::Small => 1,
|
|
|
|
// }
|
|
|
|
// }) < self.max_asteroids
|
2023-01-05 20:10:35 +00:00
|
|
|
// || self.player.lifespan % 200 == 0
|
2023-01-04 19:57:59 +00:00
|
|
|
// {
|
2023-04-04 03:36:06 +00:00
|
|
|
let num_small: usize = self
|
|
|
|
.asteroids
|
|
|
|
.iter()
|
|
|
|
.filter(|a| a.size == AsteroidSize::Small)
|
|
|
|
.map(|_| 1)
|
|
|
|
.sum();
|
|
|
|
let num_medium: usize = self
|
|
|
|
.asteroids
|
|
|
|
.iter()
|
|
|
|
.filter(|a| a.size == AsteroidSize::Medium)
|
|
|
|
.map(|_| 1)
|
|
|
|
.sum();
|
|
|
|
let num_large: usize = self
|
|
|
|
.asteroids
|
|
|
|
.iter()
|
|
|
|
.filter(|a| a.size == AsteroidSize::Large)
|
|
|
|
.map(|_| 1)
|
|
|
|
.sum();
|
|
|
|
let area = num_small + num_medium * 2 + num_large * 4;
|
|
|
|
if self.player.lifespan % 200 == 0 && area < 12 {
|
2022-10-23 19:07:18 +00:00
|
|
|
self.asteroids
|
|
|
|
.push(Asteroid::new_to(self.player.pos, 1.5, AsteroidSize::Large));
|
2022-10-08 15:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 19:25:18 +00:00
|
|
|
pub fn draw(&self, debug: bool) {
|
2023-01-11 18:56:03 +00:00
|
|
|
self.player.draw(self.color, debug);
|
2022-10-08 15:27:05 +00:00
|
|
|
for asteroid in &self.asteroids {
|
2023-01-11 18:56:03 +00:00
|
|
|
asteroid.draw(self.color);
|
2022-10-08 15:27:05 +00:00
|
|
|
}
|
2023-01-04 19:57:59 +00:00
|
|
|
draw_text(
|
2023-01-08 20:09:10 +00:00
|
|
|
&format!("{:.2}", self.fitness),
|
|
|
|
self.player.pos.x - 22.,
|
2023-01-04 19:57:59 +00:00
|
|
|
self.player.pos.y - 20.,
|
|
|
|
12.,
|
|
|
|
WHITE,
|
|
|
|
);
|
2022-10-08 15:27:05 +00:00
|
|
|
}
|
2023-01-08 19:07:35 +00:00
|
|
|
|
2023-01-11 18:56:03 +00:00
|
|
|
pub fn draw_stats(&self, width: f32, height: f32, rank: usize) {
|
2023-01-08 19:07:35 +00:00
|
|
|
draw_rectangle_lines(-width * 0.5, -height * 0.5, width, height, 2., WHITE);
|
|
|
|
|
|
|
|
let scale = 2.5;
|
|
|
|
let offset = vec2(-width * 0.3, -height * 0.1);
|
|
|
|
let p1 = scale * vec2(0., -20.) + offset;
|
|
|
|
let p2 = scale * vec2(-12.667, 18.) + offset;
|
|
|
|
let p3 = scale * vec2(12.667, 18.) + offset;
|
|
|
|
let p4 = scale * vec2(-10., 10.) + offset;
|
|
|
|
let p5 = scale * vec2(10., 10.) + offset;
|
|
|
|
let p6 = scale * vec2(0., 25.) + offset;
|
|
|
|
let p7 = scale * vec2(-6., 10.) + offset;
|
|
|
|
let p8 = scale * vec2(6., 10.) + offset;
|
|
|
|
|
|
|
|
draw_line(p1.x, p1.y, p2.x, p2.y, 2., WHITE);
|
|
|
|
draw_line(p1.x, p1.y, p3.x, p3.y, 2., WHITE);
|
|
|
|
draw_line(p4.x, p4.y, p5.x, p5.y, 2., WHITE);
|
|
|
|
if self.player.outputs[2] > 0. && (gen_range(0., 1.) < 0.4 || self.over) {
|
|
|
|
draw_triangle_lines(p6, p7, p8, 2., WHITE);
|
|
|
|
}
|
|
|
|
let l1 = scale * vec2(30., 0.) + offset;
|
|
|
|
let l2 = scale * vec2(25., -5.) + offset;
|
|
|
|
let l3 = scale * vec2(25., 5.) + offset;
|
|
|
|
if self.player.outputs[0] > 0. {
|
|
|
|
draw_line(l1.x, l1.y, l2.x, l2.y, 2., WHITE);
|
|
|
|
draw_line(l1.x, l1.y, l3.x, l3.y, 2., WHITE);
|
|
|
|
}
|
|
|
|
let l1 = -scale * vec2(30., 0.) + offset;
|
|
|
|
let l2 = -scale * vec2(25., -5.) + offset;
|
|
|
|
let l3 = -scale * vec2(25., 5.) + offset;
|
|
|
|
if self.player.outputs[1] > 0. {
|
|
|
|
draw_line(l1.x, l1.y, l2.x, l2.y, 2., WHITE);
|
|
|
|
draw_line(l1.x, l1.y, l3.x, l3.y, 2., WHITE);
|
|
|
|
}
|
|
|
|
let l1 = -scale * vec2(0., 35.) + offset;
|
|
|
|
if self.player.outputs[3] > 0. {
|
|
|
|
draw_circle(l1.x, l1.y, 5., WHITE);
|
|
|
|
draw_circle(l1.x, l1.y, 3.5, BLACK);
|
|
|
|
}
|
2023-01-09 19:25:18 +00:00
|
|
|
let params = TextParams {
|
|
|
|
font_size: 48,
|
|
|
|
font_scale: 0.5,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
draw_text_ex(
|
2023-01-08 19:07:35 +00:00
|
|
|
if self.over { "DEAD" } else { "ALIVE" },
|
|
|
|
-width * 0.5 + 20.,
|
2023-01-12 18:53:40 +00:00
|
|
|
55.,
|
2023-01-09 19:25:18 +00:00
|
|
|
{
|
|
|
|
let mut p = params.clone();
|
|
|
|
p.color = if self.over { RED } else { GREEN };
|
|
|
|
p
|
|
|
|
},
|
2023-01-08 19:07:35 +00:00
|
|
|
);
|
2023-01-09 19:25:18 +00:00
|
|
|
draw_text_ex(
|
2023-01-08 19:26:00 +00:00
|
|
|
&format!("Hits: {}", self.score),
|
2023-01-08 19:07:35 +00:00
|
|
|
-width * 0.5 + 20.,
|
2023-01-12 18:53:40 +00:00
|
|
|
75.,
|
2023-01-09 19:25:18 +00:00
|
|
|
params,
|
2023-01-08 19:26:00 +00:00
|
|
|
);
|
2023-01-09 19:25:18 +00:00
|
|
|
draw_text_ex(
|
2023-01-08 19:26:00 +00:00
|
|
|
&format!("Fired: {}", self.player.shots),
|
|
|
|
-width * 0.5 + 20.,
|
2023-01-12 18:53:40 +00:00
|
|
|
95.,
|
2023-01-09 19:25:18 +00:00
|
|
|
params,
|
2023-01-08 19:07:35 +00:00
|
|
|
);
|
2023-01-09 19:25:18 +00:00
|
|
|
draw_text_ex(
|
2023-01-08 19:07:35 +00:00
|
|
|
&format!("Fitness: {:.2}", self.fitness),
|
|
|
|
-width * 0.5 + 20.,
|
2023-01-12 18:53:40 +00:00
|
|
|
115.,
|
|
|
|
params,
|
|
|
|
);
|
|
|
|
draw_text_ex(
|
|
|
|
&format!("Lifetime: {:.2}", self.player.lifespan as f32 / 60.),
|
|
|
|
-width * 0.5 + 20.,
|
|
|
|
135.,
|
2023-01-09 19:25:18 +00:00
|
|
|
params,
|
2023-01-08 19:07:35 +00:00
|
|
|
);
|
2023-01-11 18:56:03 +00:00
|
|
|
let str = &format!("RANK #{}", rank);
|
|
|
|
let w = measure_text(str, None, 64, 0.5);
|
|
|
|
|
|
|
|
draw_text_ex(str, -w.width * 0.5, -height * 0.35, {
|
|
|
|
let mut p = params.clone();
|
|
|
|
p.font_size = 64;
|
|
|
|
p
|
|
|
|
});
|
2023-01-08 19:07:35 +00:00
|
|
|
}
|
2022-10-08 15:27:05 +00:00
|
|
|
}
|