This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Movable Refactoring #563
Open
homoroselaps
wants to merge
54
commits into
jsettlers:master
Choose a base branch
from
homoroselaps:MovableRefactoring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Movable Refactoring #563
Changes from 32 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
f10bf7d
Create Entity and delegate all calls to Movable's interfaces to the C…
homoroselaps c7cb40f
Merge branch 'master' into MovableRefactoring
homoroselaps c5be586
Filled in the boilerplate code for the Components
homoroselaps a8547e9
missed file
homoroselaps 400bcbd
remove String indexing, switch to Class IdentityHashMap
homoroselaps 3d2fffa
incorporate IScheduleTimerable into Entity
homoroselaps 7353c95
add SimpleBehaviourTree
homoroselaps 94df9bd
Merge remote-tracking branch 'original/master' into MovableRefactoring
homoroselaps 2ca099a
Implement Pathing Code into SteeringComponent
homoroselaps df49b0f
unify the Movable interface to support future refactorings
homoroselaps 40799ce
remove author
homoroselaps 8396115
Merge branch 'MovableInterface' into MovableRefactoring
homoroselaps 08c034f
lambda behavior tree, basc geologist
homoroselaps 40423a0
Entity lifecycle, convertTo
homoroselaps 5b6bd8a
introduce OnEnable/OnDisable/OnDestroy in Component life cycle
homoroselaps 65fd543
cleanup
homoroselaps 1a7fffb
first draft for bearer behavior tree and component cleanup
homoroselaps 20a7456
fix geologist behavior tree
homoroselaps 1a4e916
first donkey behavior tree and cleanup
homoroselaps b1f911c
architecture runs, Donkey AI works, too many fixes
homoroselaps c7b8018
fix AnimationComponent
homoroselaps 7f308b6
support game serialization
homoroselaps 9c0c2da
Clean up code, fix static code inspection issues
homoroselaps aff376b
Merge remote-tracking branch 'original/master' into MovableRefactoring
homoroselaps d7ca002
Merge branch 'master' into MovableRefactoring and regenerate replay s…
homoroselaps adb8826
fix: more than one material got removed at each call
homoroselaps 474fd44
Improve BehaviorTree nodes and design, improve donkey BT
homoroselaps 623daf4
fix run components multiple times per timerEvent, support Notificatio…
homoroselaps cf110b7
fix Entity called only once
homoroselaps 3c94118
Merge branch 'master' into MovableRefactoring
homoroselaps f86b87c
implement idle behavior
homoroselaps 186e419
implement basic reaction to push requests, improve handling of entity…
homoroselaps 2005940
Merge branch 'master' into MovableRefactoring
andreas-eberle 7de0f8f
Fix some method and field names
andreas-eberle 4027c57
Further renamings
andreas-eberle d140c09
Change notification retrieval to use streams instead of iterator
andreas-eberle f2bf74d
Refactor MovableWrapper
andreas-eberle 918c57a
Add test map with specialists
andreas-eberle db980d2
Further refactorings and started work on Geologist
andreas-eberle 116bc1f
Improve debugging node to print debug information in levels and only …
andreas-eberle ac76ec4
Add DynamicGuardSelector and make Geologist mostly working
andreas-eberle e76340f
Improve stop working behavior of geologist
andreas-eberle 076a981
Make Notification implement Serializable
andreas-eberle 1fd9452
Use notification to start and stop work of geologist
andreas-eberle 54e5d7f
Improve tree readability by including debug message to parameters of …
andreas-eberle b3aa62f
Merge remote-tracking branch 'original/master' into MovableRefactoring
homoroselaps 1b7175b
fix notificationCondition, fix chained animation
homoroselaps 7761f6e
fix bearer steeringComponent idleBehavior
homoroselaps 8ddfef7
implement convertTo other entity
homoroselaps a54b76c
start implementing BuildingWorker
homoroselaps ea0d6f8
WIP: BuildingWorkerBehaviorComponent
0642bbb
BuldingWorker Melter is working
0a3b0e9
presumably all BuildingWorkers work (tested: StoneCutter, Farmer, Shi…
7fccf39
basic BrickLayer Behavior implemented. not tested yet
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 239 additions & 0 deletions
239
jsettlers.logic/src/main/java/jsettlers/logic/movable/BehaviorTreeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
package jsettlers.logic.movable; | ||
|
||
import java.util.Iterator; | ||
|
||
import jsettlers.common.movable.EMovableAction; | ||
import jsettlers.common.movable.EMovableType; | ||
import jsettlers.logic.constants.MatchConstants; | ||
import jsettlers.logic.movable.components.SteeringComponent; | ||
import jsettlers.logic.movable.simplebehaviortree.Decorator; | ||
import jsettlers.logic.movable.simplebehaviortree.IBooleanConditionFunction; | ||
import jsettlers.logic.movable.simplebehaviortree.INodeStatusActionConsumer; | ||
import jsettlers.logic.movable.simplebehaviortree.INodeStatusActionFunction; | ||
import jsettlers.logic.movable.simplebehaviortree.Node; | ||
import jsettlers.logic.movable.simplebehaviortree.NodeStatus; | ||
import jsettlers.logic.movable.simplebehaviortree.Tick; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Action; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Condition; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Failer; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Guard; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Inverter; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.MemSelector; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.MemSequence; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Parallel; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Property; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Repeat; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Selector; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Sequence; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Succeeder; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Wait; | ||
|
||
public final class BehaviorTreeHelper { | ||
|
||
/* --- Node Factory --- */ | ||
|
||
public static Action<Context> Action(INodeStatusActionConsumer<Context> action) { | ||
return new Action<Context>(action); | ||
} | ||
|
||
public static Action<Context> Action(INodeStatusActionFunction<Context> action) { | ||
return new Action<Context>(action); | ||
} | ||
|
||
public static Condition<Context> Condition(IBooleanConditionFunction<Context> condition) { | ||
return new Condition<>(condition); | ||
} | ||
|
||
public static Failer<Context> Failer() { | ||
return new Failer<>(); | ||
} | ||
|
||
public static Guard<Context> Guard(IBooleanConditionFunction<Context> condition, Node<Context> child) { | ||
return new Guard<Context>(condition, child); | ||
} | ||
|
||
public static Guard<Context> Guard(IBooleanConditionFunction<Context> condition, boolean shouldBe, Node<Context> child) { | ||
return new Guard<Context>(condition, shouldBe, child); | ||
} | ||
|
||
public static Inverter<Context> Inverter(Node<Context> child) { | ||
return new Inverter<>(child); | ||
} | ||
|
||
@SafeVarargs | ||
public static MemSelector<Context> MemSelector(Node<Context>... children) { | ||
return new MemSelector<>(children); | ||
} | ||
|
||
@SafeVarargs | ||
public static MemSequence<Context> MemSequence(Node<Context>... children) { | ||
return new MemSequence<>(children); | ||
} | ||
|
||
@SafeVarargs | ||
public static Parallel<Context> Parallel(Parallel.Policy successPolicy, boolean preemptive, Node<Context>... children) { | ||
return new Parallel<Context>(successPolicy, preemptive, children); | ||
} | ||
|
||
public static Repeat<Context> Repeat(Repeat.Policy policy, Node<Context> condition, Node<Context> child) { | ||
return new Repeat<>(policy, condition, child); | ||
} | ||
|
||
public static Repeat<Context> Repeat(Node<Context> condition, Node<Context> child) { | ||
return new Repeat<>(condition, child); | ||
} | ||
|
||
@SafeVarargs | ||
public static Selector<Context> Selector(Node<Context>... children) { | ||
return new Selector<>(children); | ||
} | ||
|
||
@SafeVarargs | ||
public static Sequence<Context> Sequence(Node<Context>... children) { | ||
return new Sequence<>(children); | ||
} | ||
|
||
public static Succeeder<Context> Succeeder() { | ||
return new Succeeder<>(); | ||
} | ||
|
||
public static Wait<Context> Wait(Node<Context> condition) { | ||
return new Wait<>(condition); | ||
} | ||
|
||
public static NotificationCondition NotificationCondition(Class<? extends Notification> type) { | ||
return new NotificationCondition(type); | ||
} | ||
|
||
public static NotificationCondition NotificationCondition(Class<? extends Notification> type, boolean consume) { | ||
return new NotificationCondition(type, consume); | ||
} | ||
|
||
public static Node<Context> WaitForTargetReached_FailIfNotReachable() { | ||
// wait for targetReached, but abort if target not reachable | ||
return Sequence( | ||
Inverter(NotificationCondition(SteeringComponent.TargetNotReachedTrigger.class, true)), | ||
Wait(NotificationCondition(SteeringComponent.TargetReachedTrigger.class, true)) | ||
); | ||
} | ||
|
||
public static Property<Context, Boolean> SetAttackableWhile(boolean value, Node<Context> child) { | ||
return new Property<>( | ||
(c,v)->{c.entity.attC().IsAttackable(v);}, | ||
(c)->{return c.entity.attC().IsAttackable();}, | ||
value, | ||
child); | ||
} | ||
|
||
public static Property<Context, Boolean> SetIdleBehaviorActiveWhile(boolean value, Node<Context> child) { | ||
return new Property<>( | ||
(c,v)->{c.entity.steerC().IsIdleBehaviorActive(v);}, | ||
(c)->c.entity.steerC().IsIdleBehaviorActive(), | ||
value, | ||
child); | ||
} | ||
|
||
public static Guard<Context> TriggerGuard(Class<? extends Notification> type, Node<Context> child) { | ||
return new Guard<>(entity -> entity.comp.getNotificationsIt(type).hasNext(), true, child); | ||
} | ||
|
||
public static Action<Context> StartAnimation(EMovableAction animation, short duration) { | ||
return new Action<>(c -> { | ||
c.entity.aniC().startAnimation(animation, duration); | ||
}); | ||
} | ||
|
||
public static void convertTo(Entity entity, EMovableType type) { | ||
Entity blueprint = EntityFactory.CreateEntity(entity.gameC().getMovableGrid(), type, entity.movC().getPos(), entity.movC().getPlayer()); | ||
entity.convertTo(blueprint); | ||
} | ||
|
||
public static <T> Node<T> Optional(Node<T> child) { | ||
return new Selector<T>(child, new Succeeder<T>()); | ||
} | ||
|
||
public static Sleep Sleep(int milliseconds) { | ||
return new Sleep(milliseconds); | ||
} | ||
|
||
public static class Sleep extends Node<Context> { | ||
private static final long serialVersionUID = 8774557186392581042L; | ||
final int delay; | ||
int endTime; | ||
public Sleep(int milliseconds) { | ||
super(); | ||
delay = milliseconds; | ||
} | ||
|
||
@Override | ||
public NodeStatus onTick(Tick<Context> tick) { | ||
int remaining = endTime - MatchConstants.clock().getTime(); | ||
if (remaining <= 0) return NodeStatus.Success; | ||
tick.Target.entity.setInvocationDelay(remaining); | ||
return NodeStatus.Running; | ||
} | ||
|
||
@Override | ||
public void onOpen(Tick<Context> tick) { | ||
endTime = MatchConstants.clock().getTime() + delay; | ||
} | ||
} | ||
|
||
public static Node<Context> WaitForNotification(Class<? extends Notification> type, boolean consume) { | ||
return Wait(NotificationCondition(type, consume)); | ||
} | ||
|
||
public static Debug $(String msg, Node<Context> child) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please name this method debug or something like that. I first thought this is a crazy new syntax... |
||
return new Debug(msg, child); | ||
} | ||
|
||
public static Debug $(String msg) { | ||
return new Debug(msg); | ||
} | ||
|
||
public static class Debug extends Decorator<Context> { | ||
private static final boolean DEBUG = false; | ||
private static final long serialVersionUID = 9019598003328102086L; | ||
private final String message; | ||
public Debug(String message) { | ||
super(null); | ||
this.message = message; | ||
} | ||
|
||
public Debug(String message, Node<Context> child) { | ||
super(child); | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public NodeStatus onTick(Tick<Context> tick) { | ||
if (DEBUG) System.out.println(message); | ||
if (child != null) { | ||
NodeStatus result = child.execute(tick); | ||
String res = result == NodeStatus.Success ? "Success" : result == NodeStatus.Failure ? "Failure" : "Running"; | ||
if (DEBUG) System.out.println(message + ": " + res); | ||
return result; | ||
} | ||
return NodeStatus.Success; | ||
} | ||
} | ||
|
||
public static class NotificationCondition extends Condition<Context> { | ||
private static final long serialVersionUID = 1780756145252644771L; | ||
|
||
public NotificationCondition(Class<? extends Notification> type) { | ||
this(type, false); | ||
} | ||
|
||
public NotificationCondition(Class<? extends Notification> type, boolean consume) { | ||
super((context)->{ | ||
Iterator<? extends Notification> it = context.comp.getNotificationsIt(type); | ||
if (it.hasNext()) { | ||
if (consume) context.comp.consumeNotification(it.next()); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
jsettlers.logic/src/main/java/jsettlers/logic/movable/Context.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package jsettlers.logic.movable; | ||
|
||
import jsettlers.logic.movable.components.Component; | ||
|
||
/** | ||
* Created by homoroselaps | ||
*/ | ||
|
||
public final class Context { | ||
public final Entity entity; | ||
public final Component comp; | ||
public Entity getEntity() { return entity; } | ||
public Component getComponent() { return comp; } | ||
public Context(Entity entity, Component component) { | ||
this.entity = entity; | ||
this.comp = component; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have a different name. An Optional is a fixed thing in java8, which makes this confusing.