#include "ships.h" #include "game.h" void updateShip(ShipInfoDesc* ship){ //local variables register signed int xaccel, yaccel; register unsigned char vx_frac, vy_frac; register unsigned char new_xfrac, new_yfrac; signed int tempvx, tempvy; signed char tempypix; signed int negvmax , vmax; // Extract x/y-components of acceleration from precalculated table , with gravity if(ship->xpix < 81 && ship->xpix > 40 && ship->ypix <71 && ship->ypix >30){ xaccel = (signed int)((ship->accel)? shipXaccel[ship->type][ship->theta >> 5]:0) + XGrav[ship->xpix-41][ship->ypix-31]; yaccel = (signed int)((ship->accel)? shipYaccel[ship->type][ship->theta >> 5]:0) + YGrav[ship->xpix-41][ship->ypix-31]; } else{ xaccel = (ship->accel)? shipXaccel[ship->type][ship->theta >> 5]:0; yaccel = (ship->accel)? shipYaccel[ship->type][ship->theta >> 5]:0; } // X-section tempvx= ship->vx + xaccel; //euler integrator treatment of acceleration //update x-velocity, constrained by max //check if exceeds max positive veloc or max negative veloc vmax = (signed int)shipTable[ship->type][SHIPVMAX]; negvmax = -vmax; if (tempvx < negvmax) { tempvx = negvmax; } else if (tempvx > vmax){ tempvx = vmax; } // Save the new velocity ship->vx = tempvx; if(tempvx >=0) { //heading in positive x dir vx_frac = (unsigned char)(tempvx & 0xff); // %256 to get the remainder portion - This case, vx_frac is positive ship->xpix = ship->xpix + (unsigned char) (tempvx>>8); //get whole pixel movement by divding by 256 new_xfrac = (ship->xfrac) + vx_frac; //update x fraction - may overflow ship->xpix = (new_xfrac < ship->xfrac)? ship->xpix++:ship->xpix; //get extra pixel movement from remainder portion based on overflow check } else { //heading in negative x dir vx_frac = (unsigned char)(-tempvx & 0xff); //negate tempvx to get abs value, &256 to get remainder ship->xpix = ship->xpix - (unsigned char) (-tempvx>>8); new_xfrac = (ship->xfrac) - vx_frac; ship->xpix = (new_xfrac > ship->xfrac)? ship->xpix--:ship->xpix; //determine whether a pixel boundary was crossed based on overflow check } ship->xfrac= new_xfrac; //update x fractional position ship->xpix = ship->xpix & 0x7f; //enforce x-wraparound //Y-section tempvy= ship->vy + yaccel; //euler integrator treatment of acceleration //check if exceeds max positive veloc or max negative veloc vmax = (signed int)shipTable[ship->type][SHIPVMAX]; negvmax = -vmax; if (tempvy < negvmax) { tempvy = negvmax; } else if (tempvy > vmax){ tempvy = vmax; } // Save the new velocity ship->vy = tempvy; if(tempvy >=0) //heading in positive y dir { vy_frac = (unsigned char)(tempvy & 0xff); // %256 to get the remainder portion - This case, vy_frac is positive tempypix = ship->ypix + (unsigned char) (tempvy>>8); //get whole pixel movement by divding by 256 new_yfrac = (ship->yfrac) + vy_frac; //update y fraction - may overflow tempypix = (new_yfrac < ship->yfrac)? tempypix++:tempypix; //determine whether an extra pixel boundary was crossed based on overflow check } else //heading in negative y dir { vy_frac = (unsigned char)(-tempvy & 0xff); //negate tempvy to get abs value tempypix = ship->ypix - (unsigned char) (-tempvy>>8); new_yfrac = (ship->yfrac) - vy_frac; tempypix = (new_yfrac > ship->yfrac)? tempypix--:tempypix; //determine whether a pixel boundary was crossed based on overflow check } ship->yfrac= new_yfrac; //update y fractional position //wraparound condition for Y if (tempypix > 99) { ship->ypix = tempypix - 100; } else if (tempypix < 0) { ship->ypix = tempypix +100; } else { ship->ypix = tempypix; } } void updateBullets(ShipInfoDesc* ship, ShipInfoDesc* shiptarg) { //local variables register unsigned char new_xfrac, new_yfrac; signed int tempvx, tempvy; signed char tempypix; register char index; // Temporary variable for-loops //parse through ship's bullet array to check for active bullets...only operate on these for(index =0; index < MAX_BULLETS; index ++){ ship->activebullets =0; if(ship->BulletActive[index]){ ship->activebullets++; //X-dir update positions tempvx = bulletVX[ship->BulletType[index]][ship->BulletDirection[index] >> 5]; if(tempvx >=0){ //heading in positive x dir ship->BulletPosX[index]+=tempvx; } else{ //heading in negative x dir ship->BulletPosX[index]-= -tempvx; } ship->BulletXFrac[index]= new_xfrac; //update x fractional position ship->BulletPosX[index] = ship->BulletPosX[index] & 0x7f; //enforce x-wraparound //Y-dir tempvy = bulletVY[ship->BulletType[index]][ship->BulletDirection[index] >> 5]; if(tempvy >=0){ //heading in positive y dir tempypix = ship->BulletPosY[index]+tempvy; } else { tempypix = ship->BulletPosY[index]- (-tempvy); } //update yfrac ship->BulletYFrac[index] = new_yfrac; //wraparound condition for Y if (tempypix > 99) { ship->BulletPosY[index] = tempypix - 100; } else if (tempypix < 0) { ship->BulletPosY[index] = tempypix +100; } else { ship->BulletPosY[index] = tempypix; } //use homing algorithm to update direction for homing weapons if((ship->BulletType[index] == 2) || (ship->BulletType[index] == 3)){ homing(ship, shiptarg, index); } } //end if bullet active }//endfor }//end updatebullet void homing(ShipInfoDesc* shipmain, ShipInfoDesc* shipopp, char index){ // for (index = 0; index < MAX_BULLETS; index++) { signed int tempx=0, tempy5, tempx12, tempx2, tempx2n, tempx12n; signed int tempy=0; //check if the bullet is active and homing type if (shipmain->BulletActive[index] && (shipmain->BulletType[index] == 2 || shipmain->BulletType[index] == 3)){ //get xy distance of opponent from bullet tempx = (signed int)shipopp->xpix - (signed int)shipmain->BulletPosX[index]; tempy = (signed int)shipopp->ypix - (signed int)shipmain->BulletPosY[index]; //split area around bullet into 8 radial zones, boundaries defined by 5y=+-12x and 5y=+-2x tempy5 = 5*tempy; tempx12 = 12*tempx; tempx2 = 2*tempx; tempx12n = -12*tempx; tempx2n = -2*tempx; //if directly on bullet...don't do anything if (tempy ==0 && tempx==0){ shipmain->BulletPosX[index] = shipopp->xpix; shipmain->BulletPosY[index] = shipopp->ypix; } //check if opponent is in particular region, turn the shortest way towards him else if(tempy5 < tempx2 && tempy5 > tempx2n){ if(shipmain->BulletDirection[index]<=THETA2 || shipmain->BulletDirection[index]>=THETA65){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA2 && shipmain->BulletDirection[index]<=THETA55){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } } //end region 6 else if( tempy5 < tempx2 && tempy5 > tempx12){ if(shipmain->BulletDirection[index]<=THETA5 && shipmain->BulletDirection[index]>=THETA15){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA5 || shipmain->BulletDirection[index]<=THETA05){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } } //end region 1 else if(tempy5 < tempx12 && tempy5 < tempx12n){ if(shipmain->BulletDirection[index]<=THETA4 && shipmain->BulletDirection[index]>=THETA05){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA4 && shipmain->BulletDirection[index]<=THETA75){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } } //end region 0 else if (tempy5 < tempx12 && tempy5 >tempx2){ if(shipmain->BulletDirection[index]<=THETA1 || shipmain->BulletDirection[index]>=THETA55){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA1 && shipmain->BulletDirection[index]<=THETA45){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } } //end region 5 else if(tempy5 < tempx12n && tempy5 > tempx2n){ if(shipmain->BulletDirection[index]<=THETA7 && shipmain->BulletDirection[index]>=THETA35){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA7 || shipmain->BulletDirection[index]<=THETA25){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } }//end region 3 else if(tempy5 < tempx2n && tempy5 > tempx12n){ if(shipmain->BulletDirection[index]<=THETA3 || shipmain->BulletDirection[index]>=THETA75){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA3 && shipmain->BulletDirection[index]<=THETA65){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } } //end region 7 else if (tempy5 < tempx2n && tempy5 >tempx2){ if(shipmain->BulletDirection[index]<=THETA6 && shipmain->BulletDirection[index]>=THETA25){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA6 || shipmain->BulletDirection[index]<=THETA15){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } } //end region 2 else{ if(shipmain->BulletDirection[index]<=0xff && shipmain->BulletDirection[index]>=THETA45){ shipmain->BulletDirection[index] -= bulletTable[shipmain->BulletType[index]][TURNRATE]; } else if(shipmain->BulletDirection[index]>=THETA0 && shipmain->BulletDirection[index]<=THETA35){ shipmain->BulletDirection[index] += bulletTable[shipmain->BulletType[index]][TURNRATE]; } } //end region 4 } //endif bullet active // } //endfor } //end homing