parameters: (self, campaign)
return_statement: return len(newunits)
docstring: We assume each unit, in mainline and all addons, has one unique id. So
          we reference them everywhere by this id, and here can add them all to
          one big collection.
docstring_summary: We assume each unit, in mainline and all addons, has one unique id. So
          we reference them everywhere by this id, and here can add them all to
          one big collection.
docstring_tokens: We assume each unit in mainline and all addons has one unique id . So we reference them everywhere by this id and here can add them all to one big collection .
function: def add_units(self, campaign):
          """
          We assume each unit, in mainline and all addons, has one unique id. So
          we reference them everywhere by this id, and here can add them all to
          one big collection.
          """
          addunits = self.parser.get_all(tag="units")
          addunits += self.parser.get_all(tag="+units")
          if not addunits:
              return 0
  
          def getall(oftype):
              res = []
              for units in addunits:
                  res += units.get_all(tag=oftype)
              return res
  
          # Find all unit types.
          newunits = getall("unit_type") + getall("unit")
          for unit in newunits:
              uid = unit.get_text_val("id")
              unit.id = uid
  
              if unit.get_text_val("do_not_list", "no") not in ["no", "false"] or \
                 unit.get_text_val("hide_help", "no") not in ["no", "false"]:
                  unit.hidden = True
              else:
                  unit.hidden = False
  
              if uid in self.unit_lookup:
                  unit = self.unit_lookup[uid]
                  # TODO: We might want to compare the two units
                  # with the same id and if one is different try
                  # to do something clever like rename it...
              else:
                  self.unit_lookup[uid] = unit
              if not hasattr(unit, "campaigns"):
                  unit.campaigns = []
              unit.campaigns.append(campaign)
  
          # Find all races.
          newraces = getall("race")
          for race in newraces:
              rid = race.get_text_val("id")
              if rid is None:
                  rid = race.get_text_val("name")
              self.race_lookup[rid] = race
  
          # Find all movetypes.
          newmovetypes = getall("movetype")
          for movetype in newmovetypes:
              mtname = movetype.get_text_val("name")
              self.movetype_lookup[mtname] = movetype
  
          # Store race/movetype/faction of each unit for easier access later.
          for unit in newunits:
              uid = unit.get_text_val("id")
              race = self.get_unit_value(unit, "race")
              try:
                  unit.race = self.race_lookup[race]
                  unit.rid = unit.race.get_text_val("id", "none")
              except KeyError:
                  unit.race = None
                  unit.rid = "none"
                  error_message("Warning: No race \"%s\" found (%s).\n" % (
                      race, unit.get_text_val("id")))
              movetype = self.get_unit_value(unit, "movement_type")
              try:
                  unit.movetype = self.movetype_lookup[movetype]
              except KeyError:
                  unit.movetype = None
  
              unit.advance = []
              advanceto = unit.get_text_val("advances_to")
              # Add backwards compatibility for 1.4
              if not advanceto:
                  advanceto = unit.get_text_val("advanceto")
              if advanceto and advanceto != "null":
                  for advance in advanceto.split(","):
                      auid = advance.strip()
                      if auid: unit.advance.append(auid)
              # level
              try:
                  level = int(self.get_unit_value(unit, "level"))
              except TypeError:
                  level = 0
              except ValueError:
                  level = 0
              if level < 0: level = 0
              unit.level = level
  
          return len(newunits)
function_tokens: def add_units ( self , campaign ) : addunits = self . parser . get_all ( tag = "units" ) addunits += self . parser . get_all ( tag = "+units" ) if not addunits : return 0 def getall ( oftype ) : res = [ ] for units in addunits : res += units . get_all ( tag = oftype ) return res # Find all unit types. newunits = getall ( "unit_type" ) + getall ( "unit" ) for unit in newunits : uid = unit . get_text_val ( "id" ) unit . id = uid if unit . get_text_val ( "do_not_list" , "no" ) not in [ "no" , "false" ] or unit . get_text_val ( "hide_help" , "no" ) not in [ "no" , "false" ] : unit . hidden = True else : unit . hidden = False if uid in self . unit_lookup : unit = self . unit_lookup [ uid ] # TODO: We might want to compare the two units # with the same id and if one is different try # to do something clever like rename it... else : self . unit_lookup [ uid ] = unit if not hasattr ( unit , "campaigns" ) : unit . campaigns = [ ] unit . campaigns . append ( campaign ) # Find all races. newraces = getall ( "race" ) for race in newraces : rid = race . get_text_val ( "id" ) if rid is None : rid = race . get_text_val ( "name" ) self . race_lookup [ rid ] = race # Find all movetypes. newmovetypes = getall ( "movetype" ) for movetype in newmovetypes : mtname = movetype . get_text_val ( "name" ) self . movetype_lookup [ mtname ] = movetype # Store race/movetype/faction of each unit for easier access later. for unit in newunits : uid = unit . get_text_val ( "id" ) race = self . get_unit_value ( unit , "race" ) try : unit . race = self . race_lookup [ race ] unit . rid = unit . race . get_text_val ( "id" , "none" ) except KeyError : unit . race = None unit . rid = "none" error_message ( "Warning: No race \"%s\" found (%s).\n" % ( race , unit . get_text_val ( "id" ) ) ) movetype = self . get_unit_value ( unit , "movement_type" ) try : unit . movetype = self . movetype_lookup [ movetype ] except KeyError : unit . movetype = None unit . advance = [ ] advanceto = unit . get_text_val ( "advances_to" ) # Add backwards compatibility for 1.4 if not advanceto : advanceto = unit . get_text_val ( "advanceto" ) if advanceto and advanceto != "null" : for advance in advanceto . split ( "," ) : auid = advance . strip ( ) if auid : unit . advance . append ( auid ) # level try : level = int ( self . get_unit_value ( unit , "level" ) ) except TypeError : level = 0 except ValueError : level = 0 if level < 0 : level = 0 unit . level = level return len ( newunits )