Skip to content

Commit

Permalink
Merge remote-tracking branch 'burkino/1.19.2' into 1.19.2-feat-where
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/ca/teamdman/sfml/ast/ASTBuilder.java
  • Loading branch information
TeamDman committed Aug 14, 2024
2 parents 515d3c0 + 05ee1a7 commit bd1da1b
Show file tree
Hide file tree
Showing 17 changed files with 427 additions and 169 deletions.
95 changes: 94 additions & 1 deletion src/gametest/java/ca/teamdman/sfm/SFMCorrectnessGameTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3436,4 +3436,97 @@ public static void conditional_output_inspection(GameTestHelper helper) {
assertTrue(rightChest.getStackInSlot(0).getCount() == 32, "Dirt did not arrive");
});
}
}

@GameTest(template = "3x3x1")
public static void input_where(GameTestHelper helper) {
// Only the right chest should move items
BlockPos managerPos = new BlockPos(1, 2, 0);
helper.setBlock(managerPos, SFMBlocks.MANAGER_BLOCK.get());
// Create positions
BlockPos leftPos = new BlockPos(2, 2, 0);
BlockPos rightPos = new BlockPos(0, 2, 0);
BlockPos abovePos = new BlockPos(1, 3, 0);
// Set blocks
helper.setBlock(leftPos, SFMBlocks.TEST_BARREL_BLOCK.get());
helper.setBlock(rightPos, SFMBlocks.TEST_BARREL_BLOCK.get());
helper.setBlock(abovePos, SFMBlocks.TEST_BARREL_BLOCK.get());

var leftChest = getItemHandler(helper, leftPos);
var rightChest = getItemHandler(helper, rightPos);
var topChest = getItemHandler(helper, abovePos);

leftChest.insertItem(0, new ItemStack(Blocks.DIRT, 1), false);

rightChest.insertItem(0, new ItemStack(Blocks.DIRT, 1), false);
rightChest.insertItem(1, new ItemStack(Blocks.STONE, 1), false);

ManagerBlockEntity manager = (ManagerBlockEntity) helper.getBlockEntity(managerPos);
manager.setItem(0, new ItemStack(SFMItems.DISK_ITEM.get()));
manager.setProgram("""
EVERY 20 TICKS DO
INPUT FROM a WHERE > 0 stone
OUTPUT dirt TO b
END
""".stripTrailing().stripIndent());
// set the labels
LabelPositionHolder.empty()
.add("a", helper.absolutePos(leftPos))
.add("a", helper.absolutePos(rightPos))
.add("b", helper.absolutePos(abovePos))
.save(manager.getDisk().get());

succeedIfManagerDidThingWithoutLagging(helper, manager, () -> {
assertTrue(leftChest.getStackInSlot(0).getCount() == 1, "Dirt moved");
assertTrue(rightChest.getStackInSlot(0).getCount() == 0, "Dirt did not move");
assertTrue(topChest.getStackInSlot(0).getCount() == 1, "Dirt did not move");

helper.succeed();
});
}

@GameTest(template = "3x3x1")
public static void output_where(GameTestHelper helper) {
// Only the right chest should get items
BlockPos managerPos = new BlockPos(1, 2, 0);
helper.setBlock(managerPos, SFMBlocks.MANAGER_BLOCK.get());
// Create positions
BlockPos leftPos = new BlockPos(2, 2, 0);
BlockPos rightPos = new BlockPos(0, 2, 0);
BlockPos abovePos = new BlockPos(1, 3, 0);
// Set blocks
helper.setBlock(leftPos, SFMBlocks.TEST_BARREL_BLOCK.get());
helper.setBlock(rightPos, SFMBlocks.TEST_BARREL_BLOCK.get());
helper.setBlock(abovePos, SFMBlocks.TEST_BARREL_BLOCK.get());

var leftChest = getItemHandler(helper, leftPos);
var rightChest = getItemHandler(helper, rightPos);
var topChest = getItemHandler(helper, abovePos);

rightChest.insertItem(1, new ItemStack(Blocks.STONE, 1), false);

topChest.insertItem(0, new ItemStack(Blocks.DIRT, 2), false);

ManagerBlockEntity manager = (ManagerBlockEntity) helper.getBlockEntity(managerPos);
manager.setItem(0, new ItemStack(SFMItems.DISK_ITEM.get()));
manager.setProgram("""
EVERY 20 TICKS DO
INPUT FROM a
OUTPUT RETAIN 1 dirt TO EACH b WHERE > 0 stone
END
""".stripTrailing().stripIndent());
// set the labels
LabelPositionHolder.empty()
.add("b", helper.absolutePos(leftPos))
.add("b", helper.absolutePos(rightPos))
.add("a", helper.absolutePos(abovePos))
.save(manager.getDisk().get());

succeedIfManagerDidThingWithoutLagging(helper, manager, () -> {
assertTrue(leftChest.getStackInSlot(0).getCount() == 0, "Dirt moved");
assertTrue(rightChest.getStackInSlot(0).getCount() == 1, "Dirt did not move");
assertTrue(topChest.getStackInSlot(0).getCount() == 1, "Dirt did not move");

helper.succeed();
});
}
}
Binary file added src/gametest/resources/data/sfm/structures/3x3x1.nbt
Binary file not shown.
30 changes: 19 additions & 11 deletions src/main/antlr/SFML.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ interval: TICK #Tick
//

block : statement* ;
statement : inputstatement #InputStatementStatement
| outputstatement #OutputStatementStatement
| ifstatement #IfStatementStatement
| forgetstatement #ForgetStatementStatement
statement : inputStatement
| outputStatement
| ifStatement
| forgetStatement
;

