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

Simple World Generator and Base / Flag Generation #64

Merged
merged 16 commits into from May 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
46 changes: 46 additions & 0 deletions src/main/java/org/terasology/las/LaSSimpleWorldGenerator.java
@@ -0,0 +1,46 @@
/*
* Copyright 2017 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.
*/
package org.terasology.las;

import org.terasology.core.world.generator.facetProviders.SeaLevelProvider;
import org.terasology.engine.SimpleUri;
import org.terasology.las.bases.BaseProvider;
import org.terasology.las.bases.BaseRasterizer;
import org.terasology.registry.In;
import org.terasology.world.generation.BaseFacetedWorldGenerator;
import org.terasology.world.generation.WorldBuilder;
import org.terasology.world.generator.RegisterWorldGenerator;
import org.terasology.world.generator.plugin.WorldGeneratorPluginLibrary;

@RegisterWorldGenerator(id = "LaSSimpleWorld", displayName = "Light and Shadow (Simple)")
public class LaSSimpleWorldGenerator extends BaseFacetedWorldGenerator {
@In
private WorldGeneratorPluginLibrary worldGeneratorPluginLibrary;

public LaSSimpleWorldGenerator(SimpleUri uri) {
super(uri);
}

@Override
protected WorldBuilder createWorld() {
return new WorldBuilder(worldGeneratorPluginLibrary)
.addProvider(new LaSSurfaceProvider())
.addProvider(new SeaLevelProvider(0))
.addProvider(new BaseProvider())
.addRasterizer(new LaSSimpleWorldRasterizer())
.addRasterizer(new BaseRasterizer());
}
}
46 changes: 46 additions & 0 deletions src/main/java/org/terasology/las/LaSSimpleWorldRasterizer.java
@@ -0,0 +1,46 @@
/*
* Copyright 2017 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.
*/
package org.terasology.las;

import org.terasology.math.ChunkMath;
import org.terasology.math.geom.Vector3i;
import org.terasology.registry.CoreRegistry;
import org.terasology.world.block.Block;
import org.terasology.world.block.BlockManager;
import org.terasology.world.chunks.CoreChunk;
import org.terasology.world.generation.Region;
import org.terasology.world.generation.WorldRasterizer;
import org.terasology.world.generation.facets.SurfaceHeightFacet;

public class LaSSimpleWorldRasterizer implements WorldRasterizer {
private Block dirt;

@Override
public void initialize() {
dirt = CoreRegistry.get(BlockManager.class).getBlock("Core:Dirt");
}

@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
SurfaceHeightFacet surfaceHeightFacet = chunkRegion.getFacet(SurfaceHeightFacet.class);
for (Vector3i position : chunkRegion.getRegion()) {
float surfaceHeight = surfaceHeightFacet.getWorld(position.x, position.z);
if (position.y < surfaceHeight) {
chunk.setBlock(ChunkMath.calcBlockPos(position), dirt);
}
}
}
}
50 changes: 50 additions & 0 deletions src/main/java/org/terasology/las/LaSSurfaceProvider.java
@@ -0,0 +1,50 @@
/*
* Copyright 2017 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.
*/
package org.terasology.las;

import org.terasology.math.geom.BaseVector2i;
import org.terasology.math.geom.Rect2i;
import org.terasology.world.generation.Border3D;
import org.terasology.world.generation.FacetProvider;
import org.terasology.world.generation.GeneratingRegion;
import org.terasology.world.generation.Produces;
import org.terasology.world.generation.facets.SurfaceHeightFacet;

