Skip to content

Commit

Permalink
JavaGOAP -> refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Sep 7, 2023
1 parent fd7e953 commit 5e39edf
Show file tree
Hide file tree
Showing 37 changed files with 460 additions and 495 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package lib.goap;
package lib.goap.action;

import lib.goap.state.GoapState;
import lib.goap.target.GoatTargetable;
import lib.goap.unit.IGoapUnit;
import lombok.Getter;
import lombok.NonNull;
import org.springframework.lang.Nullable;

import java.util.HashSet;

@Getter
public abstract class GoapAction {
public abstract class GoapActionBase {

@Nullable
protected final Object target;
@Getter
@NonNull
protected final GoatTargetable target;
@NonNull
private final HashSet<GoapState> preconditions = new HashSet<>();
@Getter
@NonNull
private final HashSet<GoapState> effects = new HashSet<>();

/**
* @param target the target of the action. Since "Object" is being used this is
* NOT type safe!
* @param target the target of the action. Can be null.
*/
public GoapAction(@NonNull final Object target) {
public GoapActionBase(@NonNull final GoatTargetable target) {
this.target = target;
}

Expand All @@ -41,7 +39,7 @@ public GoapAction(@NonNull final Object target) {
* Gets called when the action is going to be executed by the Unit.
*
* @param goapUnit the GoapUnit that is trying to execute the action.
* @return true or false depending if the action was successful.
* @return true or false depending on if the action was successful.
*/
public abstract boolean performAction(@NonNull final IGoapUnit goapUnit);

Expand Down Expand Up @@ -118,22 +116,6 @@ public float generateCost(@NonNull final IGoapUnit goapUnit) {
*/
public abstract void reset();

/**
* Overloaded function for convenience.
*
* @param importance the importance of the precondition being added.
* @param effect the effect of the precondition being added.
* @param value the value of the precondition being added.
* @see #addPrecondition(GoapState precondition)
*/
public void addPrecondition(
final int importance,
@NonNull final String effect,
@NonNull final Object value
) {
addPrecondition(new GoapState(importance, effect, value));
}

/**
* Add a precondition, which is not already in the HashSet.
*
Expand All @@ -142,9 +124,10 @@ public void addPrecondition(
public void addPrecondition(@NonNull final GoapState precondition) {
boolean alreadyInList = false;

for (final GoapState goapState : this.preconditions) {
for (final GoapState goapState : preconditions) {
if (goapState.equals(precondition)) {
alreadyInList = true;
break;
}
}

Expand All @@ -153,18 +136,6 @@ public void addPrecondition(@NonNull final GoapState precondition) {
}
}

/**
* Overloaded function for convenience.
*
* @param precondition the precondition that is being removed.
* @return true or false depending on if the precondition was removed
* successfully.
* @see #removePrecondition(String preconditionEffect)
*/
public boolean removePrecondition(@NonNull final GoapState precondition) {
return removePrecondition(precondition.effect);
}

/**
* Remove a precondition from the HashSet.
*
Expand All @@ -174,8 +145,8 @@ public boolean removePrecondition(@NonNull final GoapState precondition) {
public boolean removePrecondition(@NonNull final String preconditionEffect) {
GoapState stateToBeRemoved = null;

for (final GoapState goapState : this.effects) {
if (goapState.effect.equals(preconditionEffect)) {
for (final GoapState goapState : effects) {
if (goapState.getEffect().equals(preconditionEffect)) {
stateToBeRemoved = goapState;
}
}
Expand All @@ -188,21 +159,6 @@ public boolean removePrecondition(@NonNull final String preconditionEffect) {
}
}

/**
* Overloaded function for convenience.
*
* @param importance the importance of the effect being added.
* @param effect the effect of the effect being added.
* @param value the value of the effect being added.
* @see #addEffect(GoapState effect)
*/
public void addEffect(
final int importance,
@NonNull final String effect,
@NonNull final Object value
) {
addEffect(new GoapState(importance, effect, value));
}

/**
* Add an effect, which is not already in the HashSet
Expand All @@ -212,14 +168,15 @@ public void addEffect(
public void addEffect(@NonNull final GoapState effect) {
boolean alreadyInList = false;

for (final GoapState goapState : this.effects) {
for (final GoapState goapState : effects) {
if (goapState.equals(effect)) {
alreadyInList = true;
break;
}
}

if (!alreadyInList) {
this.effects.add(effect);
effects.add(effect);
}
}

Expand All @@ -231,7 +188,7 @@ public void addEffect(@NonNull final GoapState effect) {
* @see #removeEffect(String effectEffect)
*/
public boolean removeEffect(@NonNull final GoapState effect) {
return this.removeEffect(effect.effect);
return removeEffect(effect.getEffect());
}

/**
Expand All @@ -243,18 +200,21 @@ public boolean removeEffect(@NonNull final GoapState effect) {
public boolean removeEffect(@NonNull final String effectEffect) {
GoapState stateToBeRemoved = null;

for (final GoapState goapState : this.effects) {
if (goapState.effect.equals(effectEffect)) {
for (final GoapState goapState : effects) {
if (goapState.getEffect().equals(effectEffect)) {
stateToBeRemoved = goapState;
}
}

if (stateToBeRemoved != null) {
this.effects.remove(stateToBeRemoved);
effects.remove(stateToBeRemoved);
return true;
} else {
return false;
}
}

@Override
public abstract int hashCode();

}
4 changes: 2 additions & 2 deletions src/main/java/lib/goap/agent/FSMPlanEventListenable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lib.goap.agent;

import lib.goap.GoapAction;
import lib.goap.action.GoapActionBase;
import lombok.NonNull;

import java.util.Queue;
Expand All @@ -12,7 +12,7 @@ public interface FSMPlanEventListenable {
*
* @param actions the rest of the action Queue which failed to execute.
*/
void onPlanFailed(@NonNull final Queue<GoapAction> actions);
void onPlanFailed(@NonNull final Queue<GoapActionBase> actions);

/**
* Gets called when a RunActionState on the FSM returns true and therefore
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/lib/goap/agent/GoapAgentBase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lib.goap.agent;

import lib.goap.GoapAction;
import lib.goap.GoapState;
import lib.goap.action.GoapActionBase;
import lib.goap.state.GoapState;
import lib.goap.fsm.FSM;
import lib.goap.fsm.states.IdleState;
import lib.goap.fsm.states.PerformActionState;
Expand Down Expand Up @@ -42,27 +42,27 @@ public void update() {
}

@Override
public void onPlanCreated(@NonNull final Queue<GoapAction> plan) {
public void onPlanCreated(@NonNull final Queue<GoapActionBase> plan) {
assignedGoapUnit.goapPlanFound(plan);
fsm.popStack();
fsm.pushStack(new PerformActionState(fsm, plan));
}

@Override
public void onImportantUnitGoalChange(@NonNull final GoapState newGoalState) {
newGoalState.importance = Integer.MAX_VALUE;
newGoalState.setImportance(Integer.MAX_VALUE);
fsm.pushStack(idleState);
}

@Override
public void onImportantUnitStackResetChange() {
assignedGoapUnit.getAvailableActions().forEach(GoapAction::reset);
assignedGoapUnit.getAvailableActions().forEach(GoapActionBase::reset);
fsm.clearStack();
fsm.pushStack(idleState);
}

@Override
public void onPlanFailed(@NonNull final Queue<GoapAction> actions) {
public void onPlanFailed(@NonNull final Queue<GoapActionBase> actions) {
assignedGoapUnit.goapPlanFailed(actions);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lib.goap.agent;

import lib.goap.GoapState;
import lib.goap.state.GoapState;

public interface ImportantUnitChangeEventListenable {

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/lib/goap/agent/PlanCreatedEventListenable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lib.goap.agent;

import lib.goap.GoapAction;
import lib.goap.action.GoapActionBase;

import java.util.Queue;

Expand All @@ -14,6 +14,6 @@ public interface PlanCreatedEventListenable {
* @param plan the plan that the Planner has created and is ready to be
* executed.
*/
void onPlanCreated(Queue<GoapAction> plan);
void onPlanCreated(Queue<GoapActionBase> plan);

}
4 changes: 2 additions & 2 deletions src/main/java/lib/goap/fsm/FSM.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lib.goap.fsm;

import lib.goap.GoapAction;
import lib.goap.action.GoapActionBase;
import lib.goap.agent.FSMPlanEventListenable;
import lib.goap.fsm.states.FSMStateful;
import lib.goap.fsm.states.PerformActionState;
Expand Down Expand Up @@ -49,7 +49,7 @@ private synchronized void dispatchPlanFinishedEvent() {
planEventListeners.forEach(FSMPlanEventListenable::onPlanFinished);
}

private synchronized void dispatchPlanFailedEvent(@NonNull final Queue<GoapAction> actions) {
private synchronized void dispatchPlanFailedEvent(@NonNull final Queue<GoapActionBase> actions) {
planEventListeners.forEach(listener -> listener.onPlanFailed(actions));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/lib/goap/fsm/states/IdleState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lib.goap.fsm.states;

import lib.goap.GoapAction;
import lib.goap.action.GoapActionBase;
import lib.goap.agent.PlanCreatedEventListenable;
import lib.goap.planner.GoapPlannerable;
import lib.goap.unit.IGoapUnit;
Expand Down Expand Up @@ -29,7 +29,7 @@ public IdleState(@NonNull final GoapPlannerable goapPlanner) {
*/
@Override
public boolean isStateStillPerforming(@NonNull final IGoapUnit goapUnit) {
final Queue<GoapAction> plannedQueue = goapPlanner.plan(goapUnit);
final Queue<GoapActionBase> plannedQueue = goapPlanner.plan(goapUnit);

if (plannedQueue != null) {
dispatchNewPlanCreatedEvent(plannedQueue);
Expand All @@ -40,7 +40,7 @@ public boolean isStateStillPerforming(@NonNull final IGoapUnit goapUnit) {
return true;
}

private synchronized void dispatchNewPlanCreatedEvent(@NonNull final Queue<GoapAction> plan) {
private synchronized void dispatchNewPlanCreatedEvent(@NonNull final Queue<GoapActionBase> plan) {
planCreatedListeners.forEach(listener -> listener.onPlanCreated(plan));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/lib/goap/fsm/states/MoveToState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lib.goap.fsm.states;

import lib.goap.GoapAction;
import lib.goap.action.GoapActionBase;
import lib.goap.unit.IGoapUnit;
import lombok.Getter;
import lombok.NonNull;
Expand All @@ -11,7 +11,7 @@
class MoveToState implements FSMStateful {

@NonNull
private final GoapAction currentAction;
private final GoapActionBase currentAction;

/**
* Move to the target of the currentAction until the unit is in range to perform the action itself.
Expand All @@ -21,14 +21,14 @@ class MoveToState implements FSMStateful {
*/
@Override
public boolean isStateStillPerforming(@NonNull final IGoapUnit goapUnit) {
// @TODO RETHINK METHODS NAME!!!
boolean stillMoving = true;

if ((currentAction.requiresInRange(goapUnit) && currentAction.isInRange(goapUnit)) || currentAction.getTarget() == null) {
stillMoving = false;
} else {
goapUnit.moveTo(currentAction.getTarget());
}

return stillMoving;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/lib/goap/fsm/states/PerformActionState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lib.goap.fsm.states;

import lib.goap.GoapAction;
import lib.goap.action.GoapActionBase;
import lib.goap.UnperformableActionException;
import lib.goap.fsm.FSM;
import lib.goap.unit.IGoapUnit;
Expand All @@ -15,11 +15,11 @@ public class PerformActionState implements FSMStateful {
private final FSM fsm;
@Getter
@NonNull
private final Queue<GoapAction> currentActions;
private final Queue<GoapActionBase> currentActions;

public PerformActionState(
@NonNull final FSM fsm,
@NonNull final Queue<GoapAction> currentActions
@NonNull final Queue<GoapActionBase> currentActions
) {
this.fsm = fsm;
this.currentActions = currentActions;
Expand Down Expand Up @@ -52,7 +52,7 @@ public boolean isStateStillPerforming(@NonNull final IGoapUnit goapUnit) throws
}

if (!currentActions.isEmpty()) {
final GoapAction currentAction = currentActions.peek();
final GoapActionBase currentAction = currentActions.peek();

// No Exception since handling this is user specific.
if (currentAction.getTarget() == null) {
Expand Down
Loading

0 comments on commit 5e39edf

Please sign in to comment.