This commit is contained in:
2023-11-23 22:23:59 +01:00
commit 7b83aa0652
7 changed files with 764 additions and 0 deletions

1
.gitignore vendored Normal file
View File

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

464
Cargo.lock generated Normal file
View File

@@ -0,0 +1,464 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "AntSimulationMultithread"
version = "0.1.0"
dependencies = [
"ctrlc",
"rand 0.5.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 = "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 = "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.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9"
dependencies = [
"cloudabi",
"fuchsia-cprng",
"libc 0.2.150",
"rand_core 0.3.1",
"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 = "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"

11
Cargo.toml Normal file
View File

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

137
src/ants.rs Normal file
View File

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

17
src/config.rs Normal file
View File

@@ -0,0 +1,17 @@
// set window details
pub const TITLE: &str = "Simulation";
pub const FRAMERATE: f32 = 120.0;
pub const WINDOW_X: usize = 550;
pub const WINDOW_Y: usize = 550;
// set ant details
pub const ANT_CNT: u64 = 35_000;
pub const MAX_VELOCITY: f32 = 2.5;
pub const MIN_VELOCITY: f32 = 0.5;
pub const DECAY_SPEED: u8 = 5;
pub const SENSOR_LENGTH: f32 = 50.0;
pub const SENSOR_ANGLE: f32 = 0.4;
// Set Multithreading details
pub const ANT_TREADS: usize = 2;
pub const BLUR_TREADS: usize = 6;

129
src/main.rs Normal file
View File

@@ -0,0 +1,129 @@
use std::sync::{Arc, Mutex};
use std::{thread, time};
use std::thread::{JoinHandle, sleep};
use std::time::Instant;
use simple::Point;
use crate::ants::{spawn, Ant, Movement, Mark};
use crate::config::{ANT_CNT, ANT_TREADS, BLUR_TREADS, DECAY_SPEED, FRAMERATE, TITLE, WINDOW_X, WINDOW_Y};
mod ants;
mod position;
mod config;
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 as u16, WINDOW_Y as u16);
// spawn ants
let ants: Arc<Vec<Mutex<Ant>>> = spawn(ANT_CNT);
// create empty feroid arrays
let mut feroids_just_marked: Arc<Mutex<[[u8; WINDOW_X]; WINDOW_Y]>> = Arc::new(Mutex::new([[0; WINDOW_X]; WINDOW_Y]));
let mut feroids_to_blur: Arc<[[u8; WINDOW_X]; WINDOW_Y]> = Arc::new([[0; WINDOW_X]; WINDOW_Y]);
let mut feroids_next: Arc<Mutex<[[u8; WINDOW_X]; WINDOW_Y]>> = Arc::new(Mutex::new([[0; WINDOW_X]; WINDOW_Y]));
let mut feroids: Arc<[[u8; WINDOW_X]; WINDOW_Y]> = Arc::new([[0; WINDOW_X]; WINDOW_Y]);
// main loop
unsafe {
while GAMELOOP {
let framestart : Instant = Instant::now();
{
//println!("{:?} {:?} {:?} {:?}", feroids[25][25], feroids_next.lock().unwrap()[25][25] , feroids_to_blur[25][25], feroids_just_marked.lock().unwrap()[25][25]);
feroids = Arc::new(feroids_next.lock().unwrap().clone());
feroids_to_blur = Arc::new(feroids_just_marked.lock().unwrap().clone());
feroids_just_marked = Arc::new(Mutex::new(*feroids));
}
let mut handles: Vec<JoinHandle<()>> = vec![];
// create threads fo ant movement
for thread_id in 0..ANT_TREADS {
// clone variables to move them to thread
let ants_for_thread: Arc<Vec<Mutex<Ant>>> = Arc::clone(&ants);
let feroids_for_thread: Arc<[[u8; WINDOW_X]; WINDOW_Y]> = Arc::clone(&feroids);
let feroids_just_marked_for_thread: Arc<Mutex<[[u8; WINDOW_X]; WINDOW_Y]>> = Arc::clone(&feroids_just_marked);
let interval = (thread_id * ANT_CNT as usize / ANT_TREADS)..((thread_id + 1) * ANT_CNT as usize / ANT_TREADS);
// spawn treads
handles.push(thread::spawn(move || {
for index in interval {
let mut ant = ants_for_thread[index].lock().unwrap();
ant.randomize();
ant.step();
ant.sense(&feroids_for_thread);
ant.mark(&feroids_just_marked_for_thread);
}
return;
}));
}
for thread_id in 0..BLUR_TREADS {
// clone variables to move them to thread
let feroids_to_blur_for_thread: Arc<[[u8; WINDOW_X]; WINDOW_Y]> = Arc::clone(&feroids_to_blur);
let feroids_next_for_thread: Arc<Mutex<[[u8; WINDOW_X]; WINDOW_Y]>> = Arc::clone(&feroids_next);
let interval = (thread_id * WINDOW_Y / BLUR_TREADS) as isize..((thread_id + 1) * WINDOW_Y / BLUR_TREADS) as isize;
// spawn treads
handles.push(thread::spawn(move || {
for y in interval{
for x in 0..WINDOW_X as isize {
let mut sum: u8 = 0;
let mut x_low: isize = -1;
if x == 0 { x_low = 0 }
let mut x_high: isize = 1;
if x == (WINDOW_X - 1) as isize { x_high = 0 }
let mut y_low: isize = -1;
if y == 0 { y_low = 0 }
let mut y_high: isize = 1;
if y == (WINDOW_Y - 1) as isize { y_high = 0 }
for j in y_low..y_high + 1 {
for i in x_low..x_high + 1 {
sum += feroids_to_blur_for_thread[(y + j) as usize][(x + i) as usize] / 9;
}
}
if sum > DECAY_SPEED {
sum -= DECAY_SPEED;
}
else {
sum = 0;
}
let mut feroids_blured = feroids_next_for_thread.lock().unwrap();
feroids_blured[y as usize][x as usize] = sum;
}
}
return;
}));
}
window.clear_to_color(0,0,0);
for y in 0..WINDOW_Y {
for x in 0..WINDOW_X {
// print feroids
window.set_color(0, 250 - feroids[y][x] / 5,127 + feroids[y][x] / 2, feroids[y][x]);
window.draw_point( Point::new(y as i32, x as i32));
}
}
window.next_frame();
for handle in handles {
handle.join().unwrap();
}
println!("{}", 1000.0 / framestart.elapsed().as_millis() as f32);
sleep(time::Duration::from_millis((1.0 / FRAMERATE - framestart.elapsed().as_millis() as f32) as u64))
}
}
}

5
src/position.rs Normal file
View File

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