Thds/Bullet code bits

From ProjectWiki
(Difference between revisions)
Jump to: navigation, search
(New page: ==2D Aiming== <source lang="c"> //Send a bullet towards x,y at speed (speed = 256 = 1:1) ... how did this simple function turn so fugly ;;; void bulletsSeek(t_bullets *bullet, int x, int y...)
 
(2D homing)
Line 41: Line 41:
 
//Specifys behavior of homing function
 
//Specifys behavior of homing function
 
typedef struct _homeparams {
 
typedef struct _homeparams {
int inner; //squared inner distance
+
int inner; //squared inner distance squared
int outer; //squared outer distance
+
int outer; //squared outer distance squared
int speed;
+
int speed;     //speed (note, speed will increase based on distance to target getting faster as it approaches. similar to a gravity effect)
 
} homeparams;
 
} homeparams;
  
Line 94: Line 94:
 
else
 
else
 
my=0;
 
my=0;
mod=speed-abs(dx)*4;
+
mod=speed-abs(dx)*4; //change the 4 here and below to change how distance affects 'gravity'
 
if(dx>0)
 
if(dx>0)
 
bullet->xinc=speed;
 
bullet->xinc=speed;

Revision as of 19:31, 7 June 2009

2D Aiming

//Send a bullet towards x,y at speed (speed = 256 = 1:1) ... how did this simple function turn so fugly ;;;
void bulletsSeek(t_bullets *bullet, int x, int y, int speed) {
	int dx, dy, mx, my;
	dx=x-bullet->realx;
	dy=y-bullet->realy;
	if(abs(dx)>abs(dy)) {
		if(dx!=0) 
			my=abs((dy<<8)/dx);
		else
			my=0;
		if(dx>0)
			bullet->xinc=speed;
		else
			bullet->xinc=-speed;
		if(dy>0)
			bullet->yinc=(my*speed)>>8;
		else
			bullet->yinc=-((my*speed)>>8);
	} else {
		if(dy!=0)
			mx=abs((dx<<8)/dy);
		else
			mx=0;
		if(dy>0)
			bullet->yinc=speed;
		else
			bullet->yinc=-speed;
		if(dx>0)
			bullet->xinc=(mx*speed)>>8;
		else
			bullet->xinc=-((mx*speed)>>8);
	}
}

2D homing

This bullet callback function will slowly seek towards a target. homeparams needs to be sent in 'extra' param.

//Specifys behavior of homing function
typedef struct _homeparams {
	int inner;	//squared inner distance squared
	int outer;	//squared outer distance squared 
	int speed;      //speed (note, speed will increase based on distance to target getting faster as it approaches. similar to a gravity effect) 
} homeparams;
 
//Home in on ppl (set homeparams for inner,outer,speed params!)
void thSpellsBulletHomeCallback(t_bullets *bullet, int who, void *extra) {
	int c;
	int closestent;
	int closest;
	int dist;
	int xc,yc;
	homeparams *hp=(homeparams*)extra;
	closest=0xffff;	//something very far far away
	closestent=-1;
 
	if(who==0) {	//Firstly find closest player or enemy depending on what u are (dont home on yerself >_>)
		c=1;
		do {
			if(entities[c].flags!=0) {
				if(c>0) { //an enemy(>0) dont wanna home on bonus boxes etc (<0)
					xc=bulletsGetRealX(entities[c].bullet);
					yc=bulletsGetRealY(entities[c].bullet);
					dist=thDistance(xc,yc,bulletsGetRealX(bullet),bulletsGetRealY(bullet));
					if(dist<closest) {
						closest=dist;
						closestent=c;
					}
				}				
			}
		} while(++c<MAXENTITIES);
	} else {
		xc=bulletsGetRealX(entities[0].bullet);
		yc=bulletsGetRealY(entities[0].bullet);
		closest=thDistance(xc,yc,bulletsGetRealX(bullet),bulletsGetRealY(bullet));
		closestent=0;
	}
	if((closestent!=-1)&&(closest>hp->inner)&&(closest<hp->outer)) {
		xc=bulletsGetRealX(entities[closestent].bullet);
		yc=bulletsGetRealY(entities[closestent].bullet);
//		bulletsSeek(bullet, xc, yc, abs(bulletsGetXinc(bullet))+100);
		//this block from bulletsSeek but modded 
		int dx, dy, mx, my;
		int speed;
		speed=hp->speed;
		int mod;
		dx=xc-bullet->realx;
		dy=yc-bullet->realy;
		if(abs(dx)>abs(dy)) {
			if(dx!=0) 
				my=abs((dy<<8)/dx);
			else
				my=0;
			mod=speed-abs(dx)*4; //change the 4 here and below to change how distance affects 'gravity'
			if(dx>0)
				bullet->xinc=speed;
			else
				bullet->xinc=-speed;
			if(dy>0)
				bullet->yinc=(my*mod)>>8;
			else
				bullet->yinc=-((my*mod)>>8);
		} else {
			if(dy!=0)
				mx=abs((dx<<8)/dy);
			else
				mx=0;
			mod=speed-abs(dy)*4;
			if(dy>0)
				bullet->yinc=speed;
			else
				bullet->yinc=-speed;
			if(dx>0)
				bullet->xinc=(mx*mod)>>8;
			else
				bullet->xinc=-((mx*mod)>>8);
		}
	}
}

Fast then slow home

This produces sorta the opposite effect as above, will rapidly seek toward a destination, then quickly slow down to almost a crawl as it approaches the player. Sorta reminds me of some of Reisan's spells in IN or similarly the Prisimriver sisters Spell in PCB.

//This looks ossssssum ;;; use for enemys
void thSpellsBulletSeekNearCallback(t_bullets *bullet, int who, void *extra) {
	int c;
	int closestent;
	int closest;
	int dist;
	int xc,yc;
	int inner,outer; //from inner to outter range home
	inner=50;
	outer=150;
	inner*=inner;
	outer*=outer;
	closest=0xffff;	//something very far far away
	closestent=-1;
	if(who==0) {	//Firstly find closest player or enemy depending on what u are (dont home on yerself >_>)
		c=1;
		do {
			if(entities[c].flags!=0) {
				if(c>0) { //an enemy(>0) dont wanna home on bonus boxes etc (<0)
					xc=bulletsGetRealX(entities[c].bullet);
					yc=bulletsGetRealY(entities[c].bullet);
					dist=thDistance(xc,yc,bulletsGetRealX(bullet),bulletsGetRealY(bullet));
					if(dist<closest) {
						closest=dist;
						closestent=c;
					}
				}				
			}
		} while(++c<MAXENTITIES);
	} else {
		xc=bulletsGetRealX(entities[0].bullet);
		yc=bulletsGetRealY(entities[0].bullet);
		closest=thDistance(xc,yc,bulletsGetRealX(bullet),bulletsGetRealY(bullet));
		closestent=0;
	}
	if((closestent!=-1)&&(closest>inner)&&(closest<outer)) {
		xc=bulletsGetRealX(entities[closestent].bullet);
		yc=bulletsGetRealY(entities[closestent].bullet);
		bulletsSeek(bullet, xc, yc, abs(bulletsGetXinc(bullet))+100);
 
	}
}
Personal tools
irssi scripts
eggdrop scripts