Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Star Walk Behavior #11

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import com.typesafe.config.ConfigFactory;
import nl.tudelft.opencraft.yardstick.experiment.Experiment;
import nl.tudelft.opencraft.yardstick.experiment.Experiment10GenerationStressTest;
import nl.tudelft.opencraft.yardstick.experiment.Experiment10WalkStraight;
import nl.tudelft.opencraft.yardstick.experiment.Experiment11Latency;
import nl.tudelft.opencraft.yardstick.experiment.Experiment11Random;
import nl.tudelft.opencraft.yardstick.experiment.Experiment12LatencyAndWalkAround;
import nl.tudelft.opencraft.yardstick.experiment.Experiment12RandomE2E;
import nl.tudelft.opencraft.yardstick.experiment.Experiment3WalkAround;
import nl.tudelft.opencraft.yardstick.experiment.Experiment4MultiWalkAround;
import nl.tudelft.opencraft.yardstick.experiment.Experiment5SimpleWalk;
import nl.tudelft.opencraft.yardstick.experiment.Experiment6InteractWalk;
import nl.tudelft.opencraft.yardstick.experiment.Experiment8BoxWalkAround;
import nl.tudelft.opencraft.yardstick.experiment.Experiment9GenerationStressTest;
import nl.tudelft.opencraft.yardstick.experiment.Experiment9Spike;
import nl.tudelft.opencraft.yardstick.experiment.RemoteControlledExperiment;
import nl.tudelft.opencraft.yardstick.game.GameArchitecture;
Expand Down Expand Up @@ -121,6 +125,18 @@ public static void main(String[] args) {
case "12":
ex = new Experiment12LatencyAndWalkAround(id, game, behaviorConfig);
break;
case "13":
ex = new Experiment9GenerationStressTest();
break;
case "14":
ex = new Experiment10WalkStraight();
break;
case "15":
ex = new Experiment11Random();
break;
case "16":
ex = new Experiment12RandomE2E();
break;
default:
System.out.println("Invalid experiment: " + behaviorName);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import nl.tudelft.opencraft.yardstick.bot.world.SimpleWorldPhysics;
import nl.tudelft.opencraft.yardstick.bot.world.World;
import nl.tudelft.opencraft.yardstick.experiment.LoggerSessionListener;
import nl.tudelft.opencraft.yardstick.telemetry.Timer;
import nl.tudelft.opencraft.yardstick.workload.WorkloadDumper;
import nl.tudelft.opencraft.yardstick.workload.WorkloadSessionListener;
import org.slf4j.Logger;
Expand Down Expand Up @@ -121,14 +122,20 @@ public void addSessionListener(SessionListener... listeners) {
* @throws IllegalStateException if the bot is already connected.
*/
public void connect() {
Session session = client.getSession();
if (session.isConnected()) {
throw new IllegalStateException("Can not start connection. Bot already isConnected!");
}
session.addListener(new LoggerSessionListener(logger));
session.connect();
Timer timer = new Timer("connect_latency");
timer.start();
try {
Session session = client.getSession();
if (session.isConnected()) {
throw new IllegalStateException("Can not start connection. Bot already isConnected!");
}
session.addListener(new LoggerSessionListener(logger));
session.connect();

initializeTaskTicker();
initializeTaskTicker();
} finally {
timer.stop();
}
}

private void initializeTaskTicker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import science.atlarge.opencraft.mcprotocollib.data.game.entity.metadata.Position;
import science.atlarge.opencraft.mcprotocollib.data.game.entity.player.Hand;
import science.atlarge.opencraft.mcprotocollib.data.game.entity.player.PlayerAction;
import science.atlarge.opencraft.mcprotocollib.packet.ingame.client.ClientChatPacket;
import science.atlarge.opencraft.mcprotocollib.packet.ingame.client.player.ClientPlayerActionPacket;
import science.atlarge.opencraft.mcprotocollib.packet.ingame.client.player.ClientPlayerPlaceBlockPacket;
import science.atlarge.opencraft.mcprotocollib.packet.ingame.client.player.ClientPlayerPositionPacket;
Expand Down Expand Up @@ -151,6 +152,10 @@ public void creativeInventoryAction(Material mat, int amt) {
getSession().send(new ClientCreativeInventoryActionPacket(PLAYER_INVENTORY_HOTBAR_0, new ItemStack(mat.getId(), amt)));
}

public void sendChatMsg(String msg) {
getSession().send(new ClientChatPacket(msg));
}

/**
* A state of breaking blocks.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
import science.atlarge.opencraft.packetlib.event.session.SessionListener;
import science.atlarge.opencraft.packetlib.packet.Packet;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
Expand All @@ -143,6 +145,9 @@ public class BotListener implements SessionListener {
private Server server;
private World world;

private final List<PacketReceivedEvent> unhandledRecvPkt = new ArrayList<>();
private boolean serverJoinGame;

/**
* Creates a new listener.
*
Expand Down Expand Up @@ -464,6 +469,13 @@ public void packetReceived(PacketReceivedEvent pre) {
this.player = new BotPlayer(bot, p.getEntityId());
player.setGamemode(p.getGameMode());
bot.setPlayer(player);

// Replay the unhandled packet events before receiving server join game
serverJoinGame = true;
for (PacketReceivedEvent packetReceivedEvent : unhandledRecvPkt) {
packetReceived(packetReceivedEvent);
}
unhandledRecvPkt.clear();
} else if (packet instanceof ServerMapDataPacket) {
// 0x24 Map
ServerMapDataPacket p = (ServerMapDataPacket) packet;
Expand Down Expand Up @@ -512,6 +524,11 @@ public void packetReceived(PacketReceivedEvent pre) {
ServerOpenTileEntityEditorPacket p = (ServerOpenTileEntityEditorPacket) packet;
// TODO

}
// ServerJoinGame packet is received after a few Player packets.
// Save them to handle later
else if (!serverJoinGame) {
unhandledRecvPkt.add(pre);
} else if (packet instanceof ServerPlayerAbilitiesPacket) {

// 0x2B Player Abilities
Expand Down Expand Up @@ -540,16 +557,15 @@ public void packetReceived(PacketReceivedEvent pre) {
} else if (packet instanceof ServerPlayerPositionRotationPacket) {
// 0x2E Player Position And Look
ServerPlayerPositionRotationPacket p = (ServerPlayerPositionRotationPacket) packet;

BotPlayer player = bot.getPlayer();

player.setLocation(new Vector3d(p.getX(), p.getY(), p.getZ()));
player.setPitch(p.getPitch());
player.setYaw(p.getYaw());
player.setOnGround(true);

Session session = bot.getClient().getSession();
session.send(new ClientTeleportConfirmPacket(p.getTeleportId()));

logger.info("Received new Player position: " + player.getLocation());

} else if (packet instanceof ServerPlayerUseBedPacket) {
Expand Down Expand Up @@ -723,7 +739,7 @@ public void packetReceived(PacketReceivedEvent pre) {
// TODO

} else {
logger.warn("Received unhandled packet: {}", packet.getClass().getName());
logger.debug("Received unhandled packet: {}", packet.getClass().getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

package nl.tudelft.opencraft.yardstick.bot.ai.task;

import java.util.ArrayList;
import java.util.List;
import nl.tudelft.opencraft.yardstick.bot.Bot;
import nl.tudelft.opencraft.yardstick.bot.world.Block;
import nl.tudelft.opencraft.yardstick.bot.world.ChunkNotLoadedException;
import nl.tudelft.opencraft.yardstick.util.Vector3i;

import java.util.ArrayList;
import java.util.List;

public class BreakBlocksTask implements Task {

private List<Vector3i> blockLocations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

package nl.tudelft.opencraft.yardstick.bot.ai.task;

import java.util.List;
import nl.tudelft.opencraft.yardstick.bot.Bot;
import nl.tudelft.opencraft.yardstick.bot.world.Material;
import nl.tudelft.opencraft.yardstick.util.Vector3i;

import java.util.List;

public class PlaceBlocksTask implements Task {

private List<Vector3i> blockPositions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

package nl.tudelft.opencraft.yardstick.bot.ai.task;

import java.util.Random;
import nl.tudelft.opencraft.yardstick.bot.Bot;
import nl.tudelft.opencraft.yardstick.bot.world.ChunkNotLoadedException;
import nl.tudelft.opencraft.yardstick.util.Vector2i;
import nl.tudelft.opencraft.yardstick.util.Vector3i;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

public class RandomSquareWalkXZTask implements Task {

private final Logger logger = LoggerFactory.getLogger(RandomSquareWalkXZTask.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nl.tudelft.opencraft.yardstick.bot.ai.task;

import nl.tudelft.opencraft.yardstick.bot.Bot;

public class StandExecutor extends AbstractTaskExecutor {
private int timeout = 1000;
private final long startTime;


public StandExecutor(Bot bot) {
super(bot);
startTime = System.currentTimeMillis();
}

public void setTimeout(int timeout) {
this.timeout = timeout;
}

@Override
protected TaskStatus onTick() {
long nowTime = System.currentTimeMillis();
if (nowTime - startTime >= timeout) {
return TaskStatus.forSuccess();
} else {
return TaskStatus.forInProgress();
}
}

@Override
protected void onStop() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ private void botTick(Bot bot) {

private Runnable newBotConnector(Bot bot) {
return () -> {
long start = System.currentTimeMillis();
bot.connect();
int sleep = 1000;

int sleep = 2000;
int tries = 3;
while (tries-- > 0 && (bot.getPlayer() == null || !bot.isJoined())) {
try {
Expand All @@ -101,11 +103,15 @@ private Runnable newBotConnector(Bot bot) {
break;
}
}

if (!bot.isJoined()) {
String host = bot.getClient().getHost();
int port = bot.getClient().getPort();
logger.warn("Could not connect bot {}:{}.", host, port);
bot.disconnect("Make sure to close all connections.");
if (number == 10) {
Experiment10WalkStraight.currBotId -= Experiment10WalkStraight.clientCount;
}
}
};
}
Expand Down Expand Up @@ -138,4 +144,8 @@ protected void after() {
bot.disconnect("disconnect");
}
}

public BotModel getModel() {
return model;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Yardstick: A Benchmark for Minecraft-like Services
* Copyright (C) 2020 AtLarge Research
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/

package nl.tudelft.opencraft.yardstick.experiment;

import java.util.UUID;
import nl.tudelft.opencraft.yardstick.bot.Bot;
import nl.tudelft.opencraft.yardstick.model.StraightMovementModel;


public class Experiment10WalkStraight extends AbstractModelExperiment {

public static int currBotId;
public static int clientCount;
public static double walkSpeed;

public Experiment10WalkStraight() {
super(10, "Bots move towards different directions based on their IDs", new StraightMovementModel());
StraightMovementModel model = (StraightMovementModel) getModel();
currBotId = Integer.parseInt(options.experimentParams.getOrDefault("botstartid", "0"));
clientCount = Integer.parseInt(options.experimentParams.getOrDefault("clientcount", "1"));
walkSpeed = Double.parseDouble(options.experimentParams.getOrDefault("walkspeed", "0.15"));
model.setWalkSpeed(walkSpeed);

}

protected Bot createBot() {
Bot bot = newBot(UUID.randomUUID().toString().substring(0, 6) + "-" + (currBotId));
currBotId += clientCount;
return bot;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Yardstick: A Benchmark for Minecraft-like Services
* Copyright (C) 2020 AtLarge Research
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/

package nl.tudelft.opencraft.yardstick.experiment;

import nl.tudelft.opencraft.yardstick.model.RandomModel;

public class Experiment11Random extends AbstractModelExperiment {

public Experiment11Random() {
super(11, "Bots move around randomly and have a chance to break or place blocks", new RandomModel());
RandomModel randomModel = (RandomModel) getModel();
randomModel.setMovementDiameter(0);
}

}
Loading