| 13 | DEPOT_ROOT = Point(125, 130) |
| 14 | |
| 15 | class Depot(): |
| 16 | def __init__(self, top_left: Point, entities: List[str]): |
| 17 | self.root = top_left |
| 18 | self.resource_roots: Dict[str, Point] = {} |
| 19 | self.resource_count: Dict[str, int] = {} |
| 20 | # keep track of how many "chunks" |
| 21 | self.height = 6 |
| 22 | for idx, entity in enumerate(entities): |
| 23 | self.resource_roots[entity] = self.root + Point(2 * idx, 0) |
| 24 | self.resource_count[entity] = 0 |
| 25 | width = len(self.resource_roots) * 3 |
| 26 | height = 6 |
| 27 | self.area = Rect(self.root, self.root + Point(width, height)) |
| 28 | |
| 29 | def address(self, entity: str, n: int) -> Point: |
| 30 | root = self.resource_roots[entity] |
| 31 | y = n + ((n - 1) // 5) |
| 32 | return root + Point(0, y) |
| 33 | |
| 34 | def store(self, dog: Dog, entity: str): |
| 35 | try: |
| 36 | self.resource_count[entity] += 1 |
| 37 | count = self.resource_count[entity] |
| 38 | address = self.address(entity, count) |
| 39 | if not self.area.contains(address): |
| 40 | print(f"WARNING address {address} outside of area: {self.area}") |
| 41 | dog.drop_off(address) |
| 42 | except KeyError: |
| 43 | print(f"error, tried to store {entity}") |
| 44 | |
| 45 | def take(self, dog: Dog, entity: str): |
| 46 | count = self.resource_count[entity] |
| 47 | if count > 0: |
| 48 | self.resource_count[entity] -= 1 |
| 49 | address = self.address(entity, count) |
| 50 | dog.pick_known(entity, address) |
| 51 | dog.goto(self.root + Point(-1, -1)) |
| 52 | |
| 53 | def lowest_resource(self) -> str: |
| 54 | min_count = min(self.resource_count.values()) |
| 55 | for (entity, count) in self.resource_count.items(): |
| 56 | if count == min_count: |
| 57 | return entity |
| 58 | |
| 59 | def expand_area(self, dog: Dog): |
| 60 | dog.charge_if_needed() |
| 61 | tl = Point(self.root.x, self.area.bot_right.y + 1) |
| 62 | br = Point(self.area.bot_right.x, self.area.bot_right.y + 6) |
| 63 | area_to_clear = Rect(tl, br) |
| 64 | clear_area(dog, area_to_clear, self) |
| 65 | self.area.bot_right.y += 6 |
| 66 | |
| 67 | |
| 68 | def gather(self, dog: Dog): |
| 69 | while True: |
| 70 | dog.charge_if_needed() |
| 71 | entity = self.lowest_resource() |
| 72 | print(f"gathering {entity}") |