Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Classes/GameDecision.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from Classes.GameObject import *

class Decision:
def __init__( self ) -> None:
pass
def GetTarget( self ) -> GameObject:

def __init__( self, target: GameObject, target_type, **kwargs) -> None:
self.target = target
self.target_type = target_type
self.kwargs = kwargs
def Getdecision( self ) -> GameObject:
return self.target
43 changes: 33 additions & 10 deletions Classes/GameObject.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@

from enum import Enum
from enum import IntEnum
from Classes.Vector import *

class ObjectType(Enum):
FRISBEE = 1
ROLLER = 2
HIGH_GOAL = 3
class ObjectType(IntEnum):
FRISBEE = 0
ROLLER = 1
RED_HIGH_GOAL = 2 # BRAIN thinks this is just a high goal
BLUE_HIGH_GOAL = 3
LOW_GOAL = 4
ENEMY_ROBOT = 5
FRIENDLY_ROBOT = 6

class RollerColor(IntEnum):
RED = 0
BLUE = 1
NEUTRAL = 2
class GoalColor(IntEnum):
RED = 0
BLUE = 1
class GameObject:
def __init__(self) -> None:
def __init__(self, pos:Vec, lifespan= 3, type=ObjectType.FRISBEE, **kwargs) -> None:
self.id = 0
self.score = 0
self.type = ObjectType.FRISBEE
self.position = Vec(0,0,0)
self.type = type
self.position = pos
self.velocity = Vec(0,0,0)

self.radial_distance = 0
self.lifespan = lifespan
self.updated = False
self.ovo = 0
self.vars = {}
if self.type == ObjectType.FRISBEE:
pass
elif self.type == ObjectType.ROLLER:
self.vars['color'] = kwargs['color'] if 'color' in kwargs else RollerColor.NEUTRAL
self.vars['is_in_contact'] = False
elif self.type == ObjectType.HIGH_GOAL:
self.vars['valid'] = False
self.vars['color'] = kwargs['color'] if 'color' in kwargs else GoalColor.RED
def __getitem__(self, item):
return self.vars[item] if item in self.vars.keys() else None
def getVector2(self) :
return Vector2(r=self.distance, theta=self.ovo)
5 changes: 0 additions & 5 deletions Classes/GameTask.py

This file was deleted.

47 changes: 47 additions & 0 deletions Classes/StaticFieldConfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from Classes.GameObject import *
from Classes.Vector import *

game_objects_config = [
{
"pos": Vec(120, 120, 0),
"type": ObjectType.HIGH_GOAL,
"kwargs": {
'color': GoalColor.RED
}
},
{
"pos": Vec(24, 24, 24),
"type": ObjectType.HIGH_GOAL,
"kwargs": {
'color': GoalColor.BLUE
}
},
{
"pos": Vec(112, 0, 18),
"type": ObjectType.ROLLER,
"kwargs": {
'color': RollerColor.NEUTRAL
}
},
{
"pos": Vec(144, 30, 18),
"type": ObjectType.ROLLER,
"kwargs": {
'color': RollerColor.NEUTRAL
}
},
{
"pos": Vec(0, 112, 18),
"type": ObjectType.ROLLER,
"kwargs": {
'color': RollerColor.NEUTRAL
}
},
{
"pos": Vec(30, 144, 18),
"type": ObjectType.ROLLER,
"kwargs": {
'color': RollerColor.NEUTRAL
}
},
]
53 changes: 52 additions & 1 deletion Classes/Vector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import math
import numpy as np

cos, sin, tan, radians, degrees, arccos, arcsin, arctan, arctan2 = np.cos, np.sin, np.tan, np.radians, np.degrees, np.arccos, np.arcsin, np.arctan2
PI = 3.14159
METERS_TO_INCHES = 39.3700787
EPSILON = 1e-7

class Vec(object):
def __init__(self, x, y, z):
Expand Down Expand Up @@ -64,4 +70,49 @@ def __div__(self, v):
return Vec(self.x / v, self.y / v, self.z / v)

def __str__(self):
return '[ %.4f, %.4f, %.4f ]' % (self.x, self.y, self.z)
return '[ %.4f, %.4f, %.4f ]' % (self.x, self.y, self.z)

def toVector2( self ):
return Vector2(x=self.x, y=self.y)

class Vector2:
'''x and y components OR the length and angle from X-Axis Counter Clockwise in Degrees'''
def __init__(self,x=0, y=0, r=0, theta=0):
if x!=0 or y!=0:
self.x = x
self.y = y
self.r = ((self.x**2 + self.y**2)**0.5)
self.theta = degrees(arctan2(self.y,self.x))
else:
self.r = r
self.theta = theta
self.x = self.r * cos(radians(theta))
self.y = self.r * sin(radians(theta))

