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
|
||||
end
|
||||
|
||||
local function cycle(func, n)
|
||||
local function cyclefn(fn)
|
||||
return function (n)
|
||||
for i = 1, n, 1 do
|
||||
if not func() then
|
||||
if not fn() then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function cyclefn(fn)
|
||||
return function (n)
|
||||
cycle(fn, n)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -96,6 +91,7 @@ local backoff = 0;
|
|||
|
||||
if not idfile then
|
||||
local fuel = turtle.getFuelLevel()
|
||||
local maxfuel = turtle.getFuelLimit()
|
||||
if fs.exists("/disk/pos") then
|
||||
io.input("/disk/pos")
|
||||
else
|
||||
|
@ -113,15 +109,21 @@ if not idfile then
|
|||
|
||||
local info = {
|
||||
fuel = fuel,
|
||||
fuellimit = maxfuel,
|
||||
position = {x, y, z},
|
||||
facing = direction,
|
||||
}
|
||||
-- TODO: get from boot floppy
|
||||
::request::
|
||||
local turtleinfo = http.post(
|
||||
endpoint .. "/turtle/new",
|
||||
textutils.serializeJSON(info),
|
||||
{ ["Content-Type"] = "application/json" }
|
||||
)
|
||||
if not turtleinfo then
|
||||
print("server not responding")
|
||||
sleep(1)
|
||||
goto request
|
||||
end
|
||||
local response = textutils.unserialiseJSON(turtleinfo.readAll())
|
||||
|
||||
idfile = fs.open("id", "w")
|
||||
|
@ -161,11 +163,9 @@ repeat
|
|||
else
|
||||
ret_table = "Failure"
|
||||
end
|
||||
else
|
||||
elseif ret then
|
||||
ret_table = ret
|
||||
end
|
||||
|
||||
if not ret_table then
|
||||
else
|
||||
ret_table = "None"
|
||||
end
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ impl LiveState {
|
|||
let mut turtles = Vec::new();
|
||||
for turtle in save.turtles.into_iter() {
|
||||
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) }
|
||||
}
|
||||
|
@ -139,7 +139,7 @@ async fn create_turtle(
|
|||
let (send, receive) = mpsc::channel(1);
|
||||
state.turtles.push(
|
||||
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());
|
||||
|
||||
|
|
|
@ -44,6 +44,7 @@ const IDLE_TIME: u32 = 3;
|
|||
pub(crate) struct Turtle {
|
||||
pub(crate) name: Name,
|
||||
pub(crate) fuel: usize,
|
||||
pub(crate) fuel_limit: usize,
|
||||
/// movement vector of last given command
|
||||
pub(crate) queued_movement: Vec3,
|
||||
pub(crate) position: Position,
|
||||
|
@ -85,6 +86,7 @@ impl Default for Turtle {
|
|||
Self {
|
||||
name: Name::from_num(0),
|
||||
fuel: Default::default(),
|
||||
fuel_limit: Default::default(),
|
||||
queued_movement: Default::default(),
|
||||
position: Position::new(Vec3::zeros(), Direction::North),
|
||||
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 {
|
||||
name: Name::from_num(id),
|
||||
fuel,
|
||||
fuel_limit,
|
||||
queued_movement: Vec3::new(0, 0, 0),
|
||||
position,
|
||||
pending_update: true,
|
||||
|
@ -169,6 +172,10 @@ impl TurtleCommander {
|
|||
self.turtle.read().await.fuel
|
||||
}
|
||||
|
||||
pub async fn fuel_limit(&self) -> usize {
|
||||
self.turtle.read().await.fuel_limit
|
||||
}
|
||||
|
||||
pub async fn world(&self) -> World {
|
||||
self.world.clone()
|
||||
}
|
||||
|
@ -392,6 +399,7 @@ pub(crate) struct TurtleUpdate {
|
|||
#[derive(Serialize, Deserialize)]
|
||||
pub(crate) struct TurtleRegister {
|
||||
pub(crate) fuel: usize,
|
||||
pub(crate) fuellimit: usize,
|
||||
pub(crate) position: Vec3,
|
||||
pub(crate) facing: Direction,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue