diff --git a/src/_loop.rs b/src/_loop.rs new file mode 100644 index 00000000000..1fc1bf8fcc2 --- /dev/null +++ b/src/_loop.rs @@ -0,0 +1,22 @@ +//! A timed loop implementation + +use async_std::{future::Future, task}; +use chrono::{DateTime, Utc}; +use std::{thread, time::Duration}; + +/// Number of ticks per second +const TICKS: u64 = 100; +const TICK_TIME: Duration = Duration::from_millis(1000 / TICKS); + +pub fn block_loop(f: F) +where + F: Future + Send + Copy + 'static, +{ + loop { + let t1 = Utc::now(); + task::block_on(f); + let t2 = Utc::now(); + let t3 = (t2 - t1).to_std().unwrap(); + thread::sleep(TICK_TIME - t3); + } +} diff --git a/src/data.rs b/src/data.rs index 940bda10e7e..5ce5517c2b0 100644 --- a/src/data.rs +++ b/src/data.rs @@ -19,22 +19,22 @@ pub type NodeId = usize; #[derive(Serialize, Deserialize)] pub struct Node { /// Each node has a unique ID by which it's addressed - id: NodeId, + pub id: NodeId, /// The current health - health: AtomicU32, + pub health: AtomicU32, /// The max health - max_health: AtomicU32, + pub max_health: AtomicU32, /// The owner of this node - owner: Owner, + pub owner: Owner, /// Upgrade state - type_: Upgrade, + pub type_: Upgrade, /// Number of links on the map - links: u8, + pub links: u8, /// Active link states - link_states: Vec>, + pub link_states: Vec>, /// Input buffer #[serde(skip)] - buffer: Vec, + pub buffer: Vec, } pub type LinkId = usize; diff --git a/src/lib.rs b/src/lib.rs index d6e3f246a08..94117ddbcff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub use _if::GameIf; pub mod data; pub mod gens; +mod _loop; mod _match; mod config; mod io; diff --git a/src/map.rs b/src/map.rs index 212ce75100a..37f758b4a43 100644 --- a/src/map.rs +++ b/src/map.rs @@ -7,12 +7,16 @@ use crate::{ wire::Response, }; use async_std::sync::Arc; -use quadtree_rs::Quadtree; +use quadtree_rs::{ + area::{Area, AreaBuilder}, + point::Point, + Quadtree, +}; use std::collections::BTreeMap; pub struct MapNode { - pos: (f64, f64), - inner: Node, + pub pos: (f64, f64), + pub inner: Node, } /// A map that people fight on @@ -45,4 +49,32 @@ impl Map { { unimplemented!() } + + /// Get all objects that can be selected by a single point + pub fn get_by_point(&self, x: i64, y: i64) -> Option> { + self.coord + .query( + AreaBuilder::default() + .anchor(Point::from((x, y))) + .dimensions((1, 1)) + .build() + .ok()?, + ) + .map(|entry| Some(entry.value_ref().inner.id)) + .collect() + } + + /// Get all objects that can be selected by a 2d area + pub fn get_by_area(&self, x: i64, y: i64, w: i64, h: i64) -> Option> { + self.coord + .query( + AreaBuilder::default() + .anchor(Point::from((x, y))) + .dimensions((w, h)) + .build() + .ok()?, + ) + .map(|entry| Some(entry.value_ref().inner.id)) + .collect() + } }