Talk:Complete Roguelike Tutorial, using python+libtcod, part 6

From RogueBasin
Jump to: navigation, search

Guys, i have a little advice for AI section.

When we are setting properties of our object, we are creating wrong movement rules (which allows monsters to run into 4 directions instead of 8). Player will broke 4-dir chains later, but now i propose a little piece of code which helps the reader to make good chasing algorithm.

As you can see bellow, i am using rude algorithm instead of "round" operation as this one doesn't allow us to make proper 8-direction integer.

Instead of move toward section we should include this into our code:

 
    def move_towards(self, target_x, target_y):
        #vector from this object to the target, and distance
        dx = target_x - self.x
        dy = target_y - self.y
        distance = math.sqrt(dx ** 2 + dy ** 2)
 
        #this returns number based on result of div operation. then taking itsinteger.
        dx = (dx / distance)
        if dx > 0:
            dx = 1
        if dx < 0:
            dx = -1
        dy = (dy / distance)
        if dy > 0:
            dy = 1
        if dy < 0:
            dy = -1		
 
        dx = int(dx)
        dy = int(dy)
        self.move(dx, dy)

--High priest of Ru (talk) 14:48, 21 May 2018 (CEST)

Personal tools