Mirror Mount reflects the footprint's cells about the centre line, derived once from the footprint
hull_grid.mirror_position gives the twin Position as grid_w - a - w, so an even-width footprint mirrors to its true reflection and the cell lookup's tie rule no longer decides a mirror; the twin's object stands in the cell that Position puts it in, so Package derives the same Position back. test_hull_grid pins the reflection on odd and even grids and rows of 4 or more cells; smoke_mounts runs footprint widths 1 to 4 at every cell of an even and an odd grid, the boundary tie, the armour and arc copy, the muzzle's local X, the centre-line threshold, the grid edge and the parent check, and keeps a mirrored pair of two-cell footprints through Package and the validator. Every commit builds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
56129b43d5
commit
96b255541c
4 changed files with 198 additions and 6 deletions
|
|
@ -359,8 +359,14 @@ class AETHERIA_OT_mirror_mount(bpy.types.Operator):
|
|||
local = _root_local(root, mount)
|
||||
if abs(local.x) < CENTRELINE:
|
||||
raise ValueError("The weapon mount is on the centreline; it has no mirror image")
|
||||
# The twin's footprint is the reflection of this one's cells; its object stands in the cell that Position
|
||||
# puts it in (never at -x, whose cell lookup breaks ties one way), so Package derives the Position back.
|
||||
width, height = _parse_footprint(row.footprint)[0][:2]
|
||||
position = hull_grid.mirror_position(_derived_position(state, origin, local, row.footprint), width,
|
||||
state.width)
|
||||
twin_x = hull_grid.cell_centre(origin, *hull_grid.mounted_cell(position, width, height))[0]
|
||||
mirrored = _add_mount(collection, state, root, origin, row.kind,
|
||||
hull_grid.mirror_rotation(int(row.rotation)), (-local.x, local.y, local.z),
|
||||
hull_grid.mirror_rotation(int(row.rotation)), (twin_x, local.y, local.z),
|
||||
hull_grid.mirror_footprint(row.footprint))
|
||||
new_row = _mount_row(state, mirrored["aetheria.id"])
|
||||
for name in ("armor", "firing_arc"): # the author's own numbers, copied; the helper invents none
|
||||
|
|
|
|||
|
|
@ -115,10 +115,27 @@ _FACING_YAW = {
|
|||
}
|
||||
|
||||
|
||||
def _centre_offset(n):
|
||||
"""The footprint cell, counted from its origin, that stands for an n-cell extent's centre: the lower of the two
|
||||
middle cells when n is even."""
|
||||
return (n - 1) // 2
|
||||
|
||||
|
||||
def mounted_position(cell, footprint_w, footprint_h):
|
||||
"""The hardpoint Position (its footprint's origin cell) whose w x h footprint is centred on cell: ShipAuthoringStore
|
||||
places footprint cell (cx, cy) at Position + (cx, cy), and the footprint's centre is ((w - 1) // 2, (h - 1) // 2)."""
|
||||
return (cell[0] - (footprint_w - 1) // 2, cell[1] - (footprint_h - 1) // 2)
|
||||
return (cell[0] - _centre_offset(footprint_w), cell[1] - _centre_offset(footprint_h))
|
||||
|
||||
|
||||
def mounted_cell(position, footprint_w, footprint_h):
|
||||
"""The cell a mount's object stands in for a hardpoint at position: the inverse of mounted_position."""
|
||||
return (position[0] + _centre_offset(footprint_w), position[1] + _centre_offset(footprint_h))
|
||||
|
||||
|
||||
def mirror_position(position, footprint_w, grid_w):
|
||||
"""The Position of the mirror image of a footprint across the grid's centre line: a footprint on cells [a, a + w) of
|
||||
a grid_w-wide grid mirrors to cells [grid_w - a - w, grid_w - a). It depends on the footprint's extent alone."""
|
||||
return (grid_w - position[0] - footprint_w, position[1])
|
||||
|
||||
|
||||
def facing_yaw(rotation):
|
||||
|
|
|
|||
|
|
@ -29,10 +29,13 @@ from pathlib import Path
|
|||
import addon_utils
|
||||
import bmesh
|
||||
import bpy
|
||||
from mathutils import Vector
|
||||
|
||||
REPO = Path(__file__).resolve().parents[3]
|
||||
PACKAGES = os.environ.get("CULTLIB_PACKAGES") or sys.exit("Set CULTLIB_PACKAGES to CultLib's packages directory")
|
||||
SHIP_ID = "smoke.mounts"
|
||||
ODD_ID = "smoke.mounts.odd"
|
||||
ODD_LENGTH = 7
|
||||
GAME = Path(os.environ.get("SMOKE_GAME_FOLDER") or sys.exit("Set SMOKE_GAME_FOLDER to a folder holding GameData/Aetheria.cc and ModTools/AetherDb"))
|
||||
MODS = GAME / "GameData" / "Mods"
|
||||
COLLECTION = "Smoke Mounts"
|
||||
|
|
@ -146,6 +149,101 @@ def glb_world_rotation_of(path, name):
|
|||
return (2 * (x * z + w * y), 2 * (y * z - w * x), 1 - 2 * (x * x + y * y))
|
||||
|
||||
|
||||
def new_gun(scene, root, location, rotation=CLOCKWISE):
|
||||
"""Add Weapon with the 3D cursor at location in Ship Root's frame; returns the new row's mount id."""
|
||||
before = {r[0] for r in rows(scene)}
|
||||
select(root) # the active object names the ship collection
|
||||
scene.cursor.location = root.matrix_world @ Vector(location)
|
||||
result, message = run(lambda: bpy.ops.aetheria.add_ship_weapon(kind=BALLISTIC, rotation=str(rotation)))
|
||||
require(result == {"FINISHED"}, f"Add Weapon was refused: {message}")
|
||||
(mount_id,) = {r[0] for r in rows(scene)} - before
|
||||
return mount_id
|
||||
|
||||
|
||||
def remove_mount(mount_id):
|
||||
select(bpy.data.objects[mount_id])
|
||||
require(run(bpy.ops.aetheria.remove_ship_mount)[0] == {"FINISHED"}, "Remove Mount was refused")
|
||||
|
||||
|
||||
def mirror_matrix(scene, root, origin, sphere):
|
||||
"""Mirror Mount against the reflection of the footprint's cells, for every footprint width 1 to 4 at every cell of
|
||||
the grid: a footprint on cells [a, a + w) of a W-wide grid has its twin on [W - a - w, W - a), whatever the grid's
|
||||
parity, and a mount within a cell of the centre line has no twin. The expected cells are computed here from the
|
||||
footprint, never read back from the twin."""
|
||||
state = scene.aetheria_layout
|
||||
grid_w, grid_h = state.width, state.height
|
||||
y_cell = 1
|
||||
y = origin[1] - 2.0 * y_cell
|
||||
mirrored = refused_count = 0
|
||||
baseline = rows(scene)
|
||||
for w in (1, 2, 3, 4):
|
||||
for c in range(grid_w): # the cell the mount's object stands in; a footprint is centred on it
|
||||
a = c - (w - 1) // 2
|
||||
if a < 0 or a + w > grid_w:
|
||||
continue
|
||||
x = origin[0] - 2.0 * c
|
||||
mount_id = new_gun(scene, root, (x, y, 0.0))
|
||||
set_footprint(scene, mount_id, "#" * w)
|
||||
select(bpy.data.objects[mount_id])
|
||||
before = rows(scene)
|
||||
if abs(x) < 1.0:
|
||||
refused(bpy.ops.aetheria.mirror_ship_mount, "no mirror image", "Mirror Mount near the centre line")
|
||||
require(rows(scene) == before, "A refused Mirror Mount changed a row")
|
||||
refused_count += 1
|
||||
else:
|
||||
require(run(bpy.ops.aetheria.mirror_ship_mount)[0] == {"FINISHED"}, f"Mirror Mount was refused (width {w}, cell {c})")
|
||||
(twin_id,) = {r[0] for r in rows(scene)} - {r[0] for r in before}
|
||||
twin = row(scene, twin_id)
|
||||
expected = (grid_w - a - w, y_cell)
|
||||
require(twin[2] == expected, f"A {w}-wide footprint at cell {a} of {grid_w} mirrored to {twin[2]}, not {expected}")
|
||||
require(twin[4] == "#" * w, "The mirrored footprint is not the reflection of the footprint")
|
||||
mirrored += 1
|
||||
remove_mount(twin_id)
|
||||
remove_mount(mount_id)
|
||||
require(mirrored >= 6 and (grid_w % 2 == 0 or refused_count >= 1), f"The matrix ran too little ({mirrored}, {refused_count})")
|
||||
require(rows(scene) == baseline, "The matrix left rows behind")
|
||||
|
||||
# A mount exactly on a cell boundary: its twin is the reflection of the cell it was placed in, not a second rounding.
|
||||
k = grid_w // 2
|
||||
mount_id = new_gun(scene, root, (origin[0] - 2.0 * k - 1.0, y, 0.0))
|
||||
source = row(scene, mount_id)
|
||||
hp = next(h for h in state.hardpoints if h.mount_id == mount_id)
|
||||
hp.armor, hp.firing_arc = 3.0, 45.0 # the author's numbers
|
||||
muzzle = bpy.data.objects[f"{mount_id}.muzzle.1"]
|
||||
muzzle.location.x = 0.3
|
||||
select(bpy.data.objects[mount_id])
|
||||
before_ids = {r[0] for r in rows(scene)}
|
||||
require(run(bpy.ops.aetheria.mirror_ship_mount)[0] == {"FINISHED"}, "Mirror Mount of a boundary mount was refused")
|
||||
(twin_id,) = {r[0] for r in rows(scene)} - before_ids
|
||||
twin = row(scene, twin_id)
|
||||
require(twin[2] == (grid_w - 1 - source[2][0], y_cell), f"The boundary mount's twin is at {twin[2]}")
|
||||
require(twin[3] == COUNTER_CLOCKWISE and twin[5:] == (3.0, 45.0), f"The twin's rotation or armour and arc are wrong: {twin}")
|
||||
gun_at, twin_at = world(bpy.data.objects[mount_id]), world(bpy.data.objects[twin_id])
|
||||
source_offset = tuple(m - g for m, g in zip(world(muzzle), gun_at))
|
||||
twin_offset = tuple(m - g for m, g in zip(world(bpy.data.objects[f"{twin_id}.muzzle.1"]), twin_at))
|
||||
require(approx(twin_offset, (-source_offset[0], source_offset[1], source_offset[2])),
|
||||
f"The mirrored muzzle is not the reflection of the source muzzle: {source_offset} vs {twin_offset}")
|
||||
remove_mount(twin_id)
|
||||
remove_mount(mount_id)
|
||||
|
||||
# The centre line is a cell wide: half a metre off it has no twin.
|
||||
mount_id = new_gun(scene, root, (0.5, y, 0.0))
|
||||
select(bpy.data.objects[mount_id])
|
||||
before = rows(scene)
|
||||
refused(bpy.ops.aetheria.mirror_ship_mount, "no mirror image", "Mirror Mount of a mount half a metre off the centre line")
|
||||
require(rows(scene) == before, "A refused Mirror Mount changed a row")
|
||||
remove_mount(mount_id)
|
||||
|
||||
# A mount whose cell is one past the grid's last is refused, on either axis, and changes nothing.
|
||||
for edge in ((origin[0] - 2.0 * grid_w, y), (origin[0], origin[1] - 2.0 * grid_h)):
|
||||
before, objects = rows(scene), len(bpy.data.objects)
|
||||
scene.cursor.location = root.matrix_world @ Vector((edge[0], edge[1], 0.0))
|
||||
select(sphere)
|
||||
refused(lambda: bpy.ops.aetheria.add_ship_weapon(kind=BALLISTIC, rotation=str(CLOCKWISE)),
|
||||
"outside the grid", "Add Weapon past the grid's edge")
|
||||
require(rows(scene) == before and len(bpy.data.objects) == objects, "A refused Add Weapon changed something")
|
||||
|
||||
|
||||
def main():
|
||||
require(bpy.app.version[:2] == (5, 2), f"This smoke is for Blender 5.2; this is {bpy.app.version_string}")
|
||||
bpy.data.objects.remove(bpy.data.objects["Cube"], do_unlink=True)
|
||||
|
|
@ -210,15 +308,12 @@ def main():
|
|||
require(approx(tuple(m - g for m, g in zip(muzzle_at, gun_at)), (-0.5, 0.0, 0.0)),
|
||||
"The Clockwise muzzle is not 0.5 m toward -X of its mount")
|
||||
|
||||
set_footprint(scene, "weapon.1", "#.") # an asymmetric footprint shows the mirror turning it
|
||||
select(gun)
|
||||
result, message = run(bpy.ops.aetheria.mirror_ship_mount)
|
||||
require(result == {"FINISHED"}, f"Mirror Mount was refused: {message}")
|
||||
twin = bpy.data.objects["weapon.2"]
|
||||
twin_row = row(scene, "weapon.2")
|
||||
require(twin_row == ("weapon.2", 10, (1, 5), COUNTER_CLOCKWISE, ".#", 0.0, 0.0), f"The mirrored row is wrong: {twin_row}")
|
||||
set_footprint(scene, "weapon.1", "#")
|
||||
set_footprint(scene, "weapon.2", "#")
|
||||
require(twin_row == ("weapon.2", 10, (1, 5), COUNTER_CLOCKWISE, "#", 0.0, 0.0), f"The mirrored row is wrong: {twin_row}")
|
||||
twin_muzzle = bpy.data.objects["weapon.2.muzzle.1"]
|
||||
twin_at = world(twin)
|
||||
require(approx(twin_at, (3.0, -1.0, 0.0)), f"The mirrored mount is not across the centreline: {twin_at}")
|
||||
|
|
@ -249,6 +344,17 @@ def main():
|
|||
require(rows(scene) == others, "Remove Mount changed a row besides its own")
|
||||
print("PASS 4 remove mount ok")
|
||||
|
||||
mirror_matrix(scene, root, (origin[0], origin[1]), sphere)
|
||||
print("PASS 5 mirror matrix ok")
|
||||
|
||||
# A mirrored pair of two-cell footprints stays where Mirror Mount put it through Package and the validator.
|
||||
pair = new_gun(scene, root, (-1.0, 1.0, 0.0))
|
||||
set_footprint(scene, pair, "##")
|
||||
select(bpy.data.objects[pair])
|
||||
require(run(bpy.ops.aetheria.mirror_ship_mount)[0] == {"FINISHED"}, "Mirror Mount of the pair was refused")
|
||||
pair_rows = [r for r in rows(scene) if r[0] not in ("weapon.1", "weapon.2") and r[0].startswith("weapon.")]
|
||||
require(sorted(r[2] for r in pair_rows) == [(1, 4), (3, 4)], f"The pair is not a reflection: {pair_rows}")
|
||||
|
||||
# Pass 2: Package; the GLB's muzzle nodes face outboard.
|
||||
select(sphere)
|
||||
result, message = run(bpy.ops.aetheria.save_ship_layout)
|
||||
|
|
@ -261,6 +367,7 @@ def main():
|
|||
port = glb_world_rotation_of(glb, "weapon.2.muzzle.1")
|
||||
require(approx(starboard, (-1.0, 0.0, 0.0)), f"The Clockwise muzzle node does not turn glTF +Z to -X: {starboard}")
|
||||
require(approx(port, (1.0, 0.0, 0.0)), f"The mirrored muzzle node does not turn glTF +Z to +X: {port}")
|
||||
require([r for r in rows(scene) if r in pair_rows] == pair_rows, "Package moved a mirrored pair")
|
||||
print("PASS 2 package and glb ok")
|
||||
|
||||
# Pass 3: a moved mount and a changed Rotation; Package derives Position and yaw from them.
|
||||
|
|
@ -296,8 +403,40 @@ def main():
|
|||
f"Package took an unloaded layout: {scene.aetheria_package_report}")
|
||||
require(glb.read_bytes() == glb_before, "A refused Package wrote the GLB")
|
||||
state.ship_id = ship_id
|
||||
require([r for r in rows(scene) if r in pair_rows] == pair_rows, "The second Package moved a mirrored pair")
|
||||
twin.parent = None # a mount that is not under Ship Root cannot be derived from
|
||||
row_before = rows(scene)
|
||||
result, message = run(bpy.ops.aetheria.package_ship)
|
||||
require(result == {"CANCELLED"} and "must be parented" in scene.aetheria_package_report and rows(scene) == row_before,
|
||||
f"Package took a mount outside Ship Root: {scene.aetheria_package_report}")
|
||||
twin.parent = root
|
||||
print("PASS 3 package derives ok")
|
||||
|
||||
# Pass 6: the same mirror matrix on a ship whose grid is an odd number of cells wide.
|
||||
for obj in list(collection.all_objects): # the first ship's objects hold the names the matrix looks mounts up by
|
||||
bpy.data.objects.remove(obj)
|
||||
odd = bpy.data.collections.new(COLLECTION + " Odd")
|
||||
scene.collection.children.link(odd)
|
||||
odd_mesh = bpy.data.meshes.new("Odd Sphere")
|
||||
bm = bmesh.new()
|
||||
bmesh.ops.create_uvsphere(bm, u_segments=48, v_segments=24, radius=0.5)
|
||||
bm.to_mesh(odd_mesh)
|
||||
bm.free()
|
||||
odd_sphere = new_object(odd, "Odd Source Sphere", odd_mesh)
|
||||
odd_sphere.scale = (1, 0.6, 0.4)
|
||||
select(odd_sphere)
|
||||
scene.aetheria_new_ship_id, scene.aetheria_new_ship_name = ODD_ID, "Smoke Mounts Odd"
|
||||
scene.aetheria_new_ship_like, scene.aetheria_new_ship_length = "Djinni", ODD_LENGTH
|
||||
result, message = run(bpy.ops.aetheria.new_ship)
|
||||
require(result == {"FINISHED"}, f"New Ship (odd) was refused: {message}")
|
||||
result, message = run(bpy.ops.aetheria.save_ship_layout)
|
||||
require(result == {"FINISHED"}, f"Save (odd) was refused: {message}")
|
||||
odd_root = aetheria_ships._ship_root(odd)
|
||||
odd_origin = odd["aetheria.grid_origin"]
|
||||
require(scene.aetheria_layout.width % 2 == 1, f"The odd ship's grid is {scene.aetheria_layout.width} wide")
|
||||
mirror_matrix(scene, odd_root, (odd_origin[0], odd_origin[1]), odd_sphere)
|
||||
print("PASS 6 mirror matrix on an odd grid ok")
|
||||
|
||||
|
||||
created_mods = not MODS.exists()
|
||||
existed = (MODS / SHIP_ID).exists()
|
||||
|
|
@ -316,6 +455,8 @@ else:
|
|||
finally:
|
||||
if not existed:
|
||||
shutil.rmtree(MODS / SHIP_ID, ignore_errors=True)
|
||||
if not existed:
|
||||
shutil.rmtree(MODS / ODD_ID, ignore_errors=True)
|
||||
if created_mods and not existed:
|
||||
shutil.rmtree(MODS, ignore_errors=True)
|
||||
sys.stdout.flush()
|
||||
|
|
|
|||
|
|
@ -198,12 +198,40 @@ class MountArithmeticTests(unittest.TestCase):
|
|||
def test_mirror_footprint_reverses_each_row(self):
|
||||
self.assertEqual(hull_grid.mirror_footprint("##./.#."), ".##/.#.")
|
||||
self.assertEqual(hull_grid.mirror_footprint("#"), "#")
|
||||
for footprint, expected in {"#.": ".#", "#..#.": ".#..#", "##..": "..##", "#..../.##..": "....#/..##.",
|
||||
"#.....": ".....#", "##.#.../......": "...#.##/......"}.items():
|
||||
self.assertEqual(hull_grid.mirror_footprint(footprint), expected, footprint)
|
||||
|
||||
def test_mounted_position_offsets_the_cell_by_the_footprint_centre(self):
|
||||
for (w, h), expected in {(1, 1): (5, 4), (3, 1): (4, 4), (3, 3): (4, 3), (2, 1): (5, 4), (2, 2): (5, 4),
|
||||
(4, 2): (4, 4)}.items():
|
||||
self.assertEqual(hull_grid.mounted_position((5, 4), w, h), expected, (w, h))
|
||||
|
||||
def test_mirror_position_reflects_the_footprints_cells_on_odd_and_even_grids(self):
|
||||
for grid_w in range(1, 10):
|
||||
for w in range(1, grid_w + 1):
|
||||
for a in range(0, grid_w - w + 1):
|
||||
twin = hull_grid.mirror_position((a, 7), w, grid_w)
|
||||
reflected = sorted(grid_w - 1 - (a + i) for i in range(w)) # each cell across the centre line
|
||||
self.assertEqual([twin[0] + i for i in range(w)], reflected, (grid_w, w, a))
|
||||
self.assertEqual(twin[1], 7)
|
||||
|
||||
def test_a_mirrored_footprint_keeps_its_picture_in_the_reflected_cells(self):
|
||||
grid_w, a, footprint = 7, 1, "#.##" # cells 1, 3, 4 filled
|
||||
twin = hull_grid.mirror_position((a, 0), 4, grid_w)
|
||||
filled = {twin[0] + i for i, c in enumerate(hull_grid.mirror_footprint(footprint)) if c == "#"}
|
||||
self.assertEqual(filled, {grid_w - 1 - c for c in (1, 3, 4)})
|
||||
|
||||
def test_an_object_in_the_mounted_cell_derives_the_position_back_for_every_width(self):
|
||||
origin = (5.0, 9.0)
|
||||
for w in range(1, 7):
|
||||
for h in range(1, 4):
|
||||
for position in ((0, 0), (3, 5), (6, 2)):
|
||||
cell = hull_grid.mounted_cell(position, w, h)
|
||||
centre = hull_grid.cell_centre(origin, *cell)
|
||||
self.assertEqual(hull_grid.mounted_position(hull_grid.cell_at(origin, centre), w, h), position,
|
||||
(w, h, position))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Reference in a new issue