Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Organize the settings by object type, instead of GLOBALS

Not sure if the settings should directly correspond to map settings or
not, right now they correspond to unit distances based on the map size
and are namespaced by the object they are defining.

For example, in any loaded AI module, you can do:

print(settings.map.size)
print dir(settings)
print dir(settings.unit)
print settings.unit.speed
  • Loading branch information...
commit f80a1a8cf895dcf9fd21f16b6f678c3e493fcc7c 1 parent f2ab86c
okay authored
45 main.py
View
@@ -2,6 +2,9 @@
import settings
import maps.default as map_settings
+from mapobject import Bullet, Building
+import worldmap
+
import logging
log = logging.getLogger("MAIN")
@@ -179,15 +182,28 @@ def sub_module_require(module_name, rel_path=None):
# These are map settings that contain too much information
# about the map.
-BLACKLIST_SETTINGS = set([
- "adjustStatsForMap",
- "unit",
- "ADDITIONAL_BUILDINGS",
- "ADDITIONAL_BUILDINGS_PER_AI",
- "BUILDING_SPAWN_DISTANCE"])
+
+UNIT_WHITELIST_SETTINGS = set([
+ 'armor',
+ 'attack',
+ 'energy',
+ 'sight',
+ 'speed',
+ 'range'])
+
+MAP_WHITELIST_SETTINGS = set([
+ 'size'])
+
+BULLET_WHITELIST_SETTINGS = set([
+ 'speed',
+ 'range'])
+
+BUILDING_WHITELIST_SETTINGS = set([
+ 'capture_time',
+ 'spawn_time'])
class Settings(object):
- def __init__(self, module=None, dict=None):
+ def __init__(self, module=None, dict=None, whitelist=[]):
self.__attrs = {}
if module:
@@ -195,7 +211,7 @@ def __init__(self, module=None, dict=None):
if attr.startswith("__"):
continue
- if attr in BLACKLIST_SETTINGS:
+ if not attr in whitelist:
continue
val = getattr(module,attr)
@@ -233,9 +249,18 @@ def setupModule(module_name, filename, require_func=None, data=None):
# Turn the module into settings object (copies the attrs out)
+ # TODO: Could be that each object says what it's settings are, instead of
+ # gathering them here.
ai_local_settings = Settings(dict={
- "map" : Settings(module=map_settings),
- "unit" : Settings(world.Stats.adjustStatsForMap(map_settings))
+ "map" : Settings(dict=worldmap.Map.aiVisibleSettings(map_settings),
+ whitelist=MAP_WHITELIST_SETTINGS),
+ "unit" : Settings(module=world.Stats.aiVisibleSettings(map_settings),
+ whitelist=UNIT_WHITELIST_SETTINGS),
+ "bullet" : Settings(dict=Bullet.aiVisibleSettings(map_settings),
+ whitelist=BULLET_WHITELIST_SETTINGS),
+ "building" : Settings(dict=Building.aiVisibleSettings(map_settings),
+ whitelist=BUILDING_WHITELIST_SETTINGS)
+
})
dmangame_ai_builtins = {
15 mapobject.py
View
@@ -15,6 +15,12 @@ def building_id_generator():
ID_GENERATOR=building_id_generator()
class Building(MapObject):
+ @classmethod
+ def aiVisibleSettings(self, map_settings):
+ settings = { "capture_time" : map_settings.CAPTURE_LENGTH,
+ "spawn_time" : map_settings.UNIT_SPAWN_MOD }
+ return settings
+
def __init__(self, worldtalker):
self.__wt = worldtalker
self.__building_id = ID_GENERATOR.next()
@@ -34,6 +40,15 @@ def getPosition(self):
position = property(getPosition)
class Bullet(MapObject):
+ @classmethod
+ def aiVisibleSettings(self, map_settings):
+ bulletRange = map_settings.MAP_SIZE/map_settings.BULLET_RANGE_MODIFIER
+ bulletSpeed = map_settings.MAP_SIZE/map_settings.BULLET_SPEED_MODIFIER
+ settings = { "speed" : bulletSpeed,
+ "range" : bulletRange }
+ return settings
+
+
def __init__(self, unit, target):
self.__target = target
self.__unit = unit
4 unit.py
View
@@ -79,6 +79,10 @@ def energy(self):
return self.__wt.getStats(self).energy
@property
+ def range(self):
+ "The bullet range of the unit - the number of units distance a bullet fired by the unit would travel."
+ return self.__wt.getRange(self)
+ @property
def sight(self):
"The sight of the unit, use: sight as R of unit"
return self.__wt.getSight(self)
3  world.py
View
@@ -140,7 +140,7 @@ def __init__(self, armor=1, attack=1, energy=1, sight=1, speed=1, team=None, ai_
@classmethod
- def adjustStatsForMap(self, map_module):
+ def aiVisibleSettings(self, map_module):
stats = Stats(**DEFAULT_UNIT_STATS)
bulletRange = map_settings.MAP_SIZE/map_settings.BULLET_RANGE_MODIFIER
@@ -154,6 +154,7 @@ def adjustStatsForMap(self, map_module):
stats.speed = int(stats.speed * (map_module.SPEED_MODIFIER * math.log(map_module.MAP_SIZE)))
+
return stats
# The world is responsible for maintaining the world
4 worldmap.py
View
@@ -140,6 +140,10 @@ def draw_map(cairo_context, width, height, world_data, turn_data):
class Map:
+ @classmethod
+ def aiVisibleSettings(self, map_settings):
+ return { "size" : map_settings.MAP_SIZE }
+
def __init__(self, N):
self.size = N
self.objectMap = {}
4 worldtalker.py
View
@@ -94,10 +94,12 @@ def inRange(self, unit):
return units
# Get functions
-
def getBulletRange(self):
return self.__world.bulletRange
+ def getBulletSpeed(self):
+ return self.__world.bulletSpeed
+
def getCurrentTurn(self):
return self.__world.getLifeTime()
Please sign in to comment.
Something went wrong with that request. Please try again.