From f2ceaf176c37d41f7eaffeceae6f687567d39d77 Mon Sep 17 00:00:00 2001 From: xtex Date: Mon, 3 Jul 2023 10:46:50 +0800 Subject: [PATCH] feat: projected person attacking --- .../projection/swarm/ai/ProjectedPersonAI.kt | 28 ++++++++++++++++++- .../resources/assets/quaedam/lang/zh_cn.json | 10 +++---- .../entity_types/projected_person/enemy.json | 4 +++ .../projected_person/no_attack.json | 5 ++++ 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 common/src/main/resources/data/quaedam/tags/entity_types/projected_person/enemy.json create mode 100644 common/src/main/resources/data/quaedam/tags/entity_types/projected_person/no_attack.json diff --git a/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt b/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt index 759c6a5..8462385 100644 --- a/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt +++ b/common/src/main/kotlin/quaedam/projection/swarm/ai/ProjectedPersonAI.kt @@ -1,11 +1,17 @@ package quaedam.projection.swarm.ai import com.google.common.collect.ImmutableList +import net.minecraft.core.registries.Registries +import net.minecraft.resources.ResourceLocation +import net.minecraft.tags.TagKey +import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.ai.Brain import net.minecraft.world.entity.ai.behavior.* import net.minecraft.world.entity.ai.memory.MemoryModuleType import net.minecraft.world.entity.ai.memory.MemoryStatus +import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities import net.minecraft.world.entity.ai.sensing.SensorType +import net.minecraft.world.entity.monster.Monster import net.minecraft.world.entity.schedule.Activity import net.minecraft.world.entity.schedule.Schedule import net.minecraft.world.entity.schedule.ScheduleBuilder @@ -13,9 +19,14 @@ import quaedam.Quaedam import quaedam.projection.swarm.ProjectedPersonEntity import quaedam.utils.weight import quaedam.utils.weightR +import java.util.* +import kotlin.jvm.optionals.getOrNull object ProjectedPersonAI { + val tagEnemy = TagKey.create(Registries.ENTITY_TYPE, ResourceLocation("quaedam", "projected_person/enemy")) + val tagNoAttack = TagKey.create(Registries.ENTITY_TYPE, ResourceLocation("quaedam", "projected_person/no_attack")) + val defaultSchedule = Quaedam.schedules.register("projected_person_default") { ScheduleBuilder(Schedule()).changeActivityAt(10, Activity.IDLE) .changeActivityAt(10, Activity.IDLE) @@ -98,10 +109,15 @@ object ProjectedPersonAI { Activity.CORE, ImmutableList.of( 0 weight Swim(0.8f), 0 weight WakeUp.create(), + 0 weight StopAttackingIfTargetInvalid.create(), 3 weight LookAtTargetSink(40, 70), 3 weight MoveToTargetSink(), 3 weight InteractWithDoor.create(), - 3 weight LostItem(400), + 3 weight SetWalkTargetAwayFrom.entity(MemoryModuleType.HURT_BY_ENTITY, 1.2f, 6, false), + 4 weight MeleeAttack.create(15), + 5 weight LostItem(400), + 5 weight StartAttacking.create(::findAttackTarget), + 5 weight SetWalkTargetFromAttackTargetIfTargetOutOfReach.create(1.1f), 10 weight GoToWantedItem.create(1.2f, false, 7), ) ) @@ -162,4 +178,14 @@ object ProjectedPersonAI { ) ) + private fun findAttackTarget(entity: ProjectedPersonEntity): Optional { + val entities = entity.brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES).getOrNull() + ?: NearestVisibleLivingEntities.empty() + return entities.findClosest { target: LivingEntity -> + entity.canAttack(target) + && !target.type.`is`(tagNoAttack) + && (target.type.`is`(tagEnemy) || target is Monster) + } + } + } \ No newline at end of file diff --git a/common/src/main/resources/assets/quaedam/lang/zh_cn.json b/common/src/main/resources/assets/quaedam/lang/zh_cn.json index 7486a87..4dfd318 100644 --- a/common/src/main/resources/assets/quaedam/lang/zh_cn.json +++ b/common/src/main/resources/assets/quaedam/lang/zh_cn.json @@ -1,7 +1,7 @@ { - "category.quaedam": "Quaedam", - "block.quaedam.projector": "投影仪", - "block.quaedam.skylight_projection": "天光投影", - "block.quaedam.swarm_projection": "人群投影", - "entity.quaedam.projected_person": "虚拟个体" + "category.quaedam": "Quaedam", + "block.quaedam.projector": "投影仪", + "block.quaedam.skylight_projection": "天光投影", + "block.quaedam.swarm_projection": "人群投影", + "entity.quaedam.projected_person": "虚拟个体" } diff --git a/common/src/main/resources/data/quaedam/tags/entity_types/projected_person/enemy.json b/common/src/main/resources/data/quaedam/tags/entity_types/projected_person/enemy.json new file mode 100644 index 0000000..d6649e7 --- /dev/null +++ b/common/src/main/resources/data/quaedam/tags/entity_types/projected_person/enemy.json @@ -0,0 +1,4 @@ +{ + "values": [ + ] +} \ No newline at end of file diff --git a/common/src/main/resources/data/quaedam/tags/entity_types/projected_person/no_attack.json b/common/src/main/resources/data/quaedam/tags/entity_types/projected_person/no_attack.json new file mode 100644 index 0000000..8304eb7 --- /dev/null +++ b/common/src/main/resources/data/quaedam/tags/entity_types/projected_person/no_attack.json @@ -0,0 +1,5 @@ +{ + "values": [ + "minecraft:creeper" + ] +} \ No newline at end of file