def new_session(mapgen=create_map, rng_seed=RANDOM_SEED, human_player=True, ai_players=0):
	"""
	Create a new session with a map, add one human player and a trader (it will crash
	otherwise). It returns both session and player to avoid making the function-based
	tests too verbose.
	"""
	session = SPTestSession(rng_seed=rng_seed)
	human_difficulty = DifficultySettings.DEFAULT_LEVEL
	ai_difficulty = DifficultySettings.EASY_LEVEL

	players = []
	if human_player:
		players.append({
			'id': 1,
			'name': 'foobar',
			'color': Color.get(1),
			'local': True,
			'ai': False,
			'difficulty': human_difficulty,
		})

	for i in range(ai_players):
		id = i + human_player + 1
		players.append({
			'id': id,
			'name': ('AI' + str(i)),
			'color': Color.get(id),
			'local': (id == 1),
			'ai': True,
			'difficulty': ai_difficulty,
		})

	session.load(mapgen(), players, ai_players > 0, True)
	return session, session.world.player
Beispiel #2
0
    def _get_player_list(self):
        if self._player_list is not None:
            return self._player_list

        # for now just make it a bit easier for the AI
        difficulty_level = {
            False: DifficultySettings.DEFAULT_LEVEL,
            True: DifficultySettings.EASY_LEVEL
        }

        players = []
        players.append({
            'id':
            1,
            'name':
            self.player_name,
            'color':
            Color.get(1) if self.player_color is None else self.player_color,
            'local':
            True,
            'ai':
            self.human_ai,
            'difficulty':
            difficulty_level[bool(self.human_ai)],
        })

        cur_locale = horizons.globals.fife.get_locale()

        # add AI players with a distinct color; if none can be found then use black
        for num in range(self.ai_players):
            color = Color.get(
                COLORS.BLACK)  # if none can be found then be black
            for possible_color in Color.get_defaults():
                if possible_color == Color.get(COLORS.BLACK):
                    continue  # black is used by the trader and the pirate
                used = any(possible_color == player['color']
                           for player in players)
                if not used:
                    color = possible_color
                    break

            name = horizons.globals.db.get_random_ai_name(
                cur_locale, [p['name'] for p in players])
            # out of pre-defined names?
            if name is None:
                name = 'AI' + str(num + 1)

            players.append({
                'id': num + 2,
                'name': name,
                'color': color,
                'local': False,
                'ai': True,
                'difficulty': difficulty_level[True],
            })
        return players
Beispiel #3
0
	def set_color(self, color_id):
		"""Updates the background color of large label where players
		see their currently chosen color.
		@param color_id: int. Gets converted to util.Color object.
		"""
		try:
			self.selected_color = Color.get(color_id)
		except KeyError:
			# For some reason, color_id can be 0 apparently:
			# http://forum.unknown-horizons.org/viewtopic.php?t=6927
			# Reset that setting to 1 if the problem occurs.
			self.selected_color = Color.get(1)
		self.gui.findChild(name='selectedcolor').background_color = self.selected_color
def setup_combat(s, ship):
    worldid = 10000000

    p0 = Player(s, worldid, "p1", Color.get(1))
    p1 = Player(s, worldid + 1, "p2", Color.get(2))

    for p in (p0, p1):
        p.initialize(None)
        s.world.players.append(p)

    s0 = CreateUnit(p0.worldid, ship, 0, 0)(issuer=p0)
    s1 = CreateUnit(p1.worldid, ship, 3, 3)(issuer=p1)

    return ((p0, s0), (p1, s1))
Beispiel #5
0
def setup_combat(s, ship):
	worldid = 10000000

	p0 = Player(s, worldid, "p1", Color.get(1))
	p1 = Player(s, worldid+1, "p2", Color.get(2))

	for p in (p0, p1):
		p.initialize(None)
		s.world.players.append(p)

	s0 = CreateUnit(p0.worldid, ship, 0, 0)(issuer=p0)
	s1 = CreateUnit(p1.worldid, ship, 3, 3)(issuer=p1)

	return ((p0, s0), (p1, s1))
    def set_color(self, color_id):
        """Updates the background color of large label where players
		see their currently chosen color.
		@param color_id: int. Gets converted to util.Color object.
		"""
        try:
            self.selected_color = Color.get(color_id)
        except KeyError:
            # For some reason, color_id can be 0 apparently:
            # http://forum.unknown-horizons.org/viewtopic.php?t=6927
            # Reset that setting to 1 if the problem occurs.
            self.selected_color = Color.get(1)
        self.gui.findChild(
            name='selectedcolor').background_color = self.selected_color
    def _get_player_list(self):
        if self._player_list is not None:
            return self._player_list

            # for now just make it a bit easier for the AI
        difficulty_level = {False: DifficultySettings.DEFAULT_LEVEL, True: DifficultySettings.EASY_LEVEL}

        players = []
        players.append(
            {
                "id": 1,
                "name": self.player_name,
                "color": Color.get(1) if self.player_color is None else self.player_color,
                "local": True,
                "ai": self.human_ai,
                "difficulty": difficulty_level[bool(self.human_ai)],
            }
        )

        cur_locale = horizons.globals.fife.get_locale()

        # add AI players with a distinct color; if none can be found then use black
        for num in xrange(self.ai_players):
            color = Color.get(COLORS.BLACK)  # if none can be found then be black
            for possible_color in Color.get_defaults():
                if possible_color == Color.get(COLORS.BLACK):
                    continue  # black is used by the trader and the pirate
                used = any(possible_color == player["color"] for player in players)
                if not used:
                    color = possible_color
                    break

            name = horizons.globals.db.get_random_ai_name(cur_locale, [p["name"] for p in players])
            # out of pre-defined names?
            if name is None:
                name = "AI" + str(num + 1)

            players.append(
                {
                    "id": num + 2,
                    "name": name,
                    "color": color,
                    "local": False,
                    "ai": True,
                    "difficulty": difficulty_level[True],
                }
            )
        return players
