Skip to content

Commit

Permalink
adds following of current gene and amount of consumed plants
Browse files Browse the repository at this point in the history
  • Loading branch information
Corvette653 committed Jan 24, 2024
1 parent 6e6d78b commit 5c2015a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/backend/model/Animal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ data class Animal(
val children: Int = 0,
val id: UUID = UUID.randomUUID(),
val parents: Pair<UUID, UUID>? = null,
val consumedPlants: Int = 0,
) : Comparable<Animal> {

val isDead by lazy { energy <= 0 }

fun rotate(): Animal = this.copy(direction = direction + genome.next())
fun turnBack(): Animal = this.copy(direction = direction.opposite)
fun grow(): Animal = this.copy(energy = energy - 1, age = age + 1)
fun eat(energy: Int): Animal = this.copy(energy = this.energy + energy)
fun eat(energy: Int): Animal = this.copy(energy = this.energy + energy, consumedPlants = this.consumedPlants + 1)

private fun decreaseEnergy(energy: Int): Animal = this.copy(energy = this.energy - energy)
private fun withChild(): Animal = this.copy(children = this.children + 1)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/backend/model/Genome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Genome(val genes: List<Gen>, private var curr: Int) : Iterator<Gen> {

override fun next(): Gen = genes[(curr++) % genes.size]

fun currentGene(): Gen = genes[curr % genes.size]

val frequencyMap by lazy { this.genes.groupingBy { it }.eachCount() }

}
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/frontend/animal/FollowedAnimalsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import backend.model.Animal
import frontend.animal.FollowedAnimalsViewModel.FollowedAnimal
import frontend.components.View
import frontend.components.readonlyColumn
import kotlinx.coroutines.flow.Flow
import frontend.components.card
import frontend.simulation.FamilyRoot
import javafx.scene.text.Text
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import tornadofx.*
Expand All @@ -33,14 +30,17 @@ class FollowedAnimalsView(
items.setAll(it)
}

readonlyColumn("X", FollowedAnimal::x) { prefWidth = 40.0; styleClass.add("centered") }
readonlyColumn("Y", FollowedAnimal::y) { prefWidth = 40.0; styleClass.add("centered") }
readonlyColumn("Energy", FollowedAnimal::energy) { prefWidth = 70.0; styleClass.add("centered") }
readonlyColumn("Genome", FollowedAnimal::genome) { prefWidth = 300.0 }
readonlyColumn("Current Gene", FollowedAnimal::currentGene) {prefWidth = 80.0; styleClass.add("centered")}
readonlyColumn("Direction", FollowedAnimal::direction) { prefWidth = 80.0; styleClass.add("centered") }
readonlyColumn("Age", FollowedAnimal::age) { prefWidth = 70.0; styleClass.add("centered") }
readonlyColumn("Children", FollowedAnimal::children) { prefWidth = 80.0; styleClass.add("centered") }
if (descendantsEnabled) readonlyColumn("Descendants", FollowedAnimal::descendants)
if (descendantsEnabled)
readonlyColumn("Descendants", FollowedAnimal::descendants) {
prefWidth = 80.0; styleClass.add("centered")
}
readonlyColumn("Consumed plants", FollowedAnimal::consumedPlants) { prefWidth = 80.0; styleClass.add("centered") }
readonlyColumn("Unfollow", FollowedAnimal::unfollowButton) { prefWidth = 80.0; styleClass.add("centered") }
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/main/kotlin/frontend/animal/FollowedAnimalsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,37 @@ class FollowedAnimalsViewModel(
val followedAnimals = combine(aliveAnimals, deadAnimals, followedIds) { aliveAnimals, deadAnimals, ids ->
aliveAnimals
.asFlow()
.flatMapMerge { (position, set) ->
.flatMapMerge { (_, set) ->
set
.asFlow()
.filter { it.id in ids }
.map { FollowedAnimal(position, it) }
.map { FollowedAnimal(it) }
}.onCompletion {
emitAll(deadAnimals
.asFlow()
.filter { it.id in ids }
.map { FollowedAnimal(null, it) }
.map { FollowedAnimal(it) }
)
}.toList()
}.toList().sortedBy { it.id }
}

inner class FollowedAnimal(
val x: Int?,
val y: Int?,
val id: UUID,
val energy: Text,
val genome: Text,
val currentGene: Text,
val direction: FontIcon,
val age: VBox,
val children: Int,
val descendants: Int?,
val consumedPlants: Int,
val unfollowButton: FontIcon,
) {

constructor(
vector: Vector?,
animal: Animal,
) : this(
x = vector?.x,
y = vector?.y,
id = animal.id,
energy = Text(animal.energy.toString()).apply {
style {
fill = when (animal.energy) {
Expand All @@ -79,6 +78,7 @@ class FollowedAnimalsViewModel(
genome = Text(animal.genome.toString().truncated(25)).apply {
tooltip { text = animal.genome.toString() }
},
currentGene = Text(animal.genome.currentGene().toString()),
direction = FontIcon(
when (animal.direction) {
N -> Material2SharpMZ.NORTH
Expand Down Expand Up @@ -106,6 +106,7 @@ class FollowedAnimalsViewModel(
},
children = animal.children,
descendants = descendantsEnabled.ifTake { familyTree.find(animal.id)?.descendants },
consumedPlants = animal.consumedPlants,
unfollowButton = FontIcon(Material2SharpAL.DELETE).apply {
onMouseClicked = EventHandler { followedIds.update { it - animal.id } }
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/frontend/simulation/SimulationViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SimulationViewModel(val simulationConfig: Config) : ViewModel() {
}

init {
combine(simulation.animals, selectedIds) { animals, ids ->
combine(simulation.aliveAnimals, selectedIds) { animals, ids ->
if (ids.isNotEmpty()) selectedAnimals.update { oldAnimals ->
animals
.asFlow()
Expand Down Expand Up @@ -109,7 +109,7 @@ class SimulationViewModel(val simulationConfig: Config) : ViewModel() {
)

fun selectAnimal(animal: AnimalModel) {
selectedIds.update { it + animal.id }
selectedIds.update { (it + animal.id).sortedBy { it } }
followedAnimalsView.openWindow(resizable = false)
}

Expand Down
9 changes: 6 additions & 3 deletions src/test/kotlin/model/AnimalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class AnimalTest : FunSpec() {
}

test("eat") {
randomAnimal.copy(energy = 10).eat(4).energy shouldBe 14
randomAnimal.copy(energy = 10).eat(4).let {
it.energy shouldBe 14
it.consumedPlants shouldBe 1
}
}

test("cover") {
Expand All @@ -65,8 +68,8 @@ class AnimalTest : FunSpec() {
result[0].energy shouldBe 50
result[1].energy shouldBe 150
result[2].energy shouldBe 200
result[0].children shouldBe setOf(result[2])
result[1].children shouldBe setOf(result[2])
result[0].children shouldBe 1
result[1].children shouldBe 1
result[2].genome shouldBe GenomeManager(Config.test).combine(parent1.genome, parent2.genome, 0.25)
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/kotlin/model/GenomeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class GenomeTest : FunSpec({
Genome(listOf(NAC, Frp, Frp, DmNotch, Frp, Frp, NAC, MDM2), 2)
}

test("currentGene") {
Genome(listOf(Frp, NAC, Frp, DmNotch, Frp, Frp, NAC, MDM2), 5).currentGene() shouldBe Frp
Genome(listOf(Frp, NAC, Frp, DmNotch, Frp, Frp, NAC, MDM2), 1).currentGene() shouldBe NAC
}

test("random gen") {
generateSequence {
Gen.random(Random)
Expand Down

0 comments on commit 5c2015a

Please sign in to comment.