def flameplayer(): fronttiles = [coords.sum(self.position, coords.mul(self.direction, i)) for i in range(1,4)] for tile in fronttiles: if tile in [player.position for player in world.players]: self._suggestmessage("The dragon breaths a jet of fire towards you", 5) for tile in fronttiles: self.cellmap.ignite(tile, forceignite=True) break
def move(self, x, y): """Move if possible, update collectable levels accordingly""" if abs(x) + abs(y) != 1: return False self.direction = (x, y) if ((self.cellmap[coords.sum(self.position, (x, y))]['solid'] or self.cellmap[coords.sum(self.position, (x, y))]['sogginess'] == 100) and not GEPlayer.FREEPLAYER): return False self.position = coords.modsum(self.position, self.direction, self.cellmap.size) collectable = self.cellmap[self.position]['collectableitem'] if collectable != 0: self.score[collectable] += collectables.value[collectable] self._suggestmessage("You pick up " + collectables.name[collectable], 4) self.cellmap[self.position]['collectableitem'] = 0 if not GEPlayer.FREEPLAYER: self.score[collectables.CHOCOLATE] -= self.terraincost() if self.layingfuse and self.cellmap[self.position]['sogginess'] < GEPlayer.MAXFUSESOG: self.cellmap.placefuse(self.position) return True
def sprite(self, player): isvisible = False tileoffset = [-1 if axis == 1 else 0 for axis in self.direction] for tile in [(0,0), (0,1), (1,0), (1,1)]: tile = coords.modsum(tile, tileoffset, self.cellmap.size) if coords.sum(self.position, tile) in player.visibletiles: isvisible = True break if isvisible: return self._pokedsprite(images.DragonRed[self.direction], layer=20, offset=coords.mul(tileoffset, images.TILESIZE)) else: return None
def scattercoins(self, radius, number): sqradius = radius**2 scattered = 0 attempts = 0 while scattered < number and attempts < 6*number and self.score[collectables.COIN] > 0: attempts += 1 tryoffset = random.randint(-radius, radius), random.randint(-radius, radius) if tryoffset[0]**2 + tryoffset[1]**2 > sqradius: continue trypos = coords.sum(self.position, tryoffset) if self.cellmap[trypos]['collectableitem'] or self.cellmap[trypos]['solid']: continue self.cellmap[trypos]['collectableitem'] = collectables.COIN self.score[collectables.COIN] -= 1 scattered += 1
def visibletiles(position, cellmap, viewradius, xray=False): """Return set of tiles visible from a position""" visibletiles = set() visibletiles.add(position) def inviewradius(a): return ((a[0]-position[0])**2 + (a[1]-position[1])**2 < viewradius**2) for outdir in CARDINALS: trunkpos = position while inviewradius(trunkpos): visibletiles.add(coords.mod(trunkpos, cellmap.size)) for perpdir in perpendiculars(outdir): diagdir = coords.sum(outdir, perpdir) branchpos = trunkpos while inviewradius(branchpos): visibletiles.add(coords.mod(branchpos, cellmap.size)) if not xray and not cellmap[branchpos]['transparent']: break branchpos = coords.sum(branchpos, diagdir) if not xray and not cellmap[trunkpos]['transparent']: break trunkpos = coords.sum(trunkpos, outdir) return visibletiles
def firstmove(startpos, destpos, costs, cellmap, pfmapsize=15, maxcost=40): """Find the best direction to move towards the destination""" if (any(ax > pfmapsize for ax in coords.mindist(startpos, destpos, cellmap.size))): # Player is outside pathfinder area return False def mapcoord(pfcoord): """Get map coordinate from pathfinder one""" return coords.modsum(startpos, pfcoord, (-pfmapsize,)*2, cellmap.size) foundtarget = False dijkstramap = [[[0, (pfmapsize,)*2, False] for x in xrange(2*pfmapsize)] for x in xrange(2*pfmapsize)] import heapq openlist = [] heapq.heappush(openlist, (0, (pfmapsize,)*2)) curpos = None while openlist: curnode = heapq.heappop(openlist) curdist = curnode[0] if curdist > maxcost: # Give up if player is painfully unreachable. break curpos = curnode[1] if mapcoord(curpos) == tuple(destpos): foundtarget = True break if dijkstramap[curpos[0]][curpos[1]][2] == True: continue else: dijkstramap[curpos[0]][curpos[1]][2] = True for nbrpos in coords.neighbours(curpos): if (nbrpos[0] < 0 or nbrpos[1] < 0 or nbrpos[0] >= 2*pfmapsize or nbrpos[1] >= 2*pfmapsize or nbrpos == (pfmapsize, pfmapsize)): continue cellcost = costs(cellmap[mapcoord(nbrpos)]) newdist = curdist+cellcost if ((dijkstramap[nbrpos[0]][nbrpos[1]][0] <= newdist and dijkstramap[nbrpos[0]][nbrpos[1]][0] != 0) or cellmap[mapcoord(nbrpos)]['solid'] or cellcost > 8): continue dijkstramap[nbrpos[0]][nbrpos[1]] = [newdist, curpos, False] heapq.heappush(openlist, (newdist, nbrpos)) if not foundtarget: return False while dijkstramap[curpos[0]][curpos[1]][1] != (pfmapsize, pfmapsize): curpos = dijkstramap[curpos[0]][curpos[1]][1] return coords.sum(curpos, (-pfmapsize,)*2)
def getAllPairwiseDistances( coords ): """Use the law of cosines to get the distances bweteen all atoms: d_{ij}^2 = x_i^2 + y_j^2 - 2\dot(x_i, x_j) Returns a matrix D of squared-distances D_ij. """ origin = coords.sum(axis=0)/float(coords.shape[0]) # print 'origin', origin coords = coords - origin # The incoming coords, X, are an N x 3 matrix. transpose_coords = numpy.transpose(coords) # X^T outer = numpy.dot(coords, transpose_coords) # X(X^T) squaredDistances = numpy.zeros( outer.shape ) dotprodrows = numpy.dot( coords**2, numpy.ones( transpose_coords.shape ) ) # M_ij = x_i*x_i dotprodcols = numpy.transpose(dotprodrows) # M_ij = x_j*x_j squaredDistances = dotprodrows + dotprodcols - outer # print 'squaredDistances', squaredDistances return squaredDistances
def getAllPairwiseDistances(coords): """Use the law of cosines to get the distances bweteen all atoms: d_{ij}^2 = x_i^2 + y_j^2 - 2\dot(x_i, x_j) Returns a matrix D of squared-distances D_ij. """ origin = coords.sum(axis=0) / float(coords.shape[0]) # print 'origin', origin coords = coords - origin # The incoming coords, X, are an N x 3 matrix. transpose_coords = numpy.transpose(coords) # X^T outer = numpy.dot(coords, transpose_coords) # X(X^T) squaredDistances = numpy.zeros(outer.shape) dotprodrows = numpy.dot(coords**2, numpy.ones( transpose_coords.shape)) # M_ij = x_i*x_i dotprodcols = numpy.transpose(dotprodrows) # M_ij = x_j*x_j squaredDistances = dotprodrows + dotprodcols - outer # print 'squaredDistances', squaredDistances return squaredDistances
def rendervisible(self, geplayer, extrasprites=[]): sprites = [] sprites += extrasprites for gemgo in self.gemgos: sprite = gemgo.sprite(geplayer) if sprite is not None: sprites.append(sprite) visibleranges = ([],[]) for axis in [0, 1]: if 2*geplayer.visibility + 5 >= self.cellmap.size[axis]: visibleranges[axis].append((0, self.cellmap.size[axis])) else: rmin = (geplayer.position[axis] - geplayer.visibility - 2) % self.cellmap.size[axis] rmax = (geplayer.position[axis] + geplayer.visibility + 2) % self.cellmap.size[axis] if rmin < rmax: visibleranges[axis].append((rmin, rmax)) else: visibleranges[axis].append((rmin, self.cellmap.size[axis])) visibleranges[axis].append((0, rmax)) surface = geplayer.surface for rx in visibleranges[0]: for ry in visibleranges[1]: surface.set_clip( rx[0]*TILESIZE, ry[0]*TILESIZE, (rx[1]-rx[0])*TILESIZE, (ry[1]-ry[0])*TILESIZE) regionsprites = sprites for ix in range(rx[0]-1, rx[1]+1): for iy in range(ry[0]-1, ry[1]+1): regionsprites += self.cellmap.sprites((ix, iy)) if coords.mod((ix, iy), self.cellmap.size) not in geplayer.visibletiles: sprites.append((images.NonVisible, coords.sum(coords.mul((ix, iy), TILESIZE), (-4,-4)), 100)) regionsprites.sort(key=lambda x: x[2]) for sprite in regionsprites: surface.blit(*sprite[:2])
def subtuple(a, b): return coords.sum(a, coords.mul(b, -1))