Skip to content

Commit

Permalink
新的伤害计算机制魔改和KB的优化
Browse files Browse the repository at this point in the history
  • Loading branch information
boybook committed Feb 15, 2024
1 parent 8193d8f commit b154076
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 99 deletions.
45 changes: 25 additions & 20 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,10 @@ public boolean onUpdate(int currentTick) {
}
this.inAirTicks = 0;
this.highestPosition = this.y;
// 地面阻力
this.motionX *= 0.3;
this.motionY = 0;
this.motionZ *= 0.3;
} else {
if (this.checkMovement && !this.isGliding() && !server.getAllowFlight() && !this.getAdventureSettings().get(Type.ALLOW_FLIGHT) && this.inAirTicks > 20 && !this.isSleeping() && !this.isImmobile() && !this.isSwimming() && this.riding == null && !this.hasEffect(Effect.LEVITATION) && !this.hasEffect(Effect.SLOW_FALLING)) {
double expectedVelocity = (-this.getGravity()) / ((double) this.getDrag()) - ((-this.getGravity()) / ((double) this.getDrag())) * Math.exp(-((double) this.getDrag()) * ((double) (this.inAirTicks - this.startAirTicks)));
Expand Down Expand Up @@ -2036,10 +2040,18 @@ public boolean onUpdate(int currentTick) {

if (this.isGliding()) this.resetFallDistance();

++this.inAirTicks;
// 空气阻力
this.motionX *= 0.9900000095367432;
this.motionY *= 0.9800000190734863;
this.motionZ *= 0.9900000095367432;

++this.inAirTicks;
}

if (Math.abs(this.motionX) < 0.0001) this.motionX = 0;
if (Math.abs(this.motionY) < 0.0001) this.motionY = 0;
if (Math.abs(this.motionZ) < 0.0001) this.motionZ = 0;

if (this.isSurvivalLike()) {
if (this.getFoodData() != null) this.getFoodData().update(tickDiff);
}
Expand Down Expand Up @@ -4826,16 +4838,9 @@ public boolean attack(EntityDamageEvent source) {

if (add) {
source.setDamage((float) (source.getDamage() * 1.3));

AnimatePacket animate = new AnimatePacket();
animate.action = AnimatePacket.Action.CRITICAL_HIT;
animate.eid = getId();
this.getLevel().addChunkPacket(damager.getChunkX(), damager.getChunkZ(), animate);

this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ATTACK_STRONG);
}

if (doubleCritical) {
/*if (doubleCritical) {
//正在叠刀
Player damagerPlayer = (Player)((EntityDamageByEntityEvent) source).getDamager();
if (damagerPlayer.attackCriticalThisJump < 2) {
Expand All @@ -4852,7 +4857,7 @@ public boolean attack(EntityDamageEvent source) {
damagerPlayer.dataPacket(animate);
this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ATTACK_STRONG, new Player[]{damagerPlayer});
}
}*/
}

if (super.attack(source)) { //!source.isCancelled()
Expand All @@ -4862,28 +4867,28 @@ public boolean attack(EntityDamageEvent source) {
EntityEventPacket pk = new EntityEventPacket();
pk.eid = this.id;
pk.event = EntityEventPacket.HURT_ANIMATION;
Server.broadcastPacket(this.hasSpawned.values(), pk);
// 这边只发给自己,因为广播给他人的已经在EntityLiving中发送了
this.dataPacket(pk);
if (add) {
if (((EntityDamageByEntityEvent) source).getDamager() instanceof Player) {
/*if (((Player) ((EntityDamageByEntityEvent) source).getDamager()).getLoginChainData().getDeviceOS() == 7) //win10
(((EntityDamageByEntityEvent) source).getDamager()).addEffect(Effect.getEffect(Effect.SLOWNESS).setDuration(10).setAmplifier(1).setVisible(false));*/
((Player)(((EntityDamageByEntityEvent) source).getDamager())).attackCriticalThisJump++;
}
/*
Random random = ThreadLocalRandom.current();
for (int i = 0; i < 10; i++) {
CriticalParticle par = new CriticalParticle(new Vector3(this.x + random.nextDouble() * 2 - 1, this.y + random.nextDouble() * 2, this.z + random.nextDouble() * 2 - 1));
this.getLevel().addParticle(par);
}*/
// 在这里发送暴击,因为事件可能被取消
AnimatePacket animate = new AnimatePacket();
animate.action = AnimatePacket.Action.CRITICAL_HIT;
animate.eid = getId();
this.getLevel().addChunkPacket(this.getChunkX(), this.getChunkZ(), animate);

this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_ATTACK_STRONG);
}
if (doubleCritical) {
/*if (doubleCritical) {
if (((EntityDamageByEntityEvent) source).getDamager() instanceof Player) {
Player damagerPlayer = (Player)((EntityDamageByEntityEvent) source).getDamager();
damagerPlayer.attackCriticalThisJump++;
}
}

}*/
}
return true;
} else {
Expand Down
46 changes: 12 additions & 34 deletions src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockDripstonePointed;
import cn.nukkit.block.BlockID;
import cn.nukkit.block.BlockLiquid;
import cn.nukkit.block.BlockWater;
import cn.nukkit.block.*;
import cn.nukkit.blockentity.BlockEntityPistonArm;
import cn.nukkit.entity.attribute.Attribute;
import cn.nukkit.entity.data.*;
Expand All @@ -18,7 +14,6 @@
import cn.nukkit.event.player.PlayerInteractEvent.Action;
import cn.nukkit.event.player.PlayerTeleportEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.level.GameRule;
import cn.nukkit.level.Level;
import cn.nukkit.level.Location;
Expand Down Expand Up @@ -46,7 +41,10 @@
import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.*;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

Expand Down Expand Up @@ -1080,37 +1078,17 @@ public void despawnFrom(Player player) {
}
}

public boolean isInvulnerableTo(EntityDamageEvent damageSource) {
return this.isClosed() || this.invulnerable && damageSource.getCause() != DamageCause.VOID && !(damageSource.getEntity() instanceof Player player && player.isCreativeLike());
}

public boolean attack(EntityDamageEvent source) {
if ((source.getCause() == DamageCause.FIRE || source.getCause() == DamageCause.FIRE_TICK
|| source.getCause() == DamageCause.LAVA || source.getCause() == DamageCause.MAGMA)
&& (fireProof || hasEffect(Effect.FIRE_RESISTANCE))) {
if (this.isInvulnerableTo(source)) {
return false;
}

getServer().getPluginManager().callEvent(source);
if (source.isCancelled()) {
} else {
// this.markHurt(); // Java Edition
return false;
}

if (source.getCause() != DamageCause.SUICIDE) {
// Make fire aspect to set the target in fire before dealing any damage so the target is in fire on death even if killed by the first hit
if (source instanceof EntityDamageByEntityEvent) {
Enchantment[] enchantments = ((EntityDamageByEntityEvent) source).getWeaponEnchantments();
if (enchantments != null) {
for (Enchantment enchantment : enchantments) {
enchantment.doAttack(((EntityDamageByEntityEvent) source).getDamager(), this);
}
}
}

if (this.absorption > 0) { // Damage Absorption
this.setAbsorption(Math.max(0, this.getAbsorption() + source.getDamage(EntityDamageEvent.DamageModifier.ABSORPTION)));
}
}

setLastDamageCause(source);
setHealth(getHealth() - source.getFinalDamage());
return true;
}

public boolean attack(float damage) {
Expand Down
39 changes: 24 additions & 15 deletions src/main/java/cn/nukkit/entity/EntityHumanType.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public Item[] getDrops() {
}

@Override
public boolean attack(EntityDamageEvent source) {
protected boolean damageEntity0(EntityDamageEvent source) {
if (this.isClosed() || !this.isAlive()) {
return false;
}
Expand All @@ -173,14 +173,14 @@ public boolean attack(EntityDamageEvent source) {
}

source.setDamage(-source.getFinalDamage() * Math.min(Mth.ceil(Math.min(epf, 25) * ((float) ThreadLocalRandom.current().nextInt(50, 100) / 100)), 20) * 0.04f,
DamageModifier.ARMOR_ENCHANTMENTS);
DamageModifier.ARMOR_ENCHANTMENTS);
}

if (source.getCause() != DamageCause.SUICIDE) {
source.setDamage(-Math.min(this.getAbsorption(), source.getFinalDamage()), DamageModifier.ABSORPTION);
}

if (super.attack(source)) {
if (super.damageEntity0(source)) {
if (source.getCause() == DamageCause.SUICIDE) {
return true;
}
Expand Down Expand Up @@ -208,18 +208,18 @@ public boolean attack(EntityDamageEvent source) {
}

if (source.getCause() != DamageCause.VOID
&& source.getCause() != DamageCause.MAGIC
&& source.getCause() != DamageCause.WITHER
&& source.getCause() != DamageCause.HUNGER
&& source.getCause() != DamageCause.DROWNING
&& source.getCause() != DamageCause.SUFFOCATION
&& source.getCause() != DamageCause.FIRE_TICK
&& source.getCause() != DamageCause.FREEZE
&& source.getCause() != DamageCause.TEMPERATURE
&& source.getCause() != DamageCause.FALL
&& source.getCause() != DamageCause.STALAGMITE
&& source.getCause() != DamageCause.FLY_INTO_WALL
&& source.getCause() != DamageCause.SONIC_BOOM
&& source.getCause() != DamageCause.MAGIC
&& source.getCause() != DamageCause.WITHER
&& source.getCause() != DamageCause.HUNGER
&& source.getCause() != DamageCause.DROWNING
&& source.getCause() != DamageCause.SUFFOCATION
&& source.getCause() != DamageCause.FIRE_TICK
&& source.getCause() != DamageCause.FREEZE
&& source.getCause() != DamageCause.TEMPERATURE
&& source.getCause() != DamageCause.FALL
&& source.getCause() != DamageCause.STALAGMITE
&& source.getCause() != DamageCause.FLY_INTO_WALL
&& source.getCause() != DamageCause.SONIC_BOOM
) { // No armor damage
if (armor.isUnbreakable() || armor instanceof ItemSkull || armor.getId() == ItemBlockID.CARVED_PUMPKIN || armor.getId() == Item.ELYTRA) {
continue;
Expand All @@ -241,6 +241,15 @@ public boolean attack(EntityDamageEvent source) {
}
}

@Override
public boolean attack(EntityDamageEvent source) {
if (this.isClosed() || !this.isAlive()) {
return false;
}

return super.attack(source);
}

protected double calculateEnchantmentProtectionFactor(Item item, EntityDamageEvent source) {
if (!item.hasEnchantments()) {
return 0;
Expand Down
Loading

0 comments on commit b154076

Please sign in to comment.