def find_line(coords1, coords2): '''Converts two x, y coords into a list of tiles. coords1 : (x1, y1) coords2 : (x2, y2) ''' x1, y1 = coords1 x2, y2 = coords2 libtcod.line_init(x1, y1, x2, y2) calc_x, calc_y = libtcod.line_step() coord_list = [] if x1 == x2 and y1 == y2: return [(x1, y1)] while (not calc_x is None): coord_list.append((calc_x, calc_y)) calc_x, calc_y = libtcod.line_step() return coord_list
def test_line_step(): """ libtcodpy.line_init and libtcodpy.line_step """ libtcodpy.line_init(*LINE_ARGS) for expected_xy in EXCLUSIVE_RESULTS: assert libtcodpy.line_step() == expected_xy assert libtcodpy.line_step() == (None, None)
def _draw_silhouettes(self, pc, tx,ty, ent, sight): ''' # extend a line from tile tx,ty to a distant tile # which is in the same direction from the player. # Check for lit tiles, and if we find any along the way, # draw a silhouette for the location of interest. # Basically, if the ent is backlit, you can see # a silhouette. ''' world=rog.world() if not sight: return if not (ent and world.has_component(ent, cmp.Creature)): return pos=world.component_for_entity(ent, cmp.Position) dist=maths.dist(pos.x,pos.y, tx,ty) dx=(tx - pos.x)/dist dy=(ty - pos.y)/dist xdest=tx + int(dx*sight) ydest=ty + int(dy*sight) libtcod.line_init(tx,ty, xdest,ydest) while True: x,y=libtcod.line_step() if x is None: return if maths.dist(pos.x,pos.y, x,y) > sight: return if self.get_blocks_sight(x,y): return if self.get_perceived_light_value(x,y): libtcod.console_put_char(self.con_map_state, tx,ty, "?") return
def generate_rivers(self): river_id = 0 # Source = sx, sy # Destination = dx, dy # Pick a random point near the coast sx = random.randint(0, self.width) sy = random.randint(self.height / 5, 4 * self.height / 5) h = self._hm[sy, sx] while (h < SAND_HEIGHT - 0.02) or (h >= SAND_HEIGHT): sx += 1 if sx == self.width: sx = 0 sy += 1 if sy == self.height: sy = 0 h = self._hm[sy, s] tree, rand_pt = [], [] tree.insert(0, (sx, sy)) river_id += 1 dx, dy = sx, sy for i in range(random.randint(50, 200)): rx = random.randint(sx - 200, sx + 200) ry = random.randint(sy - 200, sy + 200) rand_pt.insert(0, (rx, ry)) for (rx, ry) in rand_pt: min_dist = 1E10 best_x, best_y = -1, -1 for (tx, ty) in tree: dist = (tx - rx) * (tx - rx) + (ty - ry) * (ty - ry) if dist < min_dist: min_dist = dist best_x, best_y = tx, ty tcod.line_init(best_x, best_y, rx, ry) len, cx, cy = 3, best_x, best_y md = self.map_data[cx, cy] if md.river_id == river_id: md.river_id = 0 while len > 0: md = self.map_data[cy, cx] if md.river_id > 0: break h = self._hm[cy, c] if h >= SAND_HEIGHT: md.river_id = river_id self._precipitation[cx, cy] = 1.0 if cx == 0 or cx == self.width - 1 or cy == 0 or cy == self.height - 1: len = 0 else: cx, cy = tcod.line_step() if cx is None or cy is None: len = 0 len -= 1 if (cx + cy * self.width != best_x + best_y * self.width): tree.insert(0, (cx, cy))
def map_find_line(coords1, coords2): '''Converts two x,y coords into list of tiles''' x1, y1 = coords1 x2, y2 = coords2 libtcodpy.line_init(x1, y1, x2, y2) cur_x, cur_y = libtcodpy.line_step() coord_list = [] if x1 == x2 and y1 == y2: return [coords1] while (not cur_x is None): coord_list.append((cur_x, cur_y)) cur_x, cur_y = libtcodpy.line_step() return coord_list
def _draw_distant_lights(self, pc, view_x,view_y,view_w,view_h): pcPos=rog.world().component_for_entity(pc, cmp.Position) for light in rog.list_lights(): lx=light.x ly=light.y if (lx == pcPos.x and ly == pcPos.y): continue if not (lx >= view_x and ly >= view_y and lx <= view_x + view_w and ly <= view_y + view_h ): continue libtcod.line_init(pcPos.x,pcPos.y, lx,ly) canSee=True while True: x,y=libtcod.line_step() if x == None: break; if self.get_blocks_sight(x,y): canSee=False;break; if canSee: libtcod.console_put_char(self.con_map_state, lx,ly, "?")