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

test: add SettlementEntityManagerTest for placeParcel #107

Merged
merged 13 commits into from
Oct 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
{
"id": "ModuleTestingEnvironment",
"minVersion": "0.2.0",
"minVersion": "0.3.2",
"optional": true
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public Optional<GenericBuildingComponent> getRandomBuildingOfZoneForCulture(Stri


private Optional<BuildingGenerator> getGenerator(String generatorName) {
Class generatorClass = GeneratorRegistry.GENERATORS.get(generatorName);
Class<?> generatorClass = GeneratorRegistry.GENERATORS.get(generatorName);
for (BuildingGenerator generator : generators) {
if (generator.getClass() == generatorClass) {
return Optional.of(generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.joml.Vector3ic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.cities.BlockTheme;
import org.terasology.cities.DefaultBlockType;
import org.terasology.cities.bldg.Building;
Expand Down Expand Up @@ -72,7 +71,6 @@
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.module.inventory.systems.InventoryManager;
import org.terasology.engine.logic.location.LocationComponent;
import org.terasology.engine.math.Side;
import org.terasology.engine.network.NetworkSystem;
Expand All @@ -89,6 +87,8 @@
import org.terasology.engine.world.block.entity.placement.PlaceBlocks;
import org.terasology.engine.world.generation.Border3D;
import org.terasology.engine.world.generation.facets.ElevationFacet;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.module.inventory.systems.InventoryManager;
import org.terasology.structureTemplates.components.SpawnBlockRegionsComponent;
import org.terasology.structureTemplates.interfaces.StructureTemplateProvider;
import org.terasology.structureTemplates.util.BlockRegionTransform;
Expand Down Expand Up @@ -554,7 +554,7 @@ public RoadStatus buildRoadParcel(RoadParcel parcel, EntityRef settlement) {

// Flatten the rect
// TODO: Find a way to store the surface height at that point to the segment here.
segment.height = flatten(segment.getRect().expand(rectExpansionFactor,new BlockArea(BlockArea.INVALID)), segmentHeight);
segment.height = flatten(segment.getRect().expand(rectExpansionFactor, new BlockArea(BlockArea.INVALID)), segmentHeight);

// Create raster targets
RasterTarget rasterTarget = new BufferRasterTarget(blockBufferSystem, roadTheme, segment.rect);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
/*
* Copyright 2016 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.dynamicCities.districts;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.dynamicCities.utilities.Toolbox;
import org.terasology.engine.entitySystem.prefab.Prefab;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.registry.In;
import org.terasology.engine.registry.Share;
import org.terasology.gestalt.assets.management.AssetManager;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -52,12 +39,7 @@ public void postBegin() {
//Get building data
if (prefab.hasComponent(DistrictType.class)) {
DistrictType districtType = prefab.getComponent(DistrictType.class);
if (!districtType.zones.isEmpty()) {
Toolbox.stringsToLowerCase(districtType.zones);
districts.add(districtType);
} else {
logger.warn("Found district prefab with empty zone list");
}
addDistrict(districtType);
}
}

Expand All @@ -73,6 +55,15 @@ public void postBegin() {
logger.info("Finished loading districts: " + districts.size() + " district types found: " + districtNames);
}

public void addDistrict(DistrictType districtType) {
if (!districtType.zones.isEmpty()) {
Toolbox.stringsToLowerCase(districtType.zones);
districts.add(districtType);
} else {
logger.warn("Found district prefab with empty zone list");
}
}

public Optional<DistrictType> getDistrictFromName(String name) {
for (DistrictType districtType : districts) {
if (districtType.name.equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.terasology.nui.Color;
import org.terasology.reflection.MappedContainer;

import java.util.Arrays;
import java.util.List;

//TODO: give mixing factors for zones
Expand All @@ -20,7 +21,12 @@ public class DistrictType implements Component<DistrictType> {
public int color;
public List<String> zones = Lists.newArrayList();

public DistrictType ( ) { }
public DistrictType() { }

public DistrictType(String name, String... zones) {
this.name = name;
this.zones.addAll(Arrays.asList(zones));
};

public boolean isValidType(DynParcel parcel) {
String zone = parcel.getZone();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
/*
* Copyright 2016 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.dynamicCities.population;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.management.AssetManager;
import org.terasology.dynamicCities.utilities.Toolbox;
import org.terasology.engine.entitySystem.prefab.Prefab;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
Expand All @@ -27,6 +13,7 @@
import org.terasology.engine.registry.In;
import org.terasology.engine.registry.Share;
import org.terasology.engine.utilities.random.MersenneRandom;
import org.terasology.gestalt.assets.management.AssetManager;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -49,27 +36,7 @@ public void postBegin() {
//Get building data
if (prefab.hasComponent(CultureComponent.class)) {
CultureComponent cultureComponent = prefab.getComponent(CultureComponent.class);
if (cultureComponent.theme != null) {
cultureComponent.theme = cultureComponent.theme.toLowerCase();
} else {
logger.warn("No theme defined for culture " + cultureComponent.name);
}
if (!cultureComponent.buildingNeedPerZone.isEmpty()) {
cultureComponents.add(cultureComponent);
cultureComponent.buildingNeedPerZone = Toolbox.stringsToLowerCase(cultureComponent.buildingNeedPerZone);
} else {
logger.warn("Found culture prefab with empty buildingNeedPerZone list");
}
if (cultureComponent.availableBuildings != null) {
Toolbox.stringsToLowerCase(cultureComponent.availableBuildings);
} else {
logger.warn("No available Buildings defined for culture " + cultureComponent.name);
}
if (cultureComponent.residentialZones != null) {
Toolbox.stringsToLowerCase(cultureComponent.residentialZones);
} else {
logger.warn("No residential zones defined for culture " + cultureComponent.name);
}
addCulture(cultureComponent);
}
}

Expand All @@ -83,13 +50,37 @@ public void postBegin() {
rng = new MersenneRandom(assetManager.hashCode() * 5 + this.hashCode());
}

public void addCulture(CultureComponent cultureComponent) {
if (cultureComponent.theme != null) {
cultureComponent.theme = cultureComponent.theme.toLowerCase();
} else {
logger.warn("No theme defined for culture " + cultureComponent.name);
}
if (!cultureComponent.buildingNeedPerZone.isEmpty()) {
cultureComponents.add(cultureComponent);
cultureComponent.buildingNeedPerZone = Toolbox.stringsToLowerCase(cultureComponent.buildingNeedPerZone);
} else {
logger.warn("Found culture prefab with empty buildingNeedPerZone list");
}
if (cultureComponent.availableBuildings != null) {
Toolbox.stringsToLowerCase(cultureComponent.availableBuildings);
} else {
logger.warn("No available Buildings defined for culture " + cultureComponent.name);
}
if (cultureComponent.residentialZones != null) {
Toolbox.stringsToLowerCase(cultureComponent.residentialZones);
} else {
logger.warn("No residential zones defined for culture " + cultureComponent.name);
}
}

public CultureComponent getRandomCulture() {
if (!cultureComponents.isEmpty()) {
int max = cultureComponents.size();
int index = rng.nextInt(max);
return (CultureComponent) cultureComponents.toArray()[index];
}
logger.error("No culture found...barbarians..." );
logger.error("No culture found...barbarians...");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,14 @@ public String toggleRegionTags(
public List<EntityRef> getRegionsInArea(BlockAreac area) {
List<EntityRef> result = new ArrayList<>();
for (Vector2ic pos : area) {
EntityRef region = getNearest(new Vector2i(pos.x(), pos.y()));
EntityRef region = getNearest(pos);

if (region == null || !region.isActive() || !region.exists()) {
logger.debug("Failed to get nearest region for " + pos.toString());
logger.debug("Failed to get nearest region for {}: {}, {}, {}",
pos, region,
region != null && region.isActive(),
region != null && region.exists()
);
} else if (!result.contains(region)) {
result.add(region);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2016 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.dynamicCities.region;

Expand Down Expand Up @@ -57,18 +44,22 @@ public void process(Region region, EntityBuffer buffer) {
if (checkCorners(worldRegion, elevationFacet)) {
RoughnessFacet roughnessFacet = region.getFacet(RoughnessFacet.class);
ResourceFacet resourceFacet = region.getFacet(ResourceFacet.class);
TreeFacet treeFacet = region.getFacet(TreeFacet.class);
SiteFacet siteFacet = region.getFacet(SiteFacet.class);
SettlementFacet settlementFacet = region.getFacet(SettlementFacet.class);

EntityStore entityStore = new EntityStore();

RoughnessFacetComponent roughnessFacetComponent = new RoughnessFacetComponent(roughnessFacet);
ResourceFacetComponent resourceFacetComponent = new ResourceFacetComponent(resourceFacet);
TreeFacetComponent treeFacetComponent = new TreeFacetComponent(treeFacet);
entityStore.addComponent(roughnessFacetComponent);
entityStore.addComponent(resourceFacetComponent);
entityStore.addComponent(treeFacetComponent);

// FIXME: is TreeFacet optional?
Copy link
Member

Choose a reason for hiding this comment

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

I would not think so. CoreWorlds as part of the core modules should always be enabled and has a TreeFacet, so I would guess if all else fails, that one should always be there, should it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know about that.

protected WorldBuilder createWorld() {
return new WorldBuilder(worldGeneratorPluginLibrary)
.addProvider(new FlatSurfaceHeightProvider(SURFACE_HEIGHT))
.addProvider(new SeaLevelProvider(SEA_LEVEL))
.addProvider(new ConstantBiomeProvider(BIOME))
.addProvider(new RoughnessProvider())
.addProvider(new SiteFacetProvider())
.addProvider(new SettlementFacetProvider())
.addProvider(new ResourceProvider())
.addEntities(new RegionEntityProvider())
.addRasterizer(new SolidRasterizer())
is a world generator that does not provide a TreeFacet.

Can entity providers use the same @Requires annotations as FacetProviders can?

If so, then this EntityProvider could declare that it does depend on having a TreeFacet. (Once MovingBlocks/Terasology#4924 is fixed.)

Copy link
Member

Choose a reason for hiding this comment

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

You're absolutely right, only because a TreeFacet exists in at least one of the modules, the world generator doesn't need to use it.

I don't know whether or not they can 🤷

TreeFacet treeFacet = region.getFacet(TreeFacet.class);
if (treeFacet != null) {
TreeFacetComponent treeFacetComponent = new TreeFacetComponent(treeFacet);
entityStore.addComponent(treeFacetComponent);
}

LocationComponent locationComponent = new LocationComponent(worldRegion.center(new Vector3f()));
entityStore.addComponent(locationComponent);
Expand Down Expand Up @@ -99,8 +90,8 @@ protected boolean checkCorners(BlockRegion worldRegion, BaseFieldFacet2D facet)

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());
positions[3] = new Vector2i(min.x(), min.z() + worldRegion.getSizeZ());
positions[2] = new Vector2i(max.x(), min.z());
positions[3] = new Vector2i(min.x(), max.z());
positions[4] = new Vector2i(worldRegion.center(new Vector3f()).x, worldRegion.center(new Vector3f()).z,
RoundingMode.FLOOR);

Expand Down