Sign in to follow this  
corner

problem getting units to patroll

Recommended Posts

corner    122
everytime i try to get my unit to patroll it moves in one direction how can i rectify this problem //this is all in one function void patrol(int ally, int foe) int action[]={1,2,3,4}; for(int x=0;x<4;x++) { if(action[x]==1){ obj[ally].positionY+=100; } if(action[x]==2){ obj[ally].positionX+=100; } if(action[x]==3){ obj[ally].positionY-=100; } if(action[x]==4){ obj[ally].positionX-=100; }

Share this post


Link to post
Share on other sites
jyk    2094
1. Be sure to paste in your actual code (what you posted contains at least one syntax error).

2. When posting code, use [source] tags to preserve formatting.

Share this post


Link to post
Share on other sites
corner    122



#include<allegro.h>
#include<math.h>

int ResX=1024,ResY=768;

BITMAP *screen_buffer;
BITMAP *sprite[10];
const int Max_objs=20;//2048;
bool timerFlag=false;



const double pixelsPWCoord= 256;
double cameraX=0;
double cameraY=0;

/*double mapMax= resX * (pow (pixelsPerWorldCoord ,2))/2;
double mapMin= - (resX * (pow (pixelsPerWorldCoord ,2))/2);
*/



struct objects
{
int ptrSprite;
int health;
int rank;
int firePower;
int positionX;
int positionY;
int speed;
int desX;
int desY;
int fuelRange;
int numUnits;
int targetY;
int targetX;
int mapPos;
int missile;
int laser;
int bullets;
int oreRange;
char status;
int size;
int type;
int angle;
int AItype;
int sides;//friend 1,foe1=2 ,foe2=3
int AIstate;//1=attack, 2=divert,3=retreat, 4 =track
};

objects obj[Max_objs];

void spawn_inventory()
{
for(int i=0;i<Max_objs;i++)
if(obj[i].status == 0)
{
obj[i].status =1;
obj[i].AItype=3;
obj[i].size = 64;
obj[i].angle=0;
obj[i].speed=3*256;
obj[i].ptrSprite=3;
obj[i].positionX =40*256;
obj[i].positionY =60*256;

obj[i].sides=1;//1=friend, 2=enemy,3=neutral

i=Max_objs;
}


}

void spawn_explosion(double posX,double posY)
{

for(int i=0;i<Max_objs;i++)
{
if(obj[i].status == 0)
{
obj[i].status = 1;
obj[i].AItype=1;
obj[i].size = 64;
obj[i].angle=0;
obj[i].ptrSprite=1;
obj[i].positionX = posX;
obj[i].positionY = posY;
obj[i].speed=3;//256
i=Max_objs;
}
}
}


void spawn_bullet(int x1,int y1)
{
for(int i=0;i<Max_objs;i++)
if(obj[i].status == 0)
{
obj[i].status = 1;
obj[i].AItype=5;
obj[i].size =8;
obj[i].angle=90;
obj[i].ptrSprite=5;
obj[i].positionX=x1;
obj[i].positionY=y1;

obj[i].speed =10*256;
//1=friend, 2=enemy,3=neutral

i=Max_objs;
}
}

void spawn_foe()
{
for(int i=0;i<Max_objs;i++)
if(obj[i].status == 0)
{
obj[i].status =1;
obj[i].AItype=4;
obj[i].size = 64;
obj[i].angle=0;
obj[i].ptrSprite=4;
obj[i].positionX =650*256;
obj[i].positionY =100*256;
obj[i].health;
obj[i].rank;
obj[i].AIstate=0;
obj[i].sides=2;//1=friend, 2=enemy,3=neutral
obj[i].speed=3;//256
i=Max_objs;
}

}

void setTimerFlag()
{
timerFlag=true;
}

void init()
{
allegro_init();
install_timer();
install_mouse();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT,ResX,ResY,0,0);
show_mouse(screen);
install_int_ex((*setTimerFlag), BPS_TO_TIMER(200));
screen_buffer=create_bitmap(ResX,ResY);
select_mouse_cursor(MOUSE_CURSOR_ALLEGRO);
}

