redruin1 / factorio-draftsman

A complete, well-tested, and up-to-date module to manipulate Factorio blueprint strings. Compatible with mods.
MIT License
94 stars 17 forks source link

Generate power connection function does not work on blueprints with groups with electric poles #19

Closed elswindle closed 2 years ago

elswindle commented 2 years ago

Probably somewhat related to #16, but the generate_power_connections errors out with a ValueError with no exception description when a Group containing any power pole exists within the Blueprint.

redruin1 commented 2 years ago

This was a legacy issue related to the function being designed before Groups were implemented. This is now fixed in 0.9.6. In addition (in order to fix this bug most easily) I also added the capability to connect entities by their references themselves:

blueprint = Blueprint()
blueprint.entities.append("wooden-chest")
blueprint.entities.append("wooden-chest", tile_position=(1,0))
blueprint.add_circuit_connection(blueprint.entities[0], blueprint.entities[1])
# which is equivalent to:
blueprint.add_circuit_connection(0, 1)

This is unlikely to be used by anyone except internally by myself, but it's provided to the user for flexibility. Attempting to connect two entities where either do not exist in the blueprint/group now raises an InvalidAssociationError:

blueprint = Blueprint()
entityA = ElectricPole()
entityB = ElectricPole()
blueprint.add_power_connection(entityA, entityB) # InvalidAssociationError: entity_1 (<ElectricPole>{'name': 'small-electric-pole', 'position': {'x': 0.5, 'y': 0.5}}) not contained within this collection

And as a consequence, __contains__ now also works recursively with EntityLists:

entityA = ElectricPole()
entityB = ElectricPole(tile_position=(1,0))
group = Group()
group.entities.append(entityA, copy=False)
group.entities.append(entityB, copy=False)
blueprint.entities.append(group, copy=False)
assert entityA in group.entities # True
assert entityA in blueprint.entities # True
assert entityB in group.entities # True
assert entityB in blueprint.entities # True

All of this updated information will be included in the function signatures in the next version.