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

Modify filling trait via genes and breeding capability #94

Merged
merged 8 commits into from
Aug 16, 2020
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
3 changes: 2 additions & 1 deletion module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
{ "id": "CoreAssets", "minVersion": "2.0.1" },
{ "id": "Inventory", "minVersion": "1.1.0" },
{ "id": "ModuleTestingEnvironment", "minVersion": "0.2.0", "optional": true },
{ "id": "Genome", "minVersion": "1.0.0" , "optional": true }
{ "id": "Genome", "minVersion": "1.0.0", "optional": true },
{ "id": "BasicCrafting", "minVersion": "1.0.0", "optional": true }
],
"isServerSideOnly": false,
"isLibrary": true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.simpleFarming.events;

import org.terasology.entitySystem.event.Event;

/**
* Event sent to modify the filling of harvested produce.
*/
public class ModifyFilling implements Event {
private float newFilling;

public ModifyFilling(float newFilling) {
this.newFilling = newFilling;
}

public float getNewFilling() {
return newFilling;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.crafting.events.OnRecipeCrafted;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.systems.BaseComponentSystem;
Expand All @@ -31,12 +32,15 @@
import org.terasology.genome.breed.mutator.GeneMutator;
import org.terasology.genome.breed.mutator.VocabularyGeneMutator;
import org.terasology.genome.component.GenomeComponent;
import org.terasology.genome.genomeMap.GenomeMap;
import org.terasology.genome.genomeMap.SeedBasedGenomeMap;
import org.terasology.genome.system.SimpleGenomeManager;
import org.terasology.logic.common.RetainComponentsComponent;
import org.terasology.registry.In;
import org.terasology.simpleFarming.components.BushDefinitionComponent;
import org.terasology.simpleFarming.events.AddGenomeRetention;
import org.terasology.simpleFarming.events.BeforePlanted;
import org.terasology.simpleFarming.events.ModifyFilling;
import org.terasology.simpleFarming.events.ProduceCreated;
import org.terasology.simpleFarming.events.TransferGenomeEvent;
import org.terasology.utilities.random.FastRandom;
Expand Down Expand Up @@ -90,6 +94,12 @@ public void onProduceCreated(ProduceCreated event, EntityRef creator) {
}
}
produce.addOrSaveComponent(genomeComponent);

GenomeMap genomeMap =
genomeRegistry.getGenomeDefinition(produce.getComponent(GenomeComponent.class).genomeId).getGenomeMap();
float newFilling = genomeMap.getProperty("filling", produce.getComponent(GenomeComponent.class).genes,
Float.class);
produce.send(new ModifyFilling(newFilling));
}

/**
Expand All @@ -111,40 +121,70 @@ public void onBeforePlantedEvent(BeforePlanted event, EntityRef plant) {

/**
* Transfers the GenomeComponent across different stages of bush growth
*
* @param event the Transfer Genome Event
* @param bush the bush that is growing
* @param bushComponent bushComponent to check if it is a bush
* @param genomeComponent genomeComponent to check if the bush has a genomeComponent to pass on
*/
@ReceiveEvent
public void onTransferGenomeEvent(TransferGenomeEvent event, EntityRef bush, BushDefinitionComponent bushComponent, GenomeComponent genomeComponent) {
public void onTransferGenomeEvent(TransferGenomeEvent event, EntityRef bush,
BushDefinitionComponent bushComponent, GenomeComponent genomeComponent) {
event.getTransferEntity().addOrSaveComponent(genomeComponent);
}

/**
* Adds the GenomeComponent to the RetainComponentsComponent of an entity
* Event handler added to maintain Genome optional dependency
* Adds the GenomeComponent to the RetainComponentsComponent of an entity Event handler added to maintain Genome
* optional dependency
*
* @param event the AddGenomeRetention event
* @param entity the entity whose RetainComponentsComponent is to be modified
*/
@ReceiveEvent
public void addGenomeRetentionEvent(AddGenomeRetention event, EntityRef entity){
public void addGenomeRetentionEvent(AddGenomeRetention event, EntityRef entity) {
RetainComponentsComponent retainComponentsComponent = new RetainComponentsComponent();
retainComponentsComponent.components.add(GenomeComponent.class);
entity.addOrSaveComponent(retainComponentsComponent);
}

/**
* Adds genes to the crafted entity if breeding is possible.
* @param event the OnRecipeCrafted event
* @param entity the crafted entity which is to be modified
*/
@ReceiveEvent
public void onRecipeCraftedEvent(OnRecipeCrafted event, EntityRef entity) {
EntityRef ingredients[] = event.getIngredients();
if (ingredients.length != 2) {
return;
}

if (!(ingredients[0].hasComponent(GenomeComponent.class) || ingredients[1].hasComponent(GenomeComponent.class))) {
return;
}

SimpleGenomeManager genomeManager = new SimpleGenomeManager();
boolean result = genomeManager.applyBreeding(ingredients[0], ingredients[1], entity);
if (entity.hasComponent(GenomeComponent.class)) {
GenomeMap genomeMap =
genomeRegistry.getGenomeDefinition(entity.getComponent(GenomeComponent.class).genomeId).getGenomeMap();
float newFilling = genomeMap.getProperty("filling", entity.getComponent(GenomeComponent.class).genes,
Float.class);
entity.send(new ModifyFilling(newFilling));
}
}

private void addPropertyMap(EntityRef entity, String genomeId) {
SeedBasedGenomeMap genomeMap = new SeedBasedGenomeMap(worldProvider.getSeed().hashCode());
String geneVocabulary = "ABCDEFGHIJK";
GeneMutator geneMutator = new VocabularyGeneMutator(geneVocabulary);
BreedingAlgorithm continuousBreedingAlgorithm = new ContinuousBreedingAlgorithm(0.3f, geneMutator);
genomeMap.addSeedBasedProperty("filling", 0, 1, 2, Integer.class, continuousBreedingAlgorithm,
new Function<String, Integer>() {
genomeMap.addSeedBasedProperty("filling", 0, 2, 3, Float.class, continuousBreedingAlgorithm,
new Function<String, Float>() {
@Nullable
@Override
public Integer apply(@Nullable String input) {
return (input.charAt(0) - 'A' + 5);
public Float apply(@Nullable String input) {
return (input.charAt(0) - 'A' + 5f)/5f;
}
});
GenomeDefinition genomeDefinition = new GenomeDefinition(continuousBreedingAlgorithm, genomeMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.slf4j.LoggerFactory;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.event.EventPriority;
import org.terasology.genome.component.GenomeComponent;
import org.terasology.logic.common.RetainComponentsComponent;
import org.terasology.simpleFarming.components.SeedDefinitionComponent;
import org.terasology.simpleFarming.events.BeforePlanted;
import org.terasology.simpleFarming.events.OnSeedPlanted;
Expand Down Expand Up @@ -92,8 +90,6 @@ public void postBegin() {
*/
@ReceiveEvent(priority = EventPriority.PRIORITY_HIGH)
public void onSeedPlant(ActivateEvent event, EntityRef seed, SeedDefinitionComponent seedComponent) {
RetainComponentsComponent retainComponentsComponent = new RetainComponentsComponent();
retainComponentsComponent.components.add(GenomeComponent.class);
/* The item is being used but not planted */
if (event.getTargetLocation() == null || event.isConsumed()) {
return;
Expand Down