double range(int i,int target)
{
double dX=obj[i].positionX-obj[target].positionX;
double dY=obj[i].positionY-obj[target].positionY;
return sqrt((dX*dX)+ (dY*dY));
}

double rangeToDestination(int i)
{
double dX=obj[i].desX-obj[i].positionX;
double dY=obj[i].desY-obj[i].positionY;
return sqrt((dX*dX)+ (dY*dY));
}


void AICalcXY(int i)
{
double dx,dy;
dx=sin(obj[i].angle);
dy=cos(obj[i].angle);


dx*=obj[i].speed;
dy*=obj[i].speed;

obj[i].positionX+=dx;
obj[i].positionY+=dy;

//rest(100);

}

void AICalcAngle(int i)
{

int dx=obj[i].desX-obj[i].positionX;
int dy=obj[i].desY-obj[i].positionY;


if(dx==0)dx=1;
if(dy==0)dy=1;

double newAngle=atan2(dy,dx);

newAngle=(newAngle/M_PI)*180;

newAngle-=270;

while(newAngle<0)
{
newAngle+=360;
}

obj[i].angle=newAngle;

dx+=(sin(obj[i].angle*obj[i].speed));
dy+=(sin(obj[i].angle*obj[i].speed));



obj[i].positionX=dx;
obj[i].positionY=dy;

}

void fire(int ally,int foe)
{

spawn_bullet(obj[ally].positionX,obj[ally].positionY);
obj[ally].desX=obj[foe].positionX;
obj[ally].desY=obj[foe].positionY;
}

void divert(int ally,int foe)
{
if(obj[ally].positionX<obj[foe].positionX)
obj[ally].positionX-=100;
if(obj[ally].positionX>obj[foe].positionX)
obj[ally].positionX+=100;


}

void patrol(int ally,int foe)
{
int action[]={1,2,3,4};
for(int x=0;x<4;x++)
{
if(action[x]==1){
obj[ally].positionY+=100;
}
if(action[x]==2){
obj[ally].positionX+=100;
}
if(action[x]==3){
obj[ally].positionY-=100;
}
if(action[x]==4){
obj[ally].positionX-=100;
}


if(range(ally,foe)<100)
{
obj[ally].AIstate=1;//fire
x=Max_objs;
}

}

}

void retreat(int ally,int foe)
{

}

void aiBullet(int i)
{

AICalcXY(i);
AICalcAngle(i);

obj[i].angle=+100;

if(rangeToDestination(i)<100)
{

obj[i].status=0;
}

}

void aiInventory(int i)
{
for(int x=0;x<Max_objs;x++)
{
/* if(obj[i].sides!=obj[x].sides&&range(x,i)<10)
{
spawn_explosion(obj[i].positionX,obj[i].positionY);
obj[i].status=0;
}*/

if (mouse_b & 2)
{
if(obj[x].AIstate==5)
{
obj[x].sides=1;
spawn_bullet(obj[i].positionX,obj[i].positionY);
obj[x].desX=mouse_x;
obj[x].desY=mouse_y;
}

}
}

}

void level(int ally,int foe)
{
switch (obj[ally].AIstate)
{
case(0)://patrol
patrol(ally,foe);
break;
case(1)://fire

while(obj[ally].status==2||obj[ally].status==3)
{
fire(ally,foe);
//obj[ally].AIstate=2;
}
break;
case(2)://divert

while(obj[ally].status==2||obj[ally].status==3)
{
divert(ally,foe);
obj[ally].AIstate=1;
}
break;
case(3)://retreat
retreat(ally,foe);
break;
/*case(4)://still
donothing(ally,foe);
break;
*/

}


}

void aiFoe(int i)
{
for(int x=0;x<Max_objs;x++)
{
if(obj[x].AItype==5)
{
obj[x].sides=2;
}
if(obj[i].sides!=obj[x].sides)
{
level(i,x);
}
}

}


void ai()
{
for(int i=0;i<Max_objs;i++)
{
if(obj[i].AItype==5)
aiBullet(i);
if(obj[i].AItype==3)
aiInventory(i);
if(obj[i].AItype==4)
aiFoe(i);

}
}

