def aim_to(agent, to, plus=0): car = agent.car car_direction = get_car_facing_vector(car) magnitude = Vector3(car.pos - to).magnitude() steer_correction = car_direction.correction_to(to.to_2d() - car.pos.to_2d()) z_correction = Vector3(car.pos - to).angle('z') draw_text(agent, str(math.degrees(z_correction)), 100) steer_correction *= -5 steer_correction += plus # aerial if to.z - car.pos.z > 500 and car.boost > 50 and agent.car_status != 'dribble': if math.degrees(z_correction) > 10 and to.z - car.pos.z > 500: # jump if still on ground if car.pos.z < 17.1: agent.jumps.append(1) print(car.pos.x, car.pos.y) # enable boost agent.controller_state.boost = True # sigmoid and correct agent.controller_state.pitch = cap_num((z_correction-car.rotation.pitch)+0.9, -1, 1) # if close to going to fly stop boost elif math.degrees(z_correction) > 4: agent.controller_state.boost = False # Drift if needs to steer much if abs(steer_correction) > 7: agent.controller_state.handbrake = True agent.controller_state.steer = cap_num(steer_correction, -1, 1)
def decide_car_status(agent, packet): car = agent.car ball = agent.ball car_to_ball_vector = Vector3(ball.pos - car.pos) ball_car_magnitude_2d = car_to_ball_vector.to_2d().magnitude() ball_location = Vector3(packet.game_ball.physics.location) # decide car status if none if agent.car_status == 'none': if car.team and ((ball_location.y > 1000) or predict_ball_own_goal(agent)): agent.car_status = 'clear_ball' elif not car.team and ((ball_location.y < -1000) or predict_ball_own_goal(agent)): agent.car_status = 'clear_ball' elif car.team == (car.pos.y-ball_location.y > 0): agent.car_status = 'kick_ball' # if ball in air and close. dribble if agent.ball.pos.z > 1000 and ball_car_magnitude_2d < 2000: agent.car_status = 'dribble' kick_ball.ball_prediction = 0 elif car.boost < 50: agent.car_status = 'get_boost' else: agent.car_status = 'clear_ball' # if kickoff. Car status kick ball if packet.game_info.is_kickoff_pause: agent.car_status = 'kickoff' agent.jumps = []
def get_ideal_car_location(agent, packet, ball_location=0): if ball_location == 0: ball_location = kick_ball.ball_prediction.pos car = agent.car car_to_ball_vector = Vector3(ball_location) - Vector3(car.pos) car_to_ball_magnitude = car_to_ball_vector.magnitude() goal = Vector3(get_opponents_goal(agent)) # If not in right line with ball and goal Go there. ball_to_goal_vector = goal - ball_location ball_to_goal_unit_vector = ball_to_goal_vector.unit_vector() distance_ball_and_car_needed = kick_ball.get_distance_ball_car_needed( agent, packet, ball_to_goal_unit_vector) ideal_car_location_relative = ball_to_goal_unit_vector.multiply( distance_ball_and_car_needed) ideal_car_location = ideal_car_location_relative + ball_location # if distance bigger than -500 inverse ideal and not in corners. And kick ball via wall if distance_ball_and_car_needed > -1000 and abs( ideal_car_location.y) < 4000: distance_ball_and_car_needed = kick_ball.ideal_location_away ideal_car_location_relative = ball_to_goal_unit_vector.multiply( distance_ball_and_car_needed) ideal_car_location_relative.y *= -1 ideal_car_location = ideal_car_location_relative + ball_location else: ideal_car_location_relative = ball_to_goal_unit_vector.multiply( distance_ball_and_car_needed) ideal_car_location = ideal_car_location_relative + ball_location return ideal_car_location
def from_file(cls, f): start = f.tell() restTranslate = Vector3(read_float(f), read_float(f), read_float(f)) f.seek(start + 0x30) invTranslate = Vector3(read_float(f), read_float(f), read_float(f)) val = read_uint32(f) parentID = (val >> 0x10) & 0xFF tagID = (val >> 0x18) & 0xFF f.seek(start + 0x40)
def __init__(self, agent, ball, is_prediction=False): self.pos = Vector3(ball.physics.location) self.rotation = ball.physics.rotation self.velocity = Vector3(ball.physics.velocity) self.ang_velocity = Vector3(ball.physics.angular_velocity) if not is_prediction: self.latest_touch = ball.latest_touch self.prediction = [] if not is_prediction: prediction = agent.get_ball_prediction_struct() for num in range(360): prediction_slice = prediction.slices[num] self.prediction.append(Ball(agent, prediction_slice, True))
def __init__(self, car): self.pos = Vector3(car.physics.location) self.rotation = car.physics.rotation self.velocity = Vector3(car.physics.velocity) self.ang_velocity = Vector3(car.physics.angular_velocity) self.is_demolished = car.is_demolished self.has_wheel_contact = car.has_wheel_contact self.is_super_sonic = car.is_super_sonic self.jumped = car.jumped self.double_jumped = car.double_jumped self.name = car.name self.boost = car.boost self.score_info = car.score_info self.team = car.team
def get_opponents_goal(agent): car = agent.car field_info = agent.get_field_info() goal = Vector2(0, 0) team = 1 if field_info.goals[team].team_num == car.team: team = 0 return Vector3(field_info.goals[team].location)
def get_distance_ball_car_needed(agent, packet, ball_to_goal_unit_vector): distance_ball_and_car_needed = -kick_ball.ideal_location_away ball_location = kick_ball.ball_prediction.pos ideal_car_location_relative = ball_to_goal_unit_vector.multiply( distance_ball_and_car_needed) ideal_car_location = ideal_car_location_relative + ball_location # https://i.imgur.com/UXvb76k.png if ideal_car_location.x > 4096: # sidewall x x = abs(4096 - ideal_car_location.x) y = abs(4096 - ideal_car_location.x) / abs( ideal_car_location.x - ball_location.x) * abs(ideal_car_location.y - ball_location.y) z = abs(4096 - ideal_car_location.x) / abs( ideal_car_location.x - ball_location.x) * abs(ideal_car_location.z - ball_location.z) distance_ball_and_car_needed += Vector3(x, y, z).magnitude() + 400 if ideal_car_location.x < -4096: # sidewall x x = abs(-4096 - ideal_car_location.x) y = abs(-4096 - ideal_car_location.x) / abs( ideal_car_location.x - ball_location.x) * abs(ideal_car_location.y - ball_location.y) z = abs(-4096 - ideal_car_location.x) / abs( ideal_car_location.x - ball_location.x) * abs(ideal_car_location.z - ball_location.z) distance_ball_and_car_needed += Vector3(x, y, z).magnitude() + 400 if ideal_car_location.y > 5120: # backwall y x = abs(5120 - ideal_car_location.y) y = abs(5120 - ideal_car_location.y) / abs( ideal_car_location.y - ball_location.y) * abs(ideal_car_location.x - ball_location.x) z = abs(5120 - ideal_car_location.y) / abs( ideal_car_location.y - ball_location.y) * abs(ideal_car_location.z - ball_location.z) distance_ball_and_car_needed += Vector3(x, y, z).magnitude() + 400 if ideal_car_location.y < -5120: # backwall y x = abs(-5120 - ideal_car_location.y) y = abs(-5120 - ideal_car_location.y) / abs( ideal_car_location.y - ball_location.y) * abs(ideal_car_location.x - ball_location.x) z = abs(-5120 - ideal_car_location.y) / abs( ideal_car_location.y - ball_location.y) * abs(ideal_car_location.z - ball_location.z) distance_ball_and_car_needed += Vector3(x, y, z).magnitude() + 400 return distance_ball_and_car_needed
def wrong_side(agent, packet): my_car = packet.game_cars[agent.index] car_location = Vector2(my_car.physics.location.x, my_car.physics.location.y) car_direction = get_car_facing_vector(my_car) ball_location = Vector2(packet.game_ball.physics.location.x, packet.game_ball.physics.location.y) goal = get_own_goal(agent, packet) difference = abs(difference_angles(Vector3(goal).to_2d().get_angle(), car_location.get_angle())) # double jump if on ground and straight and going good direction if agent.jumps == [] and my_car.physics.location.z < 17.1 and difference < 10 and get_xy_speed(agent, packet) > 1000 and my_car.boost > 50: agent.controller_state.boost = True if agent.jumps == [] and my_car.physics.location.z < 17.1 and difference < 10 and get_xy_speed(agent, packet) > 1000 and not agent.controller_state.boost: double_jump(agent) aim_to(agent, goal) agent.controller_state.throttle = 1.0 # if in front of the ball+200 reset car status if my_car.team == (my_car.physics.location.y-ball_location.y > -2000): agent.car_status = 'none'
def main(agent, packet): car = agent.car ball = agent.ball car_to_ball_vector = Vector3(ball.pos - car.pos) ball_car_magnitude_2d = car_to_ball_vector.to_2d().magnitude() rotated_car_to_ball_vector2 = car_to_ball_vector.to_2d().rotate( car.rotation.yaw) throttle = -cap_num(rotated_car_to_ball_vector2.x / 800, -1, 1) # if ball is far away from ball. or on ground. stop dribbling if ball_car_magnitude_2d > 2000 or ball.pos.z < 100: aim_to(agent, ball.pos) agent.car_status = 'none' # if ball in air. Try to get under it if ball.pos.z > 155: aim_to(agent, ball.pos) agent.controller_state.throttle = ball_car_magnitude_2d / 200 agent.renderer.draw_string_3d( car.pos.change('z', 100).get_array(), 2, 2, str(round(throttle * 100) / 100), white(agent)) #if ball_car_magnitude_2d > 400: agent.controller_state.boost = True # if ball on top of car elif ball_car_magnitude_2d < 150: aim_to(agent, ball.pos) # difference velocity car and ball diff = (ball.velocity.to_2d().magnitude() - car.velocity.to_2d().magnitude()) / 20 agent.controller_state.throttle = cap_num(throttle + diff, -1, 1) if throttle + diff > 4: agent.controller_state.boost = True agent.renderer.draw_string_3d( car.pos.change('z', 100).get_array(), 2, 2, str(round(throttle * 100) / 100) + ' :: ' + str(round(diff * 100) / 200), white(agent)) # flip the ball when its about to fall off if throttle > 0.15: double_jump(agent) agent.car_status = 'none'
def main(agent, packet): if kick_ball.ball_prediction != 0: ball_location = kick_ball.ball_prediction.pos else: ball_location = Vector3(packet.game_ball.physics.location) kick_ball.prediction_timer = -1 ball = agent.ball car = agent.car car_location = car.pos.to_2d() car_to_ball_vector = Vector3(ball.pos) - Vector3(car.pos) car_to_ball_magnitude = car_to_ball_vector.magnitude() car_to_ball_magnitude_2d = car_to_ball_vector.to_2d().magnitude() car_direction = get_car_facing_vector(car) goal = get_opponents_goal(agent) go_to = car_location plus = 0 # ball prediction kick_ball.prediction_timer -= 1 / 60 if kick_ball.prediction_timer < -0.1: agent.car_status = 'none' kick_ball.in_position = False time = kick_ball.predict_when_ball_hit(agent) kick_ball.prediction_timer = time kick_ball.ball_prediction = ball.prediction[int(time * 60)] kick_ball.correction_timer = kick_ball.when_ball_hit_correction(agent) kick_ball.ball_prediction = ball.prediction[int( cap_num(kick_ball.prediction_timer * 60, 0, 1e9))] # a few variables ball_location = kick_ball.ball_prediction.pos car_to_ball_vector = Vector3(ball.pos) - Vector3(car.pos) car_to_ball_magnitude = car_to_ball_vector.magnitude() car_to_ball_magnitude_2d = car_to_ball_vector.to_2d().magnitude() ideal_car_location = kick_ball.get_ideal_car_location( agent, packet, ball_location) # draw line from ball to ideal car location Vector3(ball_location).draw_to(agent, ideal_car_location, [0, 100, 0]) # 3D correction timer vs actual prediction agent.renderer.draw_string_3d( car.pos.change('z', 100).get_array(), 2, 2, 'corr: ' + str( round(kick_ball.correction_timer, 2) - round(kick_ball.prediction_timer, 2)), white(agent)) # draw place where car will hit ball agent.renderer.draw_string_3d(ball_location.get_array(), 2, 2, ball_location.string(), white(agent)) ideal_car_to_ball_angle = (ball_location - ideal_car_location).to_2d().get_angle() car_to_ball_to_angle = (ball_location - car.pos).to_2d().get_angle() difference_angle_car_ideal = difference_angles(ideal_car_to_ball_angle, car_to_ball_to_angle) if difference_angle_car_ideal > kick_ball.get_angle_for_goal( agent, packet, ball_location) * 2: kick_ball.in_position = False car_inline_bool = abs( difference_angle_car_ideal) < kick_ball.get_angle_for_goal( agent, packet, ball_location) * 2 # If car not close enough to ideal and car not close to idealLine. Go there if Vector2( ideal_car_location.x - car_location.x, ideal_car_location.y - car_location.y).magnitude( ) > 1000 and not car_inline_bool and not kick_ball.in_position: go_to = ideal_car_location #plus = -difference_angle_car_ideal/(car_to_ball_magnitude/200) # double jump if on ground and straight and going good direction and far away far_away_enough = Vector2( car_location.x - ideal_car_location.x, car_location.y - ideal_car_location.y).magnitude() > 1000 # draw ideal location a = [ideal_car_location.x, ideal_car_location.y, 17] agent.renderer.draw_rect_3d(a, 40, 40, True, own_color(agent, packet)) # Else head towards the ball else: kick_ball.in_position = True # predict ball location and go there go_to = ball_location plus = -difference_angle_car_ideal / (car_to_ball_magnitude / 200) #if car_to_ball_magnitude_2d > 2000: plus = -difference_angle_car_ideal/40 # boost too_slow = kick_ball.correction_timer > kick_ball.prediction_timer + 0.01 if car.pos.z < 17.1 and car_inline_bool and too_slow: agent.controller_state.boost = True agent.renderer.draw_line_3d( car.pos.get_array(), ball_location.get_array(), agent.renderer.create_color(255, 255, 255, 255)) agent.renderer.draw_line_3d(ideal_car_location.get_array(), ball_location.get_array(), own_color(agent, packet)) draw_text(agent, 'LR. corr: ' + str(plus), 110) aim_to(agent, go_to, plus) # If realy close to ball and good direction Jump if car_to_ball_magnitude < 200 and abs( car_direction.correction_to( Vector2(ball_location.x, ball_location.y) - car_location)) < 0.1: double_jump(agent) agent.controller_state.throttle = 1 if kick_ball.correction_timer + 0.05 < kick_ball.prediction_timer: agent.controller_state.throttle = 1 - ( kick_ball.prediction_timer - kick_ball.correction_timer - kick_ball.prediction_timer) / 5 # if me hit the ball stop kicking the ball if packet.game_ball.latest_touch.player_name == car.name and packet.game_info.seconds_elapsed - packet.game_ball.latest_touch.time_seconds < 1: agent.car_status = 'none' kick_ball.ball_prediction = 0 return agent
def from_file(cls, f, out, outmat, mtlname, materials, textures): version = read_uint32(f) assert version == 0xA shaderCount = read_uint8(f) boneCount = read_uint8(f) vertGroupCount = read_uint8(f) meshGroupCount = read_uint8(f) flags = read_uint32(f) boundary = Boundary.from_file(f) vtxgroups = [] for i in range(4): vtxtype = read_uint8(f) f.read(3) # padd vtxcount = read_uint32(f) vtxgroups.append((vtxtype, vtxcount)) indexCount = read_uint32(f) lodStarts = [read_uint32(f) for i in range(4)] assert f.tell() == 88 lodCount = read_uint8(f) f.seek(0x5C) shaders = [] for i in range(shaderCount): name = f.read(0x20).strip(b"\x00") shaders.append(name.decode("ascii")) bones = [] for i in range(boneCount): bone = Bone.from_file(f) bones.append(bone) if boneCount == 0: bone = Bone() bones.append(bone) vertexGroups = [] #with open("model.obj", "w") as g: if True: for shader in shaders: print(shader.lower(), shader.lower() in materials) if shader.lower() in materials: texpath = materials[shader.lower()] else: if shader in textures: texpath = textures[shader] elif shader + ".dds" in textures: texpath = textures[shader + ".dds"] else: texpath = "NotFound" outmat.write("newmtl {0}\n".format(shader)) outmat.write("map_kd {0}\n".format(texpath)) out.write("mtllib {0}\n".format(mtlname)) g = out print("vert group count", vertGroupCount) for i in range(vertGroupCount): group = [] verttype, vertcount = vtxgroups[i] print(verttype, vertcount) #assert verttype == 0 if verttype == 0 or verttype == 3: for i in range(vertcount): start = f.tell() vertexpos = Vector3(read_float(f), read_float(f), read_float(f)) normal = Vector3(read_float(f), read_float(f), read_float(f)) uv = Vector3(read_float(f), read_float(f), 0.0) assert f.tell() - start == 0x20 #g.write("v {0} {1} {2}\n".format(vertexpos.x, vertexpos.z, -vertexpos.y)) group.append(Vertex(vertexpos, uv=uv)) elif verttype == 1: for i in range(vertcount): start = f.tell() vertexpos = Vector3(read_float(f), read_float(f), read_float(f)) normal = Vector3(read_float(f), read_float(f), read_float(f)) uv = Vector3(read_float(f), read_float(f), 0.0) f.read(4) assert f.tell() - start == 0x24 #g.write("v {0} {1} {2}\n".format(vertexpos.x, vertexpos.z, -vertexpos.y)) group.append(Vertex(vertexpos, uv=uv)) elif verttype == 2: for i in range(vertcount): start = f.tell() vertexpos = Vector3(read_float(f), read_float(f), read_float(f)) normal = Vector3(read_float(f), read_float(f), read_float(f)) uv = Vector3(read_float(f), read_float(f), 0.0) f.read(0x30 - 0x20) assert f.tell() - start == 0x30 #g.write("v {0} {1} {2}\n".format(vertexpos.x, vertexpos.z, -vertexpos.y)) group.append(Vertex(vertexpos, uv=uv)) else: raise RuntimeError("Unknown vtx type {0}".format(verttype)) pass vertexGroups.append(group) indices = [] for i in range(indexCount): indices.append(read_uint16(f)) meshes = [] index = 0 for i in range(meshGroupCount): mesh, index = MeshGroupFile.from_file(f, index) meshes.append(mesh) vtxGroupOffsets = [] for i in range(vertGroupCount): if i == 0: vtxGroupOffsets.append(0) else: vtxGroupOffsets.append(len(vertexGroups[i - 1])) for group in vertexGroups: for vertex in group: vertexpos = vertex.pos uv = vertex.uv g.write("v {0} {1} {2}\n".format(vertexpos.x, vertexpos.z, -vertexpos.y)) if uv is not None: g.write("vt {0} {1}\n".format(uv.x, 1.0 - uv.y)) #meshes.pop(0) #meshes.pop(0) vtxOffset = 0 k = 0 drawMesh = 4 out.write("o {0}\n".format(os.path.basename(mtlname)[:-4])) for mesh in meshes: #print(mesh.materialid) out.write("usemtl {0}\n".format(shaders[mesh.materialid])) mainlod = mesh.lods[0] print(mesh.vtxGroup, vtxGroupOffsets) print("Mesh has", mainlod.stripCount, "strips") """if k != drawMesh: vtxOffset += mesh.vertexCount k += 1 continue""" if mainlod.stripCount > 0: v1, v2, v3 = None, None, None n = 0 for i in range(mainlod.stripStart, mainlod.stripStart + mainlod.stripCount): if v1 is None: v1 = indices[i] continue elif v2 is None: v2 = indices[i] continue elif v3 is None: v3 = indices[i] else: v1 = v2 v2 = v3 v3 = indices[i] offset = vtxGroupOffsets[mesh.vtxGroup] #offset = 0 if n == 0: g.write("f {0}/{0} {1}/{1} {2}/{2}\n".format( v1 + 1 + vtxOffset, v2 + 1 + vtxOffset, v3 + 1 + vtxOffset)) else: g.write("f {0}/{0} {1}/{1} {2}/{2}\n".format( v1 + 1 + vtxOffset, v3 + 1 + vtxOffset, v2 + 1 + vtxOffset, )) n = (n + 1) % 2 print("Mesh has", mainlod.listCount, "lists") if mainlod.listCount > 0: for i in range(mainlod.listCount // 3): v1 = indices[mainlod.listStart + i * 3] v2 = indices[mainlod.listStart + i * 3 + 1] v3 = indices[mainlod.listStart + i * 3 + 2] offset = vtxGroupOffsets[mesh.vtxGroup] #offset = 0 g.write("f {0}/{0} {1}/{1} {2}/{2}\n".format( v1 + 1 + vtxOffset, v2 + 1 + vtxOffset, v3 + 1 + vtxOffset)) vtxOffset += mesh.vertexCount #if k == 4: # break k += 1
def from_file(cls, f): start = Vector3(read_float(f), read_float(f), read_float(f)) end = Vector3(read_float(f), read_float(f), read_float(f)) return cls(start, end)
def __init__(self, position=Vector3(), rotation=Vector3()): self.rotation = deepcopy(rotation) self.position = deepcopy(position) self.target_rotation = deepcopy(rotation) self.target_position = deepcopy(position)
from camera import Camera from vectors import Vector3 DEFAULT_CAMERA_POSITION = Vector3(0,0,-3) DEFAULT_CAMERA_ROTATION = Vector3(0,0,0) class ViewController: def __init__(self): self.camera = Camera(position=Vector3(0,0,-3)) #self.camera = Camera(position=Vector3(0,0,0)) self.meshes = [] self.bored = False def reset_camera(self): self.camera.target_position.x = DEFAULT_CAMERA_POSITION.x self.camera.target_position.y = DEFAULT_CAMERA_POSITION.y self.camera.target_position.z = DEFAULT_CAMERA_POSITION.z self.camera.target_rotation.x = DEFAULT_CAMERA_ROTATION.x self.camera.target_rotation.y = DEFAULT_CAMERA_ROTATION.y self.camera.target_rotation.z = DEFAULT_CAMERA_ROTATION.z def rotate_camera_to(self, x, y): self.camera.rotate_to(x, y) def rotate_camera_by(self, x, y): self.camera.rotate_by(x, y) def zoom_camera_by(self, z): self.camera.zoom_by(z)
def get_xy_speed(agent): car = agent.car car_xy_velocity = Vector3(car.velocity).to_2d() car_xy_velocity_magnitude = car_xy_velocity.magnitude() return car_xy_velocity_magnitude
def __init__(self): self.camera = Camera(position=Vector3(0,0,-3)) #self.camera = Camera(position=Vector3(0,0,0)) self.meshes = [] self.bored = False
def get_own_goal(agent): car = agent.car field_info = agent.get_field_info() team = 0 if field_info.goals[team].team_num != car.team: team = 1 return Vector3(field_info.goals[team].location)