When I right click an enemy tank, the unit currently selected is supposed to move in and attack, which he does. The problem comes when I tell him to move in and attack a second enemy tank, which he won't do. But he'll go in and attack the first one again. There a way to fix this? Code:

Code:

function enemyEvents()
{
selectMe();
if(event_type == event_rightclick)
{
//breakpoint;
enTarg = my;
attack = 1;
wait(1);
attack = 0;
}
}

action TankTurret_AI
{
my.metal = on;
my.enable_rightclick = on;
my.enable_click = on;
my.event = enemyEvents;
while(you)
{
you.selected = (my.selected == on);
if(enTarg == my) { enTarg = you; }
my.x = you.x;
my.y = you.y;
my.z = you.z;
my.pan = you.pan;
wait(1);
}
ent_remove(me);
}

action Tank_AI //tank used by computer. A7V, Panzer IV, Leopard I, Leopard II, ?
{
my.metal = on;
my.health = 250 + TankBonus;
my.enable_shoot = on;
my.enable_touch = on;
my.enable_click = on; //enable click
my.enable_rightclick = on;
my.enable_detect = on;
my.enable_scan = on;
my.event = enemyEvents; //select unit if he is clicked
my.team = 2; //team is set to 2, ai defaults as an enemy
my.person = 2; //belongs to player two for testing MUST BE SET TO ZERO!!!!
my.class = 2; //is a vehicle
my.subClass = 7; //defaults at WW1 tank (See RTSinfo.qpw)
my.mode = 0; //moving or no?
my.pan = ang(my.pan); //set rotation to -180 . . . +180 range
my.selected = off;
if(curEra == ww1) { ent_create(a7vTurret,my.x,TankTurret_AI); }
if(curEra == ww2) { ent_create(panzTurret,my.x,TankTurret_AI); }
if(curEra == viet) { ent_create(leo1Turret,my.x,TankTurret_AI); }
if(curEra == mod) { ent_create(leo2Turret,my.x,TankTurret_AI); }
if(curEra == maakra) { ent_create(aveTurret,my.x,TankTurret_AI); }
while(my.health > 0)
{
if(my.selected == on) //if selected
{
if(my.selecter != 1) //if doesn't have selection ring
{
ent_create(selectBMP,my.x,selectSprite); //create it
my.selecter = 1; //set selecter to one
}
if(unselect == 1)
{
my.selected = off; //deselect if something else is clicked on
}
}
wait(1);
}
}

action Tank //controls the player's tanks
{
my.metal = on;
my.narrow = on;
my.fat = off;
my.health = 250 + TankBonus; //set health to 250. Since this is the USA, apply a bonus
my.LoS = 500; //set view distance
my.enable_click = on; //enable click
my.enable_detect = on;
my.event = handleTankEvents; //handle events, such as stuck, collision, click
my.team = 1; //team is set to 1, these are the player's units and the player defaults to team 1.
my.person = 1; //used by player one, the human player
my.stance = aggressive; //default stance is agressive
my.class = 2; //is a vehicle
my.subClass = 7; ////define me as a WW1 tank (See RTSinfo.qpw, Unit Attributes)
my.mode = 0; //moving or no?
my.civ = America; //civ is set by the tank factory that creates it. (AMERICAN FOR TESTING)
my.pan = ang(my.pan); //set rotation to -180 . . . +180 range
my.selected = off; //default to unselected
createTurret(my.civ); //create the turret according to civilization and era.
while(my.health > 0)
{
if(degroup == my.group) { my.group = 0; }
if(lasso == 1) { lassoMe(); }
if(selectAllClass == my.subClass) { my.selected = on; }
if(selAll == 1) { my.selected = on; }
if(my.selected == on) //if selected
{
if(my.selecter != 1) //if doesn't have selection ring
{
ent_create(selectBMP,my.x,selectSprite); //create it
my.selecter = 1; //set selecter to one
}
my.selected = (unselect == 0); //deselect if something else is clicked on
if((mouse_right) && (!key_x)) //if rightclicked while selected
{
mouse_trace(); //find coordinate
ent_create(targBMP,target,beacon);
if(you != null)
{
wait(1);
}
vec_set(my.targX,target); //set to unit's targets
my.mode = 1; //set him to move
}
if(key_ctrl) //assign hotkeys on ctrl+#
{
assignHotKeys();
}
if(key_del) { my.health = -50; } //kill on delete command
if(key_s) { my.mode = 0; } //stop the unit on key s
if(key_a) { selectAllOfClass(); } //select all of the same unit on key a
if(key_e) { /*evacuate();*/ } //on e evacuate all units in the tank.
if(attack == 1) { my.mode = 2; } //attack when enemy unit rightclicked
}
//. . . . my.mode 0 through my.mode 1 are controlled in here
if(my.mode == 2) //if attacking
{
if(my.stance == aggressive)
{
if(vec_dist(my.x,enTarg.x) > p1TankRange)
{
moveVehicle(10); //move in range
}
else
{
my.inRange = on; //rotate turret. Turret commences the attack.
}
}
if(my.stance == standGround)
{
if(vec_dist(my.x,enTarg.x) < p1TankRange)
{
my.inRange = on;
}
else
{
my.mode = 0;
}
}

}
wait(1);
}
//run death animation, make passable and remove
ent_remove(me);
}



What happens is this: If the Tank_AI or TankTurret_AI is rightclicked, it is assigned the pointer "enTarg" and the flag attack is set to one. Then the enemyEvents() waits a frame. Meanwhile, all the selected tanks check the attack flag and seeing that it is one, move in to the attack. And enemyEvents(), after that frame, sets attack back to zero again to allow the tanks to stop attacking if they need to and then attack another tank.

And I performed the following test: I rightclicked the first enemy tank, and my tank went in and attacked. I then rightclicked the second tank and my tank didn't attack (contrarily, he drove right up to it). Then I entered "ent_remove(enTarg)" into the command line and it was the second tank that disappeared, rather than the first, so it is not a problem with the pointer (although that showed that I have empty pointer problems to deal with in the future, no biggie)

Last edited by anonymous_alcoho; 08/23/06 00:25.

"Oh no, it's true! I'm a love magnet!" Calvin from Calvin and Hobbes My name's Anonymous_Alcoholic.