From e97770f86035630e3b211aa56a0f6bace82318e9 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Wed, 16 Sep 2020 18:45:12 -0400 Subject: [PATCH 01/11] Skeleton setup + Tuple Impl --- .../RadiationDetector.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java new file mode 100644 index 0000000..a3ca753 --- /dev/null +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -0,0 +1,89 @@ +package net.simon987.pluginradioactivecloud; + +import java.util.HashSet; + +import org.bson.Document; +import net.simon987.server.assembly.HardwareModule; +import net.simon987.server.assembly.Status; +import net.simon987.server.game.objects.ControllableUnit; + +public class RadiationDetector extends HardwareModule { + + // Need to change to whatever the last unique address is + public static final int DEFAULT_ADDRESS = 0x010F; + + /** + * Hardware ID (Should be unique) -- NEEDS TO BE CHANGED + */ + public static final char HWID = 0x010F; + + /** + * Radiation detected by cubot + */ + private double currentRadiation = 0; + + /** + * Helper class for getTiles + */ + private class Tuple { + public final int x; + public final int y; + + public Tuple(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + + if (!(o instanceof Tuple)) { + return false; + } + + Tuple t = (Tuple) o; + + return Integer.compare(x, t.x) == 0 && Integer.compare(y, t.y) == 0; + } + } + + /** + * Find tiles between two given tiles. + */ + private HashSet getTiles(int x0, int y0, int x1, int y1) { + + HashSet ret = new HashSet<>(); + double slope = (y1 - y0) / (double) (x1 - x0); + + return ret; + } + + public RadiationDetector(ControllableUnit unit) { + super(null, unit); + + // Set default values + currentRadiation = 0; + } + + public RadiationDetector(Document document, ControllableUnit cubot) { + super(document, cubot); + + // Set default values + currentRadiation = 0; + } + + @Override + public void handleInterrupt(Status status) { + + // Fill in + } + + @Override + public char getId() { + return HWID; + } + +} From f526f369c4c4c54bd3816403ef466747a1e0fb83 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Wed, 16 Sep 2020 20:03:06 -0400 Subject: [PATCH 02/11] Add getTiles() to calculate tiles between two coords --- .../RadiationDetector.java | 74 ++++++++++++++----- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java index a3ca753..e31301b 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -1,6 +1,6 @@ package net.simon987.pluginradioactivecloud; -import java.util.HashSet; +import java.util.ArrayList; import org.bson.Document; import net.simon987.server.assembly.HardwareModule; @@ -33,30 +33,64 @@ public Tuple(int x, int y) { this.x = x; this.y = y; } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - - if (!(o instanceof Tuple)) { - return false; - } - - Tuple t = (Tuple) o; - - return Integer.compare(x, t.x) == 0 && Integer.compare(y, t.y) == 0; - } } /** - * Find tiles between two given tiles. + * Find tiles between two given coordinates. + * + * @return List of tile coordinates. An empty list indicates tiles are next to + * each other. */ - private HashSet getTiles(int x0, int y0, int x1, int y1) { + public ArrayList getTiles(int x0, int y0, int x1, int y1) { + + ArrayList ret = new ArrayList<>(); + double slope; + if (x1 > x0) + slope = (y1 - y0) / (double) (x1 - x0); + else { + slope = (y0 - y1) / (double) (x0 - x1); + + // Swap values so that x0 < x1. This preps the following code where y is + // determined by adding a step value (1) to x0 till it reaches x1. + int tmp = x1; + x1 = x0; + x0 = tmp; + + tmp = y1; + y1 = y0; + y0 = tmp; + } - HashSet ret = new HashSet<>(); - double slope = (y1 - y0) / (double) (x1 - x0); + // If slope is zero or undefined, return tiles directly along the + // appropriate cardinal direction. + if (x0 == x1) { + int smaller = y0 < y1 ? y0 : y1; + int larger = y0 > y1 ? y0 : y1; + System.out.printf("%d %d", smaller, larger); + for (int i = smaller + 1; i < larger; i++) { + ret.add(new Tuple(x0, i)); + } + } else if (y0 == y1) { + int smaller = x0 < x1 ? x0 : x1; + int larger = x0 > x1 ? x0 : x1; + for (int i = smaller + 1; i < larger; i++) { + ret.add(new Tuple(i, y0)); + } + } else { + // Find all coordinates with 0.1 step + int lastX = x0; + int lastY = y0; + for (int i = x0 * 10; i < x1 * 10; i += 1) { + if (i / 10 != lastX || (int) (slope * i / 10) != lastY) { + // Update last values + lastX = i / 10; + lastY = (int) (slope * i / 10); + + // Add new values to array + ret.add(new Tuple(lastX, lastY)); + } + } + } return ret; } From 72ea92ffb3503adcff52c13fb55c341af741123a Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Wed, 16 Sep 2020 20:13:33 -0400 Subject: [PATCH 03/11] Add Euclidean Distance calculation method from coords --- .../RadiationDetector.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java index e31301b..9f40251 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -36,8 +36,13 @@ public Tuple(int x, int y) { } /** - * Find tiles between two given coordinates. + * Finds the tiles between the two tiles located at the given coordinates. The + * tiles located at the coordinates are not included in the list. * + * @param x0 x-coordinate of first point + * @param y0 y-coordinate of first point + * @param x1 x-coordinate of second point + * @param y1 y-coordinate of second point * @return List of tile coordinates. An empty list indicates tiles are next to * each other. */ @@ -95,6 +100,19 @@ public ArrayList getTiles(int x0, int y0, int x1, int y1) { return ret; } + /** + * Finds the Euclidean Distance between two coordinates. + * + * @param x0 x-coordinate of first point + * @param y0 y-coordinate of first point + * @param x1 x-coordinate of second point + * @param y1 y-coordinate of second point + * @return distance between two points + */ + public double getDistanceOfCoords(int x0, int y0, int x1, int y1) { + return Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)); + } + public RadiationDetector(ControllableUnit unit) { super(null, unit); From a3c4c333000539a3d85376f13dde07eae5585d33 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Wed, 16 Sep 2020 20:37:18 -0400 Subject: [PATCH 04/11] Add getAlphaCounts() to Radioactive interface --- .../java/net/simon987/server/game/objects/Radioactive.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java index dc1fcf1..ba7b501 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java @@ -6,5 +6,7 @@ public interface Radioactive { - + public static int getAlphaCounts(double distance) { + return (int) (1000 * 1.0 / (distance * distance)); + } } From 3c16bd3f30a0beb969a16635c7baea81e659e355 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Wed, 16 Sep 2020 21:10:39 -0400 Subject: [PATCH 05/11] Implement RadiationDetector's handleInterrupt --- .../RadiationDetector.java | 29 ++++++++++++++++++- .../server/game/objects/Radioactive.java | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java index 9f40251..1dc57e1 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -2,10 +2,14 @@ import java.util.ArrayList; +import javax.lang.model.type.UnionType; + import org.bson.Document; import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Radioactive; public class RadiationDetector extends HardwareModule { @@ -130,7 +134,30 @@ public RadiationDetector(Document document, ControllableUnit cubot) { @Override public void handleInterrupt(Status status) { - // Fill in + // Find all game entities in world + ArrayList entities = new ArrayList<>(unit.getWorld().getGameObjects()); + + // Check for alpha particles by finding Radioactive entities + int alphaParticles = 0; + for (GameObject entity : entities) { + if (entity instanceof Radioactive) { + // Calculate distance between object and cubot + double pathLength = getDistanceOfCoords(unit.getX(), unit.getY(), entity.getX(), entity.getY()); + alphaParticles += ((Radioactive) entity).getAlphaCounts(pathLength); + + // Get all tiles in between cubot and Radioactive entity + ArrayList tiles = getTiles(unit.getX(), unit.getY(), entity.getX(), entity.getY()); + for (Tuple tup : tiles) { + // If intermediary tile is blocked, reduce alphaParticles by 5 + if (unit.getWorld().isTileBlocked(tup.x, tup.y)) { + alphaParticles -= 5; + } + } + } + } + + // Save Alpha Radioactive Particles to register B + getCpu().getRegisterSet().getRegister("B").setValue(alphaParticles); } @Override diff --git a/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java index ba7b501..f0bd502 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java @@ -6,7 +6,7 @@ public interface Radioactive { - public static int getAlphaCounts(double distance) { + public default int getAlphaCounts(double distance) { return (int) (1000 * 1.0 / (distance * distance)); } } From 2aeca6c9cee0fc59bfb25976e9e76bd7271c1916 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Wed, 16 Sep 2020 21:13:08 -0400 Subject: [PATCH 06/11] Small cleanup --- .../pluginradioactivecloud/RadiationDetector.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java index 1dc57e1..d7953ef 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -13,6 +13,7 @@ public class RadiationDetector extends HardwareModule { + // NEEDS TO BE CHANGED // Need to change to whatever the last unique address is public static final int DEFAULT_ADDRESS = 0x010F; @@ -21,11 +22,6 @@ public class RadiationDetector extends HardwareModule { */ public static final char HWID = 0x010F; - /** - * Radiation detected by cubot - */ - private double currentRadiation = 0; - /** * Helper class for getTiles */ @@ -119,16 +115,10 @@ public double getDistanceOfCoords(int x0, int y0, int x1, int y1) { public RadiationDetector(ControllableUnit unit) { super(null, unit); - - // Set default values - currentRadiation = 0; } public RadiationDetector(Document document, ControllableUnit cubot) { super(document, cubot); - - // Set default values - currentRadiation = 0; } @Override From 0c3a3f06f09f0288440e12881400ac346e995cb4 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Thu, 17 Sep 2020 10:47:18 -0400 Subject: [PATCH 07/11] Add Beta and Gamma particles; added suggestions --- .../RadiationDetector.java | 37 ++++++++++++++----- .../server/game/objects/Radioactive.java | 8 ++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java index d7953ef..2855bf0 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -22,6 +22,13 @@ public class RadiationDetector extends HardwareModule { */ public static final char HWID = 0x010F; + /** + * Radiation Constants + */ + private static final int ALPHA_BLOCKED_VALUE = 5; + private static final int BETA_BLOCKED_VALUE = 2; + private static final int GAMMA_BLOCKED_VALUE = 1; + /** * Helper class for getTiles */ @@ -50,9 +57,9 @@ public ArrayList getTiles(int x0, int y0, int x1, int y1) { ArrayList ret = new ArrayList<>(); double slope; - if (x1 > x0) + if (x1 > x0) { slope = (y1 - y0) / (double) (x1 - x0); - else { + } else { slope = (y0 - y1) / (double) (x0 - x1); // Swap values so that x0 < x1. This preps the following code where y is @@ -69,15 +76,15 @@ public ArrayList getTiles(int x0, int y0, int x1, int y1) { // If slope is zero or undefined, return tiles directly along the // appropriate cardinal direction. if (x0 == x1) { - int smaller = y0 < y1 ? y0 : y1; - int larger = y0 > y1 ? y0 : y1; + int smaller = Math.min(y0, y1); + int larger = Math.max(y0, y1); System.out.printf("%d %d", smaller, larger); for (int i = smaller + 1; i < larger; i++) { ret.add(new Tuple(x0, i)); } } else if (y0 == y1) { - int smaller = x0 < x1 ? x0 : x1; - int larger = x0 > x1 ? x0 : x1; + int smaller = Math.min(x0, x1); + int larger = Math.max(x0, x1); for (int i = smaller + 1; i < larger; i++) { ret.add(new Tuple(i, y0)); } @@ -129,25 +136,37 @@ public void handleInterrupt(Status status) { // Check for alpha particles by finding Radioactive entities int alphaParticles = 0; + int betaParticles = 0; + int gammaParticles = 0; for (GameObject entity : entities) { if (entity instanceof Radioactive) { // Calculate distance between object and cubot double pathLength = getDistanceOfCoords(unit.getX(), unit.getY(), entity.getX(), entity.getY()); alphaParticles += ((Radioactive) entity).getAlphaCounts(pathLength); + betaParticles += ((Radioactive) entity).getBetaCounts(pathLength); + gammaParticles += ((Radioactive) entity).getGammaCounts(pathLength); // Get all tiles in between cubot and Radioactive entity ArrayList tiles = getTiles(unit.getX(), unit.getY(), entity.getX(), entity.getY()); for (Tuple tup : tiles) { // If intermediary tile is blocked, reduce alphaParticles by 5 if (unit.getWorld().isTileBlocked(tup.x, tup.y)) { - alphaParticles -= 5; + alphaParticles -= ALPHA_BLOCKED_VALUE; + betaParticles -= BETA_BLOCKED_VALUE; + gammaParticles -= GAMMA_BLOCKED_VALUE; } } } } - // Save Alpha Radioactive Particles to register B - getCpu().getRegisterSet().getRegister("B").setValue(alphaParticles); + // Save Alpha Radioactive Particles to register A + getCpu().getRegisterSet().getRegister("A").setValue(alphaParticles); + + // Save Beta Radioactive Particles to register B + getCpu().getRegisterSet().getRegister("B").setValue(betaParticles); + + // Save Gamma Radioactive Particles to register C + getCpu().getRegisterSet().getRegister("C").setValue(gammaParticles); } @Override diff --git a/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java index f0bd502..f113d80 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java +++ b/Server/src/main/java/net/simon987/server/game/objects/Radioactive.java @@ -9,4 +9,12 @@ public interface Radioactive { public default int getAlphaCounts(double distance) { return (int) (1000 * 1.0 / (distance * distance)); } + + public default int getBetaCounts(double distance) { + return (int) (2000 * 1.0 / (distance * distance)); + } + + public default int getGammaCounts(double distance) { + return (int) (5000 * 1.0 / (distance * distance)); + } } From 3b12e2aeca394c1c6b182d09c746ea41aa40669e Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Thu, 17 Sep 2020 22:56:06 -0400 Subject: [PATCH 08/11] Set unique HWID --- .../pluginradioactivecloud/RadiationDetector.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java index 2855bf0..ee6ed92 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -2,8 +2,6 @@ import java.util.ArrayList; -import javax.lang.model.type.UnionType; - import org.bson.Document; import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.Status; @@ -13,14 +11,15 @@ public class RadiationDetector extends HardwareModule { - // NEEDS TO BE CHANGED - // Need to change to whatever the last unique address is - public static final int DEFAULT_ADDRESS = 0x010F; + /** + * Should be unique and same as HWID + */ + public static final int DEFAULT_ADDRESS = 0x000E; /** - * Hardware ID (Should be unique) -- NEEDS TO BE CHANGED + * Hardware ID (Should be unique) */ - public static final char HWID = 0x010F; + public static final char HWID = 0x000E; /** * Radiation Constants From 678b56c2dd875f05b34836e867bd7efdd821900d Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Thu, 17 Sep 2020 23:20:39 -0400 Subject: [PATCH 09/11] Add RadioactiveWorldUtils and refactor spacing in WorldUtils --- .../simon987/biomassplugin/WorldUtils.java | 16 ++-- .../RadioactiveWorldUtils.java | 77 +++++++++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java index 61dda4b..36eb15c 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java @@ -20,8 +20,9 @@ public static ArrayList generateBlobs(World world, int minCount, in int blobCount = random.nextInt(maxCount - minCount) + minCount; ArrayList biomassBlobs = new ArrayList<>(blobCount); - //Count number of plain tiles. If there is less plain tiles than desired amount of blobs, - //set the desired amount of blobs to the plain tile count + // Count number of plain tiles. If there is less plain tiles than desired amount + // of blobs, + // set the desired amount of blobs to the plain tile count TileMap m = world.getTileMap(); int plainCount = 0; for (int y = 0; y < world.getWorldSize(); y++) { @@ -37,16 +38,15 @@ public static ArrayList generateBlobs(World world, int minCount, in blobCount = plainCount; } - outerLoop: - for (int i = 0; i < blobCount; i++) { + outerLoop: for (int i = 0; i < blobCount; i++) { Point p = m.getRandomTile(TilePlain.ID); if (p != null) { - //Don't block worlds + // Don't block worlds int counter = 0; - while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || - world.getGameObjectsAt(p.x, p.y).size() != 0) { + while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 + || world.getGameObjectsAt(p.x, p.y).size() != 0) { p = m.getRandomTile(TilePlain.ID); counter++; @@ -57,7 +57,7 @@ public static ArrayList generateBlobs(World world, int minCount, in for (BiomassBlob biomassBlob : biomassBlobs) { if (biomassBlob.getX() == p.x && biomassBlob.getY() == p.y) { - //There is already a blob here + // There is already a blob here continue outerLoop; } } diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java new file mode 100644 index 0000000..a3107bf --- /dev/null +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java @@ -0,0 +1,77 @@ +package net.simon987.pluginradioactivecloud; + +import net.simon987.server.game.world.TileMap; +import net.simon987.server.game.world.TilePlain; +import net.simon987.server.game.world.World; +import org.bson.types.ObjectId; + +import java.awt.*; +import java.util.ArrayList; +import java.util.Random; + +public class RadioactiveWorldUtils { + + /** + * Generate a list of biomass blobs for a world + */ + public static ArrayList generateRadioactiveObstacles(World world, int minCount, int maxCount) { + + Random random = new Random(); + int radioactiveObjCount = random.nextInt(maxCount - minCount) + minCount; + ArrayList radioactiveObstacles = new ArrayList<>(radioactiveObjCount); + + // Count number of plain tiles. If there is less plain tiles than desired amount + // of radioactive objects, set the desired amount of radioactive objects to the + // plain tile count + TileMap m = world.getTileMap(); + int plainCount = 0; + for (int y = 0; y < world.getWorldSize(); y++) { + for (int x = 0; x < world.getWorldSize(); x++) { + + if (m.getTileIdAt(x, y) == TilePlain.ID) { + plainCount++; + } + } + } + + if (radioactiveObjCount > plainCount) { + radioactiveObjCount = plainCount; + } + + outerLoop: for (int i = 0; i < radioactiveObjCount; i++) { + + Point p = m.getRandomTile(TilePlain.ID); + if (p != null) { + + // Don't block worlds + int counter = 0; + while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 + || world.getGameObjectsAt(p.x, p.y).size() != 0) { + p = m.getRandomTile(TilePlain.ID); + counter++; + + if (counter > 25) { + continue outerLoop; + } + } + + for (RadioactiveObstacle radioactiveObstacle : radioactiveObstacles) { + if (radioactiveObstacle.getX() == p.x && radioactiveObstacle.getY() == p.y) { + // There is already a blob here + continue outerLoop; + } + } + + RadioactiveObstacle radioactiveObstacle = new RadioactiveObstacle(); + radioactiveObstacle.setObjectId(new ObjectId()); + radioactiveObstacle.setX(p.x); + radioactiveObstacle.setY(p.y); + radioactiveObstacle.setWorld(world); + + radioactiveObstacles.add(radioactiveObstacle); + } + } + + return radioactiveObstacles; + } +} From a92256008c8c1ca241bb4cdc3c32694098ef26a1 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Thu, 17 Sep 2020 23:47:19 -0400 Subject: [PATCH 10/11] Added Radioactive Obstacle to map with WorldCreationListener --- .../event/WorldCreationListener.java | 32 +++++++++++++++++++ Server/src/main/resources/config.properties | 2 ++ 2 files changed, 34 insertions(+) create mode 100644 Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/event/WorldCreationListener.java diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/event/WorldCreationListener.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/event/WorldCreationListener.java new file mode 100644 index 0000000..1876fc3 --- /dev/null +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/event/WorldCreationListener.java @@ -0,0 +1,32 @@ +package net.simon987.pluginradioactivecloud.event; + +import net.simon987.pluginradioactivecloud.RadioactiveObstacle; +import net.simon987.pluginradioactivecloud.RadioactiveWorldUtils; +import net.simon987.server.GameServer; +import net.simon987.server.event.GameEvent; +import net.simon987.server.event.GameEventListener; +import net.simon987.server.event.WorldGenerationEvent; + +import java.util.ArrayList; + +public class WorldCreationListener implements GameEventListener { + @Override + public Class getListenedEventType() { + return WorldGenerationEvent.class; + } + + @Override + public void handle(GameEvent event) { + + int minCount = GameServer.INSTANCE.getConfig().getInt("min_radioactive_obstacle_count"); + int maxCount = GameServer.INSTANCE.getConfig().getInt("max_radioactive_obstacle_count"); + + ArrayList radioactiveObstacles = RadioactiveWorldUtils + .generateRadioactiveObstacles(((WorldGenerationEvent) event).getWorld(), minCount, maxCount); + + for (RadioactiveObstacle radioactiveObstacle : radioactiveObstacles) { + ((WorldGenerationEvent) event).getWorld().addObject(radioactiveObstacle); + } + + } +} diff --git a/Server/src/main/resources/config.properties b/Server/src/main/resources/config.properties index 7bf0cf6..5509ffe 100644 --- a/Server/src/main/resources/config.properties +++ b/Server/src/main/resources/config.properties @@ -94,6 +94,8 @@ electric_box_energy_given=70 #RadioactiveObstacle radioactive_obstacle_corruption_block_size=10 radioactive_cloud_corruption_block_size=40 +min_radioactive_obstacle_count=0 +max_radioactive_obstacle_count=1 #SecretKey secret_key= From 2ee98a843974623c113d5c33a003f25ba60230e3 Mon Sep 17 00:00:00 2001 From: Khalid Ali Date: Fri, 18 Sep 2020 10:07:57 -0400 Subject: [PATCH 11/11] Added getCount() method to World --- .../simon987/biomassplugin/WorldUtils.java | 10 +- .../RadioactiveWorldUtils.java | 12 +-- .../net/simon987/server/game/world/World.java | 102 ++++++++++-------- 3 files changed, 63 insertions(+), 61 deletions(-) diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java index 36eb15c..77ebe82 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java @@ -24,15 +24,7 @@ public static ArrayList generateBlobs(World world, int minCount, in // of blobs, // set the desired amount of blobs to the plain tile count TileMap m = world.getTileMap(); - int plainCount = 0; - for (int y = 0; y < world.getWorldSize(); y++) { - for (int x = 0; x < world.getWorldSize(); x++) { - - if (m.getTileIdAt(x, y) == TilePlain.ID) { - plainCount++; - } - } - } + int plainCount = world.getCount(TilePlain.ID); if (blobCount > plainCount) { blobCount = plainCount; diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java index a3107bf..9a78045 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java @@ -12,7 +12,7 @@ public class RadioactiveWorldUtils { /** - * Generate a list of biomass blobs for a world + * Generate a list of radioactive obstacles for a world */ public static ArrayList generateRadioactiveObstacles(World world, int minCount, int maxCount) { @@ -24,15 +24,7 @@ public static ArrayList generateRadioactiveObstacles(World // of radioactive objects, set the desired amount of radioactive objects to the // plain tile count TileMap m = world.getTileMap(); - int plainCount = 0; - for (int y = 0; y < world.getWorldSize(); y++) { - for (int x = 0; x < world.getWorldSize(); x++) { - - if (m.getTileIdAt(x, y) == TilePlain.ID) { - plainCount++; - } - } - } + int plainCount = world.getCount(TilePlain.ID); if (radioactiveObjCount > plainCount) { radioactiveObjCount = plainCount; diff --git a/Server/src/main/java/net/simon987/server/game/world/World.java b/Server/src/main/java/net/simon987/server/game/world/World.java index 3e73cb2..7820233 100644 --- a/Server/src/main/java/net/simon987/server/game/world/World.java +++ b/Server/src/main/java/net/simon987/server/game/world/World.java @@ -61,7 +61,8 @@ public TileMap getTileMap() { } /** - * Check if a tile is blocked, either by a game object or an impassable tile type + * Check if a tile is blocked, either by a game object or an impassable tile + * type */ public boolean isTileBlocked(int x, int y) { return getGameObjectsBlockingAt(x, y).size() > 0 || tileMap.getTileAt(x, y).isBlocked(); @@ -70,8 +71,8 @@ public boolean isTileBlocked(int x, int y) { /** * Computes the world's unique id from its coordinates. * - * @param x the x coordinate of the world - * @param y the y coordinate of the world + * @param x the x coordinate of the world + * @param y the y coordinate of the world * * @return long */ @@ -84,7 +85,7 @@ public static String idFromCoordinates(int x, int y, String dimension) { * * @return long */ - public String getId(){ + public String getId() { return World.idFromCoordinates(x, y, dimension); } @@ -136,22 +137,22 @@ public GameObject findObject(ObjectId objectId) { } /** - * Update this World and its GameObjects - *
+ * Update this World and its GameObjects
* The update is handled by plugins first */ public void update() { - //Dispatch update event + // Dispatch update event GameEvent event = new WorldUpdateEvent(this); - GameServer.INSTANCE.getEventDispatcher().dispatch(event); //Ignore cancellation + GameServer.INSTANCE.getEventDispatcher().dispatch(event); // Ignore cancellation for (GameObject object : gameObjects.values()) { - //Clean up dead objects + // Clean up dead objects if (object.isDead()) { if (!object.onDeadCallback()) { removeObject(object); - //LogManager.LOGGER.fine("Removed object " + object + " id: " + object.getObjectId()); + // LogManager.LOGGER.fine("Removed object " + object + " id: " + + // object.getObjectId()); } else if (object instanceof Updatable) { ((Updatable) object).update(); } @@ -182,7 +183,7 @@ public Document mongoSerialise() { dbObject.put("size", worldSize); dbObject.put("updatable", updatable); - dbObject.put("shouldUpdate",shouldUpdate()); + dbObject.put("shouldUpdate", shouldUpdate()); return dbObject; } @@ -231,17 +232,17 @@ public static World deserialize(Document dbObject) { } /** - * Get a binary representation of the map as an array of 16-bit bit fields, one word for each - * tile. + * Get a binary representation of the map as an array of 16-bit bit fields, one + * word for each tile. *

- * Each tile is represented as such: OOOOOOOOTTTTTTTB where O is the object, - * T the tile and B if the tile is blocked or not + * Each tile is represented as such: OOOOOOOOTTTTTTTB where O is + * the object, T the tile and B if the tile is blocked or not */ public char[][] getMapInfo() { char[][] mapInfo = new char[worldSize][worldSize]; - //Tile + // Tile for (int x = 0; x < worldSize; x++) { for (int y = 0; y < worldSize; y++) { Tile tile = tileMap.getTileAt(x, y); @@ -252,7 +253,8 @@ public char[][] getMapInfo() { } for (GameObject obj : gameObjects.values()) { - //Overwrite, only the last object on a tile is considered but the blocked bit is kept + // Overwrite, only the last object on a tile is considered but the blocked bit + // is kept mapInfo[obj.getX()][obj.getY()] &= 0x00FE; mapInfo[obj.getX()][obj.getY()] |= obj.getMapInfo(); } @@ -261,12 +263,11 @@ public char[][] getMapInfo() { } /** - * Get a random tile that is empty and passable - * The function ensures that a object spawned there will not be trapped - * and will be able to leave the World + * Get a random tile that is empty and passable The function ensures that a + * object spawned there will not be trapped and will be able to leave the World *
- * Note: This function is quite expensive and shouldn't be used - * by some HardwareModule in its current state + * Note: This function is quite expensive and shouldn't be used by some + * HardwareModule in its current state * * @return random non-blocked tile */ @@ -277,7 +278,7 @@ public Point getRandomPassableTile() { while (true) { counter++; - //Prevent infinite loop + // Prevent infinite loop if (counter >= 1000) { return null; } @@ -318,10 +319,9 @@ public ArrayList getGameObjectsBlockingAt(int x, int y) { } /** - * Get the list of game objects that are exactly at a given location - *
- * Note: Objects like the Factory that are more than 1x1 tiles wide will only be returned - * when their exact coordinates are specified + * Get the list of game objects that are exactly at a given location
+ * Note: Objects like the Factory that are more than 1x1 tiles wide will only be + * returned when their exact coordinates are specified * * @param x X coordinate on the World * @param y Y coordinate on the World @@ -355,29 +355,28 @@ public int getWorldSize() { return worldSize; } - private GameUniverse universe = null; - public void setUniverse(GameUniverse universe){ + public void setUniverse(GameUniverse universe) { this.universe = universe; } private ArrayList getNeighbouringLoadedWorlds() { ArrayList neighbouringWorlds = new ArrayList<>(); - if (universe == null){ + if (universe == null) { return neighbouringWorlds; } - for (int dx=-1; dx<=+1; dx+=2){ + for (int dx = -1; dx <= +1; dx += 2) { World nw = universe.getLoadedWorld(x + dx, y, dimension); - if (nw != null){ + if (nw != null) { neighbouringWorlds.add(nw); } } - for (int dy=-1; dy<=+1; dy+=2){ + for (int dy = -1; dy <= +1; dy += 2) { World nw = universe.getLoadedWorld(x, y + dy, dimension); - if (nw != null){ + if (nw != null) { neighbouringWorlds.add(nw); } } @@ -385,14 +384,14 @@ private ArrayList getNeighbouringLoadedWorlds() { return neighbouringWorlds; } - public boolean canUnload(){ - return updatable==0; + public boolean canUnload() { + return updatable == 0; } - public boolean shouldUnload(){ + public boolean shouldUnload() { boolean res = canUnload(); - for (World nw : getNeighbouringLoadedWorlds() ){ + for (World nw : getNeighbouringLoadedWorlds()) { res &= nw.canUnload(); } @@ -403,6 +402,24 @@ public Collection getGameObjects() { return gameObjects.values(); } + /** + * Find the number of tiles of a given type on the world. + * + * @param tileId id of tile + * @return number of occurrences + */ + public int getCount(int tileId) { + int tileCount = 0; + for (int y = 0; y < getWorldSize(); y++) { + for (int x = 0; x < getWorldSize(); x++) { + if (tileMap.getTileIdAt(x, y) == tileId) { + tileCount++; + } + } + } + + return tileCount; + } /** * Get a random tile with N adjacent non-blocked tile @@ -412,12 +429,12 @@ public Collection getGameObjects() { */ public Point getRandomTileWithAdjacent(int n, int tile) { int counter = 0; - int[] xPositions = {1, 0, -1, 0, 1, -1, 1, -1}; - int[] yPositions = {0, 1, 0, -1, 1, 1, -1, -1}; + int[] xPositions = { 1, 0, -1, 0, 1, -1, 1, -1 }; + int[] yPositions = { 0, 1, 0, -1, 1, 1, -1, -1 }; while (true) { counter++; - //Prevent infinite loop + // Prevent infinite loop if (counter >= 2500) { return null; } @@ -428,7 +445,8 @@ public Point getRandomTileWithAdjacent(int n, int tile) { int adjacentTiles = 0; for (int idx = 0; idx < xPositions.length; idx++) { - if (tileMap.isInBounds(rTile.x + xPositions[idx], rTile.y + yPositions[idx]) && !isTileBlocked(rTile.x + xPositions[idx], rTile.y + yPositions[idx])) { + if (tileMap.isInBounds(rTile.x + xPositions[idx], rTile.y + yPositions[idx]) + && !isTileBlocked(rTile.x + xPositions[idx], rTile.y + yPositions[idx])) { adjacentTiles++; } }