Skip to content

Commit

Permalink
Added nicer feedback to build:reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zangl committed Apr 24, 2014
1 parent ae756d9 commit d382608
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Minebot/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

version = "0.1.8"
version = "0.1.9"
// group= "net.famzangl.minecraft.minebot" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
sourceSets.main{
java{
Expand Down
6 changes: 5 additions & 1 deletion Minebot/src/net/famzangl/minecraft/minebot/MinebotMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.event.FMLInitializationEvent;

@Mod(modid = "minebot-mod", name = "Minebot", version = "0.1.8")
@Mod(modid = "minebot-mod", name = "Minebot", version = "0.1.9")
public class MinebotMod {
@Instance(value = "minebot-mod")
public static MinebotMod instance;
Expand All @@ -22,4 +22,8 @@ public void init(FMLInitializationEvent event) {
// FMLCommonHandler.instance().onPlayerPostTick(player)
}

public static String getVersion() {
return MinebotMod.class.getAnnotation(Mod.class).version();
}

}
31 changes: 23 additions & 8 deletions Minebot/src/net/famzangl/minecraft/minebot/ai/AIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ public void onPlayerTick(ClientTickEvent evt) {
desync = true;
} else {
timeout--;
task.runTick(this);
try {
task.runTick(this);
} catch (Throwable t) {
t.printStackTrace();
AIChatController
.addChatLine("Unexpected Error ("
+ t.getMessage()
+ "). Please report (and send the output on the console)!");
}
}
}
} else {
Expand Down Expand Up @@ -191,15 +199,22 @@ public void resetOnGameEnd(WorldEvent.Unload unload) {
dead = true;
buildManager.reset();
}

@SubscribeEvent
public void drawMarkers(RenderWorldLastEvent event) {
EntityLivingBase player = getMinecraft().renderViewEntity;
if (player.getHeldItem() != null && player.getHeldItem().getItem() instanceof MarkingAxe) {
double x = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) event.partialTicks;
double y = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) event.partialTicks;
double z = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) event.partialTicks;

