fix: swarm

This commit is contained in:
xtex 2023-07-02 21:10:29 +08:00
parent 5e3df22a10
commit 14a3b23e43
Signed by: xtex
GPG Key ID: B918086ED8045B91
3 changed files with 68 additions and 34 deletions

View File

@ -1,7 +1,6 @@
package quaedam.projection.swarm package quaedam.projection.swarm
import com.google.common.collect.ImmutableList 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.Brain
import net.minecraft.world.entity.ai.behavior.* import net.minecraft.world.entity.ai.behavior.*
import net.minecraft.world.entity.ai.memory.MemoryModuleType 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.Schedule
import net.minecraft.world.entity.schedule.ScheduleBuilder import net.minecraft.world.entity.schedule.ScheduleBuilder
import quaedam.Quaedam import quaedam.Quaedam
import quaedam.utils.weight
import quaedam.utils.weightR
object ProjectedPersonAI { object ProjectedPersonAI {
@ -21,14 +22,15 @@ object ProjectedPersonAI {
MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM, MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM,
MemoryModuleType.HURT_BY, MemoryModuleType.HURT_BY,
MemoryModuleType.ATTACK_COOLING_DOWN MemoryModuleType.ATTACK_COOLING_DOWN,
MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE,
) )
private val sensorTypes = listOf( private val sensorTypes = listOf(
SensorType.NEAREST_LIVING_ENTITIES, SensorType.NEAREST_LIVING_ENTITIES,
SensorType.NEAREST_PLAYERS, SensorType.NEAREST_PLAYERS,
SensorType.HURT_BY, SensorType.HURT_BY,
SensorType.NEAREST_ITEMS SensorType.NEAREST_ITEMS,
) )
val defaultSchedule = Quaedam.schedule.register("projected_person_default") { val defaultSchedule = Quaedam.schedule.register("projected_person_default") {
@ -80,52 +82,61 @@ object ProjectedPersonAI {
private fun initCoreActivity(brain: Brain<ProjectedPersonEntity>) { private fun initCoreActivity(brain: Brain<ProjectedPersonEntity>) {
brain.addActivity( brain.addActivity(
Activity.CORE, 0, ImmutableList.of( Activity.CORE, ImmutableList.of(
Swim(0.8f), 0 weight Swim(0.8f),
InteractWithDoor.create(), 0 weight InteractWithDoor.create(),
LookAtTargetSink(40, 70), 0 weight LookAtTargetSink(40, 70),
MoveToTargetSink(), 0 weight MoveToTargetSink(),
WakeUp.create(), 0 weight WakeUp.create(),
) 3 weight GoToWantedItem.create(1.2f, false, 7),
)
brain.addActivity(
Activity.CORE, 3, ImmutableList.of(
GoToWantedItem.create(0.7f, false, 7)
) )
) )
} }
private fun initIdleActivity(brain: Brain<ProjectedPersonEntity>) { private fun initIdleActivity(brain: Brain<ProjectedPersonEntity>) {
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<ProjectedPersonEntity>) { private fun initPlayActivity(brain: Brain<ProjectedPersonEntity>) {
brain.addActivity( brain.addActivity(
Activity.PLAY, 3, ImmutableList.of( Activity.PLAY, ImmutableList.of(
GoToWantedItem.create(1.75f, true, 32), 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<ProjectedPersonEntity>) { private fun initWorkActivity(brain: Brain<ProjectedPersonEntity>) {
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<ProjectedPersonEntity>) { private fun initRestActivity(brain: Brain<ProjectedPersonEntity>) {
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)
)
)
} }

View File

@ -10,18 +10,21 @@ import net.minecraft.network.protocol.game.DebugPackets
import net.minecraft.network.syncher.EntityDataAccessor import net.minecraft.network.syncher.EntityDataAccessor
import net.minecraft.network.syncher.EntityDataSerializers import net.minecraft.network.syncher.EntityDataSerializers
import net.minecraft.network.syncher.SynchedEntityData import net.minecraft.network.syncher.SynchedEntityData
import net.minecraft.server.level.ServerLevel
import net.minecraft.world.DifficultyInstance import net.minecraft.world.DifficultyInstance
import net.minecraft.world.SimpleContainer import net.minecraft.world.SimpleContainer
import net.minecraft.world.entity.* import net.minecraft.world.entity.*
import net.minecraft.world.entity.ai.Brain import net.minecraft.world.entity.ai.Brain
import net.minecraft.world.entity.ai.attributes.AttributeSupplier import net.minecraft.world.entity.ai.attributes.AttributeSupplier
import net.minecraft.world.entity.ai.attributes.Attributes 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.item.ItemEntity
import net.minecraft.world.entity.npc.InventoryCarrier import net.minecraft.world.entity.npc.InventoryCarrier
import net.minecraft.world.level.Level import net.minecraft.world.level.Level
import net.minecraft.world.level.ServerLevelAccessor import net.minecraft.world.level.ServerLevelAccessor
import quaedam.Quaedam import quaedam.Quaedam
import quaedam.projector.Projector import quaedam.projector.Projector
import kotlin.jvm.optionals.getOrNull
class ProjectedPersonEntity(entityType: EntityType<out PathfinderMob>, level: Level) : PathfinderMob(entityType, level), class ProjectedPersonEntity(entityType: EntityType<out PathfinderMob>, level: Level) : PathfinderMob(entityType, level),
InventoryCarrier { InventoryCarrier {
@ -51,7 +54,8 @@ class ProjectedPersonEntity(entityType: EntityType<out PathfinderMob>, level: Le
} }
private fun createAttributes(): AttributeSupplier.Builder = 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) .add(Attributes.ATTACK_SPEED)
} }
@ -107,7 +111,7 @@ class ProjectedPersonEntity(entityType: EntityType<out PathfinderMob>, level: Le
override fun getTypeName(): Component = override fun getTypeName(): Component =
shape.name.takeIf { it.isNotEmpty() }?.let { Component.literal(it) } ?: super.getTypeName() 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) override fun createNavigation(level: Level) = ProjectedPersonNavigation(this, level)
@ -152,6 +156,11 @@ class ProjectedPersonEntity(entityType: EntityType<out PathfinderMob>, level: Le
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun getBrain(): Brain<ProjectedPersonEntity> = super.getBrain() as Brain<ProjectedPersonEntity> override fun getBrain(): Brain<ProjectedPersonEntity> = super.getBrain() as Brain<ProjectedPersonEntity>
override fun customServerAiStep() {
super.customServerAiStep()
getBrain().tick(level() as ServerLevel, this)
}
override fun isBaby() = shape.baby override fun isBaby() = shape.baby
} }

View File

@ -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 <B : BehaviorControl<E>, E : LivingEntity> Int.weight(behavior: B): Pair<Int, B> =
Pair.of(this, behavior)
@Suppress("NOTHING_TO_INLINE")
inline infix fun <B, E> Int.weightR(behavior: B): Pair<B, Int>
where B : BehaviorControl<E>, E : LivingEntity =
Pair.of(behavior, this)