Compare commits

...

5 Commits

7 changed files with 88 additions and 19 deletions

View File

@@ -1,5 +1,6 @@
package bin;
import kernel.EndOfLoop;
import lua.NativeStringTools;
import kernel.binstore.BinStore;
import kernel.ps.ProcessHandle;
@@ -75,6 +76,15 @@ class Terminal implements Process {
}));
this.requestRender();
if (handle.args.length > 0) {
var arg1 = handle.args[0];
this.backlog.push("> " + arg1);
EndOfLoop.endOfLoop(() -> {
this.invokeCommand(arg1);
});
}
}
private function stopCurrentPS() {

View File

@@ -1,5 +1,6 @@
package bin;
import lib.turtle.InvManager;
import lib.CLIAppBase;
using tink.CoreApi;
@@ -30,6 +31,15 @@ class Turtle extends CLIAppBase {
registerSyncSubcommand("down", (args) -> {
return checkAvailable() && perform(kernel.turtle.Turtle.down());
});
registerSyncSubcommand("defrag", (args) -> {
if (!checkAvailable()) {
return false;
}
InvManager.defrag();
return true;
});
}
private function checkAvailable():Bool {

View File

@@ -106,7 +106,7 @@ class ProcessHandle {
}
public function get_args():ReadOnlyArray<String> {
return this.config.args;
return this.config.args != null ? this.config.args : [];
}
public function claimTurtleMutex():Bool {

View File

@@ -119,23 +119,17 @@ class Turtle {
}
public static function selectSlot(slot:TurtleSlot):Outcome<Noise, Noise> {
// TODO: slot in bounds?
var r = cc.Turtle.select(slot + 1);
var r = cc.Turtle.select(slot.toCCSlot());
return (r) ? Outcome.Success(null) : Outcome.Failure("Slot out of bounds");
}
public static function getItemCount(?slot:TurtleSlot):Int {
// TODO: slot in bounds?
return cc.Turtle.getItemCount(slot + 1);
return cc.Turtle.getItemCount(slot.toCCSlot());
}
public static function getItemSpace(?slot:TurtleSlot):Int {
// TODO: slot in bounds?
return cc.Turtle.getItemSpace(slot + 1);
return cc.Turtle.getItemSpace(slot.toCCSlot());
}
public static function detect(dir:InteractDirections):Bool {
@@ -208,12 +202,12 @@ class Turtle {
}
public static function compareSlot(otherSlot:TurtleSlot):Bool {
return cc.Turtle.compareTo(otherSlot + 1);
return cc.Turtle.compareTo(otherSlot.toCCSlot());
}
public static function transfer(from:TurtleSlot, to:TurtleSlot, ?count:Int):Outcome<Noise, Noise> {
selectSlot(from);
var r = cc.Turtle.transferTo(to + 1, count);
var r = cc.Turtle.transferTo(to.toCCSlot(), count);
return r ? Outcome.Success(null) : Outcome.Failure(null);
}
@@ -258,7 +252,7 @@ class Turtle {
}
public static function getItemDetail(slot:TurtleSlot, ?detailed:Bool = false):Option<ItemInspect> {
var r = cc.Turtle.getItemDetail(slot + 1); // FIXME: can take detailed as flag. Has to be fixed upstream
var r = cc.Turtle.getItemDetail(slot.toCCSlot()); // FIXME: can take detailed as flag. Has to be fixed upstream
if (r == null) {
return None;

View File

@@ -0,0 +1,26 @@
package kernel.turtle;
using tink.CoreApi;
/**
The slot nummber for turtle inventory. 0 based. Assured to be in range.
**/
abstract TurtleSlot(Int) to Int {
public function new(i:Int) {
if (i >= 0 && i < Turtle.MAX_SLOTS) {
this = i;
} else {
throw new Error("Slot not in range: " + i);
}
}
@:from
public static function fromInt(i:Int) {
return new TurtleSlot(i);
}
@:allow(kernel.turtle)
private inline function toCCSlot():Int {
return this + 1;
}
}

View File

@@ -22,8 +22,3 @@ typedef ItemInspect = {
public var damage:Int;
public var count:Int;
}
/**
The slot nummber for turtle inventory. 0 based.
**/
typedef TurtleSlot = Int;

View File

@@ -1,7 +1,8 @@
package lib.turtle;
import kernel.turtle.Types.ToolSide;
import kernel.log.Log;
import kernel.turtle.Types.TurtleSlot;
import kernel.turtle.TurtleSlot;
import kernel.turtle.Types.InteractDirections;
import kernel.turtle.Turtle;
@@ -103,6 +104,21 @@ class InvManager {
return rtn;
}
/**
Returns the first slot that contains this item.
**/
public static function findItemSlot(item:Item):Null<TurtleSlot> {
var invState = getInvState();
for (k => slot in invState) {
if (slot.name == item) {
return k;
}
}
return null;
}
/**
Cleans up turtle inventory. Moves items together.
This should be run in a turtle thread.
@@ -180,4 +196,22 @@ class InvManager {
}
}
}
public static function equipTool(item:Item, side:ToolSide):Outcome<TurtleSlot, String> {
var toolSlot = findItemSlot(item);
if (toolSlot == null) {
return Failure("Item not found");
}
Turtle.selectSlot(toolSlot);
var result = Turtle.equip(side);
switch result {
case Success(_):
return Success(toolSlot);
case Failure(failure):
return Failure(failure);
}
}
}