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
Changes from 3 commits
e3a6a96
dd4a437
8bebe85
e20bdb9
93b39dc
84e812e
e9d4edb
63ae1b4
5ed5de3
69ba453
998827f
b5db282
f70b5e1
86ebb0a
08e5e33
c6a859a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add |
||
Vector3i min = new Vector3i(centerBasePosition.x() - extent + 2, centerBasePosition.y() - extent, centerBasePosition.z() - extent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do |
||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a |
||
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); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.