Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
elachuni committed Jul 8, 2012
2 parents 0a6907c + f922f7a commit 403af5d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
17 changes: 8 additions & 9 deletions bots/maptools.py
Expand Up @@ -3,7 +3,7 @@

from spacecraft import map

from shapely.geometry import Polygon
from shapely.geometry import Polygon, LineString
from spacecraft import euclid

class MapLoader(map.MapLoader):
Expand All @@ -18,7 +18,6 @@ def __init__(self, filename):
self.close_methods = {
}


def open_rect(self, node, game, transform):
x = float(node.attrib["x"])
y = float(node.attrib["y"])
Expand All @@ -28,23 +27,23 @@ def open_rect(self, node, game, transform):
height = float(node.attrib["height"])
game.add_wall(x, y, width, height)


class GridMap(object):

xsize = 300
ysize = 300
xbuckets = ybuckets = 50


def __init__(self, filename):
l = MapLoader(filename)
self.walls = []
l.setup_map(self)
self.build_grid()
self.goal = None
self.goal_grid = [ [float("+inf")] * self.ybuckets for i in range(self.xbuckets) ]
self.goal_grid = None

def build_grid(self):
self.grid = [ [0] * self.ybuckets for i in range(self.xbuckets) ]
self.grid = [[0] * self.ybuckets for i in range(self.xbuckets)]
for xb in range(self.xbuckets):
for yb in range(self.ybuckets):
cell = self.build_cell(xb, yb)
Expand Down Expand Up @@ -109,7 +108,7 @@ def dump_search_state(self, open, closed, start, goal):
else:
val = str(self.grid[xp][yp])

parts.append( val )
parts.append(val)
print "".join(parts)

def dump_path(self, start, goal, path, X=[]):
Expand Down Expand Up @@ -223,9 +222,7 @@ def is_wall(self, pos):
return False

def visible(self, start, end):
_start = self.world_to_cell(*start)
_end = self.world_to_cell(*end)
return self._visible(_start, _end)
return not self.intersects(LineString([start, end]))

def waypoint(self, start, goal):
"""In world coordinates!"""
Expand Down Expand Up @@ -281,6 +278,8 @@ def towards_goal_from(self, current):
step = 0
while True:
np = winner(self.neighbor_nodes(goal), self.goalpath)
if np is None:
break
if not self._visible(p, np):
break
goal = np
Expand Down
28 changes: 23 additions & 5 deletions bots/navigator.py
@@ -1,4 +1,5 @@
# -*- coding: utf-8 *-*
import uuid
from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor
import random
Expand All @@ -18,7 +19,7 @@ def closer(target, candidates):

class RandomClient(spacecraft.server.ClientBase):

name = "navigator"
name = "navigator_" + str(uuid.uuid4())[:3]

gridmap = None
way = None
Expand All @@ -44,6 +45,16 @@ def look_to(self, target):
target.x, target.y, self.angle)
self.command('turn', value=turn)

def goto(self, target):
dist = abs(self.pos - target)
target = target - self.vel * dist / 70
self.look_to(target)

def aim(self, target, target_velocity):
dist = abs(self.pos - target)
target = target + target_velocity * dist / 70
self.look_to(target)

def messageReceived(self, message):
if self.gridmap is None:
self.gridmap = maptools.GridMap("maps/cross.svg")
Expand All @@ -55,6 +66,7 @@ def messageReceived(self, message):
return

self.pos = euclid.Point2(*message['gps']['position'])
self.vel = euclid.Point2(*message['gps']['velocity'])
self.angle = message['gps']['angle']

# if number of enemies visible == 1
Expand All @@ -65,13 +77,21 @@ def messageReceived(self, message):
if obj['object_type'] in ['player']:
enemies += 1
ep = p2(*obj['position'])
ev = p2(*obj['velocity'])

if enemies == 1:
if self.gridmap.visible(self.pos, ep):
self.look_to(ep)
self.aim(ep, ev)
self.command("throttle", value=1)
self.command("fire")
return
else:
print "enemy not visible."
elif enemies > 1:
# FLEE
while abs(self.pos - self.camp) < 150:
self.camp, self.camp_look = random.choice(self.camp_options)


# camped!!!
if abs(self.pos - self.camp) < 10:
Expand All @@ -95,10 +115,8 @@ def messageReceived(self, message):


waypoint = p2(*self.gridmap.towards_goal_from(self.pos))
print "going to", waypoint, "from", self.pos
print "w1", self.gridmap.is_wall(waypoint)

self.look_to(waypoint)
self.goto(waypoint)
self.command("throttle", value=1)


Expand Down
24 changes: 17 additions & 7 deletions spacecraft/monitor.py
Expand Up @@ -136,6 +136,16 @@ def process_events(self):
self.track_next()
elif event.key == pygame.K_RIGHT:
self.track_previous()
elif event.key == pygame.K_UP:
self.zoom_in()
elif event.key == pygame.K_DOWN:
self.zoom_out()

def zoom_in(self):
self.scene.scale(1.1)

def zoom_out(self):
self.scene.scale(0.9)

def process_message(self, message):
pass
Expand Down Expand Up @@ -167,8 +177,9 @@ def draw_avatar(self, position, angle, velocity, throttle=None,

def draw_proximity_area(self, position):
color = (36, 46, 56)
pygame.draw.circle(self.screen, color, position,
UNIVERSE_SCALING_FACTOR * world.ProximitySensor.radius, 1)
radius, _ = self.scene.to_screen(
world.ProximitySensor.radius, world.ProximitySensor.radius)
pygame.draw.circle(self.screen, color, position, radius, 1)

def draw_name(self, position, name):
font_size = 16
Expand Down Expand Up @@ -202,10 +213,9 @@ def render_screen(self, messages):
self.offset = self.next_offset
for wall in self.terrain:
x, y = self.to_screen((wall['x'], wall['y']))
w = int(wall['width'] * 7) # Because 7 works
h = int(wall['height'] * 7)
y = y - h
rect = pygame.Rect(x, y, w, h)
w, h = self.to_screen((wall['width'] + wall['x'],
wall['height'] + wall['y']))
rect = pygame.Rect(x, y, w - x, h - y)
pygame.draw.rect(self.screen, (100, 100, 100), rect, 0)
for msg in messages:
kind = msg.get("type", None)
Expand Down Expand Up @@ -267,7 +277,7 @@ def render_screen(self, messages):
def set_next_offset(self, msg):
if self.world_size==[0,0]:
# the world size has not arrived yet
return
return
x, y = self.scene.to_screen(*msg['position'])
speedx, speedy = msg['velocity']
w, h = self.scene.size
Expand Down
1 change: 1 addition & 0 deletions spacecraft/world.py
Expand Up @@ -177,6 +177,7 @@ def get_result_table(self):


class ObjectBase(object):
name = "unknown"

def __init__(self, map, x=None, y=None):
self.map = map
Expand Down

0 comments on commit 403af5d

Please sign in to comment.