Add combat job with safe target filtering
This commit is contained in:
1 parent
2ecaa9a30a
commit
1b9f973db3
12 files changed
+486
-7
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import unittest
|
||||
|
||||
from munch import Munch
|
||||
from minecraft.networking.modern import ClientTickEnd, PunchPacket
|
||||
from minecraft.networking.packets import PacketBuffer
|
||||
|
||||
from mosfet.game import Game
|
||||
from mosfet.protocol.modern import AttackEntityPacket
|
||||
|
||||
|
||||
class FakeConnection:
|
||||
def __init__(self):
|
||||
self.packets = []
|
||||
|
||||
def write_packet(self, packet, force=False):
|
||||
self.packets.append((packet, force))
|
||||
|
||||
|
||||
class AttackPacketTests(unittest.TestCase):
|
||||
def test_attack_packet_encodes_protocol_777_entity_id(self):
|
||||
packet = AttackEntityPacket(entity_id=300)
|
||||
payload = PacketBuffer()
|
||||
|
||||
packet.write_fields(payload)
|
||||
|
||||
self.assertEqual(packet.id, 0x01)
|
||||
self.assertEqual(payload.get_writable(), bytes.fromhex('ac02'))
|
||||
|
||||
def test_game_attack_sends_attack_and_visible_main_hand_swing(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(connection=connection)
|
||||
|
||||
game.attack_entity(17)
|
||||
|
||||
self.assertEqual(
|
||||
[type(packet) for packet, _force in connection.packets],
|
||||
[AttackEntityPacket, ClientTickEnd, PunchPacket, ClientTickEnd])
|
||||
self.assertEqual(connection.packets[0][0].entity_id, 17)
|
||||
self.assertTrue(all(force for _packet, force in connection.packets))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -5,7 +5,8 @@ from munch import Munch
|
||||
from mosfet.bot import _recover_invalid_path
|
||||
from mosfet.game import Game
|
||||
from mosfet.info import blocks
|
||||
from mosfet.path import Pathfinder
|
||||
from mosfet.path import AStarTimeout, Pathfinder
|
||||
from mosfet.world import World
|
||||
|
||||
|
||||
class FakeChunks:
|
||||
@@ -44,6 +45,37 @@ class DoorNavigationTests(unittest.TestCase):
|
||||
finder = Pathfinder(g)
|
||||
self.assertFalse(finder.bair((1, 64, 0)))
|
||||
|
||||
def test_world_pathfinder_receives_custom_timeout(self):
|
||||
calls = []
|
||||
|
||||
class FakePathfinder:
|
||||
def __init__(self, global_state, timeout):
|
||||
calls.append((global_state, timeout))
|
||||
|
||||
@staticmethod
|
||||
def astar(start, destination):
|
||||
return [start, destination]
|
||||
|
||||
world = World.__new__(World)
|
||||
world.g = Munch(job=None)
|
||||
with patch('mosfet.world.path.Pathfinder', FakePathfinder):
|
||||
route = world.path_to_place(
|
||||
(0, 64, 0), (1, 64, 0), timeout=0.1)
|
||||
|
||||
self.assertEqual(route, [(0, 64, 0), (1, 64, 0)])
|
||||
self.assertEqual(calls, [(world.g, 0.1)])
|
||||
|
||||
def test_pathfinder_honors_custom_search_timeout(self):
|
||||
g = Munch(chunks=FakeChunks({}), crawling=False,
|
||||
unopenable_doors=set())
|
||||
with patch('mosfet.path.time.monotonic', return_value=10.0):
|
||||
finder = Pathfinder(g, timeout=0.1)
|
||||
|
||||
self.assertEqual(finder.timeout, 0.1)
|
||||
with patch('mosfet.path.time.monotonic', return_value=10.101):
|
||||
with self.assertRaises(AStarTimeout):
|
||||
finder.neighbors((0, 64, 0))
|
||||
|
||||
def test_path_prefix_detects_new_obstacles_and_missing_support(self):
|
||||
class TerrainChunks:
|
||||
def __init__(self):
|
||||
|
||||
Reference in new issue
Block a user