Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

API change to hide more unobservable details from players.

[ADDED]     Observable - all things that can be observed by a unit or player; currently Tile and GameObject.
[UPDATED]   canObserve harmonized to deal with any observable.
[UPDATED]   getOwner -> checkOwner - hide the owner of unobservable tiles/game objects.
[UPDATED]   More centralized observation test logic.
[FIXED]     Some warnings.
  • Loading branch information...
commit c971a841beb15f73b1aef5a6b047d39f2cbb254a 1 parent d79c73f
@lhunath lhunath authored
Showing with 411 additions and 397 deletions.
  1. +9 −2 .idea/inspectionProfiles/Lhunath.xml
  2. +0 −3  omicron-api/src/main/java/com/lyndir/omicron/api/model/Color.java
  3. +1 −2  omicron-api/src/main/java/com/lyndir/omicron/api/model/GameObserver.java
  4. +0 −1  omicron-api/src/main/java/com/lyndir/omicron/api/model/IPlayer.java
  5. +3 −6 omicron-api/src/main/java/com/lyndir/omicron/api/model/IPlayerController.java
  6. +1 −1  omicron-api/src/main/java/com/lyndir/omicron/api/model/ITile.java
  7. +0 −25 omicron-api/src/main/java/com/lyndir/omicron/api/model/ImmutableResourceCost.java
  8. +0 −25 omicron-api/src/main/java/com/lyndir/omicron/api/model/MutableResourceCost.java
  9. +5 −11 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicBaseModule.java
  10. +5 −3 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicConstructorModule.java
  11. +0 −3  omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicGame.java
  12. +13 −13 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicGameObject.java
  13. +5 −14 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicGameObjectController.java
  14. +3 −2 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicModule.java
  15. +6 −12 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicPlayer.java
  16. +7 −14 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicPlayerController.java
  17. +13 −0 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicTile.java
  18. +0 −1  omicron-api/src/main/java/com/lyndir/omicron/api/model/Security.java
  19. +12 −0 omicron-api/src/main/java/com/lyndir/omicron/api/util/Maybe.java
  20. +3 −1 omicron-cli/src/main/java/com/lyndir/lanterna/view/TextView.java
  21. +1 −0  omicron-cli/src/main/java/com/lyndir/lanterna/view/View.java
  22. +0 −1  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/FireCommand.java
  23. +2 −0  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/ListCommand.java
  24. +1 −1  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/PrintCommand.java
  25. +1 −2  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/SetGameCommand.java
  26. +0 −3  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/ViewCommand.java
  27. +3 −1 omicron-cli/src/main/java/com/lyndir/omicron/cli/view/CommandView.java
  28. +3 −3 omicron-cli/src/main/java/com/lyndir/omicron/cli/view/MapView.java
  29. +0 −1  omicron-cli/src/main/java/com/lyndir/omicron/cli/view/OmicronView.java
  30. +37 −30 omicron-core/src/main/java/com/lyndir/omicron/api/model/BaseModule.java
  31. +2 −2 omicron-core/src/main/java/com/lyndir/omicron/api/model/ContainerModule.java
  32. +18 −3 omicron-core/src/main/java/com/lyndir/omicron/api/model/CoreUtils.java
  33. +12 −5 omicron-core/src/main/java/com/lyndir/omicron/api/model/ExtractorModule.java
  34. +3 −3 omicron-core/src/main/java/com/lyndir/omicron/api/model/Game.java
  35. +3 −3 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameController.java
  36. +24 −19 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameObject.java
  37. +30 −34 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameObjectController.java
  38. +70 −48 omicron-core/src/main/java/com/lyndir/omicron/api/model/MobilityModule.java
  39. +3 −4 omicron-core/src/main/java/com/lyndir/omicron/api/model/Module.java
  40. +2 −2 omicron-core/src/main/java/com/lyndir/omicron/api/model/Player.java
  41. +29 −28 omicron-core/src/main/java/com/lyndir/omicron/api/model/PlayerController.java
  42. +20 −1 omicron-core/src/main/java/com/lyndir/omicron/api/model/Tile.java
  43. +2 −2 omicron-core/src/main/java/com/lyndir/omicron/api/model/WeaponModule.java
  44. +2 −2 omicron-core/src/test/java/com/lyndir/omicron/api/model/AbstractTest.java
  45. +25 −25 omicron-core/src/test/java/com/lyndir/omicron/api/model/ConstructorModuleTest.java
  46. +7 −7 omicron-core/src/test/java/com/lyndir/omicron/api/model/ExtractorModuleTest.java
  47. +15 −15 omicron-core/src/test/java/com/lyndir/omicron/api/model/MobilityModuleTest.java
  48. +0 −3  omicron-web/src/main/java/com/lyndir/omicron/webapp/data/service/ServiceModule.java
  49. +4 −3 omicron-web/src/main/java/com/lyndir/omicron/webapp/data/service/StateManager.java
  50. +0 −3  omicron-web/src/main/java/com/lyndir/omicron/webapp/listener/GuiceContext.java
  51. +6 −4 omicron-web/src/main/java/com/lyndir/omicron/webapp/resource/GameResource.java
