| 10 | |
| 11 | |
| 12 | class Dog(Agent): |
| 13 | |
| 14 | def __init__(self, port: int): |
| 15 | # fake location to start |
| 16 | super().__init__(port, Point(-1, -1)) |
| 17 | self.char_locations: Dict[str, Set[Point]] = defaultdict(set) |
| 18 | self.goto_recursion_count = 0 |
| 19 | (location, _, _, _) = self.stat() |
| 20 | self.location: Point = location |
| 21 | self.facing: Direction = Direction.E |
| 22 | # TODO add these to init arguments |
| 23 | self.charge_location: Optional[Point] = None |
| 24 | self.depot_area: Rect = Rect(Point(0,0), Point(0,0)) |
| 25 | |
| 26 | def charge_if_needed(self): |
| 27 | (_, _, charge, _) = self.stat() |
| 28 | if charge < 90: |
| 29 | if not self.charge_location: |
| 30 | raise ValueError("DID NOT INIT CHARGE LOCATION") |
| 31 | else: |
| 32 | print("attempting to charge") |
| 33 | self.approach(self.charge_location) |
| 34 | self.send("CHRG") |
| 35 | seconds = (100 - charge) / 10 |
| 36 | print(f"sleeping for {seconds} seconds") |
| 37 | time.sleep(seconds) |
| 38 | |
| 39 | |
| 40 | def send(self, msg: str) -> str: |
| 41 | resp = super().send(msg) |
| 42 | if resp == "ERRR low battery": |
| 43 | seconds = 200 |
| 44 | print(f"sleeping for {seconds} to let dog charge") |
| 45 | time.sleep(seconds) |
| 46 | (pos, facing, _, _) = self.stat() |
| 47 | self.charge_if_needed() |
| 48 | print("returning to position") |
| 49 | self.goto(pos) |
| 50 | self.face(facing) |
| 51 | print("resuming") |
| 52 | return self.send(msg) |
| 53 | |
| 54 | return resp |
| 55 | |
| 56 | def pick(self, char: str, target: Optional[Point] = None): |
| 57 | self.send(f"PICK {char}") |
| 58 | if target is None: |
| 59 | target = self.location.forward(self.facing) |
| 60 | # (pos, facing, _, _) = self.stat() |
| 61 | # target = pos.forward(facing) |
| 62 | try: |
| 63 | self.char_locations[char].remove(target) |
| 64 | except KeyError: |
| 65 | print(f"{char} {target} was not in known locations") |
| 66 | |
| 67 | def stat(self) -> Tuple[Point, str, int, str]: |
| 68 | reply = self.send("STAT") |
| 69 | parts = reply.split(" ") |