Skip to content

Commit

Permalink
Add repeat action & tweak untrigger filter (#1411)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 authored Oct 11, 2024
1 parent 24b6769 commit 852bf3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
35 changes: 24 additions & 11 deletions core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import tc.oc.pgm.action.actions.KillEntitiesAction;
import tc.oc.pgm.action.actions.MessageAction;
import tc.oc.pgm.action.actions.PasteStructureAction;
import tc.oc.pgm.action.actions.RepeatAction;
import tc.oc.pgm.action.actions.ReplaceItemAction;
import tc.oc.pgm.action.actions.ScopeSwitchAction;
import tc.oc.pgm.action.actions.SetVariableAction;
Expand Down Expand Up @@ -197,22 +198,23 @@ public <T extends Filterable<?>> Trigger<T> parseTrigger(Element el) throws Inva

// Generic action with N children parser
private <B extends Filterable<?>> ActionNode<? super B> parseAction(
Element el, Class<B> scope, boolean includeObs) throws InvalidXMLException {
Element el, Class<B> scope, boolean obs) throws InvalidXMLException {
scope = parseScope(el, scope);

ImmutableList.Builder<Action<? super B>> builder = ImmutableList.builder();
if (el.getChildren().isEmpty())
throw new InvalidXMLException("No action children were defined", el);

ImmutableList.Builder<Action<? super B>> children = ImmutableList.builder();
for (Element child : el.getChildren()) {
builder.add(parse(child, scope));
children.add(parse(child, scope));
}

Filter filter = wrapFilter(parser.filter(el, "filter").orAllow(), includeObs);
Filter untriggerFilter = wrapFilter(
parser
.filter(el, "untrigger-filter")
.optional(legacy ? StaticFilter.DENY : StaticFilter.ALLOW),
includeObs);
Filter filter = parser.filter(el, "filter").orAllow();
Filter untriggerFilter =
parser.filter(el, "untrigger-filter").result(!legacy && filter == StaticFilter.ALLOW);

return new ActionNode<>(builder.build(), filter, untriggerFilter, scope);
return new ActionNode<>(
children.build(), wrapFilter(filter, obs), wrapFilter(untriggerFilter, obs), scope);
}

// Parsers
Expand All @@ -222,13 +224,24 @@ public <B extends Filterable<?>> ActionNode<? super B> parseAction(Element el, C
return parseAction(el, scope, true);
}

@MethodParser("repeat")
public <B extends Filterable<?>> Action<? super B> parseRepeat(Element el, Class<B> scope)
throws InvalidXMLException {
scope = parseScope(el, scope);

Action<? super B> child = parseAction(el, scope, true);
Formula<B> formula = parser.formula(scope, el, "times").required();

return new RepeatAction<>(scope, child, formula);
}

@MethodParser("switch-scope")
public <O extends Filterable<?>, I extends Filterable<?>> Action<? super O> parseSwitchScope(
Element el, Class<O> outer) throws InvalidXMLException {
outer = parseScope(el, outer, "outer");
Class<I> inner = parseScope(el, null, "inner");

ActionDefinition<? super I> child = parseAction(el, inner, includeObs(el, inner));
Action<? super I> child = parseAction(el, inner, includeObs(el, inner));

Action<? super O> result = ScopeSwitchAction.of(child, outer, inner);
if (result == null) {
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/actions/RepeatAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tc.oc.pgm.action.actions;

import tc.oc.pgm.action.Action;
import tc.oc.pgm.filters.Filterable;
import tc.oc.pgm.util.math.Formula;

public class RepeatAction<B extends Filterable<?>> extends AbstractAction<B> {

protected final Action<? super B> action;
protected final Formula<B> formula;

public RepeatAction(Class<B> scope, Action<? super B> action, Formula<B> formula) {
super(scope);
this.action = action;
this.formula = formula;
}

@Override
public void trigger(B t) {
for (int i = (int) formula.applyAsDouble(t); i > 0; i--) {
action.trigger(t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public Filter orDeny() throws InvalidXMLException {
return optional(StaticFilter.DENY);
}

public Filter result(boolean result) throws InvalidXMLException {
return optional(result ? StaticFilter.ALLOW : StaticFilter.DENY);
}

@Override
protected Filter parse(Node node) throws InvalidXMLException {
if (prop.length == 0) return filters.parse(el);
Expand Down

0 comments on commit 852bf3d

Please sign in to comment.