Author Topic: Find all 'objects' in an area (python+libtcod)  (Read 6785 times)

MisterMoxxie

  • Newcomer
  • Posts: 19
  • Karma: +0/-0
    • View Profile
    • Email
Find all 'objects' in an area (python+libtcod)
« on: April 24, 2012, 02:01:23 AM »
Hello! I've been following the 'complete roguelike tutorial using Python and Libtcod' tutorial, and it's been wonderful.

I finished the whole of the tutorial, and have since begun rewriting the AI. I want to create a need-driven AI, but I'm having a bit of trouble. I'm trying to use the FOV kit within libtcod to create a field of view from the creature, and then create a list of all of the objects that creature can see.

Here's what I have so far.

Code: [Select]
class AI_Evaluate
#AI Component - Creature is evaluating it's situation

objects = [] #a list of all the objects the creature can see

#get an FOV for the creature
my_fov = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
libtcod.map_set_properties(my_fov, self.x, self.y, not map[x][y].block_sight, not map[x][y].blocked)

for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
#visible = libtcod.map_is_in_fov(my_fov, x, y)
#if visible:
#if there is an object here:
#objects.append(object)

any ideas?

guest509

  • Guest
Re: Find all 'objects' in an area (python+libtcod)
« Reply #1 on: April 24, 2012, 05:02:06 AM »
  Is there an error here? Or is it just not working? What is the behavior? What's the problem specifically?

EDIT: I guess your algorithm seems sound. I don't know enough about this language though, sorry.

MisterMoxxie

  • Newcomer
  • Posts: 19
  • Karma: +0/-0
    • View Profile
    • Email
Re: Find all 'objects' in an area (python+libtcod)
« Reply #2 on: April 24, 2012, 08:43:02 AM »
Well the last few lines, after 'for x in range(MAP_HEIGHT_:' are all pseudo-code, commented out. I think I may have figured it out though, trying this where 'objects' is a global list of all the objects on the current dungeon level.

Code: [Select]
class AI_Evaluate:
    #AI Component - Creature is evaluating it's situation
    creature = self.owner

    myobjects = [] #a list of all the objects the creature can see
   
    #get an FOV for the creature
    my_fov = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)
    for y in range(MAP_HEIGHT):
        for x in range(MAP_WIDTH):
for object in objects:
if self.x = x:
if self.y = y:
myobjects.append(object)

for object in myobjects:
if object != None:
#assign need values

MisterMoxxie

  • Newcomer
  • Posts: 19
  • Karma: +0/-0
    • View Profile
    • Email
Re: Find all 'objects' in an area (python+libtcod)
« Reply #3 on: May 08, 2012, 09:16:47 AM »
So after working on this for a while, I've come up with a solution (possibly). Libtcod has a built in line-drawing function. So I was thinking during the AI loop, draw a line from the creature to each object in the dungeon. If there are no obstructions then that creature can see that item. Here's what I have so far, but when I run it, the game freezes. I'm not sure if the loop is infinite maybe, or if it's just THAT much overhead?! Sorry, I'm still very new to Python.

Code: [Select]
def CanSee(self, o):
    #Returns true if this fighter can see the object
        
        monster = self.owner
        
        if o.fighter:
            Object = o.owner
        else:
            Object = o
            
        libtcod.line_init(monster.x, monster.y, Object.x, Object.y)
        
        x,y=libtcod.line_step()
        
        while (not x is None ):
            for object in objects:
                if map[x][y].blocked == False:
                    if object.x == x and object.y == y:
                        return True
            x,y=libtcod.line_step()
                        
        return False
« Last Edit: May 08, 2012, 11:38:16 PM by MisterMoxxie »