Skip to content

Commit

Permalink
Use all tuple-manipulations functions for stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Francis Herne committed Nov 7, 2014
1 parent f8fa620 commit 22324ce
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 31 deletions.
16 changes: 7 additions & 9 deletions Bear.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import images
import coords
import MGO

def mindist(a, b, size):
Expand Down Expand Up @@ -28,14 +29,13 @@ def directiontoplayer(self, playerpos):

def mapcoord(pfcoord):
'''Get map coordinate from pathfinder one'''
return ((self.position[0] + pfcoord[0] - self.pfmapsize) % self.cellmap.size[0],
(self.position[1] + pfcoord[1] - self.pfmapsize) % self.cellmap.size[1])
return coords.mod(coords.sum(self.position, pfcoord, (-self.pfmapsize,)*2), self.cellmap.size)

foundtarget = False
dijkstramap = [[[0, (self.pfmapsize, self.pfmapsize), False] for x in xrange(2*self.pfmapsize)] for x in xrange(2*self.pfmapsize)]
dijkstramap = [[[0, (self.pfmapsize,)*2, False] for x in xrange(2*self.pfmapsize)] for x in xrange(2*self.pfmapsize)]
import heapq
openlist = []
heapq.heappush(openlist, (0, (self.pfmapsize, self.pfmapsize)))
heapq.heappush(openlist, (0, (self.pfmapsize,)*2))
curpos = None
while openlist:
curnode = heapq.heappop(openlist)
Expand All @@ -48,7 +48,7 @@ def mapcoord(pfcoord):
continue
else:
dijkstramap[curpos[0]][curpos[1]][2] = True
for nbrpos in [(curpos[0]-1, curpos[1]), (curpos[0], curpos[1]-1), (curpos[0]+1, curpos[1]), (curpos[0], curpos[1]+1)]:
for nbrpos in coords.neighbours(curpos):
if (nbrpos[0] < 0 or nbrpos[1] < 0 or
nbrpos[0] >= 2*self.pfmapsize or nbrpos[1] >= 2*self.pfmapsize or
nbrpos == (self.pfmapsize, self.pfmapsize)):
Expand All @@ -63,8 +63,7 @@ def mapcoord(pfcoord):
return False
while dijkstramap[curpos[0]][curpos[1]][1] != (self.pfmapsize, self.pfmapsize):
curpos = dijkstramap[curpos[0]][curpos[1]][1]
return [curpos[0]-self.pfmapsize,
curpos[1]-self.pfmapsize]
return coords.sum(curpos, (-self.pfmapsize,)*2)

def update(self, player):
playerpos = player.position
Expand Down Expand Up @@ -101,8 +100,7 @@ def randommove():
self._suggestmessage("The bear has lost interest in you", 1)

self.direction = poschange[0] if abs(poschange[0]) else self.direction
newpos = [(self.position[0]+poschange[0]) % self.cellmap.size[0],
(self.position[1]+poschange[1]) % self.cellmap.size[1]]
newpos = coords.mod(coords.sum(self.position, poschange), self.cellmap.size)

if not self.cellmap[newpos]['solid']:
self.position = newpos
Expand Down
8 changes: 3 additions & 5 deletions Dragon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
from directions import *
import images
import coords
import MGO

class Dragon(MGO.GEMGO):
Expand Down Expand Up @@ -30,9 +31,7 @@ def tileoffset(a, b, size):
return offset

def flameplayer():
def addtuple(a, b, c=1):
return (a[0]+b[0]*c, a[1]+b[1]*c)
fronttiles = [addtuple(self.position, self.direction, i) for i in range(1,4)]
fronttiles = [coords.sum(self.position, coords.mul(self.direction, i)) for i in range(1,4)]
for tile in fronttiles:
if tile == tuple(playerpos):
self._suggestmessage("The dragon breaths a jet of fire towards you", 5)
Expand Down Expand Up @@ -66,8 +65,7 @@ def addtuple(a, b, c=1):
if washunting:
self._suggestmessage("The dragon starts to fly away", 1)

self.position = ((self.position[0]+self.direction[0]) % self.cellmap.size[0],
(self.position[1]+self.direction[1]) % self.cellmap.size[1])
self.position = coords.mod(coords.sum(self.position, self.direction), self.cellmap.size)
flameplayer()

def sprite(self, player):
Expand Down
15 changes: 8 additions & 7 deletions Map.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import CellFiller
from colors import *
import directions
import coords
import collectables
import images
import numpy
Expand Down Expand Up @@ -118,7 +119,7 @@ def pickrandomsprite(spritelist):
addsprite(pickrandomsprite(images.Damaged), -3)
if coord in self.fusetiles:
for direction in directions.CARDINALS:
nbrcoord = ((coord[0]+direction[0])%self.size[0], (coord[1]+direction[1])%self.size[1])
nbrcoord = coords.mod(coords.sum(coord, direction), self.size)
if nbrcoord in self.fusetiles or self[nbrcoord]['collectableitem'] == collectables.DYNAMITE:
addsprite(images.Fuse[direction], -2)
if cell['collectableitem'] != 0:
Expand All @@ -128,10 +129,10 @@ def pickrandomsprite(spritelist):
return sprites

def placefuse(self, coord):
self.fusetiles.add((coord[0]%self.size[0], coord[1]%self.size[1]))
self.fusetiles.add(coords.mod(coord, self.size))

