Skip to content

Commit

Permalink
refactor: make ClimateConditions dependency optional
Browse files Browse the repository at this point in the history
  • Loading branch information
naalit committed Aug 16, 2021
1 parent efbf6d8 commit f5e9cea
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
3 changes: 2 additions & 1 deletion module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
{
"id": "ClimateConditions",
"minVersion": "1.0.0"
"minVersion": "1.0.0",
"optional": "true"
},
{
"id": "CoreAssets",
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/terasology/gf/PlantGrowingSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.block.BlockComponent;
import org.terasology.gf.generator.PlantGrowthDefinition;
import org.terasology.gf.util.EnvironmentLocalParameters;
import org.terasology.randomUpdate.RandomUpdateEvent;

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ public void updatePlant(DelayedActionTriggeredEvent event, EntityRef plant, Livi
PerformanceMonitor.startActivity("GrowingFlora - Updating plant");
try {
PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(plantComponent.type);
Long updateDelay = plantDefinition.requestedUpdatePlant(worldProvider, environmentSystem, blockEntityRegistry, plant);
Long updateDelay = plantDefinition.requestedUpdatePlant(worldProvider, new EnvironmentLocalParameters(environmentSystem, blockComponent.getPosition()), blockEntityRegistry, plant);
if (updateDelay != null) {
delayManager.addDelayedAction(plant, UPDATE_PLANT_ACTION_ID, updateDelay);
}
Expand All @@ -79,7 +80,7 @@ public void randomPlantUpdate(RandomUpdateEvent event, EntityRef plant, LivingPl
PerformanceMonitor.startActivity("GrowingFlora - Updating plant");
try {
PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(plantComponent.type);
if (plantDefinition.randomUpdatePlant(worldProvider, environmentSystem, blockEntityRegistry, plant)) {
if (plantDefinition.randomUpdatePlant(worldProvider, new EnvironmentLocalParameters(environmentSystem, blockComponent.getPosition()), blockEntityRegistry, plant)) {
if (delayManager.hasDelayedAction(plant, UPDATE_PLANT_ACTION_ID)) {
delayManager.cancelDelayedAction(plant, UPDATE_PLANT_ACTION_ID);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/terasology/gf/SaplingInitializeSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.block.BlockComponent;
import org.terasology.gf.generator.PlantGrowthDefinition;
import org.terasology.gf.util.EnvironmentLocalParameters;

/**
* @author Marcin Sciesinski <marcins78@gmail.com>
Expand Down Expand Up @@ -72,7 +73,7 @@ public void plantedSapling(OnAddedComponent event, EntityRef sapling, LivingPlan
Vector3i blockLocation = blockComponent.getPosition(new Vector3i());
String saplingType = livingPlant.type;
PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(saplingType);
Long updateDelay = plantDefinition.initializePlantedPlant(worldProvider, climateConditionsSystem, blockEntityRegistry, sapling);
Long updateDelay = plantDefinition.initializePlantedPlant(worldProvider, new EnvironmentLocalParameters(climateConditionsSystem, blockLocation), blockEntityRegistry, sapling);
EntityRef blockEntity = blockEntityRegistry.getBlockEntityAt(blockLocation);
if (blockEntity.hasComponent(PlantedSaplingComponent.class)) {
blockEntity.removeComponent(PlantedSaplingComponent.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package org.terasology.gf.generator;

import org.terasology.climateConditions.ClimateConditionsSystem;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.world.BlockEntityRegistry;
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.chunks.Chunk;
import org.terasology.engine.world.generation.Region;
import org.terasology.engine.world.generator.plugin.WorldGeneratorPlugin;
import org.terasology.gf.util.LocalParameters;

/**
* @author Marcin Sciesinski <marcins78@gmail.com>
Expand All @@ -45,31 +45,31 @@ public interface PlantGrowthDefinition extends WorldGeneratorPlugin {
* Returns how long to next update (if any). If null is returned, it's considered that the sapling was not initialized.
*
* @param worldProvider
* @param environmentSystem
* @param localParameters
* @param blockEntityRegistry
* @param plant @return
*/
Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant);
Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant);

/**
* Returns how long to next update (if any). If null is returned, it's considered this plant requires no more updates.
*
* @param worldProvider
* @param environmentSystem
* @param localParameters
* @param blockEntityRegistry
* @param plant @return
* @return
*/
Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant);
Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant);

/**
* Called randomly on a plant. If true is returned - this plant will no longer receive requested updates.
*
* @param worldProvider
* @param environmentSystem
* @param localParameters
* @param blockEntityRegistry
* @param plant
* @return
*/
boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant);
boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import org.joml.Vector3i;
import org.terasology.gf.util.EnvironmentLocalParameters;
import org.terasology.gf.util.GenerationLocalParameters;
import org.terasology.gf.util.LocalParameters;
import org.terasology.climateConditions.ClimateConditionsSystem;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.registry.CoreRegistry;
import org.terasology.engine.world.BlockEntityRegistry;
Expand Down Expand Up @@ -68,19 +66,17 @@ public Long initializeGeneratedPlant(WorldProvider worldProvider, BlockEntityReg
}

@Override
public Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
BlockComponent block = plant.getComponent(BlockComponent.class);
Vector3i position = block.getPosition(new Vector3i());
return growthTimeFunction.apply(new EnvironmentLocalParameters(environmentSystem, position));
public Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
return growthTimeFunction.apply(localParameters);
}

@Override
public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
BlockManager blockManager = CoreRegistry.get(BlockManager.class);
BlockComponent block = plant.getComponent(BlockComponent.class);
Vector3i position = block.getPosition(new Vector3i());

if (shouldDie(environmentSystem, position)) {
if (shouldDie(localParameters)) {
replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true);

return null;
Expand All @@ -94,7 +90,7 @@ public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsS
replaceBlock(worldProvider, blockManager, plant, position, nextStage, !hasMoreStages);

if (hasMoreStages) {
return growthTimeFunction.apply(new EnvironmentLocalParameters(environmentSystem, position));
return growthTimeFunction.apply(localParameters);
} else {
// Entered the last phase
return null;
Expand All @@ -103,12 +99,12 @@ public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsS
}

@Override
public boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
BlockManager blockManager = CoreRegistry.get(BlockManager.class);
BlockComponent block = plant.getComponent(BlockComponent.class);
Vector3i position = block.getPosition(new Vector3i());

if (shouldDie(environmentSystem, position)) {
if (shouldDie(localParameters)) {
replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true);

return true;
Expand All @@ -120,7 +116,7 @@ protected void replaceBlock(WorldProvider worldProvider, BlockManager blockManag
worldProvider.setBlock(position, blockManager.getBlock(nextStage));
}

private boolean shouldDie(ClimateConditionsSystem environmentSystem, Vector3i position) {
return deathCondition != null && deathCondition.apply(new EnvironmentLocalParameters(environmentSystem, position));
private boolean shouldDie(LocalParameters localParameters) {
return deathCondition != null && deathCondition.apply(localParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import org.joml.Vector3i;
import org.terasology.gf.util.EnvironmentLocalParameters;
import org.joml.Vector3ic;
import org.terasology.gf.util.GenerationLocalParameters;
import org.terasology.gf.util.LocalParameters;
import org.terasology.climateConditions.ClimateConditionsSystem;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.registry.CoreRegistry;
import org.terasology.engine.utilities.random.FastRandom;
Expand Down Expand Up @@ -91,7 +90,7 @@ public Long initializeGeneratedPlant(WorldProvider worldProvider, BlockEntityReg
}

@Override
public Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
if (growthIntervals.size() > 0) {
return growthIntervals.get(0);
} else {
Expand All @@ -100,19 +99,19 @@ public Long initializePlantedPlant(WorldProvider worldProvider, ClimateCondition
}

@Override
public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
BlockManager blockManager = CoreRegistry.get(BlockManager.class);
BlockComponent block = plant.getComponent(BlockComponent.class);
Vector3i position = block.getPosition(new Vector3i());
Vector3ic position = block.getPosition();

if (shouldDie(environmentSystem, position)) {
if (shouldDie(localParameters)) {
replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true);

return null;
} else {
int currentIndex = plantStages.indexOf(block.getBlock().getURI());

if (shouldGrow(plant, environmentSystem, position)) {
if (shouldGrow(plant, localParameters)) {
int nextIndex = currentIndex + 1;
BlockUri nextStage = plantStages.get(nextIndex);
final boolean hasMoreStages = nextIndex < plantStages.size() - 1;
Expand All @@ -131,31 +130,31 @@ public Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsS
}

@Override
public boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
BlockManager blockManager = CoreRegistry.get(BlockManager.class);
BlockComponent block = plant.getComponent(BlockComponent.class);
Vector3i position = block.getPosition(new Vector3i());
Vector3ic position = block.getPosition();

if (shouldDie(environmentSystem, position)) {
if (shouldDie(localParameters)) {
replaceBlock(worldProvider, blockManager, plant, position, deadPlantBlock, true);

return true;
}
return false;
}

protected void replaceBlock(WorldProvider worldProvider, BlockManager blockManager, EntityRef plant, Vector3i position, BlockUri nextStage, boolean isLast) {
protected void replaceBlock(WorldProvider worldProvider, BlockManager blockManager, EntityRef plant, Vector3ic position, BlockUri nextStage, boolean isLast) {
worldProvider.setBlock(position, blockManager.getBlock(nextStage));
}

private boolean shouldDie(ClimateConditionsSystem environmentSystem, Vector3i position) {
return deathCondition != null && deathCondition.apply(new EnvironmentLocalParameters(environmentSystem, position));
private boolean shouldDie(LocalParameters localParameters) {
return deathCondition != null && deathCondition.apply(localParameters);
}

private boolean shouldGrow(EntityRef plant, ClimateConditionsSystem environmentSystem, Vector3i position) {
private boolean shouldGrow(EntityRef plant, LocalParameters localParameters) {
float chance = 1f;
if (growthChance != null) {
chance = growthChance.apply(new EnvironmentLocalParameters(environmentSystem, position));
chance = growthChance.apply(localParameters);
}
GetGrowthChance event = new GetGrowthChance(chance);
plant.send(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
package org.terasology.gf.tree.lsystem;

import org.joml.Vector3i;
import org.terasology.climateConditions.ClimateConditionsSystem;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.world.BlockEntityRegistry;
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.chunks.Chunk;
import org.terasology.engine.world.generation.Region;
import org.terasology.gf.generator.ConnectedPlantGrowthDefinition;
import org.terasology.gf.util.LocalParameters;

import java.util.Collection;

Expand All @@ -29,17 +29,17 @@ public final Long initializeGeneratedPlant(WorldProvider worldProvider, BlockEnt
}

@Override
public Long initializePlantedPlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public Long initializePlantedPlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
return getTreeDefinition().setupPlantedSapling(plant);
}

@Override
public final Long requestedUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public final Long requestedUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
return getTreeDefinition().updateTree(worldProvider, blockEntityRegistry, plant);
}

@Override
public boolean randomUpdatePlant(WorldProvider worldProvider, ClimateConditionsSystem environmentSystem, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
public boolean randomUpdatePlant(WorldProvider worldProvider, LocalParameters localParameters, BlockEntityRegistry blockEntityRegistry, EntityRef plant) {
// Do nothing on random update
return false;
}
Expand Down

0 comments on commit f5e9cea

Please sign in to comment.