Add vanilla's team rules and compare them with vanilla

Friendly fire (doTeamsAllowDamage), alliances, collision rules
(EntitySelector.pushableBy), seeing invisible team mates (isInvisibleTo),
name tag visibility (LivingEntityRenderer.shouldShowName) and who sees a
player's own death message. tools/test_team_rules_parity.py runs vanilla's
methods on stand-in entities and a stand-in client over 6000 cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-12 16:36:04 +02:00
parent 68520308d0
commit 1515c5e46c
4 changed files with 475 additions and 0 deletions
@@ -0,0 +1,50 @@
//! Team rules in gameplay for differential tests against vanilla
//! (tools/test_team_rules_parity.py): friendly fire, alliances, collision
//! rules, seeing invisible team mates and name tag visibility, from
//! `horizon_client::scoreboard`, in VanillaTeamRulesOracle's JSON lines.
//!
//! usage: team_rules_fixture INPUTS OUTPUT
use horizon_client::scoreboard::{self as sb, CollisionRule, NameView, Scoreboard, Visibility};
use serde_json::{Value, json};
use std::{fs, io::Write};
fn main() {
let args: Vec<String> = std::env::args().collect();
let mut out = std::io::BufWriter::new(fs::File::create(&args[2]).expect("output"));
for line in fs::read_to_string(&args[1]).expect("inputs").lines() {
let c: Value = serde_json::from_str(line).expect("case");
let mut board = Scoreboard::default();
for name in ["A", "B"] {
let o = &c[name];
let t = board.add_team(name);
t.allow_friendly_fire = o["ff"].as_bool().unwrap_or(true);
t.see_friendly_invisibles = o["si"].as_bool().unwrap_or(true);
t.collision_rule =
CollisionRule::by_name(o["collision"].as_str().unwrap_or("")).expect("rule");
t.nametag_visibility =
Visibility::by_name(o["nametag"].as_str().unwrap_or("")).expect("visibility");
}
let team = |k: &str| c[k].as_str().and_then(|n| board.team(n));
let flag = |k: &str| c[k].as_bool().unwrap_or(false);
let (own, other, viewer) = (team("own"), team("other"), team("viewer"));
let invisible =
sb::invisible_to(other, viewer, flag("viewer_spectator"), flag("invisible"));
let view = NameView {
discrete: flag("discrete"),
distance_sqr: c["distance"].as_f64().unwrap_or(0.0),
invisible_to_viewer: invisible,
hud_hidden: flag("hud_hidden"),
is_camera: flag("camera_is_entity"),
is_vehicle: flag("vehicle"),
};
let r = json!({
"case": c["case"],
"damage": sb::teams_allow_damage(own, other),
"allied": sb::teams_allied(own, other),
"pushes": !flag("spectator") && sb::pushable_by(own, other, flag("pushable")),
"invisible": invisible,
"name": sb::name_shown(other, viewer, view),
});
writeln!(out, "{r}").expect("write");
}
}
+107
View File
@@ -558,6 +558,113 @@ impl Team {
}
}
// ---- team rules in gameplay ------------------------------------------------
/// `Team.isAlliedTo`: both on the same team.
pub fn teams_allied(own: Option<&Team>, other: Option<&Team>) -> bool {
matches!((own, other), (Some(a), Some(b)) if a.name == b.name)
}
/// `Entity.doTeamsAllowDamage`: an entity may hurt another unless they are
/// allies on a team without friendly fire (also `Player.canHarmPlayer`).
pub fn teams_allow_damage(own: Option<&Team>, other: Option<&Team>) -> bool {
match own {
None => true,
Some(t) => !teams_allied(own, other) || t.allow_friendly_fire,
}
}
/// `EntitySelector.pushableBy(entity).test(input)` on the server (or for the
/// local player on the client): whether `entity` pushes `input`, which must
/// be pushable and not a spectator.
pub fn pushable_by(own: Option<&Team>, their: Option<&Team>, their_pushable: bool) -> bool {
let own_rule = own.map_or(CollisionRule::Always, |t| t.collision_rule);
if own_rule == CollisionRule::Never || !their_pushable {
return false;
}
let their_rule = their.map_or(CollisionRule::Always, |t| t.collision_rule);
if their_rule == CollisionRule::Never {
return false;
}
let same = teams_allied(own, their);
if (own_rule == CollisionRule::PushOwnTeam || their_rule == CollisionRule::PushOwnTeam) && same
{
return false;
}
own_rule != CollisionRule::PushOtherTeams && their_rule != CollisionRule::PushOtherTeams || same
}
/// `Entity.isInvisibleTo(player)`: invisible unless the viewer spectates or
/// shares a team that sees friendly invisibles.
pub fn invisible_to(
own: Option<&Team>,
viewer: Option<&Team>,
viewer_spectator: bool,
invisible: bool,
) -> bool {
if viewer_spectator {
return false;
}
match own {
Some(t) if teams_allied(own, viewer) && t.see_friendly_invisibles => false,
_ => invisible,
}
}
/// The team part of `LivingEntityRenderer.shouldShowName` for an entity
/// other than the viewer: `Some(shown)` when the entity's team decides,
/// `None` when it has no team.
pub fn team_shows_name(
own: Option<&Team>,
viewer: Option<&Team>,
visible_to_viewer: bool,
) -> Option<bool> {
let t = own?;
Some(match t.nametag_visibility {
Visibility::Always => visible_to_viewer,
Visibility::Never => false,
Visibility::HideForOtherTeams => match viewer {
None => visible_to_viewer,
Some(_) => {
teams_allied(own, viewer) && (t.see_friendly_invisibles || visible_to_viewer)
}
},
Visibility::HideForOwnTeam => match viewer {
None => visible_to_viewer,
Some(_) => !teams_allied(own, viewer) && visible_to_viewer,
},
})
}
/// What `LivingEntityRenderer.shouldShowName` asks of a living entity other
/// than the viewer.
#[derive(Clone, Copy, Debug, Default)]
pub struct NameView {
/// Crouching (`isDiscrete`): its name hides beyond 32 blocks.
pub discrete: bool,
pub distance_sqr: f64,
/// `isInvisibleTo(viewer)`.
pub invisible_to_viewer: bool,
/// F1 (`Hud.isHidden`).
pub hud_hidden: bool,
/// The camera is this entity.
pub is_camera: bool,
/// Something rides it.
pub is_vehicle: bool,
}
/// `LivingEntityRenderer.shouldShowName` (before a mob renderer's own check
/// for a visible or targeted custom name).
pub fn name_shown(own: Option<&Team>, viewer: Option<&Team>, v: NameView) -> bool {
if v.discrete && v.distance_sqr >= 1024.0 {
return false;
}
let visible = !v.invisible_to_viewer;
if let Some(shown) = team_shows_name(own, viewer, visible) {
return shown;
}
!v.hud_hidden && !v.is_camera && visible && !v.is_vehicle
}
/// `ServerPlayer.die`: whether a player's death message reaches the player
/// themselves (the team broadcasts leave out the dying player).
pub fn own_death_message_shown(team: Option<&Team>) -> bool {
team.is_none_or(|t| t.death_message_visibility == Visibility::Always)
}
// ---- objectives and scores ---------------------------------------------
#[derive(Clone, Debug, PartialEq)]
@@ -0,0 +1,209 @@
package dev.horizonlab.parity;
import com.google.gson.*;
import java.io.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.Hud;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.state.LivingEntityRenderState;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.zombie.Zombie;
import net.minecraft.world.level.Level;
import net.minecraft.world.scores.*;
/**
* Vanilla's team rules in gameplay, run on stand-in entities whose teams and flags come from each case:
* {@code Entity.doTeamsAllowDamage} (friendly fire), {@code Entity.isAlliedTo}, {@code EntitySelector.pushableBy}
* (collision rules), {@code Entity.isInvisibleTo} (seeing invisible team mates) and
* {@code LivingEntityRenderer.shouldShowName} (name tag visibility) with a stand-in client.
*
* <pre> INPUTS OUTPUT</pre>
*/
public final class VanillaTeamRulesOracle {
static Object unsafe;
static Method allocate;
@SuppressWarnings("unchecked")
static <T> T alloc(Class<T> type) throws Exception {
return (T) allocate.invoke(unsafe, type);
}
static void put(Object target, Class<?> owner, String name, Object value) throws Exception {
Field f = owner.getDeclaredField(name);
f.setAccessible(true);
f.set(target, value);
}
static final class Stub extends Zombie {
PlayerTeam team;
boolean pushable, spectator, invisible, discrete, vehicle;
Level home;
Stub() {
super((Level) null);
}
@Override
public PlayerTeam getTeam() {
return this.team;
}
@Override
public boolean isPushable() {
return this.pushable;
}
@Override
public boolean isSpectator() {
return this.spectator;
}
@Override
public boolean isInvisible() {
return this.invisible;
}
@Override
public boolean isDiscrete() {
return this.discrete;
}
@Override
public boolean isVehicle() {
return this.vehicle;
}
@Override
public Level level() {
return this.home;
}
}
static final class Viewer extends LocalPlayer {
PlayerTeam team;
boolean spectator;
Viewer() {
super(null, null, null, null, null, null, false, null, null);
}
@Override
public PlayerTeam getTeam() {
return this.team;
}
@Override
public boolean isSpectator() {
return this.spectator;
}
}
abstract static class Renderer extends LivingEntityRenderer<LivingEntity, LivingEntityRenderState, net.minecraft.client.model.EntityModel<LivingEntityRenderState>> {
Renderer() {
super(null, null, 0.0F);
}
}
static final class Concrete extends Renderer {
@Override
public Identifier getTextureLocation(final LivingEntityRenderState state) {
return null;
}
@Override
public LivingEntityRenderState createRenderState() {
return null;
}
}
static void options(PlayerTeam t, JsonObject o) {
t.setAllowFriendlyFire(o.get("ff").getAsBoolean());
t.setSeeFriendlyInvisibles(o.get("si").getAsBoolean());
for (Team.CollisionRule r : Team.CollisionRule.values()) {
if (r.getSerializedName().equals(o.get("collision").getAsString())) t.setCollisionRule(r);
}
for (Team.Visibility v : Team.Visibility.values()) {
if (v.getSerializedName().equals(o.get("nametag").getAsString())) t.setNameTagVisibility(v);
}
}
public static void main(String[] args) throws Exception {
net.minecraft.SharedConstants.tryDetectVersion();
net.minecraft.server.Bootstrap.bootStrap();
var unsafeField = Class.forName("sun.misc.Unsafe").getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
unsafe = unsafeField.get(null);
allocate = unsafe.getClass().getMethod("allocateInstance", Class.class);
MethodHandle shouldShowName = MethodHandles.privateLookupIn(LivingEntityRenderer.class, MethodHandles.lookup())
.findVirtual(LivingEntityRenderer.class, "shouldShowName", MethodType.methodType(boolean.class, LivingEntity.class, double.class));
Renderer renderer = alloc(Concrete.class);
ServerLevel level = alloc(ServerLevel.class);
Gson gson = new GsonBuilder().serializeNulls().create();
try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]), StandardCharsets.UTF_8));
BufferedReader in = Files.newBufferedReader(Path.of(args[0]), StandardCharsets.UTF_8)) {
String line;
while ((line = in.readLine()) != null) {
JsonObject c = JsonParser.parseString(line).getAsJsonObject();
Scoreboard board = new Scoreboard();
PlayerTeam a = board.addPlayerTeam("A");
PlayerTeam b = board.addPlayerTeam("B");
options(a, c.getAsJsonObject("A"));
options(b, c.getAsJsonObject("B"));
java.util.function.Function<String, PlayerTeam> team = k -> c.get(k).isJsonNull() ? null : c.get(k).getAsString().equals("A") ? a : b;
Stub own = alloc(Stub.class);
own.team = team.apply("own");
own.home = level;
Stub other = alloc(Stub.class);
other.team = team.apply("other");
other.home = level;
other.pushable = c.get("pushable").getAsBoolean();
other.spectator = c.get("spectator").getAsBoolean();
other.invisible = c.get("invisible").getAsBoolean();
other.discrete = c.get("discrete").getAsBoolean();
other.vehicle = c.get("vehicle").getAsBoolean();
Viewer viewer = alloc(Viewer.class);
viewer.team = team.apply("viewer");
viewer.spectator = c.get("viewer_spectator").getAsBoolean();
Minecraft mc = alloc(Minecraft.class);
put(mc, Minecraft.class, "player", viewer);
Camera camera = alloc(Camera.class);
put(camera, Camera.class, "entity", c.get("camera_is_entity").getAsBoolean() ? other : viewer);
GameRenderer gameRenderer = alloc(GameRenderer.class);
put(gameRenderer, GameRenderer.class, "mainCamera", camera);
put(mc, Minecraft.class, "gameRenderer", gameRenderer);
Hud hud = alloc(Hud.class);
put(hud, Hud.class, "isHidden", c.get("hud_hidden").getAsBoolean());
Gui gui = alloc(Gui.class);
put(gui, Gui.class, "hud", hud);
put(mc, Minecraft.class, "gui", gui);
put(null, Minecraft.class, "instance", mc);
JsonObject r = new JsonObject();
r.add("case", c.get("case"));
try {
r.addProperty("damage", own.doTeamsAllowDamage(other));
r.addProperty("allied", own.isAlliedTo((Entity) other));
r.addProperty("pushes", EntitySelector.pushableBy(own).test(other));
r.addProperty("invisible", other.isInvisibleTo(viewer));
r.addProperty("name", (boolean) shouldShowName.invoke(renderer, (LivingEntity) other, c.get("distance").getAsDouble()));
} catch (Throwable e) {
r.addProperty("error", e.toString());
}
out.println(gson.toJson(r));
}
}
}
}
+109
View File
@@ -0,0 +1,109 @@
#!/usr/bin/env python3
"""Compare the team rules Horizon's gameplay uses with vanilla's.
The Java oracle (VanillaTeamRulesOracle) runs vanilla's own
Entity.doTeamsAllowDamage (friendly fire: player attacks, explosions,
pufferfish, slimes), Entity.isAlliedTo (targeting, sweeping), the
EntitySelector.pushableBy predicate (collision rules), Entity.isInvisibleTo
(team mates who see friendly invisibles) and LivingEntityRenderer.shouldShowName
(name tag visibility, with a stand-in client for the viewer, camera and F1) on
stand-in entities, for every combination of two teams' options, the three
entities' teams (none, either team) and the flags involved.
`examples/team_rules_fixture` answers from `horizon_client::scoreboard`.
test_team_rules_parity.py [--count N]
"""
import argparse
import itertools
import json
import os
import random
import subprocess
import sys
from pathlib import Path
try:
from . import lab, test_rust_parity as base, test_model_baking_parity as baking
except ImportError:
import lab
import test_rust_parity as base
import test_model_baking_parity as baking
PARITY = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity'
RULES = ['always', 'never', 'pushOtherTeams', 'pushOwnTeam']
VISIBILITY = ['always', 'never', 'hideForOtherTeams', 'hideForOwnTeam']
def cases(count):
rng = random.Random(0x5445414d)
out = []
teams = [None, 'A', 'B']
# Every pair of collision rules and name tag visibilities with every team assignment.
for ra, rb, va, own, other, viewer in itertools.product(RULES, RULES, VISIBILITY, teams, teams, teams):
out.append((ra, rb, va, own, other, viewer))
rows = []
for n in range(max(count, len(out))):
ra, rb, va, own, other, viewer = out[n] if n < len(out) else (
rng.choice(RULES), rng.choice(RULES), rng.choice(VISIBILITY), rng.choice(teams), rng.choice(teams), rng.choice(teams))
rows.append({
'case': n,
'A': {'ff': rng.random() < 0.5, 'si': rng.random() < 0.5, 'collision': ra, 'nametag': va},
'B': {'ff': rng.random() < 0.5, 'si': rng.random() < 0.5, 'collision': rb, 'nametag': rng.choice(VISIBILITY)},
'own': own, 'other': other, 'viewer': viewer,
'pushable': rng.random() < 0.8, 'spectator': rng.random() < 0.15, 'invisible': rng.random() < 0.4,
'discrete': rng.random() < 0.3, 'vehicle': rng.random() < 0.15, 'viewer_spectator': rng.random() < 0.15,
'camera_is_entity': rng.random() < 0.1, 'hud_hidden': rng.random() < 0.15,
'distance': rng.choice([0.0, 100.0, 1023.99, 1024.0, 4095.0, 5000.0]),
})
return rows
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--count', type=int, default=6000)
args = parser.parse_args()
lock, jars, _ = base.verify_artifacts()
libraries = baking.client_libraries()
build = lab.LOCAL / 'team-rules-parity'
build.mkdir(parents=True, exist_ok=True)
classes = build / 'classes'
classes.mkdir(exist_ok=True)
dependencies = os.pathsep.join(map(str, [jars['client'], *libraries]))
def run(command, name):
with (build / (name + '.log')).open('w') as log:
result = subprocess.run(list(map(str, command)), cwd=lab.ROOT, timeout=3600, stdout=log, stderr=subprocess.STDOUT)
if result.returncode:
print((build / (name + '.log')).read_text()[-4000:])
sys.exit(f'{name} failed')
chosen = cases(args.count)
inputs = build / 'inputs.jsonl'
inputs.write_text(''.join(json.dumps(c) + '\n' for c in chosen))
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-cp', dependencies, '-d', classes,
PARITY / 'VanillaTeamRulesOracle.java'], 'compile-java')
run(['java', '-Xmx1G', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.VanillaTeamRulesOracle',
inputs, build / 'vanilla.jsonl'], 'vanilla')
run(['cargo', 'build', '--release', '-p', 'horizon-client', '--example', 'team_rules_fixture'], 'compile-rust')
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release/examples' / ('team_rules_fixture.exe' if os.name == 'nt' else 'team_rules_fixture')
run([binary, inputs, build / 'rust.jsonl'], 'rust')
vanilla = [json.loads(l) for l in (build / 'vanilla.jsonl').read_text().splitlines()]
rust = [json.loads(l) for l in (build / 'rust.jsonl').read_text().splitlines()]
assert len(vanilla) == len(rust) == len(chosen)
errors = [v for v in vanilla if 'error' in v]
bad = [(c, v, r) for c, v, r in zip(chosen, vanilla, rust) if 'error' not in v and v != r]
for v in errors[:3]:
print('ORACLE ERROR', v)
for c, v, r in bad[:5]:
print('MISMATCH', json.dumps(c))
print(' vanilla', json.dumps(v))
print(' horizon', json.dumps(r))
print(f'{len(vanilla) - len(bad) - len(errors)}/{len(vanilla)} team rule cases match; {len(errors)} oracle errors')
if bad or errors:
sys.exit(1)
print('team rules parity ok')
if __name__ == '__main__':
main()