"""The library used by bots to sense and to activate. All names in this module are exported and available to the bots. """ import math ; _math = math ; del math # constants for objects in the world ROBOT = 'robot' WALL = 'wall' OBSTACLE = 'obstacle' ### expose status values (speed, position, damage, etc) through an object ### that has a __getattr__ on it? def GetPosition(bot): """GetPosition(bot): return bot's position as an (x,y) tuple.""" return bot.x, bot.y def GetHeading(bot): """GetHeading(bot): return current heading in degrees.""" return bot.heading def GetHP(bot): """GetHP(bot): return bot's current hit point value.""" return bot.hp def GetTurretAngle(bot): """GetTurretAngle(bot): return the current turret angle in degrees.""" return bot.turretAngle def GetSpeed(bot): """GetSpeed(bot): return current speed.""" return _math.sqrt(bot.dx ** 2 + bot.dy ** 2) def Radar(bot): """Radar(bot): return (distance,type) of nearest object in current turret direction.""" return bot.Radar() def Turn(bot, reldir): """Turn(bot,reldir): change heading by +reldir degrees.""" ### instant turn bot.heading = bot.heading + reldir ### turn on a dime speed = GetSpeed(bot) rad = bot.heading * _mathpi / 180 bot.dx = speed * _math.cos(rad) bot.dy = speed * _math.sin(rad) def TurnTurret(bot, reldir): """TurnTurret(bot,reldir): rotate weapons/sensor turret.""" ### instant turn bot.turretAngle = bot.turretAngle + reldir def SetSpeed(bot, speed): """SetSpeed(bot,speed): set desired speed (may take a little while to accelerate or decelerate.""" ### instant accel rad = bot.heading * _math.pi / 180 bot.dx = speed * _math.cos(rad) bot.dy = speed * _math.sin(rad) def Fire(bot, energy, weaponType=0): """Fire a weapon, putting the specified amount of energy into it.""" bot.Fire() class _Status: attrs = { 'position' : GetPosition, 'heading' : GetHeading, 'hp' : GetHP, 'turretAngle' : GetTurretAngle, 'speed' : GetSpeed, } def __init__(self, bot): self.__dict__['bot'] = bot def __getattr__(self, name): try: return self.attrs[name](self.bot) except KeyError: raise AttributeError, name def __setattr__(self, name, value): raise AttributeError, name