View
11 .idea/inspectionProfiles/Lhunath.xml
@@ -60,6 +60,14 @@
</option>
</inspection_tool>
<inspection_tool class="BadExceptionDeclared" enabled="true" level="WARNING" enabled_by_default="true">
+ <scope name="Tests" level="WARNING" enabled="false">
+ <option name="exceptionsString" value="java.lang.Throwable,java.lang.Exception,java.lang.Error,java.lang.RuntimeException,java.lang.ClassCastException,java.lang.ArrayIndexOutOfBoundsException" />
+ <option name="exceptions">
+ <value />
+ </option>
+ <option name="ignoreTestCases" value="true" />
+ <option name="ignoreLibraryOverrides" value="false" />
+ </scope>
<option name="exceptionsString" value="java.lang.Throwable,java.lang.Exception,java.lang.Error,java.lang.RuntimeException,java.lang.ClassCastException,java.lang.ArrayIndexOutOfBoundsException" />
<option name="exceptions">
<value />
@@ -477,7 +485,6 @@
</inspection_tool>
<inspection_tool class="NegatedConditionalExpression" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="NegatedEqualityExpression" enabled="true" level="WARNING" enabled_by_default="true" />
- <inspection_tool class="NestedSwitchStatement" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="NestedSynchronizedStatement" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="NewExceptionWithoutArguments" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreWithoutParameters" value="true" />
@@ -541,7 +548,7 @@
<inspection_tool class="OverriddenMethodCallDuringObjectConstruction" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="PackageDotHtmlMayBePackageInfo" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="PackageInfoWithoutPackage" enabled="true" level="WARNING" enabled_by_default="true" />
- <inspection_tool class="ParameterHidingMemberVariable" enabled="true" level="WARNING" enabled_by_default="true">
+ <inspection_tool class="ParameterHidingMemberVariable" enabled="true" level="BUGGY WARNING" enabled_by_default="true">
<option name="m_ignoreInvisibleFields" value="true" />
<option name="m_ignoreStaticMethodParametersHidingInstanceFields" value="true" />
<option name="m_ignoreForConstructors" value="true" />
View
3  omicron-api/src/main/java/com/lyndir/omicron/api/model/Color.java
@@ -3,10 +3,7 @@
import com.google.common.base.Splitter;
import com.lyndir.lhunath.opal.system.util.MetaObject;
import java.io.Serializable;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
import java.util.*;
-import java.util.regex.Pattern;
import javax.annotation.Nullable;
View
3  omicron-api/src/main/java/com/lyndir/omicron/api/model/GameObserver.java
@@ -1,6 +1,5 @@
package com.lyndir.omicron.api.model;
-import com.google.common.base.Optional;
import com.lyndir.omicron.api.Authenticated;
import com.lyndir.omicron.api.model.*;
import com.lyndir.omicron.api.util.Maybool;
@@ -32,6 +31,6 @@ Maybool canObserve(@Nonnull GameObservable observable)
*/
@Nonnull
@Authenticated
- Iterable<? extends ITile> listObservableTiles()
+ Iterable<? extends ITile> iterateObservableTiles()
throws Security.NotAuthenticatedException, Security.NotObservableException;
}
View
1  omicron-api/src/main/java/com/lyndir/omicron/api/model/IPlayer.java
@@ -1,6 +1,5 @@
package com.lyndir.omicron.api.model;
-import com.lyndir.omicron.api.util.Maybool;
import javax.annotation.Nonnull;
View
9 omicron-api/src/main/java/com/lyndir/omicron/api/model/IPlayerController.java
@@ -2,7 +2,6 @@
import static com.lyndir.omicron.api.model.Security.*;
-import com.google.common.collect.*;
import com.lyndir.omicron.api.util.Maybe;
@@ -13,13 +12,11 @@
IGameController getGameController();
/**
- * List the objects of this player.
+ * List the objects of this player observable by the current player.
*
- * NOTE: The controller must be of the currently authenticated player.
- *
- * @return A list of game objects owned by this controller's player.
+ * @return A list of game objects owned by this controller's player filtered by visibility by the current player.
*/
- ImmutableSet<? extends IGameObject> listObjects()
+ Iterable<? extends IGameObject> iterateObservableObjects()
throws NotAuthenticatedException;
/**
View
2  omicron-api/src/main/java/com/lyndir/omicron/api/model/ITile.java
@@ -13,7 +13,7 @@
*
* @author lhunath
*/
-public interface ITile {
+public interface ITile extends GameObservable {
@Nonnull
Maybe<? extends IGameObject> checkContents()
View
25 omicron-api/src/main/java/com/lyndir/omicron/api/model/ImmutableResourceCost.java
@@ -41,11 +41,6 @@ protected ImmutableResourceCost(final Map<ResourceType, Integer> resourceQuantit
return resourceQuantities;
}
- /**
- * {@inheritDoc}
- *
- * @return A new immutable resource cost.
- */
@Override
public ImmutableResourceCost reduce(final ResourceType resourceType, final int term) {
Preconditions.checkArgument( term >= 0, "Amount to reduce resource cost with must be positive." );
@@ -55,11 +50,6 @@ public ImmutableResourceCost reduce(final ResourceType resourceType, final int t
.build() );
}
- /**
- * {@inheritDoc}
- *
- * @return A new immutable resource cost.
- */
@Override
public ImmutableResourceCost add(final ResourceType resourceType, final int term) {
Preconditions.checkArgument( term >= 0, "Amount to increase resource cost with must be positive." );
@@ -69,11 +59,6 @@ public ImmutableResourceCost add(final ResourceType resourceType, final int term
.build() );
}
- /**
- * {@inheritDoc}
- *
- * @return A new immutable resource cost.
- */
@Override
public ImmutableResourceCost add(final ResourceCost resourceCost) {
ImmutableMap.Builder<ResourceType, Integer> builder = ImmutableMap.builder();
@@ -83,11 +68,6 @@ public ImmutableResourceCost add(final ResourceCost resourceCost) {
return new ImmutableResourceCost( builder.build() );
}
- /**
- * {@inheritDoc}
- *
- * @return A new immutable resource cost.
- */
@Override
public ImmutableResourceCost multiply(final ResourceType resourceType, final int factor) {
return new ImmutableResourceCost( ImmutableMap.<ResourceType, Integer>builder()
@@ -96,11 +76,6 @@ public ImmutableResourceCost multiply(final ResourceType resourceType, final int
.build() );
}
- /**
- * {@inheritDoc}
- *
- * @return A new immutable resource cost.
- */
@Override
public ImmutableResourceCost multiply(final int factor) {
ImmutableMap.Builder<ResourceType, Integer> builder = ImmutableMap.builder();
View
25 omicron-api/src/main/java/com/lyndir/omicron/api/model/MutableResourceCost.java
@@ -42,11 +42,6 @@ protected MutableResourceCost(final Map<ResourceType, Integer> resourceQuantitie
return ImmutableMap.copyOf( resourceQuantities );
}
- /**
- * {@inheritDoc}
- *
- * @return This resource cost after applying the mutation.
- */
@Override
public MutableResourceCost reduce(final ResourceType resourceType, final int term) {
Preconditions.checkArgument( term >= 0, "Amount to reduce resource cost with must be positive." );
@@ -55,11 +50,6 @@ public MutableResourceCost reduce(final ResourceType resourceType, final int ter
return this;
}
- /**
- * {@inheritDoc}
- *
- * @return This resource cost after applying the mutation.
- */
@Override
public MutableResourceCost add(final ResourceType resourceType, final int term) {
Preconditions.checkArgument( term >= 0, "Amount to increase resource cost with must be positive." );
@@ -68,11 +58,6 @@ public MutableResourceCost add(final ResourceType resourceType, final int term)
return this;
}
- /**
- * {@inheritDoc}
- *
- * @return This resource cost after applying the mutation.
- */
@Override
public MutableResourceCost add(final ResourceCost resourceCost) {
for (final ResourceType resourceType : ResourceType.values())
@@ -81,11 +66,6 @@ public MutableResourceCost add(final ResourceCost resourceCost) {
return this;
}
- /**
- * {@inheritDoc}
- *
- * @return This resource cost after applying the mutation.
- */
@Override
public MutableResourceCost multiply(final ResourceType resourceType, final int factor) {
resourceQuantities.put( resourceType, get( resourceType ) * factor );
@@ -93,11 +73,6 @@ public MutableResourceCost multiply(final ResourceType resourceType, final int f
return this;
}
- /**
- * {@inheritDoc}
- *
- * @return This resource cost after applying the mutation.
- */
@Override
public MutableResourceCost multiply(final int factor) {
for (final ResourceType resourceType : ResourceType.values())
View
16 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicBaseModule.java
@@ -1,6 +1,5 @@
package com.lyndir.omicron.api.model;
-import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.lyndir.omicron.api.Authenticated;
import com.lyndir.omicron.api.util.Maybool;
@@ -19,21 +18,16 @@
@Override
@Authenticated
- public Maybool canObserve(@Nonnull final ITile location) {
- return core.canObserve( location );
+ public Maybool canObserve(@Nonnull final GameObservable observable)
+ throws Security.NotAuthenticatedException {
+ return core.canObserve( observable );
}
@Nonnull
@Override
@Authenticated
- public Iterable<? extends ITile> listObservableTiles() {
- return core.listObservableTiles();
- }
-
- @Nonnull
- @Override
- public Optional<? extends IPlayer> getOwner() {
- return core.getOwner();
+ public Iterable<? extends ITile> iterateObservableTiles() {
+ return core.iterateObservableTiles();
}
@Override
View
8 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicConstructorModule.java
@@ -19,7 +19,9 @@
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.lyndir.omicron.api.Authenticated;
-import com.lyndir.omicron.api.model.error.OmicronException;
+import com.lyndir.omicron.api.model.IConstructorModule.InaccessibleException;
+import com.lyndir.omicron.api.model.IConstructorModule.IncompatibleLevelException;
+import com.lyndir.omicron.api.model.IConstructorModule.OutOfRangeException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.annotation.Nonnull;
@@ -106,8 +108,8 @@ public void setTarget(final IGameObject target)
@Authenticated
public IConstructionSite schedule(final IUnitType unitType, final ITile location)
throws Security.NotOwnedException, Security.NotAuthenticatedException, Security.NotObservableException,
- IConstructorModule.OutOfRangeException, IConstructorModule.InaccessibleException,
- IConstructorModule.IncompatibleLevelException {
+ OutOfRangeException, InaccessibleException,
+ IncompatibleLevelException {
assertOwned();
Security.assertObservable( location );
View
3  omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicGame.java
@@ -2,12 +2,9 @@
import com.google.common.collect.ImmutableList;
import com.lyndir.lhunath.opal.math.Size;
-import com.lyndir.lhunath.opal.system.error.InternalInconsistencyException;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.MetaObject;
-import com.lyndir.lhunath.opal.system.util.TypeUtils;
import com.lyndir.omicron.api.GameListener;
-import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
View
26 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicGameObject.java
@@ -21,8 +21,8 @@
*/
public class PublicGameObject extends MetaObject implements IGameObject {
- @ObjectMeta(ignoreFor = ObjectMeta.For.all)
- final Logger logger = Logger.get( getClass() );
+ @SuppressWarnings("UnusedDeclaration")
+ private static final Logger logger = Logger.get( PublicGameObject.class );
private final IGameObject core;
@@ -49,12 +49,6 @@ public boolean equals(@Nullable final Object obj) {
return core.getController();
}
- @Nonnull
- @Override
- public Optional<? extends IPlayer> checkOwner() {
- return core.checkOwner();
- }
-
@Override
@Authenticated
public boolean isOwnedByCurrentPlayer()
@@ -62,18 +56,24 @@ public boolean isOwnedByCurrentPlayer()
return core.isOwnedByCurrentPlayer();
}
+ @Override
@Authenticated
+ public Maybe<? extends IPlayer> checkOwner() {
+ return core.checkOwner();
+ }
+
@Override
- @SuppressWarnings("ParameterHidesMemberVariable")
- public Maybool canObserve(@Nonnull final ITile location) {
- return core.canObserve( location );
+ @Authenticated
+ public Maybool canObserve(@Nonnull final GameObservable observable)
+ throws Security.NotAuthenticatedException {
+ return core.canObserve( observable );
}
@Authenticated
@Nonnull
@Override
- public Iterable<? extends ITile> listObservableTiles() {
- return core.listObservableTiles();
+ public Iterable<? extends ITile> iterateObservableTiles() {
+ return core.iterateObservableTiles();
}
@Override
View
19 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicGameObjectController.java
@@ -1,6 +1,5 @@
package com.lyndir.omicron.api.model;
-import com.google.common.base.Optional;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.*;
import com.lyndir.omicron.api.Authenticated;
@@ -11,9 +10,6 @@
public class PublicGameObjectController<O extends IGameObject> extends MetaObject implements IGameObjectController<O> {
- @ObjectMeta(ignoreFor = ObjectMeta.For.all)
- protected final Logger logger = Logger.get( getClass() );
-
private final IGameObjectController<O> core;
PublicGameObjectController(final IGameObjectController<O> core) {
@@ -38,22 +34,17 @@ public O getGameObject() {
return core.getGameObject();
}
- @Nonnull
- @Override
- public Optional<? extends IPlayer> getOwner() {
- return core.getOwner();
- }
-
@Override
@Authenticated
- public Maybool canObserve(@Nonnull final ITile location) {
- return core.canObserve( location );
+ public Maybool canObserve(@Nonnull final GameObservable observable)
+ throws Security.NotAuthenticatedException {
+ return core.canObserve( observable );
}
@Nonnull
@Override
@Authenticated
- public Iterable<? extends ITile> listObservableTiles() {
- return core.listObservableTiles();
+ public Iterable<? extends ITile> iterateObservableTiles() {
+ return core.iterateObservableTiles();
}
}
View
5 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicModule.java
@@ -25,8 +25,9 @@
public abstract class PublicModule extends MetaObject implements IModule {
- @ObjectMeta(ignoreFor = ObjectMeta.For.all)
- protected final Logger logger = Logger.get( getClass() );
+ @SuppressWarnings("UnusedDeclaration")
+ private static final Logger logger = Logger.get( PublicModule.class );
+
private final IModule coreModule;
protected PublicModule(final IModule coreModule) {
View
18 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicPlayer.java
@@ -1,6 +1,5 @@
package com.lyndir.omicron.api.model;
-import com.google.common.base.Optional;
import com.lyndir.lhunath.opal.system.util.MetaObject;
import com.lyndir.omicron.api.Authenticated;
import com.lyndir.omicron.api.util.Maybool;
@@ -41,22 +40,17 @@ public IPlayerController getController() {
}
@Authenticated
- @Override
- public Maybool canObserve(@Nonnull final ITile location) {
- return core.canObserve( location );
- }
-
- @Authenticated
@Nonnull
@Override
- public Iterable<? extends ITile> listObservableTiles() {
- return core.listObservableTiles();
+ public Iterable<? extends ITile> iterateObservableTiles() {
+ return core.iterateObservableTiles();
}
- @Nonnull
@Override
- public Optional<? extends IPlayer> getOwner() {
- return core.getOwner();
+ @Authenticated
+ public Maybool canObserve(@Nonnull final GameObservable observable)
+ throws Security.NotAuthenticatedException {
+ return core.canObserve( observable );
}
@Override
View
21 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicPlayerController.java
@@ -1,7 +1,5 @@
package com.lyndir.omicron.api.model;
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableCollection;
import com.lyndir.omicron.api.Authenticated;
import com.lyndir.omicron.api.util.Maybe;
import com.lyndir.omicron.api.util.Maybool;
@@ -34,12 +32,6 @@ public IPlayer getPlayer() {
return core.getPlayer();
}
- @Nonnull
- @Override
- public Optional<? extends IPlayer> getOwner() {
- return core.getOwner();
- }
-
@Override
public IGameController getGameController() {
return core.getGameController();
@@ -47,15 +39,16 @@ public IGameController getGameController() {
@Override
@Authenticated
- public Maybool canObserve(@Nonnull final ITile location) {
- return core.canObserve( location );
+ public Maybool canObserve(@Nonnull final GameObservable observable)
+ throws Security.NotAuthenticatedException {
+ return core.canObserve( observable );
}
@Nonnull
@Override
@Authenticated
- public Iterable<? extends ITile> listObservableTiles() {
- return core.listObservableTiles();
+ public Iterable<? extends ITile> iterateObservableTiles() {
+ return core.iterateObservableTiles();
}
/**
@@ -67,9 +60,9 @@ public Maybool canObserve(@Nonnull final ITile location) {
*/
@Override
@Authenticated
- public ImmutableCollection<? extends IGameObject> listObjects()
+ public Iterable<? extends IGameObject> iterateObservableObjects()
throws Security.NotAuthenticatedException {
- return core.listObjects();
+ return core.iterateObservableObjects();
}
/**
View
13 omicron-api/src/main/java/com/lyndir/omicron/api/model/PublicTile.java
@@ -94,4 +94,17 @@ public boolean checkAccessible()
throws Security.NotAuthenticatedException {
return core.checkAccessible();
}
+
+ @Override
+ @Authenticated
+ public Maybe<? extends IPlayer> checkOwner() {
+ return core.checkOwner();
+ }
+
+ @Override
+ @Authenticated
+ public Maybe<? extends ITile> checkLocation()
+ throws Security.NotAuthenticatedException {
+ return core.checkLocation();
+ }
}
View
1  omicron-api/src/main/java/com/lyndir/omicron/api/model/Security.java
@@ -19,7 +19,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.lyndir.omicron.api.model.error.ExceptionUtils.*;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.lyndir.lhunath.opal.system.util.Job;
import com.lyndir.lhunath.opal.system.util.ObjectUtils;
View
12 omicron-api/src/main/java/com/lyndir/omicron/api/util/Maybe.java
@@ -59,8 +59,17 @@
public abstract Presence presence();
+ @Nonnull
public abstract T get();
+ @Nullable
+ public T orNull() {
+ if (presence() == Presence.PRESENT)
+ return get();
+
+ return null;
+ }
+
public abstract String toString();
public enum Presence {
@@ -85,6 +94,7 @@ public Presence presence() {
return Presence.ABSENT;
}
+ @Nonnull
@Override
public T get() {
throw new IllegalStateException( "Cannot get() an absent reference." );
@@ -114,6 +124,7 @@ public Presence presence() {
return Presence.UNKNOWN;
}
+ @Nonnull
@Override
public T get() {
throw new IllegalStateException( "Cannot get() an unknown reference." );
@@ -149,6 +160,7 @@ public Presence presence() {
return Presence.PRESENT;
}
+ @Nonnull
@Override
public T get() {
return reference;
View
4 omicron-cli/src/main/java/com/lyndir/lanterna/view/TextView.java
@@ -16,6 +16,8 @@
package com.lyndir.lanterna.view;
+import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
+
import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
@@ -69,7 +71,7 @@ protected void drawForeground(final Screen screen) {
}
private Iterable<String> getTextLines() {
- return Splitter.on( '\n' ).split( getText() );
+ return Splitter.on( strf( "%n" ) ).split( getText() );
}
public Terminal.Color getTextColor() {
View
1  omicron-cli/src/main/java/com/lyndir/lanterna/view/View.java
@@ -35,6 +35,7 @@
*/
public class View {
+ @SuppressWarnings("NonConstantLogger")
protected final Logger logger = Logger.get( getClass() );
private final List<View> children = new LinkedList<>();
View
1  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/FireCommand.java
@@ -6,7 +6,6 @@
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterators;
import com.lyndir.lhunath.opal.math.Vec2;
-import com.lyndir.lhunath.opal.math.Vec2;
import com.lyndir.lhunath.opal.system.util.ConversionUtils;
import com.lyndir.omicron.api.model.*;
import com.lyndir.omicron.api.util.Maybe;
View
2  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/ListCommand.java
@@ -1,5 +1,7 @@
package com.lyndir.omicron.cli.command;
+import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse;
+
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.lyndir.omicron.api.model.*;
View
2  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/PrintCommand.java
@@ -54,7 +54,7 @@ public void field(final Iterator<String> tokens) {
grid.put( y, x, new StringBuilder( " " ) );
// Iterate observable tiles and populate the grid.
- for (final ITile tile : localPlayer.listObservableTiles()) {
+ for (final ITile tile : localPlayer.iterateObservableTiles()) {
Maybe<? extends IGameObject> contents = tile.checkContents();
char contentsChar;
if (contents.presence() == Maybe.Presence.PRESENT)
View
3  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/SetGameCommand.java
@@ -52,8 +52,7 @@ public void worldSize(final Iterator<String> tokens) {
@SubCommand( abbr = "p", desc = "The players that will compete in this game.")
public void players(final Iterator<String> tokens) {
- String gameSettingValue = Iterators.getOnlyElement( tokens, null );
- if (gameSettingValue != null) {
+ if (tokens.hasNext()) {
err( "players cannot be set this way. Use 'add' and 'rm' instead." );
return;
}
View
3  omicron-cli/src/main/java/com/lyndir/omicron/cli/command/ViewCommand.java
@@ -1,8 +1,5 @@
package com.lyndir.omicron.cli.command;
-import com.lyndir.lanterna.view.Rectangle;
-import com.lyndir.lhunath.opal.math.Size;
-import com.lyndir.lhunath.opal.math.Vec2;
import com.lyndir.omicron.cli.OmicronCLI;
import com.lyndir.omicron.cli.view.MapView;
import java.util.*;
View
4 omicron-cli/src/main/java/com/lyndir/omicron/cli/view/CommandView.java
@@ -16,6 +16,8 @@
package com.lyndir.omicron.cli.view;
+import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
+
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.lyndir.lanterna.view.*;
@@ -35,7 +37,7 @@ public CommandView() {
TextView logView = new TextView() {
@Override
public String getText() {
- return Joiner.on( '\n' ).join( OmicronCLI.get().getLog() );
+ return Joiner.on( strf("%n") ).join( OmicronCLI.get().getLog() );
}
};
CommandInputView commandInputView = new CommandInputView();
View
6 omicron-cli/src/main/java/com/lyndir/omicron/cli/view/MapView.java
@@ -93,7 +93,7 @@ protected void drawForeground(final Screen screen) {
Table<Integer, Integer, ITile> grid = HashBasedTable.create( levelSize.getHeight(), levelSize.getWidth() );
// Iterate observable tiles and populate the grid.
- for (final ITile tile : localPlayer.listObservableTiles()) {
+ for (final ITile tile : localPlayer.iterateObservableTiles()) {
Vec2 coordinate = positionToMapCoordinate( tile.getPosition() );
grid.put( coordinate.getY(), coordinate.getX(), tile );
}
@@ -182,9 +182,9 @@ protected boolean onKey(final Key key) {
private void setHomeOffset() {
Optional<IPlayer> localPlayerOptional = OmicronCLI.get().getLocalPlayer();
- ImmutableCollection<? extends IGameObject> gameObjects = ImmutableSet.of();
+ Iterable<? extends IGameObject> gameObjects = ImmutableSet.of();
if (localPlayerOptional.isPresent())
- gameObjects = localPlayerOptional.get().getController().listObjects();
+ gameObjects = localPlayerOptional.get().getController().iterateObservableObjects();
setOffset( FluentIterable.from( gameObjects ).filter( new PredicateNN<IGameObject>() {
@Override
View
1  omicron-cli/src/main/java/com/lyndir/omicron/cli/view/OmicronView.java
@@ -17,7 +17,6 @@
package com.lyndir.omicron.cli.view;
import com.lyndir.lanterna.view.LinearView;
-import com.lyndir.omicron.cli.OmicronCLI;
import com.lyndir.omicron.cli.view.CommandView;
import com.lyndir.omicron.cli.view.DashboardView;
View
67 omicron-core/src/main/java/com/lyndir/omicron/api/model/BaseModule.java
@@ -1,9 +1,12 @@
package com.lyndir.omicron.api.model;
-import com.google.common.base.Optional;
+import static com.lyndir.omicron.api.model.CoreUtils.*;
+import static com.lyndir.omicron.api.model.Security.currentPlayer;
+
import com.google.common.base.Predicate;
import com.google.common.collect.*;
import com.lyndir.lhunath.opal.system.error.AlreadyCheckedException;
+import com.lyndir.lhunath.opal.system.error.InternalInconsistencyException;
import com.lyndir.lhunath.opal.system.util.ObjectUtils;
import com.lyndir.omicron.api.Authenticated;
import com.lyndir.omicron.api.ChangeInt;
@@ -42,9 +45,16 @@ static Builder0 createWithExtraResourceCost(final ImmutableResourceCost resource
return new Builder0( ModuleType.BASE.getStandardCost().add( resourceCost ) );
}
+ /**
+ * @see Player#canObserve(GameObservable)
+ * @see GameObservable#checkLocation()
+ */
@Override
- public Maybool canObserve(@Nonnull final ITile location)
+ public Maybool canObserve(@Nonnull final GameObservable observable)
throws NotAuthenticatedException {
+ if (ObjectUtils.isEqual( getGameObject(), observable ))
+ return Maybool.YES;
+
Maybe<Tile> ourLocation = getGameObject().checkLocation();
switch (ourLocation.presence()) {
case ABSENT:
@@ -52,26 +62,29 @@ public Maybool canObserve(@Nonnull final ITile location)
case UNKNOWN:
return Maybool.UNKNOWN;
case PRESENT:
- return Maybool.from(ourLocation.get().getPosition().distanceTo( location.getPosition() ) > viewRange);
- }
-
- throw new AlreadyCheckedException( "Switch statement should handle all cases." );
- }
-
- @Override
- public Maybool canObserve(@Nonnull final IGameObject gameObject)
- throws NotAuthenticatedException {
- if (ObjectUtils.isEqual( getGameObject(), gameObject ))
- return Maybool.YES;
- Maybe<? extends ITile> location = gameObject.checkLocation();
- switch (location.presence()) {
+ ITile observableLocation = null;
+ if (getGameObject().isOwnedByCurrentPlayer()) {
+ // FIXME: HACKS! Get rid of the instanceof.
+ if (observable instanceof ITile)
+ observableLocation = (ITile) observable;
+ else if (observable instanceof IGameObject)
+ observableLocation = coreGO( (IGameObject) observable ).getLocation().get();
+ else
+ throw new InternalInconsistencyException( "This hack failed. We seem to have an unexpected kind of observable." );
+ }
+ else {
+ Maybe <? extends ITile> location = observable.checkLocation();
+ switch (location.presence()) {
+ case ABSENT:
+ return Maybool.NO;
+ case UNKNOWN:
+ return Maybool.UNKNOWN;
+ case PRESENT:
+ observableLocation = location.get();
+ }
+ }
- case ABSENT:
- return Maybool.NO;
- case UNKNOWN:
- return Maybool.UNKNOWN;
- case PRESENT:
- return canObserve( location.get() );
+ return Maybool.from(ourLocation.get().getPosition().distanceTo( observableLocation.getPosition() ) > viewRange);
}
throw new AlreadyCheckedException( "Switch statement should handle all cases." );
@@ -80,7 +93,7 @@ public Maybool canObserve(@Nonnull final IGameObject gameObject)
@Nonnull
@Override
@Authenticated
- public Iterable<Tile> listObservableTiles()
+ public Iterable<Tile> iterateObservableTiles()
throws NotAuthenticatedException {
Maybe<Tile> location = getGameObject().checkLocation();
if (location.presence() != Maybe.Presence.PRESENT)
@@ -88,18 +101,12 @@ public Maybool canObserve(@Nonnull final IGameObject gameObject)
return FluentIterable.from( location.get().getLevel().getTiles().values() ).filter( new Predicate<Tile>() {
@Override
- public boolean apply(final Tile input) {
- return canObserve( input ).isTrue();
+ public boolean apply(final Tile tile) {
+ return canObserve( tile ).isTrue();
}
} );
}
- @Nonnull
- @Override
- public Optional<Player> getOwner() {
- return getGameObject().checkOwner();
- }
-
@Override
public int getMaxHealth() {
return maxHealth;
View
4 omicron-core/src/main/java/com/lyndir/omicron/api/model/ContainerModule.java
@@ -68,7 +68,7 @@ int addStock(final int amount) {
int stocked = newStock - stock;
stock = newStock;
- getGameObject().getGame().getController().fireIfObservable( getGameObject().getLocation() ) //
+ getGameObject().getGame().getController().fireIfObservable( getGameObject() ) //
.onContainerStockChanged( this, stockChange.to( stock ) );
return stocked;
@@ -90,7 +90,7 @@ int depleteStock(final int amount) {
int depleted = stock - newStock;
stock = newStock;
- getGameObject().getGame().getController().fireIfObservable( getGameObject().getLocation() ) //
+ getGameObject().getGame().getController().fireIfObservable( getGameObject() ) //
.onContainerStockChanged( this, stockChange.to( stock ) );
return depleted;
View
21 omicron-core/src/main/java/com/lyndir/omicron/api/model/CoreUtils.java
@@ -2,7 +2,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
-import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.lyndir.lhunath.opal.system.error.InternalInconsistencyException;
import com.lyndir.lhunath.opal.system.util.NNFunctionNN;
@@ -14,48 +13,62 @@
/**
* @author lhunath, 2013-08-24
*/
-@Nonnull
public abstract class CoreUtils {
+ @Nonnull
@SuppressWarnings("unchecked")
static GameObject coreGO(final IGameObject object) {
return (GameObject) checkNotNull( object );
}
+ @Nullable
@SuppressWarnings("unchecked")
- @Nullable static GameObject coreGON(@Nullable final IGameObject object) {
+ static GameObject coreGON(@Nullable final IGameObject object) {
return (GameObject) object;
}
+ @Nonnull
@SuppressWarnings("unchecked")
static Game coreG(final IGame object) {
return (Game) checkNotNull( object );
}
+ @Nonnull
@SuppressWarnings("unchecked")
static Player coreP(final IPlayer object) {
return (Player) checkNotNull( object );
}
+ @Nullable
+ @SuppressWarnings("unchecked")
+ static Player corePN(@Nullable final IPlayer object) {
+ return (Player) object;
+ }
+
+ @Nonnull
@SuppressWarnings("unchecked")
static List<Player> coreP(final List<? extends IPlayer> object) {
return (List<Player>) checkNotNull( object );
}
+ @Nonnull
@SuppressWarnings("unchecked")
static Tile coreT(final ITile object) {
return (Tile) checkNotNull( object );
}
+ @Nonnull
static <M extends IModule> ModuleType<M> coreMT(final PublicModuleType<M> object) {
return ModuleType.of( checkNotNull( object ) );
}
+ @Nonnull
@SuppressWarnings("unchecked")
static UnitType coreUT(final IUnitType object) {
return (UnitType) checkNotNull( object );
}
+ @Nonnull
@SuppressWarnings("unchecked")
static VictoryConditionType coreVCT(final PublicVictoryConditionType object) {
switch (checkNotNull( object )) {
@@ -73,6 +86,7 @@ static VictoryConditionType coreVCT(final PublicVictoryConditionType object) {
throw new InternalInconsistencyException( "Unsupported type: " + object );
}
+ @Nonnull
@SuppressWarnings("unchecked")
static List<VictoryConditionType> coreVCT(final List<PublicVictoryConditionType> object) {
return Lists.transform( checkNotNull( object ), new NNFunctionNN<PublicVictoryConditionType, VictoryConditionType>() {
@@ -84,6 +98,7 @@ public VictoryConditionType apply(@Nonnull final PublicVictoryConditionType inpu
} );
}
+ @Nonnull
@SuppressWarnings("unchecked")
static PublicVictoryConditionType publicVCT(final VictoryConditionType object) {
switch (checkNotNull( object )) {
View
17 omicron-core/src/main/java/com/lyndir/omicron/api/model/ExtractorModule.java
@@ -3,6 +3,7 @@
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
+import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.*;
import com.lyndir.omicron.api.Constants;
import com.lyndir.omicron.api.util.PathUtils;
@@ -12,6 +13,9 @@
public class ExtractorModule extends Module implements IExtractorModule {
+ @SuppressWarnings("UnusedDeclaration")
+ private static final Logger logger = Logger.get( ExtractorModule.class );
+
private final ResourceType resourceType;
private final int speed;
@@ -51,8 +55,11 @@ protected void onReset() {
@Override
protected void onNewTurn() {
// Mine some resources.
- Tile location = getGameObject().getLocation();
- Optional<Integer> availableResources = location.getResourceQuantity( resourceType );
+ Optional<Tile> location = getGameObject().getLocation();
+ if (!location.isPresent())
+ return;
+
+ Optional<Integer> availableResources = location.get().getResourceQuantity( resourceType );
if (!availableResources.isPresent())
// No resources left to mine.
return;
@@ -84,8 +91,8 @@ public Double apply(@Nonnull final PathUtils.Step<GameObject> input) {
NNFunctionNN<GameObject, Iterable<GameObject>> neighboursFunction = new NNFunctionNN<GameObject, Iterable<GameObject>>() {
@Nonnull
@Override
- public Iterable<GameObject> apply(@Nonnull final GameObject input) {
- return FluentIterable.from( input.getLocation().neighbours() ).transform( new NFunctionNN<Tile, GameObject>() {
+ 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) {
@@ -111,7 +118,7 @@ public GameObject apply(@Nonnull final Tile input) {
newAvailableResources += minedResources;
// Update the amount of resources left in the tile after this turn's extraction.
- location.setResourceQuantity( resourceType, newAvailableResources );
+ location.get().setResourceQuantity( resourceType, newAvailableResources );
logger.trc( "unstocked resources: %d %s, left in tile: %d", minedResources, resourceType, newAvailableResources );
}
View
6 omicron-core/src/main/java/com/lyndir/omicron/api/model/Game.java
@@ -22,7 +22,8 @@
@SuppressWarnings("ParameterHidesMemberVariable") // IDEA doesn't understand setters that return this.
public class Game extends MetaObject implements IGame {
- static final Logger logger = Logger.get( IGame.class );
+ @SuppressWarnings("UnusedDeclaration")
+ private static final Logger logger = Logger.get( Game.class );
private static final Random RANDOM = new Random();
@@ -42,8 +43,7 @@ public static Builder builder() {
}
private Game(final Size levelSize, final Iterable<Player> players, final Iterable<VictoryConditionType> victoryConditions,
- final Map<GameListener, IPlayer> gameListeners, final GameResourceConfig resourceConfig,
- final GameUnitConfig unitConfig)
+ final Map<GameListener, IPlayer> gameListeners, final GameResourceConfig resourceConfig, final GameUnitConfig unitConfig)
throws Security.NotAuthenticatedException {
this.levelSize = levelSize;
levels = ImmutableList.of( new Level( levelSize, LevelType.GROUND, this ), new Level( levelSize, LevelType.SKY, this ),
View
6 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameController.java
@@ -2,7 +2,6 @@
import static com.lyndir.omicron.api.model.CoreUtils.*;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import com.lyndir.lhunath.opal.system.error.InternalInconsistencyException;
@@ -69,7 +68,7 @@ public void addGameListener(final GameListener gameListener)
@Authenticated
public PlayerGameInfo getPlayerGameInfo(final IPlayer player)
throws Security.NotAuthenticatedException {
- if (player.listObservableTiles().iterator().hasNext())
+ if (player.iterateObservableTiles().iterator().hasNext())
return PlayerGameInfo.discovered( player, player.getScore() );
return PlayerGameInfo.undiscovered( player );
@@ -186,6 +185,7 @@ public void run() {
*/
GameListener fire() {
return TypeUtils.newProxyInstance( GameListener.class, new InvocationHandler() {
+ @Nullable
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) {
synchronized (gameListeners) {
@@ -218,7 +218,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
throws Throwable {
synchronized (gameListeners) {
for (final Map.Entry<GameListener, IPlayer> gameListenerEntry : gameListeners.entrySet()) {
- Player gameListenerOwner = coreP( gameListenerEntry.getValue() );
+ Player gameListenerOwner = corePN( gameListenerEntry.getValue() );
if (gameListenerOwner == null)
Security.godRun( newGameListenerJob( gameListenerEntry.getKey(), method, args ) );
else if (playerCondition.apply( gameListenerOwner ))
View
43 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameObject.java
@@ -23,14 +23,15 @@
*/
public class GameObject extends MetaObject implements IGameObject {
- @ObjectMeta(ignoreFor = ObjectMeta.For.all)
- final Logger logger = Logger.get( getClass() );
+ @SuppressWarnings("UnusedDeclaration")
+ private static final Logger logger = Logger.get( GameObject.class );
private final GameObjectController<? extends GameObject> controller;
private final UnitType unitType;
private final Game game;
private final int objectID;
private final ImmutableListMultimap<ModuleType<?>, Module> modules;
+ @Nullable
private Player owner;
@Nullable
private Tile location;
@@ -75,9 +76,14 @@ public boolean equals(final Object obj) {
}
@Nonnull
+ Optional<Player> getOwner() {
+ return Optional.fromNullable( owner );
+ }
+
+ @Nonnull
@Override
- public Maybe<? extends Player> checkOwner() {
- return currentPlayer() == owner || currentPlayer().canObserve( this ).isTrue()? Maybe.of( owner ): Maybe.<Player>unknown();
+ public Maybe<Player> checkOwner() {
+ return currentPlayer() == owner || currentPlayer().canObserve( this ).isTrue()? Maybe.fromNullable( owner ): Maybe.<Player>unknown();
}
@Override
@@ -92,24 +98,23 @@ void setOwner(@Nullable final Player owner) {
this.owner = owner;
- getGame().getController().fireIfObservable( getLocation() ) //
+ getGame().getController().fireIfObservable( this ) //
.onUnitCaptured( this, ownerChange.to( this.owner ) );
}
@Authenticated
@Override
- @SuppressWarnings("ParameterHidesMemberVariable")
- public Maybool canObserve(@Nonnull final ITile location)
+ public Maybool canObserve(@Nonnull final GameObservable observable)
throws NotAuthenticatedException {
- return getController().canObserve( location );
+ return getController().canObserve( observable );
}
@Authenticated
@Nonnull
@Override
- public Iterable<Tile> listObservableTiles()
+ public Iterable<Tile> iterateObservableTiles()
throws NotAuthenticatedException, NotObservableException {
- return getController().listObservableTiles();
+ return getController().iterateObservableTiles();
}
@Override
@@ -130,7 +135,7 @@ public Game getGame() {
public Maybe<Tile> checkLocation()
throws NotAuthenticatedException {
Optional<Tile> location = getLocation();
- if (!isGod()) {
+ if (!isGod() && !isOwnedByCurrentPlayer()) {
if (location.isPresent()) {
if (!currentPlayer().canObserve( location.get() ).isTrue())
// Has a location but current player cannot observe it.
@@ -160,7 +165,7 @@ public UnitType getType() {
@Override
public <M extends IModule> Optional<M> getModule(final PublicModuleType<M> moduleType, final int index)
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
return Optional.fromNullable( Iterables.get( getModules( moduleType ), index, null ) );
}
@@ -168,7 +173,7 @@ public UnitType getType() {
@Override
public <M extends IModule> Optional<M> getModule(final PublicModuleType<M> moduleType, final PredicateNN<M> predicate)
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
return FluentIterable.from( getModules( moduleType ) ).firstMatch( predicate );
}
@@ -176,7 +181,7 @@ public UnitType getType() {
@Override
public <M extends IModule> List<M> getModules(final PublicModuleType<M> moduleType)
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
// Checked by Module's constructor.
//noinspection unchecked
@@ -186,7 +191,7 @@ public UnitType getType() {
@Override
public <M extends IModule> M onModuleElse(final PublicModuleType<M> moduleType, final int index, @Nullable final Object elseValue)
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
return ObjectUtils.ifNotNullElse( moduleType.getModuleType(), getModule( moduleType, index ).orNull(), elseValue );
}
@@ -195,7 +200,7 @@ public UnitType getType() {
public <M extends IModule> M onModuleElse(final PublicModuleType<M> moduleType, final PredicateNN<M> predicate,
@Nullable final Object elseValue)
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
return ObjectUtils.ifNotNullElse( moduleType.getModuleType(), getModule( moduleType, predicate ).orNull(), elseValue );
}
@@ -203,7 +208,7 @@ public UnitType getType() {
@Override
public <M extends IModule> M onModule(final PublicModuleType<M> moduleType, final int index)
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
return onModuleElse( moduleType, index, null );
}
@@ -211,7 +216,7 @@ public UnitType getType() {
@Override
public <M extends IModule> M onModule(final PublicModuleType<M> moduleType, final PredicateNN<M> predicate)
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
return onModuleElse( moduleType, predicate, null );
}
@@ -219,7 +224,7 @@ public UnitType getType() {
@Override
public ImmutableCollection<Module> listModules()
throws NotAuthenticatedException, NotObservableException {
- assertObservable( getLocation() );
+ assertObservable( this );
return ImmutableList.copyOf( modules.values() );
}
View
64 omicron-core/src/main/java/com/lyndir/omicron/api/model/GameObjectController.java
@@ -15,8 +15,8 @@
public class GameObjectController<O extends GameObject> extends MetaObject implements IGameObjectController<O> {
- @ObjectMeta(ignoreFor = ObjectMeta.For.all)
- final Logger logger = Logger.get( getClass() );
+ @SuppressWarnings("UnusedDeclaration")
+ private static final Logger logger = Logger.get( GameObjectController.class );
private final O gameObject;
@@ -39,14 +39,8 @@ public O getGameObject() {
return gameObject;
}
- @Nonnull
- @Override
- public Optional<Player> getOwner() {
- return getGameObject().checkOwner();
- }
-
void setOwner(@Nullable final Player owner) {
- Optional<Player> oldOwner = getOwner();
+ Optional<Player> oldOwner = getGameObject().getOwner();
if (oldOwner.isPresent() && ObjectUtils.isEqual( oldOwner.get(), owner ))
// Object already owned by owner.
return;
@@ -56,43 +50,39 @@ void setOwner(@Nullable final Player owner) {
getGameObject().setOwner( owner );
- Optional<Player> newOwner = getOwner();
+ Optional<Player> newOwner = getGameObject().getOwner();
if (newOwner.isPresent())
newOwner.get().addObjects( getGameObject() );
}
void setLocation(@Nonnull final Tile location) {
- Tile oldLocation = getGameObject().getLocation();
- if (oldLocation != null && ObjectUtils.isEqual( oldLocation, location ))
+ Optional<Tile> oldLocation = getGameObject().getLocation();
+ if (oldLocation.isPresent() && ObjectUtils.isEqual( oldLocation.get(), location ))
// Object already at location.
return;
- if (oldLocation != null)
- oldLocation.setContents( null );
+ if (oldLocation.isPresent())
+ oldLocation.get().setContents( null );
getGameObject().setLocation( location );
- Tile newLocation = getGameObject().getLocation();
- if (newLocation != null)
- newLocation.setContents( getGameObject() );
+ Optional<Tile> newLocation = getGameObject().getLocation();
+ if (newLocation.isPresent())
+ newLocation.get().setContents( getGameObject() );
}
@Override
- @Authenticated
- public Maybool canObserve(@Nonnull final ITile location)
- throws NotAuthenticatedException, NotObservableException {
- if (isGod() || (getGameObject().isOwnedByCurrentPlayer() && ObjectUtils.equals( location, getGameObject().getLocation() )))
- return Maybool.YES;
-
- return getGameObject().onModuleElse( ModuleType.BASE, 0, Maybool.NO ).canObserve( location );
+ public Maybool canObserve(@Nonnull final GameObservable observable)
+ throws NotAuthenticatedException {
+ return getGameObject().onModuleElse( ModuleType.BASE, 0, Maybool.NO ).canObserve( observable );
}
@Nonnull
@Override
@Authenticated
- public Iterable<Tile> listObservableTiles()
+ public Iterable<Tile> iterateObservableTiles()
throws NotAuthenticatedException, NotObservableException {
- return getGameObject().onModuleElse( ModuleType.BASE, 0, ImmutableList.of() ).listObservableTiles();
+ return getGameObject().onModuleElse( ModuleType.BASE, 0, ImmutableList.of() ).iterateObservableTiles();
}
void onReset() {
@@ -107,23 +97,29 @@ void onNewTurn() {
void die() {
GameObject gameObject = getGameObject();
- gameObject.getGame().getController().fireIfObservable( gameObject.getLocation() ) //
+ gameObject.getGame().getController().fireIfObservable( gameObject ) //
.onUnitDied( gameObject );
- // Remove from the game (map & player).
- Optional<Player> owner = gameObject.checkOwner();
+ // Remove from the game: player.
+ Optional<Player> owner = gameObject.getOwner();
if (owner.isPresent())
owner.get().getController().removeObject( gameObject );
- gameObject.getLocation().setContents( null );
+
+ // Remove from the game: level.
+ Optional<Tile> location = getGameObject().getLocation();
+ if (location.isPresent())
+ location.get().setContents( null );
}
void replaceWith(final GameObject replacementObject) {
- GameObject currentGameObject = getGameObject();
+ GameObject gameObject = getGameObject();
// Remove from the game (map & player).
- Optional<Player> owner = currentGameObject.checkOwner();
+ Optional<Player> owner = gameObject.getOwner();
if (owner.isPresent())
- owner.get().getController().removeObject( currentGameObject );
- currentGameObject.getLocation().replaceContents( replacementObject );
+ owner.get().getController().removeObject( gameObject );
+ Optional<Tile> location = getGameObject().getLocation();
+ if (location.isPresent())
+ location.get().replaceContents( replacementObject );
}
}
View
118 omicron-core/src/main/java/com/lyndir/omicron/api/model/MobilityModule.java
@@ -9,6 +9,7 @@
import com.lyndir.omicron.api.*;
import java.util.*;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
public class MobilityModule extends Module implements IMobilityModule {
@@ -70,13 +71,16 @@ public double costForMovingInLevel(final LevelType levelType)
public double costForLevelingToLevel(final LevelType levelType)
throws Security.NotAuthenticatedException, Security.NotObservableException {
assertObservable();
+ Optional<Tile> location = getGameObject().getLocation();
+ if (!location.isPresent())
+ return Double.MAX_VALUE;
// Level up until we reach the target level.
double cost = 0;
- LevelType currentLevel = getGameObject().getLocation().getLevel().getType();
+ LevelType currentLevel = location.get().getLevel().getType();
if (levelType == currentLevel)
return 0;
- do {
+ while (true) {
Optional<LevelType> newLevel = currentLevel.up();
if (!newLevel.isPresent())
break;
@@ -93,12 +97,11 @@ public double costForLevelingToLevel(final LevelType levelType)
if (currentLevel == levelType)
return cost;
}
- while (true);
// Level down until we reach the target level.
cost = 0;
- currentLevel = getGameObject().getLocation().getLevel().getType();
- do {
+ currentLevel = location.get().getLevel().getType();
+ while (true) {
Optional<LevelType> newLevel = currentLevel.down();
if (!newLevel.isPresent())
break;
@@ -115,7 +118,6 @@ public double costForLevelingToLevel(final LevelType levelType)
if (currentLevel == levelType)
return cost;
}
- while (true);
// Unreachable code.
throw new IllegalArgumentException( "Unsupported level type: " + levelType );
@@ -132,17 +134,22 @@ public Leveling leveling(final LevelType levelType)
throws Security.NotAuthenticatedException, Security.NotOwnedException, Security.NotObservableException {
assertOwned();
- Tile currentLocation = getGameObject().getLocation();
- if (levelType == currentLocation.getLevel().getType())
+ Optional<Tile> currentLocation = getGameObject().getLocation();
+ if (!currentLocation.isPresent())
+ return Leveling.impossible( this, 0 );
+
+ if (levelType == currentLocation.get().getLevel().getType())
// Already in the destination level.
- return new Leveling( 0, currentLocation );
+ return Leveling.possible( this, currentLocation.get(), 0 );
double cost = costForLevelingToLevel( levelType );
if (cost > remainingSpeed)
// Cannot move: insufficient speed remaining this turn.
- return new Leveling( cost );
+ return Leveling.impossible( this, cost );
- return new Leveling( cost, getGameObject().getGame().getLevel( levelType ).getTile( currentLocation.getPosition() ).get() );
+ return Leveling.possible( this,
+ getGameObject().getGame().getLevel( levelType ).getTile( currentLocation.get().getPosition() ).get(),
+ cost );
}
/**
@@ -159,7 +166,7 @@ public Movement movement(final ITile target)
Leveling leveling = leveling( target.getLevel().getType() );
if (!leveling.isPossible())
// Cannot move because we can't level to the target's level.
- return new Movement( leveling.getCost() );
+ return Movement.impossible( this, leveling.getCost() );
// Initialize cost calculation.
Tile currentLocation = leveling.getTarget();
@@ -192,7 +199,7 @@ public Double apply(@Nonnull final Step<Tile> input) {
// Find the path!
Optional<Path<Tile>> path = find( currentLocation, foundFunction, costFunction, remainingSpeed, neighboursFunction );
- return new Movement( leveling.getCost() + (path.isPresent()? path.get().getCost(): 0), leveling, path );
+ return Movement.possible( this, leveling.getCost() + (path.isPresent()? path.get().getCost(): 0), leveling, path );
}
@Override
@@ -209,19 +216,24 @@ protected void onNewTurn() {
return ModuleType.MOBILITY;
}
- public class Leveling extends MetaObject implements ILeveling {
+ public static class Leveling extends MetaObject implements ILeveling {
- private final double cost;
+ private final MobilityModule module;
+ private final double cost;
private final Optional<Tile> target;
- Leveling(final double cost) {
+ private Leveling(final MobilityModule module, final Optional<Tile> target, final double cost) {
+ this.module = module;
this.cost = cost;
- target = Optional.absent();
+ this.target = target;
}
- Leveling(final double cost, final Tile target) {
- this.cost = cost;
- this.target = Optional.of( target );
+ static Leveling impossible(final MobilityModule module, final double cost) {
+ return new Leveling( module, Optional.<Tile>absent(), cost );
+ }
+
+ static Leveling possible(final MobilityModule module, final Tile target, final double cost) {
+ return new Leveling( module, Optional.of( target ), cost );
}
@Override
@@ -254,44 +266,51 @@ public Tile getTarget() {
@Authenticated
public void execute()
throws Security.NotAuthenticatedException, Security.NotOwnedException, ImpossibleException, InvalidatedException {
- assertOwned();
+ module.assertOwned();
assertState( isPossible(), ImpossibleException.class );
- assertState( cost <= remainingSpeed, InvalidatedException.class );
+ assertState( cost <= module.remainingSpeed, InvalidatedException.class );
// TODO: No target.isAccessible check: Most units that level cannot see other levels before they go there.
// TODO: Should we disallow leveling until you can see the level above you like we do with movement and the tile you move to?
- Change.From<ITile> locationChange = Change.<ITile>from( getGameObject().getLocation() );
- ChangeDbl.From remainingSpeedChange = ChangeDbl.from( remainingSpeed );
+ Change.From<ITile> locationChange = Change.<ITile>from( module.getGameObject().getLocation().get() );
+ ChangeDbl.From remainingSpeedChange = ChangeDbl.from( module.remainingSpeed );
// Execute the leveling.
- getGameObject().getController().setLocation( target.get() );
- remainingSpeed -= cost;
+ module.getGameObject().getController().setLocation( target.get() );
+ module.remainingSpeed -= cost;
- getGameController().fireIfObservable( getGameObject().getLocation() )
- .onMobilityLeveled( MobilityModule.this, locationChange.to( getGameObject().getLocation() ),
- remainingSpeedChange.to( remainingSpeed ) );
+ module.getGameController()
+ .fireIfObservable( module.getGameObject() )
+ .onMobilityLeveled( module, locationChange.to( module.getGameObject().getLocation().get() ),
+ remainingSpeedChange.to( module.remainingSpeed ) );
}
}
- public class Movement extends MetaObject implements IMovement {
+ public static class Movement extends MetaObject implements IMovement {
+ private final MobilityModule module;
private final double cost;
private final Leveling leveling;
private final Optional<Path<Tile>> path;
- Movement(final double cost) {
- this.cost = cost;
- leveling = null;
- path = Optional.absent();
- }
-
- Movement(final double cost, @Nonnull final Leveling leveling, final Optional<Path<Tile>> path) {
+ private Movement(final MobilityModule module, final double cost, @Nullable final Leveling leveling,
+ final Optional<Path<Tile>> path) {
+ this.module = module;
this.cost = cost;
this.leveling = leveling;
this.path = path;
}
+ static Movement impossible(final MobilityModule module, final double cost) {
+ return new Movement( module, cost, null, Optional.<Path<Tile>>absent() );
+ }
+
+ static Movement possible(final MobilityModule module, final double cost, @Nonnull final Leveling leveling,
+ final Optional<Path<Tile>> path) {
+ return new Movement( module, cost, leveling, path );
+ }
+
/**
* The cost for executing the movement. If not possible, the cost is either zero if unknown or the cost for the action that
* exceeded the module's remaining speed (not the cost of getting to the target).
@@ -322,19 +341,21 @@ public boolean isPossible() {
@Authenticated
public void execute()
throws Security.NotAuthenticatedException, Security.NotOwnedException, ImpossibleException, InvalidatedException {
- assertOwned();
+ module.assertOwned();
assertState( isPossible(), ImpossibleException.class );
- assertState( cost <= remainingSpeed, InvalidatedException.class );
+ assertState( cost <= module.remainingSpeed, InvalidatedException.class );
+ assert leveling != null;
- Change.From<ITile> locationChange = Change.<ITile>from( getGameObject().getLocation() );
- ChangeDbl.From remainingSpeedChange = ChangeDbl.from( remainingSpeed );
+ Change.From<ITile> locationChange = Change.<ITile>from( module.getGameObject().getLocation().get() );
+ ChangeDbl.From remainingSpeedChange = ChangeDbl.from( module.remainingSpeed );
// Check that the path can still be walked.
Path<Tile> tracePath = path.get();
do {
assertState( tracePath.getTarget().checkAccessible() || //
- isEqual( getGameObject().getLocation(), tracePath.getTarget() ), PathInvalidatedException.class,
- tracePath );
+ isEqual( module.getGameObject().getLocation().get(), tracePath.getTarget() ), //
+ PathInvalidatedException.class, tracePath
+ );
Optional<Path<Tile>> parent = tracePath.getParent();
if (!parent.isPresent())
@@ -348,12 +369,13 @@ public void execute()
leveling.execute();
// Execute the path.
- getGameObject().getController().setLocation( path.get().getTarget() );
- remainingSpeed -= path.get().getCost();
+ module.getGameObject().getController().setLocation( path.get().getTarget() );
+ module.remainingSpeed -= path.get().getCost();
- getGameController().fireIfObservable( getGameObject().getLocation() )
- .onMobilityMoved( MobilityModule.this, locationChange.to( getGameObject().getLocation() ),
- remainingSpeedChange.to( remainingSpeed ) );
+ module.getGameController()
+ .fireIfObservable( module.getGameObject() )
+ .onMobilityMoved( module, locationChange.to( module.getGameObject().getLocation().get() ),
+ remainingSpeedChange.to( module.remainingSpeed ) );
}
}
View
7 omicron-core/src/main/java/com/lyndir/omicron/api/model/Module.java
@@ -19,14 +19,13 @@
import com.google.common.base.Preconditions;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.MetaObject;
-import com.lyndir.lhunath.opal.system.util.ObjectMeta;
import javax.annotation.Nullable;
public abstract class Module extends MetaObject implements IModule {
- @ObjectMeta(ignoreFor = ObjectMeta.For.all)
- protected final Logger logger = Logger.get( getClass() );
+ @SuppressWarnings("UnusedDeclaration")
+ private static final Logger logger = Logger.get( Module.class );
private final ImmutableResourceCost resourceCost;
@@ -73,7 +72,7 @@ void assertOwned()
void assertObservable()
throws Security.NotAuthenticatedException, Security.NotObservableException {
- Security.assertObservable( getGameObject().getLocation() );
+ Security.assertObservable( getGameObject() );
}
protected abstract void onReset();
View
4 omicron-core/src/main/java/com/lyndir/omicron/api/model/Player.java
@@ -82,8 +82,8 @@ public Maybool canObserve(@Nonnull final GameObservable observable)
@Authenticated
@Nonnull
@Override
- public Iterable<Tile> listObservableTiles() {
- return getController().listObservableTiles();
+ public Iterable<Tile> iterateObservableTiles() {
+ return getController().iterateObservableTiles();
}
@Override
View
57 omicron-core/src/main/java/com/lyndir/omicron/api/model/PlayerController.java
@@ -9,6 +9,7 @@
import com.lyndir.omicron.api.util.Maybe;
import com.lyndir.omicron.api.util.Maybool;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
public class PlayerController extends MetaObject implements IPlayerController {
@@ -40,19 +41,24 @@ public GameController getGameController() {
}
/**
- * @see GameObservable#checkOwner() IGameObject#canObserve(GameObservable)
+ * @see #iterateObservableObjects()
+ * @see IGameObject#canObserve(GameObservable)
*/
@Override
public Maybool canObserve(@Nonnull final GameObservable observable)
throws NotAuthenticatedException {
- ImmutableSet<GameObject> objects = getPlayer().getObjects();
- if (getPlayer().isCurrentPlayer() && objects.contains( observable. )) {
- // If we're the current player, checkOwner is a shortcut.
- Maybe<? extends IPlayer> owner = observable.checkOwner();
- if (owner.presence() == Maybe.Presence.PRESENT && ObjectUtils.isEqual( getPlayer(), owner.get() ))
- // Observable is owned by us.
- return Maybool.YES;
- }
+
+ Iterable<GameObject> objects = iterateObservableObjects();
+ if (observable instanceof GameObject && Iterables.contains( objects, observable ))
+ return Maybool.YES;
+ if (observable instanceof Tile && FluentIterable.from( objects ).transform( new NFunctionNN<GameObject, Tile>() {
+ @Nullable
+ @Override
+ public Tile apply(@Nonnull final GameObject gameObject) {
+ return gameObject.checkLocation().orNull();
+ }
+ } ).contains( observable ))
+ return Maybool.YES;
// Observable is not owned by us, check if any of our objects can see it.
return FluentIterable.from( objects ).transform( new Function<IGameObject, Maybool>() {
@@ -68,39 +74,34 @@ public boolean apply(final Maybool result) {
} ).or( Maybool.NO );
}
+ /**
+ * @see #iterateObservableObjects()
+ * @see IGameObject#iterateObservableTiles()
+ */
@Nonnull
@Override
@Authenticated
- public Iterable<Tile> listObservableTiles()
+ public Iterable<Tile> iterateObservableTiles()
throws NotAuthenticatedException {
- return FluentIterable.from( getPlayer().getObjects() ).transformAndConcat( new Function<GameObject, Iterable<? extends Tile>>() {
+ return FluentIterable.from( iterateObservableObjects() ).transformAndConcat( new Function<GameObject, Iterable<? extends Tile>>() {
@Override
- public Iterable<Tile> apply(final GameObject input) {
- return input.listObservableTiles();
+ public Iterable<Tile> apply(final GameObject gameObject) {
+ return gameObject.iterateObservableTiles();
}
} );
}
/**
- * List the objects of this player.
- *
- * NOTE: The controller must be of the currently authenticated player.
- *
- * @return A list of game objects owned by this controller's player.
+ * @see IPlayer#canObserve(GameObservable)
*/
@Override
@Authenticated
- public ImmutableSet<GameObject> listObjects()
+ public Iterable<GameObject> iterateObservableObjects()
throws NotAuthenticatedException {
if (isGod() || getPlayer().isCurrentPlayer())
- return ImmutableSet.copyOf( getPlayer().getObjects() );
+ return getPlayer().getObjects();
- return FluentIterable.from( getPlayer().getObjects() ).filter( new PredicateNN<GameObject>() {
- @Override
- public boolean apply(@Nonnull final GameObject input) {
- return currentPlayer().canObserve( input ).isTrue();
- }
- } ).toSet();
+ return iterateObservableObjects( currentPlayer() );
}
/**
@@ -115,8 +116,8 @@ public boolean apply(@Nonnull final GameObject input) {
public Iterable<GameObject> iterateObservableObjects(final GameObserver observer) {
return FluentIterable.from( getPlayer().getObjects() ).filter( new Predicate<GameObject>() {
@Override
- public boolean apply(final GameObject input) {
- return observer.canObserve( input ).isTrue();
+ public boolean apply(final GameObject gameObject) {
+ return observer.canObserve( gameObject ).isTrue();
}
} );
}
View
21 omicron-core/src/main/java/com/lyndir/omicron/api/model/Tile.java
@@ -10,7 +10,6 @@
import com.lyndir.lhunath.opal.math.Vec2;
import com.lyndir.lhunath.opal.system.util.*;
import com.lyndir.omicron.api.*;
-import com.lyndir.omicron.api.model.Security.NotAuthenticatedException;
import com.lyndir.omicron.api.util.Maybe;
import java.util.*;
import java.util.Objects;
@@ -61,6 +60,26 @@ public boolean equals(final Object obj) {
return isEqual( position, o.position ) && isEqual( level, o.level );
}
+ @Override
+ public Maybe<? extends IPlayer> checkOwner() {
+ if (!currentPlayer().canObserve( this ).isTrue())
+ return Maybe.unknown();
+
+ if (contents == null)
+ return Maybe.absent();
+
+ return contents.checkOwner();
+ }
+
+ @Override
+ public Maybe<? extends ITile> checkLocation()
+ throws NotAuthenticatedException {
+ if (!currentPlayer().canObserve( this ).isTrue())
+ return Maybe.unknown();
+
+ return Maybe.of( this );
+ }
+
@Nonnull
Optional<GameObject> getContents() {
return Optional.fromNullable( contents );
View
4 omicron-core/src/main/java/com/lyndir/omicron/api/model/WeaponModule.java
@@ -99,7 +99,7 @@ public boolean fireAt(final ITile target)
OutOfAmmunitionException {
assertOwned();
Security.assertObservable( target );
- assertState( getGameObject().getLocation().getPosition().distanceTo( target.getPosition() ) <= range, OutOfRangeException.class );
+ assertState( !getGameObject().getLocation().isPresent() || getGameObject().getLocation().get().getPosition().distanceTo( target.getPosition() ) <= range, OutOfRangeException.class );
assertState( repeated < repeat, OutOfRepeatsException.class );
assertState( ammunition > 0, OutOfAmmunitionException.class );
@@ -109,7 +109,7 @@ public boolean fireAt(final ITile target)
++repeated;
--ammunition;
- getGameController().fireIfObservable( getGameObject().getLocation() )
+ getGameController().fireIfObservable( getGameObject() )
.onWeaponFired( this, target, repeatedChange.to( repeated ), ammunitionChange.to( ammunition ) );
Tile coreTarget = coreT( target );
View
4 omicron-core/src/test/java/com/lyndir/omicron/api/model/AbstractTest.java
@@ -17,6 +17,7 @@
*/
public abstract class AbstractTest {
+ @SuppressWarnings("NonConstantLogger")
protected final Logger logger = Logger.get( getClass() );