Compare commits
3 Commits
90b4015ba1
...
2799a0be3d
| Author | SHA1 | Date | |
|---|---|---|---|
|
2799a0be3d
|
|||
|
e527dd5b6a
|
|||
|
fe17b4fd67
|
@@ -1,5 +1,6 @@
|
|||||||
package bin;
|
package bin;
|
||||||
|
|
||||||
|
import kernel.turtle.Types.ToolSide;
|
||||||
import lib.turtle.Helper;
|
import lib.turtle.Helper;
|
||||||
import kernel.turtle.TurtleMutex;
|
import kernel.turtle.TurtleMutex;
|
||||||
import kernel.turtle.Turtle;
|
import kernel.turtle.Turtle;
|
||||||
@@ -116,6 +117,28 @@ class TurtleCtl extends CLIAppBase {
|
|||||||
var len = args.getInt("length");
|
var len = args.getInt("length");
|
||||||
return asynPerform(Helper.combine.bind([Turtle.digEmpty.bind(Front), Turtle.forward, Turtle.digEmpty.bind(Up)]), len);
|
return asynPerform(Helper.combine.bind([Turtle.digEmpty.bind(Front), Turtle.forward, Turtle.digEmpty.bind(Up)]), len);
|
||||||
}, [Int("length")]);
|
}, [Int("length")]);
|
||||||
|
|
||||||
|
registerSyncSubcommand("inspect", (args) -> {
|
||||||
|
var res = Turtle.inspect(Front);
|
||||||
|
|
||||||
|
switch res {
|
||||||
|
case Failure(err):
|
||||||
|
handle.writeLine("Failed: " + err);
|
||||||
|
return false;
|
||||||
|
case Success(data):
|
||||||
|
handle.writeLine('Name: ${data.name}');
|
||||||
|
handle.writeLine("Tags:");
|
||||||
|
|
||||||
|
for (tag in data.tags.sortByName()) {
|
||||||
|
handle.writeLine(' ${tag}');
|
||||||
|
}
|
||||||
|
|
||||||
|
handle.writeLine("State:");
|
||||||
|
handle.writeLine(data.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function asynPerform(op:Void->Outcome<Noise, String>, times:Int):Future<Bool> {
|
private function asynPerform(op:Void->Outcome<Noise, String>, times:Int):Future<Bool> {
|
||||||
|
|||||||
76
src/kernel/peripherals/GeoScanner.hx
Normal file
76
src/kernel/peripherals/GeoScanner.hx
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package kernel.peripherals;
|
||||||
|
|
||||||
|
import lib.BlockPos;
|
||||||
|
import lib.Tags;
|
||||||
|
import cc.Peripheral;
|
||||||
|
|
||||||
|
using tink.CoreApi;
|
||||||
|
using lua.Table;
|
||||||
|
|
||||||
|
@:structInit typedef ScanBlock = {
|
||||||
|
name:String,
|
||||||
|
tags:Tags,
|
||||||
|
pos:BlockPos
|
||||||
|
}
|
||||||
|
|
||||||
|
class GeoScanner implements IPeripheral {
|
||||||
|
public static inline final TYPE_NAME:String = "geoScanner";
|
||||||
|
|
||||||
|
private final addr:String;
|
||||||
|
|
||||||
|
public function new(addr:String) {
|
||||||
|
this.addr = addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAddr():String {
|
||||||
|
return this.addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getType():String {
|
||||||
|
return TYPE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the current time remaining until then next scan() can be ran.
|
||||||
|
**/
|
||||||
|
public function getScanCooldown():Int {
|
||||||
|
return Peripheral.call(this.addr, "getScanCooldown");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the cost in FE for a scan with the given radius.
|
||||||
|
Returns null of scan of this radius is not posible.
|
||||||
|
**/
|
||||||
|
public function cost(radius:Int):Null<Int> {
|
||||||
|
return Peripheral.call(this.addr, "cost", radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a list of data about all blocks in the radius.
|
||||||
|
X,Y,Z are relative to the geoscanner block.
|
||||||
|
Air blocks are not included.
|
||||||
|
List is unsorted.
|
||||||
|
**/
|
||||||
|
public function scan(radius:Int):Outcome<Array<ScanBlock>, String> {
|
||||||
|
// TODO: Handel fail state
|
||||||
|
var result:lua.Table<Int, {
|
||||||
|
x:Int,
|
||||||
|
y:Int,
|
||||||
|
z:Int,
|
||||||
|
name:String,
|
||||||
|
tags:lua.Table<Int, String>
|
||||||
|
}> = Peripheral.call(this.addr, "scan", radius);
|
||||||
|
|
||||||
|
return Success(result.toArray().map((e) -> {
|
||||||
|
name: e.name,
|
||||||
|
tags: Tags.fromIntTable(e.tags),
|
||||||
|
pos: new BlockPos(e.x, e.y, e.z)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function chunkAnalyze():Outcome<Map<String, Int>, String> {
|
||||||
|
var result:lua.Table<String, Int> = Peripheral.call(this.addr, "chunkAnalyze");
|
||||||
|
|
||||||
|
return Success(result.toMap());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package kernel.peripherals;
|
package kernel.peripherals;
|
||||||
|
|
||||||
import kernel.log.Log;
|
|
||||||
import kernel.peripherals.Modem;
|
import kernel.peripherals.Modem;
|
||||||
import kernel.peripherals.Screen;
|
import kernel.peripherals.Screen;
|
||||||
|
|
||||||
@@ -83,6 +82,8 @@ class Peripheral {
|
|||||||
return getRedstone(addr);
|
return getRedstone(addr);
|
||||||
case Inventory.TYPE_NAME:
|
case Inventory.TYPE_NAME:
|
||||||
return getInventory(addr);
|
return getInventory(addr);
|
||||||
|
case GeoScanner.TYPE_NAME:
|
||||||
|
return getGeoScanner(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -180,4 +181,15 @@ class Peripheral {
|
|||||||
public static function getAllInventorys():Array<Inventory> {
|
public static function getAllInventorys():Array<Inventory> {
|
||||||
return [for (addr in findAddrByType(Inventory.TYPE_NAME)) new Inventory(addr)];
|
return [for (addr in findAddrByType(Inventory.TYPE_NAME)) new Inventory(addr)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getGeoScanner(addr:String):Null<GeoScanner> {
|
||||||
|
var addr = safeGetAddr(addr, GeoScanner.TYPE_NAME);
|
||||||
|
if (addr == null)
|
||||||
|
return null;
|
||||||
|
return new GeoScanner(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAllGeoScanners():Array<GeoScanner> {
|
||||||
|
return [for (addr in findAddrByType(GeoScanner.TYPE_NAME)) new GeoScanner(addr)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/lib/Block.hx
Normal file
12
src/lib/Block.hx
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
class Block {
|
||||||
|
public final name:PaN;
|
||||||
|
public final tags:Tags;
|
||||||
|
|
||||||
|
public function new(name:String, tags:Tags) {
|
||||||
|
this.name = name;
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
|
}
|
||||||
114
src/lib/BlockBlockMap.hx
Normal file
114
src/lib/BlockBlockMap.hx
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
import kernel.peripherals.GeoScanner.ScanBlock;
|
||||||
|
import lib.BlockMap;
|
||||||
|
|
||||||
|
@:forward
|
||||||
|
abstract BlockBlockMap(BlockMap<Block>) {
|
||||||
|
public inline function new() {
|
||||||
|
this = new BlockMap<Block>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromScan(scan:Array<ScanBlock>):BlockBlockMap {
|
||||||
|
var map:BlockBlockMap = new BlockBlockMap();
|
||||||
|
|
||||||
|
for (block in scan) {
|
||||||
|
map.set(block.pos, new Block(block.name, block.tags));
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Navigate from one point into another.
|
||||||
|
Returns the path to take.
|
||||||
|
Based on A*
|
||||||
|
**/
|
||||||
|
public function nav(from:BlockPos, to:BlockPos):Array<BlockPos> {
|
||||||
|
if (!canPass(to)) {
|
||||||
|
trace("End is blocked");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
var openSet = new PriorityQueue();
|
||||||
|
openSet.insert(from, 0);
|
||||||
|
|
||||||
|
var cameFrom:BlockMap<BlockPos> = new BlockMap();
|
||||||
|
|
||||||
|
var gCost:BlockMap<Int> = new BlockMap(); // G cost is the distance from start
|
||||||
|
gCost.set(from, 0);
|
||||||
|
|
||||||
|
var fCost:BlockMap<Float> = new BlockMap(); // F cost is the combines cost of moving to this pos (G cost) + the distnace to the end
|
||||||
|
fCost.set(from, from.distance(to));
|
||||||
|
|
||||||
|
while (!openSet.isEmpty()) {
|
||||||
|
var current = openSet.extractMin(); // Check the pos with the lowest F cost
|
||||||
|
|
||||||
|
if (current.equals(to)) {
|
||||||
|
var totalPath = [];
|
||||||
|
var currentPathPos = to;
|
||||||
|
|
||||||
|
while (cameFrom.exists(currentPathPos)) {
|
||||||
|
totalPath.unshift(currentPathPos);
|
||||||
|
currentPathPos = cameFrom.get(currentPathPos);
|
||||||
|
}
|
||||||
|
totalPath.unshift(from);
|
||||||
|
|
||||||
|
return totalPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (neighbor in current.neighbors()) {
|
||||||
|
if (!canPass(neighbor)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var newGCost = gCost.get(current) + 1; // Movment cost to neighbor is always 1 as we can't move diagonally
|
||||||
|
|
||||||
|
if (newGCost < (gCost.get(neighbor) ?? 9999)) {
|
||||||
|
cameFrom.set(neighbor, current);
|
||||||
|
gCost.set(neighbor, newGCost);
|
||||||
|
fCost.set(neighbor, newGCost + neighbor.distance(to));
|
||||||
|
|
||||||
|
if (!openSet.containsElement(neighbor)) {
|
||||||
|
openSet.insert(neighbor, fCost.get(neighbor));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Some block can be passed by the turtle.
|
||||||
|
Some blocks get destroyed by it.
|
||||||
|
**/
|
||||||
|
public function canPass(k:BlockPos):Bool {
|
||||||
|
var block = this.get(k);
|
||||||
|
if (block == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch block.name {
|
||||||
|
case "minecraft:water":
|
||||||
|
return true;
|
||||||
|
case "minecaft:lava":
|
||||||
|
case "mimecraft:grass":
|
||||||
|
case "minecraft:hanging_roots":
|
||||||
|
case "minecraft:warped_roots":
|
||||||
|
case "minecraft:dead_bush":
|
||||||
|
case "minecraft:fern":
|
||||||
|
case "minecraft:vine":
|
||||||
|
case "minecraft:glow_lichen":
|
||||||
|
case "minecraft:seagrass":
|
||||||
|
case "minecraft:crimson_roots":
|
||||||
|
case "minecraft:nether_sprouts":
|
||||||
|
case "minecraft:snow":
|
||||||
|
case "minecraft:rose_bush":
|
||||||
|
case "minecraft:fire":
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/lib/BlockMap.hx
Normal file
48
src/lib/BlockMap.hx
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package lib;
|
||||||
|
|
||||||
|
import haxe.ds.IntMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Map values to positions in 3D space. For representing blocks from a scan see `BlockBlockMap`.
|
||||||
|
|
||||||
|
The only reason this exsists is that the abstract class for BlockPos does not
|
||||||
|
satisfy the constraint of HashMap. The hashCode function needs to be on a real class and not on
|
||||||
|
an abstract class. Thanks Obama.
|
||||||
|
**/
|
||||||
|
@:forward(iterator, clear)
|
||||||
|
abstract BlockMap<T>(IntMap<T>) {
|
||||||
|
/**
|
||||||
|
Creates a new HashMap.
|
||||||
|
**/
|
||||||
|
public inline function new() {
|
||||||
|
this = new IntMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.set`
|
||||||
|
**/
|
||||||
|
@:arrayAccess public inline function set(k:BlockPos, v:T) {
|
||||||
|
this.set(k.hashCode(), v);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.get`
|
||||||
|
**/
|
||||||
|
@:arrayAccess public inline function get(k:BlockPos) {
|
||||||
|
return this.get(k.hashCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.exists`
|
||||||
|
**/
|
||||||
|
public inline function exists(k:BlockPos) {
|
||||||
|
return this.exists(k.hashCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
See `Map.remove`
|
||||||
|
**/
|
||||||
|
public inline function remove(k:BlockPos) {
|
||||||
|
return this.remove(k.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user