C
Carbiner
Guest
I have a problem with teams in my Ai. Both the Ai and the player have a variable "team" which is an integer. What I want to have happen is that the Ai will "target" the nearest instance of "par_actor" that it can see. "par_actor" is the parent of both the Ai and the player.
The first solution that I tried was an extension to find the nearest instance with a variable and a comparison. The extension was out of date and even when I removed the code that was outdated, It told the Ai to target itself, regardless of "team".
My next step came from looking at the code for the extension more closely. From this I made a ds_priority list for each Ai which would store the id's of every other "par_actor" that have a different team from the caller, priority based on distance to the caller, and then run that through the sight script. That worked better than before. They now do not shoot at themselves, but most just stand there. The ones that do shoot have no regard for any team, nor even if what they are shooting at. It is just as likely their target is a "par_actor" or the walls and control objects.
I feel rather lost at the moment and would like some help. My code for sight is:
The code I am using is a modified version of a tutorial on sight cones.
My array code is:
Not sure if it will help but my step code for the obj_ai is:
I would really appreciate some help with this.
The first solution that I tried was an extension to find the nearest instance with a variable and a comparison. The extension was out of date and even when I removed the code that was outdated, It told the Ai to target itself, regardless of "team".
My next step came from looking at the code for the extension more closely. From this I made a ds_priority list for each Ai which would store the id's of every other "par_actor" that have a different team from the caller, priority based on distance to the caller, and then run that through the sight script. That worked better than before. They now do not shoot at themselves, but most just stand there. The ones that do shoot have no regard for any team, nor even if what they are shooting at. It is just as likely their target is a "par_actor" or the walls and control objects.
I feel rather lost at the moment and would like some help. My code for sight is:
Code:
///scr_ai_sight();
// Do we have line of sight?
with(array)
{
var inst = id;
if(inst != noone)
{
if(!collision_line(other.x, other.y, inst.x, inst.y, obj_wall, false, true))
{
//Set vision cone
other.cone_left = other.rot+other.sight_cone_left;
if(other.cone_left > 359) { other.cone_left = other.cone_left-359 }
other.cone_right = other.rot+other.sight_cone_right;
if(other.cone_right < 0) { other.cone_right = other.cone_right+359 }
// Get angle to player
other.p_angle = point_direction(other.x, other.y, inst.x, inst.y);
// Is the player in the cone?
if(other.cone_right > other.cone_left)
{
if(other.p_angle <= other.cone_left or other.p_angle >= other.cone_right) { other.in_cone = true }
else{ other.in_cone = false }
}
else
{
if(other.p_angle <= other.cone_left and other.p_angle >= other.cone_right) { other.in_cone = true }
else{ other.in_cone = false }
}
// Do stuff if they are
if(other.in_cone)
{
// Get the distance to the player
other.dist_to_enemy = point_distance(other.x,other.y, inst.x, inst.y);
// Check if you can see them
if(other.dist_to_enemy <= other.sight_range) {
return inst;
}
else
{
return noone;
}
}
else
{
return noone;
}
}
else
{
return noone;
}
}
else
{
return noone;
}
}
My array code is:
Code:
///scr_ai_database();
ds_priority_clear(array);
with(obj_ai)
{
if(other.team != team and other.team != 0)
{
ds_priority_add(other.array,id,point_distance(x,y,other.x,other.y));
}
}
if(target != par_actor)
{
ds_priority_delete_value(array,target);
}
if(target = id)
{
ds_priority_delete_value(array,target);
}
Code:
scr_ai_database();
target = scr_ai_sight();
if(target = self)
{
target = noone;
}
scr_ai_state();
scr_ai_shoot(obj_bullet,8);
scr_ai_movement();
if(hp <= 0)
{
instance_destroy();
}
ammo = 120;