Beispiel #8
0
 def get_player_list(self):
     ret_players = []
     for index, player in enumerate(self.players, start=1):
         # TODO: add support for selecting difficulty levels to the GUI
         status = T('Ready') if player.ready else T('Not Ready')
         ret_players.append({
             'id':
             index,
             'sid':
             player.sid,
             'name':
             player.name,
             'color':
             Color.get(player.color),
             'clientid':
             player.clientid,
             'local':
             self.netif.get_client_name() == player.name,
             'ai':
             False,
             'difficulty':
             DifficultySettings.DEFAULT_LEVEL,
             'status':
             status
         })
     return ret_players
Beispiel #9
0
	def _load(self, db, worldid):
		"""This function makes it possible to load playerdata into an already allocated
		Player instance, which is used e.g. in Trader.load"""
		super(Player, self).load(db, worldid)

		color, name, client_id, settlerlevel, difficulty_level, max_tier_notification = db(
			"SELECT color, name, client_id, settler_level, difficulty_level, max_tier_notification"
			" FROM player WHERE rowid = ?", worldid)[0]
		self.__init(name, Color.get(color), client_id, difficulty_level, max_tier_notification, settlerlevel = settlerlevel)
Beispiel #10
0
	def _load(self, db, worldid):
		"""This function makes it possible to load playerdata into an already allocated
		Player instance, which is used e.g. in Trader.load"""
		super(Player, self).load(db, worldid)

		color, name, client_id, settlerlevel, difficulty_level, max_tier_notification = db(
			"SELECT color, name, client_id, settler_level, difficulty_level, max_tier_notification"
			" FROM player WHERE rowid = ?", worldid)[0]
		self.__init(name, Color.get(color), client_id, difficulty_level, max_tier_notification, settlerlevel = settlerlevel)
Beispiel #11
0
def new_session(mapgen=create_map,
                rng_seed=RANDOM_SEED,
                human_player=True,
                ai_players=0):
    """
	Create a new session with a map, add one human player and a trader (it will crash
	otherwise). It returns both session and player to avoid making the function-based
	tests too verbose.
	"""
    session = SPTestSession(rng_seed=rng_seed)
    human_difficulty = DifficultySettings.DEFAULT_LEVEL
    ai_difficulty = DifficultySettings.EASY_LEVEL

    players = []
    if human_player:
        players.append({
            'id': 1,
            'name': 'foobar',
            'color': Color.get(1),
            'local': True,
            'ai': False,
            'difficulty': human_difficulty,
        })

    for i in xrange(ai_players):
        id = i + human_player + 1
        players.append({
            'id': id,
            'name': ('AI' + str(i)),
            'color': Color.get(id),
            'local': (id == 1),
            'ai': True,
            'difficulty': ai_difficulty,
        })

    session.load(mapgen(), players, ai_players > 0, True)
    return session, session.world.player
	def get_player_list(self):
		ret_players = []
		for index, player in enumerate(self.players, start=1):
			# TODO: add support for selecting difficulty levels to the GUI
			status = T('Ready') if player.ready else T('Not Ready')
			ret_players.append({
				'id':         index,
				'sid':        player.sid,
				'name':       player.name,
				'color':      Color.get(player.color),
				'clientid':   player.clientid,
				'local':      self.netif.get_client_name() == player.name,
				'ai':         False,
				'difficulty': DifficultySettings.DEFAULT_LEVEL,
				'status':     status
				})
		return ret_players
Beispiel #13
0
def test_indexing():
    assert Color.get(1) == Color(0, 0, 0, 255)
    assert Color.get('black') == Color(0, 0, 0, 255)
def test_indexing():
	assert Color.get(1) == Color(0, 0, 0, 255)
	assert Color.get('black') == Color(0, 0, 0, 255)
Beispiel #15
0
 def test_indexing(self):
     self.assertEqual(Color.get(1), Color(0, 0, 0, 255))
     self.assertEqual(Color.get('black'), Color(0, 0, 0, 255))