done for the day

This commit is contained in:
2023-11-20 02:12:39 +01:00
commit 97adc47291
9 changed files with 795 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

534
Cargo.lock generated Normal file
View File

@@ -0,0 +1,534 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "AntSimulation"
version = "0.1.0"
dependencies = [
"crossbeam",
"ctrlc",
"rand 0.4.6",
"simple",
]
[[package]]
name = "autocfg"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dde43e75fd43e8a1bf86103336bc699aa8d17ad1be60c76c0bdfd4828e19b78"
dependencies = [
"autocfg 1.1.0",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "crossbeam"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg 1.1.0",
"cfg-if 1.0.0",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "ctrlc"
version = "3.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82e95fbd621905b854affdc67943b043a0fbb6ed7385fd5a25650d19a8a6cfdf"
dependencies = [
"nix",
"windows-sys",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e32a70cf75e5846d53a673923498228bbec6a8624708a9ea5645f075d6276122"
[[package]]
name = "libc"
version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg 1.1.0",
]
[[package]]
name = "nix"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
"bitflags 2.4.1",
"cfg-if 1.0.0",
"libc 0.2.150",
]
[[package]]
name = "num"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e63899ad0da84ce718c14936262a41cee2c79c981fc0a0e7c7beb47d5a07e8c1"
dependencies = [
"num-integer",
"num-traits",
"rand 0.4.6",
"rustc-serialize",
]
[[package]]
name = "num-complex"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b288631d7878aaf59442cffd36910ea604ecd7745c36054328595114001c9656"
dependencies = [
"num-traits",
"rustc-serialize",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg 1.1.0",
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
dependencies = [
"autocfg 1.1.0",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee314c74bd753fc86b4780aa9475da469155f3848473a261d2d18e35245a784e"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
"rustc-serialize",
]
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg 1.1.0",
]
[[package]]
name = "rand"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
dependencies = [
"libc 0.2.150",
"rand 0.4.6",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc 0.2.150",
"rand_core 0.3.1",
"rdrand",
"winapi",
]
[[package]]
name = "rand"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
dependencies = [
"autocfg 0.1.8",
"libc 0.2.150",
"rand_chacha",
"rand_core 0.4.2",
"rand_hc",
"rand_isaac",
"rand_jitter",
"rand_os",
"rand_pcg",
"rand_xorshift",
"winapi",
]
[[package]]
name = "rand_chacha"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
dependencies = [
"autocfg 0.1.8",
"rand_core 0.3.1",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_hc"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rand_isaac"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rand_jitter"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b"
dependencies = [
"libc 0.2.150",
"rand_core 0.4.2",
"winapi",
]
[[package]]
name = "rand_os"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
dependencies = [
"cloudabi",
"fuchsia-cprng",
"libc 0.2.150",
"rand_core 0.4.2",
"rdrand",
"winapi",
]
[[package]]
name = "rand_pcg"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44"
dependencies = [
"autocfg 0.1.8",
"rand_core 0.4.2",
]
[[package]]
name = "rand_xorshift"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rustc-serialize"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "sdl2"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d051a07231e303f5f719da78cb6f7394f6d5b54f733aef5b0b447804a83edd7b"
dependencies = [
"bitflags 1.3.2",
"lazy_static",
"libc 0.2.150",
"num",
"rand 0.6.5",
"sdl2-sys",
]
[[package]]
name = "sdl2-sys"
version = "0.32.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34e71125077d297d57e4c1acfe8981b5bdfbf5a20e7b589abfdcb33bf1127f86"
dependencies = [
"cfg-if 0.1.10",
"libc 0.2.150",
]
[[package]]
name = "simple"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa821f780dcfc349088d15d40eec3de34e1dc6923b1e39c83041d5ca6c686087"
dependencies = [
"libc 0.1.12",
"num",
"rand 0.3.23",
"sdl2",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

15
Cargo.toml Normal file
View File

@@ -0,0 +1,15 @@
[package]
name = "AntSimulation"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.simple]
version = "0.3.0"
[dependencies.ctrlc]
version = "3.4.1"
[dependencies]
rand = "0.4.6"
crossbeam = "0.8.2"

3
build.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-link-search=framework=/Library/Frameworks");
}

87
src/main.rs Normal file
View File

