Added getCount() method to World

This commit is contained in:
Khalid Ali 2020-09-18 10:07:57 -04:00
parent a92256008c
commit 2ee98a8439
3 changed files with 63 additions and 61 deletions

View File

@ -24,15 +24,7 @@ public class WorldUtils {
// 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;

View File

@ -12,7 +12,7 @@ import java.util.Random;
public class RadioactiveWorldUtils {
/**
* Generate a list of biomass blobs for a world
* Generate a list of radioactive obstacles for a world
*/
public static ArrayList<RadioactiveObstacle> generateRadioactiveObstacles(World world, int minCount, int maxCount) {
@ -24,15 +24,7 @@ public class RadioactiveWorldUtils {
// 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;

View File

@ -61,7 +61,8 @@ public class World implements MongoSerializable {
}
/**
* 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();
@ -136,8 +137,7 @@ public class World implements MongoSerializable {
}
/**
* Update this World and its GameObjects
* <br>
* Update this World and its GameObjects <br>
* The update is handled by plugins first
*/
public void update() {
@ -151,7 +151,8 @@ public class World implements MongoSerializable {
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();
}
@ -231,11 +232,11 @@ public class World implements MongoSerializable {
}
/**
* 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.
* <p>
* Each tile is represented as such: <code>OOOOOOOOTTTTTTTB</code> where O is the object,
* T the tile and B if the tile is blocked or not
* Each tile is represented as such: <code>OOOOOOOOTTTTTTTB</code> where O is
* the object, T the tile and B if the tile is blocked or not
*/
public char[][] getMapInfo() {
@ -252,7 +253,8 @@ public class World implements MongoSerializable {
}
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 class World implements MongoSerializable {
}
/**
* 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
* <br>
* 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
*/
@ -318,10 +319,9 @@ public class World implements MongoSerializable {
}
/**
* Get the list of game objects that are exactly at a given location
* <br>
* 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 <br>
* 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,7 +355,6 @@ public class World implements MongoSerializable {
return worldSize;
}
private GameUniverse universe = null;
public void setUniverse(GameUniverse universe) {
@ -403,6 +402,24 @@ public class World implements MongoSerializable {
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
@ -428,7 +445,8 @@ public class World implements MongoSerializable {
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++;
}
}