[Balder-cvs] balder2d/bin/scripts balder.py,NONE,1.1 balderai.py,NONE,1.1 balderaitest.py,NONE,1.1
Status: Beta
Brought to you by:
holomorph
From: Bjorn H. <hol...@us...> - 2006-03-11 09:30:38
|
Update of /cvsroot/balder/balder2d/bin/scripts In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12435/bin/scripts Added Files: balder.py balderai.py balderaitest.py Log Message: added python bindings to AI, and a simple AI script --- NEW FILE: balderaitest.py --- import unittest import balder import balderai # for reference; n*pi/4, n=0...8: # 0.0 # 0.785398163397 # 1.57079632679 # 2.35619449019 # 3.14159265359 # 3.92699081699 # 4.71238898038 # 5.49778714378 # 6.28318530718 class DoProbeControlTests(unittest.TestCase): def setUp(self): #lets surround the target at 4 points, 2 probes each point self.probe_dict = { 2:{'x':10,'y':10, 'rotation_angle':0.0}, # the target probe 1:{'x':10,'y':20, 'rotation_angle':4.71}, # ~3*pi/2should fire 3:{'x':0,'y':10, 'rotation_angle':1.57}, # ~ pi/2, needs to turn left 4:{'x':0,'y':10, 'rotation_angle':4.71}, # ~3*pi/2, needs to turn right 5:{'x':10,'y':0, 'rotation_angle':3.14}, # needs to turn left 6:{'x':10,'y':0, 'rotation_angle':0.0}, # ~pi, needs to turn right 7:{'x':20,'y':10, 'rotation_angle':4.71}, # ~3*pi/2, needs to turn left 8:{'x':20,'y':10, 'rotation_angle':1.57}, # ~pi/2, needs to turn right 9:{'x':10,'y':20, 'rotation_angle':0.0}, # ~needs to turn left 10:{'x':10,'y':20, 'rotation_angle':3.14}} # ~pi, needs to turn right def testFire(self): inputs = balderai.DoProbeControl(1, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_FIRE) def testOnLeftTurnLeft(self): inputs = balderai.DoProbeControl(3, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_LEFT) def testOnLeftTurnRight(self): inputs = balderai.DoProbeControl(4, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_RIGHT) def testAboveTurnLeft(self): inputs = balderai.DoProbeControl(5, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_LEFT) def testAboveTurnRight(self): inputs = balderai.DoProbeControl(6, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_RIGHT) def testOnRightTurnLeft(self): inputs = balderai.DoProbeControl(7, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_LEFT) def testOnRightTurnRight(self): inputs = balderai.DoProbeControl(8, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_RIGHT) def testBelowTurnLeft(self): inputs = balderai.DoProbeControl(9, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_LEFT) def testBelowTurnRight(self): inputs = balderai.DoProbeControl(10, self.probe_dict,[],[]) self.failUnlessEqual( inputs, balder.INPUT_RIGHT) questionSuite = unittest.makeSuite(DoProbeControlTests) #questionManagerSuite = unittest.makeSuite(QuestionManagerTests) testSuite = unittest.TestSuite((questionSuite)) unittest.TextTestRunner().run(testSuite) --- NEW FILE: balderai.py --- import balder import math framecount = 0 def DoProbeControl(probe_id, probe_dict, projectile_list, powerup_list): global framecount inputs = 0 if probe_id == 3: framecount += 1; # first, lets find out where we are and where we are aiming xloc = probe_dict[probe_id]['x'] yloc = probe_dict[probe_id]['y'] aiming = probe_dict[probe_id]['rotation_angle'] # lets all aim for probe number 2. . muahaha if not 2 in probe_dict: return 0 xtarget = probe_dict[2]['x'] ytarget = probe_dict[2]['y'] # now, figure out how we should be aiming, we need these to be floats # so that later divisions don't do integer division xaim = float(xtarget - xloc) yaim = float(ytarget - yloc) # if xaim is very small, we are going to get something very large if we try # to devide yaim by xaim. So in this case we just want to aim up or down if math.fabs(xaim) < 0.1: if yaim == math.fabs(yaim): # yaim is positive (y is positive down) want_to_aim = math.pi/2 # pi/2 is straight down else: want_to_aim = 3*math.pi/2 else: # it should be safe to compute the angle directly want_to_aim = math.atan(yaim/xaim) # this only gives us the right angle if x and y are positive though if xaim < 0: want_to_aim = want_to_aim + math.pi elif yaim < 0: # y negative, but x positive gives a negative angle, add 2*pi want_to_aim = want_to_aim + 2*math.pi # first, lets check if we are already pointed approximately that way if math.fabs(want_to_aim - aiming) < 0.1: inputs = balder.INPUT_FIRE else: # need to adjust our aim if want_to_aim > aiming: # make sure we turn the shortest way if want_to_aim - aiming < math.pi: inputs = balder.INPUT_RIGHT else: inputs = balder.INPUT_LEFT else: # again, make sure to turn the quickest way if aiming - want_to_aim > math.pi: inputs = balder.INPUT_RIGHT else: inputs = balder.INPUT_LEFT return inputs --- NEW FILE: balder.py --- INPUT_LEFT = 1; INPUT_RIGHT = 2; INPUT_STICK = 4; INPUT_PUSH = 8; INPUT_FIRE = 16; |