multiple job locations
parent
b773d8e92a
commit
aece757dde
|
|
@ -1,6 +1,7 @@
|
||||||
package xyz.valnet.hadean.gameobjects;
|
package xyz.valnet.hadean.gameobjects;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import xyz.valnet.engine.math.Vector2f;
|
import xyz.valnet.engine.math.Vector2f;
|
||||||
|
|
@ -13,11 +14,10 @@ import xyz.valnet.hadean.interfaces.IWorkable;
|
||||||
|
|
||||||
public class Job extends GameObject {
|
public class Job extends GameObject {
|
||||||
|
|
||||||
private Terrain terrain;
|
|
||||||
private Job that = this;
|
private Job that = this;
|
||||||
|
|
||||||
public abstract class JobStep {
|
public abstract class JobStep {
|
||||||
public abstract Vector2f getLocation();
|
public abstract Vector2i[] getLocations();
|
||||||
public void next() {
|
public void next() {
|
||||||
that.nextStep();
|
that.nextStep();
|
||||||
}
|
}
|
||||||
|
|
@ -32,8 +32,8 @@ public class Job extends GameObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vector2f getLocation() {
|
public Vector2i[] getLocations() {
|
||||||
return item.getWorldPosition();
|
return new Vector2i[] { item.getWorldPosition().asInt() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,10 +43,10 @@ public class Job extends GameObject {
|
||||||
this.item = item;
|
this.item = item;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2f getLocation() {
|
public Vector2i[] getLocations() {
|
||||||
Stockpile pile = that.get(Stockpile.class);
|
Stockpile pile = that.get(Stockpile.class);
|
||||||
Vector4f box = pile.getWorldBox();
|
Vector4f box = pile.getWorldBox();
|
||||||
return new Vector2f(box.x, box.y);
|
return new Vector2i[] { new Vector2f(box.x, box.y).asInt() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,8 +56,8 @@ public class Job extends GameObject {
|
||||||
this.subject = subject;
|
this.subject = subject;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Vector2f getLocation() {
|
public Vector2i[] getLocations() {
|
||||||
return subject.getWorkablePositions()[0].asFloat();
|
return subject.getWorkablePositions();
|
||||||
}
|
}
|
||||||
public boolean doWork() {
|
public boolean doWork() {
|
||||||
return subject.doWork();
|
return subject.doWork();
|
||||||
|
|
@ -72,11 +72,6 @@ public class Job extends GameObject {
|
||||||
step = 0;
|
step = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
this.terrain = get(Terrain.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Job(String name) {
|
public Job(String name) {
|
||||||
this.steps = new ArrayList<JobStep>();
|
this.steps = new ArrayList<JobStep>();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|
@ -86,10 +81,10 @@ public class Job extends GameObject {
|
||||||
steps.add(step);
|
steps.add(step);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2i getLocation() {
|
public Vector2i[] getLocations() {
|
||||||
if(steps.size() == 0) throw new Error("Cannot get location of job with no steps");
|
if(steps.size() == 0) throw new Error("Cannot get location of job with no steps");
|
||||||
JobStep step = steps.get(0);
|
JobStep step = steps.get(0);
|
||||||
return step.getLocation().asInt();
|
return step.getLocations();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void nextStep() {
|
public void nextStep() {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
package xyz.valnet.hadean.gameobjects;
|
package xyz.valnet.hadean.gameobjects;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.function.BinaryOperator;
|
||||||
|
import java.util.stream.Stream;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import xyz.valnet.engine.math.Vector2f;
|
import xyz.valnet.engine.math.Vector2f;
|
||||||
|
|
@ -60,10 +63,9 @@ public class JobBoard extends GameObject {
|
||||||
.stream()
|
.stream()
|
||||||
.map(job -> new Pair<Job, Float>(
|
.map(job -> new Pair<Job, Float>(
|
||||||
job,
|
job,
|
||||||
job.getLocation().distanceTo(
|
Stream.of(job.getLocations())
|
||||||
(int) workerLocation.x,
|
.map(v -> v.distanceTo((int) workerLocation.x, (int) workerLocation.y))
|
||||||
(int) workerLocation.y
|
.reduce(Float.MAX_VALUE, (a, b) -> a < b ? a : b)
|
||||||
)
|
|
||||||
))
|
))
|
||||||
// sort the jobs by their distance from the worker
|
// sort the jobs by their distance from the worker
|
||||||
.sorted(new Comparator<Pair<Job, Float>>() {
|
.sorted(new Comparator<Pair<Job, Float>>() {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import static xyz.valnet.engine.util.Math.lerp;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
@ -71,6 +72,7 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
|
||||||
if(clazz.isAnonymousClass()) continue;
|
if(clazz.isAnonymousClass()) continue;
|
||||||
if(!IBuildable.class.isAssignableFrom(clazz)) continue;
|
if(!IBuildable.class.isAssignableFrom(clazz)) continue;
|
||||||
if(clazz.isInterface()) continue;
|
if(clazz.isInterface()) continue;
|
||||||
|
if(Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||||
|
|
||||||
Constructor<? extends IBuildable> constructor = clazz.getConstructor();
|
Constructor<? extends IBuildable> constructor = clazz.getConstructor();
|
||||||
if(constructor.getParameterCount() != 0) {
|
if(constructor.getParameterCount() != 0) {
|
||||||
|
|
|
||||||
|
|
@ -70,8 +70,6 @@ public class FarmPlot extends WorldObject implements ISelectable, ITileThing, IB
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.w = w;
|
this.w = w;
|
||||||
this.h = h;
|
this.h = h;
|
||||||
System.out.println("<" + x + ", " + y + ", " + w + ", " + h + ">");
|
|
||||||
System.out.println(inScene());
|
|
||||||
|
|
||||||
for(int i = x; i < x + w; i ++) {
|
for(int i = x; i < x + w; i ++) {
|
||||||
for(int j = y; j < y + h; j ++) {
|
for(int j = y; j < y + h; j ++) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package xyz.valnet.hadean.gameobjects.worldobjects;
|
package xyz.valnet.hadean.gameobjects.worldobjects;
|
||||||
|
|
||||||
import xyz.valnet.engine.math.Vector2f;
|
import xyz.valnet.engine.math.Vector2f;
|
||||||
|
import xyz.valnet.engine.math.Vector2i;
|
||||||
import xyz.valnet.engine.math.Vector4f;
|
import xyz.valnet.engine.math.Vector4f;
|
||||||
import xyz.valnet.hadean.gameobjects.Job;
|
import xyz.valnet.hadean.gameobjects.Job;
|
||||||
import xyz.valnet.hadean.gameobjects.JobBoard;
|
import xyz.valnet.hadean.gameobjects.JobBoard;
|
||||||
|
|
@ -51,7 +52,8 @@ public class Pawn extends Agent implements IWorker {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vector4f getWorldBox() {
|
public Vector4f getWorldBox() {
|
||||||
return new Vector4f(x, y, x + 1, y + 1);
|
Vector2f pos = getCalculatedPosition();
|
||||||
|
return new Vector4f(pos.x, pos.y, pos.x+1, pos.y+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JobBoard jobboard;
|
private JobBoard jobboard;
|
||||||
|
|
@ -73,26 +75,25 @@ public class Pawn extends Agent implements IWorker {
|
||||||
|
|
||||||
// if we still dont have a job and we're not moving around
|
// if we still dont have a job and we're not moving around
|
||||||
if(!jobboard.workerHasJob(this) && !isPathing()) {
|
if(!jobboard.workerHasJob(this) && !isPathing()) {
|
||||||
if(Math.random() > 0.001f) wander(); // have a chance of wandering!
|
if(Math.random() < 0.001f) wander(); // have a chance of wandering!
|
||||||
return; // and dont think about anything else.
|
return; // and dont think about anything else.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPathingToJobLocation() {
|
private boolean isPathingToJobLocation() {
|
||||||
if(!isPathing()) return false;
|
if(!isPathing()) return false;
|
||||||
return getDestination().equals(jobboard.getJob(this).getCurrentStep().getLocation().asInt());
|
return getDestination().isOneOf(jobboard.getJob(this).getCurrentStep().getLocations());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAtJobStepLocation() {
|
private boolean isAtJobStepLocation() {
|
||||||
return jobboard.getJob(this).getCurrentStep().getLocation().equals(this.getWorldPosition());
|
return this.getWorldPosition().asInt().isOneOf(jobboard.getJob(this).getCurrentStep().getLocations());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void goToJobStepLocation() {
|
private void goToJobStepLocation() {
|
||||||
goTo(jobboard
|
goToClosest(jobboard
|
||||||
.getJob(this)
|
.getJob(this)
|
||||||
.getCurrentStep()
|
.getCurrentStep()
|
||||||
.getLocation()
|
.getLocations()
|
||||||
.asInt()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,7 +109,8 @@ public class Pawn extends Agent implements IWorker {
|
||||||
private boolean doJob() {
|
private boolean doJob() {
|
||||||
if(!jobboard.workerHasJob(this)) return false;
|
if(!jobboard.workerHasJob(this)) return false;
|
||||||
JobStep step = jobboard.getJob(this).getCurrentStep();
|
JobStep step = jobboard.getJob(this).getCurrentStep();
|
||||||
if(!getWorldPosition().asInt().equals(step.getLocation().asInt())) return false;
|
// if we're not at the location of the job...
|
||||||
|
if(!isAtJobStepLocation()) return false;
|
||||||
|
|
||||||
if(step instanceof Job.Work) {
|
if(step instanceof Job.Work) {
|
||||||
Job.Work workStep = (Job.Work)step;
|
Job.Work workStep = (Job.Work)step;
|
||||||
|
|
|
||||||
|
|
@ -97,9 +97,13 @@ public abstract class Agent extends WorldObject implements ISelectable {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void goTo(int x, int y) {
|
protected void goTo(int x, int y) {
|
||||||
if(x == (int) this.x && y == (int) this.y) return;
|
|
||||||
frameCounter = 0;
|
|
||||||
path = pathfinder.getPath((int)this.x, (int)this.y, x, y);
|
path = pathfinder.getPath((int)this.x, (int)this.y, x, y);
|
||||||
|
frameCounter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void goToClosest(Vector2i[] destinations) {
|
||||||
|
path = pathfinder.getBestPath(this.getWorldPosition().asInt(), destinations);
|
||||||
|
frameCounter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void goTo(Vector2i location) {
|
protected void goTo(Vector2i location) {
|
||||||
|
|
@ -107,7 +111,6 @@ public abstract class Agent extends WorldObject implements ISelectable {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void wander() {
|
protected void wander() {
|
||||||
System.out.println("WANDER!");
|
|
||||||
int randomX = (int)Math.floor(Math.random() * Terrain.WORLD_SIZE);
|
int randomX = (int)Math.floor(Math.random() * Terrain.WORLD_SIZE);
|
||||||
int randomY = (int)Math.floor(Math.random() * Terrain.WORLD_SIZE);
|
int randomY = (int)Math.floor(Math.random() * Terrain.WORLD_SIZE);
|
||||||
path = pathfinder.getPath((int)x, (int)y, randomX, randomY);
|
path = pathfinder.getPath((int)x, (int)y, randomX, randomY);
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,9 @@ public class AStarPathfinder implements IPathfinder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Path getBestPath(Vector2i src, Vector2i[] dsts) {
|
public Path getBestPath(Vector2i src, Vector2i[] dsts) {
|
||||||
|
|
||||||
|
if(src.isOneOf(dsts)) return null;
|
||||||
|
|
||||||
int cost = Integer.MAX_VALUE;
|
int cost = Integer.MAX_VALUE;
|
||||||
Path bestPath = null;
|
Path bestPath = null;
|
||||||
for(Vector2i dst : dsts) {
|
for(Vector2i dst : dsts) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue