summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2014-04-07 17:52:56 +1000
committerPeter Ward <peteraward@gmail.com>2014-04-07 17:52:56 +1000
commite62702bfa94ed70b8102006e63eb39805e5e8c89 (patch)
treef619ab23af6c3883aeb1f99e7b0e930828fae5a5
parent0b49012756c4e68fd0fa7d44907483b0b0932e88 (diff)
factories produce robots
This is kind of broken until combat is implemented.
-rw-r--r--robots/game.py18
-rw-r--r--robots/state.py9
2 files changed, 27 insertions, 0 deletions
diff --git a/robots/game.py b/robots/game.py
index 733068c..827562a 100644
--- a/robots/game.py
+++ b/robots/game.py
@@ -1,6 +1,7 @@
from collections import defaultdict, deque
from contextlib import contextmanager
from copy import deepcopy
+from random import random
from robots.constants import Action, City, DIRECTIONS, Energy, ENERGY_BY_ACTION
from robots.state import GameState
@@ -144,6 +145,22 @@ class Game:
for name, robots in self.state.robots_by_player.items():
robots[:] = robots_by_player[name]
+ def factories_produce_robots(self):
+ state_robots = self.state.robots
+
+ for x, y in self.state.factories:
+ if state_robots[(x, y)]:
+ continue
+
+ owner = self.state.allegiances.get((x, y))
+ if owner is None:
+ continue
+
+ if random() < 0.2:
+ self.state.robots_by_player[owner].append(
+ (x, y, Energy.INITIAL)
+ )
+
def robots_gain_energy(self):
for robots in self.state.robots_by_player.values():
robots[:] = [
@@ -163,6 +180,7 @@ class Game:
# 2. They perform the requested commands.
self.apply_actions(actions)
# 3. Factories produce new robots.
+ self.factories_produce_robots()
# 4. Combats are resolved.
# 5. Each robot gains energy, up to the maximum.
self.robots_gain_energy()
diff --git a/robots/state.py b/robots/state.py
index 1fe9c26..5a1450d 100644
--- a/robots/state.py
+++ b/robots/state.py
@@ -76,6 +76,15 @@ class GameState:
return len(self.cities)
@property
+ def factories(self):
+ return {
+ (x, y)
+ for y, row in enumerate(self.cities)
+ for x, city in enumerate(row)
+ if city == City.FACTORY
+ }
+
+ @property
def n_alive_players(self):
"""How many players are still alive."""
return sum(