void moveunit(unit_type &unitp)
{
double arbx,arby,arbz;
///////////////////////////////action 1////////////////////////////////
if(unitp.action==1)// if unit is in motion
{
for(int ucounter = 0;ucounter<MAX_UNITS;ucounter++)/// go through all units
{
if((distance3d(unit[ucounter].x,unit[ucounter].y,unit[ucounter].z,unitp.x+unitp.dir.vx,unitp.y+unitp.dir.vy,unitp.z+unitp.dir.vz)<2)&&(unit[ucounter].id!=unitp.id))//check for collision
{
if((unit[ucounter].action!=1)&&(distance3d(unit[ucounter].x,unit[ucounter].y,unit[ucounter].z,unitp.targetx,unitp.targety,unitp.targetz)<2))
{/// if the the other unit is standing on the moving units target tell it to move
unit[ucounter].targetx=unit[ucounter].z+(unitp.dir.vz*2);
unit[ucounter].targety=0;
unit[ucounter].targetz=unit[ucounter].x+(unitp.dir.vx*2);
unit[ucounter].temptargetx=unit[ucounter].z+(rand()%5);
unit[ucounter].temptargety=0;
unit[ucounter].temptargetz=unit[ucounter].x+(rand()%5);
unit[ucounter].temptarget=true;
}else/// if the unit is in the way set up temporary target to move to
{
unitp.temptarget=true;/// we have temporary target
unitp.temptargetx=unitp.x+(((unitp.z-unitp.targetz)/distance2d(unitp.x,unitp.z,unitp.targetx,unitp.targetz))*4)+(((rand()%50)/10)-2.5);//calculate temp target
unitp.temptargetz=unitp.z-(((unitp.x-unitp.targetx)/distance2d(unitp.x,unitp.z,unitp.targetx,unitp.targetz))*4)+(((rand()%50)/10)-2.5);//calculate temp target
}
}
}
if(!unitp.temptarget)/// if no temporary target
{
if((model[unitp.modelid].currentFrameIndex<40)||(model[unitp.modelid].currentFrameIndex>45))
{
model[unitp.modelid].SetAnimation(40,45);//set model animation
model[unitp.weaponid].SetAnimation(40,45);
//guideunit(unitp);
}
crossproduct(unitp.dir.vx,unitp.dir.vy,unitp.dir.vz,-unitp.x+unitp.targetx,-unitp.y+unitp.targety,-unitp.z+unitp.targetz,arbx,arby,arbz);// find a 90 degree axis of rotate between the unit direction and the target vector
if(fabs(anglebetween2d(unitp.dir.vx,unitp.dir.vz,unitp.x-unitp.targetx,unitp.z-unitp.targetz)-180)<20){// if we're moving in the correct general direction move fast and rotate slow
unitp.dir.rotateARB(arbx,arby,arbz,1);
unitp.x+=unitp.dir.vx/2;
unitp.z+=unitp.dir.vz/2;
}else// if we're not looking in the right direction turn fast and move slow
{
unitp.dir.rotateARB(arbx,arby,arbz,+5);
unitp.x+=unitp.dir.vx/4;
unitp.z+=unitp.dir.vz/4;
}
if(distance3d(unitp.x,0,unitp.z,unitp.targetx,0,unitp.targetz)<4)//if we've reached the target
{
unitp.action=0;//stop moving
model[unitp.modelid].SetAnimation(1,10);//set animation
model[unitp.weaponid].SetAnimation(1,10);
}
}else//if we have temporary target repeat the same thing for the temporary targets
{
if((model[unitp.modelid].currentFrameIndex<40)||(model[unitp.modelid].currentFrameIndex>45))
{
model[unitp.modelid].SetAnimation(40,45);
model[unitp.weaponid].SetAnimation(40,45);
}
crossproduct(unitp.dir.vx,unitp.dir.vy,unitp.dir.vz,-unitp.x+unitp.temptargetx,-unitp.y+unitp.temptargety,-unitp.z+unitp.temptargetz,arbx,arby,arbz);
if(fabs(anglebetween2d(unitp.dir.vx,unitp.dir.vz,unitp.x-unitp.temptargetx,unitp.z-unitp.temptargetz)-180)<6){
unitp.dir.rotateARB(arbx,arby,arbz,1);
unitp.x+=unitp.dir.vx/2;
unitp.z+=unitp.dir.vz/2;
}else
{
unitp.dir.rotateARB(arbx,arby,arbz,+5);
unitp.x+=unitp.dir.vx/10;
unitp.z+=unitp.dir.vz/10;
}
if(distance3d(unitp.x,0,unitp.z,unitp.temptargetx,0,unitp.temptargetz)<2)//if we reached temporary target move towards original target
{
unitp.temptarget=false;
}
}
}
///////////////////////////////action 1////////////////////////////////
model[unitp.modelid].movemodel(timemove);//animate model
model[unitp.weaponid].movemodel(timemove);
}
|