// IO STATEMENT
forgetstatement : FORGET label? (COMMA label)* COMMA?;
inputstatement : INPUT inputmatchers? resourceexclusion? FROM EACH? labelaccess
forgetStatement : FORGET label? (COMMA label)* COMMA?;
inputStatement : INPUT inputmatchers? resourceexclusion? FROM EACH? labelaccess
| FROM EACH? labelaccess INPUT inputmatchers? resourceexclusion?
;
outputstatement : OUTPUT outputmatchers? resourceexclusion? TO EACH? labelaccess
outputStatement : OUTPUT outputmatchers? resourceexclusion? TO EACH? labelaccess
| TO EACH? labelaccess OUTPUT outputmatchers? resourceexclusion?
;
inputmatchers : movement; // separate for different defaults
Expand Down Expand Up @@ -74,7 +74,7 @@ rangeset : range (COMMA range)*;
range : number (DASH number)? ;


ifstatement : IF boolexpr THEN block (ELSE IF boolexpr THEN block)* (ELSE block)? END;
ifStatement : IF boolexpr THEN block (ELSE IF boolexpr THEN block)* (ELSE block)? END;
boolexpr : TRUE #BooleanTrue
| FALSE #BooleanFalse
| LPAREN boolexpr RPAREN #BooleanParen
Expand Down Expand Up @@ -112,12 +112,20 @@ setOp : OVERALL
//
// IO HELPERS
//
labelaccess : label (COMMA label)* roundrobin? sidequalifier? slotqualifier?;
roundrobin: ROUND ROBIN BY (LABEL | BLOCK);
labelaccess : label (COMMA label)* roundrobin? sidequalifier? slotqualifier? (WHERE where)?;
roundrobin : ROUND ROBIN BY (LABEL | BLOCK);
label : (IDENTIFIER|REDSTONE) #RawLabel
| string #StringLabel
;

where : LPAREN where RPAREN # WhereParen
| NOT where # WhereNegation
| where AND where # WhereConjunction
| where OR where # WhereDisjunction
| resourcecomparison # WhereComparison
;


resourceid : (IDENTIFIER|REDSTONE) (COLON (IDENTIFIER|REDSTONE)? (COLON (IDENTIFIER|REDSTONE)? (COLON (IDENTIFIER|REDSTONE)?)?)?)? # Resource
| string # StringResource
;
Expand Down Expand Up @@ -204,7 +212,7 @@ DO : D O ;
WORLD : W O R L D ;
PROGRAM : P R O G R A M ;
END : E N D ;
NAME : N A M E ;
NAME : N A M E;

// GENERAL SYMBOLS
// used by triggers and as a set operator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private static ChatFormatting getColour(Token token) {
case SFMLLexer.TRUE:
case SFMLLexer.FALSE:
case SFMLLexer.FORGET:
case SFMLLexer.WHERE:
return ChatFormatting.BLUE;
case SFMLLexer.IDENTIFIER:
case SFMLLexer.STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public static <STACK, ITEM, CAP> String buildInspectionResults(
? EnumSet.noneOf(Direction.class)
: EnumSet.of(direction)),
NumberRangeSet.MAX_RANGE,
RoundRobin.disabled()
RoundRobin.disabled(),
WhereStatement.ALWAYS_TRUE
),
new ResourceLimits(
resourceLimitList.stream().distinct().toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,46 @@ public void forEachCapability(
}
}

public void forCapabilityOfBlock(
ProgramContext programContext,
DirectionQualifier directionQualifier,
Pair<Label, BlockPos> labelPosPair,
CapabilityConsumer<CAP> consumer
) {
CableNetwork network = programContext.getNetwork();

var label = labelPosPair.getFirst();
var blockPos = labelPosPair.getSecond();

for (Direction dir : (Iterable<? extends Direction>) directionQualifier.stream()::iterator) {
// Get capability from the network
Optional<CAP> maybeCap = network
.getCapability(CAPABILITY_KIND, blockPos, dir, programContext.getLogger())
.resolve();
if (maybeCap.isPresent()) {
programContext
.getLogger()
.debug(x -> x.accept(Constants.LocalizationKeys.LOG_RESOURCE_TYPE_GET_CAPABILITIES_CAP_PRESENT.get(
displayAsCapabilityClass(),
blockPos,
dir
)));
CAP cap = maybeCap.get();
consumer.accept(label, blockPos, dir, cap);

} else {
// Log error
programContext
.getLogger()
.error(x -> x.accept(Constants.LocalizationKeys.LOG_RESOURCE_TYPE_GET_CAPABILITIES_CAP_NOT_PRESENT.get(
displayAsCapabilityClass(),
blockPos,
dir
)));
}
}
}

public Stream<STACK> getStacksInSlots(
CAP cap,
NumberRangeSet slots
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/ca/teamdman/sfm/common/util/SFMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public static <STACK, ITEM, CAP> Optional<InputStatement> getInputStatementForSl
labelAccess.directions(),
inputStatement.labelAccess()
.slots(),
RoundRobin.disabled()
RoundRobin.disabled(),
labelAccess.where()
), inputStatement.resourceLimits(), inputStatement.each()));
}

Expand All @@ -178,7 +179,8 @@ public static <STACK, ITEM, CAP> InputStatement getInputStatementForStack(
new NumberRangeSet(
new NumberRange[]{new NumberRange(slot, slot)}
),
RoundRobin.disabled()
RoundRobin.disabled(),
WhereStatement.ALWAYS_TRUE
);
Limit limit = new Limit(
new ResourceQuantity(
Expand Down
Loading

0 comments on commit bd1da1b

Please sign in to comment.