RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ActionManager.java
1package com.osroyale.game.action;
2
3import com.osroyale.game.action.policy.WalkablePolicy;
4import com.osroyale.game.world.World;
5import com.osroyale.game.world.entity.mob.Mob;
6
7import java.util.ArrayDeque;
8import java.util.Objects;
9import java.util.Queue;
10
48
49* The class which manages {@link Action}s executed by mobs.
50 *
51 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand Up</a>
52 */
53public final class ActionManager {
54
56 private final Queue<Action<?>> queuedActions = new ArrayDeque<>();
57
59 private Action<?> currentAction = null;
60
62 public void sequence() {
63 if (queuedActions.isEmpty() || (currentAction != null && currentAction.isRunning())) {
64 return;
65 }
66
67 execute(this.currentAction = queuedActions.poll(), true);
68 }
69
71 public <A extends Action<?>> void queue(A action) {
72 queuedActions.add(action);
73 }
74
75 public <A extends Action<?>> void execute(A action) {
76 execute(action, true);
77 }
78
80 public <A extends Action<?>> void execute(A action, boolean override) {
81 //if current action is priorized stop.
82 if (currentAction != null && currentAction.isRunning() && currentAction.prioritized()) {
83 return;
84 }
85
86 //if there is already an action active and we don't want to override
87 //we ignore.
88 //if u don't want to override but you also dont want to ignore
89 //queue the action.
90 if (currentAction != null && currentAction.isRunning() && !override) {
91 return;
92 }
93
94 //if there is an action active, make sure we stop it before running
95 //another action.
96 if (currentAction != null && currentAction.isRunning()) {
97 currentAction.cancel();
98 }
99
100 //finally submit the action.
101 World.schedule(currentAction = action);
102 }
103
105 private void cancelQueuedActions() {
106 queuedActions.forEach(Action::cancel);
107 queuedActions.clear();
108 }
109
111 public void cancel() {
112 if (currentAction != null && currentAction.isRunning() && currentAction.prioritized()) {
113 currentAction.cancel();
114 currentAction = null;
115 }
116 }
117
119 public void reset() {
120 cancel();
121 cancelQueuedActions();
122 }
123
125 public void clearNonWalkableActions() {
126 final Action<?> currentAction = this.currentAction;
127 if (currentAction != null) {
128 if (!currentAction.cancellableInProgress()) {
129 return;
130 }
131 if (currentAction.getWalkablePolicy() == WalkablePolicy.NON_WALKABLE) {
132 currentAction.cancel();
133 this.currentAction = null;
134 }
135 }
136
137 for (Action<?> actionEvent : queuedActions) {
138 if (actionEvent.getWalkablePolicy() != WalkablePolicy.WALKABLE) {
139 actionEvent.cancel();
140 queuedActions.remove(actionEvent);
141 }
142 }
143 }
144
146 public void cancel(String name) {
147 if (currentAction != null && Objects.equals(currentAction.getName(), name)) {
148 currentAction.cancel();
149 currentAction = null;
150 }
151
152 for (Action<?> actionEvent : queuedActions) {
153 if (Objects.equals(actionEvent.getName(), name)) {
154 actionEvent.cancel();
155 queuedActions.remove(actionEvent);
156 }
157 }
158 }
159
161 public Action<?> getCurrentAction() {
162 return currentAction;
163 }
164
165 @Override
166 public String toString() {
167 return String.format("ActionManager[size=%s, action=%s]", queuedActions.size(), currentAction);
168 }
169
170
171}