1
Fork 0

removed trait tasks

This commit is contained in:
Andy Killorin 2023-12-22 09:51:14 -06:00
parent bda32e34a9
commit 058e12ef23
Signed by: ank
GPG key ID: B6241CA3B552BCA4
5 changed files with 14 additions and 175 deletions

View file

@ -1,9 +1,9 @@
use std::{sync::Arc, ops::Index}; use std::sync::Arc;
use nalgebra::Vector3; use nalgebra::Vector3;
use rstar::{self, PointDistance, RTree, RTreeObject, AABB}; use rstar::{PointDistance, RTree, RTreeObject, AABB};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::sync::{RwLock, RwLockReadGuard, OwnedRwLockReadGuard}; use tokio::sync::{RwLock, OwnedRwLockReadGuard};
use crate::{turtle::TurtleCommand, paths::{self, TRANSPARENT}}; use crate::{turtle::TurtleCommand, paths::{self, TRANSPARENT}};

View file

@ -2,32 +2,25 @@
use std::{collections::VecDeque, io::ErrorKind, sync::Arc, env::args, path}; use std::{collections::VecDeque, io::ErrorKind, sync::Arc, env::args, path};
use anyhow::{Context, Error, Ok}; use anyhow::{Error, Ok};
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
http::request,
routing::{get, post}, routing::{get, post},
Json, Router, Json, Router,
}; };
use blocks::{World, Position, Vec3}; use blocks::{World, Position, Vec3};
use indoc::formatdoc;
use log::info; use log::info;
use mine::TurtleMineJob; use rstar::RTree;
use rstar::{self, AABB, RTree};
use const_format::formatcp;
use hyper::body::Incoming;
use nalgebra::Vector3;
use names::Name; use names::Name;
use serde::{Deserialize, Serialize};
use tokio::{sync::{ use tokio::{sync::{
watch::{self}, RwLock, mpsc, OnceCell
Mutex, RwLock, mpsc, OnceCell
}, fs}; }, fs};
use tower::Service; use turtle::{Turtle, TurtleInfo, TurtleCommand, TurtleCommander, TurtleCommandResponse};
use turtle::{TurtleTask, Iota, Receiver, Sender, Turtle, TurtleUpdate, TurtleInfo, TurtleCommand, TurtleCommander, TurtleCommandResponse}; use serde::{Deserialize, Serialize};
use indoc::formatdoc;
use crate::{blocks::Block, paths::route}; use crate::blocks::Block;
mod blocks; mod blocks;
mod names; mod names;
@ -46,7 +39,7 @@ struct SavedState {
struct LiveState { struct LiveState {
turtles: Vec<Arc<RwLock<turtle::Turtle>>>, turtles: Vec<Arc<RwLock<turtle::Turtle>>>,
tasks: Vec<VecDeque<TurtleMineJob>>, tasks: Vec<VecDeque<()>>,
world: blocks::World, world: blocks::World,
} }

View file

@ -1,133 +1,6 @@
use hilbert_index::FromHilbertIndex;
use log::{info, warn}; use log::{info, warn};
use nalgebra::SimdValue;
use serde::{Deserialize, Serialize};
use rstar::{self, AABB};
use std::collections::VecDeque;
use crate::{blocks::{Position, Vec3, Block, Direction}, turtle::{TurtleTask, Iota, self, Turtle, TurtleCommand, TurtleCommander, TurtleCommandResponse, InventorySlot}, paths::TRANSPARENT};
#[derive(Serialize, Deserialize)]
pub struct TurtleMineJobParams {
pub chunks: Vec<Vec3>,
pub method: TurtleMineMethod,
pub refuel: Position,
pub storage: Position,
}
#[derive(Serialize, Deserialize)]
pub struct TurtleMineJob {
to_mine: VecDeque<Vec3>,
mined: u32,
mined_chunks: u32,
params: TurtleMineJobParams,
state: State,
}
#[derive(Serialize, Deserialize, Debug)]
enum State {
Mining,
Refueling,
Storing,
}
#[derive(Serialize, Deserialize)]
pub enum TurtleMineMethod {
Clear,
Hilbert,
}
fn next_hilbert_chunk(n: i32, min: Vec3, half: bool) -> Option<Vec3> {
let max = min + Vec3::new(16, 16, 16);
let level = if half {
3
} else { 4 };
let point: [usize; 3] = FromHilbertIndex::from_hilbert_index(&(n as usize), level);
let mut point = Vec3::new(point[0] as i32, point[1] as i32, point[2] as i32);
dbg!(point);
point = if half {
point * 2
} else {
point
};
point += min;
if point > max {
None
} else {
Some(dbg!(point))
}
}
impl TurtleMineJob {
pub fn chunk(point: Vec3) -> Self {
//let chunk_min = Vec3::new(
// point.x - point.x % 16,
// point.y - point.y % 16,
// point.z - point.z % 16);
let chunk_min = point;
TurtleMineJob { to_mine: VecDeque::new(), mined: 0, mined_chunks: 0,
params: TurtleMineJobParams { chunks: vec![chunk_min], method: TurtleMineMethod::Hilbert, refuel:
Position::new(Vec3::new(-30,65,-44), Direction::South),
storage: Position::new(Vec3::new(-29,65,-44), Direction::South)
},
state: State::Mining }
}
}
impl TurtleTask for TurtleMineJob {
fn handle_block(&mut self, block: Block) {
// TODO: more logic
if block.name.contains("ore") {
self.to_mine.push_back(block.pos);
}
}
fn next(&mut self, turtle: &Turtle) -> Iota {
if (turtle.fuel as i32) < (turtle.position.pos - self.params.refuel.pos).abs().sum() * 2 {
self.state = State::Refueling;
}
println!("{:?}", self.state);
println!("{}m to depot",(turtle.position.pos - self.params.refuel.pos).abs().sum());
match self.state {
State::Mining => {
if let Some(block) = self.to_mine.pop_back() {
return Iota::Mine(block.into());
}
if let Some(block) = next_hilbert_chunk(self.mined as i32, self.params.chunks[self.mined_chunks as usize], false) {
self.mined += 1;
return Iota::Mine(block.into());
}
Iota::End
},
State::Refueling => {
if turtle.position != self.params.refuel {
Iota::Goto(self.params.refuel)
} else {
self.state = State::Mining;
Iota::Execute(TurtleCommand::Refuel)
}
},
State::Storing => {
if turtle.position != self.params.storage {
Iota::Goto(self.params.storage)
} else {
self.state = State::Mining;
Iota::Execute(TurtleCommand::Poweroff)
}
},
}
}
}
use crate::{blocks::{Position, Vec3}, turtle::{TurtleCommand, TurtleCommander, TurtleCommandResponse, InventorySlot}, paths::TRANSPARENT};
use TurtleCommand::*; use TurtleCommand::*;
/// Things to leave in the field (not worth fuel) /// Things to leave in the field (not worth fuel)

View file

@ -1,7 +1,5 @@
use std::rc::Rc;
use crate::{ use crate::{
blocks::{Block, World, Position, Direction, Vec3, WorldReadLock}, turtle::TurtleCommand, blocks::{World, Position, Direction, Vec3, WorldReadLock},
}; };
use pathfinding::prelude::astar; use pathfinding::prelude::astar;

View file

@ -1,13 +1,8 @@
use crate::SharedControl;
use crate::blocks::Block; use crate::blocks::Block;
use crate::blocks::Direction; use crate::blocks::Direction;
use crate::blocks::Position; use crate::blocks::Position;
use crate::blocks::Vec3; use crate::blocks::Vec3;
use crate::blocks::World; use crate::blocks::World;
use crate::blocks::nearest;
use crate::mine::TurtleMineJob;
use crate::paths;
use crate::paths::route_facing; use crate::paths::route_facing;
use anyhow::Ok; use anyhow::Ok;
@ -21,13 +16,11 @@ use tokio::sync::OnceCell;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use tokio::sync::oneshot::channel;
use tokio::time::timeout; use tokio::time::timeout;
use super::LiveState; use super::LiveState;
use std::collections::VecDeque;
use std::future::Ready;
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::AtomicUsize; use std::sync::atomic::AtomicUsize;
use std::time::Duration; use std::time::Duration;
@ -53,7 +46,6 @@ pub(crate) struct Turtle {
/// movement vector of last given command /// movement vector of last given command
pub(crate) queued_movement: Vec3, pub(crate) queued_movement: Vec3,
pub(crate) position: Position, pub(crate) position: Position,
pub(crate) goal: Option<Iota>,
pub(crate) pending_update: bool, pub(crate) pending_update: bool,
#[serde(skip)] #[serde(skip)]
callback: Option<oneshot::Sender<TurtleInfo>>, callback: Option<oneshot::Sender<TurtleInfo>>,
@ -94,7 +86,6 @@ impl Default for Turtle {
fuel_limit: Default::default(), fuel_limit: Default::default(),
queued_movement: Default::default(), queued_movement: Default::default(),
position: Position::new(Vec3::zeros(), Direction::North), position: Position::new(Vec3::zeros(), Direction::North),
goal: None,
pending_update: Default::default(), pending_update: Default::default(),
callback: None, callback: None,
sender: Some(Arc::new(sender)), sender: Some(Arc::new(sender)),
@ -466,19 +457,3 @@ pub(crate) struct TurtleResponse {
pub(crate) id: u32, pub(crate) id: u32,
pub(crate) command: TurtleCommand, pub(crate) command: TurtleCommand,
} }
#[derive(Serialize, Deserialize, Clone)]
pub enum Iota {
End,
Goto(Position),
Mine(Vec3),
Execute(TurtleCommand),
}
pub trait TurtleTask: erased_serde::Serialize {
fn handle_block(&mut self, _: Block) { }
fn next(&mut self, turtle: &Turtle) -> Iota
{ Iota::End }
}
erased_serde::serialize_trait_object!(TurtleTask);