Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Use hash map to store settler images instead of 4-dim array #733

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ env:
# Run single test targets
- TARGET=":jsettlers.logic:unitTest"
- TARGET=":jsettlers.common:test"
- TARGET=":jsettlers.network:test --info"
- TARGET=":jsettlers.graphics:test"
- TARGET=":jsettlers.network:test"
- TARGET=":jsettlers.logic:aiDifficultiesIT"
- TARGET=":jsettlers.logic:autoReplayIT"
- TARGET=":jsettlers.logic:replayValidationIT"
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ subprojects {
tasks.withType(Test) {
testLogging {
exceptionFormat = 'full'
events "PASSED", "FAILED", "SKIPPED"
}
}

Expand Down
5 changes: 5 additions & 0 deletions jsettlers.graphics/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ dependencies {
implementation project(':jsettlers.common')
implementation project(':go.graphics')
implementation 'com.google.code.gson:gson:2.8.2'

testImplementation 'org.mockito:mockito-core:2.18.3'
testImplementation 'org.hamcrest:hamcrest-library:1.3'


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,47 @@
import jsettlers.common.movable.EMovableType;
import jsettlers.graphics.image.Image;
import jsettlers.graphics.localization.Labels;
import jsettlers.graphics.map.draw.settlerimages.SettlerImageFlavor;
import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap;
import jsettlers.graphics.ui.UIPanel;

public class SelectionRow extends UIPanel {

private final EMovableType type;
private final Image movableImage;
private String localizedLabelName;
private final int count;

/**
* Creates a new row in the selection view
*
* @param type
* The type of the movables
* @param count
* How many of them are selected.
* Creates a new row in the selection view.
*
* @param movableImage
* @param localizedLabelName
* @param selectionCount how many are selected.
*/
public SelectionRow(EMovableType type, int count) {
this.type = type;
this.count = count;
public SelectionRow(Image movableImage, String localizedLabelName, int selectionCount) {
this.count = selectionCount;
this.movableImage = movableImage;
this.localizedLabelName = localizedLabelName;
}

static SelectionRow createFromMovableType(EMovableType type, int count) {
SettlerImageFlavor flavor = new SettlerImageFlavor(type, EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, EDirection.SOUTH_EAST);
return new SelectionRow(SettlerImageMap.getInstance().getImageForSettler(flavor, 0.0f), Labels.getName(type), count);
}

@Override
public void drawAt(GLDrawContext gl) {
float width = getPosition().getWidth();
Image image =
SettlerImageMap.getInstance().getImageForSettler(type,
EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL,
EDirection.SOUTH_EAST, 0);

Color color = getColor();
float bottomy = getPosition()
.getMinY() + getPosition().getHeight() / 4;
float bottomY = getPosition().getMinY() + getPosition().getHeight() / 4;
float left = getPosition().getMinX();
float imagex = left + width / 20;
image.drawAt(gl, imagex, bottomy, color);
float imageX = left + width / 20;
movableImage.drawAt(gl, imageX, bottomY, color);

TextDrawer drawer = gl.getTextDrawer(EFontSize.NORMAL);

drawer.drawString(left + width / 5, getPosition().getMinY() + getPosition().getHeight() * .75f, "" + count);
drawer.drawString(left + width / 5, bottomy, Labels.getName(type));

drawer.drawString(left + width / 5, bottomY, localizedLabelName);
}

private Color getColor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void addRowsToPanel(UIPanel panel, ISelectionSet selection, EMovab
int count = selection.getMovableCount(type);

if (count > 0) {
SelectionRow row = new SelectionRow(type, count);
SelectionRow row = SelectionRow.createFromMovableType(type, count);
panel.addChild(row, 0.1f, rowHeight * (rowi - 1), .9f,
rowHeight * (rowi));
rowi--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void addRowsToPanel(UIPanel panel, ISelectionSet selection,
int count = selection.getMovableCount(type);

if (count > 0) {
SelectionRow row = new SelectionRow(type, count);
SelectionRow row = SelectionRow.createFromMovableType(type, count);
panel.addChild(row, 0.1f, rowHeight * (rowi - 1), .9f,
rowHeight * (rowi));
rowi--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap;

public class ImagePreloadTask implements Runnable {

private final SettlerImageMap settlerImageMap;

public ImagePreloadTask(SettlerImageMap settlerImageMap) {
this.settlerImageMap = settlerImageMap;
}

@Override
public void run() {
SettlerImageMap.getInstance();
settlerImageMap.loadFromMovablesTextFile();

Background.preloadTexture();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import jsettlers.graphics.image.reader.versions.SettlersVersionMapping;
import jsettlers.graphics.image.sequence.ArraySequence;
import jsettlers.graphics.image.sequence.Sequence;
import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap;
import jsettlers.graphics.map.draw.settlerimages.SettlerImageMapItem;

import java.io.File;
import java.util.Arrays;
Expand All @@ -51,7 +53,7 @@
*
* @author michael
*/
public final class ImageProvider {
public class ImageProvider {
private static final String FILE_PREFIX = "siedler3_";
private static final int LAST_SEQUENCE_NUMBER = 2;
private static final List<Integer> HIGHRES_IMAGE_FILE_NUMBERS = Arrays.asList(3, 14);
Expand All @@ -73,7 +75,7 @@ public final class ImageProvider {
private Thread preloadingThread;
private ImageIndexFile indexFile = null;

private ImageProvider() {
public ImageProvider() {
}

/**
Expand Down Expand Up @@ -305,7 +307,7 @@ private DatFileReader createFileReader(int fileIndex) {
*/
public void startPreloading() {
if (lookupPath != null && preloadingThread == null) {
preloadingThread = new Thread(new ImagePreloadTask(), "image preloader");
preloadingThread = new Thread(new ImagePreloadTask(SettlerImageMap.getInstance()), "image preloader");
preloadingThread.start();
}
}
Expand All @@ -327,4 +329,9 @@ public void waitForPreloadingFinish() {
public void addPreloadTask(GLPreloadTask task) {
tasks.add(task);
}

public Image getImageSafe(SettlerImageMapItem item, float progress) {
return getSettlerSequence(item.getFile(), item.getSequenceIndex())
.getImageSafe(item.imageIndex(progress));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import jsettlers.graphics.image.SingleImage;
import jsettlers.graphics.image.sequence.Sequence;
import jsettlers.graphics.map.MapDrawContext;
import jsettlers.graphics.map.draw.settlerimages.SettlerImageFlavor;
import jsettlers.graphics.map.draw.settlerimages.SettlerImageMap;
import jsettlers.graphics.map.geometry.MapCoordinateConverter;
import jsettlers.graphics.sound.SoundManager;
Expand Down Expand Up @@ -375,8 +376,8 @@ private void drawShip(IMovable ship, int x, int y) {
if (yShift >= 0) {
float xShift = PASSENGER_POSITION_TO_FRONT[j] * xShiftForward + PASSENGER_POSITION_TO_RIGHT[j] * xShiftRight;
IMovable passenger = passengerList.get(j);
Image image = this.imageMap.getImageForSettler(passenger.getMovableType(), EMovableAction.NO_ACTION,
EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i), 0
Image image = this.imageMap.getImageForSettler(
new SettlerImageFlavor(passenger.getMovableType(), EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i)), 0
);
image.drawAt(glDrawContext, drawBuffer, viewX + xShift, viewY + yShift + PASSENGER_DECK_HEIGHT, color, shade);
}
Expand Down Expand Up @@ -408,8 +409,8 @@ EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i), 0
if (yShift < 0) {
float xShift = PASSENGER_POSITION_TO_FRONT[j] * xShiftForward + PASSENGER_POSITION_TO_RIGHT[j] * xShiftRight;
IMovable passenger = passengerList.get(j);
Image image = this.imageMap.getImageForSettler(passenger.getMovableType(), EMovableAction.NO_ACTION,
EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i), 0
Image image = this.imageMap.getImageForSettler(
new SettlerImageFlavor(passenger.getMovableType(), EMovableAction.NO_ACTION, EMaterialType.NO_MATERIAL, getPassengerDirection(direction, shipPosition, i)), 0
);
image.drawAt(glDrawContext, drawBuffer, viewX + xShift, viewY + yShift + PASSENGER_DECK_HEIGHT, color, shade);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*******************************************************************************
* Copyright (c) 2015 - 2018
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package jsettlers.graphics.map.draw.settlerimages;

import jsettlers.common.material.EMaterialType;
import jsettlers.common.movable.EDirection;
import jsettlers.common.movable.EMovableAction;
import jsettlers.common.movable.EMovableType;
import jsettlers.common.movable.IMovable;

import java.util.Objects;

public final class SettlerImageFlavor {

public static final SettlerImageFlavor NONE = new SettlerImageFlavor(null, null, null, null);
private final EMovableType type;
private final EMovableAction action;
private final EMaterialType material;
private final EDirection direction;

public SettlerImageFlavor(EMovableType type, EMovableAction action, EMaterialType material, EDirection direction) {
this.type = type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can ensure that the parameters are not null by adding Objects.requireNonNull() calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map contains one default entry that is defined by all parameters as null and comes from "movables.txt" file, entry ",,,=10, 0, 0, 1". * is converted to null. That's why I cannot prohibit null.

this.action = action;
this.material = material;
this.direction = direction;
}

static SettlerImageFlavor createFromMovable(IMovable movable) {
return new SettlerImageFlavor(movable.getMovableType(), movable.getAction(), movable.getMaterial(), movable.getDirection());
}

public EMovableType getType() {
return type;
}

public EMovableAction getAction() {
return action;
}

public EMaterialType getMaterial() {
return material;
}

public EDirection getDirection() {
return direction;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SettlerImageFlavor that = (SettlerImageFlavor) o;

if (type != that.type) return false;
if (action != that.action) return false;
if (material != that.material) return false;
return direction == that.direction;
}

@Override
public int hashCode() {
int result = type != null ? type.hashCode() : 0;
result = 31 * result + (action != null ? action.hashCode() : 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - java is very efficient at testing for non-null and throwing an exception if a value is null.

result = 31 * result + (material != null ? material.hashCode() : 0);
result = 31 * result + (direction != null ? direction.hashCode() : 0);
return result;
}

int calculatePriority() {
int priority = 1;// more than 0.
if (getType() != null) {
priority += 10;
}
if (getAction() != null) {
priority += 100;
}
if (getMaterial() != null) {
priority += 1000;
}
if (getDirection() != null) {
priority += 10000;
}
return priority;
}
}
Loading