Physical and arcane damage

This commit is contained in:
2026-09-30 09:56:53 -03:00
parent 88659bb7af
commit 9e580f175e
9 changed files with 54 additions and 75 deletions
+4 -1
View File
@@ -98,6 +98,9 @@ export interface EntityJson {
max_health?: number;
// minecraft's per tick speed, 0.25 for most animals
movement_speed?: number;
// protection from physical and from arcane damage, not used yet
physical_shielding?: number;
arcane_shielding?: number;
}
type Vec3 = [number, number, number];
@@ -678,7 +681,7 @@ export function validate_entity(json: unknown): Problems {
problems.push(`${key} must be a number of blocks above 0, at most 16`);
}
}
for (const key of ["max_health", "movement_speed"]) {
for (const key of ["max_health", "movement_speed", "physical_shielding", "arcane_shielding"]) {
if (json[key] !== undefined && (typeof json[key] !== "number" || json[key] < 0)) {
problems.push(`${key} must be a number, 0 or more`);
}
+11 -9
View File
@@ -22,15 +22,17 @@ and players. Mods define new kinds in their `entities/` folder, and draw them wi
}
```
| Field | Default | Meaning |
| ----------------- | ---------- | ------------------------------------------------------------------------------------------------------ |
| `id` | required | The entity's id. |
| `texture` | required | Any texture, stretched over the model's `texture_width` and `texture_height`. |
| `model` | none | An [entity model](#entity-models). Without one it's drawn as its texture, facing the camera. |
| `behavior` | `"animal"` | `"animal"` or `"scripted"`, see below. |
| `width`, `height` | required | Its collision box in blocks, up to 16. `width` is for both x and z. Its eyes are at 85% of the height. |
| `max_health` | `10` | In half hearts. |
| `movement_speed` | `0.25` | Minecraft's per tick speed, 0.25 for most animals. |
| Field | Default | Meaning |
| -------------------- | ---------- | ------------------------------------------------------------------------------------------------------ |
| `id` | required | The entity's id. |
| `texture` | required | Any texture, stretched over the model's `texture_width` and `texture_height`. |
| `model` | none | An [entity model](#entity-models). Without one it's drawn as its texture, facing the camera. |
| `behavior` | `"animal"` | `"animal"` or `"scripted"`, see below. |
| `width`, `height` | required | Its collision box in blocks, up to 16. `width` is for both x and z. Its eyes are at 85% of the height. |
| `max_health` | `10` | In half hearts. |
| `movement_speed` | `0.25` | Minecraft's per tick speed, 0.25 for most animals. |
| `physical_shielding` | `0` | Protection from physical damage. Not used yet. |
| `arcane_shielding` | `0` | Protection from arcane damage. Not used yet. |
`/summon <entity>` spawns one where you stand.
+3 -3
View File
@@ -24,9 +24,9 @@ function attribute(id: string, value: number, min: number, max: number): Attribu
export const ATTRIBUTES = {
max_health: attribute("max_health", 20, 1, 1024),
max_absorption: attribute("max_absorption", 0, 0, 2048),
armor: attribute("armor", 0, 0, 30),
armor_toughness: attribute("armor_toughness", 0, 0, 20),
// protection from physical and from arcane damage
physical_shielding: attribute("physical_shielding", 0, 0, 1024),
arcane_shielding: attribute("arcane_shielding", 0, 0, 1024),
knockback_resistance: attribute("knockback_resistance", 0, 0, 1),
movement_speed: attribute("movement_speed", 0.7, 0, 1024),
attack_damage: attribute("attack_damage", 2, 0, 2048),
-4
View File
@@ -51,10 +51,6 @@ export const EFFECTS = {
modifiers: [{ attribute: ATTRIBUTES.max_health, amount: 4, operation: "add_value" }],
on_removed: (entity) => entity.health = Math.min(entity.health, entity.max_health),
}),
absorption: effect("absorption", true, {
modifiers: [{ attribute: ATTRIBUTES.max_absorption, amount: 4, operation: "add_value" }],
on_added: (entity, amplifier) => entity.absorption = Math.max(entity.absorption, 4 * (amplifier + 1)),
}),
regeneration: effect("regeneration", true, {
should_tick: every(50),
on_tick: (entity) => {
+10 -9
View File
@@ -32,28 +32,29 @@ export interface PlayerTarget extends Attacker {
readonly name: string;
}
export type DamageType = "physical" | "arcane" | "absolute";
// what hurt something. cause is for mods and death messages, like "fall", "drown" or "mob_attack"
export interface DamageSource {
type: DamageType;
cause: string;
// who did it, which knockback pushes away from
attacker?: Attacker;
by_player?: boolean;
// fire resistance stops it
fire?: boolean;
// armor doesn't help, like drowning, falling and starving
bypasses_armor?: boolean;
// even invulnerable entities are hurt, like falling out of the world
bypasses_invulnerability?: boolean;
}
export const DAMAGE = {
generic: { cause: "generic" },
fall: { cause: "fall", bypasses_armor: true },
drown: { cause: "drown", bypasses_armor: true },
on_fire: { cause: "on_fire", fire: true, bypasses_armor: true },
void: { cause: "void", bypasses_armor: true, bypasses_invulnerability: true },
magic: { cause: "magic", bypasses_armor: true },
wither: { cause: "wither", bypasses_armor: true },
generic: { cause: "generic", type: "physical" },
fall: { cause: "fall", type: "physical" },
drown: { cause: "drown", type: "physical" },
on_fire: { cause: "on_fire", fire: true, type: "arcane" },
void: { cause: "void", bypasses_invulnerability: true, type: "absolute" },
magic: { cause: "magic", type: "arcane" },
wither: { cause: "wither", type: "arcane" },
} satisfies Record<string, DamageSource>;
// what entities can see and do in the world, the entity manager is one
+11 -33
View File
@@ -33,7 +33,6 @@ const SLOW_FALL_GRAVITY = 0.01;
export interface SavedLivingEntity extends SavedEntity {
health?: number;
absorption_amount?: number;
active_effects?: SavedEffect[];
attributes?: unknown;
death_time?: number;
@@ -51,7 +50,6 @@ export abstract class LivingEntity extends Entity {
attributes = new AttributeMap(this.default_attributes());
#health = this.max_health;
#absorption = 0;
effects = new Map<string, EffectInstance>();
equipment = new Map<EquipmentSlot, ItemStack>();
@@ -80,9 +78,6 @@ export abstract class LivingEntity extends Entity {
default_attributes(): [AttributeType, number?][] {
return [
[ATTRIBUTES.max_health],
[ATTRIBUTES.max_absorption],
[ATTRIBUTES.armor],
[ATTRIBUTES.armor_toughness],
[ATTRIBUTES.knockback_resistance],
[ATTRIBUTES.movement_speed],
[ATTRIBUTES.jump_strength],
@@ -109,15 +104,6 @@ export abstract class LivingEntity extends Entity {
this.#health = Math.min(this.max_health, Math.max(0, value));
}
// extra health from the absorption effect, used up before health
get absorption() {
return this.#absorption;
}
set absorption(value: number) {
this.#absorption = Math.min(this.attribute(ATTRIBUTES.max_absorption), Math.max(0, value));
}
get dead() {
return this.#health <= 0;
}
@@ -326,22 +312,23 @@ export abstract class LivingEntity extends Entity {
return true;
}
// after armor, resistance and absorption
// after resistance
actually_hurt(amount: number, source: DamageSource) {
if (!source.bypasses_armor) {
amount = damage_after_armor(
amount,
this.attribute(ATTRIBUTES.armor),
this.attribute(ATTRIBUTES.armor_toughness),
);
// absolute damage, take it and theres no counterplay
if (source.type === "absolute") {
this.health -= amount;
return;
}
const resistance = this.get_effect(EFFECTS.resistance);
if (resistance && !source.bypasses_invulnerability) {
amount = Math.max(0, amount * (25 - (resistance.amplifier + 1) * 5) / 25);
}
const absorbed = Math.min(amount, this.absorption);
this.absorption -= absorbed;
this.health -= amount - absorbed;
const shielding = this.attribute(
source.type === "physical" ? ATTRIBUTES.physical_shielding : ATTRIBUTES.arcane_shielding,
);
const post_mitigation = amount * (100 / (100 + shielding));
this.health -= post_mitigation;
}
// it was just hurt, for kinds that react
@@ -464,7 +451,6 @@ export abstract class LivingEntity extends Entity {
effect.type.on_removed?.(this, effect.amplifier);
// back inside what the attributes allow now
this.health = this.#health;
this.absorption = this.#absorption;
}
// equipment
@@ -503,7 +489,6 @@ export abstract class LivingEntity extends Entity {
override save_additional(saved: SavedLivingEntity) {
super.save_additional(saved);
saved.health = this.#health;
if (this.#absorption) saved.absorption_amount = this.#absorption;
if (this.effects.size) saved.active_effects = [...this.effects.values()].map((effect) => effect.save());
const attributes = this.attributes.save(new AttributeMap(this.default_attributes()));
if (attributes.length) saved.attributes = attributes;
@@ -534,7 +519,6 @@ export abstract class LivingEntity extends Entity {
this.#apply_modifiers(effect);
}
this.health = finite(saved.health, this.max_health);
this.absorption = finite(saved.absorption_amount, 0);
this.death_time = finite(saved.death_time, 0);
this.hurt_time = finite(saved.hurt_time, 0);
this.last_hurt_by_mob = typeof saved.last_hurt_by_mob === "string" ? saved.last_hurt_by_mob : undefined;
@@ -550,9 +534,3 @@ export abstract class LivingEntity extends Entity {
}
}
}
// minecraft's CombatRules.getDamageAfterAbsorb
export function damage_after_armor(damage: number, armor: number, toughness: number) {
const reduction = Math.min(20, Math.max(armor * 0.2, armor - damage / (2 + toughness / 4)));
return damage * (1 - reduction / 25);
}
+8 -2
View File
@@ -63,7 +63,13 @@ export abstract class Mob extends LivingEntity {
jump_control = new JumpControl(this);
override default_attributes(): [AttributeType, number?][] {
return [...super.default_attributes(), [ATTRIBUTES.follow_range, 16], [ATTRIBUTES.attack_knockback]];
return [
...super.default_attributes(),
[ATTRIBUTES.follow_range, 16],
[ATTRIBUTES.attack_knockback],
[ATTRIBUTES.physical_shielding],
[ATTRIBUTES.arcane_shielding],
];
}
#registered_goals = false;
@@ -161,7 +167,7 @@ export abstract class Mob extends LivingEntity {
// hits whoever it's attacking with its attack damage, like minecraft's doHurtTarget. returns whether it hurt them
do_hurt_target(target: Entity | PlayerTarget): boolean {
const damage = this.attribute(ATTRIBUTES.attack_damage);
const source = { cause: "mob_attack", attacker: this };
const source = { cause: "mob_attack", attacker: this, type: "physical" } as const;
if (!(target instanceof Entity)) {
this.level.damage_player(target.id, damage, source);
return true;
+2
View File
@@ -25,6 +25,8 @@ export function mod_entity_factory(json: EntityJson): EntityFactory {
...super.default_attributes(),
[ATTRIBUTES.max_health, json.max_health ?? 10],
[ATTRIBUTES.movement_speed, json.movement_speed ?? 0.25],
[ATTRIBUTES.physical_shielding, json.physical_shielding ?? 0],
[ATTRIBUTES.arcane_shielding, json.arcane_shielding ?? 0],
];
}
+5 -14
View File
@@ -129,27 +129,18 @@ Deno.test("hurting: a weaker hit right after does nothing, a harder one only the
assert(mob.hurt(1, DAMAGE.generic), "hurt normally again after the cooldown");
assertEquals(mob.health, 4);
ticks(11);
mob.absorption = 0;
mob.add_effect(new EffectInstance(EFFECTS.absorption, 100));
assertEquals(mob.absorption, 4);
mob.hurt(3, DAMAGE.generic);
assertEquals([mob.absorption, mob.health], [1, 4], "absorption goes first");
ticks(11);
mob.invulnerable = true;
assertEquals(mob.hurt(3, DAMAGE.generic), false);
assert(mob.hurt(1, DAMAGE.void), "falling out of the world hurts anyway");
});
Deno.test("armor takes some of the damage, and hits push away from the attacker", async () => {
Deno.test("hits push away from the attacker", async () => {
const { spawn, ticks } = await floor_game();
const mob = spawn(0.5, 100, 2.5);
ticks(2);
mob.attributes.get(ATTRIBUTES.armor)!.base = 10;
// minecraft: 10 armor against 5 damage takes 7.5 / 25 of it off
mob.hurt(5, { cause: "mob_attack", attacker: { id: "x", x: 0.5, y: 100, z: 0.5 } });
assertAlmostEquals(mob.health, 10 - 5 * (1 - 7.5 / 25));
mob.hurt(5, { type: "physical", cause: "mob_attack", attacker: { id: "x", x: 0.5, y: 100, z: 0.5 } });
assertEquals(mob.health, 5);
assert(mob.vz > 0 && mob.vy > 0, "knocked back, away from the attacker at a smaller z");
});
@@ -336,7 +327,7 @@ Deno.test("entities are saved with everything about them, riders on what they ri
mob.health = 6;
mob.add_effect(new EffectInstance(EFFECTS.speed, 100));
mob.add_effect(new EffectInstance(EFFECTS.speed, 10, { amplifier: 1 }));
mob.attributes.get(ATTRIBUTES.armor)!.base = 4;
mob.attributes.get(ATTRIBUTES.physical_shielding)!.base = 4;
mob.set_item_slot("mainhand", new ItemStack("bworld:stick"));
mob.restrict_to(1, 2, 3, 8);
assert(rider.start_riding(mob));
@@ -360,7 +351,7 @@ Deno.test("entities are saved with everything about them, riders on what they ri
const speed = horse.get_effect(EFFECTS.speed)!;
assertEquals([speed.amplifier, speed.duration, speed.hidden?.amplifier, speed.hidden?.duration], [1, 9, 0, 99]);
assertAlmostEquals(horse.attribute(ATTRIBUTES.movement_speed), 0.25 * 1.4);
assertEquals(horse.attribute(ATTRIBUTES.armor), 4);
assertEquals(horse.attribute(ATTRIBUTES.physical_shielding), 4);
assertEquals(horse.main_hand_item?.type_id, "bworld:stick");
assertEquals([horse.home, horse.home_radius], [{ x: 1, y: 2, z: 3 }, 8]);
assertEquals(loaded_rider.vehicle, horse);