From 1dd02f887c252206ad6cad4bbcf1bbd3cfcbddfd Mon Sep 17 00:00:00 2001 From: Tanner Date: Thu, 19 Nov 2026 17:23:42 +0000 Subject: [PATCH] Allow bot names 1-9 instead of 1-3 --- README.md | 4 ++-- mosfet/utils.py | 10 +++++---- mosfet/world.py | 36 ++++++-------------------------- tests/test_inventory_commands.py | 7 +++++++ tests/test_nearby_bots.py | 18 +++++++++------- tests/test_safety_signs.py | 4 +++- tests/test_signs.py | 36 ++++++++++++++++++++++++++++++-- tests/test_storage_labels.py | 4 +++- tests/test_throw_signs.py | 4 ++++ 9 files changed, 75 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 0212943..d3038df 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,8 @@ The bot reads both the front and back of loaded signs. Use `!signs [distance]` to inspect nearby text and coordinates; the default distance is 32 blocks. Sign matching is case-insensitive. For bot-addressed signs, the first word must be the bot's full name (for example, `mosfet1`) or its group name (for example, -`mosfet`, which addresses bots ending in `1`, `2`, or `3`). A colon after the -address is optional. +`mosfet`, which addresses bots ending in any digit from `1` through `9`). A +colon after the address is optional. ### Sand origin diff --git a/mosfet/utils.py b/mosfet/utils.py index c574e03..15dfe0f 100644 --- a/mosfet/utils.py +++ b/mosfet/utils.py @@ -1,6 +1,5 @@ import importlib import collections -import re from math import floor, ceil, sqrt from mosfet.info import blocks @@ -26,12 +25,15 @@ def phyp(p1, p2): def bot_name_family(name): - """Return a bot username with all digits removed, case-insensitively.""" - return re.sub(r'\d', '', str(name)).casefold() + """Return the group name for a bot ending in one digit from 1 to 9.""" + name = str(name or '').casefold() + if len(name) > 1 and name[-1] in '123456789': + return name[:-1] + return name def find_nearby_bots(global_state, position, distance): - """Find nearby online bots sharing this bot's digits-stripped name.""" + """Find nearby online bots sharing this bot's numbered name family.""" own_name = str(global_state.get('name', '') or '') family = bot_name_family(own_name) if not family: diff --git a/mosfet/world.py b/mosfet/world.py index f76dd9f..e852266 100644 --- a/mosfet/world.py +++ b/mosfet/world.py @@ -125,11 +125,7 @@ class World: def find_throw_signs(self, center, bot_name, distance=105): """Find nearby signs directing this bot to throw an item at them.""" bot_name = str(bot_name).casefold() - group_name = (bot_name[:-1] if len(bot_name) > 1 and - bot_name[-1] in '123' else None) - addresses = {bot_name} - if group_name: - addresses.add(group_name) + addresses = {bot_name, utils.bot_name_family(bot_name)} result = [] for sign in self.find_signs(center, distance): @@ -163,11 +159,7 @@ class World: def find_sand_origins(self, center, bot_name, distance=200): """Find signs addressed to this bot that designate a sand origin.""" bot_name = str(bot_name).casefold() - group_name = (bot_name[:-1] if len(bot_name) > 1 and - bot_name[-1] in '123' else None) - addresses = {bot_name} - if group_name: - addresses.add(group_name) + addresses = {bot_name, utils.bot_name_family(bot_name)} result = [] for sign in self.find_signs(center, distance): @@ -188,11 +180,7 @@ class World: def find_safety_signs(self, center, bot_name, distance=80): """Find nearby signs addressed to this bot that mark safe refuges.""" bot_name = str(bot_name).casefold() - group_name = (bot_name[:-1] if len(bot_name) > 1 and - bot_name[-1] in '123' else None) - addresses = {bot_name} - if group_name: - addresses.add(group_name) + addresses = {bot_name, utils.bot_name_family(bot_name)} result = [] for sign in self.find_signs(center, distance): @@ -360,11 +348,7 @@ class World: def find_cache_chests(self, center, bot_name, distance=105): """Find bot/group-addressed general cache chests from signs.""" bot_name = str(bot_name).casefold() - group_name = (bot_name[:-1] if len(bot_name) > 1 and - bot_name[-1] in '123' else None) - addresses = {bot_name} - if group_name: - addresses.add(group_name) + addresses = {bot_name, utils.bot_name_family(bot_name)} grouped = {} for sign in self.find_signs(center, distance): @@ -392,11 +376,7 @@ class World: def find_supply_chests(self, center, bot_name, distance=80): """Find chests whose sign is addressed to this bot's supplies.""" bot_name = str(bot_name).casefold() - group_name = (bot_name[:-1] if len(bot_name) > 1 and - bot_name[-1] in '123' else None) - addresses = {bot_name} - if group_name: - addresses.add(group_name) + addresses = {bot_name, utils.bot_name_family(bot_name)} grouped = {} sign_offsets = ( @@ -442,11 +422,7 @@ class World: def sign_policies(self, bot_name, distance=None): """Parse sign policies addressed to this bot or its numbered group.""" bot_name = str(bot_name).casefold() - group_name = (bot_name[:-1] if len(bot_name) > 1 and - bot_name[-1] in '123' else None) - addresses = {bot_name} - if group_name: - addresses.add(group_name) + addresses = {bot_name, utils.bot_name_family(bot_name)} center = utils.pint(self.g.pos) policies = [] diff --git a/tests/test_inventory_commands.py b/tests/test_inventory_commands.py index 7be6f89..4a74a86 100644 --- a/tests/test_inventory_commands.py +++ b/tests/test_inventory_commands.py @@ -41,6 +41,13 @@ class TestInventoryCommands(unittest.TestCase): self.commands.handle_chat(('CHAT', 'tester', 'mosfet1: 1drop')) self.assertEqual(self.chat.sent, ['ok']) + def test_command_prefix_uses_the_ninth_bot_number(self): + self.commands.g.name = 'mosfet9' + + self.commands.handle_chat(('CHAT', 'tester', 'mosfet9: 9drop')) + + self.assertEqual(self.chat.sent, ['ok']) + def test_dump_command_starts_dump_for_item_id(self): self.commands.handle_chat(('CHAT', 'tester', 'mosfet1: 1dump 62')) self.assertEqual(self.game.dump_item, 62) diff --git a/tests/test_nearby_bots.py b/tests/test_nearby_bots.py index 661d2a6..c150c88 100644 --- a/tests/test_nearby_bots.py +++ b/tests/test_nearby_bots.py @@ -5,11 +5,13 @@ from mosfet import utils class NearbyBotTests(unittest.TestCase): - def test_family_removes_every_digit(self): - self.assertEqual(utils.bot_name_family('foo1'), 'foo') - self.assertEqual(utils.bot_name_family('foo2'), 'foo') - self.assertEqual(utils.bot_name_family('bar1'), 'bar') - self.assertEqual(utils.bot_name_family('bar123'), 'bar') + def test_family_strips_only_a_valid_single_digit_suffix(self): + for suffix in range(1, 10): + self.assertEqual( + utils.bot_name_family('foo{}'.format(suffix)), 'foo') + self.assertEqual(utils.bot_name_family('FOO9'), 'foo') + self.assertEqual(utils.bot_name_family('bar123'), 'bar12') + self.assertEqual(utils.bot_name_family('foo10'), 'foo10') def test_finds_only_nearby_other_bots_in_same_name_family(self): global_state = Munch( @@ -22,7 +24,7 @@ class NearbyBotTests(unittest.TestCase): }, player_names={ 'self': 'bar1', - 'bar-uuid': 'bar123', + 'bar-uuid': 'bar9', 'foo-uuid': 'foo2', 'far-uuid': 'bar2', }) @@ -30,12 +32,12 @@ class NearbyBotTests(unittest.TestCase): nearby = utils.find_nearby_bots( global_state, (0, 64, 0), 4) - self.assertEqual([bot['name'] for bot in nearby], ['bar123']) + self.assertEqual([bot['name'] for bot in nearby], ['bar9']) self.assertEqual(nearby[0]['distance'], 3.0) def test_returns_no_matches_without_a_bot_name_family(self): self.assertEqual(utils.find_nearby_bots( - Munch(name='123', players={}, player_names={}), + Munch(name='', players={}, player_names={}), (0, 64, 0), 4), []) diff --git a/tests/test_safety_signs.py b/tests/test_safety_signs.py index 23e8ae9..c101485 100644 --- a/tests/test_safety_signs.py +++ b/tests/test_safety_signs.py @@ -48,8 +48,10 @@ class SafetySignTests(unittest.TestCase): self.assertEqual(bot2[1]['normal'], (-1.0, 0.0, 0.0)) self.assertEqual([sign['position'] for sign in world.find_safety_signs( (0, 64, 0), 'mosfet1', 10)], [group_sign]) + self.assertEqual([sign['position'] for sign in world.find_safety_signs( + (0, 64, 0), 'mosfet9', 10)], [group_sign]) self.assertEqual(world.find_safety_signs( - (0, 64, 0), 'mosfet4', 10), []) + (0, 64, 0), 'mosfet10', 10), []) def test_safety_openings_try_text_facing_standable_positions_first(self): sign_pos = (0, 64, 0) diff --git a/tests/test_signs.py b/tests/test_signs.py index baaf632..e78518f 100644 --- a/tests/test_signs.py +++ b/tests/test_signs.py @@ -93,10 +93,11 @@ class SignReadingTests(unittest.TestCase): 'mosfet', 'stay within25', 'square', '', ]}}}, }), pos=(0, 64, 0))) - for bot_name in ('mosfet1', 'mosfet2', 'mosfet3'): + for bot_num in range(1, 10): + bot_name = 'mosfet{}'.format(bot_num) self.assertEqual([p['shape'] for p in group_world.sign_policies(bot_name)], ['square']) - self.assertEqual(group_world.sign_policies('mosfet4'), []) + self.assertEqual(group_world.sign_policies('mosfet10'), []) individual_world = World(Munch(chunks=Munch(block_entities={ (1, 64, 0): {'data': {'front_text': {'messages': [ @@ -131,6 +132,37 @@ class SignReadingTests(unittest.TestCase): world.find_sand_origins((0, 64, 0), 'mosfet2')], [(1, 64, 0), (2, 64, 0)], ) + self.assertEqual( + [origin['position'] for origin in + world.find_sand_origins((0, 64, 0), 'mosfet9')], + [(1, 64, 0)], + ) + self.assertEqual( + world.find_sand_origins((0, 64, 0), 'mosfet10'), []) + + def test_supply_sign_group_address_includes_bot_nine_only(self): + sign_pos = (0, 64, 0) + chest_pos = (1, 64, 0) + sign_state = blocks.MCD_BLOCKS['oak_wall_sign']['minStateId'] + chest_state = next( + state for state in blocks.CHEST_IDS + if blocks.PROPS[state].get('type') == 'single') + block_states = {sign_pos: sign_state, chest_pos: chest_state} + chunks = Munch( + block_entities={sign_pos: {'data': {'front_text': { + 'messages': ['mosfet supplies', '', '', ''], + }}}}, + index={}, + get_block_at=lambda *pos: block_states.get(tuple(pos), blocks.AIR), + ) + world = World(Munch(chunks=chunks)) + + supplies = world.find_supply_chests(sign_pos, 'mosfet9', 10) + + self.assertEqual(len(supplies), 1) + self.assertEqual(supplies[0]['positions'], (chest_pos,)) + self.assertEqual(world.find_supply_chests( + sign_pos, 'mosfet10', 10), []) def test_sign_policies_read_any_distance_within_loaded_signs(self): position = (100, 64, 0) diff --git a/tests/test_storage_labels.py b/tests/test_storage_labels.py index 0d9977a..cde4624 100644 --- a/tests/test_storage_labels.py +++ b/tests/test_storage_labels.py @@ -99,7 +99,9 @@ class StorageLabelTests(unittest.TestCase): self.assertEqual(group_caches[0]['position'], side_chest) self.assertEqual(group_caches[0]['positions'], (side_chest,)) self.assertEqual(world.find_cache_chests( - sign_pos, 'mosfet4', 10), []) + sign_pos, 'mosfet9', 10), group_caches) + self.assertEqual(world.find_cache_chests( + sign_pos, 'mosfet10', 10), []) def test_side_chest_takes_priority_over_chest_below_sign(self): sign_pos = (0, 64, 0) diff --git a/tests/test_throw_signs.py b/tests/test_throw_signs.py index e0ebb20..452091d 100644 --- a/tests/test_throw_signs.py +++ b/tests/test_throw_signs.py @@ -64,6 +64,10 @@ class ThrowSignTests(unittest.TestCase): bot1 = world.find_throw_signs((0, 64, 0), 'mosfet1', 10) self.assertEqual([sign['position'] for sign in bot1], [group_sign]) + bot9 = world.find_throw_signs((0, 64, 0), 'mosfet9', 10) + self.assertEqual([sign['position'] for sign in bot9], [group_sign]) + self.assertEqual(world.find_throw_signs( + (0, 64, 0), 'mosfet10', 10), []) def test_throw_accepts_unqualified_items_and_species_log_labels(self): single_item_sign = (0, 64, 0)