Difference between revisions of "Talk:Complete Roguelike Tutorial, using python+libtcod, part 6"

From RogueBasin
Jump to navigation Jump to search
(Created page with 'Did anybody else think of the Ravenous Bugblatter Beast of Traal when they read the AI section?')
 
Line 1: Line 1:
Did anybody else think of the Ravenous Bugblatter Beast of Traal when they read the AI section?
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:
 
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">       
    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(round(dx))
        dy = int(round(dy))
        self.move(dx, dy)
</syntaxhighlight></div>
 
--[[User:High priest of Ru|High priest of Ru]] ([[User talk:High priest of Ru|talk]]) 14:48, 21 May 2018 (CEST)

Revision as of 12:48, 21 May 2018

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(round(dx))
        dy = int(round(dy))
        self.move(dx, dy)

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