示例#1
0
def trials_main():
   from optparse import OptionParser, OptionGroup
   description='''Conquest of Elysium 3 - Trial By Fire scenario map generator.
Default values for options given in parentheses.'''

   global options
   parser = OptionParser(description=description, version=options.version)
   parser.set_defaults(**default_options)
   parser.add_option("--debug",action="store_true",dest="debug",
         help="Really verbose output")
   parser.add_option("-d","--mapdir",dest="mapdir", help="Game map directory")
   parser.add_option("-r","--rungame",action="store_true",dest="rungame",
         help="Run game with the selected trial")
   parser.add_option("--nomap",action="store_false",dest="mapgen",
         help="Just show selected trial description")
   parser.add_option("--list",action="store_true",dest="list_trials",
         help="List trials titles")

   (options, args) = parser.parse_args()

   trials = get_ordered_trials()

   if options.list_trials:
      print_columns(2, [t['title'] for t in trials])
      return

   arg = None
   if len(args) > 0:
      arg = args[0]
   interactive = len(args) == 0
   doloop = True
   while doloop:
      if interactive:
         print_columns(2, [t['title'] for t in trials])
         try:
            arg = raw_input(format("Enter Trial [1-{0}]: ",len(trials)))
         except KeyboardInterrupt:
            print ''
            break
      else:
         doloop = False

      try:
         num = int(arg)
         if num < 0 or num > len(trials): raise IndexError
         doloop = False
      except:
         print format("\n*** Input must be a number between 1 and {0}\n",
               len(trials))
         continue

      trialgen(trials[num-1], options.mapdir, options.rungame)
示例#2
0
def trialgen(trial, mapdir, rungame):
   mapname = format("trial{0}",trial['order'])
   mapgen.options.filename = os.path.join(mapdir, format("{0}.coem",mapname)) 
   mapgen.options.debug = options.debug
   map_url = trial.get('options',{}).get('map url',None)
   if not map_url:
      mapdim = MAP_DIMS[trial['map_size']]

   if options.mapgen:
      if map_url:
         cmd = format("curl -o {0} {1}",mapgen.options.filename,
            trial['options']['map url'])
         print cmd
         os.system(cmd)
      else:
         mapgen.options.basic = True
         mapgen.options.mapwidth = mapdim[0]
         mapgen.options.mapheight = mapdim[1]
         mapgen.mapgen()

   title = format("Trial By Fire {0}: {1}",trial['order'], trial["title"])
   output = ['',title, '-'*len(title)] 
   if trial.has_key('quote'):
      for line in trial['quote'][0].split('\n'):
         output.append( line)
      output.append('\t- ' + trial['quote'][1])
      output.append('')

   output.extend(trial['desc'].split('\n'))

   mapfile = None
   if not map_url:
      output.append( format("Map Size: {0} ({1}x{2})",
            MAPSIZE[trial['map_size']],mapdim[0], mapdim[1]))
      output.append( format("Society: {0} ({1})",SOCIETY[trial["society"]], 
                  trial['society']))
      optmap = {True: 'On', False: 'Off'}
      for k,v in trial.get('options',{}).iteritems():
         output.append(format("{0}: {1}",k,optmap[v]))

      if options.mapgen:
         mapfile = open(mapgen.options.filename,'a')
         mapfile.write('\n')
         mapfile.write( format('mapdescr "{0}"\n',
           '^'.join(map(lambda s: s.replace("\n","^").replace('"',"'"),output)) 
            ))

      players = pick_players(trial['classes'],trial['levels'],
                         trial.get('teams',None),trial.get('sets',None))
      if len(players):
         output.append('')
         output.append('Player\tTeam\tAI\tClass')
      for i, c, l, t in players:         
         output.append( format("{0}\t{1}\t{2}\t{3}", i,t,AI[l],CLASS[c]))
         if mapfile:
            mapfile.write( format("fixedplayer {0} {1} {2} {3} {4}\n",
               i, c, max(t,0), min(l,1), l))
      if mapfile: mapfile.close()
   
   for line in output: print line

   if mapfile: 
      print "Map saved at:", mapgen.options.filename
      cmd_options = [ format("-p {0}",mapname), format("--loadmap={0}",mapname)]
      if trial.has_key('society'): 
         cmd_options.append( format("--society={0}",trial["society"]))
      if trial.has_key('options'):
         if trial['options'].get('Common cause',False) == True:
            cmd_options.append('--commoncause')
         if trial['options'].get('Cluster start',False) == True:
            cmd_options.append('--clusterstart')
      cmd = format("./run_coe3 {0}"," ".join(cmd_options))
      print cmd
      if rungame: os.system(cmd)