diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs index 417e28c0f..157baa74c 100644 --- a/crates/horizon-client/examples/probe/mob_ai_probe.rs +++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs @@ -1375,6 +1375,31 @@ pub fn main() -> horizon_client::Result<()> { world.set(at[0], at[1], at[2], registry.resolve(&st)?)?; } } + // Doors villagers opened or closed (DoorBlock.setOpen), both halves. + let doors: Vec<([i32; 3], bool)> = events + .iter() + .filter_map(|e| match e { + MobEvent::SetDoorOpen { pos, open } => Some((*pos, *open)), + _ => None, + }) + .collect(); + for (at, open) in doors { + let Some(half) = registry + .state(world.block(at[0], at[1], at[2])) + .and_then(|s| s.properties.get("half").cloned()) + else { + continue; + }; + let other = if half == "lower" { 1 } else { -1 }; + for y in [at[1], at[1] + other] { + if let Some(mut st) = registry.state(world.block(at[0], y, at[2])).cloned() + && st.properties.contains_key("open") + { + st.properties.insert("open".to_owned(), open.to_string()); + world.set(at[0], y, at[2], registry.resolve(&st)?)?; + } + } + } mobs.append(&mut born); mobs.append(&mut added); for e in &heart_events { diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 6f47fd632..7a685c48d 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1007,6 +1007,11 @@ impl Game<'_> { MobEvent::SetBlock { pos, state } => { self.world.set(pos[0], pos[1], pos[2], state)? } + MobEvent::SetDoorOpen { pos, open } => { + let pos = BlockPos::new(pos[0], pos[1], pos[2]); + self.with_level(|b, level, ticks| b.set_door_open(level, ticks, pos, open)); + self.process_level_events()?; + } MobEvent::SetBlockName { pos, name, effect } => { if effect { let old = self.world.block(pos[0], pos[1], pos[2]); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index e020b92a4..47bd8a73e 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -987,6 +987,12 @@ pub enum MobEvent { pos: [i32; 3], state: horizon_world::registry::BlockStateId, }, + /// `DoorBlock.setOpen` by a mob (`InteractWithDoor`): the door opens or + /// closes, both halves, with its sound and game event. + SetDoorOpen { + pos: [i32; 3], + open: bool, + }, /// Destroy a block, dropping nothing (sheep eating grass becomes dirt via SetBlock). BreakBlock { pos: [i32; 3], diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 336dc6b93..74d34e2fd 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -2260,6 +2260,7 @@ impl B { } t.begin(cx.lr, cx.now); if now > *next_ok { + close_doors(m, cx, None, None); if super::villager::start_sleeping(m, home, w) { mem(m).last_slept.set(now); } @@ -3500,7 +3501,74 @@ fn walk_to_block_memory( } /// `InteractWithDoor`: open the doors on the path, close the ones passed. fn interact_with_doors(m: &mut Mob, cx: &mut Bx, from: PathNode, to: PathNode) { - let _ = (m, cx, from, to); + let (from, to) = ([from.x, from.y, from.z], [to.x, to.y, to.z]); + // The door being left is remembered even when it is open already; the + // one ahead only when this villager opens it. + for (p, always) in [(from, true), (to, false)] { + let Some(open) = door_open(cx.w, p) else { + continue; + }; + if !open { + cx.events.push(MobEvent::SetDoorOpen { pos: p, open: true }); + } + if always || !open { + remember_door(m, p); + } + } + close_doors(m, cx, Some(from), Some(to)); +} +/// Whether the block at `p` is a door mobs open (`MOB_INTERACTABLE_DOORS`), and +/// then whether it is open. +fn door_open(w: &MobWorld, p: [i32; 3]) -> Option { + let name = w.block_name(p[0], p[1], p[2]); + (name.ends_with("_door") && w.has_tag("minecraft:mob_interactable_doors", name)) + .then(|| w.prop(p[0], p[1], p[2], "open") == Some("true")) +} +fn remember_door(m: &mut Mob, p: [i32; 3]) { + let doors = &mut mem(m).doors_to_close; + match doors.get_mut() { + Some(list) if list.contains(&p) => {} + Some(list) => list.push(p), + None => doors.set(vec![p]), + } +} +/// `InteractWithDoor.closeDoorsThatIHaveOpenedOrPassedThrough`: the remembered +/// doors other than the two of the step being taken are forgotten, and closed +/// when still open, near (3 blocks) and no other villager within 2 blocks of +/// one is passing through it. +fn close_doors(m: &mut Mob, cx: &mut Bx, from: Option<[i32; 3]>, to: Option<[i32; 3]>) { + let Some(doors) = mem(m).doors_to_close.get().cloned() else { + return; + }; + let near = mem(m).mobs.value().unwrap_or_default(); + let mut kept = Vec::new(); + for door in doors { + if from == Some(door) || to == Some(door) { + kept.push(door); + continue; + } + if !closer_to_center(door, m.position, 3.0) || door_open(cx.w, door) != Some(true) { + continue; + } + let passing = near.iter().any(|&who| { + let Who::Mob(id) = who else { return false }; + cx.w.seen(id).is_some_and(|s| { + s.species == Species::Villager + && closer_to_center(door, s.position, 2.0) + && s.villager + .as_ref() + .and_then(|v| v.path_nodes) + .is_some_and(|(prev, next)| prev.is_some_and(|p| p == door || next == door)) + }) + }); + if !passing { + cx.events.push(MobEvent::SetDoorOpen { + pos: door, + open: false, + }); + } + } + mem(m).doors_to_close.set(kept); } /// The scan order of POI candidates (before the nearest-first sort). fn pois_scan_order(_m: &Mob, found: &[(Poi, [i32; 3])]) -> Vec { diff --git a/crates/horizon-world/src/level/interact.rs b/crates/horizon-world/src/level/interact.rs index c38d81f26..b8251ba46 100644 --- a/crates/horizon-world/src/level/interact.rs +++ b/crates/horizon-world/src/level/interact.rs @@ -543,6 +543,33 @@ impl Behaviours { self.sim(level, ticks) .use_without_item(pos, face, hit, player) } + /// `DoorBlock.setOpen`: a mob opens or closes the door at `pos` (either half), with its sound + /// and game event. False when it is no door or already so. + pub fn set_door_open( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + open: bool, + ) -> bool { + let state = level.block_state(pos); + if !self.data(state).is(Class::Door) || self.table.bool(state, self.props.open) == open { + return false; + } + let mut sim = self.sim(level, ticks); + let next = self.table.set_bool(state, self.props.open, open); + sim.set_block(pos, next, 10); + sim.door_sound(pos, next, open); + sim.game_event( + pos, + if open { + "minecraft:block_open" + } else { + "minecraft:block_close" + }, + ); + true + } /// `BlockState.useItemOn` with `item` in the main hand, then, when the /// block asks to try with an empty hand (the default), `useWithoutItem`. /// `SweetBerryBushBlock.useItemOn` passes bone meal on a bush that can diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index cba3d283c..f44a84ea8 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -257,7 +257,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'styles', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'border', 'particles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'hands', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'styles', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'border', 'particles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'doors', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'hands', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] class Game: @@ -4152,6 +4152,54 @@ def check_growth(host, guest, out): return detail +def check_doors(host, guest, out): + """A villager shut in a room opens its oak door on its way to a free composter outside + (InteractWithDoor), takes the composter as its job site and closes the door behind it; the + guest sees the door open and close again.""" + gx, gy, gz = (int(v // 1) for v in pos_of(guest)) + x, y, z = gx + 4, gy, gz - 8 + lower, upper, composter, marker = (x + 2, y, z), (x + 2, y + 1, z), (x + 7, y, z), (x + 7, y, z + 2) + tag = 'dg_door_villager' + farmer = (f'execute if entity @e[type=minecraft:villager,tag={tag},' + f'nbt={{VillagerData:{{profession:"minecraft:farmer"}}}}] run setblock {marker[0]} {marker[1]} {marker[2]} ' + 'minecraft:gold_block') + seen = {'opened': False, 'farmer': False, 'closed': None} + try: + host('command', command='time set 2000') + host('command', command=f'fill {x - 3} {y - 1} {z - 3} {x + 9} {y - 1} {z + 3} minecraft:stone') + host('command', command=f'fill {x - 3} {y} {z - 3} {x + 9} {y + 3} {z + 3} minecraft:air') + host('command', command=f'fill {x - 2} {y} {z - 2} {x + 2} {y + 2} {z + 2} minecraft:oak_planks') + host('command', command=f'fill {x - 1} {y} {z - 1} {x + 1} {y + 1} {z + 1} minecraft:air') + for (bx, by, bz), half in ((lower, 'lower'), (upper, 'upper')): + host('command', command=f'setblock {bx} {by} {bz} minecraft:oak_door[facing=east,half={half},' + 'hinge=left,open=false,powered=false]') + host('command', command=f'setblock {composter[0]} {composter[1]} {composter[2]} minecraft:composter') + host('command', command=f'summon minecraft:villager {x + 0.5} {y} {z + 0.5} {{Tags:["{tag}"],' + 'PersistenceRequired:1b,VillagerData:{profession:"minecraft:none",level:1,' + 'type:"minecraft:plains"}}') + for _ in range(120): + guest.wait(10) + seen['opened'] = seen['opened'] or state_of(guest, lower, 'open') == 'true' + host('command', command=farmer) + if host.raw('block', *marker).get('block') == 'minecraft:gold_block': + seen['farmer'] = True + break + seen['closed'] = wait_state(guest, lower, 'open', 'false', 200) == 'false' + seen['server_door'] = state_of(host, lower, 'open') + guest.raw('look-at', f'{lower[0] + 0.5:.2f}', f'{lower[1] + 1:.2f}', f'{lower[2] + 0.5:.2f}') + guest.wait(10) + guest.shot(out, 'doors') + finally: + host('command', command=f'kill @e[type=minecraft:villager,tag={tag}]') + host('command', command=f'fill {x - 3} {y} {z - 3} {x + 9} {y + 3} {z + 3} minecraft:air') + host('command', command='time set day') + assert seen['opened'], f'the villager never opened its door: {seen}' + assert seen['farmer'], f'the villager did not take the composter outside: {seen}' + assert seen['closed'] and seen['server_door'] == 'false', f'the villager left its door open: {seen}' + return seen + + + def check_light(host, guest, out): gx, gy, gz = (int(v // 1) for v in pos_of(guest)) results = {} @@ -4413,7 +4461,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'eat': check_eat, 'eating': check_eating, 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, 'blast': check_blast, 'light': check_light, 'plates': check_plates, 'swings': check_swings, - 'cracks': check_cracks, 'time': check_time, 'growth': check_growth, + 'cracks': check_cracks, 'time': check_time, 'growth': check_growth, 'doors': check_doors, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict, 'ladder': check_ladder,