Bladeren bron

Added Terror!

Steve Thielemann 5 jaren geleden
bovenliggende
commit
25d0b5d297
1 gewijzigde bestanden met toevoegingen van 134 en 1 verwijderingen
  1. 134 1
      flexible.py

+ 134 - 1
flexible.py

@@ -1754,7 +1754,88 @@ class ScriptSpace(object):
                 self.deactivate()
 
 
-from boxes import Boxes
+class ScriptTerror(object):
+    """ Terror script. """
+    def __init__(self, game, proxy, count):
+        self.game = game
+        self.queue_game = game.queue_game
+        self.queue_player = game.queue_player
+        self.proxy = proxy
+        self.count = count
+        self.observer = game.observer
+        self.r = Style.RESET_ALL
+        self.nl = "\n\r"
+
+        self.target_sector = self.proxy.find_next_good_trade_pair()   # Sector going to
+
+        # Activate
+        self.prompt = game.buffer
+        self.save = self.observer.save()
+        self.observer.connect('player', self.player)
+        self.observer.connect("prompt", self.game_prompt)
+        self.observer.connect("game-line", self.game_line)
+
+        self.defer = None
+
+        self.state = 1
+        self.queue_player.put("{0}\r".format(self.target_sector))
+
+
+    def whenDone(self):
+        self.defer = defer.Deferred()
+        # Call this to chain something after we exit.
+        return self.defer
+
+    def deactivate(self, andExit=False):
+        self.state = 0
+        log.debug("ScriptTerror.deactivate")
+        assert(not self.save is None)
+        self.observer.load(self.save)
+        self.save = None
+        if self.defer:
+            if andExit:
+                self.defer.callback({'exit':True})                
+            else:
+                self.defer.callback('done')
+            self.defer = None
+
+    def player(self, chunk: bytes):
+        # If we receive anything -- ABORT!
+        self.deactivate(True)
+
+    def journey_on(self, *_):
+        log.info("journey_on( {0})".format(self.count))
+        if self.count > 0:
+            self.count -= 1
+            self.target_sector = self.proxy.find_next_good_trade_pair()   # Sector going to
+            self.state = 1
+            self.queue_player.put("{0}\r".format(self.target_sector))
+        else:
+            self.deactivate()
+
+    def game_prompt(self, prompt: str):
+        log.debug("{0} : {1}".format(self.state, prompt))
+        if self.state == 1:
+            if prompt.startswith('Do you want to engage the TransWarp drive? '):
+                self.queue_player.put("N")
+        elif self.state == 2:
+            if prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
+                self.queue_player.put("E")
+            elif re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt): 
+                # We should be where we wanted to.
+                ports = ScriptPort(self.game)
+                d = ports.whenDone()
+                d.addCallback(self.journey_on)
+                d.addErrback(self.journey_on)
+
+    def game_line(self, line: str):
+        log.debug("line {0} : {1}".format(self.state, line))
+
+        if self.state == 1:
+            if line.startswith('The shortest path ('):
+                self.state = 2
+
+
 
 class ProxyMenu(object):
     """ Display ProxyMenu 
@@ -1858,6 +1939,40 @@ class ProxyMenu(object):
         self.queue_game.put("Loaded {0} sectors.".format(len(warpdata)) + self.nl)
         self.welcome_back()
 
+    def find_next_good_trade_pair(self):
+        """ Find the next GOOD trade pair sector. """
+
+        show_limit = 90
+
+        for sector in sorted(self.game.gamedata.ports.keys()):
+            pd = self.game.gamedata.ports[sector]
+            if not GameData.port_burnt(pd):
+                pc = pd['class']
+
+                # Ok, let's look into it.
+                if not sector in self.game.gamedata.warps:
+                    continue
+
+                warps = self.game.gamedata.warps[sector]
+                for w in warps:
+                    # We can get there, and get back.
+                    if w in self.game.gamedata.warps and sector in self.game.gamedata.warps[w]:
+                        # Ok, we can get there -- and get back!
+                        if w > sector and w in self.game.gamedata.ports and not GameData.port_burnt(self.game.gamedata.ports[w]):
+                            wd = self.game.gamedata.ports[w]
+                            wc = wd['class']
+
+                            if pc in (1,5) and wc in (2,4):
+                                data = self.game.gamedata.port_trade_show(sector, w, show_limit)
+                                if data:
+                                    return sector
+                            elif pc in (2,4) and wc in (1,5):
+                                data = self.game.gamedata.port_trade_show(sector, w, show_limit)
+                                if data:
+                                    return sector
+            # yield 
+        return None
+
     def make_trade_report(self):
         log.debug("make_trade_report()")
         ok_trades = []
@@ -2285,6 +2400,18 @@ class ProxyMenu(object):
         self.queue_game.put(box.bottom())
         self.queue_game.put("   " + c1 + "-=>" + self.r + " ")
 
+    def terror(self, *_):
+        log.debug("terror {0}".format(_))
+        loops = _[0]
+        if loops.strip() == '':
+            self.deactivate_scripts_menu()
+        else:
+            # Ok, we have something here, I think...
+            terror = ScriptTerror(self.game, self, int(loops))
+            d = terror.whenDone()
+            d.addCallback(self.deactivate_scripts_menu)
+            d.addErrback(self.deactivate_scripts_menu)
+
     def scripts_player(self, chunk: bytes):
         """ Data from player (in bytes). """
         chunk = chunk.decode("latin-1", "ignore")
@@ -2300,6 +2427,12 @@ class ProxyMenu(object):
             d.addCallback(self.deactivate_scripts_menu)
             d.addErrback(self.deactivate_scripts_menu)
             return
+        elif key == '!':
+            self.queue_game.put(self.c + key + self.r + self.nl)
+            ask = PlayerInput(self.game)
+            d = ask.prompt("How many loops of terror?", 4, name="loops", digits=True, abort_blank=True)
+            d.addCallback(self.terror, ask)
+            return            
         elif key == '2':
             self.queue_game.put(self.c + key + self.r + self.nl)
             explore = ScriptExplore(self.game)