Utilities¶
This module holds functions, exceptions and constants that are or might be used by both the game, renderer and perhaps the agents. By putting this code in a separate module, each of them can access it without requiring the other modules.

domination.utilities.
frange
(limit1, limit2=None, increment=1.0)[source]¶ Like xrange, but for real numbers.

domination.utilities.
stdev
(iterable)[source]¶ Returns standard deviation of given list or generator.
>>> stdev([1,2,3]) 1.0

domination.utilities.
point_add
(a, b)[source]¶ Add the coordinates of two points (Inline this if you can, function calls are slow)

domination.utilities.
point_sub
(a, b)[source]¶ Subtract two 2d vectors (Inline this if you can, function calls are slow)

domination.utilities.
point_mul
(a, f)[source]¶ Multiply a vector by a scalar (Inline this if you can, function calls are slow)

domination.utilities.
line_intersects_rect
(p0, p1, r)[source]¶ Check where a line between p1 and p2 intersects given axisaligned rectangle r. Returns False if no intersection found. Uses the LiangBarsky line clipping algorithm.
>>> line_intersects_rect((1.0,0.0),(1.0,4.0),(0.0,1.0,4.0,1.0)) ((0.25, (1.0, 1.0)), (0.5, (1.0, 2.0)))
>>> line_intersects_rect((1.0,0.0),(3.0,0.0),(0.0,1.0,3.0,1.0)) False

domination.utilities.
line_intersects_circ
((p0x, p0y), (p1x, p1y), (cx, cy), r)[source]¶ Computes intersections between line and circle. The line runs between (p0x,p0y) and (p1x,p1y) and the circle is centered at (cx,cy) with a radius r. Returns False if no intersection is found, and one or two intersection points otherwise. Intersection points are (t, (x, y)) where t is the distance along the line between 01. (From stackoverflow.com/questions/1073336/circlelinecollisiondetection)
>>> line_intersects_circ((0,0), (4,0), (2,0), 1) [(0.25, (1.0, 0.0)), (0.75, (3.0, 0.0))]
>>> line_intersects_circ((0,0), (2,0), (2,0), 1) [(0.5, (1.0, 0.0))]
>>> line_intersects_circ((0,1), (2,1), (1,0), 1) [(0.5, (1.0, 1.0))]
>>> line_intersects_circ((0,0), (0,1), (2,0), 1) False

domination.utilities.
line_intersects_grid
((x0, y0), (x1, y1), grid, grid_cell_size=1)[source]¶ Performs a line/grid intersection, finding the “super cover” of a line and seeing if any of the grid cells are occupied. The line runs between (x0,y0) and (x1,y1), and (0,0) is the topleft corner of the topleft grid cell.
>>> line_intersects_grid((0,0),(2,2),[[0,0,0],[0,1,0],[0,0,0]]) True
>>> line_intersects_grid((0,0),(0.99,2),[[0,0,0],[0,1,0],[0,0,0]]) False

domination.utilities.
rect_offset
(rect, offset)[source]¶ Offsets (grows) a rectangle in each direction.

domination.utilities.
rect_corners
(rect)[source]¶ Returns cornerpoints of given rectangle.
>>> rect_corners((1,2,1,3)) ((1, 2), (2, 2), (2, 5), (1, 5))

domination.utilities.
rects_bound
(rects)[source]¶ Returns a rectangle that bounds all given rectangles
>>> rects_bound([(0,0,1,1), (3,3,1,1)]) (0, 0, 4, 4)

domination.utilities.
rects_merge
(rects)[source]¶ Merge a list of rectangle (xywh) tuples. Returns a list of rectangles that cover the same surface. This is not necessarily optimal though.
>>> rects_merge([(0,0,1,1),(1,0,1,1)]) [(0, 0, 2, 1)]

domination.utilities.
angle_fix
(theta)[source]¶ Fixes an angle to a value between pi and pi.
>>> angle_fix(2*pi) 0.0

domination.utilities.
reachable
(grid, (x, y), border=1)[source]¶ Performs a ‘flood fill’ operation to find reachable areas on given tile map from (x,y). Returns as binary grid with 1 for reachable.
Parameters: border – can be a value or a function indicating borders of region >>> reachable([[0,1,0],[0,1,0]], (0,0)) [[1, 0, 0], [1, 0, 0]]
Generate an almost optimal navigation mesh between the given walls (rectangles), within the world bounds (a big rectangle). Mesh is a dictionary of dictionaries:
mesh[point1][point2] = distance

domination.utilities.
find_path
(start, end, mesh, grid, tilesize=16)[source]¶ Uses astar to find a path from start to end, using the given mesh and tile grid.
>>> grid = [[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0],[0,0,0,0,0]] >>> mesh = make_nav_mesh([(2,2,1,1)],(0,0,4,4),1) >>> find_path((0,0),(4,4),mesh,grid,1) [(4, 1), (4, 4)]