def randomDigWalk(self, mob ): digtiles = [] goodtiles = [] for tile in mob.tile.neighbours(): if not tile.cannotEnterBecause( mob ): goodtiles.append( tile ) elif tile.diggable() and tile.impassable: digtiles.append( tile ) if digtiles and random.randint(0,1) > 0: from level import makeFloor digtile = random.choice( digtiles ) mob.logVisualMon( "%s digs out a cave wall." ) makeFloor( digtile ) mob.moveto( digtile ) elif goodtiles: tile = random.choice( goodtiles ) mob.moveto( tile )
def doChokepointSleeping(self, mob ): isChokepoint = lambda tile : (tile.mobile == mob or not tile.cannotEnterBecause(mob)) and len([n for n in tile.neighbours() if not n.impassable]) == 2 if isChokepoint( mob.tile ): return path = seekGoal(mob, isChokepoint, self.radius, openDoors = True ) if not path: doRandomWalk( mob ) else: try: step = path[1] except IndexeError: return if step.isDoor: from level import makeFloor mob.logVisualMon( "%s knocks down the door." ) makeFloor( step ) else: mob.moveto( step )
def cast(self, context): from level import makeFloor from traps import TrapDoor, clearTraps context.log( "Dig in which direction?" ) dxdy = context.game.main.query( DirectionWidget, acceptZ = True ) nodig = [ "stairs up", "stairs down" ] if dxdy == '<': tile = context.player.tile prevLev = tile.level.previousLevel if not prevLev: context.log( "You fail to dig through the ceiling." ) else: tileAbove = prevLev.randomTile( lambda tile : not tile.impassable and not tile.trap and not tile.mobile and not tile.name in nodig ) td = TrapDoor( tileAbove, context = context ) td.difficulty = 0 td.setTarget( tile ) context.log( "You dig through the ceiling!" ) items = tileAbove.items tileAbove.items = [] if items: tile.level.scatterItemsAround( items, (tile.x, tile.y) ) msg = [ capitalizeFirst( grammar.makeCountingList( countItems( items ) ) ), "fall" if len(items) > 1 else "falls", "through the hole!" ] context.log( " ".join( msg ) ) elif dxdy == '>': tile = context.player.tile nexLev = tile.level.generateDeeperLevel() if nexLev and tile.name not in nodig: clearTraps( tile ) td = TrapDoor( tile, context = context ) td.difficulty = 0 items = tile.items tile.items = [] target = td.getTarget() context.log( "You dig through the floor!" ) if items: tile.level.scatterItemsAround( items, target ) msg = [ capitalizeFirst( grammar.makeCountingList( countItems( items ) ) ), "fall" if len(items) > 1 else "falls", "through the hole!" ] context.log( " ".join( msg ) ) tile.enters( context.player ) # trigger? might not, flying etc. else: context.log( "You fail to dig through the floor." ) else: rayLength = 8 region = context.game.showStraightRay( (context.player.tile.x,context.player.tile.y), dxdy, rayLength, 'black', 'magenta', lambda (x,y) : not context.player.tile.level.tiles[x,y].diggable() ) for x, y in region: try: tile = context.player.tile.level.tiles[x,y] if tile.diggable() and tile.impassable: makeFloor( tile ) if tile.mobile and tile.mobile.destroyedByDigging: tile.mobile.killmessage() tile.mobile.kill() except KeyError: # shouldn't happen break