@Produces(SurfaceHeightFacet.class)
public class LaSSurfaceProvider implements FacetProvider {

@Override
public void setSeed(long seed) {
}

@Override
public void process(GeneratingRegion region) {
// Create our surface height facet (we will get into borders later)
Border3D border = region.getBorderForFacet(SurfaceHeightFacet.class);
SurfaceHeightFacet facet = new SurfaceHeightFacet(region.getRegion(), border);

// Loop through every position in our 2d array
Rect2i processRegion = facet.getWorldRegion();
for (BaseVector2i position: processRegion.contents()) {
facet.setWorld(position, 10f);
}

// Pass our newly created and populated facet to the region
region.setRegionFacet(SurfaceHeightFacet.class, facet);
}


}
1 change: 0 additions & 1 deletion src/main/java/org/terasology/las/LaSWorldGenerator.java
Expand Up @@ -199,6 +199,5 @@ protected WorldBuilder createWorld() {
.addRasterizer(new LaSFloraRasterizer())
.addRasterizer(new TreeRasterizer());
return worldBuilder;
//return super.createWorld();
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/terasology/las/bases/Base.java
@@ -0,0 +1,22 @@
/*
* Copyright 2017 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.
*/
package org.terasology.las.bases;

public class Base {
public int getExtent() {
return 4; //use this to adjust base size
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/terasology/las/bases/BaseFacet.java
@@ -0,0 +1,26 @@
/*
* Copyright 2017 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.
*/
package org.terasology.las.bases;

import org.terasology.math.Region3i;
import org.terasology.world.generation.Border3D;
import org.terasology.world.generation.facets.base.SparseObjectFacet3D;

public class BaseFacet extends SparseObjectFacet3D<Base> {
public BaseFacet(Region3i targetRegion, Border3D border) {
super(targetRegion, border);
}
}
62 changes: 62 additions & 0 deletions src/main/java/org/terasology/las/bases/BaseProvider.java
@@ -0,0 +1,62 @@
/*
* Copyright 2017 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.
*/
package org.terasology.las.bases;

import org.terasology.math.TeraMath;
import org.terasology.math.geom.Rect2i;
import org.terasology.world.generation.*;
import org.terasology.world.generation.facets.SurfaceHeightFacet;

@Produces(BaseFacet.class)
@Requires(@Facet(SurfaceHeightFacet.class))
public class BaseProvider implements FacetProvider {

@Override
public void setSeed(long seed) {
}

@Override
public void process(GeneratingRegion region) {

//Don't forget you sometimes have to extend the borders.
//extendBy(top, bottom, sides) is the method used for this.

Border3D border = region.getBorderForFacet(BaseFacet.class).extendBy(0, 8, 4);
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need the extension? I removed and tested it with abnormally large bases and it seems to work.

BaseFacet facet = new BaseFacet(region.getRegion(), border);
SurfaceHeightFacet surfaceHeightFacet = region.getRegionFacet(SurfaceHeightFacet.class);

Rect2i worldRegion = surfaceHeightFacet.getWorldRegion();

for (int wz = worldRegion.minY(); wz <= worldRegion.maxY(); wz++) {
for (int wx = worldRegion.minX(); wx <= worldRegion.maxX(); wx++) {
int surfaceHeight = TeraMath.floorToInt(surfaceHeightFacet.getWorld(wx, wz));
if (surfaceHeight >= facet.getWorldRegion().minY() &&
surfaceHeight <= facet.getWorldRegion().maxY()) {
//hardcoding coordinates for base 1
if (wx == 30 && wz == 0) {
facet.setWorld(wx, surfaceHeight, wz, new Base());
}
//hardcoding coordinates for base 2
if (wx == -30 && wz == 0) {
facet.setWorld(wx, surfaceHeight, wz, new Base());
}
}
}
}

region.setRegionFacet(BaseFacet.class, facet);
}
}
77 changes: 77 additions & 0 deletions src/main/java/org/terasology/las/bases/BaseRasterizer.java
@@ -0,0 +1,77 @@
/*
* Copyright 2017 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.
*/
package org.terasology.las.bases;

import java.util.Map.Entry;

import org.terasology.math.ChunkMath;
import org.terasology.math.Region3i;
import org.terasology.math.geom.BaseVector3i;
import org.terasology.math.geom.Vector3i;
import org.terasology.registry.CoreRegistry;
import org.terasology.world.block.Block;
import org.terasology.world.block.BlockManager;
import org.terasology.world.chunks.CoreChunk;
import org.terasology.world.generation.Region;
import org.terasology.world.generation.WorldRasterizer;

public class BaseRasterizer implements WorldRasterizer {
private Block stone;
private Block redFlag;
private Block blackFlag;

@Override
public void initialize() {
stone = CoreRegistry.get(BlockManager.class).getBlock("Core:HardStone");
redFlag = CoreRegistry.get(BlockManager.class).getBlock("LightAndShadowResources:redFlag");
blackFlag = CoreRegistry.get(BlockManager.class).getBlock("LightAndShadowResources:blackFlag");
}

@Override
public void generateChunk(CoreChunk chunk, Region chunkRegion) {
BaseFacet baseFacet = chunkRegion.getFacet(BaseFacet.class);

for (Entry<BaseVector3i, Base> entry : baseFacet.getWorldEntries().entrySet()) {

Vector3i centerBasePosition = new Vector3i(entry.getKey());
int extent = entry.getValue().getExtent();
centerBasePosition.add(0, extent, 0);
Copy link
Member

Choose a reason for hiding this comment

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

Why add extent to y only to subtract it in the next line?

Vector3i min = new Vector3i(centerBasePosition.x() - extent + 2, centerBasePosition.y() - extent, centerBasePosition.z() - extent);
Copy link
Member

Choose a reason for hiding this comment

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

Why do centerBasePosition.x() - extent + 2? Shouldn't it be symmetric to z?
Do you have a rectangular base in mind? What is 2?

Vector3i max = new Vector3i(centerBasePosition.x() + extent - 2, centerBasePosition.y() - extent, centerBasePosition.z() + extent);
Region3i walls = Region3i.createFromMinMax(min, max);

// loop through each of the positions in the base
for (Vector3i newBlockPosition : walls) {
//place base stone
if (chunkRegion.getRegion().encompasses(newBlockPosition)) {
chunk.setBlock(ChunkMath.calcBlockPos(newBlockPosition), stone);
}
//place flags
if (centerBasePosition.x > 0){
if (chunkRegion.getRegion().encompasses(centerBasePosition)) {
Copy link
Member

@nihal111 nihal111 May 20, 2018

Choose a reason for hiding this comment

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

Create a Vector3i flagPosition with centerBasePosition.x(), centerBasePosition.y() - extent + 1, centerBasePosition.z() and check for the chunkRegion to encompass flagPosition

chunk.setBlock(ChunkMath.calcBlockPos(centerBasePosition.x(), centerBasePosition.y() - extent + 1, centerBasePosition.z() + 2), redFlag);
}
}
if (centerBasePosition.x < 0){
if (chunkRegion.getRegion().encompasses(centerBasePosition)) {
chunk.setBlock(ChunkMath.calcBlockPos(centerBasePosition.x(), centerBasePosition.y() - extent + 1, centerBasePosition.z() + 2), blackFlag);
}
}

}
}
}
}