Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openrac/project/addons/openrac_library/planet_inspector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static func export_to(path: String) -> void:
var code: int = OS.execute(python, args, output, true)
var lines := PackedStringArray()
for chunk in output:
lines.append_array(String(chunk).split("\n", false))
lines.append_array(String(chunk).replace("\r\n", "\n").split("\n", false))
finish.call_deferred(code, lines))


Expand Down
12 changes: 9 additions & 3 deletions openrac/project/addons/openrac_library/play.gd
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ func run(command: Dictionary, fixing: bool = false) -> void:
var code: int = OS.execute(command["program"], command["args"], output, true)
var lines := PackedStringArray()

# Windows ends lines with \r\n: drop the \r or no marker line matches.
for chunk in output:
lines.append_array(String(chunk).split("\n", false))
lines.append_array(String(chunk).replace("\r\n", "\n").split("\n", false))

finish.call_deferred(code, lines, fixing)

Expand Down Expand Up @@ -274,8 +275,13 @@ func show_result(code: int, lines: PackedStringArray) -> void:
summary.text = "\n".join(said) if not said.is_empty() else "Couldn't build the level. Try again."

problems.visible = problems.item_count > 0
# Sized for the editor's display scale, never past most of its window.
dialog.popup_centered_clamped(Vector2(560, 320) * EditorInterface.get_editor_scale(), 0.8)
# Sized for the editor's display scale, never past most of its window. Before the first show the
# label has no width yet and puts each word on its own line, so the dialog would ask for the
# whole height: give both their size first.
var wanted := Vector2i(Vector2(560, 320) * EditorInterface.get_editor_scale())
summary.size = Vector2(summary.custom_minimum_size.x, 0)
dialog.size = wanted
dialog.popup_centered_clamped(wanted, 0.8)


## Selects the node a problem names ("<node>: <why>"), when the edited scene has it.
Expand Down
21 changes: 18 additions & 3 deletions openrac/project/addons/openrac_library/terrain_dock.gd
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,22 @@ func _ready() -> void:
update_rows()


func _enter_tree() -> void:
var fs := EditorInterface.get_resource_filesystem()
if not fs.resources_reimported.is_connected(ground_reimported):
fs.resources_reimported.connect(ground_reimported)


## The terrain's file was imported again: pick on the new ground from now on.
func ground_reimported(resources: PackedStringArray) -> void:
if resources.has(Tools.terrain_res()):
picker.clear()


func _exit_tree() -> void:
var fs := EditorInterface.get_resource_filesystem()
if fs.resources_reimported.is_connected(ground_reimported):
fs.resources_reimported.disconnect(ground_reimported)
if worker != null and worker.is_started():
worker.wait_to_finish()

Expand Down Expand Up @@ -184,15 +199,15 @@ func start(op: String, args: PackedStringArray, what: String, changes: bool = tr
worker.start(func() -> void:
var out: Array = []
var code := OS.execute(cmd["program"], cmd["args"], out, true)
var lines := PackedStringArray(String("\n".join(out)).split("\n", false))
var lines := PackedStringArray(String("\n".join(out)).replace("\r\n", "\n").split("\n", false))
done.call_deferred(code, lines, what, changes))


func done(code: int, lines: PackedStringArray, what: String, changes: bool) -> void:
for b in buttons:
b.disabled = false
if changes:
picker.clear()
# The picker is cleared once the new ground is imported (ground_reimported), not here: a mouse
# move before that would read the old ground into it again.
var result: Dictionary = Tools.finished(code, lines, changes)
if not result.get("ok", false):
status.text = what + " failed: " + String(result.get("error", ""))
Expand Down
2 changes: 1 addition & 1 deletion openrac/tools/collision_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def make(level: mc.Level, water: float | None = None) -> dict | None:
return {"text": text, "triangles": len(new), "replaced": [g for g in gone if g != NAME]}


SECTION = re.compile(r'^\[node name="([^"]+)" parent="([^"]+)"', re.M)
SECTION = re.compile(r'^\[node name="([^"]+)"(?: type="[^"]*")? parent="([^"]+)"', re.M)


def remove_nodes(text: str, paths: list[str]) -> str:
Expand Down
8 changes: 5 additions & 3 deletions openrac/tools/placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
# classes, the actors, are left as placed too: see animated().)
HOVER = {13, 14, 15, 16, 1134, 758, 803}

NODE = re.compile(r'^\[node name="([^"]+)" parent="([^"]+)"(?: instance=ExtResource\("([^"]+)"\))?')
RESOURCE = re.compile(r'^\[ext_resource type="PackedScene" path="res://([^"]+)" id="([^"]+)"\]')
# The editor's own saves add type=, uid=, unique_id=, index= and groups= to these heads.
NODE = re.compile(r'^\[node name="([^"]+)"(?: type="[^"]*")? parent="([^"]+)"'
r'(?:[^\n]*? instance=ExtResource\("([^"]+)"\))?')
RESOURCE = re.compile(r'^\[ext_resource type="PackedScene"(?: uid="[^"]*")? path="res://([^"]+)" id="([^"]+)"\]')
TRANSFORM = re.compile(r"^transform = Transform3D\(([^)]*)\)", re.M)
CLASS = re.compile(r"_(\d+)\.(?:glb|tscn)$")
KINDS = {"Game/Mobys": "moby", "Game/Ties": "tie", "Game/Shrubs": "shrub"}
Expand Down Expand Up @@ -145,7 +147,7 @@ def model_of(project: Path, res: str) -> tuple[Path, np.ndarray] | None:
m = RESOURCE.match(line)
if m:
ids[m.group(2)] = m.group(1)
model = re.search(r'^\[node name="Model" parent="\." instance=ExtResource\("([^"]+)"\)\]\n(transform = Transform3D\(([^)]*)\))?', text, re.M)
model = re.search(r'^\[node name="Model" parent="\."[^\n]*? instance=ExtResource\("([^"]+)"\)\]\n(transform = Transform3D\(([^)]*)\))?', text, re.M)
if not model or model.group(1) not in ids:
return None
m = np.eye(4)
Expand Down
48 changes: 34 additions & 14 deletions openrac/tools/test_placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,24 @@ def setUp(self):
def tearDown(self):
self.dir.cleanup()

def write(self, things):
"""THINGS: (name, kind, class, transform text)."""
lines = ['[gd_scene format=3]', '',
'[ext_resource type="PackedScene" path="res://levels/level_00/terrain.glb" id="terrain"]',
'[ext_resource type="PackedScene" path="res://levels/level_00/mobys/moby_500.tscn" id="moby_500"]',
'[ext_resource type="PackedScene" path="res://levels/level_00/mobys/moby_13.tscn" id="moby_13"]',
'[ext_resource type="PackedScene" path="res://levels/level_00/mobys/moby_1246.tscn" id="moby_1246"]',
'[ext_resource type="PackedScene" path="res://levels/level_00/shrubs/shrub_1.glb" id="shrub_1"]',
'[ext_resource type="PackedScene" path="res://levels/level_00/shrubs/shrub_2.glb" id="shrub_2"]', '',
'[node name="Level" type="Node3D"]', '', '[node name="Game" type="Node3D" parent="."]', '',
'[node name="Terrain" parent="Game" instance=ExtResource("terrain")]', '']
for name, kind, cls, xform in things:
def write(self, things, saved: bool = False):
"""THINGS: (name, kind, class, transform text). SAVED writes the heads as the editor saves
them: uid=, unique_id= and groups= added."""
files = [("terrain", "levels/level_00/terrain.glb")]
files += [(f"moby_{c}", f"levels/level_00/mobys/moby_{c}.tscn") for c in (500, 13, 1246)]
files += [(f"shrub_{c}", f"levels/level_00/shrubs/shrub_{c}.glb") for c in (1, 2)]
uid = (lambda n: f' uid="uid://b{n}x"') if saved else (lambda n: "")
extra = (lambda n: f" unique_id={1000 + n}") if saved else (lambda n: "")
lines = ['[gd_scene format=3]', '']
lines += [f'[ext_resource type="PackedScene"{uid(i)} path="res://{path}" id="{rid}"]' for i, (rid, path) in enumerate(files)]
lines += ['', f'[node name="Level" type="Node3D"{extra(0)}]', '',
f'[node name="Game" type="Node3D" parent="."{extra(1)}]', '',
f'[node name="Terrain" parent="Game"{extra(2)} instance=ExtResource("terrain")]', '']
for i, (name, kind, cls, xform) in enumerate(things):
parent = {"moby": "Game/Mobys", "shrub": "Game/Shrubs"}[kind]
lines += [f'[node name="{name}" parent="{parent}" instance=ExtResource("{kind}_{cls}")]', f"transform = {xform}",
"metadata/rc1_index = 0", ""]
groups = ' groups=["openrac"]' if saved else ""
lines += [f'[node name="{name}" parent="{parent}"{extra(10 + i)}{groups} instance=ExtResource("{kind}_{cls}")]',
f"transform = {xform}", "metadata/rc1_index = 0", ""]
self.scene.write_text("\n".join(lines), encoding="utf-8")

def placed(self, **kw):
Expand All @@ -121,6 +124,23 @@ def test_floating_crate_is_lowered(self):
self.assertIn("lowered", report["Crate"]["done"])
self.assertAlmostEqual(self.origin("Crate")[2], 0.0, delta=0.02)

def test_scene_saved_by_the_editor(self):
# The editor's save adds uid=, unique_id= and groups=: the things must still be seen.
level = self.project / "levels" / "level_00"
moby = level / "mobys" / "moby_500.tscn"
moby.write_text(moby.read_text().replace('type="PackedScene" path=', 'type="PackedScene" uid="uid://cmodel" path=')
.replace('[node name="Model" parent="."', '[node name="Model" parent="." unique_id=7'))
self.write([("Crate", "moby", 500, transform(0, 0, 0.5)), ("Up", "shrub", 1, transform(3, 0, 0.6))], saved=True)
self.assertEqual(placement.main(self.project, check=True), 1)
report = self.placed()
self.assertIn("lowered", report["Crate"]["done"])
self.assertAlmostEqual(self.origin("Crate")[2], 0.0, delta=0.02)
self.assertAlmostEqual(self.origin("Up")[2], 0.0, delta=0.02)
self.assertIn('[node name="Crate" parent="Game/Mobys" unique_id=1010 groups=["openrac"]', self.scene.read_text())
import map_check
nodes = {n[0]: n[2] for n in map_check.scene_nodes(self.scene.read_text())}
self.assertEqual(nodes["Crate"], "levels/level_00/mobys/moby_500.tscn")

def test_sunk_crate_is_raised(self):
self.write([("Crate", "moby", 500, transform(0, 0, -0.8))])
self.placed()
Expand Down
Loading