mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 14:49:03 +00:00
Added maven framework support. Started working on NPCs #19
This commit is contained in:
21
Plugin NPC/Plugin NPC.iml
Normal file
21
Plugin NPC/Plugin NPC.iml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
|
||||
<orderEntry type="module" module-name="Server" />
|
||||
<orderEntry type="library" name="Maven: org.java-websocket:Java-WebSocket:1.3.6" level="project" />
|
||||
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
3
Plugin NPC/plugin.properties
Normal file
3
Plugin NPC/plugin.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
classpath=net.simon987.npcplugin.NpcPlugin
|
||||
name=NPC Plugin
|
||||
version=1.0
|
||||
36
Plugin NPC/pom.xml
Normal file
36
Plugin NPC/pom.xml
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>net.simon987.pluginnpc</groupId>
|
||||
<artifactId>Plugin NPC</artifactId>
|
||||
<version>1.2a</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.simon987.server</groupId>
|
||||
<artifactId>Server</artifactId>
|
||||
<version>1.2a</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
100
Plugin NPC/src/main/java/net/simon987/npcplugin/HarvestTask.java
Normal file
100
Plugin NPC/src/main/java/net/simon987/npcplugin/HarvestTask.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
|
||||
import net.simon987.server.assembly.Util;
|
||||
import net.simon987.server.game.Direction;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.game.InventoryHolder;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class HarvestTask extends NPCTask {
|
||||
|
||||
private Random random;
|
||||
|
||||
private int pause;
|
||||
|
||||
public HarvestTask() {
|
||||
random = new Random();
|
||||
pause = 0;
|
||||
}
|
||||
|
||||
private Direction nextWorldDirection = null;
|
||||
|
||||
@Override
|
||||
public boolean checkCompleted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(NonPlayerCharacter npc) {
|
||||
|
||||
if (pause == 0) {
|
||||
//Get biomass
|
||||
ArrayList<GameObject> biomass = new ArrayList<>(10);
|
||||
|
||||
for (GameObject object : npc.getWorld().getGameObjects()) {
|
||||
//Plant MAP_INFO
|
||||
if ((object.getMapInfo() & 0x4000) == 0x4000) {
|
||||
biomass.add(object);
|
||||
}
|
||||
}
|
||||
|
||||
//Get closest one
|
||||
int minDist = Integer.MAX_VALUE;
|
||||
GameObject minBiomass = null;
|
||||
|
||||
for (GameObject plant : biomass) {
|
||||
|
||||
int dist = Util.manhattanDist(npc.getX(), npc.getY(), plant.getX(), plant.getY());
|
||||
|
||||
if (dist < minDist) {
|
||||
minDist = dist;
|
||||
minBiomass = plant;
|
||||
}
|
||||
}
|
||||
|
||||
//Move towards it
|
||||
if (minBiomass != null && minDist == 1) {
|
||||
//Reached biomass, change direction to face it
|
||||
Direction newDirection = Direction.getFacing(npc.getX(), npc.getY(),
|
||||
minBiomass.getX(), minBiomass.getY());
|
||||
|
||||
if (newDirection != null) {
|
||||
npc.setDirection(newDirection);
|
||||
|
||||
//Reached biomass, harvest it
|
||||
if (minBiomass instanceof InventoryHolder) {
|
||||
((InventoryHolder) minBiomass).takeItem(1);
|
||||
pause += 6;
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
LogManager.LOGGER.severe("FIXME: tick:HarvestTask, Direction is null");
|
||||
}
|
||||
|
||||
nextWorldDirection = null;
|
||||
} else if (minBiomass != null && npc.moveTo(minBiomass.getX(), minBiomass.getY(), 1)) {
|
||||
//Moving towards biomass...
|
||||
nextWorldDirection = null;
|
||||
} else {
|
||||
|
||||
if (nextWorldDirection == null) {
|
||||
nextWorldDirection = Direction.getDirection(random.nextInt(3));
|
||||
pause += 6;
|
||||
}
|
||||
npc.gotoWorld(nextWorldDirection);
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
pause--;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.game.Direction;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class HarvesterNPC extends NonPlayerCharacter {
|
||||
|
||||
public static final int ID = 10; //todo change
|
||||
|
||||
|
||||
public HarvesterNPC() {
|
||||
setTask(new HarvestTask());
|
||||
hp = 10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
|
||||
if (hp <= 0) {
|
||||
setDead(true);
|
||||
//TODO: drop biomass
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (getTask().checkCompleted()) {
|
||||
|
||||
setTask(new HarvestTask());
|
||||
|
||||
} else {
|
||||
getTask().tick(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = super.serialise();
|
||||
|
||||
json.put("id", getObjectId());
|
||||
json.put("x", getX());
|
||||
json.put("y", getY());
|
||||
json.put("direction", getDirection().ordinal());
|
||||
json.put("hp", hp);
|
||||
json.put("energy", energy);
|
||||
json.put("action", getAction().ordinal());
|
||||
json.put("type", 10);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static HarvesterNPC deserialize(JSONObject json) {
|
||||
|
||||
HarvesterNPC npc = new HarvesterNPC();
|
||||
npc.setObjectId((int) (long) json.get("id"));
|
||||
npc.setX((int) (long) json.get("x"));
|
||||
npc.setY((int) (long) json.get("y"));
|
||||
npc.hp = (int) (long) json.get("hp");
|
||||
npc.setDirection(Direction.getDirection((int) (long) json.get("direction")));
|
||||
npc.energy = (int) (long) json.get("energy");
|
||||
npc.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");
|
||||
|
||||
return npc;
|
||||
|
||||
}
|
||||
}
|
||||
10
Plugin NPC/src/main/java/net/simon987/npcplugin/NPCTask.java
Normal file
10
Plugin NPC/src/main/java/net/simon987/npcplugin/NPCTask.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
|
||||
public abstract class NPCTask {
|
||||
|
||||
public abstract boolean checkCompleted();
|
||||
|
||||
public abstract void tick(NonPlayerCharacter npc);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
import net.simon987.server.game.Action;
|
||||
import net.simon987.server.game.Direction;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.game.Updatable;
|
||||
import net.simon987.server.game.pathfinding.Node;
|
||||
import net.simon987.server.game.pathfinding.Pathfinder;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class NonPlayerCharacter extends GameObject implements Updatable {
|
||||
|
||||
private static final int MAP_INFO = 0x0040;
|
||||
|
||||
protected int hp;
|
||||
|
||||
protected int energy;
|
||||
protected int maxEnergy;
|
||||
|
||||
private NPCTask task;
|
||||
|
||||
private Action lastAction = Action.IDLE;
|
||||
|
||||
@Override
|
||||
public char getMapInfo() {
|
||||
return MAP_INFO;
|
||||
}
|
||||
|
||||
|
||||
public boolean moveTo(int x, int y, int range) {
|
||||
|
||||
ArrayList<Node> path = Pathfinder.findPath(getWorld(), getX(), getY(), x, y, range);
|
||||
|
||||
if (path != null && path.size() > 0) {
|
||||
|
||||
Node nextTile = path.get(1);
|
||||
|
||||
Direction newDirection = Direction.getFacing(getX(), getY(), nextTile.x, nextTile.y);
|
||||
|
||||
if (newDirection != null) {
|
||||
setDirection(newDirection);
|
||||
} else {
|
||||
LogManager.LOGGER.severe("FIXME: moveTo:NonPlayerCharacter, Direction is null");
|
||||
}
|
||||
|
||||
if (incrementLocation()) {
|
||||
lastAction = Action.WALKING;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
lastAction = Action.IDLE;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean gotoWorld(Direction direction) {
|
||||
|
||||
System.out.println("going " + direction);
|
||||
|
||||
if (direction == Direction.NORTH) {
|
||||
if (!moveTo(8, 0, 0)) {
|
||||
setDirection(Direction.NORTH);
|
||||
return incrementLocation();
|
||||
}
|
||||
} else if (direction == Direction.EAST) {
|
||||
if (!moveTo(15, 8, 0)) {
|
||||
setDirection(Direction.EAST);
|
||||
return incrementLocation();
|
||||
}
|
||||
} else if (direction == Direction.SOUTH) {
|
||||
if (!moveTo(7, 15, 0)) {
|
||||
setDirection(Direction.SOUTH);
|
||||
return incrementLocation();
|
||||
}
|
||||
} else if (direction == Direction.WEST) {
|
||||
if (!moveTo(0, 7, 0)) {
|
||||
setDirection(Direction.WEST);
|
||||
return incrementLocation();
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public NPCTask getTask() {
|
||||
return task;
|
||||
}
|
||||
|
||||
public void setTask(NPCTask task) {
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
public Action getAction() {
|
||||
return lastAction;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
import net.simon987.npcplugin.event.WorldUpdateListener;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.io.GameObjectDeserializer;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.plugin.ServerPlugin;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer {
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
listeners.add(new WorldUpdateListener());
|
||||
|
||||
LogManager.LOGGER.info("Initialised NPC plugin");
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameObject deserializeObject(JSONObject object) {
|
||||
;
|
||||
int objType = (int) (long) object.get("type");
|
||||
|
||||
if (objType == HarvesterNPC.ID) {
|
||||
return HarvesterNPC.deserialize(object);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package net.simon987.npcplugin.event;
|
||||
|
||||
import net.simon987.npcplugin.HarvesterNPC;
|
||||
import net.simon987.npcplugin.NonPlayerCharacter;
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.event.WorldUpdateEvent;
|
||||
import net.simon987.server.game.World;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class WorldUpdateListener implements GameEventListener {
|
||||
|
||||
private boolean ok = true;
|
||||
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return WorldUpdateEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
//LogManager.LOGGER.info("Time is " + );
|
||||
|
||||
World world = ((WorldUpdateEvent) event).getWorld();
|
||||
|
||||
if (GameServer.INSTANCE.getGameUniverse().getTime() % 10 == 0) {
|
||||
|
||||
if (ok) {
|
||||
ok = false;
|
||||
LogManager.LOGGER.info("Spawning Harvester\n--------------------------------------");
|
||||
|
||||
NonPlayerCharacter npc = new HarvesterNPC();
|
||||
|
||||
//todo set max iteration
|
||||
Point p = null;
|
||||
while (p == null) {
|
||||
p = world.getRandomPassableTile();
|
||||
}
|
||||
|
||||
|
||||
npc.setWorld(world);
|
||||
npc.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
|
||||
npc.setX(p.x);
|
||||
npc.setY(p.y);
|
||||
|
||||
world.getGameObjects().add(npc);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user