def dot(self, b):
return (self.x*b.x) + (self.y*b.y)
def unit(self) -> 'Vector2':
return Vector2(x=self.x/self.r, y=self.y/self.r)
def scale(self, scalar: float) -> 'Vector2':
return Vector2(x=self.x*scalar, y=self.y*scalar)
def angle_from_dot(a, b):
return degrees(arccos((a.dot(b)) / (a.r * b.r) ))
def __str__(self):
return "i:{}, j:{}, r:{}, theta:{}".format(self.x, self.y, self.r, self.theta)
def __repr__(self):
return "i:{}, j:{}, r:{}, theta:{}".format(self.x, self.y, self.r, self.theta)
def __getitem__(self, idx):
if idx == 0:
return self.x
if idx == 1:
return self.y
else:
return None
def __add__(self, other) -> 'Vector2':
return Vector2(self.x + other.x, self.y + other.y)
def __sub__(self, other) -> 'Vector2':
return Vector2(self.x - other.x, self.y - other.y)
def __mul__(self, scalar: float) -> 'Vector2':
return Vector2(self.x * scalar, self.y * scalar)
def __truediv__(self, scalar: float) -> 'Vector2':
return Vector2(self.x / scalar, self.y / scalar)
34 changes: 29 additions & 5 deletions Managers/GameAnalysis.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
from Classes.GameDecision import *
from globals import *
from Classes.GameCorrection import *
from Managers.GameHasher import *
from Managers.GameObjects import *
from Managers.GameTasks import *
from concurrent.futures import ThreadPoolExecutor
import time

from typing import List, Dict, Tuple, Union
class Analyzer:
def __init__(self) -> None:
self.__gameStartTime = time.time() * 1000
self.__hasher = Hasher()
self.__hasher = None
self.__Turned = False
pass
# Finds the best decision
# Add roller blocking

def FindBest( self, game_data: ObjectManager ) -> Decision:

def FindBest( self, game_data: ObjectManager, task_manager: TaskManager ) -> Decision:
hash_list = []
if self.getMagEstimate(game_data, task_manager) >= 3: # We decide to shoot the disks
return Decision(game_data.GetGoals()[GoalColor(CURRENT_TEAM_COLOR)], Actions.Type.SHOOT_DISK)

bots = game_data.GetBots()
frisbees = game_data.GetFrisbees()
closest_frisbee = min(frisbees, key= lambda obj: obj.distance)
if closest_frisbee.distance < 2.5: # Pick up the closest frisbee
return Decision(closest_frisbee, Actions.Type.LOAD_DISK)

self.__hasher = Hasher(game_data)
bots = game_data.GetBots()
rollers = game_data.GetRollers()

extra_data = {"bots": bots, "frisbees": frisbees, "rollers": rollers}
Expand All @@ -29,8 +40,21 @@ def FindBest( self, game_data: ObjectManager ) -> Decision:

hash_list.extend(hashedFrisbees)
hash_list.extend(hashedRollers)

hash_list = sorted(hash_list, key=lambda x: x[next(iter(x.keys()))]['score'], reverse=True)
# We have a target object
if hash_list:
target_object = hash_list[0]['object']
if target_object.type == ObjectType.FRISBEE:
return Decision(target_object, Actions.Type.DRIVE_TO_OBJECT)
elif target_object.type == ObjectType.ROLLER:
return Decision (target_object, Actions.Type.CAPTURE_ROLLER, kwargs= {'power': 0.35})
else:
# if self.__Turned:
# return Decision(None, Actions.Type.DRIVE_TO_TARGET, kwargs= {'r': 3.5, 'theta': 0, 'reversed': START_REVERSED})
# else:
return Decision(None, Actions.Type.TURN_TO_ANGLE, kwargs= {'theta': 45, 'reversed': START_REVERSED})

return hash_list

def __ThreadedHashing( self, objects: list, extra: dict) -> list:

Expand Down
7 changes: 4 additions & 3 deletions Managers/GameHasher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from Managers.GameObjects import *
from Classes.GameObject import *

ROBOT_SPEED = 100.0
Expand Down Expand Up @@ -38,9 +39,9 @@
}

class Hasher:
def __init__(self) -> None:
self.robot_velocity = Vec(1,0,0)
pass
def __init__(self, object_manager) -> None:
self.object_manager = object_manager
self.robot_velocity = object_manager.my_robot['velocity']

def GetGenericHash( self, weight_type: ObjectType, weight, delta ) -> float:
weights = hash_weights[weight_type]
Expand Down
Loading