Skip to content

Commit

Permalink
Merge pull request #36 from tommai78101/feature/trigger_data_identifi…
Browse files Browse the repository at this point in the history
…cation_code

Feature/trigger data identification code

- Implemented TDIC into the game and level editor.
- Updated test area maps with TDIC, generated by the level editor.
- Fixed a couple of bugs pertaining to loading area maps containing checksums.
- Integrated a way to easily convert old area maps into area maps with TDIC support by opening old area maps and overwriting them by saving them.
- Fixed some level editor bugs.
  • Loading branch information
tommai78101 authored Jul 24, 2021
2 parents fb8edcf + 488cd33 commit 1be6438
Show file tree
Hide file tree
Showing 21 changed files with 382 additions and 171 deletions.
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1627090007868</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
11 changes: 11 additions & 0 deletions common/.project
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1627090007874</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
33 changes: 33 additions & 0 deletions common/src/main/java/common/Debug.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package common;

import javax.swing.JOptionPane;

import enums.AnsiColors;

/**
Expand Down Expand Up @@ -55,6 +57,37 @@ public static void exception(Exception e) {
Debug.printColor(AnsiColors.Red, Debug.createExceptionString(e));
}

/**
* Displays an alert message box with the error message and line number.
*
* @param e
*/
public static void showExceptionCause(Exception e) {
Debug.showExceptionCause("", e);
}

/**
* Displays an alert message box with a customized message, the error message, and the line number.
*
* @param msg
* @param e
*/
public static void showExceptionCause(String msg, Exception e) {
if (msg == null) {
StackTraceElement threadStack = Thread.currentThread().getStackTrace()[2];
String threadCause = threadStack.getClassName() + ":" + threadStack.getLineNumber();
JOptionPane.showMessageDialog(null, "Debug.showExceptionCause encountered null message.\n" + threadCause);
return;
}
StackTraceElement element = e.getStackTrace()[0];
String cause = "(" + element.getFileName() + ":" + element.getLineNumber() + ")";
if (!msg.isBlank()) {
msg += "\n";
}
msg += e.getMessage() + " " + cause;
JOptionPane.showMessageDialog(null, msg);
}

// -----------------------------------------------------------------
// Private static methods and class member fields.

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/common/Sha2Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static String generateRandom(String input) {
return new BigInteger(1, messageDigest.digest()).toString(16);
}
catch (NoSuchAlgorithmException e) {
Debug.error("No such algorithm found. Returning original input string. ", e);
Debug.showExceptionCause("No such algorithm found. Returning original input string. ", e);
}
return input;
}
Expand Down Expand Up @@ -55,7 +55,7 @@ public static String generate(String input) {
return new BigInteger(1, messageDigest.digest()).toString(16);
}
catch (NoSuchAlgorithmException e) {
Debug.error("No such algorithm found. Returning original input string. ", e);
Debug.showExceptionCause("No such algorithm found. Returning original input string. ", e);
}
return input;
}
Expand Down
Binary file added common/src/main/resources/art/area/test/debug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified common/src/main/resources/art/area/test/testArea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified common/src/main/resources/art/area/test/testArea2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions game/.project
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@
<location>D:/Documents/JavaProjects/PokemonWalking/common/src/main/resources</location>
</link>
</linkedResources>
<filteredResources>
<filter>
<id>1627090007881</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
7 changes: 5 additions & 2 deletions game/src/main/java/data/GameSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import java.util.Set;

