Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chests Considering Location #129

Merged
merged 7 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public void playerMove(String playerName, PrisonEscapeLocation loc) {
playerEscaped(player);
}

if (_prison.isInRestrictedAreas(loc)) {
if (_prison.isInRestrictedArea(loc)) {
player.enteredRestrictedArea();
} else if (player.isInRestrictedArea()) {
player.leftRestrictedArea();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public class Chest implements Clickable {
private List<ItemProbability> _itemsProbability;
private boolean _isOpened;

protected Chest() {
protected Chest(String regionName) {
ConfigManager config = ConfigManager.getInstance();

this._contents = createContentsList();
this._itemsProbability = createItemsProbabilityList();
this._itemsProbability = config.getChestContents(regionName);
this._isOpened = false;
}

Expand All @@ -36,21 +38,6 @@ private List<PrisonEscapeItem> createContentsList() {
return list;
}

private List<ItemProbability> createItemsProbabilityList() {
List<ItemProbability> list = new ArrayList<>();

ConfigManager config = ConfigManager.getInstance();
double commonProbability = config.getCommonItemsProbability();
double rareProbability = config.getRareItemsProbability();

for (PrisonEscapeItem item : PrisonEscapeItem.values()) {
double probability = item.isRare() ? rareProbability : commonProbability;
list.add(new ItemProbability(item, probability));
}

return list;
}

public boolean isOpened() {
return _isOpened;
}
Expand All @@ -66,8 +53,7 @@ public void reload() {
}

private PrisonEscapeItem getRandomItem() {
double totalWeight = _itemsProbability.stream().mapToDouble(ItemProbability::getProbability).sum();
double randomValue = new Random().nextDouble(totalWeight);
double randomValue = new Random().nextDouble();

double cumulativeWeight = 0;
for (ItemProbability itemProbability : _itemsProbability) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.tiagofar78.prisonescape.bukkit.BukkitWorldEditor;
import net.tiagofar78.prisonescape.game.PrisonEscapePlayer;
import net.tiagofar78.prisonescape.game.prisonbuilding.regions.Region;
import net.tiagofar78.prisonescape.game.prisonbuilding.regions.SquaredRegion;
import net.tiagofar78.prisonescape.managers.ConfigManager;

import java.util.ArrayList;
Expand All @@ -11,13 +13,13 @@

public class PrisonBuilding {

private static final String PRISON_REGION_NAME = "PRISON";

private PrisonEscapeLocation _waitingLobbyLocation;
private PrisonEscapeLocation _prisonTopLeftCorner;
private PrisonEscapeLocation _prisonBottomRightCorner;
private Region _prison;
private List<Region> _regions;
private List<PrisonEscapeLocation> _prisionersSpawnLocations;
private List<PrisonEscapeLocation> _policeSpawnLocations;
private List<PrisonEscapeLocation> _restrictedAreasBottomRightCornerLocations;
private List<PrisonEscapeLocation> _restrictedAreasTopLeftCornerLocations;
private PrisonEscapeLocation _solitaryLocation;
private PrisonEscapeLocation _solitaryExitLocation;
private Hashtable<String, PrisonEscapeLocation> _prisionersSecretPassageLocations;
Expand All @@ -29,92 +31,106 @@ public class PrisonBuilding {
private Hashtable<String, Chest> _chests;
private List<PrisonEscapeLocation> _metalDetectorsLocations;

// #########################################
// # Constructor #
// #########################################

public PrisonBuilding(PrisonEscapeLocation reference) {
ConfigManager config = ConfigManager.getInstance();

_waitingLobbyLocation = addReferenceLocation(reference, config.getWaitingLobbyLocation());
_prisonTopLeftCorner = addReferenceLocation(reference, config.getPrisonTopLeftCornerLocation());
_prisonBottomRightCorner = addReferenceLocation(reference, config.getPrisonBottomRightCornerLocation());
PrisonEscapeLocation prisonUpperCorner = config.getPrisonUpperCornerLocation().add(reference);
PrisonEscapeLocation prisonLowerCorner = config.getPrisonLowerCornerLocation().add(reference);
_prison = new SquaredRegion(PRISON_REGION_NAME, false, prisonUpperCorner, prisonLowerCorner);

_restrictedAreasBottomRightCornerLocations = new ArrayList<>();
for (PrisonEscapeLocation loc : config.getRestrictedAreasBottomRightCornerLocations()) {
_restrictedAreasBottomRightCornerLocations.add(addReferenceLocation(reference, loc));
}
_regions = createRegionsList(reference, config.getRegions());

_restrictedAreasTopLeftCornerLocations = new ArrayList<>();
for (PrisonEscapeLocation loc : config.getRestrictedAreasTopLeftCornerLocations()) {
_restrictedAreasTopLeftCornerLocations.add(addReferenceLocation(reference, loc));
}
_waitingLobbyLocation = config.getWaitingLobbyLocation().add(reference);

_prisionersSpawnLocations = new ArrayList<>();
for (PrisonEscapeLocation loc : config.getPrisionersSpawnLocations()) {
_prisionersSpawnLocations.add(addReferenceLocation(reference, loc));
}
_prisionersSpawnLocations = createLocationsList(reference, config.getPrisionersSpawnLocations());
_policeSpawnLocations = createLocationsList(reference, config.getPoliceSpawnLocations());

_policeSpawnLocations = new ArrayList<>();
for (PrisonEscapeLocation loc : config.getPoliceSpawnLocations()) {
_policeSpawnLocations.add(addReferenceLocation(reference, loc));
}
_solitaryLocation = config.getSolitaryLocation().add(reference);
_solitaryExitLocation = config.getSolitaryExitLocation().add(reference);

_solitaryLocation = addReferenceLocation(reference, config.getSolitaryLocation());
_solitaryExitLocation = addReferenceLocation(reference, config.getSolitaryExitLocation());

_prisionersSecretPassageLocations = new Hashtable<>();
for (Entry<PrisonEscapeLocation, PrisonEscapeLocation> entry : config.getPrisionersSecretPassageLocations()
.entrySet()) {
_prisionersSecretPassageLocations.put(
addReferenceLocation(reference, entry.getKey()).createKey(),
addReferenceLocation(reference, entry.getValue())
);
}

_policeSecretPassageLocations = new Hashtable<>();
for (Entry<PrisonEscapeLocation, PrisonEscapeLocation> entry : config.getPoliceSecretPassageLocations()
.entrySet()) {
_policeSecretPassageLocations.put(
addReferenceLocation(reference, entry.getKey()).createKey(),
addReferenceLocation(reference, entry.getValue())
);
}
_prisionersSecretPassageLocations = createLocationsMap(reference, config.getPrisionersSecretPassageLocations());
_policeSecretPassageLocations = createLocationsMap(reference, config.getPoliceSecretPassageLocations());

_vaults = new ArrayList<>();
_vaultsLocations = new ArrayList<>();
for (PrisonEscapeLocation location : config.getVaultsLocations()) {
_vaultsLocations.add(addReferenceLocation(reference, location));
}
_vaultsLocations = createLocationsList(reference, config.getVaultsLocations());

_chests = new Hashtable<>();
for (PrisonEscapeLocation loc : config.getChestsLocations()) {
_chests.put(addReferenceLocation(reference, loc).createKey(), new Chest());
String regionName = getRegionName(loc);
_chests.put(loc.add(reference).createKey(), new Chest(regionName));
}

_metalDetectorsLocations = new ArrayList<>();
}

private PrisonEscapeLocation addReferenceLocation(PrisonEscapeLocation reference, PrisonEscapeLocation loc) {
return new PrisonEscapeLocation(
loc.getX() + reference.getX(),
loc.getY() + reference.getX(),
loc.getZ() + reference.getZ()
);
private List<PrisonEscapeLocation> createLocationsList(
PrisonEscapeLocation reference,
List<PrisonEscapeLocation> locs
) {
List<PrisonEscapeLocation> list = new ArrayList<>();

for (PrisonEscapeLocation loc : locs) {
list.add(loc.add(reference));
}

return list;
}

public boolean isOutsidePrison(PrisonEscapeLocation loc) {
return loc.getX() > _prisonTopLeftCorner.getX() || loc.getX() < _prisonBottomRightCorner.getX() || loc
.getY() > _prisonTopLeftCorner.getY() || loc.getY() < _prisonBottomRightCorner.getY() || loc
.getZ() > _prisonTopLeftCorner.getZ() || loc.getZ() < _prisonBottomRightCorner.getZ();
private Hashtable<String, PrisonEscapeLocation> createLocationsMap(
PrisonEscapeLocation reference,
Hashtable<PrisonEscapeLocation, PrisonEscapeLocation> locs
) {
Hashtable<String, PrisonEscapeLocation> map = new Hashtable<>();

for (Entry<PrisonEscapeLocation, PrisonEscapeLocation> entry : locs.entrySet()) {
String key = entry.getKey().add(reference).createKey();
PrisonEscapeLocation value = entry.getValue().add(reference);
map.put(key, value);
}

return map;
}

private List<Region> createRegionsList(PrisonEscapeLocation reference, List<SquaredRegion> regions) {
List<Region> list = new ArrayList<>();

for (Region region : regions) {
region.add(reference);
list.add(region);
}

return list;
}

// #########################################
// # Regions #
// #########################################

public String getRegionName(PrisonEscapeLocation location) {
for (Region region : _regions) {
if (region.contains(location)) {
return region.getName();
}
}

return null;
}

public boolean isInRestrictedAreas(PrisonEscapeLocation loc) {
for (int i = 0; i < _restrictedAreasTopLeftCornerLocations.size(); i++) {
PrisonEscapeLocation bottomRight = _restrictedAreasBottomRightCornerLocations.get(i);
PrisonEscapeLocation topLeft = _restrictedAreasTopLeftCornerLocations.get(i);
// #########################################
// # Metal Detectors #
// #########################################

if (loc.getX() > bottomRight.getX() && loc.getX() < topLeft.getX() && loc.getZ() > bottomRight.getZ() && loc
.getZ() < topLeft.getZ()) {
return true;
public boolean isInRestrictedArea(PrisonEscapeLocation loc) {
for (Region region : _regions) {
if (region.contains(loc)) {
return region.isRestricted();
}
}

return false;
}

Expand Down Expand Up @@ -204,4 +220,8 @@ public PrisonEscapeLocation getSecretPassageDestinationLocation(

return secretPassageLocations.get(location.createKey());
}

public boolean isOutsidePrison(PrisonEscapeLocation loc) {
return !_prison.contains(loc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class PrisonEscapeLocation {
private int y;
private int z;

public PrisonEscapeLocation(PrisonEscapeLocation location) {
this(location.getX(), location.getY(), location.getZ());
}

public PrisonEscapeLocation(int x, int y, int z) {
this.x = x;
this.y = y;
Expand All @@ -24,6 +28,18 @@ public int getZ() {
return this.z;
}

public PrisonEscapeLocation add(PrisonEscapeLocation location) {
return add(location.getX(), location.getY(), location.getZ());
}

public PrisonEscapeLocation add(int x, int y, int z) {
this.x += x;
this.y += y;
this.z += z;

return this;
}

public String createKey() {
return "X" + this.x + "Y" + this.y + "Z" + this.z;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.tiagofar78.prisonescape.game.prisonbuilding.regions;

import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;

import java.util.List;

public class ComplexRegion extends Region {

public ComplexRegion(String name, boolean isRestricted, List<PrisonEscapeLocation> locations) {
super(name, isRestricted);
}

@Override
public boolean contains(PrisonEscapeLocation loc) {
// TODO
return false;
}

@Override
public void add(PrisonEscapeLocation location) {
// TODO
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.tiagofar78.prisonescape.game.prisonbuilding.regions;

import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;

public interface Locatable {

public boolean contains(PrisonEscapeLocation loc);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.tiagofar78.prisonescape.game.prisonbuilding.regions;

import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;

public abstract class Region implements Locatable {

private String _name;
private boolean _isRestricted;

public Region(String name, boolean isRestricted) {
_name = name;
_isRestricted = isRestricted;
}

public String getName() {
return _name;
}

public boolean isRestricted() {
return _isRestricted;
}

public abstract void add(PrisonEscapeLocation location);

@Override
public abstract boolean contains(PrisonEscapeLocation loc);

}
Loading
Loading