mirror of
https://github.com/JackHopkins/factorio-learning-environment.git
synced 2025-09-06 21:48:51 +00:00
Add function to deduplicate entities and improve entity creation in ConnectEntities
This commit adds a new method `_deduplicate_entities` to the `ConnectEntities` class in the `connect_entities.py` file. This method removes duplicate entities from a list while maintaining the original order. It checks the position of each entity and only keeps the last occurrence of entities with the same position. The commit also improves the entity creation process in the same class by directly appending the created entity to the path list instead of appending a dictionary and then creating the entity separately. Finally, it updates the existing entity creation process to use the new deduplication function for the `path` list, ensuring that only unique entities are stored.
This commit is contained in:
@@ -71,6 +71,20 @@ class ConnectEntities(Action):
|
||||
def _round_position(self, position: Position):
|
||||
return Position(x=math.floor(position.x), y=math.floor(position.y))
|
||||
|
||||
def _deduplicate_entities(self, entities: List[Entity]) -> List[Entity]:
|
||||
"""
|
||||
Remove duplicate entities while maintaining the original order.
|
||||
Later entities with the same position override earlier ones.
|
||||
"""
|
||||
unique_entities = []
|
||||
seen = set()
|
||||
for entity in reversed(entities):
|
||||
position = (entity.position.x, entity.position.y)
|
||||
if position not in seen:
|
||||
unique_entities.append(entity)
|
||||
seen.add(position)
|
||||
return list(reversed(unique_entities))
|
||||
|
||||
def __call__(self,
|
||||
source: Union[Position, Entity],
|
||||
target: Union[Position, Entity],
|
||||
@@ -230,10 +244,12 @@ class ConnectEntities(Action):
|
||||
for value in entities_list:
|
||||
if isinstance(value, dict):
|
||||
try:
|
||||
path.append(metaclass(prototype=connection_type, **value))
|
||||
entity = metaclass(prototype=connection_type, **value)
|
||||
path.append(entity)
|
||||
except Exception as e:
|
||||
if not value:
|
||||
continue
|
||||
raise Exception(f"Could not create {connection_prototype} object from response: {response}", e)
|
||||
|
||||
return path
|
||||
# Use the new deduplication function
|
||||
return self._deduplicate_entities(path)
|
@@ -12,10 +12,10 @@ from factorio_types import Prototype, Resource
|
||||
def game(instance):
|
||||
instance.initial_inventory = {
|
||||
**instance.initial_inventory,
|
||||
'stone-furnace': 1,
|
||||
'burner-inserter': 5,
|
||||
'stone-furnace': 10,
|
||||
'burner-inserter': 50,
|
||||
}
|
||||
#instance.reset()
|
||||
instance.reset()
|
||||
yield instance
|
||||
|
||||
|
||||
@@ -116,8 +116,6 @@ def test_place_and_connect_entities_in_grid(game):
|
||||
assert spent_furnaces == grid_size * grid_size
|
||||
assert spent_inserters == 2 * grid_size * (grid_size - 1)
|
||||
|
||||
game.reset()
|
||||
|
||||
def test_basic_connection_between_furnace_and_miner(game):
|
||||
"""
|
||||
Place a furnace with a burner inserter pointing towards it.
|
||||
@@ -197,7 +195,7 @@ def test_burner_inserter_grid_with_coal_movement(game):
|
||||
target = game.place_entity(Prototype.IronChest, position=inserters[0][0].drop_position)
|
||||
game.insert_item(Prototype.Coal, source, 50)
|
||||
# Wait for some time to allow coal to move, assuming there's a method to wait in game
|
||||
sleep(60) # Wait for 200 ticks or adjust as needed based on game speed
|
||||
game.sleep(60) # Wait for 200 ticks or adjust as needed based on game speed
|
||||
|
||||
# Now check if the coal has reached the top left point (i.e., the first inserter in the grid)
|
||||
# Assuming there's a method to inspect the contents of an inserter
|
||||
|
Reference in New Issue
Block a user