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 25 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
116 changes: 116 additions & 0 deletions
116
jsettlers.logic/src/main/java/jsettlers/logic/movable/BehaviorTreeFactory.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,116 @@ | ||
package jsettlers.logic.movable; | ||
|
||
import java.io.Serializable; | ||
|
||
import jsettlers.common.movable.EMovableAction; | ||
import jsettlers.common.movable.EMovableType; | ||
import jsettlers.logic.movable.components.SteeringComponent; | ||
import jsettlers.logic.movable.simplebehaviortree.Decorator; | ||
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.Guard; | ||
import jsettlers.logic.movable.simplebehaviortree.nodes.Inverter; | ||
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.WaitFor; | ||
|
||
/** | ||
* @author homoroselaps | ||
*/ | ||
|
||
public abstract class BehaviorTreeFactory implements Serializable { | ||
private static final long serialVersionUID = 8396039806339873520L; | ||
|
||
protected static Node<Entity> WaitForTargetReached_FailIfNot() { | ||
// wait for targetReached, but abort if target not reachable | ||
return new Sequence<>( | ||
new Inverter<>(TriggerCondition(SteeringComponent.TargetNotReachedTrigger.class)), | ||
new WaitFor(SteeringComponent.TargetReachedTrigger.class, true) | ||
); | ||
} | ||
|
||
protected static Guard<Entity> TriggerGuard(Class<? extends Notification> type, Node<Entity> child) { | ||
return new Guard<>(entity -> entity.getNotificationsIt(type).hasNext(), true, child); | ||
} | ||
|
||
protected static Action<Entity> StartAnimation(EMovableAction animation, short duration) { | ||
return new Action<>(e -> { | ||
e.aniC().startAnimation(animation, duration); | ||
}); | ||
} | ||
|
||
protected static void convertTo(Entity entity, EMovableType type) { | ||
Entity blueprint = EntityFactory.CreateEntity(entity.gameC().getMovableGrid(), type, entity.movC().getPos(), entity.movC().getPlayer()); | ||
entity.convertTo(blueprint); | ||
} | ||
|
||
protected static <T> Node<T> Optional(Node<T> child) { | ||
return new Selector<T>(child, new Succeeder<T>()); | ||
} | ||
|
||
protected static class Wait extends Node<Entity> { | ||
private static final long serialVersionUID = 8774557186392581042L; | ||
boolean done = false; | ||
int delay = -1; | ||
public Wait(int milliseconds) { | ||
super(); | ||
delay = milliseconds; | ||
} | ||
|
||
@Override | ||
protected NodeStatus onTick(Tick<Entity> tick) { | ||
if (done) return NodeStatus.Success; | ||
tick.Target.setInvocationDelay(delay); | ||
done = true; | ||
return NodeStatus.Running(); | ||
} | ||
|
||
@Override | ||
protected void onOpen(Tick<Entity> tick) { | ||
done = false; | ||
} | ||
} | ||
|
||
protected static Condition<Entity> TriggerCondition(Class<? extends Notification> type) { | ||
return new Condition<>(entity -> entity.getNotificationsIt(type).hasNext()); | ||
} | ||
|
||
protected static Debug $(String message, Node<Entity> child) { | ||
return new Debug(message, child); | ||
} | ||
|
||
protected static Debug $(String message) { | ||
return new Debug(message); | ||
} | ||
|
||
protected static class Debug extends Decorator<Entity> { | ||
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<Entity> child) { | ||
super(child); | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
protected NodeStatus onTick(Tick<Entity> 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; | ||
} | ||
} | ||
} |
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.
The name of the method does not express its behaviour.
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.
Thank you for your annotation. You are completely right, this indicates bad code style.
In this decision I prioritized readability of the whole behavior tree over comprehensibility of this single function. Some behavior tree frameworks ship with a graphical tool, which we don't have. Therefore, I did my best to avoid as much boilerplate code/code repetition as possible, to support behavior design in code.
The function $ is here defined as a shorthand for the creation of a ´Debug´ node, which annotates tree nodes with debug information/comments.
I choose $ as to imitate the jQuery function, which developers might already be familiar with.
I'd fine with any other valid symbol, but would rather not clatter the tree with to many
new Debug(...)
statements.