Skip to content

Commit

Permalink
Ground, MineralVein: Convert 'exposed' to a new enum type.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjon3377 committed Nov 16, 2024
1 parent 5faf631 commit 0704aca
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exploration.common;

import common.map.HasName;
import legacy.map.fixtures.resources.ExposureStatus;
import legacy.map.fixtures.resources.MineralVein;
import legacy.map.fixtures.resources.StoneDeposit;
import lovelace.util.LovelaceLogger;
Expand Down Expand Up @@ -720,8 +721,8 @@ public final void dig() {
final TileFixture newFixture = oldFixture.copy(IFixture.CopyBehavior.KEEP);
// TODO: Extract an interface for 'exposed' so we only have to do one test
switch (newFixture) {
case final Ground g -> g.setExposed(true);
case final MineralVein mv -> mv.setExposed(true);
case final Ground g -> g.setExposure(ExposureStatus.EXPOSED);
case final MineralVein mv -> mv.setExposure(ExposureStatus.EXPOSED);
default -> {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import legacy.map.HasKind;
import legacy.map.fixtures.LegacyQuantity;
import legacy.map.fixtures.resources.CultivationStatus;
import legacy.map.fixtures.resources.ExposureStatus;
import legacy.map.fixtures.towns.ITownFixture;
import common.map.fixtures.towns.TownStatus;
import legacy.map.fixtures.towns.CommunityStats;
Expand Down Expand Up @@ -230,7 +231,7 @@ private static boolean bothOrNeitherOcean(final @Nullable TileType one, final @N
private static boolean isReallyClaimable(final HarvestableFixture fix) {
switch (fix) {
case final MineralVein mv -> {
return mv.isExposed();
return mv.getExposure() == ExposureStatus.EXPOSED;
}
case final Meadow m -> {
return m.getCultivation() == CultivationStatus.CULTIVATED;
Expand Down
3 changes: 2 additions & 1 deletion drivers.utility/src/main/java/utility/MapTradeCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Objects;

import legacy.map.fixtures.resources.ExposureStatus;
import legacy.map.fixtures.towns.AbstractTown;
import legacy.map.fixtures.towns.IFortress;
import legacy.map.fixtures.towns.Village;
Expand Down Expand Up @@ -116,7 +117,7 @@ private static List<FixtureMatcher> initializeMatchers() {
FixtureMatcher.complements(Meadow.class, m -> m.getType() == Meadow.MeadowType.FIELD, "Fields", "Meadows")
.forEach(retval::add);
retval.add(FixtureMatcher.trivialMatcher(Hill.class));
FixtureMatcher.complements(Ground.class, Ground::isExposed, "Ground (exposed)",
FixtureMatcher.complements(Ground.class, g -> g.getExposure() == ExposureStatus.EXPOSED, "Ground (exposed)",
"Ground").forEach(retval::add);
return Collections.unmodifiableList(retval);
}
Expand Down
5 changes: 3 additions & 2 deletions main/src/main/java/drivers/map_viewer/FixtureFilterList.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.swing.ListSelectionModel;
import javax.swing.DropMode;

import legacy.map.fixtures.resources.ExposureStatus;
import lovelace.util.ReorderableListModel;

import legacy.map.TileFixture;
Expand All @@ -30,9 +31,9 @@

public FixtureFilterList() {
matcherListModel = new ReorderableListModel<>(
FixtureMatcher.simpleMatcher(Ground.class, Ground::isExposed,
FixtureMatcher.simpleMatcher(Ground.class, g -> g.getExposure() == ExposureStatus.EXPOSED,
"Ground (exposed)"),
FixtureMatcher.simpleMatcher(Ground.class, not(Ground::isExposed), "Ground"),
FixtureMatcher.simpleMatcher(Ground.class, g -> g.getExposure() == ExposureStatus.HIDDEN, "Ground"),
FixtureMatcher.simpleMatcher(Grove.class, g -> g.getType() == Grove.GroveType.ORCHARD, "Orchards"),
FixtureMatcher.simpleMatcher(Grove.class, g -> g.getType() == Grove.GroveType.GROVE, "Groves"),
FixtureMatcher.simpleMatcher(Meadow.class, m -> m.getType() == Meadow.MeadowType.FIELD, "Fields"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.swing.table.AbstractTableModel;

import drivers.common.IterableComparator;
import legacy.map.fixtures.resources.ExposureStatus;
import lovelace.util.Reorderable;

import legacy.map.TileFixture;
Expand Down Expand Up @@ -121,8 +122,8 @@ public FixtureFilterTableModel() {
.forEach(matchers::add);
matchers.add(FixtureMatcher.trivialMatcher(Shrub.class));
matchers.add(FixtureMatcher.trivialMatcher(Hill.class));
FixtureMatcher.complements(Ground.class, Ground::isExposed, "Ground (exposed)", "Ground")
.forEach(matchers::add);
FixtureMatcher.complements(Ground.class, g -> g.getExposure() == ExposureStatus.EXPOSED, "Ground (exposed)",
"Ground").forEach(matchers::add);
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions model/src/main/java/legacy/dbio/DBGroundHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import legacy.map.IMutableLegacyMap;
import legacy.map.fixtures.Ground;
import common.xmlio.Warning;
import legacy.map.fixtures.resources.ExposureStatus;

import static io.jenetics.facilejdbc.Param.value;

Expand Down Expand Up @@ -50,7 +51,8 @@ INSERT INTO ground (row, column, id, kind, exposed, image)
public void write(final Transactional db, final Ground obj, final Point context) throws SQLException {
INSERT_SQL.on(value("row", context.row()), value("column", context.column()),
value("id", obj.getId()), value("kind", obj.getKind()),
value("exposed", obj.isExposed()), value("image", obj.getImage())).execute(db.connection());
value("exposed", obj.getExposure() == ExposureStatus.EXPOSED),
value("image", obj.getImage())).execute(db.connection());
}

private TryBiConsumer<Map<String, Object>, Warning, SQLException> readGround(final IMutableLegacyMap map) {
Expand All @@ -59,7 +61,8 @@ private TryBiConsumer<Map<String, Object>, Warning, SQLException> readGround(fin
final int column = (Integer) dbRow.get("column");
final int id = (Integer) dbRow.get("id");
final String kind = (String) dbRow.get("kind");
final boolean exposed = getBooleanValue(dbRow, "exposed");
final ExposureStatus exposed = getBooleanValue(dbRow, "exposed") ? ExposureStatus.EXPOSED :
ExposureStatus.HIDDEN;
final String image = (String) dbRow.get("image");
final Ground ground = new Ground(id, kind, exposed);
if (!Objects.isNull(image)) {
Expand Down
6 changes: 4 additions & 2 deletions model/src/main/java/legacy/dbio/DBMineralHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import legacy.map.Point;
import legacy.map.IMutableLegacyMap;

import legacy.map.fixtures.resources.ExposureStatus;
import legacy.map.fixtures.resources.MineralVein;
import legacy.map.fixtures.MineralFixture;
import legacy.map.fixtures.resources.StoneDeposit;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void write(final Transactional db, final MineralFixture obj, final Point
switch (obj) {
case final MineralVein m -> {
type = "mineral";
exposed = m.isExposed();
exposed = m.getExposure() == ExposureStatus.EXPOSED;
}
case final StoneDeposit stoneDeposit -> {
type = "stone";
Expand All @@ -86,7 +87,8 @@ private TryBiConsumer<Map<String, Object>, Warning, SQLException> readMineralVei
final int column = (Integer) dbRow.get("column");
final int id = (Integer) dbRow.get("id");
final String kind = (String) dbRow.get("kind");
final boolean exposed = getBooleanValue(dbRow, "exposed");
final ExposureStatus exposed = getBooleanValue(dbRow, "exposed") ? ExposureStatus.EXPOSED :
ExposureStatus.HIDDEN;
final int dc = (Integer) dbRow.get("dc");
final String image = (String) dbRow.get("image");
final MineralVein mineral = new MineralVein(kind, exposed, dc, id);
Expand Down
38 changes: 23 additions & 15 deletions model/src/main/java/legacy/map/fixtures/Ground.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import legacy.map.IFixture;
import legacy.map.HasMutableImage;
import legacy.map.fixtures.resources.ExposureStatus;
import org.jetbrains.annotations.NotNull;

/**
* A TileFixture to represent the basic rock beneath the tile, possibly exposed.
*/
public final class Ground implements MineralFixture, HasMutableImage {
public Ground(final int id, final String kind, final boolean exposed) {
public Ground(final int id, final String kind, final ExposureStatus exposure) {
this.id = id;
this.kind = kind;
this.exposed = exposed;
this.exposure = exposure;
}

/**
Expand All @@ -29,23 +30,21 @@ public Ground(final int id, final String kind, final boolean exposed) {

/**
* Whether the ground is exposed.
*
* TODO: convert to enum, or make Ground a sealed interface with ExposedGround and UnexposedGround implementations.
*/
private boolean exposed;
private ExposureStatus exposure;

/**
* Whether the ground is exposed.
*/
public boolean isExposed() {
return exposed;
public ExposureStatus getExposure() {
return exposure;
}

/**
* Set whether the ground is exposed.
*/
public void setExposed(final boolean exposed) {
this.exposed = exposed;
public void setExposure(final ExposureStatus exposure) {
this.exposure = exposure;
}

/**
Expand Down Expand Up @@ -98,7 +97,7 @@ public void setImage(final @NotNull String image) {
*/
@Override
public @NotNull Ground copy(final @NotNull IFixture.CopyBehavior zero) {
final Ground retval = new Ground(id, kind, exposed);
final Ground retval = new Ground(id, kind, exposure);
retval.setImage(image);
return retval;
}
Expand All @@ -108,7 +107,10 @@ public void setImage(final @NotNull String image) {
*/
@Override
public @NotNull String getDefaultImage() {
return (exposed) ? "expground.png" : "blank.png";
return switch (exposure) {
case EXPOSED -> "expground.png";
case HIDDEN -> "blank.png";
};
}

/**
Expand All @@ -121,7 +123,7 @@ public boolean equals(final Object obj) {
return true;
} else if (obj instanceof final Ground that) {
return kind.equals(that.getKind()) &&
exposed == that.isExposed() && id == that.getId();
exposure == that.getExposure() && id == that.getId();
} else {
return false;
}
Expand All @@ -134,7 +136,10 @@ public int hashCode() {

@Override
public @NotNull String getShortDescription() {
return "%s %s ground".formatted((exposed) ? "Exposed" : "Unexposed", kind);
return (switch (exposure) {
case EXPOSED -> "Exposed %s ground";
case HIDDEN -> "Unexposed %s ground";
}).formatted(kind);
}

@Override
Expand All @@ -151,7 +156,7 @@ public boolean equalsIgnoringID(final @NotNull IFixture fixture) {
if (this == fixture) {
return true;
} else if (fixture instanceof final Ground that) {
return kind.equals(that.getKind()) && exposed == that.isExposed();
return kind.equals(that.getKind()) && exposure == that.getExposure();
} else {
return false;
}
Expand All @@ -172,6 +177,9 @@ public boolean equalsIgnoringID(final @NotNull IFixture fixture) {
@SuppressWarnings("MagicNumber")
@Override
public int getDC() {
return (exposed) ? 10 : 40;
return switch (exposure) {
case EXPOSED -> 10;
case HIDDEN -> 40;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package legacy.map.fixtures.resources;

import java.text.ParseException;
import java.util.stream.Stream;

/**
* Exposure status of ground, mineral resources, etc.
*
* @author Jonathan Lovelace
*/
public enum ExposureStatus {
EXPOSED, HIDDEN;

@Override
public String toString() {
return name().toLowerCase();
}

public static ExposureStatus parse(final String str) throws ParseException {
return Stream.of(values()).filter(v -> v.toString().equals(str)).findAny()
.orElseThrow(() -> new ParseException("Unexpected exposure status %s".formatted(str), 0));
}

}
32 changes: 17 additions & 15 deletions model/src/main/java/legacy/map/fixtures/resources/MineralVein.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* A vein of a mineral.
*/
public final class MineralVein implements HarvestableFixture, MineralFixture {
public MineralVein(final String kind, final boolean exposed, final int dc, final int id) {
public MineralVein(final String kind, final ExposureStatus exposure, final int dc, final int id) {
this.kind = kind;
this.exposed = exposed;
this.exposure = exposure;
this.dc = dc;
this.id = id;
}
Expand All @@ -33,16 +33,16 @@ public String getKind() {
* and make callers remove the non-exposed and add the exposed version
* back.
*/
private boolean exposed;
private ExposureStatus exposure;

/**
* Whether the vein is exposed or not.
* TODO: Make non-variable, and instead make a copy-with-modification
* and make callers remove the non-exposed and add the exposed version
* back.
*/
public boolean isExposed() {
return exposed;
public ExposureStatus getExposure() {
return exposure;
}

/**
Expand All @@ -51,8 +51,8 @@ public boolean isExposed() {
* and make callers remove the non-exposed and add the exposed version
* back.
*/
public void setExposed(final boolean exposed) {
this.exposed = exposed;
public void setExposure(final ExposureStatus exposure) {
this.exposure = exposure;
}

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ public void setImage(final String image) {

@Override
public MineralVein copy(final CopyBehavior zero) {
final MineralVein retval = new MineralVein(kind, exposed, (zero == CopyBehavior.ZERO) ? 0 : dc, id);
final MineralVein retval = new MineralVein(kind, exposure, (zero == CopyBehavior.ZERO) ? 0 : dc, id);
retval.setImage(image);
return retval;
}
Expand All @@ -117,7 +117,7 @@ public MineralVein copy(final CopyBehavior zero) {
public boolean equalsIgnoringID(final IFixture fixture) {
if (fixture instanceof final MineralVein it) {
return kind.equals(it.getKind()) &&
exposed == it.isExposed();
exposure == it.getExposure();
} else {
return false;
}
Expand All @@ -139,11 +139,10 @@ public int hashCode() {

@Override
public String toString() {
if (exposed) {
return "A %s deposit, exposed, DC %d".formatted(kind, dc);
} else {
return "A %s deposit, not exposed, DC %d".formatted(kind, dc);
}
return (switch (exposure) {
case EXPOSED -> "A %s deposit, exposed, DC %d";
case HIDDEN -> "A %s deposit, not exposed, DC %d";
}).formatted(kind, dc);
}

@Override
Expand All @@ -158,6 +157,9 @@ public String getPlural() {

@Override
public String getShortDescription() {
return (exposed) ? "exposed " + kind : "unexposed " + kind;
return (switch (exposure) {
case EXPOSED -> "exposed ";
case HIDDEN -> "unexposed ";
}) + kind;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import legacy.map.fixtures.ResourcePileImpl;
import legacy.map.fixtures.resources.CacheFixture;
import legacy.map.fixtures.resources.CultivationStatus;
import legacy.map.fixtures.resources.ExposureStatus;
import legacy.map.fixtures.resources.Grove;
import legacy.map.fixtures.resources.Meadow;
import legacy.map.fixtures.resources.Mine;
Expand Down Expand Up @@ -225,7 +226,7 @@ public static MineralVein readMineral(final StartElement element, final @Nullabl
return setImage(
new MineralVein(
getAttrWithDeprecatedForm(element, path, "kind", "mineral", warner),
getBooleanAttribute(element, path, "exposed"),
getBooleanAttribute(element, path, "exposed") ? ExposureStatus.EXPOSED : ExposureStatus.HIDDEN,
getIntegerAttribute(element, path, "dc"),
getOrGenerateID(element, warner, path, idFactory)), element, path, warner);
}
Expand Down Expand Up @@ -328,7 +329,7 @@ public static void writeMineral(final XMLStreamWriter ostream, final MineralVein
throws XMLStreamException {
writeTag(ostream, "mineral", indent, true);
writeAttributes(ostream, Pair.with("kind", obj.getKind()),
Pair.with("exposed", obj.isExposed()), Pair.with("dc", obj.getDC()),
Pair.with("exposed", obj.getExposure() == ExposureStatus.EXPOSED), Pair.with("dc", obj.getDC()),
Pair.with("id", obj.getId()));
writeImage(ostream, obj);
}
Expand Down
Loading

0 comments on commit 0704aca

Please sign in to comment.