added fuel limit
This commit is contained in:
parent
5e9a99e030
commit
598fdf4df6
3 changed files with 26 additions and 18 deletions
|
@ -25,18 +25,13 @@ local function update()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function cycle(func, n)
|
|
||||||
for i = 1, n, 1 do
|
|
||||||
if not func() then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
local function cyclefn(fn)
|
local function cyclefn(fn)
|
||||||
return function (n)
|
return function (n)
|
||||||
cycle(fn, n)
|
for i = 1, n, 1 do
|
||||||
|
if not fn() then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -96,6 +91,7 @@ local backoff = 0;
|
||||||
|
|
||||||
if not idfile then
|
if not idfile then
|
||||||
local fuel = turtle.getFuelLevel()
|
local fuel = turtle.getFuelLevel()
|
||||||
|
local maxfuel = turtle.getFuelLimit()
|
||||||
if fs.exists("/disk/pos") then
|
if fs.exists("/disk/pos") then
|
||||||
io.input("/disk/pos")
|
io.input("/disk/pos")
|
||||||
else
|
else
|
||||||
|
@ -113,15 +109,21 @@ if not idfile then
|
||||||
|
|
||||||
local info = {
|
local info = {
|
||||||
fuel = fuel,
|
fuel = fuel,
|
||||||
|
fuellimit = maxfuel,
|
||||||
position = {x, y, z},
|
position = {x, y, z},
|
||||||
facing = direction,
|
facing = direction,
|
||||||
}
|
}
|
||||||
-- TODO: get from boot floppy
|
::request::
|
||||||
local turtleinfo = http.post(
|
local turtleinfo = http.post(
|
||||||
endpoint .. "/turtle/new",
|
endpoint .. "/turtle/new",
|
||||||
textutils.serializeJSON(info),
|
textutils.serializeJSON(info),
|
||||||
{ ["Content-Type"] = "application/json" }
|
{ ["Content-Type"] = "application/json" }
|
||||||
)
|
)
|
||||||
|
if not turtleinfo then
|
||||||
|
print("server not responding")
|
||||||
|
sleep(1)
|
||||||
|
goto request
|
||||||
|
end
|
||||||
local response = textutils.unserialiseJSON(turtleinfo.readAll())
|
local response = textutils.unserialiseJSON(turtleinfo.readAll())
|
||||||
|
|
||||||
idfile = fs.open("id", "w")
|
idfile = fs.open("id", "w")
|
||||||
|
@ -161,11 +163,9 @@ repeat
|
||||||
else
|
else
|
||||||
ret_table = "Failure"
|
ret_table = "Failure"
|
||||||
end
|
end
|
||||||
else
|
elseif ret then
|
||||||
ret_table = ret
|
ret_table = ret
|
||||||
end
|
else
|
||||||
|
|
||||||
if not ret_table then
|
|
||||||
ret_table = "None"
|
ret_table = "None"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ impl LiveState {
|
||||||
let mut turtles = Vec::new();
|
let mut turtles = Vec::new();
|
||||||
for turtle in save.turtles.into_iter() {
|
for turtle in save.turtles.into_iter() {
|
||||||
let (tx, rx) = mpsc::channel(1);
|
let (tx, rx) = mpsc::channel(1);
|
||||||
turtles.push(Turtle::with_channel(turtle.name.to_num(), turtle.position, turtle.fuel, tx, rx));
|
turtles.push(Turtle::with_channel(turtle.name.to_num(), turtle.position, turtle.fuel, turtle.fuel_limit, tx, rx));
|
||||||
};
|
};
|
||||||
Self { turtles: turtles.into_iter().map(|t| Arc::new(RwLock::new(t))).collect(), tasks: Vec::new(), world: World::from_tree(save.world) }
|
Self { turtles: turtles.into_iter().map(|t| Arc::new(RwLock::new(t))).collect(), tasks: Vec::new(), world: World::from_tree(save.world) }
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ async fn create_turtle(
|
||||||
let (send, receive) = mpsc::channel(1);
|
let (send, receive) = mpsc::channel(1);
|
||||||
state.turtles.push(
|
state.turtles.push(
|
||||||
Arc::new(RwLock::new(
|
Arc::new(RwLock::new(
|
||||||
turtle::Turtle::with_channel(id, Position::new(req.position, req.facing), req.fuel, send,receive)
|
turtle::Turtle::with_channel(id, Position::new(req.position, req.facing), req.fuel, req.fuellimit, send,receive)
|
||||||
)));
|
)));
|
||||||
state.tasks.push(VecDeque::new());
|
state.tasks.push(VecDeque::new());
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ const IDLE_TIME: u32 = 3;
|
||||||
pub(crate) struct Turtle {
|
pub(crate) struct Turtle {
|
||||||
pub(crate) name: Name,
|
pub(crate) name: Name,
|
||||||
pub(crate) fuel: usize,
|
pub(crate) fuel: usize,
|
||||||
|
pub(crate) fuel_limit: usize,
|
||||||
/// 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,
|
||||||
|
@ -85,6 +86,7 @@ impl Default for Turtle {
|
||||||
Self {
|
Self {
|
||||||
name: Name::from_num(0),
|
name: Name::from_num(0),
|
||||||
fuel: Default::default(),
|
fuel: 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,
|
goal: None,
|
||||||
|
@ -121,10 +123,11 @@ impl Turtle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_channel(id: u32, position: Position, fuel: usize, sender: Sender, receiver: Receiver) -> Self {
|
pub fn with_channel(id: u32, position: Position, fuel: usize, fuel_limit: usize, sender: Sender, receiver: Receiver) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: Name::from_num(id),
|
name: Name::from_num(id),
|
||||||
fuel,
|
fuel,
|
||||||
|
fuel_limit,
|
||||||
queued_movement: Vec3::new(0, 0, 0),
|
queued_movement: Vec3::new(0, 0, 0),
|
||||||
position,
|
position,
|
||||||
pending_update: true,
|
pending_update: true,
|
||||||
|
@ -169,6 +172,10 @@ impl TurtleCommander {
|
||||||
self.turtle.read().await.fuel
|
self.turtle.read().await.fuel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn fuel_limit(&self) -> usize {
|
||||||
|
self.turtle.read().await.fuel_limit
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn world(&self) -> World {
|
pub async fn world(&self) -> World {
|
||||||
self.world.clone()
|
self.world.clone()
|
||||||
}
|
}
|
||||||
|
@ -392,6 +399,7 @@ pub(crate) struct TurtleUpdate {
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub(crate) struct TurtleRegister {
|
pub(crate) struct TurtleRegister {
|
||||||
pub(crate) fuel: usize,
|
pub(crate) fuel: usize,
|
||||||
|
pub(crate) fuellimit: usize,
|
||||||
pub(crate) position: Vec3,
|
pub(crate) position: Vec3,
|
||||||
pub(crate) facing: Direction,
|
pub(crate) facing: Direction,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue