Closed rpdelaney closed 2 years ago
This approach is somewhat malformed: if you add each new entity one at a time, how can you guarantee that associated entities (the wire connected ones) will even exist in the same blueprint? Here you copy everything, but Draftsman has no knowledge of this. As a result, the entities in new_bp
will point to the entities in old_bp
, as they have no knowledge of what entities in new_bp
are their equivalent.
A way to circumvent this is to set new_bp.entities = old_bp.entities
, (something I needed to implement for entity merging) which preserves these associations. You can then replace only the entities you need in new_bp
:
old_bp = get_blueprintable_from_string("...")
new_bp = Blueprint()
# Copy all entities (preserving their associations!)
new_bp.entities = old_bp.entities
# Replace only the centrifuges
for i, entity in enumerate(new_bp.entities):
if (
entity.name == "centrifuge"
and entity.recipe == "kovarex-enrichment-process"
):
new_centrifuge = AssemblingMachine("centrifuge", position=entity.position)
new_centrifuge.recipe = "kovarex-enrichment-process"
new_centrifuge.items.update(entity.items) # ensure modules are copied
new_centrifuge.items.update({"uranium-235": 40, "uranium-238": 5})
# Place the new entity into the replaced one's position
# (Making it the same position isn't actually necessary, but makes sense in this context)
new_bp.entities[i] = new_centrifuge
print(f"{entity.position}: replaced Centrifuge", file=sys.stderr)
print(new_bp.to_string(), end="")
From what I can tell from what you're trying to do though, you only need one copy of the blueprint; only protected attributes cannot be changed in entities when they currently exist in a Blueprint. Item requests are not protected, and can be changed at will:
old_bp = get_blueprintable_from_string("...")
for entity in old_bp.entities:
if entity.name == "centrifuge":
# Update item requests (so we preserve modules)
entity.items.update({"uranium-235": 40, "uranium-238": 5})
print(f"{entity.position}: replaced Centrifuge", file=sys.stderr)
print(old_bp.to_string(), end="")
Both snippets produce the same result.
The usage of KovarexAssembler
is possible as well, though also not necessary. The usefulness of overwriting Entity classes is still in it's infancy, and was designed more along the lines of modded entities with custom interfaces. Think LTN train stops, or Space Exploration rocket launchpads, where having custom functions like launchpad.set_destination(x)
or launchpad.destination = x
is much nicer than launchpad.tags = {"destination": x}
, which is what internal format dictates.
This script:
On this bp:
Gives this exception at the very end: