Skip to content

Commit

Permalink
Viewable distance check fixed.
Browse files Browse the repository at this point in the history
[FIXED]     BaseModule viewRange check was inverted.
[UPDATED]   Better variable naming on Functions.
  • Loading branch information
lhunath committed Feb 28, 2014
1 parent c971a84 commit ea588ac
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public int get(final ResourceType resourceType) {
public boolean isZero() {
return FluentIterable.from( getResourceQuantities().values() ).filter( new PredicateNN<Integer>() {
@Override
public boolean apply(@Nonnull final Integer input) {
return input > 0;
public boolean apply(@Nonnull final Integer resourceQuantity) {
return resourceQuantity > 0;
}
} ).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public abstract class PathUtils {

@SuppressWarnings("UnusedDeclaration")
private static final Logger logger = Logger.get( PathUtils.class );

/**
Expand All @@ -30,8 +31,10 @@ public static <E> Optional<Path<E>> find(final E root, final PredicateNN<E> foun
final NNFunctionNN<E, Iterable<E>> neighboursFunction) {

// Test the root.
if (foundFunction.apply( root ))
if (foundFunction.apply( root )) {
logger.trc( "found root: %s", root );
return Optional.of( new Path<>( root, 0 ) );
}

// Initialize breath-first.
Set<E> testedNodes = new HashSet<>();
Expand All @@ -50,14 +53,19 @@ public static <E> Optional<Path<E>> find(final E root, final PredicateNN<E> foun
continue;

double neighbourCost = testPath.getCost() + costFunction.apply( new Step<>( testPath.getTarget(), neighbour ) );
if (neighbourCost > maxCost)
if (neighbourCost > maxCost) {
// Stepping to neighbour from here would exceed maximum cost.
logger.trc( "neighbour exceeds maximum cost (%.2f > %.2f): %s", neighbourCost, maxCost, neighbour );
continue;
}

// Did we find the target?
Path<E> neighbourPath = new Path<>( testPath, neighbour, neighbourCost );
if (foundFunction.apply( neighbour ))
if (foundFunction.apply( neighbour )) {
logger.trc( "found neighbour at cost %.2f: %s", neighbourCost, neighbour );
return Optional.of( neighbourPath );
}
logger.trc( "intermediate neighbour at cost %.2f: %s", neighbourCost, neighbour );

// Neighbour is not the target, add it for testing its neighbours later.
testPaths.add( neighbourPath );
Expand Down Expand Up @@ -100,13 +108,16 @@ public static <E> Collection<E> neighbours(final E root, final int radius, final
// Neighbour was already tested.
continue;

double neighbourCost = testPath.getCost() + 1;
if (neighbourCost > radius)
double neighbourDistance = testPath.getCost() + 1;
if (neighbourDistance > radius) {
// Stepping to neighbour from here would exceed maximum cost.
logger.trc( "neighbour exceeds radius (%.2f > %.2f): %s", neighbourDistance, radius, neighbour );
continue;
}

// Add it for testing its neighbours later.
testPaths.add( new Path<>( testPath, neighbour, neighbourCost ) );
logger.trc( "neighbour at distance %.2f: %s", neighbourDistance, neighbour );
testPaths.add( new Path<>( testPath, neighbour, neighbourDistance ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public void evaluate(final Iterator<String> tokens) {
final String levelArgument = ifNotNullElse( weaponIndexOrLevelArgument, location.getLevel().getType().getName() );
Optional<? extends ILevel> level = FluentIterable.from( gameController.get().listLevels() ).firstMatch( new Predicate<ILevel>() {
@Override
public boolean apply(final ILevel input) {
public boolean apply(final ILevel aLevel) {

return input.getType().getName().equalsIgnoreCase( levelArgument );
return aLevel.getType().getName().equalsIgnoreCase( levelArgument );
}
} );
if (!level.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ public void evaluate(final Iterator<String> tokens) {
err( "No such side/level: %s. Valid sides are: %s, valid levels are: %s", side, //
FluentIterable.from( ImmutableList.copyOf( Side.values() ) ).transform( new Function<Side, String>() {
@Override
public String apply(final Side input) {
public String apply(final Side aSide) {

return input.name();
return aSide.name();
}
} ), //
FluentIterable.from( ImmutableList.copyOf( LevelType.values() ) ).transform( new Function<LevelType, String>() {
@Override
public String apply(final LevelType input) {
public String apply(final LevelType levelType) {

return input.name();
return levelType.name();
}
} )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ private void setHomeOffset() {

setOffset( FluentIterable.from( gameObjects ).filter( new PredicateNN<IGameObject>() {
@Override
public boolean apply(@Nonnull final IGameObject input) {
public boolean apply(@Nonnull final IGameObject gameObject) {
// Only game objects in this map's displayed level.
return input.checkLocation().get().getLevel().getType() == getLevelType();
return gameObject.checkLocation().get().getLevel().getType() == getLevelType();
}
} ).transform( new NNFunctionNN<IGameObject, Vec2>() {
@Nonnull
@Override
public Vec2 apply(@Nonnull final IGameObject input) {
public Vec2 apply(@Nonnull final IGameObject gameObject) {
// Transform game objects into their offset from the center of the map.
hasUnits = true;
Box contentBox = getContentBoxOnScreen();
return positionToMapCoordinate( input.checkLocation().get().getPosition() ) //
return positionToMapCoordinate( gameObject.checkLocation().get().getPosition() ) //
.translate( -contentBox.getSize().getWidth() / 2, -contentBox.getSize().getHeight() / 2 );
}
} ).first().or( new Supplier<Vec2>() {
Expand Down
3 changes: 2 additions & 1 deletion omicron-cli/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<configuration scan="true">

<logger name="com.lyndir.omicron" level="TRACE" />
<logger name="com.lyndir" level="INFO" />
<logger name="com.lyndir.omicron" level="DEBUG" />

<root level="INFO" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ else if (observable instanceof IGameObject)
}
}

return Maybool.from(ourLocation.get().getPosition().distanceTo( observableLocation.getPosition() ) > viewRange);
return Maybool.from(ourLocation.get().getPosition().distanceTo( observableLocation.getPosition() ) <= viewRange);
}

throw new AlreadyCheckedException( "Switch statement should handle all cases." );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ private void construct(final ConstructionSite site) {
// Initialize path finding functions.
PredicateNN<GameObject> foundFunction = new PredicateNN<GameObject>() {
@Override
public boolean apply(@Nonnull final GameObject input) {
for (final ContainerModule containerModule : input.getModules( ModuleType.CONTAINER ))
public boolean apply(@Nonnull final GameObject gameObject) {
for (final ContainerModule containerModule : gameObject.getModules( ModuleType.CONTAINER ))
if (resourceCost.get( containerModule.getResourceType() ) > 0 && containerModule.getStock() > 0)
return true;

Expand All @@ -100,7 +100,7 @@ public boolean apply(@Nonnull final GameObject input) {
NNFunctionNN<PathUtils.Step<GameObject>, Double> costFunction = new NNFunctionNN<PathUtils.Step<GameObject>, Double>() {
@Nonnull
@Override
public Double apply(@Nonnull final PathUtils.Step<GameObject> input) {
public Double apply(@Nonnull final PathUtils.Step<GameObject> gameObjectStep) {
return 1d;
}
};
Expand Down Expand Up @@ -309,8 +309,8 @@ protected void onNewTurn() {
// Initialize path finding functions.
PredicateNN<GameObject> foundFunction = new PredicateNN<GameObject>() {
@Override
public boolean apply(@Nonnull final GameObject input) {
for (final ConstructorModule module : input.getModules( ModuleType.CONSTRUCTOR ))
public boolean apply(@Nonnull final GameObject gameObject) {
for (final ConstructorModule module : gameObject.getModules( ModuleType.CONSTRUCTOR ))
if (module.getRemainingSpeed() > 0 && !module.isResourceConstrained()
&& getRemainingWork( module.getBuildsModule() ) > 0)
return true;
Expand All @@ -321,7 +321,7 @@ && getRemainingWork( module.getBuildsModule() ) > 0)
NNFunctionNN<PathUtils.Step<GameObject>, Double> costFunction = new NNFunctionNN<PathUtils.Step<GameObject>, Double>() {
@Nonnull
@Override
public Double apply(@Nonnull final PathUtils.Step<GameObject> input) {
public Double apply(@Nonnull final PathUtils.Step<GameObject> gameObjectStep) {
return 1d;
}
};
Expand All @@ -337,8 +337,8 @@ public Iterable<GameObject> apply(@Nonnull final GameObject neighbourInput) {
.transform( new NFunctionNN<Tile, GameObject>() {
@Nullable
@Override
public GameObject apply(@Nonnull final Tile input) {
Optional<GameObject> contents = input.getContents();
public GameObject apply(@Nonnull final Tile tile) {
Optional<GameObject> contents = tile.getContents();
if (contents.isPresent()) {
for (final ConstructorModule module : contents.get()
.getModules(
Expand Down Expand Up @@ -373,8 +373,8 @@ public GameObject apply(@Nonnull final Tile input) {
synchronized (remainingWork) {
if (FluentIterable.from( remainingWork.values() ).filter( new PredicateNN<Integer>() {
@Override
public boolean apply(@Nonnull final Integer input) {
return input > 0;
public boolean apply(@Nonnull final Integer remainingWork) {
return remainingWork > 0;
}
} ).isEmpty())
// No more work remaining; create the constructed unit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ static List<VictoryConditionType> coreVCT(final List<PublicVictoryConditionType>
return Lists.transform( checkNotNull( object ), new NNFunctionNN<PublicVictoryConditionType, VictoryConditionType>() {
@Nonnull
@Override
public VictoryConditionType apply(@Nonnull final PublicVictoryConditionType input) {
return coreVCT( input );
public VictoryConditionType apply(@Nonnull final PublicVictoryConditionType victoryConditionType) {
return coreVCT( victoryConditionType );
}
} );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ protected void onNewTurn() {
// Initialize path finding functions.
PredicateNN<GameObject> foundFunction = new PredicateNN<GameObject>() {
@Override
public boolean apply(@Nonnull final GameObject input) {
for (final ContainerModule containerModule : input.getModules( ModuleType.CONTAINER ))
public boolean apply(@Nonnull final GameObject gameObject) {
for (final ContainerModule containerModule : gameObject.getModules( ModuleType.CONTAINER ))
if (containerModule.getAvailable() > 0)
return true;

Expand All @@ -84,7 +84,7 @@ public boolean apply(@Nonnull final GameObject input) {
NNFunctionNN<PathUtils.Step<GameObject>, Double> costFunction = new NNFunctionNN<PathUtils.Step<GameObject>, Double>() {
@Nonnull
@Override
public Double apply(@Nonnull final PathUtils.Step<GameObject> input) {
public Double apply(@Nonnull final PathUtils.Step<GameObject> gameObjectStep) {
return 1d;
}
};
Expand All @@ -95,8 +95,8 @@ public Iterable<GameObject> apply(@Nonnull final GameObject neighbour) {
return FluentIterable.from( neighbour.getLocation().get().neighbours() ).transform( new NFunctionNN<Tile, GameObject>() {
@Nullable
@Override
public GameObject apply(@Nonnull final Tile input) {
return input.getContents().orNull();
public GameObject apply(@Nonnull final Tile tile) {
return tile.getContents().orNull();
}
} ).filter( Predicates.notNull() );
}
Expand Down
28 changes: 14 additions & 14 deletions omicron-core/src/main/java/com/lyndir/omicron/api/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ private Game(final Size levelSize, final Iterable<Player> players, final Iterabl
Map<ResourceType, Integer> remainingResources = new EnumMap<>( ResourceType.class );
remainingResources.putAll( FluentIterable.from( levels ).transformAndConcat( new Function<Level, Iterable<ResourceType>>() {
@Override
public Iterable<ResourceType> apply(final Level input) {
return input.getType().getSupportedResources();
public Iterable<ResourceType> apply(final Level level) {
return level.getType().getSupportedResources();
}
} ).toMap( new Function<ResourceType, Integer>() {
@Override
public Integer apply(final ResourceType input) {
return resourceConfig.quantity( input );
public Integer apply(final ResourceType resourceType) {
return resourceConfig.quantity( resourceType );
}
} ) );
while (true) {
Expand All @@ -91,8 +91,8 @@ public Integer apply(final ResourceType input) {
new NNFunctionNN<Tile, Iterable<Tile>>() {
@Nonnull
@Override
public Iterable<Tile> apply(@Nonnull final Tile input) {
return input.neighbours();
public Iterable<Tile> apply(@Nonnull final Tile tile) {
return tile.neighbours();
}
} );

Expand Down Expand Up @@ -135,8 +135,8 @@ public GameController getController() {
public Level getLevel(final LevelType levelType) {
return FluentIterable.from( levels ).firstMatch( new Predicate<Level>() {
@Override
public boolean apply(final Level input) {
return input.getType() == levelType;
public boolean apply(final Level level) {
return level.getType() == levelType;
}
} ).get();
}
Expand Down Expand Up @@ -229,8 +229,8 @@ public Collection<IPlayer> getPlayers() {
public Builder setPlayer(final PlayerKey playerKey, final String name, final Color primaryColor, final Color secondaryColor) {
IPlayer existingPlayer = Iterables.find( players, new PredicateNN<IPlayer>() {
@Override
public boolean apply(@Nonnull final IPlayer input) {
return input.hasKey( playerKey );
public boolean apply(@Nonnull final IPlayer player) {
return player.hasKey( playerKey );
}
}, null );
if (existingPlayer != null)
Expand All @@ -244,8 +244,8 @@ public boolean apply(@Nonnull final IPlayer input) {
public Builder addPlayer(final IPlayer player) {
IPlayer existingPlayer = Iterables.find( players, new PredicateNN<IPlayer>() {
@Override
public boolean apply(@Nonnull final IPlayer input) {
return input.getPlayerID() == player.getPlayerID();
public boolean apply(@Nonnull final IPlayer aPlayer) {
return aPlayer.getPlayerID() == player.getPlayerID();
}
}, null );
Preconditions.checkState( existingPlayer == null, "A player with this player's ID has already been added: %s", existingPlayer );
Expand Down Expand Up @@ -361,8 +361,8 @@ public void addUnits(final IGame game, final IPlayer player) {
GameObject engineer = new GameObject( UnitTypes.ENGINEER, coreGame, corePlayer );
engineer.onModule( ModuleType.CONTAINER, new PredicateNN<ContainerModule>() {
@Override
public boolean apply(@Nonnull final ContainerModule input) {
return input.getResourceType() == ResourceType.METALS;
public boolean apply(@Nonnull final ContainerModule module) {
return module.getResourceType() == ResourceType.METALS;
}
} ).addStock( Integer.MAX_VALUE );
engineerTile.get().setContents( engineer );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ else if (playerCondition.apply( gameListenerOwner ))
GameListener fireIfObservable(@Nonnull final ITile location) {
return fireIfPlayer( new PredicateNN<Player>() {
@Override
public boolean apply(@Nonnull final Player input) {
public boolean apply(@Nonnull final Player player) {
return Security.godRun( new Job<Boolean>() {
@Override
public Boolean execute() {
return input.canObserve( location ).isTrue();
return player.canObserve( location ).isTrue();
}
} );
}
Expand All @@ -260,11 +260,11 @@ public Boolean execute() {
GameListener fireIfObservable(@Nonnull final IGameObject gameObject) {
return fireIfPlayer( new PredicateNN<Player>() {
@Override
public boolean apply(@Nonnull final Player input) {
public boolean apply(@Nonnull final Player player) {
return Security.godRun( new Job<Boolean>() {
@Override
public Boolean execute() {
return input.canObserve( gameObject ).isTrue();
return player.canObserve( gameObject ).isTrue();
}
} );
}
Expand Down
Loading

0 comments on commit ea588ac

Please sign in to comment.