Skip to content

Commit

Permalink
完成基本的 AI 设计
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Dec 10, 2023
1 parent 9a9b4fc commit 5ebadaa
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public class BlockBookshelf extends BlockJoy {
public static final VoxelShape SHAPE = Block.box(1, 0, 1, 15, 5, 15);
public static final String TYPE_NAME = "BookShelf";

@Override
protected Vec3 sitPosition() {
Expand All @@ -24,6 +25,11 @@ protected int sitYRot() {
return -90;
}

@Override
protected String getTypeName() {
return TYPE_NAME;
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class BlockComputer extends BlockJoy {
Block.box(6, 3, 6, 10, 9, 10),
Block.box(1, 9, 1, 15, 12, 15),
Block.box(0, 12, 0, 16, 14, 16));
public static final String TYPE_NAME = "Computer";

@Override
protected Vec3 sitPosition() {
Expand All @@ -29,6 +30,11 @@ protected int sitYRot() {
return 180;
}

@Override
protected String getTypeName() {
return TYPE_NAME;
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public BlockJoy() {

protected abstract Vec3 sitPosition();

protected abstract String getTypeName();

protected abstract int sitYRot();

@Override
Expand All @@ -52,7 +54,7 @@ public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Play
if (oldSitEntity != null && oldSitEntity.isAlive()) {
return super.use(state, worldIn, pos, playerIn, hand, hit);
}
EntitySit newSitEntity = new EntitySit(worldIn, Vec3.atLowerCornerWithOffset(pos, this.sitPosition().x, this.sitPosition().y, this.sitPosition().z));
EntitySit newSitEntity = new EntitySit(worldIn, Vec3.atLowerCornerWithOffset(pos, this.sitPosition().x, this.sitPosition().y, this.sitPosition().z), this.getTypeName());
newSitEntity.setYRot(state.getValue(FACING).getOpposite().toYRot() + this.sitYRot());
worldIn.addFreshEntity(newSitEntity);
joy.setSitId(newSitEntity.getUUID());
Expand All @@ -69,7 +71,7 @@ public void startMaidSit(EntityMaid maid, BlockState state, Level worldIn, Block
if (oldSitEntity != null && oldSitEntity.isAlive()) {
return;
}
EntitySit newSitEntity = new EntitySit(worldIn, Vec3.atLowerCornerWithOffset(pos, this.sitPosition().x, this.sitPosition().y, this.sitPosition().z));
EntitySit newSitEntity = new EntitySit(worldIn, Vec3.atLowerCornerWithOffset(pos, this.sitPosition().x, this.sitPosition().y, this.sitPosition().z), this.getTypeName());
newSitEntity.setYRot(state.getValue(FACING).getOpposite().toYRot() + this.sitYRot());
worldIn.addFreshEntity(newSitEntity);
joy.setSitId(newSitEntity.getUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

public class BlockKeyboard extends BlockJoy {
public static final VoxelShape SHAPE = Block.box(4, 0, 4, 12, 10, 12);
public static final String TYPE_NAME = "Keyboard";

@Override
protected Vec3 sitPosition() {
Expand All @@ -25,6 +26,11 @@ protected int sitYRot() {
return 0;
}

@Override
protected String getTypeName() {
return TYPE_NAME;
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public void tick() {
counter.values().forEach(Time::tick);
}

public void addCooldown(String type, int time) {
this.counter.put(type, new Time(time));
public void addCooldown(String type, int tickCount) {
this.counter.put(type, new Time(tickCount));
}

public boolean canAdd(String type) {
Expand All @@ -31,7 +31,7 @@ public boolean canAdd(String type) {

public void addAdditionalSaveData(CompoundTag compound) {
CompoundTag data = new CompoundTag();
this.counter.forEach((name, time) -> data.putInt(name, time.getTime()));
this.counter.forEach((name, time) -> data.putInt(name, time.getTickCount()));
compound.put(TAG_NAME, data);
}

Expand All @@ -45,28 +45,28 @@ public void readAdditionalSaveData(CompoundTag compound) {
}

public static class Time {
private int time;
private int tickCount;

public Time(int time) {
this.time = time;
public Time(int tickCount) {
this.tickCount = tickCount;
}

public int getTime() {
return time;
public int getTickCount() {
return tickCount;
}

public void setTime(int time) {
this.time = time;
public void setTickCount(int tickCount) {
this.tickCount = tickCount;
}

public void tick() {
if (time > 0) {
time--;
if (tickCount > 0) {
tickCount--;
}
}

public boolean isZero() {
return this.time <= 0;
return this.tickCount <= 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package com.github.tartaricacid.touhoulittlemaid.entity.item;

import com.github.tartaricacid.touhoulittlemaid.entity.ai.brain.MaidSchedule;
import com.github.tartaricacid.touhoulittlemaid.entity.favorability.FavorabilityUtils;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.init.InitEntities;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.schedule.Activity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.NetworkHooks;
import org.apache.commons.lang3.StringUtils;

public class EntitySit extends Entity {
public static final EntityType<EntitySit> TYPE = EntityType.Builder.<EntitySit>of(EntitySit::new, MobCategory.MISC)
.sized(0.5f, 0.1f).clientTrackingRange(10).build("sit");
private int passengerTick = 0;
private String joyType = null;

public EntitySit(EntityType<?> entityTypeIn, Level worldIn) {
super(entityTypeIn, worldIn);
}

public EntitySit(Level worldIn, Vec3 pos) {
public EntitySit(Level worldIn, Vec3 pos, String joyType) {
this(TYPE, worldIn);
this.setPos(pos);
this.joyType = joyType;
}

@Override
Expand All @@ -36,22 +44,43 @@ protected void defineSynchedData() {

@Override
protected void readAdditionalSaveData(CompoundTag tag) {
if (tag.contains("SitJoyType", Tag.TAG_STRING)) {
this.joyType = tag.getString("SitJoyType");
}
}

@Override
protected void addAdditionalSaveData(CompoundTag tag) {
if (StringUtils.isNotBlank(joyType)) {
tag.putString("SitJoyType", joyType);
}
}

@Override
public void tick() {
if (!this.level.isClientSide) {
this.checkBelowWorld();
this.checkPassengers();
if (this.getFirstPassenger() instanceof EntityMaid maid) {
this.tickMaid(maid);
}
}
if (this.getFirstPassenger() instanceof EntityMaid maid) {
}

private void tickMaid(EntityMaid maid) {
if (tickCount % 2 == 0) {
maid.setYRot(this.getYRot());
maid.setYHeadRot(this.getYRot());
}
if (tickCount % 20 == 0) {
if (maid.getFavorabilityManager().canAdd(this.joyType)) {
FavorabilityUtils.add(maid, 2);
maid.getFavorabilityManager().addCooldown(this.joyType, 24000);
}
if (!this.isIdleSchedule(maid)) {
maid.stopRiding();
}
}
}

private void checkPassengers() {
Expand All @@ -65,6 +94,22 @@ private void checkPassengers() {
}
}

private boolean isIdleSchedule(EntityMaid maid) {
MaidSchedule schedule = maid.getSchedule();
int time = (int) (maid.level.getDayTime() % 24000L);
switch (schedule) {
case ALL -> {
return false;
}
case NIGHT -> {
return InitEntities.MAID_NIGHT_SHIFT_SCHEDULES.get().getActivityAt(time) == Activity.IDLE;
}
default -> {
return InitEntities.MAID_DAY_SHIFT_SCHEDULES.get().getActivityAt(time) == Activity.IDLE;
}
}
}

@Override
public boolean skipAttackInteraction(Entity pEntity) {
return true;
Expand Down Expand Up @@ -109,4 +154,8 @@ public boolean canCollideWith(Entity entity) {
public Packet<ClientGamePacketListener> getAddEntityPacket() {
return NetworkHooks.getEntitySpawningPacket(this);
}

public String getJoyType() {
return joyType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.tartaricacid.touhoulittlemaid.entity.favorability.FavorabilityManager;
import com.github.tartaricacid.touhoulittlemaid.entity.info.ServerCustomPackLoader;
import com.github.tartaricacid.touhoulittlemaid.entity.item.EntityPowerPoint;
import com.github.tartaricacid.touhoulittlemaid.entity.item.EntitySit;
import com.github.tartaricacid.touhoulittlemaid.entity.item.EntityTombstone;
import com.github.tartaricacid.touhoulittlemaid.entity.task.TaskIdle;
import com.github.tartaricacid.touhoulittlemaid.entity.task.TaskManager;
Expand Down Expand Up @@ -1427,8 +1428,11 @@ public String getAtBiomeTemp() {
}

public boolean isSitInJoyBlock() {
// TODO:待完成
return false;
return getVehicle() instanceof EntitySit;
}

public FavorabilityManager getFavorabilityManager() {
return favorabilityManager;
}

@Deprecated
Expand Down

0 comments on commit 5ebadaa

Please sign in to comment.