-
Notifications
You must be signed in to change notification settings - Fork 1
/
playergen.py
136 lines (118 loc) · 4.29 KB
/
playergen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Provides the Player class and functions for random
player generation, to be used in a future settings module."""
from collections import namedtuple
import random
import army
import cubic
import camera
ACTIONS_PER_TURN = 5
ColorRGB = namedtuple("ColorRGB", ["r", "g", "b"])
RED = ColorRGB(255, 0, 0)
GREEN = ColorRGB(0, 255, 0)
BLUE = ColorRGB(0, 0, 255)
MAGENTA = ColorRGB(255, 0, 255)
class Player:
"""The player class.
Players interact with the game by clicking on tiles and issuing
commands to the corresponding armies."""
def __init__(self, game=None, name="None", color=None, ai=True):
self.game = game
self.camera = None
self.name = name
self.ai = ai
self.actions = ACTIONS_PER_TURN
self.selection = None
self.starting_cube = None
self.color = color
self.is_defeated = False
def __str__(self):
return self.name
def calc_army_growth(self):
"""Calculates the army growth rate based on tiles controlled."""
growth = 0
for tile in self.game.world.values():
if tile.owner == self:
growth += 1
return growth
def click_on_tile(self, tilepair):
"""
Clicking on a tile with an army selects it. If the player
already has a selection, it will issue a command with the
clicked tile as the target of the command. If no command can
be issued, and the clicked tile has an army, it will be
selected. If it does not, the player's selection will be set
to None. Clicking on the selected tile deselects it.
"""
clicked_cube, clicked_tile = tilepair
is_clicked_tile_selectable = (
clicked_tile.army
and clicked_tile.owner == self.game.current_player
and clicked_tile.army.can_move
and tilepair != self.selection
)
if self.selection:
legal_moves = cubic.get_reachable_cubes(self.game.world, self.selection[0], army.MAX_TRAVEL_DISTANCE)
if clicked_cube in legal_moves:
army.issue_order(self.game.world, self.selection, tilepair)
self.game.check_victory_condition()
self.selection = None # deselect
elif is_clicked_tile_selectable:
self.selection = tilepair
else: self.selection = None
elif is_clicked_tile_selectable:
self.selection = tilepair
else:
self.selection = None
def skip_turn(self):
self.actions = 0
# if (tile.owner == None):
# color = 0xFFAAAAAA
# elif (tile.owner.name == "Redosia"):
# color = 0x660000FF
# elif (tile.owner.name == "Bluegaria"):
# color = 0x66d0e040
# elif (tile.owner.name == "Greenland"):
# color = 0x6600FF00
# elif (tile.owner.name == "Violetnam"):
# color = 0x66800080
def set_color(tile):
if tile.owner is None:
color = ColorRGB(255, 255, 255)
else:
color = tile.owner.color
return color
def random_color():
levels = range(0, 256)
color = ColorRGB(random.choice(levels), random.choice(levels), random.choice(levels))
return color
def random_player(game):
player = Player(game, "unnamed player", random_color())
return player
def classic(game):
"""Hex Empire 1 players."""
players = []
players.append(Player(game, "Redosia", ColorRGB(255, 102, 102), ai=False))
players.append(Player(game, "Bluegaria", ColorRGB(51, 204, 204)))
players.append(Player(game, "Greenland", ColorRGB(102, 255, 102)))
players.append(Player(game, "Violetnam", ColorRGB(128, 0, 128)))
return players
def maxdist(game, number_of_players):
"""Spawn players maximum distance apart."""
# Generate random players
players = []
for _ in range(number_of_players):
players.append(random_player(game))
# Find n points maximum distance apart
#
# Create a bounding box
max_q, max_r, max_s = 0, 0, 0
for cube in game.world:
max_q = max(max_q, cube.q)
max_r = max(max_r, cube.q)
max_s = max(max_s, cube.q)
min_q = min(min_q, cube.q)
min_r = min(min_r, cube.r)
min_s = min(min_s, cube.s)
def create_player_cameras(game):
for player in game.players:
player.camera = camera.Camera(game.initial_layout, game.world)