Skip to content

Commit

Permalink
Big strategy update. Changed the way strategies work and make them st…
Browse files Browse the repository at this point in the history
…ackable and not depend on tasks.
  • Loading branch information
Michael Zangl committed Aug 17, 2014
1 parent 9848803 commit 6e88ead
Show file tree
Hide file tree
Showing 155 changed files with 1,998 additions and 1,394 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.2.4"
version = "0.3.0"
// group= "net.famzangl.minecraft.minebot" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
sourceSets.main{
java{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@

# Health levels at which the bot should do the action. 0..20
# 19 means that you got half a heart damage.
on_damage_logout_level = -1
# Hint: Command may he a minebot command, this command then replaces the current command.
on_damage_logout_value = -1

on_damage_stop_value = -1
on_damage_command = /home
on_damage_command_level = -1
on_damage_command_value = -1

# Same with distance an other player has to you. -1 disables.
on_player_comes_logout_value = -1
on_player_comes_stop_value = -1
on_player_comes_command =
on_player_comes_command_value = -1

# Same with distance a creeper has to you. -1 disables.
on_creeper_comes_logout_value = -1
on_creeper_comes_stop_value = -1
on_creeper_comes_command =
on_creeper_comes_command_value = -1

# Distance bonus points for this ore
mine_points_coal_ore = 1
Expand All @@ -34,7 +48,9 @@ mine_factor_emerald_ore = 12
# How much extra points two blocks above each other give us.
# Extra points to add for double mining.
mine_double_add = 2
# How much random to use
# How much random to use 0 (disabled) .. 1 (a lot)
# This prevents two people who use the bot to do exactly the same.
mine_randomness = 0.05

# Blocks to use to build upwards.
upwards_place_block = dirt,stone,cobblestone
2 changes: 1 addition & 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.2.4")
@Mod(modid = "minebot-mod", name = "Minebot", version = "0.3.0")
public class MinebotMod {
@Instance(value = "minebot-mod")
public static MinebotMod instance;
Expand Down
182 changes: 38 additions & 144 deletions Minebot/src/net/famzangl/minecraft/minebot/ai/AIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;

import net.famzangl.minecraft.minebot.Pos;
import net.famzangl.minecraft.minebot.ai.command.AIChatController;
import net.famzangl.minecraft.minebot.ai.command.IAIControllable;
import net.famzangl.minecraft.minebot.ai.enchanting.EnchantStrategy;
import net.famzangl.minecraft.minebot.ai.render.BuildMarkerRenderer;
import net.famzangl.minecraft.minebot.ai.render.MarkingStrategy;
import net.famzangl.minecraft.minebot.ai.render.PosMarkerRenderer;
import net.famzangl.minecraft.minebot.ai.strategy.LayRailStrategy;
import net.famzangl.minecraft.minebot.ai.strategy.LumberjackStrategy;
import net.famzangl.minecraft.minebot.ai.strategy.MineStrategy;
import net.famzangl.minecraft.minebot.ai.strategy.PlantStrategy;
import net.famzangl.minecraft.minebot.ai.task.AITask;
import net.famzangl.minecraft.minebot.ai.task.CanPrefaceAndDestroy;
import net.famzangl.minecraft.minebot.ai.task.SkipWhenSearchingPrefetch;
import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy;
import net.famzangl.minecraft.minebot.ai.strategy.AIStrategy.TickResult;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.settings.KeyBinding;
Expand Down Expand Up @@ -53,46 +45,38 @@
*
*/
public class AIController extends AIHelper implements IAIControllable {

private static final int TIMEOUT = 5 * 20;

private final static Hashtable<KeyBinding, AIStrategyFactory> uses = new Hashtable<KeyBinding, AIStrategyFactory>();

protected static final KeyBinding stop = new KeyBinding("Stop",
Keyboard.getKeyIndex("N"), "Command Mod");
protected static final KeyBinding ungrab = new KeyBinding("Ungrab",
Keyboard.getKeyIndex("U"), "Command Mod");

private static final int MAX_LOOKAHEAD = 5;

static {
final KeyBinding mine = new KeyBinding("Farm ores",
Keyboard.getKeyIndex("K"), "Command Mod");
final KeyBinding lumberjack = new KeyBinding("Farm wood",
Keyboard.getKeyIndex("J"), "Command Mod");
final KeyBinding build_rail = new KeyBinding("Build Minecart tracks",
Keyboard.getKeyIndex("H"), "Command Mod");
final KeyBinding mobfarm = new KeyBinding("Farm mobs",
Keyboard.getKeyIndex("M"), "Command Mod");
final KeyBinding plant = new KeyBinding("Plant seeds",
Keyboard.getKeyIndex("P"), "Command Mod");
uses.put(mine, new MineStrategy());
uses.put(lumberjack, new LumberjackStrategy());
uses.put(build_rail, new LayRailStrategy());
uses.put(mobfarm, new EnchantStrategy());
uses.put(plant, new PlantStrategy());
ClientRegistry.registerKeyBinding(mine);
ClientRegistry.registerKeyBinding(lumberjack);
ClientRegistry.registerKeyBinding(build_rail);
ClientRegistry.registerKeyBinding(mobfarm);
ClientRegistry.registerKeyBinding(plant);
// final KeyBinding mine = new KeyBinding("Farm ores",
// Keyboard.getKeyIndex("K"), "Command Mod");
// final KeyBinding lumberjack = new KeyBinding("Farm wood",
// Keyboard.getKeyIndex("J"), "Command Mod");
// final KeyBinding build_rail = new KeyBinding("Build Minecart tracks",
// Keyboard.getKeyIndex("H"), "Command Mod");
// final KeyBinding mobfarm = new KeyBinding("Farm mobs",
// Keyboard.getKeyIndex("M"), "Command Mod");
// final KeyBinding plant = new KeyBinding("Plant seeds",
// Keyboard.getKeyIndex("P"), "Command Mod");
// uses.put(mine, new MineStrategy());
// uses.put(lumberjack, new LumberjackStrategy());
// uses.put(build_rail, new LayRailStrategy());
// uses.put(mobfarm, new EnchantStrategy());
// uses.put(plant, new PlantStrategy());
// ClientRegistry.registerKeyBinding(mine);
// ClientRegistry.registerKeyBinding(lumberjack);
// ClientRegistry.registerKeyBinding(build_rail);
// ClientRegistry.registerKeyBinding(mobfarm);
// ClientRegistry.registerKeyBinding(plant);
ClientRegistry.registerKeyBinding(stop);
ClientRegistry.registerKeyBinding(ungrab);
}

private final LinkedList<AITask> tasks = new LinkedList<AITask>();
private boolean desync;
private int timeout = 10 * 20;
private boolean dead;
private AIStrategy currentStrategy;

Expand All @@ -107,29 +91,12 @@ public class AIController extends AIHelper implements IAIControllable {

private boolean skipNextTick;

private boolean inUngrabMode;

private MouseHelper oldMouseHelper;

private BuildMarkerRenderer buildMarkerRenderer;

public AIController() {
new AIChatController(this);
}

@Override
public void addTask(AITask task) {
if (task == null) {
throw new NullPointerException();
}
tasks.add(task);
}

@Override
public void desync() {
System.out.println("Desync. This is an error. Did the server lag?");
Thread.dumpStack();
desync = true;
AIChatController.getRegistry().setControlled(this);
}

/**
Expand All @@ -156,67 +123,21 @@ public void onPlayerTick(ClientTickEvent evt) {

AIStrategy newStrat;
if (dead || stop.isPressed() || stop.getIsKeyPressed()) {
deactivateCurrentStrategy();
dead = false;
currentStrategy = null;
tasks.clear();
System.out.println("New Strategy: None");
resetTimeout();
} else if ((newStrat = findNewStrategy()) != null) {
tasks.clear();
deactivateCurrentStrategy();
currentStrategy = newStrat;
System.out.println("New Strategy: " + newStrat);
resetTimeout();
} else if (desync) {
tasks.clear();
currentStrategy.setActive(true, this);
}
desync = false;

if (currentStrategy != null) {
synchronized (strategyDescrMutex) {
strategyDescr = currentStrategy.getDescription();
}
final AITask overrideTask = currentStrategy.getOverrideTask(this);
if (overrideTask != null) {
tasks.clear();
tasks.push(overrideTask);
} else if (tasks.isEmpty()) {
currentStrategy.searchTasks(this);
resetTimeout();
System.out.println("Found new task: " + tasks.peekFirst());
}

if (tasks.isEmpty()) {
final TickResult result = currentStrategy.gameTick(this);
if (result == TickResult.NO_MORE_WORK) {
dead = true;
} else {
AITask task = tasks.get(0);
while (task.isFinished(this)) {
tasks.remove(0);
resetTimeout();
System.out.println("Next task: " + tasks.peekFirst());
if (tasks.peekFirst() != null) {
timeout = tasks.peekFirst().getGameTickTimeout();
task = tasks.peekFirst();
} else {
task = null;
break;
}
}
if (task == null) {
// pass
} else if (timeout <= 0) {
desync = true;
} else {
timeout--;
try {
task.runTick(this);
} catch (final Throwable t) {
t.printStackTrace();
AIChatController
.addChatLine("Unexpected Error ("
+ t.getMessage()
+ "). Please report (and send the output on the console)!");
}
}
}
} else {
synchronized (strategyDescrMutex) {
Expand All @@ -225,35 +146,11 @@ public void onPlayerTick(ClientTickEvent evt) {
}
}

@Override
public boolean faceAndDestroyForNextTask() {
boolean found = false;
for (int i = 1; i < MAX_LOOKAHEAD && i < tasks.size() && !found; i++) {
AITask task = tasks.get(i);
System.out.println("Prefetching with: " + task);
if (tasks.get(i).getClass()
.isAnnotationPresent(SkipWhenSearchingPrefetch.class)) {
continue;
} else if (task instanceof CanPrefaceAndDestroy) {
CanPrefaceAndDestroy dTask = (CanPrefaceAndDestroy) task;
List<Pos> positions = dTask.getPredestroyPositions(this);
for (Pos pos : positions) {
if (!isAirBlock(pos.x, pos.y, pos.z)) {
faceAndDestroy(pos.x, pos.y, pos.z);
found = true;
break;
}
}
System.out.println("Prefacing: " + found + " for " + positions);
} else {
System.out.println("Prefetching showstopper: " + task);
break;
}
}
if (!found) {
System.out.println("Could not prefetch anything. " + tasks.size());
private void deactivateCurrentStrategy() {
if (currentStrategy != null) {
currentStrategy.setActive(false, this);
}
return found;
currentStrategy = null;
}

@SubscribeEvent
Expand All @@ -275,8 +172,9 @@ public void drawHUD(RenderGameOverlayEvent.Post event) {
try {
// Dynamic 1.7.2 / 1.7.10 fix.
ScaledResolution res;
Constructor<?> method = ScaledResolution.class.getConstructors()[0];
Object arg1 = method.getParameterTypes()[0] == Minecraft.class ? getMinecraft()
final Constructor<?> method = ScaledResolution.class
.getConstructors()[0];
final Object arg1 = method.getParameterTypes()[0] == Minecraft.class ? getMinecraft()
: getMinecraft().gameSettings;
res = (ScaledResolution) method.newInstance(arg1,
getMinecraft().displayWidth, getMinecraft().displayHeight);
Expand All @@ -290,11 +188,11 @@ public void drawHUD(RenderGameOverlayEvent.Post event) {
res.getScaledWidth()
- getMinecraft().fontRenderer.getStringWidth(str)
- 10, 10, 16777215);
} catch (InstantiationException e) {
} catch (final InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
} catch (final InvocationTargetException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -387,10 +285,6 @@ private AIStrategy findNewStrategy() {
return null;
}

private void resetTimeout() {
timeout = TIMEOUT;
}

@Override
public AIHelper getAiHelper() {
return this;
Expand Down
Loading

0 comments on commit 6e88ead

Please sign in to comment.