Targeting System for Custom_entities, NPC's and Enemies

Started by polyglot762, August 06, 2015, 10:00:51 PM

Previous topic - Next topic
Hello Again,
I have been working steadily on methods to create a targeting system for my custom entities and enemies.
Is there any way to target map entities with out giving them a name?
That right now is the biggest hurdle I have come across.

Currently, the system I am working on uses collision detection with an invisible circular sprite to create the illusion of a line of sight search...
But this is just a work around...
Any suggestions? Better ideas? Things I'm missing?
Thanks.

For custom entities you can use a collision test of type function (the last one).

Another solution, that can be used also with enemies, is using a timer with a function:

--start the timer in this line
  for other_entity in map:get_entities("") do
    -- do here what you need...
  end
--end the timer in this line

With a function like this you can, for instance, find the closest entity (of certain type) to your enemy, etc.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Thanks for the help... I went a different direction and just decided to have proceedural entity naming for targeting.
It took me the better part of two days to get it working once I got my head wrapped around the base code.
The code can be inserted into any type of entity that can be searched for... It was well worth the time invested... thanks for the quick reply earlier.
I hope that others can be able to use this code.
NOW... I can get to the business of creating more complex AI's and NPC interactions and reactions 8)
local target_dt = nil
local target_dh = nil
local target_dth = nil
  local entity_slots = {}
  for i = 1, 25 do
    local hero = game:get_map():get_entity("hero")
    local hero_x, hero_y, hero_layer = hero:get_position()
    local self_x, self_y, self_layer = entity:get_position()
 
    local distance_hero = math.abs((hero_x+hero_y)-(self_x+self_y))
    local target = game:get_map():get_entity("e_" .. i .. "")
    if target ~= nil then
      entity_slots[i] = target
      print("target ID")
      print(entity_slots[i])       
      local sort_target = target:get_type()
      entity_slots[i] = sort_target
      print(entity_slots[i])
      local breed = target:get_breed()
        entity_slots[i] = breed
        print("breed ID")
        print(entity_slots[i])
     
      local t_x, t_y, t_layer  = target:get_position()
        entity_slots[i] = t_x
        print(entity_slots[i])
        entity_slots[i] = t_y
        print(entity_slots[i])
        entity_slots[i] = t_layer
        print(entity_slots[i])
       
      local distance_target = math.abs((t_x+t_y)-(self_x+self_y))
         entity_slots[i] = distance_target
        print(entity_slots[i])
       
      local distance_to_hero = math.abs((hero_x+hero_y)-(t_x+t_y))
         entity_slots[i] = distance_to_hero
         print(entity_slots[i])
     
      print("B_sight")
      print("B_targeting_started...")
     
      if distance_target <= 100 then
        target_dt = ("e_" .. i .. "")
        print(target_dt)
        print("B_targeting_dt")
      elseif distance_to_hero >= 100 then
        target_dth = ("e_" .. i .. "")
        print(target_dth)
        print("B_targeting_dth")
     
      end
      print("B_targeting_finished...")
end
end

  if target_dt ~= nil then
  entity:go_enemy() -- Not a real function... you have to make you own.
end

I did have some trouble with the ~= nil aspect... but at least this search target code comes up with a name that you can target that satisfies the variables. ;D 8)


Just something I noticed while reading your code: I don't understand your distance computation, I don't think it works.
There is a function entity:get_distance(other) in the API that does the work for you.
If you prefer a manhattan distance, it should be math.abs(t_x - self_x) + math.abs(t_y - self_y).

How do you implement entity:get_position(other) ?

I haven't been able to find anything on it in the documentation api on the website...
I bet I'm not looking in the right place.

Thanks for the help.

I don't understand exactly what you mean by implementing entity:get_position(other). Maybe what you are looking for is the function entity:set_position(x, y, [layer]). You can find it here:
http://www.solarus-games.org/doc/latest/lua_api_entity.html
(Look on the links on the left of the Lua API website, where you can choose other types of objects to see their funcions.)
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Thanks Chrostopho and Diarandor.
I'm fixing the distance calc.
And investigating the "entity:get_position(other)" function...
What I mean my implementing I think would actually be better described as putting it into my code and debugging how to use the code...
Maybe that is just me.
Thanks So much.

Also... If anyone has a code example using the "other" variable that works. I would very much like to see it and reduce the code monster that I am presently using.
Thanks.