def test_move(self): the_world = world.World() man = character.Pc() the_world.current.arena.get_location(spatial.Point(1, 5)).additem(man) self.assertEqual(the_world.current.arena.find_character(man), spatial.Point(1, 5)) world.move(the_world.current, man.id, 'E', lambda x: x) self.assertEqual(the_world.current.arena.find_character(man), spatial.Point(1, 6)) world.move(the_world.current, man.id, 'N', lambda x: x) self.assertEqual(the_world.current.arena.find_character(man), spatial.Point(0, 6))
def test_pickup(self): the_world = world.World() man = character.Pc() key = items.Bone() the_world.current.arena.get_location(spatial.Point(5, 5)).additem(man) the_world.current.arena.get_location(spatial.Point(5, 5)).items.append(key) world.pickup(the_world.current, man.id, lambda x: x) self.assertGreater(len(man.inventory), 0) kitty = bestiary.Cat() the_world.current.arena.get_location(spatial.Point(5, 7)).additem(kitty) kitty.kill() the_world.attempt(man.id, actions.Move('E')) the_world.attempt(man.id, actions.Move('E')) world.pickup(the_world.current, man.id, lambda x: x)
def test_death(self): the_world = world.World() the_world.genMobs = lambda x: [] man = character.Pc() world.spawn(the_world.current, man) location = the_world.current.arena.find_character_location(man) location.characters[0].kill() loc = the_world.current.arena.find_character(man) the_world.attempt(man.id, actions.Move('E')) new_man = the_world.current.arena.find(man.id) self.assertIsNone(new_man) self.assertEqual(0, len(the_world.current.pcs)) if the_world.current.arena.in_grid(loc.add(spatial.Point(0, 1))): self.assertGreater(len(the_world.current.arena.get_location(loc.add(spatial.Point(0, 1))).items), 0)
def dropoff_in_polygon(filename, polygons, do='JFK', idx_intersect=None): # Dropoff latitude and Longitude indexes d_lat_ix = 2 d_long_ix = 3 in_poly = [] j = 0 trips = open(filename, 'r') for i, t in enumerate(trips): if idx_intersect and j == len(idx_intersect): break elif idx_intersect and i != idx_intersect[j]: continue elif idx_intersect: j += 1 splitted = t.replace('\n', '').replace('\r', '').split(',') try: d_lat = float(splitted[d_lat_ix]) d_long = float(splitted[d_long_ix]) except ValueError: # header continue trip_do = sp.Point(d_lat, d_long) if polygons[do].contains(trip_do): in_poly.append(i) return in_poly
def test_inventory(self): an_arena = arena.Arena(12, 12) snap = world.WorldSnapshot(an_arena) test_loc = an_arena.get_location(spatial.Point(1, 1)) test_pc = character.Pc() test_loc.additem(test_pc) self.assertEquals(test_pc.inventory, queries.inventory(snap, test_pc.id))
def attempt(depth): if depth > 5: return None point = spatial.Point(self.rng() % self.width, self.rng() % self.height) if not self.get_location(point).characters: return point else: return attempt(depth + 1)
def find_target(self, arena, point): min_dist = 1000 nearest = None for i, row in enumerate(arena.grid): for j, loc in enumerate(row): if loc.characters and self not in loc.characters: if self.targets_any(loc.characters): distance = abs(i - point.x) + abs(j - point.y) if distance < min_dist: min_dist = distance nearest = spatial.Point(i, j) return nearest
def build_idx(filename): # Dropoff latitude and Longitude indexes d_lat_ix = 2 d_long_ix = 3 idx = index.Index() trips = open(filename, 'r') for i, t in enumerate(trips): splitted = t.replace('\n', '').replace('\r', '').split(',') try: d_lat = float(splitted[d_lat_ix]) d_long = float(splitted[d_long_ix]) except ValueError: # header continue trip_do = sp.Point(d_lat, d_long) idx.insert(i, (trip_do.x, trip_do.y, trip_do.x, trip_do.y)) return idx
def read_polygons(filename): polygons = {} poly = open(filename, 'r') while True: try: line = poly.readline() name = line.split()[0] num_points = int(poly.readline()) points = [] for i in xrange(num_points): lon, lat = poly.readline().split(',') lon, lat = float(lon), float(lat) poly_point = sp.Point(lat, lon) points.append(poly_point) polygons[name] = sp.Polygon(points) poly.readline() except IndexError: break return polygons
def find_character(self, character): for x in range(len(self.grid)): for y in range(len(self.grid[x])): if character in self.grid[x][y]: return spatial.Point(x, y)
import copy import arena import bestiary import spatial __author__ = 'python' directions = { "N": spatial.Point(-1, 0), "S": spatial.Point(1, 0), "W": spatial.Point(0, -1), "E": spatial.Point(0, 1), "NE": spatial.Point(-1, 1), "NW": spatial.Point(-1, -1), "SE": spatial.Point(1, 1), "SW": spatial.Point(1, -1) } class WorldSnapshot: def __init__(self, current_arena): self.pcs = [] self.npcs = [] self.arena = current_arena class World: def __init__(self): an_arena = arena.Arena(12, 12) self.current = WorldSnapshot(an_arena)