Merge pull request #4 from dginovker/development

Bot Development
This commit is contained in:
Daniel Ginovker 2019-06-09 13:03:34 -04:00 committed by GitHub
commit a95b194865
11 changed files with 96 additions and 67 deletions

View file

@ -57,11 +57,6 @@ public class AIPlayer extends Player {
*/
private Player controler;
/**
* The scriptAPI the bot will perform
*/
private ScriptAPI scriptAPI;
/**
* Constructs a new {@code AIPlayer} {@code Object}.
* @param name The name of the AIP.
@ -75,7 +70,6 @@ public class AIPlayer extends Player {
super.getDetails().setSession(ArtificialSession.getSingleton());
this.username = StringUtils.formatDisplayName(name);
this.uid = currentUID++;
this.scriptAPI = new ScriptAPI();
}
@Override
@ -143,26 +137,6 @@ public class AIPlayer extends Player {
this.setAttribute("dead", true);
}
public void runScript(Script script)
{
if (!this.getPulseManager().hasPulseRunning())
{
getPulseManager().run(new Pulse() {
@Override
public boolean pulse() {
return false;
}
@Override
public boolean update() {
super.update();
script.runLoop();
return true;
}
});
}
}
/**
* Gets the UID.
* @return the UID.

View file

@ -3,6 +3,7 @@ package org.crandor.game.node.entity.player.ai.general;
import org.crandor.game.node.entity.player.ai.AIPBuilder;
import org.crandor.game.node.entity.player.ai.AIPlayer;
import org.crandor.game.node.entity.player.ai.general.scriptrepository.Script;
import org.crandor.game.node.item.Item;
import org.crandor.game.system.task.Pulse;
import org.crandor.game.world.GameWorld;
import org.crandor.game.world.map.Location;
@ -10,24 +11,27 @@ import org.crandor.game.world.repository.Repository;
public class GeneralBotCreator {
//org/crandor/net/packet/in/InteractionPacket.java <<< This is a very useful class for learning to code bots
public GeneralBotCreator(String botName, Location loc, Script botScript)
{
AIPlayer bot = AIPBuilder.create(botName, loc);
Repository.getPlayers().add(bot);
bot.init();
botScript.setPlayer(bot);
botScript.bot = AIPBuilder.create(botName, loc);
Repository.getPlayers().add(botScript.bot);
botScript.init();
GameWorld.submit(new Pulse(1, bot) {
GameWorld.submit(new Pulse(1, botScript.bot) {
int ticks = 0;
@Override
public boolean pulse() {
if (ticks ++ == 5000)
{
AIPlayer.deregister(bot.getUid());
AIPlayer.deregister(botScript.bot.getUid());
return true;
}
bot.runScript(botScript);
if (!botScript.bot.getPulseManager().hasPulseRunning())
{
botScript.runLoop();
}
return false;
}
});

View file

@ -1,34 +1,39 @@
package org.crandor.game.node.entity.player.ai.general;
import org.crandor.game.node.entity.npc.NPC;
import org.crandor.game.node.Node;
import org.crandor.game.node.entity.player.Player;
import org.crandor.game.node.object.GameObject;
import org.crandor.game.world.map.Location;
import org.crandor.game.world.map.RegionManager;
import org.crandor.game.world.map.RegionPlane;
import org.crandor.game.world.map.path.Pathfinder;
public class ScriptAPI {
public double distance(Location l1, Location l2) {
return Pathfinder.find(l1, l2).getPoints().size();
private Player bot;
public ScriptAPI(Player bot) {
this.bot = bot;
}
public NPC getNearestNPC(Location loc, String npcName) {
NPC npc = null;
public double distance(Node n1, Node n2)
{
return Math.sqrt(Math.pow(n1.getLocation().getX() - n2.getLocation().getX(), 2) + Math.pow(n2.getLocation().getY() - n1.getLocation().getY(), 2));
}
public Node getNearestNode(String entityName)
{
Node entity = null;
double minDistance = Double.MAX_VALUE;
for (RegionPlane p : RegionManager.forId(loc.getRegionId()).getPlanes()) {
for (NPC n : p.getNpcs()) {
if (n != null) {
if (n.getName().equals(npcName) && distance(loc, n.getLocation()) < minDistance) {
npc = n;
}
}
for (Node e : RegionManager.forId(bot.getLocation().getRegionId()).getPlanes()[bot.getLocation().getZ()].getEntities())
{
if (e != null && e.getName().equals(entityName) && distance(bot, e) < minDistance && !Pathfinder.find(bot, e).isMoveNear())
{
entity = e;
minDistance = distance(bot, e);
}
}
System.out.println("Returning " + npc.getName());
return npc;
return entity;
}
public GameObject getNearestGameObject(Location loc, int objectId) {
@ -37,7 +42,7 @@ public class ScriptAPI {
for (GameObject[] o : RegionManager.forId(loc.getRegionId()).getPlanes()[0].getObjects()) {
for (GameObject obj : o) {
if (obj != null) {
if (distance(loc, obj.getLocation()) < minDistance) {
if (distance(loc, obj) < minDistance) {
nearestObject = obj;
}
}

View file

@ -1,12 +1,23 @@
package org.crandor.game.node.entity.player.ai.general.scriptrepository;
import org.crandor.game.node.entity.player.Player;
import plugin.skill.thieving.ThievingOptionPlugin;
import org.crandor.game.node.Node;
import org.crandor.game.node.item.Item;
import java.util.Arrays;
public class ManThiever extends Script {
public ManThiever()
{
this.equipment.addAll(Arrays.asList(new Item(1103), new Item(1139), new Item(1265)));
}
@Override
public void runLoop() {
new ThievingOptionPlugin().handle(me, scriptAPI.getNearestNPC(me.getLocation(), "Man"), "pickpocket");
Node man = scriptAPI.getNearestNode("Man");
if (man != null)
{
man.getInteraction().handle(bot, man.getInteraction().get(2));
}
}
}

View file

@ -1,15 +1,31 @@
package org.crandor.game.node.entity.player.ai.general.scriptrepository;
import org.crandor.game.node.entity.player.Player;
import org.crandor.game.node.entity.player.ai.AIPlayer;
import org.crandor.game.node.entity.player.ai.general.ScriptAPI;
import org.crandor.game.node.item.Item;
import java.util.ArrayList;
public abstract class Script {
public Player me;
public ScriptAPI scriptAPI = new ScriptAPI();
public ScriptAPI scriptAPI;
public ArrayList<Item> inventory = new ArrayList<>();
public ArrayList<Item> equipment = new ArrayList<>();
public void setPlayer(Player me)
public AIPlayer bot;
public void init()
{
this.me = me;
bot.init();
scriptAPI = new ScriptAPI(bot);
for (Item i : inventory)
{
bot.getInventory().add(i);
}
for (Item i : equipment)
{
bot.getEquipment().add(i, true, false);
}
}
public abstract void runLoop();

View file

@ -1,5 +1,7 @@
package org.crandor.game.world.map;
import org.crandor.game.node.Node;
import org.crandor.game.node.entity.Entity;
import org.crandor.game.node.entity.npc.NPC;
import org.crandor.game.node.entity.player.Player;
import org.crandor.game.node.item.GroundItem;
@ -13,6 +15,8 @@ import org.crandor.net.packet.context.BuildItemContext;
import org.crandor.net.packet.out.ClearGroundItem;
import org.crandor.net.packet.out.ConstructGroundItem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@ -320,6 +324,17 @@ public final class RegionPlane {
return npcs;
}
public List<Node> getEntities()
{
List<Node> entities = new ArrayList<>(npcs);
for (GameObject[] o : getObjects())
{
entities.addAll(Arrays.asList(o));
}
return entities;
}
/**
* Gets the players.
* @return The players.

View file

@ -2,6 +2,7 @@ package org.crandor.game.world.map.path;
import org.crandor.game.world.map.Location;
import org.crandor.game.world.map.Point;
import org.crandor.game.world.map.RegionManager;
public final class SmartPathfinder extends Pathfinder {

View file

@ -234,7 +234,7 @@ public final class InteractionPacket implements IncomingPacket {
/**
* Handles player interaction.
* @param playerThe player interacting.
* @param player The player interacting.
* @param optionIndex The option index.
* @param index The target index.
*/
@ -262,7 +262,7 @@ public final class InteractionPacket implements IncomingPacket {
* @param player The player.
* @param index The index of the reward.
* @param itemId The item id.
* @param locationThe location of the item.
* @param location The location of the item.
*/
private static void handleGroundItemInteraction(final Player player, int index, int itemId, Location location) {
final GroundItem item = GroundItemManager.get(itemId, location, player);

View file

@ -1,5 +1,6 @@
package plugin.command;
import org.crandor.game.container.Container;
import org.crandor.game.interaction.Interaction;
import org.crandor.game.node.entity.player.Player;
import org.crandor.game.node.entity.player.ai.AIPBuilder;
@ -10,6 +11,7 @@ import org.crandor.game.node.entity.player.ai.pvp.PVPAIPActions;
import org.crandor.game.node.entity.player.ai.pvp.PVPAIPBuilderUtils;
import org.crandor.game.node.entity.player.ai.resource.ResourceAIPActions;
import org.crandor.game.node.entity.player.link.appearance.Gender;
import org.crandor.game.node.item.Item;
import org.crandor.game.system.command.CommandPlugin;
import org.crandor.game.system.command.CommandSet;
import org.crandor.game.system.task.Pulse;
@ -22,6 +24,7 @@ import org.crandor.plugin.InitializablePlugin;
import org.crandor.plugin.Plugin;
import org.crandor.tools.RandomFunction;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

View file

@ -50,14 +50,14 @@ public class PlayerExamineInterfacePlugin extends ComponentPlugin {
player.getPacketDispatch().sendInterfaceConfig(component.getId(), 13, true);
player.getInterfaceManager().open(component);
player.getPacketDispatch().sendString("<col=" + examined.getSavedData().getSpawnData().getTitle().getTitleColor() + ">" + examined.getSavedData().getSpawnData().getTitle().getName() + "</col> " + StringUtils.formatDisplayName(examined.getName()) + " - Combat Level: " + player.getProperties().getCurrentCombatLevel(), 31, 3);
player.getPacketDispatch().sendString("KDR -> ", 31, 9);
player.getPacketDispatch().sendString("Total Kills -> ", 31, 10);
player.getPacketDispatch().sendString("Total Deaths -> ", 31, 16);
player.getPacketDispatch().sendString("Killstreak -> ", 31, 19);
player.getPacketDispatch().sendString("" + examined.getSavedData().getSpawnData().getKdr(), 31, 11);
player.getPacketDispatch().sendString("" + examined.getSavedData().getSpawnData().getKills(), 31, 12);
player.getPacketDispatch().sendString("" + examined.getSavedData().getSpawnData().getDeaths(), 31, 17);
player.getPacketDispatch().sendString("" + examined.getSavedData().getSpawnData().getKillStreak(), 31, 20);
player.getPacketDispatch().sendString("Total Level -> ", 31, 9);
player.getPacketDispatch().sendString("Inventory items -> ", 31, 10);
player.getPacketDispatch().sendString("Inventory value -> ", 31, 16);
player.getPacketDispatch().sendString("Bank value-> ", 31, 19);
player.getPacketDispatch().sendString("" + examined.getSkills().getTotalLevel(), 31, 11);
player.getPacketDispatch().sendString("" + examined.getInventory().itemCount(), 31, 12);
player.getPacketDispatch().sendString("" + examined.getInventory().getWealth(), 31, 17);
player.getPacketDispatch().sendString("" + examined.getBank().getWealth(), 31, 20);
player.getPacketDispatch().sendString("Close", 31, 5);
}
}