Compare commits
4 Commits
9353ccba8a
...
a9f6adcd9d
| Author | SHA1 | Date | |
|---|---|---|---|
|
a9f6adcd9d
|
|||
|
757d7098cf
|
|||
|
4e69bda3c8
|
|||
|
755e5ff6b4
|
@@ -48,6 +48,53 @@ class TurtleCtl extends CLIAppBase {
|
|||||||
handle.writeLine('${lvl}/${limit} (${(lvl / limit) * 100}%)');
|
handle.writeLine('${lvl}/${limit} (${(lvl / limit) * 100}%)');
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
registerAsyncSubcommand("fuel-sources", (args) -> {
|
||||||
|
return asynPerform(() -> {
|
||||||
|
var items = InvManager.getCombustableItems();
|
||||||
|
|
||||||
|
for (i in items) {
|
||||||
|
handle.writeLine(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Success(null);
|
||||||
|
}, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
registerAsyncSubcommand("refuel", (args) -> {
|
||||||
|
var refuelTo = Turtle.getFuelLimit();
|
||||||
|
if (args.length > 0) {
|
||||||
|
var split = args[0].split("%");
|
||||||
|
|
||||||
|
if (split.length > 1) {
|
||||||
|
// Is percentage
|
||||||
|
var parsed = Std.parseFloat(split[0]);
|
||||||
|
|
||||||
|
if (parsed == null) {
|
||||||
|
handle.writeLine("Failed to parse ammount");
|
||||||
|
return Future.sync(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
refuelTo = Math.round(refuelTo * (parsed / 100));
|
||||||
|
} else {
|
||||||
|
// Is absolute
|
||||||
|
var parsed = Std.parseInt(args[0]);
|
||||||
|
|
||||||
|
if (parsed == null) {
|
||||||
|
handle.writeLine("Failed to parse ammount");
|
||||||
|
return Future.sync(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
refuelTo = parsed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return asynPerform(() -> {
|
||||||
|
InvManager.refuel(refuelTo, []);
|
||||||
|
|
||||||
|
return Success(null);
|
||||||
|
}, 1);
|
||||||
|
}, "<to|to%>");
|
||||||
}
|
}
|
||||||
|
|
||||||
private function asynPerform(op:Void->Outcome<Noise, String>, times:Int):Future<Bool> {
|
private function asynPerform(op:Void->Outcome<Noise, String>, times:Int):Future<Bool> {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package lib;
|
|||||||
/**
|
/**
|
||||||
Represents an item in the game.
|
Represents an item in the game.
|
||||||
**/
|
**/
|
||||||
abstract Item(String) to String {
|
enum abstract Item(String) to String {
|
||||||
public inline function new(name:String) {
|
public inline function new(name:String) {
|
||||||
// Check if the name is valid. in the format `mod:item_name` e.g. `minecraft:apple`
|
// Check if the name is valid. in the format `mod:item_name` e.g. `minecraft:apple`
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
@@ -19,4 +19,7 @@ abstract Item(String) to String {
|
|||||||
function getBase():String {
|
function getBase():String {
|
||||||
return this.split(":")[0];
|
return this.split(":")[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var Coal = "minecraft:coal";
|
||||||
|
var Charcoal = "minecraft:charcoal";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,4 +120,117 @@ class InvManager {
|
|||||||
return Failure(failure);
|
return Failure(failure);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Check what items in the inventory can be used as fuel.
|
||||||
|
**/
|
||||||
|
public static function getCombustableItems():Array<Item> {
|
||||||
|
var invState = new InvState();
|
||||||
|
var rtn = new Map<Item, Noise>();
|
||||||
|
|
||||||
|
for (slot => v in invState) {
|
||||||
|
if (rtn.exists(v.name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Turtle.selectSlot(slot);
|
||||||
|
var result = Turtle.canRefultWithSlot();
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
rtn.set(v.name, true); // How do i set a Noise type???
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [for (k => _ in rtn) k];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Refule to a specific value.
|
||||||
|
Use array items first (in order).
|
||||||
|
Set `useAll` to false to don't use items not listes in priority.
|
||||||
|
**/
|
||||||
|
public static function refuel(to:Int, priority:Array<Item>, useAll:Bool = true) {
|
||||||
|
var invState = new InvState();
|
||||||
|
var fuelTo = MathI.min(to, Turtle.getFuelLimit());
|
||||||
|
|
||||||
|
if (Turtle.getFuelLevel() >= fuelTo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var items = invState.getSlotsForItems();
|
||||||
|
|
||||||
|
for (item in priority) {
|
||||||
|
if (!items.exists(item)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var allSlotsWithItemOrderd = items.get(item);
|
||||||
|
allSlotsWithItemOrderd.sort((a, b) -> {
|
||||||
|
return invState.get(a).count - invState.get(b).count;
|
||||||
|
});
|
||||||
|
|
||||||
|
var fuelPerItem = 0;
|
||||||
|
|
||||||
|
for (slot in allSlotsWithItemOrderd) {
|
||||||
|
var slotInfo = invState.get(slot);
|
||||||
|
Turtle.selectSlot(slot);
|
||||||
|
|
||||||
|
if (fuelPerItem == 0) {
|
||||||
|
fuelPerItem = refuelFromSelectedSlot(slotInfo.count, fuelTo);
|
||||||
|
if (fuelPerItem == 0) {
|
||||||
|
Log.warn('Item ${slotInfo.name} is not combustable');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var itemsToUse = MathI.min(Math.ceil((fuelTo - (Turtle.getFuelLevel())) / fuelPerItem), slotInfo.count);
|
||||||
|
Turtle.refuel(itemsToUse);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Turtle.getFuelLevel() >= fuelTo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not reached our goal yet, check all other items to use as fuel.
|
||||||
|
if (!useAll) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (slot => slotInfo in invState) {
|
||||||
|
// Skip items that we already checked
|
||||||
|
// Also the invstate might be invalid for these slots
|
||||||
|
if (priority.contains(slotInfo.name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Turtle.selectSlot(slot);
|
||||||
|
refuelFromSelectedSlot(slotInfo.count, fuelTo);
|
||||||
|
|
||||||
|
if (Turtle.getFuelLevel() >= fuelTo) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Refuel from selected slot. The ammount of items in the slot must be given.
|
||||||
|
The `to` arg should be the desired fuel level.
|
||||||
|
Returns the ammount of fuel on item of this kind refuels.
|
||||||
|
Returns 0 of item is not combustable.
|
||||||
|
**/
|
||||||
|
private static function refuelFromSelectedSlot(count:Int, to:Int):Int {
|
||||||
|
var fuelLevel = Turtle.getFuelLevel();
|
||||||
|
|
||||||
|
if (!Turtle.refuel(1).isSuccess()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fuelPerItem = Turtle.getFuelLevel() - fuelLevel;
|
||||||
|
var itemsToUse = MathI.min(Math.ceil((to - (fuelLevel + fuelPerItem)) / fuelPerItem), count - 1);
|
||||||
|
|
||||||
|
Turtle.refuel(itemsToUse);
|
||||||
|
|
||||||
|
return fuelPerItem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,4 +96,16 @@ abstract InvState(Map<TurtleSlot, {count:Int, name:Item, max:Int}>) {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Get all slots with items sorted from low to high.
|
||||||
|
**/
|
||||||
|
public function getSlotsSortedByCount():Array<TurtleSlot> {
|
||||||
|
var keys = [for (k in this.keys()) k];
|
||||||
|
keys.sort((a, b) -> {
|
||||||
|
return this.get(a).count - this.get(b).count;
|
||||||
|
});
|
||||||
|
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user