@@ -0,0 +1,87 @@
use std::thread::{sleep};
use std::{time};
use simple::Point;
use crate::models::ant;
use crate::models::ant::{Ant, Mark, Movement};
use crate::models::config::{ANT_CNT, DECAY_SPEED, FRAMERATE, TITLE, WINDOW_X, WINDOW_Y};
mod models;
// main loop
static mut GAMELOOP: bool = true;
fn main() {
// create interrupt handler
ctrlc::set_handler(|| unsafe {
GAMELOOP = false;
}).expect("Error setting Ctrl-C handler");
// create window
let mut window = simple::Window::new(TITLE, WINDOW_X, WINDOW_Y);
let mut ants: Vec<Ant> = ant::spawn(ANT_CNT);
let mut feroines: [[u8; WINDOW_Y as usize]; WINDOW_X as usize] = [[0; WINDOW_Y as usize]; WINDOW_X as usize];
unsafe {
while GAMELOOP {
window.clear_to_color(0,0,0);
for i in 0..ants.len() {
ants[i].sense(&mut feroines);
ants[i].step();
ants[i].randomize();
ants[i].mark(&mut feroines);
}
// blur
let mut feroines_next = [[0; WINDOW_Y as usize]; WINDOW_X as usize];
for i in 0..feroines.len() {
for j in 0..feroines[i].len() {
let mut sum: u8 = 0;
let mut x_low: i8 = -1;
if i == 0 {x_low = 0}
let mut x_high: i8 = 1;
if i == (WINDOW_X - 1) as usize {x_high = 0}
let mut y_low: i8 = -1;
if j == 0 {y_low = 0}
let mut y_high: i8 = 1;
if j == (WINDOW_Y - 1) as usize {y_high = 0}
for x in x_low..x_high+1 {
for y in y_low..y_high+1 {
if x == 0 && y == 0 {
sum += (feroines[i + x as usize][j + y as usize] as f64 * 0.25) as u8;
}
else if x == 0 || y == 0 {
sum += (feroines[i + x as usize][j + y as usize] as f64 * 0.125) as u8;
}
else {
sum += (feroines[i + x as usize][j + y as usize] as f64 * 0.0625) as u8;
}
}
}
feroines_next[i][j] = sum;
}
}
feroines = feroines_next;
for i in 0..feroines.len() {
for j in 0..feroines[i].len() {
// decay feroids
if feroines[i][j] > DECAY_SPEED {
feroines[i][j] -= DECAY_SPEED;
}
else {
feroines[i][j] = 0;
}
// print feroids
window.set_color(0, 250 - feroines[i][j] / 5,127 + feroines[i][j] / 2, feroines[i][j]);
window.draw_point( Point::new(i as i32, j as i32));
}
}
window.next_frame();
sleep(time::Duration::from_millis(1000 / FRAMERATE as u64 / 2))
};
};
}

3
src/models.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod position;
pub mod ant;
pub mod config;

134
src/models/ant.rs Normal file
View File

