| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- """Group placement regression checks using an entirely temporary map."""
- import json
- import tempfile
- from pathlib import Path
- from verify_recovery import editor, pygame
- from game.frame import obstacle
- from game.frame.frame import Frame
- from game.frame.obstacle_instance import ObstacleInstance
- def verify():
- with tempfile.TemporaryDirectory(prefix="editor_selection_") as tmp:
- target = Path(tmp) / "map.json"
- data = dict(basic=dict(name="test", width=40, height=40, scroll=[0, 0], begin=[0, 0], finish=[39, 39]),
- floor=[], obstacle=[], districts=[])
- target.write_text(json.dumps(data))
- editor.MAP_JSON_FILE_NAME = str(target)
- update = editor.MapBuilder.update
- editor.MapBuilder.update = lambda self: None
- try:
- app = editor.MapBuilder()
- finally:
- editor.MapBuilder.update = update
- for name, width, height in [("large", 2, 2), ("small", 1, 1)]:
- obstacle.obstacles.setdefault("selection_test", {})[name] = dict(
- TYPE="selection_test", NAME=name, WIDTH=width, HEIGHT=height,
- STAND=[Frame(pygame.Surface((width * 40, height * 40)), 0, 0)])
- large = obstacle.get_obstacle("selection_test", "large")
- small = obstacle.get_obstacle("selection_test", "small")
- ObstacleInstance(2, 2, app.map_obstacles_dict, large)
- ObstacleInstance(6, 2, app.map_obstacles_dict, large)
- selection = app.selection
- selection.copy_rectangle((7, 3), (3, 2), "obstacle")
- assert len(selection.group) == 2 # Partial selection includes the full object, once.
- before = app.document_snapshot()
- ObstacleInstance(15, 11, app.map_obstacles_dict, small)
- blocked = app.document_snapshot()
- assert not selection.place((10, 10)) # Collision in the second object's far corner.
- assert app.document_snapshot() == blocked and (10, 10) not in app.map_obstacles_dict
- app.remove_elem((15, 11), "obstacle")
- assert not selection.place((39, 39))
- assert app.document_snapshot() == before
- app.menu.edit(lambda: selection.place((10, 10)))
- placed = app.document_snapshot()
- assert (11, 11) in app.map_obstacles_dict and (15, 11) in app.map_obstacles_dict
- app.menu.undo()
- assert app.document_snapshot() == before
- app.menu.redo()
- assert app.document_snapshot() == placed
- assert not selection.place((10, 10)) # Existing copies may not be replaced.
- app.current_elem = dict(elem_type="obstacle", type="selection_test", name="large")
- app.add_elem((9, 9))
- assert app.map_obstacles_dict[(9, 9)] is app.map_obstacles_dict[(10, 10)]
- assert (11, 11) not in app.map_obstacles_dict # Overlapping object removed entirely.
- app.restore_document(placed)
- app.map_json["floor"] = [dict(type="test", name="tile", points=[dict(x=0, y=0), dict(x=1, y=0)])]
- selection.copy_rectangle((0, 0), (1, 0), "floor")
- floor_before = app.document_snapshot()
- assert selection.place((1, 2)) # Floor placement ignores obstacles without modifying them.
- assert app.document_snapshot()["obstacle"] == floor_before["obstacle"]
- assert selection.plan((1, 2))[0] and not selection.conflicts
- assert {"x": 2, "y": 2} in app.map_json["floor"][0]["points"]
- app.current_elem = dict(elem_type="floor", type="test", name="tile")
- app.add_elem((3, 3))
- assert {"x": 3, "y": 3} in app.map_json["floor"][0]["points"]
- assert app.document_snapshot()["obstacle"] == floor_before["obstacle"]
- before_bounds = app.document_snapshot()
- assert not selection.place((39, 39))
- assert app.document_snapshot() == before_bounds
- assert selection.place((20, 20))
- selection.cancel()
- assert not selection.group
- # Exercise Shift-drag events, reverse direction, and release outside the viewport.
- pygame.key.set_mods(pygame.KMOD_SHIFT)
- def pos(x, y):
- return (editor.MAP_AT[0] + x * 40 + 5, editor.MAP_AT[1] + y * 40 + 5)
- app.show_obstacle = True
- selection.handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=pos(7, 3)))
- selection.handle_event(pygame.event.Event(pygame.MOUSEMOTION, pos=pos(2, 2)))
- selection.handle_event(pygame.event.Event(pygame.MOUSEBUTTONUP, button=1, pos=pos(2, 2)))
- assert selection.start is None and len(selection.group) == 2
- pygame.key.set_mods(0)
- app.menu.begin_frame()
- app.menu.undo_stack.clear()
- app.menu.redo_stack.clear()
- event = pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=pos(20, 10))
- app.menu.handle_event(event)
- assert selection.handle_event(event)
- app.menu.handle_event(pygame.event.Event(pygame.MOUSEBUTTONUP, button=1, pos=event.pos))
- assert len(app.menu.undo_stack) == 1 and (24, 10) in app.map_obstacles_dict
- app.menu.undo()
- assert (24, 10) not in app.map_obstacles_dict
- app.save_map_json()
- assert target.exists()
- pygame.quit()
- print("PASS: Shift-drag copy, full-object deduplication, full-footprint collision, atomic rejection,")
- print("bounds, floors ignore obstacles, direct group placement, undo/redo, large-obstacle replacement")
- if __name__ == "__main__":
- verify()
|