Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor maps settings into maps.default.

Update tests to work with global unit stats and new map settings
location.
  • Loading branch information...
commit 24b355aba3439e230d40e9c63c48ef56f849ce2d 1 parent 3578d79
@okayzed authored
View
6 main.py
@@ -1,5 +1,6 @@
#! /usr/bin/env python
import settings
+import maps.default as map_settings
import logging
log = logging.getLogger("MAIN")
@@ -286,7 +287,6 @@ def loadAIModules(ais, highlight=False):
return ai_classes
def loadMap(filename):
-
try:
log.info("Loading Map %s..." % (filename),)
split_ext = os.path.splitext(filename)
@@ -306,7 +306,7 @@ def loadMap(filename):
for attr in dir(mod):
if not attr.startswith("__"):
log.info("Setting: %s to %s", attr, getattr(mod, attr))
- setattr(settings, attr, getattr(mod,attr))
+ setattr(map_settings, attr, getattr(mod,attr))
except Exception, e:
log.info("Error loading %s, %s", filename, e)
@@ -323,7 +323,7 @@ def appengine_run_tournament(ai_files, argv_str, tournament_key):
if tournament_map:
use_map = "maps/%s" % tournament_map
else:
- use_map = None
+ use_map = "maps/micro.py"
if options.map:
use_map = options.map
View
0  maps/__init__.py
No changes.
View
21 maps/default.py
@@ -0,0 +1,21 @@
+# MAP MODIFIERS
+MAP_SIZE=100
+ADDITIONAL_BUILDINGS=0
+ADDITIONAL_BUILDINGS_PER_AI=0
+
+# BUILDING MODIFIERS
+CAPTURE_LENGTH=3
+UNIT_SPAWN_MOD=CAPTURE_LENGTH*10
+BUILDING_SPAWN_DISTANCE=5
+
+# STAT MODIFIERS
+ARMOR_MODIFIER = 1
+ATTACK_MODIFIER = 0.5
+ENERGY_MODIFIER = 10
+SIGHT_MODIFIER = 1
+SPEED_MODIFIER = 1
+BULLET_RANGE_MODIFIER = 8
+BULLET_SPEED_MODIFIER = 10
+
+
+
View
9 maps/micro.py
@@ -1,10 +1,17 @@
# MAP MODIFIERS
MAP_SIZE=100
+ADDITIONAL_BUILDINGS=0
+ADDITIONAL_BUILDINGS_PER_AI=0
+
# BUILDING MODIFIERS
CAPTURE_LENGTH=3
UNIT_SPAWN_MOD=CAPTURE_LENGTH*10
+
# STAT MODIFIERS
ARMOR_MODIFIER = 1
ATTACK_MODIFIER = 0.5
ENERGY_MODIFIER = 10
-SIGHT_MODIFIER = 0.8
+SIGHT_MODIFIER = 1
+SPEED_MODIFIER = 1
+BULLET_RANGE_MODIFIER = 8
+BULLET_SPEED_MODIFIER = 10
View
21 settings.py
@@ -2,25 +2,9 @@
# through code must also be defined here from the beginning,
# because this is a 'reset' file, as well.
-# MAP MODIFIERS
-MAP_SIZE=100
-MAP_NAME="default"
-ADDITIONAL_BUILDINGS=0
-ADDITIONAL_BUILDINGS_PER_AI=0
-
-# BUILDING MODIFIERS
-CAPTURE_LENGTH=3
-UNIT_SPAWN_MOD=CAPTURE_LENGTH*10
-
-# STAT MODIFIERS
-ARMOR_MODIFIER = 1
-ATTACK_MODIFIER = 0.5
-ENERGY_MODIFIER = 10
-SIGHT_MODIFIER = 1
-SPEED_MODIFIER = 1
-BULLET_RANGE_MODIFIER = 8
-BULLET_SPEED_MODIFIER = 10
+### ENGINE SETTINGS (SHOULDNT BE USABLE BY AI)
+MAP_NAME="maps/micro.py"
SINGLE_THREAD=False
IGNORE_EXCEPTIONS=False
PROFILE=False
@@ -29,7 +13,6 @@
# Set of AI classes to show map debugging information for.
SHOW_HIGHLIGHTS=set()
-BUILDING_SPAWN_DISTANCE=5
JS_REPLAY_FILENAME=None
JS_REPLAY_FILE=None
END_GAME_TURNS=100
View
16 test/security_test.py
@@ -2,6 +2,7 @@
sys.path.append(".")
import settings
+import maps.default as map_settings
import ai_exceptions
import mapobject
import ai
@@ -10,6 +11,8 @@
import unittest
import world
import worldtalker
+import copy
+from collections import defaultdict
class SecurityWorld(world.World):
@@ -21,6 +24,12 @@ def calcVisibility(self):
at = getattr(self, '_World__calcVisibility')
return at()
+ def Stats(self, *args, **kwargs):
+ s = copy.copy(self.unit_stats)
+ for k in kwargs:
+ setattr(s, k, kwargs[k])
+ return s
+
class SecurityAI(ai.AI):
# Tests that an AI can not manipulate units from another
# AI's team
@@ -45,12 +54,12 @@ def setUp(self):
self.top_left = top_left
self.bottom_right = bottom_right
- s = world.Stats(ai_id=self.own_ai.ai_id,
+ s = self.w.Stats(ai_id=self.own_ai.ai_id,
team=self.own_ai.team)
s.ai = self.own_ai
self.own_unit = self.w.createUnit(s, top_left)
- s = world.Stats(ai_id=self.other_ai.ai_id, team=self.other_ai.team)
+ s = self.w.Stats(ai_id=self.other_ai.ai_id, team=self.other_ai.team)
s.ai = self.other_ai
self.other_unit = self.w.createUnit(s, bottom_right)
@@ -108,7 +117,6 @@ def do_test_action(self, action, true_comp=None, false_comp=None, args=[]):
# Verify own AI can move unit
ret = self.own_ai.do_unit_action(self.own_unit, action, *args)
nret = None
-# print "t", action, ret
if true_comp is not None:
self.assertTrue(true_comp(ret))
else:
@@ -176,7 +184,7 @@ def test_is_capturing(self):
self.w.map.placeObject(self.other_unit, self.bottom_right)
self.own_ai.do_unit_action(self.own_unit, "capture", self.own_b)
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.spawn_counters = defaultdict(lambda: 100)
self.w.Turn()
self.do_test_property("is_capturing")
View
45 test/world_test.py
@@ -1,7 +1,7 @@
import sys
sys.path.append(".")
-import settings
+import maps.default as map_settings
import ai_exceptions
import mapobject
import ai
@@ -10,6 +10,7 @@
import unittest
import world
import worldtalker
+import copy
from collections import defaultdict
@@ -24,6 +25,12 @@ def killUnit(self, unit):
at = getattr(self, '_World__unitCleanup')
return at(unit)
+ def Stats(self, *args, **kwargs):
+ s = copy.copy(self.unit_stats)
+ for k in kwargs:
+ setattr(s, k, kwargs[k])
+ return s
+
class SecurityAI(ai.AI):
# Tests that an AI can not manipulate units from anown
# AI's team
@@ -50,7 +57,7 @@ def setUp(self):
self.top_left = top_left
self.bottom_right = bottom_right
- s = world.Stats(ai_id=self.ai.ai_id,
+ s = self.w.Stats(ai_id=self.ai.ai_id,
team=self.ai.team)
s.ai = self.ai
self.unit = self.w.createUnit(s, top_left)
@@ -68,25 +75,25 @@ def setUp(self):
# Test that melee kills a unit on the same square
def test_melee(self):
other_ai = SecurityAI(self.wt)
- s = world.Stats(ai_id=other_ai.ai_id,
+ s = self.w.Stats(ai_id=other_ai.ai_id,
team=other_ai.team)
s.ai = other_ai
other_unit = self.w.createUnit(s, self.top_left)
self.assertEqual(other_unit in self.w.units, True)
self.w.map.placeObject(self.unit, self.top_left)
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.currentTurn = map_settings.UNIT_SPAWN_MOD+1
self.w.createShootEvent(self.unit, self.top_left, 1)
self.w.Turn()
self.assertEqual(other_unit in self.w.units, False)
# Test that melee doesn't kill allies on the same square
def test_melee_no_friendly_fire(self):
- s = world.Stats(ai_id=self.ai.ai_id,
+ s = self.w.Stats(ai_id=self.ai.ai_id,
team=self.ai.team)
friendly_unit = self.w.createUnit(s, self.top_left)
self.assertEqual(friendly_unit in self.w.units, True)
self.w.map.placeObject(self.unit, self.top_left)
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.currentTurn = map_settings.UNIT_SPAWN_MOD+1
self.w.createShootEvent(self.unit, self.top_left, 1)
self.w.Turn()
self.assertEqual(friendly_unit in self.w.units, True)
@@ -97,7 +104,7 @@ def test_melee_kill_all_enemies(self):
for x in xrange(5):
other_ai = SecurityAI(self.wt)
- s = world.Stats(ai_id=other_ai.ai_id,
+ s = self.w.Stats(ai_id=other_ai.ai_id,
team=other_ai.team)
s.ai = other_ai
other_unit = self.w.createUnit(s, self.top_left)
@@ -106,7 +113,7 @@ def test_melee_kill_all_enemies(self):
for other_unit in enemies:
self.assertEqual(other_unit in self.w.units, True)
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.currentTurn = map_settings.UNIT_SPAWN_MOD+1
self.w.createShootEvent(self.unit, self.top_left, 1)
self.w.Turn()
for other_unit in enemies:
@@ -117,7 +124,7 @@ def test_melee_kill_all_enemies(self):
def test_shoot_deals_damage(self):
self.w.map.placeObject(self.unit, self.top_left)
other_ai = SecurityAI(self.wt)
- s = world.Stats(ai_id=other_ai.ai_id,
+ s = self.w.Stats(ai_id=other_ai.ai_id,
team=other_ai.team)
s.ai = other_ai
other_unit = self.w.createUnit(s, (5,5))
@@ -131,7 +138,7 @@ def test_shoot_deals_damage(self):
def test_shoot_hits_enemy_just_in_range(self):
self.w.map.placeObject(self.unit, self.top_left)
other_ai = SecurityAI(self.wt)
- s = world.Stats(ai_id=other_ai.ai_id,
+ s = self.w.Stats(ai_id=other_ai.ai_id,
team=other_ai.team)
s.ai = other_ai
@@ -168,7 +175,7 @@ def test_shoot_hits_enemy_just_in_range(self):
def test_shoot_does_not_hit_distant_enemy(self):
self.w.map.placeObject(self.unit, self.top_left)
other_ai = SecurityAI(self.wt)
- s = world.Stats(ai_id=other_ai.ai_id,
+ s = self.w.Stats(ai_id=other_ai.ai_id,
team=other_ai.team)
s.ai = other_ai
other_unit = self.w.createUnit(s, self.bottom_right)
@@ -182,7 +189,7 @@ def test_shoot_does_not_hit_distant_enemy(self):
def test_shoot_can_kill_enemy(self):
self.w.map.placeObject(self.unit, self.top_left)
other_ai = SecurityAI(self.wt)
- s = world.Stats(ai_id=other_ai.ai_id,
+ s = self.w.Stats(ai_id=other_ai.ai_id,
team=other_ai.team)
s.ai = other_ai
other_unit = self.w.createUnit(s, (5,5))
@@ -202,7 +209,7 @@ def test_shoot_can_kill_enemy(self):
def test_move(self):
# Test the move event
self.w.map.placeObject(self.unit, self.bottom_right)
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.currentTurn = map_settings.UNIT_SPAWN_MOD+1
self.w.createMoveEvent(self.unit, self.top_left)
self.assertEqual(self.w.map.getPosition(self.unit), self.bottom_right)
while True:
@@ -220,10 +227,10 @@ def test_capture_simple(self):
self.w.map.placeObject(self.unit, self.bottom_right)
self.w.buildings[self.b] = None
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.currentTurn = map_settings.UNIT_SPAWN_MOD+1
self.w.createCaptureEvent(self.unit, self.b)
self.assertNotEqual(self.w.buildings[self.b], self.ai)
- for i in xrange(settings.CAPTURE_LENGTH):
+ for i in xrange(map_settings.CAPTURE_LENGTH):
self.w.Turn()
self.assertEqual(self.w.buildings[self.b], self.ai)
@@ -233,10 +240,10 @@ def test_capture_ended_by_death(self):
self.w.map.placeObject(self.unit, self.bottom_right)
self.w.buildings[self.b] = None
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.currentTurn = map_settings.UNIT_SPAWN_MOD+1
self.w.createCaptureEvent(self.unit, self.b)
self.assertNotEqual(self.w.buildings[self.b], self.ai)
- for i in xrange(settings.CAPTURE_LENGTH-1):
+ for i in xrange(map_settings.CAPTURE_LENGTH-1):
self.w.Turn()
self.w.killUnit(self.unit)
@@ -251,11 +258,11 @@ def test_capture_twice(self):
self.w.createCaptureEvent(self.unit, self.b)
self.w.Turn()
- self.w.currentTurn = settings.UNIT_SPAWN_MOD+1
+ self.w.currentTurn = map_settings.UNIT_SPAWN_MOD+1
self.w.createCaptureEvent(self.unit, self.b)
self.assertNotEqual(self.w.buildings[self.b], self.ai)
- for i in xrange(settings.CAPTURE_LENGTH-1):
+ for i in xrange(map_settings.CAPTURE_LENGTH-1):
self.assertNotEqual(self.w.buildings[self.b], self.ai)
self.w.Turn()
View
37 world.py
@@ -8,6 +8,7 @@
import settings
import tuct
from unit import Unit
+import maps.default as map_settings
import copy
import itertools
@@ -139,7 +140,7 @@ def __init__(self, armor=1, attack=1, energy=1, sight=1, speed=1, team=None, ai_
class World:
def __init__(self, mapsize=None):
if not mapsize:
- mapsize = settings.MAP_SIZE
+ mapsize = map_settings.MAP_SIZE
self.wt = worldtalker.WorldTalker(self)
self.AI = []
@@ -170,8 +171,8 @@ def __init__(self, mapsize=None):
self.buildings = {}
# These contain the amount of time left for a building to spawn a unit
self.spawn_counters = defaultdict(int)
- log.info('Adding %s buildings to map', settings.ADDITIONAL_BUILDINGS)
- for i in xrange(settings.ADDITIONAL_BUILDINGS):
+ log.info('Adding %s buildings to map', map_settings.ADDITIONAL_BUILDINGS)
+ for i in xrange(map_settings.ADDITIONAL_BUILDINGS):
self.buildings[self.placeRandomBuilding()] = None
@@ -187,8 +188,8 @@ def __init__(self, mapsize=None):
self.dead_units = {}
self.oldbullets = []
self.bullet_endings = defaultdict(bool)
- self.bulletRange = self.mapSize/settings.BULLET_RANGE_MODIFIER
- self.bulletSpeed = self.mapSize/settings.BULLET_SPEED_MODIFIER
+ self.bulletRange = self.mapSize/map_settings.BULLET_RANGE_MODIFIER
+ self.bulletSpeed = self.mapSize/map_settings.BULLET_SPEED_MODIFIER
self.__initStats()
self.visibleunits = defaultdict(set)
@@ -198,11 +199,11 @@ def __init__(self, mapsize=None):
def __initStats(self):
stats = Stats(**DEFAULT_UNIT_STATS)
- stats.armor = stats.armor * settings.ARMOR_MODIFIER
- stats.energy = stats.energy * settings.ENERGY_MODIFIER
- stats.attack = stats.attack * settings.ATTACK_MODIFIER * math.log(self.mapSize)
- stats.sight = int((stats.sight * self.bulletRange) * settings.SIGHT_MODIFIER)
- stats.speed = int(stats.speed * (settings.SPEED_MODIFIER * math.log(settings.MAP_SIZE)))
+ stats.armor = stats.armor * map_settings.ARMOR_MODIFIER
+ stats.energy = stats.energy * map_settings.ENERGY_MODIFIER
+ stats.attack = stats.attack * map_settings.ATTACK_MODIFIER * math.log(self.mapSize)
+ stats.sight = int((stats.sight * self.bulletRange) * map_settings.SIGHT_MODIFIER)
+ stats.speed = int(stats.speed * (map_settings.SPEED_MODIFIER * math.log(map_settings.MAP_SIZE)))
self.unit_stats = stats
@@ -236,7 +237,7 @@ def placeRandomBuilding(self):
pos = self.map.getPosition(building)
if building == b or not pos:
continue
- spawn_distance = settings.BUILDING_SPAWN_DISTANCE * math.log(self.mapSize)
+ spawn_distance = map_settings.BUILDING_SPAWN_DISTANCE * math.log(self.mapSize)
dist = calcDistance(pos, rand_square)
if dist < spawn_distance:
within_range_of_other_building = True
@@ -272,8 +273,8 @@ def addAI(self, ai_class):
b = self.placeRandomBuilding()
self.buildings[b] = self.ai_cycler.next()
- log.info("Adding %s new buildings for %s AI to map", settings.ADDITIONAL_BUILDINGS_PER_AI, ai_player)
- for n in xrange(settings.ADDITIONAL_BUILDINGS_PER_AI):
+ log.info("Adding %s new buildings for %s AI to map", map_settings.ADDITIONAL_BUILDINGS_PER_AI, ai_player)
+ for n in xrange(map_settings.ADDITIONAL_BUILDINGS_PER_AI):
b = self.placeRandomBuilding()
self.buildings[b] = None
@@ -420,7 +421,7 @@ def __handleCaptureEvent(self, event, garbage, to_queue):
building.building_id)
else:
# Reset the unit spawn timer
- self.spawn_counters[building] = settings.UNIT_SPAWN_MOD
+ self.spawn_counters[building] = map_settings.UNIT_SPAWN_MOD
if old_owner:
log.info("BUILDING: %s lost %s to %s",
@@ -487,7 +488,7 @@ def __spawnUnit(self, owner, square):
def __spawnUnits(self):
for b in self.buildings:
if self.spawn_counters[b] <= 0:
- self.spawn_counters[b] = settings.UNIT_SPAWN_MOD
+ self.spawn_counters[b] = map_settings.UNIT_SPAWN_MOD
log.info("Spawning Units for building %s" % b.building_id)
owner = self.buildings[b]
@@ -502,6 +503,9 @@ def __spawnUnits(self):
log.info("SCORES:")
scores = self.calcScores()
for t in scores:
+ if not t in self.team_map:
+ continue
+
log.info("%s\t%s", t, self.team_map[t].__class__.__name__)
for k in scores[t]:
log.info(" %s:\t%s", k, scores[t][k])
@@ -620,7 +624,6 @@ def __cleanupDead(self):
# This is not a validated creation, so it will always
# create the unit
def __createUnit(self, stats, square):
-
# modify the stats and copy them for our world.
stats = copy.copy(stats)
@@ -846,7 +849,7 @@ def createCaptureEvent(self, unit, building):
#I'm trying to check if the unit is inside the building
#we will also have to check if there are enemies inside
#the building, but I'm not sure how
- e = CaptureEvent(unit, building, settings.CAPTURE_LENGTH)
+ e = CaptureEvent(unit, building, map_settings.CAPTURE_LENGTH)
self.__queueEvent(e)
else:
raise ai_exceptions.IllegalCaptureEvent("The unit is not in the building.")
Please sign in to comment.
Something went wrong with that request. Please try again.