from .. import Region, Node, Ifc, Utl, Map, g from ..data import Species def SpanningRegion(origin, region, direction): dists = {} for n in g.deadends: d = Map.distance(origin, n) if not d in dists: dists[d] = [] dists[d].append(n) _dists = dists.keys() avg = sum(_dists) / float(len(_dists)) _dists = [x for x in _dists if x > avg] destination = Utl.rn_seq(dists[Utl.rn_seq(_dists)]) g.deadends.remove(destination) #destination = g.deadend() destination = Region.RegionBridge(src=destination) g.grid[destination.pos] = destination origin.name = destination.name = "Open Pipe" origin.color = destination.color = Ifc.BRIGHT_BLUE origin.unique = destination.unique = False origin.description = destination.description = \ """The tunnel dead-ends with a large open steam pipe leading away into darkness.""" destination.bridge_region = region xdist = origin.x - destination.x ydist = origin.y - destination.y phase, curdir = 0, direction direc1, direc2 = 2 if ydist <= 0 else 0, 1 if xdist <= 0 else 3 xdist, ydist = abs(xdist), abs(ydist) if xdist == ydist: xdist = ydist = (xdist / 3) + 1 elif xdist < ydist: xdist, ydist=(xdist / 3) + 1, (xdist + 1) * ((ydist / (xdist + 1)) + 1) elif ydist < xdist: xdist, ydist=(ydist + 1) * ((xdist / (ydist + 1)) + 1), (ydist / 3) + 1 oldnode, newnode = origin, None while xdist + ydist > 0: newnode = SpanningTunnel(region=region) Region.makelink(newnode, oldnode, curdir) if curdir in (1, 3): xdist -= 1 else: ydist -= 1 if phase == 0: if curdir in (1, 3): curdir = direc1 else: curdir = direc2 phase = 1 elif phase == 1: if Utl.rn() and xdist: curdir = direc2 elif ydist: curdir = direc1 else: curdir = direc2 oldnode = newnode if (curdir == 0 and destination.south) or (curdir == 2 and destination.north): curdir = direc2 elif (curdir == 1 and destination.west) or (curdir == 3 and destination.east): curdir = direc1 Region.makelink(destination, newnode, curdir) class SpanningTunnel(Node.Node): def xinit(self): self.color = Ifc.BLUE self.description = "The pipe stretches on in front of and behind you." self.name = "Steam Tunnel" self.tunnel = True if Utl.rn(): self.add(Utl.rn_seq(Species.span_spawn).make())