def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] self.commands = [] # Move forward for 3-8 seconds for idx, tank in enumerate(mytanks): move_timer = self.mytanks[idx].move_timer move_timer -= time_diff self.mytanks[idx].move_timer = move_timer if move_timer > 0: self.commands.append(Command(idx, 10, 0, 0)) else: # Turn left about 60 degrees and then start going straight again self.commands.append(Command(idx, 0, 3, 0)) move_timer = randint(3, 8) if int(move_timer) % 2 == 0: self.commands.append(Command(idx, 10, 0, 1)) # In addition to this movement your really dumb agent should also shoot every 2 seconds (random between 1.5 and 2.5 seconds) or so. # Once you have one tank doing this, create a team that has two such agents. # for tank in mytanks: # self.attack_enemies(tank) results = self.bzrc.do_commands(self.commands)
def lock_on(self, targetTank): agentTank = self.mytanks[self.agent_index] # print str(targetTank.x) + ' ' + str(targetTank.y) # print str(targetTank) Zt = array([[targetTank.x], [targetTank.y]]) self._kalman.updateKalmanFilter(Zt) est = self._kalman.H.dot(self._kalman.mu) self.updates.append(((int(est[0][0]), int(est[1][0])),self._kalman.sigmaT)) aimAngle,distance = self.take_aim((agentTank.x,agentTank.y), agentTank.angle) command = Command(0,0,aimAngle*2,True) if aimAngle < 1 and aimAngle > -1: if distance < 350: # print 'shooting' command = Command(self.agent_index,0,aimAngle*2,True) else: # print 'not shooting distance' command = Command(self.agent_index,0,aimAngle*2,False) else: # print 'not shooting aimAngle' command = Command(self.agent_index,0,aimAngle*2,False) self.commands.append(command) self.bzrc.do_commands(self.commands)
def tick(self, time_diff): mytanks, othertanks, flags, shots, obstacles, bases = self.bzrc.get_lots_o_stuff( ) """don't do anything, just sit there.""" for tank in mytanks: out_of_range, direction = outOfRange(tank) if tank.status == "dead": self.aliveTime = 0 self.prevTime = time_diff else: self.aliveTime = time_diff - self.prevTime if out_of_range and self.aliveTime < 10.0: target_angle = math.atan2(0 - tank.y, 0 - tank.x) relative_angle = normalize_angle(target_angle - tank.angle) if (abs(relative_angle) > .5): print ">" self.commands.append( Command(tank.index, 0, relative_angle * 2, False)) else: print "not" self.commands.append(Command(tank.index, .3, 0, False)) else: print "stopping" self.commands.append(Command(tank.index, 0, 0, False)) results = self.bzrc.do_commands(self.commands)
def kalman(self, tank): sensor = self.get_target_loc() if sensor != None: X = np.matrix([[sensor.x], [sensor.y]]) P_k = self.get_Pk() K = self.get_K(P_k) self.mu = self.F * self.mu + K * (X - self.H * self.F * self.mu) self.SIGMA_T = (self.I - K * self.H) * P_k current_shot_dist = self.get_target_dist(tank.x, tank.y, sensor.x, sensor.y) iterations = 0 if current_shot_dist <= 350: iterations = int(current_shot_dist) target_position = self.predict_future_position(iterations) #calculate angle delta_x, delta_y, magnitude = self.calculate_objective_delta( tank.x, tank.y, target_position[0, 0], target_position[3, 0]) turn_angle = math.atan2(delta_y, delta_x) relative_angle = self.normalize_angle(turn_angle - tank.angle) if abs(relative_angle) < 0.001: command = Command(tank.index, 0, 2 * relative_angle, False) else: command = Command(tank.index, 0, 2 * relative_angle, True) self.commands.append(command) else: self.victory_lap(tank)
def tick(self, time_diff): mytanks, othertanks, flags, shots, obstacles, bases = self.bzrc.get_lots_o_stuff( ) #---------------------MAIN LOGIC AREA------------------------ for tank in mytanks: out_of_range, direction = outOfRange(tank) if out_of_range: target_angle = math.atan2(0 - tank.y, 0 - tank.x) relative_angle = normalize_angle(target_angle - tank.angle) #if(time_diff%4 >= 0.0 and time_diff%4 <= 0.1): #print relative_angle if (abs(relative_angle) > .7): self.commands.append( Command(tank.index, .2, 2 * relative_angle, False)) else: self.commands.append(Command(tank.index, 1, 0, False)) else: # speed 1 for 5 seconds, turning one way. if time_diff % 10 < 5: self.commands = [] for x in range(0, len(mytanks)): self.commands.append(Command(x, 1, .5, False)) #speed .5 for 4 seconds, turning other way. elif time_diff % 10 < 9: self.commands = [] for x in range(0, len(mytanks)): self.commands.append(Command(x, .5, -1, False)) results = self.bzrc.do_commands(self.commands)
def do_dumb_stuff(self, tick_time): turn_speed = math.pi / 6 # 30 degrees per second if not self.tank_tracker: # Initialize for bot in self.mytanks: move_duration = random.random() * 5 + 3 turn_angle = math.radians(60 + random.random() * 10) turn_duration = turn_angle / turn_speed shoot_time = random.random() + 1.5 self.tank_tracker[bot.index] = [ move_duration, move_duration + turn_duration, shoot_time ] self.commands.append(Command(bot.index, 1, 0, 0)) for bot in self.mytanks: if bot.status == "alive": # Delay commands for dead tank until it respawns self.tank_tracker[bot.index][0] -= tick_time self.tank_tracker[bot.index][1] -= tick_time self.tank_tracker[bot.index][2] -= tick_time if self.tank_tracker[bot.index][0] < 0: self.commands.append(Command(bot.index, 0, turn_speed, 0)) self.tank_tracker[bot.index][0] = random.random( ) * 5 + 3 + self.tank_tracker[bot.index][1] if self.tank_tracker[bot.index][1] < 0: self.commands.append(Command(bot.index, 1, 0, 0)) self.tank_tracker[bot.index][1] = math.radians( 60 + random.random() * 10) / turn_speed + self.tank_tracker[bot.index][0] if self.tank_tracker[bot.index][2] < 0: self.commands.append(Command(bot.index, None, None, 1)) self.tank_tracker[bot.index][2] = random.random() + 1.5
def turn(self, tank): rand = random.random() # returns a number [0, 1) if rand < self.turndecisionprob: command = Command(tank.index, 1, 1, False) # turn right self.commands.append(command) else: command = Command(tank.index, 0.5, -1, False) # turn left self.commands.append(command)
def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots, obstacles, bases = self.bzrc.get_lots_o_stuff( ) self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.obstacles = obstacles self.bases = bases self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] #---------------------MAIN LOGIC AREA------------------------ #In addition to this movement your really dumb agent should also shoot every 2 seconds (random between 1.5 and 2.5 seconds) or so. shootCmd = time_diff % 2 < .1 #Move forward for 5 seconds if time_diff % 10 < 5: self.commands = [] for x in range(0, len(mytanks)): self.commands.append(Command(x, 1, 0, shootCmd)) #stop for 2 seconds elif time_diff % 10 < 7: self.commands = [] for x in range(0, len(mytanks)): self.commands.append(Command(x, 0, 0, shootCmd)) #rotate for 1.5 sec elif time_diff % 10 < 8.5: self.commands = [] for x in range(0, len(mytanks)): self.commands.append(Command(x, 0, 1, shootCmd)) else: self.commands = [] for x in range(0, len(mytanks)): self.commands.append(Command(x, 0, 0, shootCmd)) #stop for 2 seconds #else: # self.commands = [Command(1, -1, 0, shootCmd), Command(2, -1, 0, shootCmd)] #Reverse for 5 seconds #Move forward for 3-8 seconds #Turn left about 60 degrees and then start going straight again #In addition to this movement your really dumb agent should also shoot every 2 seconds (random between 1.5 and 2.5 seconds) or so. #A command has a tank, a speed, and angle, and a shoot command #self.commands = [Command(1, 1, .1, True), Command(2, 1, .1, True)] #for tank in mytanks: # self.attack_enemies(tank) results = self.bzrc.do_commands(self.commands)
def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] self.commands = [] self.shoottimecounter += time_diff self.turntimecounter += time_diff if self.turntimecounter >= 8.0: for tank in mytanks: #print "turning" if not self.angleset[tank.index]: print "setting new angle" self.startangle[tank.index] = tank.angle self.angleset[tank.index] = True self.turntimecounter = 0 for tank in mytanks: if self.angleset[tank.index] == True: if self.normalize_angle(tank.angle) <= self.normalize_angle( self.startangle[tank.index] + (math.pi / 3)): command = Command(tank.index, 1, math.pi, False) self.commands.append(command) else: print "stop turning!!" self.angleset[tank.index] = False command = Command(tank.index, 1, 0, False) else: command = Command(tank.index, 1.0, 0, False) self.commands.append(command) #self.turntimecounter = 0 # shoot every 2s if self.shoottimecounter > 2.0: print "firing!" for tank in mytanks: command = Command(tank.index, 0, 0, True) self.commands.append(command) self.shoottimecounter = 0 results = self.bzrc.do_commands(self.commands)
def tick(self): self.commands = [] flags = self.bzrc.get_flags() self.has_flag = False for flag in flags: if not str(flag.color) in str(self.ourCallsign): self.flag_goal = flag if str(flag.poss_color) in str(self.ourCallsign): self.has_flag = True mytanks = self.bzrc.get_mytanks() for tank in mytanks: x_force, y_force = self.get_forces_on_tank(tank) magnitude = math.sqrt(x_force**2 + y_force**2) self.targetAngle = math.atan2(y_force, x_force) # randomly shoot should_shoot = False if random.random() < .01: should_shoot = True command = Command(tank.index, magnitude, self.calculate_angvel(tank), should_shoot) self.commands.append(command) if self.commands: self.bzrc.do_commands(self.commands)
def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] self.commands = [] reached = False for tank in mytanks: if (self.dist(tank.x, tank.y, self.waiting_spot_x, self.waiting_spot_y) <= self.goal_sphere): reached = True if (reached == False): self.move_to_position(tank, self.waiting_spot_x, self.waiting_spot_y) else: command = Command(tank.index, 0, 0, False) self.commands.append(command) results = self.bzrc.do_commands(self.commands)
def __init__(self, bzrc): self.bzrc = bzrc self.commands = [] self.constants = self.bzrc.get_constants() self.mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.tank = self.mytanks[0] self.enemy = othertanks[0] self.firstTime = True self.goalAngle = 0 self.mu = np.matrix([[0], [0], [0], [0], [0], [0]]) self.sigma_t = kalman_args.sigma_t self.sigma_x = kalman_args.sigma_x self.H = np.matrix([[1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]]) self.sigma_z = np.matrix([[kalman_args.posnoise**2, 0], [0, kalman_args.posnoise**2]]) delta_t = kalman_args.delta_t self.F = np.matrix([[1, delta_t, delta_t**2 / 2, 0, 0, 0], [0, 1, delta_t, 0, 0, 0], [0, -kalman_args.c, 1, 0, 0, 0], [0, 0, 0, 1, delta_t, delta_t**2 / 2], [0, 0, 0, 0, 1, delta_t], [0, 0, 0, 0, -kalman_args.c, 1]]) command = Command(0, 0, 0, False) self.commands.append(command) results = self.bzrc.do_commands(self.commands)
def tick(self, time_diff): '''Some time has passed; decide what to do next''' # Get information from the BZRC server mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] # Reset my set of commands (we don't want to run old commands) self.commands = [] self.currentTank = mytanks[0] self.flag = flags[1] self.attractiveField() self.repulsiveFields() # self.tangentialFields() self.controller() position, occGrid = self.bzrc.get_occgrid(0) self.update(position, occGrid) # self.goalPos = self.chooseTarget() gf.update_grid(self.grid) gf.draw_grid() command = Command(0, 1, self.curAngVel, True) self.commands.append(command) results = self.bzrc.do_commands(self.commands)
def move_to_position(self, tank, target_x, target_y): """Set command to move to given coordinates.""" target_angle = math.atan2(target_y - tank.y, target_x - tank.x) relative_angle = self.normalize_angle(target_angle - tank.angle) # Don't want them to shoot so I set it to false command = Command(tank.index, 1, 2 * relative_angle, False) self.commands.append(command)
def tick(self): if(self.ticks < 10): pass if self.tank.status == "dead": return self.commands = [] curtime = time.time() curLocation = Point(self.tank.x, self.tank.y) target = self.locationList[self.tank.index] if curLocation.distance(target) < 10: target = self.getRandomCoordinate(self.tank.index) while self.grid.get(target.x, target.y) > .95: target = self.getRandomCoordinate(self.tank.index) self.locationList[self.tank.index] = target self.oldlocation[self.tank.index] = curLocation self.goToPoint(self.tank, target) if(curtime - self.time > 2.25 ): command = Command(self.tank.index,0,0,False) self.commands = [] self.commands.append(command) self.bzrc.do_commands(self.commands) self.commands = [] self.getObservation(self.tank) self.time = time.time()
def move_to_position(self, tank, target_x, target_y): """Set command to move to given coordinates.""" target_angle = tank.angle + 60 relative_angle = self.normalize_angle(target_angle - tank.angle) #print str(relative_angle) command = Command(tank.index, 0, 1, True) self.commands.append(command)
def move_to_position(self, tank, target_x, target_y): """Set command to move to given coordinates.""" target_angle = math.atan2(target_y - tank.y, target_x - tank.x) relative_angle = self.normalize_angle(target_angle - tank.angle) command = Command(tank.index, 1, 2 * relative_angle, True) self.commands.append(command)
def kalman(self, tank): target = self.get_target_loc(tank) X = np.matrix([target.x, target.y]) if target != None: P_k = self.get_Pk() K = self.get_K(P_k) self.mu = self.F * self.mu + K * (X - self.H * self.F * self.mu) self.SIGMA_T = (self.I - K * self.H) * P_k mu_x = self.mu[0, 0] mu_y = self.mu[3, 0] if self.num_ticks % self.DEBUGTICKS == 0: print 'target: x=', target.x, ", y=", target.y print 'mu:\n', self.mu #calculate angle delta_x, delta_y, magnitude = self.calculate_objective_delta( tank.x, tank.y, mu_x, mu_y) turn_angle = math.atan2(delta_y, delta_x) relative_angle = self.normalize_angle(turn_angle - tank.angle) command = Command(tank.index, 0, 2 * relative_angle, True) self.commands.append(command)
def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = [ flag for flag in flags if flag.color != self.constants['team'] ] self.shots = shots self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] self.obstacles = self.bzrc.get_obstacles() self.commands = [] if self.num_ticks % self.MAXTICKS == 0: for tank in mytanks: # make sure the velocity is between 0.5 and 1 magnitude = random.random() * 0.5 + 0.5 relative_angle = 0.5 command = Command(tank.index, magnitude, 2 * relative_angle, False) self.commands.append(command) results = self.bzrc.do_commands(self.commands) self.num_ticks = self.num_ticks + 1
def tick(self): self.commands = [] curtime = time.time() self.mytanks = self.bzrc.get_mytanks() for tank in self.mytanks: curLocation = Point(tank.x, tank.y) target = self.locationList[tank.index] if curLocation.distance(target) < 10: target = self.getRandomCoordinate(tank.index) while self.grid.get(target.x, target.y) > .95: target = self.getRandomCoordinate(tank.index) self.locationList[tank.index] = target self.oldlocation[tank.index] = curLocation self.goToPoint(tank, target) if(curtime - self.time > 4.25 ): for tank in self.mytanks: command = Command(tank.index,0,0,False) self.commands = [] self.commands.append(command) self.bzrc.do_commands(self.commands) self.commands = [] self.getObservation(tank) self.time = time.time()
def pf_move(self, tank, pf, pfo, pfe): final_angle = 0 if pfo != None: # print 'pfo != None' #print self.constants['team'] + " tank: %d = pfo" % tank.index speedmod, angle = pfo.calc_vector(tank.x, tank.y) elif pfe != None: # print 'pfe ! = None' #print self.constants['team'] + " tank: %d = pfe" % tank.index speedmod, angle = pfe.calc_vector(tank.x, tank.y) else: # print 'else' #print self.constants['team'] + " tank: %d = pf" % tank.index speedmod, angle = pf.calc_vector(tank.x, tank.y) angle = self.normalize_angle(angle - tank.angle) if final_angle == 0: final_angle = angle else: final_angle = (float(final_angle) + float(angle)) / 2.0 # current_tank_speed = math.sqrt(float(tank.vx**2) + float(tank.vy**2)) # print current_tank_speed #command = Command(tank.index, speedmod * current_tank_speed, 2 * final_angle, True) command = Command(tank.index, speedmod, 2 * final_angle, True) self.commands.append(command)
def sendCommand(self, totalX, totalY, tank): othertanks = self.bzrc.get_othertanks() me = Point(tank.x, tank.y) cur = 100000 mins = 100000 enemy = Point(0, 0) for other in othertanks: cur = Point(other.x, other.y).distance(me) if (cur < mins): mins = cur enemy = other if mins <= 200: self.doKalman(enemy.x, enemy.y) else: theta = math.atan2(totalY, totalX) theta = self.normalize_angle(theta - tank.angle) speed = math.sqrt(totalY**2 + totalX**2) self.commands = [] command = Command(tank.index, .15 * speed, .85 * theta, False) self.commands.append(command) self.bzrc.do_commands(self.commands) self.commands = []
def getCommandFromVectors(self, desiredVector, timeDiff): curTankAngle = self.getCurrentDirectionInPolar() #angleVel = desiredVector.angle - curTankVector.angle #intialize constants #Kp creates tight turns Kp = 1.0 Kd = 0.0 #magnitudeDiff = desiredVector.velocity - curVector.velocity #TODO how does this work with polar cooridantes or those returned by the potential_feild? Could we get strange behavoir with the angles overlapping angleDiff = desiredVector.angle - curTankAngle if abs(angleDiff + 2*pi) < abs(angleDiff): angleDiff = angleDiff + 2*pi elif abs(angleDiff - 2*pi) < abs(angleDiff): angleDiff = angleDiff - 2*pi #TODO is timeDiff an int? Will dividing by timeDiff work? #commandVector.velocity = Kp(velocityDiff) + Kd( velocityDiff - self.speed_error )/timeDiff #prevent a divide by zero error if timeDiff == 0: timeDiff = 0.01 angleVel = Kp*angleDiff + Kd* (angleDiff - self.prev_angle_error )/timeDiff #speed_error = velocityDiff prev_angle_error = angleDiff #TODO depending on how we get the angle and speed, convert these to a command if 1 < angleVel: angleVel = 1 elif angleVel < -1: angleVel = -1 """ index, speed, angle, shoot""" return Command(self.tank.index, 1, angleVel, True)
def move_to_position(self, tank, target_x, target_y): """Set command to move to given coordinates.""" #get deltas delta_xG, delta_yG, magnitude = self.calculate_objective_delta(tank.x, tank.y, target_x, target_y) #delta_xO, delta_yO = self.calculate_obstacles_delta(tank.x, tank.y) delta_xO, delta_yO = 0, 0 delta_xR, delta_yR = self.calculate_random_delta() delta_xA, delta_yA = self.calculate_enemies_delta(tank.x, tank.y, self.enemies) #combine delta_x = delta_xG + delta_xO + delta_xR + delta_xA delta_y = delta_yG + delta_yO + delta_yR + delta_xA #calculate angle turn_angle = math.atan2(delta_y, delta_x) relative_angle = self.normalize_angle(turn_angle - tank.angle) #put lower bound on speed: no slower than 40% if magnitude < 0.4: magnitude = 0.4 fire = self.should_fire(tank) command = Command(tank.index, magnitude, 2 * relative_angle, False) self.commands.append(command)
def goto_flags(self, tank): best_flag = self.get_best_flag(tank.x, tank.y) if best_flag is None: command = Command(tank.index, 0, 0, False) self.commands.append(command) else: self.move_to_position(tank, best_flag.x, best_flag.y)
def goto_closest_target(self, tank): best_tar = self.get_best_target(tank.x, tank.y) if best_tar is None: command = Command(tank.index, 0, 0, False) self.commands.append(command) else: self.move_to_position(tank, best_tar[0], best_tar[1])
def tick(self, time_diff): second = int(time_diff) if second == self.lastTimeUpdated: return self.lastTimeUpdated = second """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots, obstacles, bases = self.bzrc.get_lots_o_stuff( ) self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.obstacles = obstacles self.bases = bases self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] #---------------------MAIN LOGIC AREA------------------------ #set up pigeon directions if needed if len(self.pigeonDirections) == 0: for x in range(0, len(mytanks)): self.pigeonDirections.append(getRandomDirection()) self.pigeonSpeeds.append(getRandomSpeed()) self.commands = [] for i in range(0, len(mytanks)): #if the pigeon is aimed in the correct direction #print "angle" + str(mytanks[i].angle) + " direct:" + str(self.pigeonDirections[i]) if ((mytanks[i].angvel == 0 and mytanks[i].vx != 0) or abs(mytanks[i].angle - self.pigeonDirections[i]) < .5 or abs(mytanks[i].angle - self.pigeonDirections[i] + 2 * pi) < .5): self.commands.append(Command(i, self.pigeonSpeeds[i], 0, False)) #if the pigeon needs to be rotated into the proper direction else: #makes sure the speed is random self.pigeonSpeeds[i] = getRandomSpeed() angvel = .5 if self.pigeonDirections[i] > pi: angvel = -.5 self.commands.append(Command(i, 0, angvel, False)) results = self.bzrc.do_commands(self.commands)
def tick(self, gameClock): """Some time has passed; decide what to do next.""" self.commands = [] command = Command(0, 1, 0, False) self.commands.append(command) self.bzrc.do_commands(self.commands) self.prevTime = gameClock
def tick(self, time_diff, current_tank): self.seconds_since_last_move += time_diff self.seconds_since_last_shot += time_diff self.commands = [] if self.is_turning: if not self.angles_are_initialized: self.init_angles(current_tank) self.currentAngle = current_tank.angle if abs(current_tank.angle - self.targetAngle) < 0.1: # Stop turning self.is_turning = False self.angles_are_initialized = False self.seconds_since_last_move = 0 self.next_move_target_time = random.uniform(3, 8) command = Command(current_tank.index, 1, 0, False) else: # Continue turning angvel = self.calculate_angvel() command = Command(current_tank.index, 0, angvel, False) self.commands.append(command) self.lastAngle = self.currentAngle elif self.seconds_since_last_move <= self.next_move_target_time: # Move forward command = Command(current_tank.index, 1, 0, False) self.commands.append(command) else: # Stop moving forward and get ready to turn command = Command(current_tank.index, 0, 0, False) self.commands.append(command) self.is_turning = True if self.commands: self.bzrc.do_commands(self.commands) if self.seconds_since_last_shot > self.next_shot_target_time: # Fire off a shot self.bzrc.shoot(current_tank.index) self.seconds_since_last_shot = 0 self.next_shot_target_time = random.uniform(1.5, 2.5)
def move_to_position(self, tank, target_x, target_y, velocity, shoot): # target_x, target_y = 0, 0 """Set command to move to given coordinates.""" target_angle = math.atan2(target_y - tank.y, target_x - tank.x) relative_angle = self.normalize_angle(target_angle - tank.angle) command = Command(tank.index, velocity, 2 * relative_angle, shoot) # Don't shoot automatically self.commands.append(command)
def tick(self): """Some time has passed; decide what to do next.""" num_tanks = 2 mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() for i in range(num_tanks): tank = self.mytanks[i] tank.move_time_diff = time.time() - tank.last_move tank.shoot_time_diff = time.time() - tank.last_shoot tank.angle = mytanks[i].angle self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [tank for tank in othertanks if tank.color != self.constants['team']] self.commands = [] #move_time_diff = time.time() - self.last_move #shoot_time_diff = time.time() - self.last_shoot for i in range(num_tanks): tank = self.mytanks[i] command = Command(tank.index, 1, 0, False) if tank.move_time_diff >= tank.move_wait: print 'tick' command.angvel = 1 if tank.start_angle == -1: tank.start_angle = tank.angle angle_diff = self.to_degrees(tank.angle) - self.to_degrees(tank.start_angle) tank.angle_diff = abs((angle_diff + 180) % 360 - 180) #print self.to_degrees(tank.angle) #print self.to_degrees(tank.start_angle) print tank.angle if tank.angle_diff >= 60: tank.last_move = time.time() tank.move_wait = random.uniform(3,8) tank.start_angle = -1 if tank.shoot_time_diff >= tank.shoot_wait: print 'shoot' command.shoot = True tank.last_shoot = time.time() tank.shoot_wait = random.uniform(1.5,2.5) self.commands.append(command) results = self.bzrc.do_commands(self.commands)