From 14a3b23e43c6b9df6f149d1803cb1c0446e9a9c5 Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 2 Jul 2023 21:10:29 +0800 Subject: [PATCH] fix: swarm --- .../projection/swarm/ProjectedPersonAI.kt | 75 +++++++++++-------- .../projection/swarm/ProjectedPersonEntity.kt | 13 +++- common/src/main/kotlin/quaedam/utils/Brain.kt | 14 ++++ 3 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 common/src/main/kotlin/quaedam/utils/Brain.kt diff --git a/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonAI.kt b/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonAI.kt index ee03cd2..9ed5a76 100644 --- a/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonAI.kt +++ b/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonAI.kt @@ -1,7 +1,6 @@ package quaedam.projection.swarm import com.google.common.collect.ImmutableList -import com.mojang.datafixers.util.Pair import net.minecraft.world.entity.ai.Brain import net.minecraft.world.entity.ai.behavior.* import net.minecraft.world.entity.ai.memory.MemoryModuleType @@ -10,6 +9,8 @@ import net.minecraft.world.entity.schedule.Activity import net.minecraft.world.entity.schedule.Schedule import net.minecraft.world.entity.schedule.ScheduleBuilder import quaedam.Quaedam +import quaedam.utils.weight +import quaedam.utils.weightR object ProjectedPersonAI { @@ -21,14 +22,15 @@ object ProjectedPersonAI { MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM, MemoryModuleType.HURT_BY, - MemoryModuleType.ATTACK_COOLING_DOWN + MemoryModuleType.ATTACK_COOLING_DOWN, + MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE, ) private val sensorTypes = listOf( SensorType.NEAREST_LIVING_ENTITIES, SensorType.NEAREST_PLAYERS, SensorType.HURT_BY, - SensorType.NEAREST_ITEMS + SensorType.NEAREST_ITEMS, ) val defaultSchedule = Quaedam.schedule.register("projected_person_default") { @@ -80,52 +82,61 @@ object ProjectedPersonAI { private fun initCoreActivity(brain: Brain) { brain.addActivity( - Activity.CORE, 0, ImmutableList.of( - Swim(0.8f), - InteractWithDoor.create(), - LookAtTargetSink(40, 70), - MoveToTargetSink(), - WakeUp.create(), - ) - ) - brain.addActivity( - Activity.CORE, 3, ImmutableList.of( - GoToWantedItem.create(0.7f, false, 7) + Activity.CORE, ImmutableList.of( + 0 weight Swim(0.8f), + 0 weight InteractWithDoor.create(), + 0 weight LookAtTargetSink(40, 70), + 0 weight MoveToTargetSink(), + 0 weight WakeUp.create(), + 3 weight GoToWantedItem.create(1.2f, false, 7), ) ) } private fun initIdleActivity(brain: Brain) { - brain.addActivity(Activity.IDLE, 99, ImmutableList.of(UpdateActivityFromSchedule.create())) + brain.addActivity( + Activity.IDLE, ImmutableList.of( + 3 weight createStrollBehavior(), + 99 weight UpdateActivityFromSchedule.create(), + ) + ) } private fun initPlayActivity(brain: Brain) { brain.addActivity( - Activity.PLAY, 3, ImmutableList.of( - GoToWantedItem.create(1.75f, true, 32), + Activity.PLAY, ImmutableList.of( + 3 weight GoToWantedItem.create(1.75f, true, 32), + 5 weight JumpOnBed(1.0f), + 5 weight createStrollBehavior(), + 99 weight UpdateActivityFromSchedule.create(), ) ) - brain.addActivity( - Activity.PLAY, 5, ImmutableList.of( - JumpOnBed(0.5f), - RunOne( - listOf( - Pair.of(RandomStroll.stroll(0.5f), 2), - Pair.of(SetWalkTargetFromLookTarget.create(1.0f, 5), 2), - Pair.of(DoNothing(30, 60), 1) - ) - ), - ) - ) - brain.addActivity(Activity.PLAY, 99, ImmutableList.of(UpdateActivityFromSchedule.create())) } private fun initWorkActivity(brain: Brain) { - brain.addActivity(Activity.WORK, 99, ImmutableList.of(UpdateActivityFromSchedule.create())) + brain.addActivity( + Activity.WORK, ImmutableList.of( + 3 weight createStrollBehavior(), + 99 weight UpdateActivityFromSchedule.create(), + ) + ) } private fun initRestActivity(brain: Brain) { - brain.addActivity(Activity.REST, 99, ImmutableList.of(UpdateActivityFromSchedule.create())) + brain.addActivity( + Activity.REST, ImmutableList.of( + 3 weight createStrollBehavior(), + 99 weight UpdateActivityFromSchedule.create(), + ) + ) } + private fun createStrollBehavior() = RunOne( + listOf( + 2 weightR RandomStroll.stroll(1.0f), + 2 weightR SetWalkTargetFromLookTarget.create(1.0f, 5), + 1 weightR DoNothing(30, 60) + ) + ) + } \ No newline at end of file diff --git a/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonEntity.kt b/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonEntity.kt index a0f965f..c28a554 100644 --- a/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonEntity.kt +++ b/common/src/main/kotlin/quaedam/projection/swarm/ProjectedPersonEntity.kt @@ -10,18 +10,21 @@ import net.minecraft.network.protocol.game.DebugPackets import net.minecraft.network.syncher.EntityDataAccessor import net.minecraft.network.syncher.EntityDataSerializers import net.minecraft.network.syncher.SynchedEntityData +import net.minecraft.server.level.ServerLevel import net.minecraft.world.DifficultyInstance import net.minecraft.world.SimpleContainer import net.minecraft.world.entity.* import net.minecraft.world.entity.ai.Brain import net.minecraft.world.entity.ai.attributes.AttributeSupplier import net.minecraft.world.entity.ai.attributes.Attributes +import net.minecraft.world.entity.ai.memory.MemoryModuleType import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.entity.npc.InventoryCarrier import net.minecraft.world.level.Level import net.minecraft.world.level.ServerLevelAccessor import quaedam.Quaedam import quaedam.projector.Projector +import kotlin.jvm.optionals.getOrNull class ProjectedPersonEntity(entityType: EntityType, level: Level) : PathfinderMob(entityType, level), InventoryCarrier { @@ -51,7 +54,8 @@ class ProjectedPersonEntity(entityType: EntityType, level: Le } private fun createAttributes(): AttributeSupplier.Builder = - Mob.createMobAttributes().add(Attributes.ATTACK_DAMAGE, 1.5).add(Attributes.MOVEMENT_SPEED, 0.11) + Mob.createMobAttributes().add(Attributes.ATTACK_DAMAGE, 1.5) + .add(Attributes.MOVEMENT_SPEED, 0.2) .add(Attributes.ATTACK_SPEED) } @@ -107,7 +111,7 @@ class ProjectedPersonEntity(entityType: EntityType, level: Le override fun getTypeName(): Component = shape.name.takeIf { it.isNotEmpty() }?.let { Component.literal(it) } ?: super.getTypeName() - override fun getNameTagOffsetY() = super.getNameTagOffsetY() - (BOUNDING_HEIGHT * (1.2f - shape.scaleY)) + override fun getNameTagOffsetY() = super.getNameTagOffsetY() - (BOUNDING_HEIGHT * (1.3f - shape.scaleY)) override fun createNavigation(level: Level) = ProjectedPersonNavigation(this, level) @@ -152,6 +156,11 @@ class ProjectedPersonEntity(entityType: EntityType, level: Le @Suppress("UNCHECKED_CAST") override fun getBrain(): Brain = super.getBrain() as Brain + override fun customServerAiStep() { + super.customServerAiStep() + getBrain().tick(level() as ServerLevel, this) + } + override fun isBaby() = shape.baby } \ No newline at end of file diff --git a/common/src/main/kotlin/quaedam/utils/Brain.kt b/common/src/main/kotlin/quaedam/utils/Brain.kt new file mode 100644 index 0000000..e6de858 --- /dev/null +++ b/common/src/main/kotlin/quaedam/utils/Brain.kt @@ -0,0 +1,14 @@ +package quaedam.utils + +import com.mojang.datafixers.util.Pair +import net.minecraft.world.entity.LivingEntity +import net.minecraft.world.entity.ai.behavior.BehaviorControl + +@Suppress("NOTHING_TO_INLINE") +inline infix fun , E : LivingEntity> Int.weight(behavior: B): Pair = + Pair.of(this, behavior) + +@Suppress("NOTHING_TO_INLINE") +inline infix fun Int.weightR(behavior: B): Pair + where B : BehaviorControl, E : LivingEntity = + Pair.of(behavior, this)