Page 1 of 2

How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 2:11 am
by montagneyaya
I will verify if there is a monster on a tile, I use entitiesAt(level, x, y) but how i see if the iterator is a monster ?

The only possibility I know

Code: Select all

monsters_list = {"snail", "crowern", "wyvern", "other_monster"}
for i in entitiesAt(1,15,15) do
    for j in monsters_list do
        if i.name == j then
            print("monster")
        end
    end
end
List all monster is laborious, there is no other possibility ?

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 2:16 am
by Neikun
I wonder if there's a way to list them by class.

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 2:25 am
by montagneyaya
All entities have the following properties that can be accessed with the dot operator:

“name” returns the name of the entity
“id” returns the unique identifier of the entity
“x” returns the x-coordinate of the entity on the map
“y” returns the y-coordinate of the entity on the map
“facing” returns the facing of the entity (0=north, 1=east, 2=south, 3=west)
“level” returns the dungeon level index of the entity
No class :(

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 2:26 am
by Komag
I don't know of any other way, but you should only have to do it once, and then you can refer to the list as many times as you need in your dungeon

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 2:40 am
by Lark
montagneyaya wrote:I will verify if there is a monster on a tile, I use entitiesAt(level, x, y) but how i see if the iterator is a monster ?
I had the same problem looking for monsters and other things. The trick is to find a keyword or method that is only attached to the entities in which you're interested. For example, only monsters have the setAIState method. So if you iterate over the entities returned and eleminate those without this method, you'll have a list of monsters. You might also want to filter on other things like finding all objects in a square - so look only for objects with the getWeight method. You can look at the entity name too, for example, and find all doors, but finding all monsters or objects without looking at methods is difficult. You can write a dedicated routine for each requirement, but I wrote a generic find and filter routine instead which is found at viewtopic.php?f=14&t=3985&p=41224&hilit=Lark#p41224 that will do the trick.

To find all monsters, just use monsters = findAll(self.level, self.x, self.y, {":setAIState"})

See the original post for more details and for the code.

Have Fun! -Lark

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 2:47 am
by Neikun
Oh that Lark and his /infinite genius/
uncompromisingly stubborn
<3

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 3:05 am
by montagneyaya
Thanks for your answers.

I test my script and I have an error :

Code: Select all

for j in monsters_list do    attempt to call a table value
I test other simple script with table

Code: Select all

function OpenAll()
	list_of_gates = {"portcullis_1", "portcullis_2", "portcullis_3"}
	for i in list_of_gates do
		i:open()
	end
end
and have the same error, why have I the error "attempt to call a table value" ?

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 4:18 am
by Lark
The problem here is that you have a table that contains the ids of the entities that you want, but not the entities themselves. Try this (untested) code:

Code: Select all

function OpenAll()
   list_of_gates = {"portcullis_1", "portcullis_2", "portcullis_3"}
   for i in list_of_gates do
      entity = findEntity(i)
      if entity ~= nil then entity:open() end
   end
end
Good Luck, -Lark

P.S. The FindAll function referenced above returns entities, not ids, so you won't have that problem if you use that function.

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 4:21 am
by Lark
Neikun wrote:Oh that Lark and his infinite genius <3
Thank you :oops: , but you should replace "infinite genius" with "uncompromisingly stubborn" to get closer to the truth! Hey, I like programming! :geek: -Lark

Re: How verify if there is a monster on a tile ?

Posted: Thu Nov 15, 2012 4:38 am
by Neikun
If you say so~ haha.
I will continue to revere you as one of the most creative scripters this community's got.