import abstracts.Character.GenderType;
import common.Debug;
import abstracts.Item;
import abstracts.SubMenu;
import common.Debug;
import data.chunk.SaveChunk;
import data.mod.AreaInfo;
import data.mod.HeaderInfo;
Expand Down Expand Up @@ -329,9 +329,9 @@ private void generateLoadData(Game game) throws Exception {
int y = (this.playerInfo.player_y[0] << 24) | (this.playerInfo.player_y[1] << 16)
| (this.playerInfo.player_y[2] << 8) | this.playerInfo.player_y[3];
gamePlayer.setAreaPosition(x, y);
gameWorld.getCurrentArea().setPlayer(gamePlayer);
gameWorld.getCurrentArea().setPlayerX(x);
gameWorld.getCurrentArea().setPlayerY(y);
gameWorld.getCurrentArea().setPlayer(gamePlayer);

// Get Player direction facing.
int facing = (this.playerInfo.player_facing[0] & 0xFF) << 24 | (this.playerInfo.player_facing[1] & 0xFF) << 16
Expand Down Expand Up @@ -496,6 +496,9 @@ public static void loadExperimental(Game game, String filename) {
catch (IOException e) {
Debug.error("Fail to load in experimental feature.", e);
}
catch (Exception e) {
Debug.error("Corrupted game save. Please delete game save, and restart.", e);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions game/src/main/java/data/chunk/PlayerChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void read(Game game, RandomAccessFile raf) throws IOException {
this.xPosition = raf.readInt();
this.yPosition = raf.readInt();
player.setAreaPosition(this.xPosition, this.yPosition);
world.getCurrentArea().setPlayer(player);
world.getCurrentArea().setPlayerX(this.xPosition);
world.getCurrentArea().setPlayerY(this.yPosition);

Expand Down
85 changes: 63 additions & 22 deletions game/src/main/java/level/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Area implements Tileable, UpdateRenderable {
private final int areaID;
private int sectorID;
private String areaName;
private String checksum;
// TODO: Add area type.
// private int areaType;

Expand All @@ -54,7 +55,7 @@ public class Area implements Tileable, UpdateRenderable {
private final List<List<PixelData>> areaData = new ArrayList<>();
private final Set<PixelData> modifiedAreaData = new HashSet<>();

private final int ReservedUsedPixelCount = 1;
private final int ReservedUsedPixelCount = 5;

// Area data hash maps.
private final Map<Map.Entry<Integer, Integer>, Obstacle> areaObstacles = new HashMap<>();
Expand All @@ -67,25 +68,61 @@ public Area(Bitmap bitmap) {
this.areaID = (tempPixels[0] >> 16) & 0xFFFF;
int triggerSize = tempPixels[0] & 0xFFFF;
int row = 0;
int column = 0;
int column = 1;
int stride = bitmap.getWidth();
for (int i = 0; i < triggerSize; i++) {
// The "color" is the ID.
// ID must not be negative. ID = 0 is reserved.
column = i + this.ReservedUsedPixelCount;
int color = tempPixels[column + (stride * row)];
if (color > 0) {
int xPosition = (color >> 24) & 0xFF;
int yPosition = (color >> 16) & 0xFF;
this.triggerDatas.put(Map.entry(xPosition, yPosition), new TriggerData().loadTriggerData(color));
}

// Get checksum first. Checksum is set immediately after the first pixel.
StringBuilder checksumBuilder = new StringBuilder();
for (; (row * stride + column) < this.ReservedUsedPixelCount;) {
int pixel = tempPixels[row * stride + column];
// There are a total of 4 bytes in an "int" type.
char ch1 = (char) ((pixel & 0xFF000000) >> 24);
char ch2 = (char) ((pixel & 0x00FF0000) >> 16);
char ch3 = (char) ((pixel & 0x0000FF00) >> 8);
char ch4 = (char) (pixel & 0x000000FF);
checksumBuilder.append(ch1).append(ch2).append(ch3).append(ch4);

column++;
if (column >= stride) {
column %= stride;
row++;
column -= stride;
}
}
this.checksum = checksumBuilder.toString();

// After checksum, the rest of the pixels in the current row are just padded pixels. Skip them until
// we reach next row.
row++;
column = 0;

// If the trigger size is larger than 1 (meaning there are triggers other than Eraser), we parse the
// trigger data.
if (triggerSize > 1) {
column++;

// TODO(2021-July-24): This is the place to insert a static method to "search for any trigger
// scripts matching the checksum" and load the trigger script into TriggerData.loadTriggerData().

for (; column < triggerSize - 1; column++) {
// The "color" is the ID.
// ID must not be negative. ID = 0 is reserved.
int color = tempPixels[column + (stride * row)];
if (color > 0) {
int xPosition = (color >> 24) & 0xFF;
int yPosition = (color >> 16) & 0xFF;
this.triggerDatas.put(Map.entry(xPosition, yPosition), new TriggerData().loadTriggerData(color));
}
if (column >= stride) {
row++;
column -= stride;
}
}
}

// We need to add the row by 1 for the last row with trailing empty trigger IDs.
row++;
column = 0;

this.width = bitmap.getWidth();
this.height = bitmap.getHeight() - row;
this.pixels = new int[this.width * this.height];
Expand Down Expand Up @@ -193,9 +230,9 @@ else if ((this.isTriggerBeingTriggered && this.trigger == null) || !this.isTrigg

// Area specific entities are updated at the end.
this.areaObstacles.forEach(
(key, obstacleValue) -> {
obstacleValue.tick();
}
(key, obstacleValue) -> {
obstacleValue.tick();
}
);
}

Expand Down Expand Up @@ -342,8 +379,8 @@ public void checkCurrentPositionDataAndSetProperties(PixelData data) {
case 0x0D: // Triggers
if (red == 0x00) { // Default starting Point
this.setPixelData(
new PixelData(0x01000000, this.currentPixelData.xPosition, this.currentPixelData.yPosition),
this.currentPixelData.xPosition, this.currentPixelData.yPosition
new PixelData(0x01000000, this.currentPixelData.xPosition, this.currentPixelData.yPosition),
this.currentPixelData.xPosition, this.currentPixelData.yPosition
);
}
break;
Expand Down Expand Up @@ -392,7 +429,7 @@ public void render(Scene screen, Graphics graphics, int xOff, int yOff) {
}
// This is for rendering exit arrows when you are in front of the exit/entrance doors.
if (x == this.player.getXInArea() && y == this.player.getYInArea()
&& ((((data.getColor() >> 24) & 0xFF) == 0x0B) || (((data.getColor() >> 24) & 0xFF) == 0x04)))
&& ((((data.getColor() >> 24) & 0xFF) == 0x0B) || (((data.getColor() >> 24) & 0xFF) == 0x04)))
this.renderExitArrow(screen, xOff, yOff, data, x, y);

// Each time the area background tile is rendered, it also updates the bitmap tick updates.
Expand All @@ -401,9 +438,9 @@ public void render(Scene screen, Graphics graphics, int xOff, int yOff) {
}
// Obstacle dialogues are rendered on top of the area background tiles.
this.areaObstacles.forEach(
(k, obstacle) -> {
obstacle.renderDialogue(screen, graphics);
}
(k, obstacle) -> {
obstacle.renderDialogue(screen, graphics);
}
);

if (this.trigger != null) {
Expand Down Expand Up @@ -681,6 +718,10 @@ else if (Entity.isItem(data)) {
return null;
}

public final String getChecksum() {
return this.checksum;
}

// --------------------- STATIC METHODS -----------------------

public static int getAreaIDFromBitmap(Bitmap bitmap) {
Expand Down
4 changes: 3 additions & 1 deletion game/src/main/java/level/OverWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,10 @@ private void handleWarpPointEvent() {
PixelData data = this.currentArea.getCurrentPixelData();
int targetAreaID = data.getTargetAreaID();
this.currentArea = WorldConstants.convertToArea(this.areas, targetAreaID);
if (this.currentArea == null)
if (this.currentArea == null) {
Debug.error("Unable to find target area to warp player to.");
return;
}
this.currentArea.setPlayer(this.player);
this.currentArea.setDefaultPosition(data);
this.invertBitmapColors = true;
Expand Down
11 changes: 11 additions & 0 deletions levelEditor/.project
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@
<location>D:/Documents/JavaProjects/PokemonWalking/common/src/main/resources</location>
</link>
</linkedResources>
<filteredResources>
<filter>
<id>1627090007889</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Loading

0 comments on commit 1be6438

Please sign in to comment.