void mouse()
{
for(int i=0;i<Max_objs;i++)
{
if (mouse_b & 1)
{
if(obj[i].AItype==3)
spawn_bullet(obj[i].positionX,obj[i].positionY);
if(obj[i].AItype==5){
obj[i].desX=mouse_x;
obj[i].desY=mouse_y;
}
}

}

}

void drawsprite(int arrPos,double drawX, double drawY)
{
int topLeftX=drawX;//obj[arrPos].positionX;
int topLeftY=drawY;//obj[arrPos].positionY;
int BtmRX=topLeftX+obj[arrPos].size;
int BtmRY=topLeftY+obj[arrPos].size;

int sX,sY;//source X
int cX,cY;

if(topLeftX<0)
{
sX=-topLeftX;
topLeftX=0;
}
else
{
sX=0;
}

if(topLeftY<0)
{
sY=-topLeftY;
topLeftY=0;
}
else
{
sY=0;
}

if(BtmRX>ResX)
{
BtmRX=ResX;

}
if(BtmRY>ResY)
{
BtmRY=ResY;
}

cX=BtmRX-topLeftX;
cY=BtmRY-topLeftY;

int pX = (obj[arrPos].angle/10)*obj[arrPos].size;

if(cX>0 && cY>0)
{
for(int y=0;y<cY;++y)
for(int x=0;x<cX;++x)
{
int col=getpixel(sprite[obj[arrPos].ptrSprite],x+sX+pX,y+sY);
putpixel(screen_buffer,topLeftX+x,topLeftY+y,col);

}
}

}

void window()
{


for(int i=0;i<Max_objs;i++)
{
if(obj[i].status!=0)
{
double drawX= (((obj[i].positionX - cameraX)/256)+ResX/2);
double drawY= (((obj[i].positionY - cameraY)/256)+ResY/2);
drawsprite(i,drawX,drawY);
}
}


}

void keyboard()
{

if(key[KEY_W])
cameraY-=1024;
if(key[KEY_A])
cameraX-=1024;
if(key[KEY_D])
cameraX+=1024;
if(key[KEY_S])
cameraY+=1024;
}

void loadunits()
{
sprite[3]=load_bitmap("shooter.bmp",NULL);
sprite[4]=load_bitmap("foe.bmp",NULL);

}

void rotate()
{
BITMAP* temp1,*temp2;


temp1=load_bitmap("bullet.bmp",NULL);
sprite[5]=create_bitmap(8*36,8);


temp2=load_bitmap("explosion.bmp",NULL);
sprite[1]=create_bitmap(64*36,64);



for (int rotNr=0; rotNr<36;rotNr++)
{
rotate_sprite(sprite[5],temp1,rotNr*8,0,itofix(7.1*rotNr));

rotate_sprite(sprite[1],temp2,rotNr*64,0,itofix(7.1*rotNr));
}


}

void passive_to_active()
{
show_mouse(screen_buffer);
blit(screen_buffer,screen,0,0,0,0,ResX,ResY);
clear_to_color(screen_buffer,makecol(0,0,0));
show_mouse(NULL);
}

void fse()
{
keyboard();
ai();
mouse();
window();
passive_to_active();

}

int main()
{
init();
loadunits();
rotate();
spawn_inventory();
spawn_foe();
while(!key[KEY_ESC])
{

if(timerFlag)
{
fse();
timerFlag=false;
}
}


}
END_OF_MAIN()


Share this post


Link to post
Share on other sites
mrbastard    1577
your patrol function equates to this:

void patrol(int ally,int foe)
{
obj[ally].positionY+=100;
obj[ally].positionX+=100;
obj[ally].positionY-=100;
obj[ally].positionX-=100;


if(range(ally,foe)<100)
{
obj[ally].AIstate=1;//fire
x=Max_objs;
}

}



which equates to:


void patrol(int ally,int foe)
{
if(range(ally,foe)<100)
{
obj[ally].AIstate=1;//fire
x=Max_objs;
}
}

Share this post


Link to post
Share on other sites
chillypacman    100
try

obj[ally].positionY = (obj[ally].positionY + 100) * timeDelta;

(timedelta would be whatever it is you use for timing in seconds or whatever else).

I've never used allegro before but I have had a similar problem which I fixed like this, so it might be the same here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this