def ignitefuse(self, coord):
coord = (coord[0]%self.size[0], coord[1]%self.size[1])
coord = coords.mod(coord, self.size)
if self[coord]['collectableitem'] == collectables.DYNAMITE:
self.detonate(coord)
if not coord in self.fusetiles:
Expand All @@ -142,10 +143,10 @@ def ignitefuse(self, coord):
curpos = openlist.pop()
if curpos in self.fusetiles:
self.fusetiles.remove(curpos)
for nbrpos in [(curpos[0]-1, curpos[1]), (curpos[0], curpos[1]-1), (curpos[0]+1, curpos[1]), (curpos[0], curpos[1]+1)]:
for nbrpos in coords.neighbours(curpos):
if self[nbrpos]['collectableitem'] == collectables.DYNAMITE:
self.detonate(nbrpos)
nbrpos = (nbrpos[0]%self.size[0], nbrpos[1]%self.size[1])
nbrpos = coords.mod(nbrpos, self.size)
if nbrpos in self.fusetiles:
openlist.add(nbrpos)

Expand All @@ -169,7 +170,7 @@ def destroy(self, coord):

def ignite(self, coord, multiplier=1, forceignite=False):
'''Start a fire at coord, with chance cell.firestartchance * multiplier'''
coord = (coord[0]%self.size[0], coord[1]%self.size[1])
coord = coords.mod(coord, self.size)
cell = self[coord]
if coord in self.fusetiles:
self.ignitefuse(coord)
Expand Down Expand Up @@ -201,7 +202,7 @@ def update(self):
'''Spread fire, potentially other continuous map processes'''
for tile in self.burningtiles.copy():
cell = self[tile]
for nbrpos in [(tile[0]-1, tile[1]), (tile[0], tile[1]-1), (tile[0]+1, tile[1]), (tile[0], tile[1]+1)]:
for nbrpos in coords.neighbours(tile):
self.ignite(nbrpos)
if random.random() < cell['fireoutchance']:
cell['burning'] = False
Expand Down
4 changes: 2 additions & 2 deletions Pixie.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import images
import coords
import random
import MGO

Expand Down Expand Up @@ -46,8 +47,7 @@ def randommove():
poschange = randommove()

self.direction = poschange[0] if abs(poschange[0]) else self.direction
newpos = ((self.position[0]+poschange[0]) % self.cellmap.size[0],
(self.position[1]+poschange[1]) % self.cellmap.size[1])
newpos = coords.mod(coords.sum(self.position, poschange), self.cellmap.size)

if self.cellmap[newpos]['solid']:
return False
Expand Down
11 changes: 5 additions & 6 deletions Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def move(self, x, y):
if self.cellmap[self.position[0]+x, self.position[1]+y]['solid'] and not Player.FREEPLAYER:
self.score[collectables.CHOCOLATE] -= 50
return False
self.position = [(self.position[0]+x)%self.cellmap.size[0],
(self.position[1]+y)%self.cellmap.size[1]]
self.position = coords.mod(coords.sum(self.position, self.direction), self.cellmap.size)
collectable = self.cellmap[self.position]['collectableitem']
if collectable != 0:
self.score[collectable] += collectables.value[collectable]
Expand All @@ -77,10 +76,10 @@ def move(self, x, y):

def followpath(self):
def subtuple(a, b):
return (a[0]-b[0], a[1]-b[1])
return coords.sum(a, coords.mul(b, -1))
oldpos = subtuple(self.position, self.direction)
pathnbrs = []
for nbrpos in [(self.position[0]-1, self.position[1]), (self.position[0], self.position[1]-1), (self.position[0]+1, self.position[1]), (self.position[0], self.position[1]+1)]:
for nbrpos in coords.neighbours(self.position):
if (nbrpos == oldpos) or (self.cellmap[nbrpos]['name'] not in ['wooden planking', 'paving']):
continue
pathnbrs.append(nbrpos)
Expand Down Expand Up @@ -108,7 +107,7 @@ def scattercoins(self, radius, number):
tryoffset = random.randint(-radius, radius), random.randint(-radius, radius)
if tryoffset[0]**2 + tryoffset[1]**2 > sqradius:
continue
trypos = (self.position[0]+tryoffset[0], self.position[1]+tryoffset[1])
trypos = coords.sum(self.position, tryoffset)
if self.cellmap[trypos]['collectableitem'] or self.cellmap[trypos]['solid']:
continue
self.cellmap[trypos]['collectableitem'] = collectables.COIN
Expand All @@ -126,7 +125,7 @@ def inrange(a):
trunkpos = self.position
while inrange(trunkpos):
self.visibletiles.add(coords.mod(trunkpos, self.cellmap.size))
for perpdir in directions.perpendiculars(outdir):
for perpdir in perpendiculars(outdir):
diagdir = coords.sum(outdir, perpdir)
branchpos = trunkpos
while inrange(branchpos):
Expand Down
3 changes: 2 additions & 1 deletion coords.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import __builtin__
import directions

def sum(*args):
'''Element-wise sum of (equal-size) tuples'''
return tuple(map(sum, zip(*args)))
return tuple(map(__builtin__.sum, zip(*args)))

def mod(a, b):
'''Element-wise modulo of a with b'''
Expand Down
2 changes: 1 addition & 1 deletion directions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

def perpendiculars(direction):
swapaxes = direction[::-1]
return tuple([swapaxes, coords.multuple(swapaxes, -1)])
return tuple([swapaxes, coords.mul(swapaxes, -1)])

0 comments on commit 22324ce

Please sign in to comment.