123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- # coding: utf-8
- import pytest
- from freezegun import freeze_time
- from synergine2_xyz.move.intention import MoveToIntention
- from synergine2_xyz.simulation import XYZSimulation
-
- from opencombat.simulation.move import MoveWithRotationBehaviour
- from opencombat.simulation.move import MoveToMechanism
- from opencombat.simulation.move import MoveBehaviour
- from opencombat.simulation.move import SubjectFinishMoveEvent
- from opencombat.simulation.move import SubjectStartRotationEvent
- from opencombat.simulation.move import SubjectFinishRotationEvent
- from opencombat.simulation.move import SubjectContinueRotationEvent
- from opencombat.simulation.move import SubjectStartTileMoveEvent
- from opencombat.simulation.move import SubjectContinueTileMoveEvent
- from opencombat.simulation.move import SubjectFinishTileMoveEvent
- from opencombat.simulation.subject import TankSubject
- from opencombat.simulation.subject import ManSubject
- from opencombat.user_action import UserAction
-
-
- def test_move_and_rotate_behaviour__begin_rotate(config):
- simulation = XYZSimulation(config)
- simulation.physics.graph.add_edge('0.0', '1.1', {})
- simulation.physics.graph.add_edge('1.1', '2.1', {})
-
- subject = TankSubject(
- config,
- simulation,
- position=(0, 0),
- )
- move = MoveToIntention(
- to=(2, 1),
- gui_action=UserAction.ORDER_MOVE,
- )
- subject.intentions.set(move)
-
- move_behaviour = MoveWithRotationBehaviour(
- config=config,
- simulation=simulation,
- subject=subject,
- )
-
- # Rotation required to begin move
- with freeze_time("2000-01-01 00:00:00", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,'gui_action': UserAction.ORDER_MOVE,
- 'path': [
- (0, 0),
- (1, 1),
- (2, 1),
- ],
- 'rotate_relative': 45,
- 'rotate_absolute': 45,
- } == data
-
- events = move_behaviour.action(data)
- assert events
- assert 1 == len(events)
- assert isinstance(events[0], SubjectStartRotationEvent)
- assert 45.0 == events[0].rotate_relative
- assert 4.9995 == events[0].duration
- assert subject.position == (0, 0)
- assert subject.direction == 0
- assert subject.rotate_to == 45
- assert subject.start_rotation == 946684800.0
- assert subject.rotate_duration == 4.9995
- assert subject.intentions.get(MoveToIntention)
-
- # This is 1 second before end of rotation
- with freeze_time("2000-01-01 00:00:04", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'rotate_relative': 45,
- 'rotate_absolute': 45,
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueRotationEvent)
- assert 9 == round(events[0].rotate_relative)
- assert 0.9995 == events[0].duration
- assert subject.position == (0, 0)
- assert int(subject.direction) == 36
- assert subject.rotate_to == 45
- assert subject.start_rotation == 946684804.0
- assert subject.rotate_duration == 0.9995
- assert subject.intentions.get(MoveToIntention)
-
- # We are now just after rotation duration, a move will start
- with freeze_time("2000-01-01 00:00:05", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (1, 1),
- 'rotate_to_finished': 45,
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[1], SubjectStartTileMoveEvent)
- assert isinstance(events[0], SubjectFinishRotationEvent)
- assert (1, 1) == events[1].move_to
- assert 9.0 == events[1].duration
- assert subject.position == (0, 0)
- assert subject.moving_to == (1, 1)
- assert subject.move_duration == 9.0
- assert subject.start_move == 946684805.0
- assert subject.intentions.get(MoveToIntention)
-
- # We are during the move
- with freeze_time("2000-01-01 00:00:13", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (1, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueTileMoveEvent)
- assert (1, 1) == events[0].move_to
- assert 1.0 == events[0].duration
- assert subject.intentions.get(MoveToIntention)
-
- # We are after the move
- with freeze_time("2000-01-01 00:00:14", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to_finished': (1, 1),
- 'rotate_relative': 45,
- 'rotate_absolute': 90,
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[0], SubjectFinishTileMoveEvent)
- assert isinstance(events[1], SubjectStartRotationEvent)
- assert (1, 1) == events[0].move_to
- assert 4.9995 == events[1].duration
- assert 45 == events[1].rotate_relative
- assert (1, 1) == subject.position
- assert (-1, -1) == subject.moving_to
- assert -1 == subject.start_move
- assert -1 == subject.move_duration
- assert subject.rotate_to == 90
- assert subject.start_rotation == 946684814.0
- assert subject.rotate_duration == 4.9995
- assert subject.intentions.get(MoveToIntention)
-
- # We are rotating
- with freeze_time("2000-01-01 00:00:18", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'rotate_relative': 45,
- 'rotate_absolute': 90,
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueRotationEvent)
- assert 9 == round(events[0].rotate_relative)
- assert 0.9995 == events[0].duration
- assert subject.position == (1, 1)
- assert int(subject.direction) == 81
- assert subject.rotate_to == 90
- assert subject.start_rotation == 946684818.0
- assert subject.rotate_duration == 0.9995
- assert subject.intentions.get(MoveToIntention)
-
- # We finish rotating and start to move to final tile
- with freeze_time("2000-01-01 00:00:19", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (2, 1),
- 'rotate_to_finished': 90,
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[1], SubjectStartTileMoveEvent)
- assert isinstance(events[0], SubjectFinishRotationEvent)
- assert (2, 1) == events[1].move_to
- assert 9.0 == events[1].duration
- assert subject.position == (1, 1)
- assert subject.moving_to == (2, 1)
- assert subject.move_duration == 9.0
- assert subject.start_move == 946684819.0
- assert subject.intentions.get(MoveToIntention)
-
- # We are moving to final tile
- with freeze_time("2000-01-01 00:00:27", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (2, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueTileMoveEvent)
- assert (2, 1) == events[0].move_to
- assert 1.0 == events[0].duration
- assert subject.intentions.get(MoveToIntention)
-
- # We arrived on final tile
- with freeze_time("2000-01-01 00:00:28", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'move_to_finished': (2, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectFinishMoveEvent)
- assert (2, 1) == events[0].move_to
- assert (2, 1) == subject.position
- assert (-1, -1) == subject.moving_to
- assert -1 == subject.start_move
- assert -1 == subject.move_duration
- with pytest.raises(KeyError):
- assert subject.intentions.get(MoveToIntention)
-
-
- def test_move_and_rotate_behaviour__begin_move(config):
- simulation = XYZSimulation(config)
- simulation.physics.graph.add_edge('0.0', '0.1', {})
- simulation.physics.graph.add_edge('0.1', '1.1', {})
-
- subject = TankSubject(
- config,
- simulation,
- position=(0, 0),
- )
- move = MoveToIntention(
- to=(1, 1),
- gui_action=UserAction.ORDER_MOVE,
- )
- subject.intentions.set(move)
-
- move_behaviour = MoveWithRotationBehaviour(
- config=config,
- simulation=simulation,
- subject=subject,
- )
-
- # First is a move
- with freeze_time("2000-01-01 00:00:00", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'path': [
- (0, 0),
- (0, 1),
- (1, 1),
- ],
- 'tile_move_to': (0, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectStartTileMoveEvent)
- assert (0, 1) == events[0].move_to
- assert 9.0 == events[0].duration
- assert subject.intentions.get(MoveToIntention)
-
- # Continue the move, rest 1s
- with freeze_time("2000-01-01 00:00:08", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (0, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueTileMoveEvent)
- assert (0, 1) == events[0].move_to
- assert 1.0 == events[0].duration
- assert subject.intentions.get(MoveToIntention)
-
- # Tile move finished, begin a rotate
- with freeze_time("2000-01-01 00:00:09", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to_finished': (0, 1),
- 'rotate_relative': 90,
- 'rotate_absolute': 90,
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[0], SubjectFinishTileMoveEvent)
- assert isinstance(events[1], SubjectStartRotationEvent)
- assert (0, 1) == events[0].move_to
- assert 10 == round(events[1].duration)
- assert 90 == events[1].rotate_relative
- assert (0, 1) == subject.position
- assert (-1, -1) == subject.moving_to
- assert -1 == subject.start_move
- assert -1 == subject.move_duration
- assert subject.rotate_to == 90
- assert subject.start_rotation == 946684809.0
- assert round(subject.rotate_duration) == 10
- assert subject.intentions.get(MoveToIntention)
-
- # We are rotating, rest 1s
- with freeze_time("2000-01-01 00:00:18", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'rotate_relative': 90,
- 'rotate_absolute': 90,
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueRotationEvent)
- assert 9 == round(events[0].rotate_relative)
- assert 1 == round(events[0].duration)
- assert subject.position == (0, 1)
- assert int(subject.direction) == 81
- assert subject.rotate_to == 90
- assert subject.start_rotation == 946684818.0
- assert round(subject.rotate_duration) == 1
- assert subject.intentions.get(MoveToIntention)
-
- # We finish rotating and start to move to final tile
- with freeze_time("2000-01-01 00:00:19", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (1, 1),
- 'rotate_to_finished': 90,
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[1], SubjectStartTileMoveEvent)
- assert isinstance(events[0], SubjectFinishRotationEvent)
- assert (1, 1) == events[1].move_to
- assert 9.0 == events[1].duration
- assert subject.position == (0, 1)
- assert subject.moving_to == (1, 1)
- assert subject.move_duration == 9.0
- assert subject.start_move == 946684819.0
- assert subject.intentions.get(MoveToIntention)
-
- # Continue the move, rest 1s
- with freeze_time("2000-01-01 00:00:27", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (1, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueTileMoveEvent)
- assert (1, 1) == events[0].move_to
- assert 1.0 == events[0].duration
- assert subject.moving_to == (1, 1)
- assert subject.move_duration == 1.0
- assert subject.start_move == 946684819.0
- assert subject.intentions.get(MoveToIntention)
-
- # We arrived on final tile
- with freeze_time("2000-01-01 00:00:28", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'move_to_finished': (1, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectFinishMoveEvent)
- assert (1, 1) == events[0].move_to
- assert (1, 1) == subject.position
- assert (-1, -1) == subject.moving_to
- assert -1 == subject.start_move
- assert -1 == subject.move_duration
- with pytest.raises(KeyError):
- assert subject.intentions.get(MoveToIntention)
-
-
- def test_move_behaviour(config):
- simulation = XYZSimulation(config)
- simulation.physics.graph.add_edge('0.0', '0.1', {})
- simulation.physics.graph.add_edge('0.1', '1.1', {})
-
- subject = ManSubject(
- config,
- simulation,
- position=(0, 0),
- )
- move = MoveToIntention(
- to=(1, 1),
- gui_action=UserAction.ORDER_MOVE,
- )
- subject.intentions.set(move)
-
- move_behaviour = MoveBehaviour(
- config=config,
- simulation=simulation,
- subject=subject,
- )
-
- # First begin move
- with freeze_time("2000-01-01 00:00:00", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'path': [
- (0, 0),
- (0, 1),
- (1, 1),
- ],
- 'tile_move_to': (0, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectStartTileMoveEvent)
- assert (0, 1) == events[0].move_to
- assert 3.0 == events[0].duration
- assert subject.intentions.get(MoveToIntention)
-
- # Continue the move, rest 1s
- with freeze_time("2000-01-01 00:00:02", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (0, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueTileMoveEvent)
- assert (0, 1) == events[0].move_to
- assert 1.0 == events[0].duration
- assert subject.intentions.get(MoveToIntention)
-
- # Tile move finished, begin a new move
- with freeze_time("2000-01-01 00:00:03", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to_finished': (0, 1),
- 'tile_move_to': (1, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[0], SubjectFinishTileMoveEvent)
- assert isinstance(events[1], SubjectStartTileMoveEvent)
- assert (0, 1) == events[0].move_to
- assert (1, 1) == events[1].move_to
- assert 3 == events[1].duration
- assert (0, 1) == subject.position
- assert (1, 1) == subject.moving_to
- assert 946684803.0 == subject.start_move
- assert 3 == subject.move_duration
- assert subject.intentions.get(MoveToIntention)
-
- # We are moving, rest 1s
- with freeze_time("2000-01-01 00:00:05", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'tile_move_to': (1, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectContinueTileMoveEvent)
- assert (1, 1) == events[0].move_to
- assert 1.0 == events[0].duration
- assert (0, 1) == subject.position
- assert (1, 1) == subject.moving_to
- assert 946684805.0 == subject.start_move
- assert 1 == subject.move_duration
- assert subject.intentions.get(MoveToIntention)
-
- # We arrived on final tile
- with freeze_time("2000-01-01 00:00:06", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'move_to_finished': (1, 1),
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectFinishMoveEvent)
- assert (1, 1) == events[0].move_to
- assert (1, 1) == subject.position
- assert (-1, -1) == subject.moving_to
- assert -1 == subject.start_move
- assert -1 == subject.move_duration
- with pytest.raises(KeyError):
- assert subject.intentions.get(MoveToIntention)
-
- def test_move_and_rotate_behaviour__rotate_negatively(config):
- simulation = XYZSimulation(config)
- simulation.physics.graph.add_edge('0.0', '1.-1', {})
- simulation.physics.graph.add_edge('1.-1', '2.-1', {})
-
- subject = TankSubject(
- config,
- simulation,
- position=(0, 0),
- )
- move = MoveToIntention(
- to=(2, -1),
- gui_action=UserAction.ORDER_MOVE,
- )
- subject.intentions.set(move)
-
- move_behaviour = MoveWithRotationBehaviour(
- config=config,
- simulation=simulation,
- subject=subject,
- )
-
- # First is a move
- with freeze_time("2000-01-01 00:00:00", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'path': [
- (0, 0),
- (1, -1),
- (2, -1),
- ],
- 'rotate_absolute': 135.0,
- 'rotate_relative': 135.0,
- } == data
-
- events = move_behaviour.action(data)
- assert 1 == len(events)
- assert isinstance(events[0], SubjectStartRotationEvent)
- assert 135 == events[0].rotate_relative
- assert 135 == events[0].rotate_absolute
- assert 15 == round(events[0].duration)
- assert subject.intentions.get(MoveToIntention)
-
- with freeze_time("2000-01-01 00:00:15", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'rotate_to_finished': 135,
- 'tile_move_to': (1, -1),
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[1], SubjectStartTileMoveEvent)
- assert isinstance(events[0], SubjectFinishRotationEvent)
- assert (1, -1) == events[1].move_to
- assert 9.0 == events[1].duration
- assert 135 == events[0].rotation_absolute
- assert subject.intentions.get(MoveToIntention)
-
- with freeze_time("2000-01-01 00:00:24", tz_offset=0):
- data = move_behaviour.run({MoveToMechanism: move.get_data()})
- assert {
- 'gui_action': UserAction.ORDER_MOVE,
- 'rotate_absolute': 90,
- 'rotate_relative': -45,
- 'tile_move_to_finished': (1, -1),
- } == data
-
- events = move_behaviour.action(data)
- assert 2 == len(events)
- assert isinstance(events[0], SubjectFinishTileMoveEvent)
- assert isinstance(events[1], SubjectStartRotationEvent)
- assert (1, -1) == events[0].move_to
- assert 90 == events[1].rotate_absolute
- assert -45 == events[1].rotate_relative
- assert 5 == round(events[1].duration)
- assert subject.intentions.get(MoveToIntention)
|