@@ -0,0 +1,134 @@
use std::f32::consts::PI;
use rand::{Rng, thread_rng};
use crate::models::position::Position;
use crate::models::config::{FRAMERATE, MAX_VELOCITY, MIN_VELOCITY, SENSOR_ANGLE, SENSOR_LENGTH, WINDOW_X, WINDOW_Y};
#[derive(Copy, Clone, Debug)]
pub struct Ant {
pub(crate) position: Position,
pub(crate) velocity: f64,
pub(crate) angle: f64,
}
pub trait Movement {
fn step(&mut self);
fn randomize(&mut self);
}
pub trait Mark {
fn mark(self, feroids: &mut [[u8; WINDOW_Y as usize]; WINDOW_X as usize]);
fn sense(&mut self, feroids: &mut [[u8; WINDOW_Y as usize]; WINDOW_X as usize]);
}
impl Movement for Ant {
fn step(&mut self) {
// move ant
self.position.x += self.velocity / FRAMERATE as f64 * self.angle.cos();
self.position.y += self.velocity / FRAMERATE as f64 * self.angle.sin();
// bounce on x walls
if self.position.x >= WINDOW_X as f64 - 26.0 && (self.angle < 0.5 * PI as f64 || self.angle > 1.5 * PI as f64) {
self.angle = PI as f64 - self.angle;
self.position.x = WINDOW_X as f64 - 26.0;
}
if self.position.x < 25.0 && (self.angle > 0.5 * PI as f64 && self.angle < 1.5 * PI as f64) {
self.angle = PI as f64 - self.angle;
self.position.x = 25.0;
}
// bounce on y walls
if self.position.y >= WINDOW_Y as f64 - 26.0 && (self.angle < PI as f64 && self.angle > 0.0) {
self.angle = 2.0 * PI as f64 - self.angle;
self.position.y = WINDOW_Y as f64 - 26.0;
}
if self.position.y < 25.0 && (self.angle < 0.0 || self.angle > PI as f64) {
self.angle = 2.0 * PI as f64 - self.angle;
self.position.y = 25.0;
}
}
fn randomize(&mut self) {
// randomize velocity
self.velocity += thread_rng().gen_range(-0.015, 0.015);
if self.velocity < MIN_VELOCITY {self.velocity = MIN_VELOCITY};
if self.velocity > MAX_VELOCITY {self.velocity = MAX_VELOCITY};
// randomize angle
self.angle += thread_rng().gen_range(-0.005 * PI as f64, 0.005 * PI as f64);
if self.angle < 0.0 {self.angle += 2.0 * PI as f64};
if self.angle >= (2.0 * PI as f64) {self.angle -= 2.0 * PI as f64};
}
}
impl Mark for Ant {
fn mark(self, feroids: &mut [[u8; WINDOW_Y as usize]; WINDOW_X as usize]) {
feroids[self.position.x as usize][self.position.y as usize] = 255;
}
fn sense(&mut self, feroids: &mut [[u8; WINDOW_Y as usize]; WINDOW_X as usize]) {
let mut xrr = (self.position.x + SENSOR_LENGTH * (self.angle + 2.0 * SENSOR_ANGLE).cos()) as usize;
let mut yrr = (self.position.y + SENSOR_LENGTH * (self.angle + 2.0 * SENSOR_ANGLE).sin()) as usize;
let mut xr = (self.position.x + SENSOR_LENGTH * (self.angle + SENSOR_ANGLE).cos()) as usize;
let mut yr = (self.position.y + SENSOR_LENGTH * (self.angle + SENSOR_ANGLE).sin()) as usize;
let mut xf = (self.position.x + SENSOR_LENGTH * self.angle.cos()) as usize;
let mut yf = (self.position.y + SENSOR_LENGTH * self.angle.sin()) as usize;
let mut xl = (self.position.x + SENSOR_LENGTH * (self.angle - SENSOR_ANGLE).cos()) as usize;
let mut yl = (self.position.y + SENSOR_LENGTH * (self.angle - SENSOR_ANGLE).sin()) as usize;
let mut xll = (self.position.x + SENSOR_LENGTH * (self.angle - 2.0 * SENSOR_ANGLE).cos()) as usize;
let mut yll = (self.position.y + SENSOR_LENGTH * (self.angle - 2.0 * SENSOR_ANGLE).sin()) as usize;
if xrr < 0 { xrr = 0 }
if xrr > (WINDOW_X - 1) as usize { xrr = (WINDOW_X - 1) as usize }
if xr < 0 { xr = 0 }
if xr > (WINDOW_X - 1) as usize { xr = (WINDOW_X - 1) as usize }
if xf < 0 { xf = 0 }
if xf > (WINDOW_X - 1) as usize { xf = (WINDOW_X - 1) as usize }
if xl < 0 { xl = 0 }
if xl > (WINDOW_X - 1) as usize { xl = (WINDOW_X - 1) as usize }
if xll < 0 { xl = 0 }
if xll > (WINDOW_X - 1) as usize { xll = (WINDOW_X - 1) as usize }
if yrr < 0 { yrr = 0 }
if yrr > (WINDOW_Y - 1) as usize { yrr = (WINDOW_Y - 1) as usize }
if yr < 0 { yr = 0 }
if yr > (WINDOW_Y - 1) as usize { yr = (WINDOW_Y - 1) as usize }
if yf < 0 { yf = 0 }
if yf > (WINDOW_Y - 1) as usize { yf = (WINDOW_Y - 1) as usize }
if yl < 0 { yl = 0 }
if yl > (WINDOW_Y - 1) as usize { yl = (WINDOW_Y - 1) as usize }
if yll < 0 { yll = 0 }
if yll > (WINDOW_Y - 1) as usize { yll = (WINDOW_Y - 1) as usize }
let srr = feroids[xrr][yrr];
let sr = feroids[xr][yr];
let sf = feroids[xf][yf];
let sl = feroids[xl][yl];
let sll = feroids[xll][yll];
let mut angles = vec![srr, sr, sf, sl, sll];
angles.sort();
match angles[0] {
a if a == srr => {self.angle += 0.8 * SENSOR_ANGLE},
a if a == sr=> {self.angle += 0.5 * SENSOR_ANGLE},
a if a == sf => {},
a if a == sl=> {self.angle -= 0.5 * SENSOR_ANGLE},
a if a == sll=> {self.angle -= 0.8 * SENSOR_ANGLE}
_ => {}
}
}
}
pub fn spawn(n: u64) -> Vec<Ant> {
let mut ants : Vec<Ant> = vec![];
for _i in 0..n {
ants.push(Ant {
position: Position {
x: thread_rng().gen_range(25.0, WINDOW_X as f64 - 26.0),
y: thread_rng().gen_range(25.0, WINDOW_Y as f64 - 26.0),
},
velocity: thread_rng().gen_range(MIN_VELOCITY, MAX_VELOCITY),
angle: thread_rng().gen_range(0.0, 2.0 * PI as f64),
})
}
return ants;
}

13
src/models/config.rs Normal file
View File

@@ -0,0 +1,13 @@
// set window details
pub const TITLE: &str = "Simulation";
pub const FRAMERATE: u8 = 120;
pub const WINDOW_X: u16 = 550;
pub const WINDOW_Y: u16 = 550;
// set ant details
pub const ANT_CNT: u64 = 35000;
pub const MAX_VELOCITY: f64 = 250.0;
pub const MIN_VELOCITY: f64 = 200.0;
pub const DECAY_SPEED: u8 = 6;
pub const SENSOR_LENGTH: f64 = 50.0;
pub const SENSOR_ANGLE: f64 = 0.7;

5
src/models/position.rs Normal file
View File

@@ -0,0 +1,5 @@
#[derive(Copy, Clone, Debug)]
pub struct Position {
pub x: f64,
pub y: f64,
}