Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
391a771792 |
|||
|
d2408b2468 |
|||
|
ada386fb30 |
|||
|
0f831acf45 |
3 changed files with 170 additions and 48 deletions
56
simulator/Cargo.lock
generated
56
simulator/Cargo.lock
generated
|
|
@ -65,6 +65,17 @@ version = "1.0.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chacha20"
|
||||||
|
version = "0.10.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "color_quant"
|
name = "color_quant"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
|
|
@ -80,6 +91,15 @@ dependencies = [
|
||||||
"libm",
|
"libm",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpufeatures"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crc32fast"
|
name = "crc32fast"
|
||||||
version = "1.5.4"
|
version = "1.5.4"
|
||||||
|
|
@ -158,6 +178,18 @@ dependencies = [
|
||||||
"ttf-parser",
|
"ttf-parser",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"r-efi",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "glam"
|
name = "glam"
|
||||||
version = "0.27.0"
|
version = "0.27.0"
|
||||||
|
|
@ -432,6 +464,29 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "r-efi"
|
||||||
|
version = "6.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.10.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"
|
||||||
|
dependencies = [
|
||||||
|
"chacha20",
|
||||||
|
"getrandom",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "robust"
|
name = "robust"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
|
|
@ -472,6 +527,7 @@ dependencies = [
|
||||||
"glam 0.34.3",
|
"glam 0.34.3",
|
||||||
"macroquad",
|
"macroquad",
|
||||||
"parry2d",
|
"parry2d",
|
||||||
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,4 @@ edition = "2024"
|
||||||
glam = "0.34.3"
|
glam = "0.34.3"
|
||||||
macroquad = "0.4.16"
|
macroquad = "0.4.16"
|
||||||
parry2d = "0.31.1"
|
parry2d = "0.31.1"
|
||||||
|
rand_gen = { package = "rand", version = "0.10.3" }
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,12 @@ use parry2d::shape::Ball;
|
||||||
use parry2d::shape::Segment;
|
use parry2d::shape::Segment;
|
||||||
use parry2d::shape::Shape;
|
use parry2d::shape::Shape;
|
||||||
|
|
||||||
|
use rand_gen::RngExt;
|
||||||
|
use rand_gen::SeedableRng;
|
||||||
|
use rand_gen::distr::Distribution;
|
||||||
|
use rand_gen::distr::Uniform;
|
||||||
|
use rand_gen::rngs::SmallRng;
|
||||||
|
|
||||||
const VIEW_DIST: f32 = 180.0;
|
const VIEW_DIST: f32 = 180.0;
|
||||||
const BOT_RADIUS: f32 = 16.0;
|
const BOT_RADIUS: f32 = 16.0;
|
||||||
const KILL_RADIUS: f32 = 140.0;
|
const KILL_RADIUS: f32 = 140.0;
|
||||||
|
|
@ -57,9 +63,7 @@ impl Mortal for Robot {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Robot {
|
impl Robot {
|
||||||
fn new(position: Vec2, team: Color) -> Self {
|
fn new(pose: Pose, team: Color) -> Self {
|
||||||
let rand_angle = rand::gen_range(0, 1000) as f32 * TAU / 1000.0;
|
|
||||||
let pose:Pose = Pose::from_parts(position.to_array().into(), Rot2::from_angle(rand_angle));
|
|
||||||
Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS), sensor: Sensor::None, team}
|
Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS), sensor: Sensor::None, team}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,6 +88,11 @@ impl Robot {
|
||||||
.map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y))
|
.map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn crater(&self) -> (f32, f32, f32, Color) {
|
||||||
|
let tl = self.pose.translation;
|
||||||
|
(tl.x, tl.y, KILL_RADIUS, ORANGE)
|
||||||
|
}
|
||||||
|
|
||||||
fn draw_weapon(&self) {
|
fn draw_weapon(&self) {
|
||||||
let tl = self.pose.translation;
|
let tl = self.pose.translation;
|
||||||
draw_circle(tl.x, tl.y, KILL_RADIUS, ORANGE);
|
draw_circle(tl.x, tl.y, KILL_RADIUS, ORANGE);
|
||||||
|
|
@ -162,55 +171,66 @@ impl Food {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_simulation() -> (i32, i32) {
|
struct Simulation {
|
||||||
let mut watchdog = 0; // iterations since something interesting
|
robots: Vec<Robot>,
|
||||||
let mut robots = Vec::new();
|
food: Vec<Food>,
|
||||||
|
craters: Vec<(f32, f32, f32, Color)>,
|
||||||
|
new_craters: Vec<(f32, f32, f32, Color)>,
|
||||||
|
watchdog: i32,
|
||||||
|
score: HashMap<[u32;4], i32>,
|
||||||
|
}
|
||||||
|
|
||||||
let mut score = HashMap::new();
|
impl Simulation {
|
||||||
|
fn new(seed: u64) -> Self {
|
||||||
|
let mut rng = SmallRng::seed_from_u64(seed);
|
||||||
|
let watchdog = 0; // iterations since something interesting
|
||||||
|
let mut robots = Vec::new();
|
||||||
|
|
||||||
for i in 5..20 {
|
let craters: Vec<(f32, f32, f32, Color)> = Vec::new();
|
||||||
robots.push(Robot::new(Vec2 { x: 100.0, y: i as f32*50.0 }, BLUE));
|
let new_craters: Vec<(f32, f32, f32, Color)> = Vec::new();
|
||||||
robots.push(Robot::new(Vec2 { x: 1200.0, y: i as f32*50.0 }, RED));
|
|
||||||
|
let score = HashMap::new();
|
||||||
|
|
||||||
|
for i in 5..20 {
|
||||||
|
let angle = Uniform::new(0.0, TAU).unwrap();
|
||||||
|
let rand_angle = angle.sample(&mut rng);
|
||||||
|
let pose:Pose = Pose::from_parts(Vec2 { x: 100.0, y: i as f32*50.0 }, Rot2::from_angle(rand_angle));
|
||||||
|
robots.push(Robot::new(pose, BLUE));
|
||||||
|
|
||||||
|
let rand_angle = angle.sample(&mut rng);
|
||||||
|
let pose:Pose = Pose::from_parts(Vec2 { x: 1200.0, y: i as f32*50.0 }, Rot2::from_angle(rand_angle));
|
||||||
|
robots.push(Robot::new(pose, RED));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut food = Vec::new();
|
||||||
|
|
||||||
|
for _ in 0..120 {
|
||||||
|
let x = rng.random_range(200.0 .. 1100.0);
|
||||||
|
let y = rng.random_range(200.0 .. 900.0);
|
||||||
|
food.push(Food::new(Vec2 { x, y }));
|
||||||
|
}
|
||||||
|
Self { robots, food, craters, new_craters, watchdog, score }
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut food = Vec::new();
|
|
||||||
|
|
||||||
for i in 0..120 {
|
|
||||||
let x = rand::gen_range(200, 1100) as f32;
|
|
||||||
let y = rand::gen_range(200, 900) as f32;
|
|
||||||
food.push(Food::new(Vec2 { x, y }));
|
|
||||||
}
|
|
||||||
|
|
||||||
//for j in 5..20 {
|
|
||||||
// for i in 5..20 {
|
|
||||||
// food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 }));
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
fn color_hash(color: Color) -> [u32; 4] {
|
fn color_hash(color: Color) -> [u32; 4] {
|
||||||
color.to_vec().to_array().map(|f| f.to_bits())
|
color.to_vec().to_array().map(|f| f.to_bits())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sim_step(&mut self) -> Option<(i32, i32)>{
|
||||||
|
let Simulation { robots, food, craters, new_craters, watchdog, score } = self;
|
||||||
|
|
||||||
let mut loo = 0;
|
for food in &*food{
|
||||||
loop {
|
|
||||||
let rendered = loo %90 == 0;
|
|
||||||
//let rendered = false;
|
|
||||||
clear_background(LIGHTGRAY);
|
|
||||||
|
|
||||||
for food in &mut food{
|
|
||||||
if let Some(team) = food.detect(robots.iter()) {
|
if let Some(team) = food.detect(robots.iter()) {
|
||||||
if food.living() {
|
if food.living() {
|
||||||
score.entry(color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1);
|
score.entry(Self::color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1);
|
||||||
food.kill();
|
food.kill();
|
||||||
watchdog = 0;
|
*watchdog = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if rendered { food.draw(); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
food = food.into_iter().filter(Mortal::living).collect(); // remove dead food
|
food.retain(Mortal::living);
|
||||||
robots = robots.into_iter().filter(Mortal::living).collect(); // remove dead robots
|
robots.retain(Mortal::living);
|
||||||
|
|
||||||
let mut sensors = Vec::new();
|
let mut sensors = Vec::new();
|
||||||
for robot in &*robots {
|
for robot in &*robots {
|
||||||
|
|
@ -248,39 +268,84 @@ async fn run_simulation() -> (i32, i32) {
|
||||||
death = false; // pacifists
|
death = false; // pacifists
|
||||||
}
|
}
|
||||||
if death {
|
if death {
|
||||||
if true {robot.draw_weapon();}
|
new_craters.push(robot.crater());
|
||||||
robot.weapon(peers);
|
robot.weapon(peers);
|
||||||
}
|
}
|
||||||
if rendered { robot.draw();}
|
|
||||||
}
|
|
||||||
if rendered {
|
|
||||||
draw_text(format!("RED: {}", score.get(&color_hash(RED)).unwrap_or(&0)), 20.0, 20.0, 30.0, DARKGRAY);
|
|
||||||
draw_text(format!("BLUE: {}", score.get(&color_hash(BLUE)).unwrap_or(&0)), 20.0, 40.0, 30.0, DARKGRAY);
|
|
||||||
}
|
|
||||||
loo += 1;
|
|
||||||
if rendered {
|
|
||||||
next_frame().await // TODO: framerate cap
|
|
||||||
}
|
}
|
||||||
|
|
||||||
watchdog += 1;
|
*watchdog += 1;
|
||||||
if food.is_empty() || watchdog > 1000 {
|
if food.is_empty() || *watchdog > 1000 {
|
||||||
let red = score.get(&color_hash(RED)).unwrap_or(&0);
|
let red = score.get(&Self::color_hash(RED)).unwrap_or(&0);
|
||||||
let blu = score.get(&color_hash(BLUE)).unwrap_or(&0);
|
let blu = score.get(&Self::color_hash(BLUE)).unwrap_or(&0);
|
||||||
return (*red, *blu);
|
return Some((*red, *blu));
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn render(&mut self) {
|
||||||
|
let Simulation { robots, food, craters, new_craters, watchdog: _, score } = self;
|
||||||
|
|
||||||
|
clear_background(LIGHTGRAY);
|
||||||
|
for crater in &*new_craters {
|
||||||
|
draw_circle(crater.0, crater.1, crater.2, crater.3);
|
||||||
|
}
|
||||||
|
craters.append(&mut *new_craters);
|
||||||
|
for crater in &*craters {
|
||||||
|
draw_circle(crater.0, crater.1, crater.2, Color::new(0.75, 0.75, 0.75, 1.0));
|
||||||
|
}
|
||||||
|
for food in &*food {
|
||||||
|
food.draw();
|
||||||
|
|
||||||
|
}
|
||||||
|
for robot in &*robots {
|
||||||
|
robot.draw();
|
||||||
|
}
|
||||||
|
draw_text(format!("RED: {}", score.get(&Self::color_hash(RED)).unwrap_or(&0)), 20.0, 20.0, 30.0, DARKGRAY);
|
||||||
|
draw_text(format!("BLUE: {}", score.get(&Self::color_hash(BLUE)).unwrap_or(&0)), 20.0, 40.0, 30.0, DARKGRAY);
|
||||||
|
next_frame().await // TODO: framerate cap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run_simulation(seed: u64) -> (i32, i32) {
|
||||||
|
let mut sim = Simulation::new(seed);
|
||||||
|
|
||||||
|
let mut loo = 1;
|
||||||
|
loop {
|
||||||
|
let rendered = loo % 20 == 0;
|
||||||
|
let result = sim.sim_step();
|
||||||
|
|
||||||
|
if rendered {
|
||||||
|
sim.render().await
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(result) = result {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
loo += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run_headless_simulation(seed: u64) -> (i32, i32) {
|
||||||
|
let mut sim = Simulation::new(seed);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if let Some(result) = sim.sim_step() {
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[macroquad::main("Adversarial Foraging Simulator")]
|
#[macroquad::main("Adversarial Foraging Simulator")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
|
||||||
let mut rtot = 0;
|
let mut rtot = 0;
|
||||||
let mut btot = 0;
|
let mut btot = 0;
|
||||||
for i in 0..100 {
|
for i in 0..100 {
|
||||||
rand::srand(i);
|
|
||||||
|
|
||||||
let (red, blu) = run_simulation().await;
|
let (red, blu) = run_headless_simulation(i).await;
|
||||||
|
|
||||||
rtot += red;
|
rtot += red;
|
||||||
btot += blu;
|
btot += blu;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue