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

fix region generation logic #81

Merged
merged 2 commits into from
Jan 1, 2021
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 @@ -35,6 +35,7 @@
import org.terasology.registry.In;
import org.terasology.registry.Share;
import org.terasology.nui.Color;
import org.terasology.world.chunks.Chunks;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -89,7 +90,6 @@ public void assignRegion(AssignRegionEvent event, EntityRef region) {
region.removeComponent(UnassignedRegionComponent.class);
}


public RegionEntitiesComponent getRegionEntitiesComponent() {
return regionEntitiesComponent;
}
Expand Down Expand Up @@ -120,15 +120,11 @@ public EntityRef get(Vector2i position) {

public EntityRef getNearest(Vector2i position) {
Map<String, EntityRef> regionEntities = regionEntitiesComponent.regionEntities;
float x = position.x();
float y = position.y();
Vector2i regionPos = new Vector2i(Math.round((x - Math.signum(x) * 16) / 32) * 32 + Math.signum(x) * 16,
Math.round((y - Math.signum(y) * 16) / 32) * 32 + Math.signum(y) * 16);
return regionEntities.get(regionPos.toString());
}
int x = Chunks.toChunkPosX(position.x) * Chunks.SIZE_X + ((Chunks.SIZE_X / 2) - 1);
int y = Chunks.toChunkPosZ(position.y) * Chunks.SIZE_Z + ((Chunks.SIZE_Z / 2) - 1);

public EntityRef getNearest(String posString) {
return getNearest(Toolbox.stringToVector2i(posString));
Vector2i regionPos = new Vector2i(x, y);
return regionEntities.get(regionPos.toString());
}

public void addCell(Vector2i position) {
Expand Down Expand Up @@ -164,13 +160,6 @@ public boolean cellIsLoaded(Vector2i position) {
return cellGrid.containsKey(getCellString(position)) && (cellGrid.get(getCellString(position)) == cellSize);
}

public boolean cellIsLoaded(String posString) {
Map<String, Integer> cellGrid =regionEntitiesComponent.cellGrid;
int cellSize = regionEntitiesComponent.cellSize;
Vector2i position = Toolbox.stringToVector2i(posString);
return cellGrid.containsKey(getCellString(position)) && (cellGrid.get(getCellString(position)) == cellSize);
}

public List<EntityRef> getRegionsInCell(Vector2i position) {
int cellSize = regionEntitiesComponent.cellSize;
List<EntityRef> regions = new ArrayList<>();
Expand Down Expand Up @@ -198,11 +187,6 @@ public List<EntityRef> getRegionsInCell(EntityRef region) {
Vector2i pos = new Vector2i(regionLocation.getLocalPosition().x(), regionLocation.getLocalPosition().z());
return getRegionsInCell(pos);
}
public List<EntityRef> getRegionsInCell(String posString) {
return getRegionsInCell(Toolbox.stringToVector2i(posString));
}



public boolean checkSidesLoadedLong(Vector2i pos) {
return (cellIsLoaded(pos.addX(3 * gridSize)) && cellIsLoaded(pos.addX(-3 * gridSize))
Expand All @@ -215,11 +199,6 @@ public boolean checkSidesLoadedLong(EntityRef region) {
return checkSidesLoadedLong(pos);
}

public boolean checkSidesLoadedLong(String posString) {
return checkSidesLoadedLong(Toolbox.stringToVector2i(posString));
}


public boolean checkSidesLoadedNear(Vector2i pos) {
return (cellIsLoaded(pos.addX(gridSize)) && cellIsLoaded(pos.addX(-gridSize))
&& cellIsLoaded(pos.addY(gridSize)) && cellIsLoaded(pos.addY(-gridSize)));
Expand Down Expand Up @@ -258,14 +237,9 @@ public void clearCell(Vector2i pos) {
region.destroy();
}
}

processed.add(pos.toString());
}

public void clearCell(String posString) {
clearCell(Toolbox.stringToVector2i(posString));
}

public void setNameTagForRegion(EntityRef region) {
if (!region.iterateComponents().iterator().hasNext()) {
logger.error("Region with no components found!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.terasology.dynamicCities.world.trees.TreeFacet;
import org.terasology.entitySystem.entity.EntityStore;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Vector2i;
import org.terasology.network.NetworkComponent;
import org.terasology.world.block.BlockRegion;
Expand Down Expand Up @@ -70,10 +69,9 @@ public void process(Region region, EntityBuffer buffer) {
entityStore.addComponent(resourceFacetComponent);
entityStore.addComponent(treeFacetComponent);

LocationComponent locationComponent = new LocationComponent(JomlUtil.from(worldRegion.center(new Vector3f())));
LocationComponent locationComponent = new LocationComponent(worldRegion.center(new Vector3f()));
entityStore.addComponent(locationComponent);


if (siteFacet.getSiteComponent() != null) {
entityStore.addComponent(siteFacet.getSiteComponent());
}
Expand All @@ -97,7 +95,7 @@ protected boolean checkCorners(BlockRegion worldRegion, BaseFieldFacet2D facet)
int counter = 0;
float[] corners = new float[5];
Vector2i[] positions = new Vector2i[5];

positions[0] = new Vector2i(max.x(), max.z());
positions[1] = new Vector2i(min.x(), min.z());
positions[2] = new Vector2i(min.x() + worldRegion.getSizeX(), min.z());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class TreeFacetComponent implements Component {

public boolean privateToOwner = true;

public final Map<String, TreeGeneratorContainer> relData = Maps.newLinkedHashMap();
public final Map<Vector3i, TreeGeneratorContainer> relData = Maps.newHashMap();
public BlockRegion relativeRegion = new BlockRegion(0,0,0);
public BlockRegion worldRegion = new BlockRegion(0,0,0);
public Vector3i center = new Vector3i();
Expand All @@ -61,11 +61,11 @@ public TreeFacetComponent(TreeFacet treeFacet) {
TreeGeneratorContainer container = new TreeGeneratorContainer(treeGen.getLeafType().toString(),
treeGen.getBarkType().toString(), treeGen.getInitialAxiom(), TreeGeneratorLSystem.class.toString(), recursiveTreeGeneratorLSystem.getMaxDepth(),
recursiveTreeGeneratorLSystem.getAngle(), recursiveTreeGeneratorLSystem.getRuleSet());
relData.put(entry.getKey().toString(), container);
relData.put(new Vector3i(entry.getKey()), container);
} else if (entry.getValue().getClass() == TreeGeneratorCactus.class) {
TreeGeneratorCactus treeGen = (TreeGeneratorCactus) entry.getValue();
TreeGeneratorContainer container = new TreeGeneratorContainer(treeGen.getCactusType().toString());
relData.put(entry.getKey().toString(), container);
relData.put(new Vector3i(entry.getKey()), container);
}


Expand All @@ -76,63 +76,63 @@ public TreeFacetComponent(TreeFacet treeFacet) {
private Rect2i copyRect2i(Rect2i value) {
return Rect2i.createFromMinAndMax(value.minX(), value.minY(), value.maxX(), value.maxY());
}

public TreeGeneratorContainer get(int x, int y, int z) {
return get(new Vector3i(x, y, z));
}


public TreeGeneratorContainer get(Vector3ic pos) {
checkRelativeCoords(pos.x(), pos.y(), pos.z());

return relData.get(pos.toString());
}


public void set(int x, int y, int z, TreeGeneratorContainer value) {
set(new Vector3i(x, y, z), value);
}


public void set(Vector3ic pos, TreeGeneratorContainer value) {
checkRelativeCoords(pos.x(), pos.y(), pos.z());

relData.put(pos.toString(), value); // TODO: consider using an immutable vector here
relData.put(new Vector3i(pos), value); // TODO: consider using an immutable vector here
}


public TreeGeneratorContainer getWorld(BaseVector3i pos) {
return getWorld(pos.x(), pos.y(), pos.z());
}


public TreeGeneratorContainer getWorld(int x, int y, int z) {
checkWorldCoords(x, y, z);

Vector3i index = worldToRelative(x, y, z);
return relData.get(index.toString());
}


public void setWorld(Vector3ic pos, TreeGeneratorContainer value) {
setWorld(pos.x(), pos.y(), pos.z(), value);
}


public void setWorld(int x, int y, int z, TreeGeneratorContainer value) {
checkWorldCoords(x, y, z);

Vector3i index = worldToRelative(x, y, z);
relData.put(index.toString(), value);
relData.put(index, value);
}

/**
* @return an unmodifiable view on the relative entries
*/
public Map<Vector3ic, TreeGeneratorContainer> getRelativeEntries() {
Map<Vector3ic, TreeGeneratorContainer> vectorMap = Maps.newLinkedHashMap();
for (Map.Entry<String, TreeGeneratorContainer> entry : relData.entrySet()) {
vectorMap.put(JomlUtil.from(Toolbox.stringToVector3i(entry.getKey())), entry.getValue());
for (Map.Entry<Vector3i, TreeGeneratorContainer> entry : relData.entrySet()) {
vectorMap.put(entry.getKey(), entry.getValue());
}
return vectorMap;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public final Vector3i relativeToWorld(int x, int y, int z) {
z - relativeRegion.minZ() + worldRegion.minZ());
}


public String toString() {
Vector3i worldMin = worldRegion.getMin(new Vector3i());
Vector3i relMin = relativeRegion.getMin(new Vector3i());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class SettlementEntityManager extends BaseComponentSystem {
@In
private EntityManager entityManager;

private EntityRef settlementEntities;
// private EntityRef settlementEntities;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out code, but I'll just remove the line while merging 👍


@In
private RegionEntityManager regionEntityManager;
Expand Down Expand Up @@ -131,8 +131,6 @@ public class SettlementEntityManager extends BaseComponentSystem {

@Override
public void postBegin() {

settlementEntities = settlementCachingSystem.getSettlementCacheEntity();
long seed = regionEntityManager.hashCode() & 0x921233;
rng = new FastRandom(seed);

Expand All @@ -142,8 +140,6 @@ public void postBegin() {
public void onWorldTimeEvent(WorldTimeEvent worldTimeEvent, EntityRef entityRef) {
if (!settlementCachingSystem.isInitialised()) {
return;
} else if (settlementEntities == null) {
settlementEntities = settlementCachingSystem.getSettlementCacheEntity();
}

cyclesLeft--;
Expand Down Expand Up @@ -228,7 +224,7 @@ public void checkZoneNeeded(CheckZoneNeededEvent event, EntityRef settlement, Cu
public boolean checkMinDistance(EntityRef siteRegion) {
Vector3f sitePos = siteRegion.getComponent(LocationComponent.class).getLocalPosition();
Vector2i pos = new Vector2i(sitePos.x(), sitePos.z());
SettlementsCacheComponent container = settlementEntities.getComponent(SettlementsCacheComponent.class);
SettlementsCacheComponent container = settlementCachingSystem.getSettlementCacheEntity().getComponent(SettlementsCacheComponent.class);
for (String vector2iString : container.settlementEntities.keySet()) {
Vector2i activePosition = Toolbox.stringToVector2i(vector2iString);
if (pos.distance(activePosition) < minDistance) {
Expand All @@ -243,7 +239,7 @@ public boolean checkMinDistanceCell(Vector2i pos) {
return true;
}

SettlementsCacheComponent container = settlementEntities.getComponent(SettlementsCacheComponent.class);
SettlementsCacheComponent container = settlementCachingSystem.getSettlementCacheEntity().getComponent(SettlementsCacheComponent.class);
for (String vector2iString : container.settlementEntities.keySet()) {
Vector2i activePosition = Toolbox.stringToVector2i(vector2iString);
if (pos.distance(activePosition) < minDistance - settlementMaxRadius) {
Expand All @@ -264,7 +260,7 @@ public boolean checkMinDistanceCell(String posString) {
* @return True if pos is not inside any settlement
*/
public boolean checkOutsideAllSettlements(Vector2i pos) {
SettlementsCacheComponent container = settlementEntities.getComponent(SettlementsCacheComponent.class);
SettlementsCacheComponent container = settlementCachingSystem.getSettlementCacheEntity().getComponent(SettlementsCacheComponent.class);
for (String vector2iString : container.settlementEntities.keySet()) {
Vector2i activePosition = Toolbox.stringToVector2i(vector2iString);
EntityRef settlement = container.settlementEntities.get(vector2iString);
Expand Down Expand Up @@ -532,7 +528,7 @@ public void growSettlement(EntityRef settlement) {
/*
Create roads between settlements
*/
SettlementsCacheComponent container = settlementEntities.getComponent(SettlementsCacheComponent.class);
SettlementsCacheComponent container = settlementCachingSystem.getSettlementCacheEntity().getComponent(SettlementsCacheComponent.class);
ImmutableVector2f source = new ImmutableVector2f(center.x, center.z);

ImmutableVector2f dest = source;
Expand Down