1
Fork 0

implemented birth (for the mother)

This commit is contained in:
Andy Killorin 2023-12-26 20:47:07 -06:00
parent 81a56331f9
commit 6f01b486ae
Signed by: ank
GPG key ID: B6241CA3B552BCA4
4 changed files with 58 additions and 3 deletions

View file

@ -43,6 +43,16 @@ local function iteminfo(slot)
return { ["Item"] = turtle.getItemDetail(slot) } return { ["Item"] = turtle.getItemDetail(slot) }
end end
local function restartfront()
front = peripheral.wrap("front")
if not front.shutdown then
return false
end
front.shutdown()
front.turnOn()
return front.isOn()
end
local function inventoryinfo() local function inventoryinfo()
return { ["Inventory"] = peripheral.wrap("front").list() } return { ["Inventory"] = peripheral.wrap("front").list() }
end end
@ -115,6 +125,7 @@ local commands = {
["Place"] = turtle.place, ["Place"] = turtle.place,
["PlaceDown"] = turtle.placeDown, ["PlaceDown"] = turtle.placeDown,
["Update"] = update, ["Update"] = update,
["CycleFront"] = restartfront,
["Poweroff"] = os.shutdown, ["Poweroff"] = os.shutdown,
["GetFuelLimit"] = turtle.getFuelLimit, ["GetFuelLimit"] = turtle.getFuelLimit,
}; };

View file

@ -25,9 +25,9 @@ pub struct DepotGuard {
} }
impl DepotGuard { impl DepotGuard {
pub fn new(mutex: OwnedMutexGuard<Position>, semaphore: OwnedSemaphorePermit) -> Self { Self { mutex, semaphore } } fn new(mutex: OwnedMutexGuard<Position>, semaphore: OwnedSemaphorePermit) -> Self { Self { mutex, semaphore } }
fn position(&self) -> &Position { pub fn position(&self) -> &Position {
&self.mutex &self.mutex
} }

View file

@ -49,6 +49,8 @@ pub async fn mine_chunk_and_sweep(turtle: TurtleCommander, pos: Vec3, chunk: Vec
turtle.dock().await; turtle.dock().await;
} }
devore(&turtle).await;
refuel_needed(&turtle, volume).await; refuel_needed(&turtle, volume).await;
mine_chunk(turtle.clone(), pos, chunk).await?; mine_chunk(turtle.clone(), pos, chunk).await?;
@ -70,6 +72,42 @@ pub async fn mine_chunk_and_sweep(turtle: TurtleCommander, pos: Vec3, chunk: Vec
Some(()) Some(())
} }
/// Send mined turtles to the nearest depot
async fn devore(turtle: &TurtleCommander) {
let turtles: Vec<u32> = turtle.inventory().await.into_iter().enumerate()
.filter(|(_,b)| b.as_ref().is_some_and(|b| b.name.contains("turtle")))
.map(|(i,_)| i as u32).collect();
if turtles.is_empty() {
return;
}
let depot = turtle.get_depot().await;
for i in turtles {
let position = depot.position();
let staging = position.pos - position.dir.unit();
turtle.goto(Position::new(staging, position.dir)).await;
turtle.execute(Select(i as u32)).await;
turtle.execute(Place).await;
turtle.execute(CycleFront).await;
loop {
let ret = turtle.execute(Wait(3)).await;
// this won't do well with dead (energy-lacking) turtles, perhaps obtaining
// a new depot (lock) for every turtle is more consistent
//
// alternatively, figure out label parsing (names with spaces)
// and issue a command to the child turtle
if !ret.ahead.contains("turtle") {
break;
}
warn!("devored turtle still inactive");
}
}
}
async fn near_valuables(turtle: &TurtleCommander, pos: Vec3, chunk: Vec3) -> Vec<Vec3> { async fn near_valuables(turtle: &TurtleCommander, pos: Vec3, chunk: Vec3) -> Vec<Vec3> {
let scan = (0..(chunk*2).product()).map(|n| fill(chunk * 2, n) - chunk/2); let scan = (0..(chunk*2).product()).map(|n| fill(chunk * 2, n) - chunk/2);
@ -227,7 +265,7 @@ pub struct Quarry {
size: Vec3, size: Vec3,
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
miners: Arc<AtomicUsize>, miners: Arc<AtomicUsize>,
pub progress: ChunkedTask, progress: ChunkedTask,
} }
impl Quarry { impl Quarry {

View file

@ -3,6 +3,7 @@ use crate::blocks::Direction;
use crate::blocks::Position; use crate::blocks::Position;
use crate::blocks::Vec3; use crate::blocks::Vec3;
use crate::blocks::SharedWorld; use crate::blocks::SharedWorld;
use crate::depot::DepotGuard;
use crate::depot::Depots; use crate::depot::Depots;
use crate::paths::route_facing; use crate::paths::route_facing;
@ -293,6 +294,10 @@ impl TurtleCommander {
self.depots.dock(self.clone()).await self.depots.dock(self.clone()).await
} }
pub async fn get_depot(&self) -> DepotGuard {
self.depots.nearest(self.pos().await).await
}
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub async fn goto(&self, pos: Position) -> Option<()> { pub async fn goto(&self, pos: Position) -> Option<()> {
let mut recent = self.pos().await; let mut recent = self.pos().await;
@ -512,6 +517,7 @@ pub enum TurtleCommand {
Update, Update,
Poweroff, Poweroff,
Refuel, Refuel,
CycleFront,
} }
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]