void SpawnEnemy() { Entity *pTemp = new Entity ; if (pEnemys == 0) { pEnemys = pTemp ; pTemp->pNext = 0 ; pTemp->pLast = 0 ; } else { pTemp->pNext = pEnemys->pNext ; if (pEnemys->pNext != 0) pEnemys->pNext->pLast = pTemp ; pTemp->pLast = pEnemys ; pEnemys->pNext = pTemp ; } pTemp->ptDirection.dX = 0.0 ; pTemp->ptDirection.dY = 0.0 ; pTemp->ptDirection.dZ = randp() * -0.8 ; pTemp->ptAngle.dX = 180.0 ; pTemp->ptAngle.dY = 0.0 ; pTemp->ptLocation.dX = randp() * 20 - 10 ; pTemp->ptLocation.dY = 0 ; pTemp->ptLocation.dZ = 100 ; pTemp->ptAngle.dZ = 0.0 ; pTemp->pModel = &Enemy01Model ; }
/*********************************************************** To generate a random integer "k" in [i,j]. * Input: integers "seed", "i" and "j" in [1,2147483646] * Ouput: integer in [i,j] * ************************************************************/ int get_rand_ij(int *seed, int i, int j ) { randp(seed); return ((double)*seed/((double)2147483647/((double)(j-i+1))))+i; }
void MakeExplosion(Point ptLocation, int iNumParticles) { for (int iTrav = 0 ; iTrav < iNumParticles ; iTrav ++) { Particle *pNew = new Particle ; pNew->ptLocation.dX = ptLocation.dX ; pNew->ptLocation.dY = ptLocation.dY ; pNew->ptLocation.dZ = ptLocation.dZ ; pNew->ptVelocity.dX = 2 * randp() - 1 ; pNew->ptVelocity.dY = 2 * randp() - 1 ; pNew->ptVelocity.dZ = 2 * randp() - 1 ; pNew->ptColor.dX = 0.98 ; pNew->ptColor.dY = 0.59 + 0.3 * randp() - 0.15 ; pNew->ptColor.dZ = 0.01 ; pNew->dAlpha = 0.7 ; pNew->dMass = 1 ; pNew->dLife = 4 * randp() + 3 ; pNew->ptAcceleration = 0 ; Particles.AddBack(pNew) ; } }
/************************************************* To check the correctness of the implementation. * Ouput: correct (1) or wrong (0) * **************************************************/ int trand() { int i, seed; seed = 1; for (i=1;i<=1000;i++) randp(&seed); if ( seed == 522329230 ) return 1; else return 0; }
void DisplayHandler () // display callback function { if (bRotate) { fCameraAngle[0] += 1.0 ; fCameraAngle[2] += 1.0 ; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; glLoadIdentity() ; SetCamera() ; if (!bPause) { GLfloat fTempX = GLfloat(iLastMouseX) / GLfloat(iWinWidth) ; GLfloat fTempY = GLfloat(iLastMouseY) / GLfloat(iWinHeight) ; GLfloat fFixedX = (Player.ptLocation.dX - 8.65) / -17.3 ; GLfloat fFixedZ = (Player.ptLocation.dZ - 8.65) / -17.3 ; if (bRightMouseDown) { if (!(fFixedX + 0.02 > fTempX && fFixedX - 0.02 < fTempX)) { if (fFixedX > fTempX) { Player.ptLocation.dX += 0.1 ; if (Player.ptAngle.dZ > -20) Player.ptAngle.dZ -= 5.0 ; } else { Player.ptLocation.dX -= 0.1 ; if (Player.ptAngle.dZ < 20) Player.ptAngle.dZ += 5.0 ; } } else { if (Player.ptAngle.dZ < 0) Player.ptAngle.dZ += 5.0 ; else if (Player.ptAngle.dZ > 0) Player.ptAngle.dZ -= 5.0 ; } if (!(fFixedZ + 0.1 > fTempY && fFixedZ - 0.02 < fTempY)) { if (fFixedZ > fTempY) { Player.ptLocation.dZ += 0.1 ; } else { Player.ptLocation.dZ -= 0.1 ; } } } else Player.ptAngle.dZ = 0 ; if (randp() > 0.98) AddPlanet() ; AddStar() ; AddStar() ; AddStar() ; AddStar() ; AddStar() ; AddStar() ; if (/*!bExplode && */iCount % 15 == 0) SpawnEnemy() ; if (!bExplode && bAutofire && iCount % 4 == 0) FireShot(&Player, pPlayerShots) ; iCount ++ ; } DisplayStars() ; DisplayPlanets() ; DisplayEnemies() ; DisplayShots(pPlayerShots) ; DisplayShots(pEnemyShots) ; if (bExplode && !bPause) { if (iCount == 0) { printf("Game over\n") ; MakeExplosion(Player.ptLocation, 1000) ; } } else { if (iCount % 4) { Particle *pNew = new Particle ; pNew->ptLocation.dX = Player.ptLocation.dX + randp() * 0.5 - 0.25; pNew->ptLocation.dY = Player.ptLocation.dY + 0.3 * randp() ; pNew->ptLocation.dZ = Player.ptLocation.dZ - 1.5 + 0.2 * randp() ; pNew->ptVelocity.dX = 0 ; pNew->ptVelocity.dY = 0 ; pNew->ptVelocity.dZ = -0.2 ; pNew->ptColor.dX = 0.98 ; pNew->ptColor.dY = 0.59 + 0.3 * randp() - 0.15 ; pNew->ptColor.dZ = 0.01 ; pNew->dAlpha = 0.7 ; pNew->dMass = 1 ; pNew->dLife = 7 * randp() + 3 ; pNew->ptAcceleration = 0 ; Particles.AddBack(pNew) ; } Player.Display() ; } LinkedListNode<Particle> *pTravParticles = Particles.pHead ; LinkedListNode<Particle> *pTempParticle ; while (pTravParticles != 0) { pTravParticles->pValue->dLife -- ; if (pTravParticles->pValue->dLife <= 0) { pTempParticle = pTravParticles ; pTravParticles = pTravParticles->pNext ; Particles.Remove(pTempParticle, true) ; } else { pTravParticles->pValue->Apply(0.9) ; DisplayParticle(pTravParticles->pValue) ; pTravParticles = pTravParticles->pNext ; } } glFinish() ; glutSwapBuffers() ; glutPostRedisplay() ; }
void DisplayEnemies() { Entity *pTrav = pEnemys ; Entity *pTravShots ; Entity *pTemp ; bool bShot ; Point ptBBModelUFL, ptBBModelLBR, ptBBShotUFL, ptBBShotLBR ; while (pTrav != 0) { if (pTrav->ptLocation.dZ > -100) { ptBBModelUFL = pTrav->ptLocation + pTrav->pModel->ptBBUpperFrontLeft ; ptBBModelLBR = pTrav->ptLocation + pTrav->pModel->ptBBLowerBackRight ; bShot = false ; pTravShots = pPlayerShots ; while (pTravShots != 0) { ptBBShotUFL = pTravShots->ptLocation + pTravShots->pModel->ptBBUpperFrontLeft ; ptBBShotLBR = pTravShots->ptLocation + pTravShots->pModel->ptBBLowerBackRight ; if ((ptBBModelUFL < ptBBShotUFL && ptBBModelLBR > ptBBShotUFL) || (ptBBModelUFL < ptBBShotLBR && ptBBModelLBR > ptBBShotLBR)) { bShot = true ; MakeExplosion(pTrav->ptLocation, 500) ; if (!bExplode) { iScore += 100 ; printf ("Score: %i\n", iScore) ; } pTemp = pTrav ; pTrav = pTrav->pNext ; pTemp->pModel = 0 ; RemoveEntity(pEnemys, pTemp) ; pTravShots->pModel = 0 ; RemoveEntity(pPlayerShots, pTravShots) ; break ; } pTravShots = pTravShots->pNext ; } if (!bShot) { ptBBShotUFL = Player.ptLocation + Player.pModel->ptBBUpperFrontLeft ; ptBBShotLBR = Player.ptLocation + Player.pModel->ptBBLowerBackRight ; if (!bExplode && ((ptBBModelUFL < ptBBShotUFL && ptBBModelLBR > ptBBShotUFL) || (ptBBModelUFL < ptBBShotLBR && ptBBModelLBR > ptBBShotLBR))) { bExplode = true ; iCount = 0 ; MakeExplosion(pTrav->ptLocation, 500) ; pTemp = pTrav ; pTrav = pTrav->pNext ; pTemp->pModel = 0 ; RemoveEntity(pEnemys, pTemp) ; } else { // if (randp() > 0.99) if (!bExplode && randp() > 0.99) FireShot(pTrav, pEnemyShots) ; pTrav->Display() ; if (!bPause) pTrav->ptLocation = pTrav->ptLocation + pTrav->ptDirection ; pTrav = pTrav->pNext ; } } } else { pTemp = pTrav ; pTrav = pTrav->pNext ; pTemp->pModel = 0 ; RemoveEntity(pEnemys, pTemp) ; } } }
/** Generate stars for nsky star fields from star catalog. \return a cell array of nskyx1, each cell contains (2+nwvl) x nstar array of location, and magnitudes. */ dcell *genstars(long nsky, /**<number of star fields wanted*/ double lat, /**<galactic latitude.*/ double lon, /**<galactic longitude*/ double catscl, /**<Scale the catlog star count.*/ double fov, /**<diameter of the patrol field of view in arcsec.*/ int nwvl, /**<number of wavelength*/ double *wvls, /**<wavelength vector*/ rand_t *rstat /**<random stream*/ ){ char fn[80]; double cat_fov=0;/*catalogue fov */ int Jind=-1; if(nwvl==2 && fabs(wvls[0]-1.25e-6)<1.e-10 && fabs(wvls[1]-1.65e-6)<1.e-10){ snprintf(fn,80,"besancon/JH_5sqdeg_lat%g_lon%g_besancon.bin", lat, lon); cat_fov=5.0;/*5 arc-degree squared. */ Jind=0; }else if(nwvl==3 && fabs(wvls[0]-1.25e-6)<1.e-10 && fabs(wvls[1]-1.65e-6)<1.e-10 && fabs(wvls[2]-2.2e-6)<1.e-10){ snprintf(fn,80,"besancon/JHK_5sqdeg_lat%g_lon%g_besancon.bin", lat, lon); cat_fov=5.0;/*5 arc-degree squared. */ Jind=0; }else{ Jind=-1; error("We only have stars for J+H and J+H+K band. Please fill this part\n"); } info("Loading star catalogue from %s\n",fn); dmat *catalog=dread("%s",fn); if(catalog->ny!=nwvl){ error("Catalogue and wanted doesn't match\n"); } long ntot=catalog->nx; long nsky0=0; dcell *res=dcellnew(nsky,1); dmat* pcatalog=catalog; double fov22=pow(fov/2/206265,2); double navg0=M_PI*pow(fov/2./3600.,2)/cat_fov * ntot; if(catscl>0){//regular sky coverage sim double navg=navg0*catscl; info("Average number of stars: %g, after scaled by %g\n", navg, catscl); /*generate nstart && magnitude according to distribution.*/ for(long isky=0; isky<nsky; isky++){ long nstar=randp(rstat, navg); if(nstar==0) continue; res->p[isky]=dnew(nwvl+2, nstar); dmat* pres=res->p[isky]; for(long istar=0; istar<nstar; istar++){ long ind=round(ntot*randu(rstat));/*randomly draw a star index in the catlog */ for(int iwvl=0; iwvl<nwvl; iwvl++){ P(pres,2+iwvl,istar)=P(pcatalog,ind,iwvl); } } } }else{ /*instead of doing draws on nb of stars, we scan all possibilities and assemble the curve in postprocessing. catscl is negative, with absolute value indicating the max number of J<=19 stars to consider*/ long nmax=round(-catscl); nsky0=nsky/nmax; if(nsky0*nmax!=nsky){ error("nsky=%ld, has to be dividable by max # of stars=%ld", nsky, nmax); } int counti[nmax];//record count in each bin memset(counti, 0, sizeof(int)*nmax); int count=0; while(count<nsky){ long nstar=randp(rstat, navg0); if(nstar==0) continue; dmat *tmp=dnew(nwvl+2, nstar); dmat* pres=tmp; int J19c=0; for(long istar=0; istar<nstar; istar++){ long ind=round((ntot-1)*randu(rstat)); for(int iwvl=0; iwvl<nwvl; iwvl++){ P(pres,2+iwvl,istar)=P(pcatalog,ind,iwvl); } if(P(pres,2+Jind,istar)<=19){ J19c++; } } //J19c=0 is ok. Do not skip. if(J19c<nmax && counti[J19c]<nsky0){ int isky=counti[J19c]+(J19c)*nsky0; res->p[isky]=dref(tmp); count++; counti[J19c]++; } dfree(tmp); } } /*Fill in the coordinate*/ for(long isky=0; isky<nsky; isky++){ if(!res->p[isky]) continue; long nstar=res->p[isky]->ny; dmat* pres=res->p[isky]; for(long istar=0; istar<nstar; istar++){ /*randomly draw the star location. */ double r=sqrt(fov22*randu(rstat)); double th=2*M_PI*randu(rstat); P(pres,0,istar)=r*cos(th); P(pres,1,istar)=r*sin(th); } } dfree(catalog); return res; }