baritone/src/main/java/baritone/utils/BaritoneAutoTest.java

143 lines
6.0 KiB
Java
Raw Normal View History

2018-09-25 18:19:25 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils;
import baritone.api.BaritoneAPI;
2018-09-25 18:19:25 +00:00
import baritone.api.event.events.TickEvent;
import baritone.api.event.listener.AbstractGameEventListener;
import baritone.api.pathing.goals.Goal;
2018-09-25 20:45:29 +00:00
import baritone.api.pathing.goals.GoalBlock;
2019-04-18 01:10:47 +00:00
import baritone.api.utils.Helper;
import baritone.api.utils.IPlayerContext;
import net.minecraft.client.Minecraft;
2018-09-25 18:19:25 +00:00
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.client.tutorial.TutorialSteps;
2018-09-25 18:19:25 +00:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.*;
2018-09-25 18:19:25 +00:00
2019-01-16 19:37:39 +00:00
/**
* Responsible for automatically testing Baritone's pathing algorithm by automatically creating a world with a specific
* seed, setting a specified goal, and only allowing a certain amount of ticks to pass before the pathing test is
* considered a failure. In order to test locally, docker may be used, or through an IDE: Create a run config which runs
* in a separate directory from the primary one (./run), and set the enrivonmental variable {@code BARITONE_AUTO_TEST}
* to {@code true}.
*
* @author leijurv, Brady
*/
2018-09-25 18:19:25 +00:00
public class BaritoneAutoTest implements AbstractGameEventListener, Helper {
public static final BaritoneAutoTest INSTANCE = new BaritoneAutoTest();
2018-09-26 21:21:53 +00:00
public static final boolean ENABLE_AUTO_TEST = "true".equals(System.getenv("BARITONE_AUTO_TEST"));
2018-09-25 18:19:25 +00:00
private static final long TEST_SEED = -928872506371745L;
2018-09-26 04:58:22 +00:00
private static final BlockPos STARTING_POSITION = new BlockPos(0, 65, 0);
private static final Goal GOAL = new GoalBlock(69, 69, 420);
2018-12-03 22:09:05 +00:00
private static final int MAX_TICKS = 3300;
2018-09-25 18:19:25 +00:00
/**
* Called right after the {@link GameSettings} object is created in the {@link Minecraft} instance.
*/
public void onPreInit() {
2018-09-26 21:21:53 +00:00
if (!BaritoneAutoTest.ENABLE_AUTO_TEST) {
return;
}
System.out.println("Optimizing Game Settings");
GameSettings s = mc.gameSettings;
2018-09-26 01:57:50 +00:00
s.limitFramerate = 20;
s.mipmapLevels = 0;
s.particleSetting = 2;
s.overrideWidth = 128;
s.overrideHeight = 128;
s.heldItemTooltips = false;
2018-09-26 01:57:50 +00:00
s.entityShadows = false;
s.chatScale = 0.0F;
s.ambientOcclusion = 0;
2018-09-26 01:57:50 +00:00
s.clouds = 0;
s.fancyGraphics = false;
s.tutorialStep = TutorialSteps.NONE;
s.hideGUI = true;
s.fovSetting = 30.0F;
}
2018-09-25 18:19:25 +00:00
@Override
public void onTick(TickEvent event) {
IPlayerContext ctx = BaritoneAPI.getProvider().getPrimaryBaritone().getPlayerContext();
2018-09-26 04:24:48 +00:00
// If we're on the main menu then create the test world and launch the integrated server
if (mc.currentScreen instanceof GuiMainMenu) {
2018-09-25 18:19:25 +00:00
System.out.println("Beginning Baritone automatic test routine");
mc.displayGuiScreen(null);
WorldSettings worldsettings = new WorldSettings(TEST_SEED, GameType.SURVIVAL, true, false, WorldType.DEFAULT);
2018-09-25 18:19:25 +00:00
mc.launchIntegratedServer("BaritoneAutoTest", "BaritoneAutoTest", worldsettings);
}
2018-09-26 04:24:48 +00:00
// If the integrated server is running, set the difficulty to peaceful
if (mc.getIntegratedServer() != null) {
mc.getIntegratedServer().setDifficultyForAllWorlds(EnumDifficulty.PEACEFUL);
for (final WorldServer world : mc.getIntegratedServer().worlds) {
// If the world has initialized, set the spawn point to our defined starting position
if (world != null) {
world.setSpawnPoint(STARTING_POSITION);
world.getGameRules().setOrCreateGameRule("spawnRadius", "0");
}
}
2018-09-26 05:18:19 +00:00
}
2018-09-26 04:24:48 +00:00
if (event.getType() == TickEvent.Type.IN) { // If we're in-game
// Force the integrated server to share the world to LAN so that
// the ingame pause menu gui doesn't actually pause our game
2018-09-25 18:19:25 +00:00
if (mc.isSingleplayer() && !mc.getIntegratedServer().getPublic()) {
mc.getIntegratedServer().shareToLAN(GameType.SURVIVAL, false);
2018-09-25 18:19:25 +00:00
}
2018-09-26 04:24:48 +00:00
// For the first 200 ticks, wait for the world to generate
if (event.getCount() < 200) {
2018-09-25 18:19:25 +00:00
System.out.println("Waiting for world to generate... " + event.getCount());
return;
}
2018-09-26 04:24:48 +00:00
// Print out an update of our position every 5 seconds
if (event.getCount() % 100 == 0) {
System.out.println(ctx.playerFeet() + " " + event.getCount());
2018-09-25 20:55:03 +00:00
}
2018-09-26 04:24:48 +00:00
// Setup Baritone's pathing goal and (if needed) begin pathing
2019-02-26 00:26:17 +00:00
if (!BaritoneAPI.getProvider().getPrimaryBaritone().getCustomGoalProcess().isActive()) {
BaritoneAPI.getProvider().getPrimaryBaritone().getCustomGoalProcess().setGoalAndPath(GOAL);
}
2018-09-26 04:24:48 +00:00
// If we have reached our goal, print a message and safely close the game
if (GOAL.isInGoal(ctx.playerFeet())) {
System.out.println("Successfully pathed to " + ctx.playerFeet() + " in " + event.getCount() + " ticks");
2018-09-25 18:19:25 +00:00
mc.shutdown();
}
2018-09-26 04:24:48 +00:00
// If we have exceeded the expected number of ticks to complete the pathing
// task, then throw an IllegalStateException to cause the build to fail
2018-09-25 18:19:25 +00:00
if (event.getCount() > MAX_TICKS) {
throw new IllegalStateException("took too long");
}
}
}
2018-10-09 04:49:36 +00:00
private BaritoneAutoTest() {}
2018-09-25 18:19:25 +00:00
}