فهرست منبع

ColoScript

Colonize your planet with ease.
david 5 سال پیش
والد
کامیت
211004b757
1فایلهای تغییر یافته به همراه258 افزوده شده و 0 حذف شده
  1. 258 0
      flexible.py

+ 258 - 0
flexible.py

@@ -2483,6 +2483,256 @@ class PlanetUpScript(object):
             self.game.to_player = self.to_player
             self.observer.load(self.save)
 
+class ColoScript(object):
+    """ Colonise Script
+
+        Macro:
+        ntel^^n1els^^1^q
+
+        Ask what planet we want to use,
+        Then Move to Terra,
+        load up,
+        Move to target planet,
+        unload,
+        repeat x times! \o/
+
+        States:
+        1 = Computer, Your Planets, Quit Computer
+        2 = Grab planets from list, ask user which planet
+        3 = Move to Sector 1
+        4 = Finish Moving, load people, then move to sector with planet
+        5 = Finish Moving, init land
+        6 = Decide to loop (jump to 3), Unload people
+    """
+    def __init__(self, game):
+        self.game = game
+        self.queue_game = game.queue_game
+        self.queue_player = game.queue_player
+        self.observer = game.observer
+        self.r = Style.RESET_ALL
+        self.c = merge(Style.BRIGHT + Fore.YELLOW)
+        self.nl = "\n\r"
+
+        # My Stuff
+        self.state = 1
+        self.corp = True
+        self.planets = {}
+        self.loops = 0
+        self.maxloops = 0
+        
+        # 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.send2game("D")
+
+    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("ColoScript.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 send2game(self, txt):
+        log.debug("ColoScript.send2game({0})".format(txt))
+        self.queue_player.put(txt)
+
+    def send2player(self, txt):
+        log.debug("ColoScript.send2player({0})".format(txt))
+        self.queue_game.put(txt)
+
+    def planet_chosen(self, choice: str):
+        if choice.strip() == '':
+            self.deactivate()
+        else:
+            self.planet_number = int(choice)
+            if self.planet_number in self.planets:
+                # Ok, this'll work
+                self.planet_sector = self.planets[self.planet_number]['sector']
+                self.planet_name = self.planets[self.planet_number]['name']
+                # Are we really getting this? Yup
+                #log.debug("Planet Number: {0} Sector: {1}".format(self.planet_number, self.planet_sector))
+                ask1 = PlayerInput(self.game)
+                d1 = ask1.prompt("How many times", 3, name="rolls", digits=True)
+                d1.addCallback(self.loop_chosen)
+                self.state = 4
+                self.send2game("1\r")
+            else:
+                self.deactivate()
+
+    def loop_chosen(self, choice: str):
+        if choice.strip() == '':
+            self.deactivate()
+        else:
+            self.loops = int(choice)
+            self.maxloops = self.loops
+            self.state = 4
+            self.send2game("1\r")
+
+    def game_prompt(self, prompt: str):
+        log.debug("P {0} | {1}".format(self.state, prompt))
+        if self.state == 1 and re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
+            # Ok, you're not on a team.  :P
+            self.queue_player.put("CYQ")
+        if self.state == 2 and re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
+            if self.corp:
+                self.corp = False
+                self.state = 1
+                self.queue_player.put("CYQ")
+                return
+            self.game.to_player = True
+            # For now we output the information, and exit
+            # self.queue_game.put("{0}\r\n".format(self.planets))
+            # self.queue_game.put(pformat(self.planets).replace("\n", self.nl) + self.nl)
+
+            if len(self.planets) == 0:
+                # Ok, this is easy.
+                self.queue_game.put(self.nl + Boxes.alert("You don't have any planets?  You poor, poor dear.", base="red"))
+                self.deactivate()
+                return
+
+            # I need this to know if I can just land, or need to move to the planet.
+            # Get current sector from the prompt
+            # Command [TL=00:00:00]:[10202] (?=Help)? :
+            _, _, part = prompt.partition(']:[')
+            sector, _, _ = part.partition(']')
+            self.current_sector = int(sector)
+
+            # A better default is to ask which planet to upgrade.
+
+            tc = merge(Style.BRIGHT + Fore.YELLOW + Back.BLUE)
+            c1 = merge(Style.BRIGHT + Fore.WHITE + Back.BLUE)
+            c2 = merge(Style.BRIGHT + Fore.CYAN + Back.BLUE)
+
+            box = Boxes(44, color=tc)
+            self.queue_game.put(box.top())
+            self.queue_game.put(box.row(tc + "{0:3} {1:6} {2:33}".format(" # ", "Sector", "Planet Name")))
+            self.queue_game.put(box.middle())
+
+            def planet_output(number, sector, name):
+                row = "{0}{1:^3} {2:6} {3}{4:33}".format(c1, number, sector, c2, name)
+                self.queue_game.put(box.row(row))
+
+            for s in sorted(self.planets.keys()):
+                planet_output(s, self.planets[s]['sector'], self.planets[s]['name'])
+
+            self.queue_game.put(box.bottom())
+
+            ask = PlayerInput(self.game)
+            d = ask.prompt("Choose a planet", 3, name="planet", digits=True)
+            d.addCallback(self.planet_chosen)
+        elif self.state == 3:
+            if prompt.startswith('Do you want to engage the TransWarp drive? '):
+                self.queue_player.put("N")
+            elif prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
+                self.queue_player.put("E")
+            elif prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ?'):
+                self.queue_player.put("N")
+            elif re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):             
+                # We're here!
+                self.state = 4
+                self.send2game("1\r")
+        elif self.state == 4:
+            if prompt.startswith('Do you want to engage the TransWarp drive? '):
+                self.queue_player.put("N")
+            elif prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
+                self.queue_player.put("E")
+            elif prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ?'):
+                self.queue_player.put("N")
+            elif re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):             
+                self.state = 5
+                self.send2game("L\r\r{0}\r".format(self.planet_sector))
+        elif self.state == 5:
+            if prompt.startswith('Do you want to engage the TransWarp drive? '):
+                self.queue_player.put("N")
+            elif prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
+                self.queue_player.put("E")
+            elif prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ?'):
+                self.queue_player.put("N")
+            elif re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):             
+                self.state = 6
+                self.send2game("L")
+        elif self.state == 6:
+            if prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
+                self.state = 5
+                self.send2game("E")
+            if prompt.startswith('Land on which planet <Q to abort> ?'):
+                self.queue_player.put("{0}\r".format(self.planet_number))
+            if prompt.startswith('Planet command (?=help) [D] '):
+                self.loops -= 1
+                if self.loops:
+                    self.state = 3
+                    self.send2game("S\r\r1\rQ")
+                else:
+                    self.send2game("S\r\r1\rQ")
+                    self.send2player("\r" + Boxes.alert("Completed ({0})".format(self.maxloops)))
+                    self.deactivate()
+            
+
+    def game_line(self, line: str):
+        log.debug("L {0} | {1}".format(self.state, line))
+        if self.state == 1:
+            if 'Personal Planet Scan' in line or 'Corporate Planet Scan' in line:
+                self.state = 2
+        elif self.state == 2:
+            # Ok, we're in the planet scan part of this
+            # 10202   #3    Home of Bugz             Class M, Earth Type        No Citadel]
+            if '#' in line:
+                # Ok, we have a planet detail line.
+                # There's an extra T when the planet is maxed out.
+                if line[7] == 'T':
+                    line = line[:7] + ' ' + line[8:]
+
+                detail, _, _ = line.partition('Class')
+                detail = detail.strip() # Sector  #X  Name of planet
+                sector, number, name = re.split(r'\s+', detail, 2)
+                sector = int(sector)
+                number = int(number[1:])
+                self.last_seen = number
+                self.planets[number] = {"sector": sector, "name": name}
+                log.info("Planet # {0} in {1} called {2}".format( number, sector, name))
+            if '---' in line:
+                number = self.last_seen
+                # Ok, take the last_seen number, and use it for this line
+                # [ Sector  Planet Name    Ore  Org  Equ   Ore   Org   Equ   Fighters    Citadel]
+                # [ Shields Population    -=Productions=-  -=-=-=-=-On Hands-=-=-=-=-    Credits]
+                # [ 10202   #3    Home of Bugz             Class M, Earth Type        No Citadel]
+                # [   ---   (1M)           144   49   26   145    75    12         10          0]
+                details = re.split(r"\s+", line.strip())
+                # OK:  Likely, I'm not going to use these numbers AT ALL.  
+                self.planets[number]['population'] = details[1].replace('(', '').replace(')', '')
+                # Ok, there's going to have to be some sort of modifier (K, M, etc.)
+                # to these numbers.  Beware!
+                self.planets[number]['ore'] = details[5]
+                self.planets[number]['org'] = details[6]
+                self.planets[number]['equ'] = details[7]
+        elif self.state == 5:
+            if 'turns left' in line:
+                work = line[19:].replace(' turns left.', '').strip()
+                self.turns = work
+                log.debug("TURNS LEFT: {0}".format(self.turns))
+                if int(self.turns) < 200:
+                    self.send2player("\r" + Boxes.alert("Low Turns! ({0})".format(self.turns)))
+                    self.deactivate()
 
 class ProxyMenu(object):
     """ Display ProxyMenu 
@@ -3102,6 +3352,7 @@ class ProxyMenu(object):
         menu_item("2", "Explore (Strange new sectors)")
         menu_item("3", "Space... the broken script...")
         menu_item("4", "Upgrade Planet")
+        menu_item("5", "Colonize Planet")
         menu_item("X", "eXit")
         self.queue_game.put(box.bottom())
         self.queue_game.put("   " + c1 + "-=>" + self.r + " ")
@@ -3164,6 +3415,13 @@ 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)
+            colo = ColoScript(self.game)
+            d = colo.whenDone()
+            d.addCallback(self.deactivate_scripts_menu)
+            d.addErrback(self.deactivate_scripts_menu)
+            return
         elif key == 'X':
             self.queue_game.put(self.c + key + self.r + self.nl)            
             self.deactivate_scripts_menu()