瀏覽代碼

MaxFighterMake sets population levels to make figs

 Designed to automatically set the population levels to make the most
fighters per day.
david 5 年之前
父節點
當前提交
ba2b7abed5
共有 1 個文件被更改,包括 415 次插入5 次删除
  1. 415 5
      flexible.py

+ 415 - 5
flexible.py

@@ -3301,10 +3301,412 @@ class ColoScript2(object):
                 )
                 # 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]
-                # self.planets[number]['cit'] = details[9] # This is if we wanted cit money... but that's not it
+                self.planets[number]['ore'] = details[5]
+                self.planets[number]['org'] = details[6]
+                self.planets[number]['equ'] = details[7]
+                #self.planets[number]['cit'] = details[9] # This is if we wanted cit money... but that's not it
+
+    
+class MaxFighterMake(object):
+    """ Max Fighter Make
+
+        State of the Union:
+        1 = Grab planets in current sector
+        2 = Ask what planet to utilize, land
+        3 = Grab Snapshot of planet (We will need to check population lvls too)
+        4 = try cat 1 to cat 2 (Or perhaps this defaults to false if cat 2 is N/A)
+        5 = if bad then try cat 1 to cat 3 (Or perhaps this defaults to false if cat 3 is N/A)
+        6 = if bad then try cat 2 to cat 3 (Or perhaps this defaults to false if cat 3 is N/A)
+        7 = loop back to state 3 until all result in bad (Yay best acheived)
+    """
+    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" in my pants!
+        self.state = 1
+        self.corp = True
+        self.target_sector = 0 # Sector Number of target
+        self.target_number = 0 # Planet Number of target
+        self.last_seen = {'Ore': False, 'Org': False, 'Equ': False, 'Fig': False}
+        self.max_product = 0
+        self.trying = 'Ore2Org'
+        self.trying_NEXT = False
+        self.movement = 1000
+
+        self.planets_list = [] # Will be a list of dicts, containing planet information
+        self.cap = False # Capture line for planet
+        
+        # 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.ore = {}
+        self.org = {}
+        self.equ = {}
+        self.fig = {}
+
+        self.defer = None
+        self.to_player = self.game.to_player
+        self.game.to_player = False
+        self.send2game("D")
+
+    def whenDone(self):
+        self.defer = defer.Deferred()
+        return self.defer
+
+    def deactivate(self, andExit=False):
+        self.state = 0
+        log.debug("ColoScript2.deactivate()")
+        self.game.to_player = True
+        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):
+        self.deactivate(True)
+
+    def send2game(self, txt):
+        # Removed debug info since I use this to also display Boxes.alert
+        self.queue_player.put(txt)
+
+    def send2player(self, txt):
+        # Removed debug since it really doesn't help with anything
+        self.queue_game.put(txt)
+
+    def target_chosen(self, choice: str):
+        if choice.strip() == '':
+            self.deactivate(True)
+        else:
+            self.target_number = int(choice)
+            if self.target_number > self.total:
+                self.deactivate(True)
+            else:
+                self.state = 3
+                self.send2game("L")
+
+    def check_resource(self, resource):
+        # Given a valid string that can be converted to int
+        if resource == 'N/A':
+            resource = -1
+        else:
+            resource = int(resource.replace(',', ''))
+        return resource
+
+    def game_prompt(self, prompt: str):
+        log.debug("P {0} | {1}".format(self.state, prompt))
+        if self.state == 2:
+            self.game.to_player = True
+            if len(self.planets_list) == 0:
+                self.send2player(self.nl + Boxes.alert("No planets in this sector!"))
+                self.deactivate(True)
+        
+            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:20} {2:18} ".format(" # ", "Planet Name", "Planet Type")))
+            self.queue_game.put(box.middle())
+
+            def planet_output(num, planet_name, planet_type):
+                row = "{0}{1:3} {2}{3:29} {4:9} ".format(c1, num, c2, planet_name, planet_type)
+                self.send2player(box.row(row))
+            
+            self.total = 0
+            for p in self.planets_list:
+                planet_output(self.total, p['name'], p['type'])
+                self.total += 1
+            
+            ask = PlayerInput(self.game)
+            d = ask.prompt("Choose a planet", 3, name="planet", digits=True)
+            d = d.addCallback(self.target_chosen)
+        elif self.state == 4:
+            if prompt.startswith("Planet command (?=help) [D]"):
+                self.send2game('?')
+                self.state = 6
+        elif self.state == 6:
+            if prompt.startswith("Planet command (?=help) [D]"):
+                self.send2game('P')
+                self.state = 7
+        elif self.state == 7:
+            if prompt.startswith("Display planet?"):
+                self.send2game('N')
+                self.state = 8
+        elif self.state == 8:
+            if prompt.startswith("(1)Ore, (2)Org or (3)Equipment?"):
+                if self.trying == 'Ore2Org' or self.trying == 'Ore2Equ':
+                    self.send2game('1')
+                elif self.trying == 'Org2Equ' or self.trying == 'Org2Ore':
+                    self.send2game('2')
+                elif self.trying == 'Equ2Org' or self.trying == 'Equ2Ore':
+                    self.send2game('3')
+                self.state = 9
+        elif self.state == 9:
+            if prompt.startswith("How many groups of Colonists do you want to move?"):
+                self.send2game('{0}\r'.format(self.movement))
+                self.state = 10
+        elif self.state == 10:
+            if prompt.startswith("(1)Ore, (2)Org or (3)Equipment?"):
+                if self.trying == 'Org2Ore' or self.trying == 'Equ2Ore':
+                    self.send2game('1')
+                elif self.trying == 'Ore2Org' or self.trying == 'Equ2Org':
+                    self.send2game('2')
+                elif self.trying == 'Ore2Equ' or self.trying == 'Org2Equ':
+                    self.send2game('3')
+                self.state = 11
+        elif self.state == 11:
+            if prompt.startswith('Planet command (?=help) [D]'):
+                self.send2game('D')
+                self.trying_NEXT = False
+                self.last_seen['Ore'] = False
+                self.last_seen['Org'] = False
+                self.last_seen['Equ'] = False
+                self.last_seen['Fig'] = False
+                self.state = 12
+        elif self.state == 12:
+            if prompt.startswith('Planet command (?=help) [D]'):
+                self.send2game('?')
+                self.state = 6
+            #if(self.trying == 'Ore2Org'):
+            #    self.send2game('P\r1{0}2D'.format(self.movement))
+            #    self.state = 4
+            #    # Back to looking at the planet
+            #elif(self.trying == 'Ore2Equ'):
+            #    self.send2game('P\r1{0}3D'.format(self.movement))
+            #    self.state = 4
+            #    # Back to looking at the planet
+            #elif(self.trying == 'Org2Equ'):
+            #    self.send2game('P\r2{0}3D'.format(self.movement))
+            #    self.state = 4
+            #elif(self.trying == 'reverse'):
+            #    if(self.trying_OLD == 'Ore2Org'):
+            #        self.send2game('P\r2{0}1D'.format(self.movement))
+            #    elif(self.trying_OLD == 'Ore2Equ'):
+            #        self.send2game('P\r3{0}1D'.format(self.movement))
+            #    elif(self.trying_OLD == 'Org2Equ'):
+            #        self.send2game('P\r3{0}2D'.format(self.movement))
+            #    self.state = 4
+            #    self.trying = self.trying_NEXT
+
+    def game_line(self, line: str):
+        log.debug("L {0} | {1}".format(self.state, line))
+        # IF at any state we see turns left lets grab it
+        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(True)
+        # IF at any state we see how many holds avalible let's get that
+        if 'Total Holds' in line:
+            work = line[16:].replace('-', '').replace('=', ' ').split()
+            self.total_holds = int(work[0])
+            count = 0
+            for w in work:
+                if(w != 'Empty'):
+                    count += 1
+                elif(w == 'Empty'):
+                    count += 1
+                    self.holds = int(work[count])
+                    log.debug("EMPTY HOLDS = {0}".format(self.holds))
+                    if(self.holds < self.total_holds):
+                        self.send2player("\r" + Boxes.alert("You need {0} holds empty! ({1} Empty)".format(self.total_holds, self.holds)))
+                        self.deactivate(True)
+        if "There aren't that many on the planet!" in line:
+            self.send2player("\r" + Boxes.alert("We're missing people!"))
+            self.deactivate(True)
+        if "There isn't room on the planet for that many!" in line:
+            self.send2player("\r" + Boxes.alert("We have to many people!"))
+            self.deactivate(True)
+
+        # Now back to our scheduled program
+        if self.state == 1:
+            if line.startswith("Planets"):
+                self.cap = True
+                work = re.split(r'\s+', line)
+                #log.debug("'{0}'".format(work))
+                final = {'name': work[-1], 'type': work[-2]}
+                self.planets_list.append(final)
+                log.debug("Planet {0} type {1}".format(final['name'], final['type']))
+            elif line.startswith("Ships"): # Stop 1, so we don't capture everything
+                self.cap = False
+            elif line.startswith("Fighters"): # Stop 2, so we don't capture everything
+                self.cap = False
+            elif self.cap == True:
+                work = re.split(r'\s+', line)
+                #log.debug("'{0}'".format(work))
+                final = {'name': work[-1], 'type': work[-2]}
+                self.planets_list.append(final)
+                log.debug("Planet {0} type {1}".format(final['name'], final['type']))
+            elif self.cap == False and len(self.planets_list) != 0:
+                self.state = 2
+        elif self.state == 3:
+            if self.planets_list[self.target_number]['name'] in line:
+                work = re.split(r'\s+', line)
+                log.debug("'{0}'".format(work))
+                pnumber = work[2].replace('>', '')
+                self.planets_list[self.target_number]['pnumber'] = pnumber # Add this to our dict of the planet
+                log.debug("Pnumber {0} is {1}".format(pnumber, self.planets_list[self.target_number]['name']))
+                self.send2game("{0}\r".format(pnumber))
+                self.state = 4
+        elif self.state == 4:
+            # Item    Colonists  Colonists    Daily     Planet      Ship      Planet  
+            #          (1000s)   2 Build 1   Product    Amount     Amount     Maximum 
+            # -------  ---------  ---------  ---------  ---------  ---------  ---------
+            # Fuel Ore        707          2        353     10,376          0    200,000
+            # Organics        651          5        130      4,304          0    200,000
+            # Equipment     1,641         20         82      2,618          0    100,000
+            # Fighters        N/A         63         47      1,463        400  1,000,000
+            if line.startswith('Fuel Ore'):
+                work = re.split(r'\s+', line)
+                #log.debug("Ore - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[2], work[3], work[4], work[5], work[6], work[7]))
+                self.ore = {
+                    'pop': self.check_resource(work[2]),
+                    'make': self.check_resource(work[3]),
+                    'daily': self.check_resource(work[4]),
+                    'amount': self.check_resource(work[5]),
+                    'ship': self.check_resource(work[6]),
+                    'pmax': self.check_resource(work[7])
+                }
+                self.last_seen['Ore'] = True
+            elif line.startswith('Organics'):
+                work = re.split(r'\s+', line)
+                #log.debug("Org - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[1], work[2], work[3], work[4], work[5], work[6]))
+                self.org = {
+                    'pop': self.check_resource(work[1]),
+                    'make': self.check_resource(work[2]),
+                    'daily': self.check_resource(work[3]),
+                    'amount': self.check_resource(work[4]),
+                    'ship': self.check_resource(work[5]),
+                    'pmax': self.check_resource(work[6])
+                }
+                self.last_seen['Org'] = True
+            elif line.startswith('Equipment'):
+                work = re.split(r'\s+', line)
+                #log.debug("Equ - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[1], work[2], work[3], work[4], work[5], work[6]))
+                self.equ = {
+                    'pop': self.check_resource(work[1]),
+                    'make': self.check_resource(work[2]),
+                    'daily': self.check_resource(work[3]),
+                    'amount': self.check_resource(work[4]),
+                    'ship': self.check_resource(work[5]),
+                    'pmax': self.check_resource(work[6])
+                }
+                self.last_seen['Equ'] = True
+            elif line.startswith('Fighters'):
+                work = re.split(r'\s+', line)
+                #log.debug("Fig - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[1], work[2], work[3], work[4], work[5], work[6]))
+                self.fig = {
+                    'pop': self.check_resource(work[1]),
+                    'make': self.check_resource(work[2]),
+                    'daily': self.check_resource(work[3]),
+                    'amount': self.check_resource(work[4]),
+                    'ship': self.check_resource(work[5]),
+                    'pmax': self.check_resource(work[6])
+                }                
+                self.last_seen['Fig'] = True
+            else:
+                if self.last_seen['Ore'] == True and self.last_seen['Org'] == True and self.last_seen['Equ'] == True and self.last_seen['Fig'] == True:
+                    if self.fig['daily'] > self.max_product:
+                        self.max_product = self.fig['daily']
+                    
+                    self.planets_list[self.target_number]['Ore'] = self.ore
+                    self.planets_list[self.target_number]['Org'] = self.org
+                    self.planets_list[self.target_number]['Equ'] = self.equ
+                    self.planets_list[self.target_number]['Fig'] = self.fig
+                    log.debug("Max Production is {0}!".format(self.max_product))
+                    self.state == 6
+        elif self.state == 12:
+            if line.startswith('Fuel Ore'):
+                work = re.split(r'\s+', line)
+                #log.debug("Ore - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[2], work[3], work[4], work[5], work[6], work[7]))
+                self.ore = {
+                    'pop': self.check_resource(work[2]),
+                    'make': self.check_resource(work[3]),
+                    'daily': self.check_resource(work[4]),
+                    'amount': self.check_resource(work[5]),
+                    'ship': self.check_resource(work[6]),
+                    'pmax': self.check_resource(work[7])
+                }
+                self.last_seen['Ore'] = True
+            elif line.startswith('Organics'):
+                work = re.split(r'\s+', line)
+                #log.debug("Org - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[1], work[2], work[3], work[4], work[5], work[6]))
+                self.org = {
+                    'pop': self.check_resource(work[1]),
+                    'make': self.check_resource(work[2]),
+                    'daily': self.check_resource(work[3]),
+                    'amount': self.check_resource(work[4]),
+                    'ship': self.check_resource(work[5]),
+                    'pmax': self.check_resource(work[6])
+                }
+                self.last_seen['Org'] = True
+            elif line.startswith('Equipment'):
+                work = re.split(r'\s+', line)
+                #log.debug("Equ - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[1], work[2], work[3], work[4], work[5], work[6]))
+                self.equ = {
+                    'pop': self.check_resource(work[1]),
+                    'make': self.check_resource(work[2]),
+                    'daily': self.check_resource(work[3]),
+                    'amount': self.check_resource(work[4]),
+                    'ship': self.check_resource(work[5]),
+                    'pmax': self.check_resource(work[6])
+                }
+                self.last_seen['Equ'] = True
+            elif line.startswith('Fighters'):
+                work = re.split(r'\s+', line)
+                #log.debug("Fig - Pop: {0}, 2make1: {1} dailyproduct: {2} pamount: {3} saamount: {4} pmax: {5}".format(work[1], work[2], work[3], work[4], work[5], work[6]))
+                self.fig = {
+                    'pop': self.check_resource(work[1]),
+                    'make': self.check_resource(work[2]),
+                    'daily': self.check_resource(work[3]),
+                    'amount': self.check_resource(work[4]),
+                    'ship': self.check_resource(work[5]),
+                    'pmax': self.check_resource(work[6])
+                }                
+                self.last_seen['Fig'] = True
+            else:
+                if self.last_seen['Ore'] == True and self.last_seen['Org'] == True and self.last_seen['Equ'] == True and self.last_seen['Fig'] == True:
+                    if self.fig['daily'] > self.max_product:
+                        self.max_product = self.fig['daily']
+                    
+                    self.planets_list[self.target_number]['Ore'] = self.ore
+                    self.planets_list[self.target_number]['Org'] = self.org
+                    self.planets_list[self.target_number]['Equ'] = self.equ
+                    self.planets_list[self.target_number]['Fig'] = self.fig
+                    log.debug("Max Production is now {0}!".format(self.max_product))
+
+                    if self.fig['daily'] < self.max_product:
+                        # Oh nose, our change actually made things worse... let's fix that and try a different way
+                        if self.trying == 'Ore2Org':
+                            self.trying = 'Org2Ore'
+                        elif self.trying == 'Ore2Equ':
+                            self.trying = 'Equ2Ore'
+                        elif self.trying == 'Org2Equ':
+                            self.trying = 'Equ2Org'
+                        self.state = 6
+
+                    if self.ore['pop'] < 1 or self.org['pop'] < 1 or self.equ['pop'] < 1:
+                        self.send2game('Q')
+                        self.send2player(self.nl + Boxes.alert('Peak Fighter Production: {0}'.format(self.fig['daily'])))
+                        self.deactivate(True)
+        
 
 
 class ProxyMenu(object):
@@ -3973,6 +4375,7 @@ class ProxyMenu(object):
         menu_item("4", "Upgrade Planet")
         # menu_item("5", "Colonize Planet")
         menu_item("5", "Colonize Planet v2.0")
+        menu_item("%", "Maxumize Fighter Production")
         menu_item("X", "eXit")
         self.queue_game.put(box.bottom())
         self.queue_game.put("   " + c1 + "-=>" + self.r + " ")
@@ -4049,8 +4452,15 @@ class ProxyMenu(object):
             d.addCallback(self.deactivate_scripts_menu)
             d.addErrback(self.deactivate_scripts_menu)
             return
-        elif key == "X":
+        elif key == '%':
             self.queue_game.put(self.c + key + self.r + self.nl)
+            maxfigs = MaxFighterMake(self.game)
+            d = maxfigs.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()
             return
         else: