Skip to content

Commit

Permalink
added wall with random cracks
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoFar78 committed Apr 16, 2024
1 parent 53a5b6c commit 9bac576
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

public class BukkitWorldEditor {

private static final int SIGN_INDEX = 1;
private static final World WORLD = Bukkit.getWorld(ConfigManager.getInstance().getWorldName());
private static final int DAY_START_TIME = 0;
private static final int NIGHT_START_TIME = 13000;

// #########################################
// # Vault #
// #########################################

private static final int SIGN_INDEX = 1;

public static void addSignAboveVault(PrisonEscapeLocation location, String text) {
Location bukkitLocation = new Location(WORLD, location.getX(), location.getY() + 1, location.getZ());
bukkitLocation.getBlock().setType(Material.OAK_WALL_SIGN);
Expand All @@ -41,6 +44,13 @@ public static void deleteVaultAndRespectiveSign(PrisonEscapeLocation location) {
signLocation.getBlock().setType(Material.AIR);
}

// ########################################
// # Time #
// ########################################

private static final int DAY_START_TIME = 0;
private static final int NIGHT_START_TIME = 13000;

public static void changeTimeToDay() {
WORLD.setTime(DAY_START_TIME);
}
Expand All @@ -49,4 +59,58 @@ public static void changeTimeToNight() {
WORLD.setTime(NIGHT_START_TIME);
}

// ########################################
// # Wall #
// ########################################

private static final Material DEFAULT_BLOCK = Material.STONE_BRICKS;
private static final Material CRACKED_BLOCK = Material.CRACKED_STONE_BRICKS;

public static void buildWall(PrisonEscapeLocation loc1, PrisonEscapeLocation loc2) {
int lowerX;
int higherX;
if (loc1.getX() >= loc2.getX()) {
higherX = loc1.getX();
lowerX = loc2.getX();
}
else {
higherX = loc2.getX();
lowerX = loc1.getX();
}

int lowerZ;
int higherZ;
if (loc1.getZ() >= loc2.getZ()) {
higherZ = loc1.getZ();
lowerZ = loc2.getZ();
}
else {
higherZ = loc2.getZ();
lowerZ = loc1.getZ();
}

int lowerY;
int higherY;
if (loc1.getY() >= loc2.getY()) {
higherY = loc1.getY();
lowerY = loc2.getY();
}
else {
higherY = loc2.getY();
lowerY = loc1.getY();
}

for (int x = lowerX; x <= higherX; x++) {
for (int z = lowerZ; z <= higherZ; z++) {
for (int y = lowerY; y <= higherY; y++) {
WORLD.getBlockAt(x, y, z).setType(DEFAULT_BLOCK);
}
}
}
}

public static void putCrackOnWall(PrisonEscapeLocation loc) {
WORLD.getBlockAt(loc.getX(), loc.getY(), loc.getZ()).setType(CRACKED_BLOCK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class PrisonBuilding {

private Hashtable<String, Chest> _chests;
private List<PrisonEscapeLocation> _metalDetectorsLocations;
private Wall _wall;

// #########################################
// # Constructor #
Expand Down Expand Up @@ -65,6 +66,9 @@ public PrisonBuilding(PrisonEscapeLocation reference) {
}

_metalDetectorsLocations = new ArrayList<>();

_wall = new Wall();
_wall.raiseFixedWall();
}

private List<PrisonEscapeLocation> createLocationsList(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package net.tiagofar78.prisonescape.game.prisonbuilding;

import java.util.List;
import java.util.Random;

import org.joml.Math;

import net.tiagofar78.prisonescape.bukkit.BukkitWorldEditor;

public class Wall {

private static final int WALL_HEIGHT = 5;
private static final int MIN_CRACK_DISTANCE = 10;
private static final int MAX_CRACK_DISTANCE = 30;

private static final char CRACK_CHAR = '#';
private static final String[] CRACK_FORMAT = {
"O###O",
"#####",
"#####",
"#####",
"O###O"
};

private List<PrisonEscapeLocation> _cornersLocations;

public Wall() {

}

public void raiseFixedWall() {
for (int i = 0; i < _cornersLocations.size() - 1; i++) {
PrisonEscapeLocation loc1 = _cornersLocations.get(i);
PrisonEscapeLocation loc2 = _cornersLocations.get(i + 1).add(0, WALL_HEIGHT - 1, 0);
BukkitWorldEditor.buildWall(loc1, loc2);
}
}

public void putRandomCracks() {
if (_cornersLocations.size() == 0) {
return;
}

int crackPos = 0;
for (int i = 0; i < _cornersLocations.size(); i++) {
PrisonEscapeLocation corner1 = _cornersLocations.get(i);
PrisonEscapeLocation corner2 = _cornersLocations.get(i + 1);

int xDiff = corner2.getX() - corner1.getX();
int xAbsDiff = Math.abs(xDiff);
int zDiff = corner2.getZ() - corner1.getZ();
int zAbsDiff = Math.abs(zDiff);

int xDirection = xDiff == 0 ? 0 : xDiff > 0 ? 1 : -1;
int zDirection = zDiff == 0 ? 0 : zDiff > 0 ? 1 : -1;

while (true) {
Random random = new Random();
crackPos += random.nextInt(MIN_CRACK_DISTANCE, MAX_CRACK_DISTANCE + 1);

int nextX = crackPos * xDirection;
int nextZ = crackPos * zDirection;
int crackLength = CRACK_FORMAT[0].length();
if (nextX + crackLength * xDirection > xAbsDiff || nextZ + crackLength * zDirection > zAbsDiff) {
crackPos = 0;
break;
}

PrisonEscapeLocation crackLoc = new PrisonEscapeLocation(corner1).add(nextX, 0, nextZ);
putCrack(crackLoc, CRACK_FORMAT, xDirection, zDirection);
crackPos += crackLength;
}
}
}

private void putCrack(PrisonEscapeLocation crackLocation, String[] crackFormat, int xDirection, int zDirection) {
int crackLength = crackFormat[0].length();
int crackHeight = crackFormat.length;

for (int l = 0; l < crackLength; l++) {
for (int h = 0; h < crackHeight; h++) {
if (crackFormat[crackHeight - 1 - h].charAt(l) == CRACK_CHAR) {
BukkitWorldEditor.putCrackOnWall(crackLocation.add(xDirection * l, h, zDirection * l));
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static ConfigManager getInstance() {
private Hashtable<PrisonEscapeLocation, PrisonEscapeLocation> _policeSecretPassageLocations;
private List<PrisonEscapeLocation> _vaultsLocations;
private List<PrisonEscapeLocation> _chestsLocations;
private List<PrisonEscapeLocation> _wallCornersLocations;

private Hashtable<String, List<ItemProbability>> _regionsChestContents;

Expand Down Expand Up @@ -102,6 +103,7 @@ public ConfigManager() {
_policeSecretPassageLocations = createLocationsMap(config, "PoliceSecretPassagesLocation");
_vaultsLocations = createLocationList(config, "VaultsLocations");
_chestsLocations = createLocationList(config, "ChestsLocations");
_wallCornersLocations = createLocationList(config, "WallCorners");

_regionsChestContents = createRegionsChestContentsMap(config);

Expand Down Expand Up @@ -344,6 +346,10 @@ public List<PrisonEscapeLocation> getChestsLocations() {
return createLocationsListCopy(_chestsLocations);
}

public List<PrisonEscapeLocation> getWallCornersLocations() {
return createLocationsListCopy(_wallCornersLocations);
}

public List<ItemProbability> getChestContents(String regionName) {
if (!_regionsChestContents.containsKey(regionName)) {
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ ChestsContents:
RegionName:
Item1Name: 0.25
Item2Name: 0.05
WallCorners:
"1":
X: 0
Y: 0
Z: 0
CommonItemsProbability: 0.8
RareItemsProbability: 0.25
ChestSize: 5

0 comments on commit 9bac576

Please sign in to comment.