Bladeren bron

Working find_nearest_selling.

Steve Thielemann 5 jaren geleden
bovenliggende
commit
fa780b1f13
1 gewijzigde bestanden met toevoegingen van 59 en 0 verwijderingen
  1. 59 0
      galaxy.py

+ 59 - 0
galaxy.py

@@ -383,3 +383,62 @@ class GameData(object):
             GameData.port_show_part(warp, warp_port),
         )
 
+    def find_nearest_selling(
+        self, sector: int, selling: str, at_least: int = 100
+    ) -> int:
+        """ find nearest port that is selling at_least amount of this item 
+
+        selling is 'f', 'o', or 'e'.
+        """
+        names = {"e": "equ", "o": "org", "f": "fuel"}
+        pos = {"f": 0, "o": 1, "e": 2}
+        sell = names[selling[0].lower()]
+        s_pos = pos[selling[0].lower()]
+        log.warn(
+            "find_nearest_selling({0}, {1}, {2}): {3}, {4}".format(
+                sector, selling, at_least, sell, s_pos
+            )
+        )
+        searched = set()
+        if sector not in self.warps:
+            log.warn("( {0}, {1}): sector not in warps".format(sector, selling))
+            return 0
+
+        # Start with the current sector
+        look = set((sector,))
+
+        while len(look) > 0:
+            for s in look:
+                if s in self.ports:
+                    # Ok, possibly?
+                    sp = self.ports[s]
+                    if sp["port"][s_pos] == "S":
+                        # Ok, they are selling!
+                        if sell in sp:
+                            if sp[sell]["units"] >= at_least:
+                                log.warn(
+                                    "find_nearest_selling( {0}, {1}): {2} {3} units".format(
+                                        sector, selling, s, sp[sell]["units"]
+                                    )
+                                )
+                                return s
+                        else:
+                            # We know they sell it, but we don't know units
+                            log.warn(
+                                "find_nearest_selling( {0}, {1}): {2} {3}".format(
+                                    sector, selling, s, sp["port"]
+                                )
+                            )
+                            return s
+            # Ok, not found here.  Branch out.
+            searched.update(look)
+            step_from = look
+            look = set()
+            for s in step_from:
+                if s in self.warps:
+                    look.update(self.warps[s])
+            # look only contains warps we haven't searched
+            look = look.difference(searched)
+        # Ok, we have run out of places to search
+        log.warn("find_nearest_selling( {0}, {1}) : failed".format(sector, selling))
+        return 0