forked from 2009Scape/Server
Simplified bot creation and API AGAINNN!
This commit is contained in:
parent
df2528c5e8
commit
42a5104a35
4 changed files with 34 additions and 34 deletions
|
|
@ -14,32 +14,21 @@ 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();
|
||||
|
||||
// Initialize inventory
|
||||
for (Item i : botScript.inventory)
|
||||
{
|
||||
bot.getInventory().add(i);
|
||||
}
|
||||
for (Item i : botScript.equipment)
|
||||
{
|
||||
bot.getEquipment().add(i, true, false);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (!bot.getPulseManager().hasPulseRunning())
|
||||
if (!botScript.bot.getPulseManager().hasPulseRunning())
|
||||
{
|
||||
botScript.runLoop();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +1,35 @@
|
|||
package org.crandor.game.node.entity.player.ai.general;
|
||||
|
||||
import org.crandor.game.interaction.MovementPulse;
|
||||
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.object.GameObject;
|
||||
import org.crandor.game.world.map.Location;
|
||||
import org.crandor.game.world.map.Point;
|
||||
import org.crandor.game.world.map.RegionManager;
|
||||
import org.crandor.game.world.map.RegionPlane;
|
||||
import org.crandor.game.world.map.path.Path;
|
||||
import org.crandor.game.world.map.path.Pathfinder;
|
||||
|
||||
public class ScriptAPI {
|
||||
|
||||
private Player bot;
|
||||
|
||||
public ScriptAPI(Player bot) {
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
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(Player me, String entityName)
|
||||
public Node getNearestNode(String entityName)
|
||||
{
|
||||
Node entity = null;
|
||||
double minDistance = Double.MAX_VALUE;
|
||||
for (Node e : RegionManager.forId(me.getLocation().getRegionId()).getPlanes()[me.getLocation().getZ()].getEntities())
|
||||
for (Node e : RegionManager.forId(bot.getLocation().getRegionId()).getPlanes()[bot.getLocation().getZ()].getEntities())
|
||||
{
|
||||
if (e != null && e.getName().equals(entityName) && distance(me, e) < minDistance && !Pathfinder.find(me, e).isMoveNear())
|
||||
if (e != null && e.getName().equals(entityName) && distance(bot, e) < minDistance && !Pathfinder.find(bot, e).isMoveNear())
|
||||
{
|
||||
entity = e;
|
||||
minDistance = distance(me, e);
|
||||
minDistance = distance(bot, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,11 +13,11 @@ public class ManThiever extends Script {
|
|||
|
||||
@Override
|
||||
public void runLoop() {
|
||||
Node man = scriptAPI.getNearestNode(me, "Man");
|
||||
Node man = scriptAPI.getNearestNode("Man");
|
||||
|
||||
if (man != null)
|
||||
{
|
||||
man.getInteraction().handle(me, man.getInteraction().get(2));
|
||||
man.getInteraction().handle(bot, man.getInteraction().get(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +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 ScriptAPI scriptAPI = new ScriptAPI();
|
||||
public Player me;
|
||||
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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue