multiple job locations

pull/1/head
valyrie97 2022-12-26 20:44:11 -05:00
parent b773d8e92a
commit aece757dde
7 changed files with 37 additions and 32 deletions

View File

@ -1,6 +1,7 @@
package xyz.valnet.hadean.gameobjects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import xyz.valnet.engine.math.Vector2f;
@ -13,11 +14,10 @@ import xyz.valnet.hadean.interfaces.IWorkable;
public class Job extends GameObject {
private Terrain terrain;
private Job that = this;
public abstract class JobStep {
public abstract Vector2f getLocation();
public abstract Vector2i[] getLocations();
public void next() {
that.nextStep();
}
@ -32,8 +32,8 @@ public class Job extends GameObject {
}
@Override
public Vector2f getLocation() {
return item.getWorldPosition();
public Vector2i[] getLocations() {
return new Vector2i[] { item.getWorldPosition().asInt() };
}
}
@ -43,10 +43,10 @@ public class Job extends GameObject {
this.item = item;
}
public Vector2f getLocation() {
public Vector2i[] getLocations() {
Stockpile pile = that.get(Stockpile.class);
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;
}
@Override
public Vector2f getLocation() {
return subject.getWorkablePositions()[0].asFloat();
public Vector2i[] getLocations() {
return subject.getWorkablePositions();
}
public boolean doWork() {
return subject.doWork();
@ -72,11 +72,6 @@ public class Job extends GameObject {
step = 0;
}
@Override
public void start() {
this.terrain = get(Terrain.class);
}
public Job(String name) {
this.steps = new ArrayList<JobStep>();
this.name = name;
@ -86,10 +81,10 @@ public class Job extends GameObject {
steps.add(step);
}
public Vector2i getLocation() {
public Vector2i[] getLocations() {
if(steps.size() == 0) throw new Error("Cannot get location of job with no steps");
JobStep step = steps.get(0);
return step.getLocation().asInt();
return step.getLocations();
}
public void nextStep() {

View File

@ -1,12 +1,15 @@
package xyz.valnet.hadean.gameobjects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;
import java.util.Set;
import xyz.valnet.engine.math.Vector2f;
@ -60,10 +63,9 @@ public class JobBoard extends GameObject {
.stream()
.map(job -> new Pair<Job, Float>(
job,
job.getLocation().distanceTo(
(int) workerLocation.x,
(int) workerLocation.y
)
Stream.of(job.getLocations())
.map(v -> v.distanceTo((int) workerLocation.x, (int) workerLocation.y))
.reduce(Float.MAX_VALUE, (a, b) -> a < b ? a : b)
))
// sort the jobs by their distance from the worker
.sorted(new Comparator<Pair<Job, Float>>() {

View File

@ -5,6 +5,7 @@ import static xyz.valnet.engine.util.Math.lerp;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
@ -71,6 +72,7 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
if(clazz.isAnonymousClass()) continue;
if(!IBuildable.class.isAssignableFrom(clazz)) continue;
if(clazz.isInterface()) continue;
if(Modifier.isAbstract(clazz.getModifiers())) continue;
Constructor<? extends IBuildable> constructor = clazz.getConstructor();
if(constructor.getParameterCount() != 0) {

View File

@ -70,8 +70,6 @@ public class FarmPlot extends WorldObject implements ISelectable, ITileThing, IB
this.y = y;
this.w = w;
this.h = h;
System.out.println("<" + x + ", " + y + ", " + w + ", " + h + ">");
System.out.println(inScene());
for(int i = x; i < x + w; i ++) {
for(int j = y; j < y + h; j ++) {

View File

@ -1,6 +1,7 @@
package xyz.valnet.hadean.gameobjects.worldobjects;
import xyz.valnet.engine.math.Vector2f;
import xyz.valnet.engine.math.Vector2i;
import xyz.valnet.engine.math.Vector4f;
import xyz.valnet.hadean.gameobjects.Job;
import xyz.valnet.hadean.gameobjects.JobBoard;
@ -51,7 +52,8 @@ public class Pawn extends Agent implements IWorker {
@Override
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;
@ -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(!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.
}
}
private boolean isPathingToJobLocation() {
if(!isPathing()) return false;
return getDestination().equals(jobboard.getJob(this).getCurrentStep().getLocation().asInt());
return getDestination().isOneOf(jobboard.getJob(this).getCurrentStep().getLocations());
}
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() {
goTo(jobboard
goToClosest(jobboard
.getJob(this)
.getCurrentStep()
.getLocation()
.asInt()
.getLocations()
);
}
@ -108,7 +109,8 @@ public class Pawn extends Agent implements IWorker {
private boolean doJob() {
if(!jobboard.workerHasJob(this)) return false;
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) {
Job.Work workStep = (Job.Work)step;

View File

@ -97,9 +97,13 @@ public abstract class Agent extends WorldObject implements ISelectable {
}
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);
frameCounter = 0;
}
protected void goToClosest(Vector2i[] destinations) {
path = pathfinder.getBestPath(this.getWorldPosition().asInt(), destinations);
frameCounter = 0;
}
protected void goTo(Vector2i location) {
@ -107,7 +111,6 @@ public abstract class Agent extends WorldObject implements ISelectable {
}
protected void wander() {
System.out.println("WANDER!");
int randomX = (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);

View File

@ -147,6 +147,9 @@ public class AStarPathfinder implements IPathfinder {
@Override
public Path getBestPath(Vector2i src, Vector2i[] dsts) {
if(src.isOneOf(dsts)) return null;
int cost = Integer.MAX_VALUE;
Path bestPath = null;
for(Vector2i dst : dsts) {