Hello

I set the my.enemy handle to the "you" pointer trough doing a c_scan. Now I also need to do a c_trace while "you" is set to the my.enemy handle. This somehow messes up the "you" pointer and the enemy will keep attacking even when "a_dist" is set back to "0" (when the opponent is moving away).

I'd be glad about any help!

Code:

action act_enemy_fight
{
ent_enemy = my;

////collision hull stuff
// my.fat = on;
// my.narrow = off;
c_setminmax(me);
// my.polygon = on;
my.passable = off;

wait(1);//this is needed to have a valid "you" pointer when using the enemy generator

entity_type_settings();

if(my.path_start_node_f == 0){my.path_start_node_f = 1;}
if(!my.norm_state_move_ai){my.norm_state_move_ai = stand_ai;}
if(!my.free_move_area_x){my.free_move_area_x = 500;}
if(!my.free_move_area_y){my.free_move_area_y = 500;}
if(!my.free_move_area_z){my.free_move_area_z = 10;}

////flag/skill settings to start with
my.weapon_do_damage_factor = 0;
my.health = my.initial_health;
my.is_attacking = off;
my.is_under_attack = off;
my.has_path = 0;
my.is_jumping = 0;
my.is_standing = 0;
my.rh_weapon = off;
my.lh_weapon = off;

////show healthbar for boss enemies or normal enemies (above head).Note: only 1 boss enemy per level allowed!!!
if(my.health_bar_type > 0){
if(my.health_bar_type == health_bar_boss_type){
health_bar_boss();
}
if(my.health_bar_type == health_bar_above_head_type){
ent_create (bmp_health_bar_seg, my.pos, health_bar_above_head);
}
}

////enable events
my.enable_scan = on;
my.event = entity_search_scan_event_fight;

entity_initial_weapons();

vec_set (my.initial_pos_x, my.x);
vec_set(my.new_pos_x,my.x);
var i;
var a_dist;

while(my && my.health > 0 )
{
////debug stuff
ifdef drawlines; //to switch off, comment "define drawlines;" in the fight_customize.wdl
if(my.lh_initial_weapon == 0){
vec_for_vertex(my.lh_attack_tracefrom_vec,my,my.lh_weapon_attach_from_vertex);
vec_for_vertex(my.lh_attack_traceto_vec,my,my.lh_weapon_attach_to_vertex);
vec_diff(temp,my.lh_attack_tracefrom_vec,my.lh_attack_traceto_vec);//store difference of distance in temp
effect(weapon_trace_line,1,my.lh_attack_traceto_vec,temp);//draw line of distance
}
if(my.rh_initial_weapon == 0){
vec_for_vertex(my.rh_attack_tracefrom_vec,my,my.rh_weapon_attach_from_vertex);
vec_for_vertex(my.rh_attack_traceto_vec,my,my.rh_weapon_attach_to_vertex);
vec_diff(temp,my.rh_attack_tracefrom_vec,my.rh_attack_traceto_vec);//store difference of distance in temp
effect(weapon_trace_line,1,my.rh_attack_traceto_vec,temp);//draw line of distance
}
endif;

handle_animation(my,1);

if(vec_dist(my.x,player.x) > (my.max_x + ((player.max_x + player.max_y)/2)*2) && vec_dist(my.x,player.x) < my.attack_range * 1.5){// && snd_playing(play_entity_snd_handle) == 0
play_entity_snd();
}

if(my.is_scanning == off)
{
////scan for enemies
scan_enemies();
}

if(my.enemy){

//get a pointer to the enemy!
you = ptr_for_handle(my.enemy);

//if the pointer is valid
if(you){

randomize();

var attack_mode;
attack_mode = int(random(2))+1;

if(vec_dist(my.x,you.x) < (my.max_x + my.max_y)){
a_dist = trace_fromto(my.x,vector(my.x + fcos(my.pan,(my.min_x*(-1))+15),my.y + fsin(my.pan,(my.min_x*(-1))+15),my.z));
}

if(a_dist != 0){

vec_diff(temp.x,you.x,my.x);
vec_to_angle(temp.pan,temp.x);

if(my.health < my.coward_health){
rotate_entity(temp.pan+180,my.entity_turn_speed * 1.3);

my.speed_x = my.entity_move_speed * 2.3 * time_step;
}
else{
rotate_entity(temp.pan,my.entity_turn_speed * 1.3);
}

if(my.is_attacking == off && my.is_under_attack == off){// && player.is_attacking == off

////perform the attack
if(attack_mode == 1){

enemy_attack_a();
}
if(attack_mode == 2){
enemy_attack_b();
}
}
}
else{
////play run animation...
if (my.f_animblend != f_run)//if our current animation is not the one we want...
{
my.f_blendframe = f_run;
}
if(my.health < my.coward_health){

avoid_obstacle(vector(you.x + fcos(you.pan,you.attack_range),you.y + fsin(you.pan,you.attack_range),my.z));

my.speed_x = my.entity_move_speed * 2.3 * time_step;
}
else{
my.speed_x = my.entity_move_speed * 1.6 * time_step;

avoid_obstacle(you.x);
}
}
}

}
else{
////different move ai options when out of attack_range (selectable trough skill15)
if(my.norm_state_move_ai == stand_ai){
my.speed_x = 0;

if (my.f_animblend != f_stand)//if our current animation is not the one we want...
{
my.f_blendframe = f_stand;
}
}

if(my.norm_state_move_ai == free_walk_ai){

if (vec_dist(my.x,my.new_pos_x) < 180) // play with 50
{
waypoint_random_in_area(); // when reached, get new way point
}
if(my.is_standing == 1){
slow_down(0);//parameters are: min_speed
if(my.speed_x < 0.1){
if (my.f_animblend != f_stand)//if our current animation is not the one we want...
{
my.f_blendframe = f_stand;
}
}
}
else{
if(my.entity_gravity == 0){//for flying entitties...changes move speed according to tilt angle
my.speed_x = clamp(my.entity_move_speed * ((my.tilt/2) * (-1)),my.entity_move_speed * 0.5,my.entity_move_speed * 1.2);
}
else{
my.speed_x = my.entity_move_speed * time_step;
}

if (my.f_animblend != f_walk)//if our current animation is not the one we want...
{
my.f_blendframe = f_walk;
}
avoid_obstacle(my.new_pos_x);
}
}

if(my.norm_state_move_ai == use_path_ai){
if(my.entity_gravity == 0){
my.speed_x = clamp(my.entity_move_speed * ((my.tilt/2) * (-1)),my.entity_move_speed * 0.5,my.entity_move_speed * 1.2);
}
else{
my.speed_x = my.entity_move_speed * time_step;
}

if (my.f_animblend != f_walk)//if our current animation is not the one we want...
{
my.f_blendframe = f_walk;
}

walk_path();
}
}


////do not float in the air...exept if wanted!;
if(my.entity_gravity > 0){
attach_to_ground();
}

////perform movement
move_min_z = 0.2;//default = move_min_z = -1...use a small positive value...0.4 to be able to climb only low obstacle
move_friction = 0.1;
result = c_move(my,vector(my.speed_x,my.speed_y,0),vector(my.pushback_speed_x,my.pushback_speed_y,my.speed_z),ignore_passable + ignore_passents + ignore_me + glide + use_box);
// if(result < 0.1 && my.is_under_attack == off && my.is_attacking == off && my.is_jumping == 0 && my.is_standing == 0){
// unstuck();
// }


////kill the "enemy" skill when player is dead
if(player.health <= 0){
my.enemy = null;
}

wait(1);
}

////reset event/triggers
my.enable_scan = off;
my.event = null;

death_pushback();

wait(1);

////play death
my.skill99 = 0;
while(my.skill99 <= 100){
ent_animate(my,"die",my.skill99,0);
my.skill99 += my.entity_death_animspeed * time_step;
wait(1);
}
play_die_snd();

////falling for flying entities
c_trace(my.x,vector(my.x,my.y,my.z-1000),ignore_passable + ignore_passents + ignore_me);
my.passable = on;
my.skill99 = target.z;//skill50 is now free for this
if(my.entity_gravity <= 0){//fall down if it's a flying entity!
while(my.z > my.skill99){
my.z -= 10 * time_step;
wait(1);
}
my.passable = off;
}

my.alpha = 100;
my.transparent = on;

my.skill99 = my.z;//skill50 is now free for this
while(my.alpha > 0)
{
my.alpha -= time_step;
// if(my.alpha < 90){
my.z = my.skill99;
my.passable = on;
// }
wait(1);
}
ent_remove(me);
me = null;
boss_health_bar.visible = off;
dead_enemies += 1;
num_enemies -= 1;
if(dead_enemies == 5)
{
wait(-2);
// exit;
}
}