if (player.getHeldItem() != null
&& player.getHeldItem().getItem() instanceof MarkingAxe) {
double x = player.lastTickPosX
+ (player.posX - player.lastTickPosX)
* (double) event.partialTicks;
double y = player.lastTickPosY
+ (player.posY - player.lastTickPosY)
* (double) event.partialTicks;
double z = player.lastTickPosZ
+ (player.posZ - player.lastTickPosZ)
* (double) event.partialTicks;

if (markerRenderer == null) {
markerRenderer = new MarkerRenderer();
}
Expand All @@ -212,7 +227,7 @@ public void positionMarkEvent(int x, int y, int z, int side) {
setPosition(pos, nextPosIsPos2);
nextPosIsPos2 ^= true;
}

private AIStrategy findNewStrategy() {
if (requestedStrategy != null) {
AIStrategy r = requestedStrategy;
Expand Down
29 changes: 16 additions & 13 deletions Minebot/src/net/famzangl/minecraft/minebot/ai/PathFinderField.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class PathFinderField implements Comparator<Integer> {
private static final long MAX_RUN_TIME = 200;
// Power of 2!
private static final int Y_LEVEL = 32;
private static int DELTA_X_Z = 100;
private static int SIZE_X_Z = 256;
private static int FIELD_SIZE = (1 << 16) * Y_LEVEL;

private PathFinderFieldData data = new PathFinderFieldData();
private final PriorityQueue<Dest> dests = new PriorityQueue<Dest>();
private Dest currentDest = null;
private final PriorityQueue<Integer> pq = new PriorityQueue<Integer>(100,
this);

Expand Down Expand Up @@ -43,20 +43,20 @@ public PathFinderField() {
}

protected int getIndexForBlock(int x, int y, int z) {
return ((x - data.offsetX) & 0xff) | (((z - data.offsetZ) & 0xff) << 8)
return ((x - data.offsetX) & (SIZE_X_Z - 1)) | (((z - data.offsetZ) & (SIZE_X_Z - 1)) << 8)
| (((y - data.offsetY) & (Y_LEVEL - 1)) << 16);
}

protected final int getX(int blockIndex) {
return (blockIndex & 0xff) + data.offsetX;
return (blockIndex & (SIZE_X_Z - 1)) + data.offsetX;
}

protected final int getY(int currentNode) {
return ((currentNode >> 16) & (Y_LEVEL - 1)) + data.offsetY;
}

protected final int getZ(int currentNode) {
return ((currentNode >> 8) & 0xff) + data.offsetZ;
return ((currentNode >> 8) & (SIZE_X_Z - 1)) + data.offsetZ;
}

private boolean isVisited(int blockIndex) {
Expand Down Expand Up @@ -155,18 +155,18 @@ public boolean searchSomethingAround(Pos playerPosition) {
}

public boolean searchSomethingAround(int cx, int cy, int cz) {
if (data.offsetX != cx - 128 || data.offsetY != cy - Y_LEVEL / 2
|| data.offsetZ != cz - 128) {
if (data.offsetX != cx - SIZE_X_Z / 2 || data.offsetY != cy - Y_LEVEL / 2
|| data.offsetZ != cz - SIZE_X_Z / 2) {
isRunning = false;
}
if (!isRunning) {
System.out.println("Restart");
field = new int[FIELD_SIZE];
data.offsetX = cx - 128;
data.offsetX = cx - SIZE_X_Z / 2;
data.offsetY = cy - Y_LEVEL / 2;
data.offsetZ = cz - 128;
data.offsetZ = cz - SIZE_X_Z / 2;
pq.clear();
dests.clear();
currentDest = null;
int start = getIndexForBlock(cx, cy, cz);
pq.add(start);
setDistance(start, 1);
Expand All @@ -178,15 +178,18 @@ public boolean searchSomethingAround(int cx, int cy, int cz) {
&& ((iteration++ & 0xff) != 0 || hasTimeLeft(startTime))) {
int currentNode = pq.poll();
int currentDistance = getDistance(currentNode);
Dest head = dests.peek();
Dest head = currentDest;
if (head != null && currentDistance + 1 > head.destDistanceRating) {
planPathTo(head.destNode, cx, cy, cz);
terminated();
return true;
}
float rating = rateDestination(currentNode);
if (rating >= 0) {
dests.add(new Dest(currentNode, rating));
Dest newDest = new Dest(currentNode, rating);
if (currentDest == null || newDest.compareTo(currentDest) < 0) {
currentDest = newDest;
}
}

int[] neighbours = getNeighbours(currentNode);
Expand Down Expand Up @@ -229,7 +232,7 @@ private void terminated() {
isRunning = false;
field = null;
pq.clear();
dests.clear();
currentDest = null;
}

protected int distanceFor(int from, int to) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public static BuildTask taskFor(Pos forPosition, Block blockToPlace,
}

public static TaskDescription getTaskDescription(Block b, AIHelper h,
int x, int y, int z) {
String name = Block.blockRegistry.getNameForObject(b);
int x, int y, int z) throws UnknownBlockException {
String name = Block.blockRegistry.getNameForObject(b).replaceFirst("minecraft:", "");
int blockMetadata = h.getMinecraft().theWorld.getBlockMetadata(x, y, z);
if (AIHelper.blockIsOneOf(b, BlockBuildTask.BLOCKS)) {
return new TaskDescription(name, CubeBuildTask.STANDABLE);
Expand Down Expand Up @@ -73,7 +73,7 @@ public static TaskDescription getTaskDescription(Block b, AIHelper h,
+ t.toString().toLowerCase() + " " + dir, pos);
}
}
throw new IllegalArgumentException("Unknown wood type " + b);
throw new UnknownBlockException("Unknown wood type " + b);
} else if (AIHelper.blockIsOneOf(b, WoodBuildTask.BLOCK)) {
return new TaskDescription(
name
Expand Down Expand Up @@ -130,10 +130,10 @@ public static TaskDescription getTaskDescription(Block b, AIHelper h,
Pos.fromDir(standable));
}
}
throw new IllegalArgumentException("Cannot find halfslabs " + b);
throw new UnknownBlockException("Cannot find halfslabs " + b);

} else {
throw new IllegalArgumentException("Cannot reverse build task for "
throw new UnknownBlockException("Cannot reverse build task for "
+ b);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.famzangl.minecraft.minebot.build.blockbuild;

public class UnknownBlockException extends Exception {

/**
*
*/
private static final long serialVersionUID = -3534869401465171196L;

public UnknownBlockException() {
super();
}

public UnknownBlockException(String message, Throwable cause) {
super(message, cause);
}

public UnknownBlockException(String message) {
super(message);
}

public UnknownBlockException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package net.famzangl.minecraft.minebot.build.reverse;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import net.famzangl.minecraft.minebot.MinebotMod;
import net.famzangl.minecraft.minebot.Pos;
import net.famzangl.minecraft.minebot.ai.AIHelper;
import net.famzangl.minecraft.minebot.ai.command.AIChatController;
import net.famzangl.minecraft.minebot.build.blockbuild.BuildTask;
import net.famzangl.minecraft.minebot.build.blockbuild.TaskDescription;
import net.famzangl.minecraft.minebot.build.blockbuild.UnknownBlockException;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraftforge.common.util.ForgeDirection;
Expand All @@ -13,10 +20,12 @@ public class BuildReverser {
private Pos pos1;
private Pos pos2;
private ReverseBuildField field;
private String outFile;
private PrintStream out;

public BuildReverser(AIHelper helper) {
public BuildReverser(AIHelper helper, String outFile) {
this(helper, minPos(helper.getPos1(), helper.getPos2()), maxPos(
helper.getPos1(), helper.getPos2()));
helper.getPos1(), helper.getPos2()), outFile);
}

private static Pos minPos(Pos p1, Pos p2) {
Expand All @@ -29,19 +38,49 @@ private static Pos maxPos(Pos p1, Pos p2) {
p1.z, p2.z));
}

public BuildReverser(AIHelper helper, Pos pos1, Pos pos2) {
public BuildReverser(AIHelper helper, Pos pos1, Pos pos2, String outFile) {
this.helper = helper;
this.pos1 = pos1;
this.pos2 = pos2;
field = new ReverseBuildField(pos2.x - pos1.x + 1, pos2.y - pos1.y + 1,
pos2.z - pos1.z + 1);
this.outFile = outFile;
}

public void run() {
for (int y = pos1.y; y <= pos2.y; y++) {
for (int x = pos1.x; x <= pos2.x; x++) {
addRow(new Pos(x, y, pos1.z), ForgeDirection.SOUTH, pos2.z
- pos1.z + 1);
try {
if (outFile == null || outFile.isEmpty() || "-".equals(outFile)) {
this.outFile = "-";
this.out = System.out;
} else {
this.out = new PrintStream(outFile);
}
out.println("# Minebot reverse build script " + MinebotMod.getVersion());
out.println("# Pos1: " + pos1);
out.println("# Pos2: " + pos2);
out.println("");
out.println("/minebot build:clear");
out.println("");
for (int y = pos1.y; y <= pos2.y; y++) {
out.println("# Layer " + (y - pos1.y));
for (int x = pos1.x; x <= pos2.x; x++) {
boolean row2 = ((x - pos1.x) & 1) == 1;
addRow(new Pos(x, y, row2 ? pos2.z : pos1.z), row2 ? ForgeDirection.NORTH
: ForgeDirection.SOUTH, pos2.z - pos1.z + 1);
}
out.println("");
}
out.println("/minebot build");

AIChatController.addChatLine("Output written to: " + this.outFile);
} catch (FileNotFoundException e) {
AIChatController.addChatLine("File/dir does not exist: " + outFile);
} catch (IOException e) {
AIChatController.addChatLine("IO-Error for: " + outFile);
} finally {
if (out != null) {
out.close();
out = null;
}
}
}
Expand All @@ -60,10 +99,18 @@ private void addBuildPlace(int x, int y, int z) {

Block b = helper.getBlock(x, y, z);
if (b != Blocks.air) {
TaskDescription taskString = BuildTask.getTaskDescription(b, helper, x, y, z);
field.setBlockAt(lx, ly, lz, b, taskString);
System.out.println("/minebot build:schedule ~" + lx + " ~" + ly
+ " ~" + lz + " " + taskString.getCommandArgs());
try {
TaskDescription taskString = BuildTask.getTaskDescription(b,
helper, x, y, z);
field.setBlockAt(lx, ly, lz, b, taskString);
out.println("/minebot build:schedule ~" + lx + " ~" + ly + " ~"
+ lz + " " + taskString.getCommandArgs());
} catch (UnknownBlockException e) {
out.println("# Missing: ~" + lx + " ~" + ly + " ~" + lz + " "
+ b.getLocalizedName());
AIChatController.addChatLine("Cannot convert Block at " + x
+ ", " + y + ", " + z + ". It will be missing.");
}
}
}
}
Loading

0 comments on commit d382608

Please sign in to comment.