Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators: 
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Giuseppe Lipari       <lipari@sssup.it> 
 *   Paolo Gai               <pj@gandalf.sssup.it>
 * 
 * Authors     : 
 *   Kabilan Sukumar      <kabbys2@yahoo.com>
 *   Rajenish Kumar jain  <rajenish_jain@yahoo.com>
 *   Deepaknath T K       <deepaknathtk@yahoo.com>
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2002      Kabilan Sukumar,  Rajenish Kumar jain, Deepaknath T K 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*--------------------------------------------------------------*/
/*              THE PING PONG GAME                              */
/*--------------------------------------------------------------*/

#include "demo.h"
#include <kernel/func.h>
#include <stdlib.h>
#include <drivers/keyb.h>

# define R  4        /* the radius of the ball */


int ballexit=0;
int horizgo;         /*when set increases the horizontal speed of the ball*/
int vertigo;         /*when set increases the horizontal speed of the ball*/
int batflagP1=0;
int batflagP2=0;
KEY_EVT keypressP1;
KEY_EVT keypressP2;
char points[3];
int jump;             /*when set makes the bat move faster*/


struct ballData
{
        /*Coordinates of centre for ball*/
        int xcur;
        int ycur;
        /*to store the directions of the ball*/
        int xdir;
        int ydir;

        int path_change;

}ball;


struct batData
{
               /*Coordinates of centre for bat*/
        int x;
        int y;
        int points;
}p1,p2;


char dir;    /*to store the direction of movement of bats*/

int gamefinish=0; /* indicates the end of the game */


/* to print the score on the screen */
void score_increase(char temp[3], int x1, int y1)
{
     grx_box(x1+10,y1+2,x1+60,y1+18,black);

     grx_text(temp,x1+30,y1-4,rgb16(0,200,0),black);
}

/* the function to take care of the score increase and determining the end of game */
void score_keeper()
{
     int i=0;
     /*ball hits upper boundry*/
     if( ball.ycur == 23)
     {
          p1.points = p1.points + 1;
          itoa(p1.points, points);
          score_increase(points,435,120);
     }
     else if( ball.ycur == 453 )/*If ball hits lower boundry*/
     {
          p2.points = p2.points + 1;
          itoa(p2.points, points);
          score_increase(points,520,180);
     }

     if( p1.points == 5 )
     {

          grx_text("!!!!! PLAYER TWO WINS, CONGRATULATIONS !!!!!",20,200,green,black);
          grx_text("Press ESC to exit to menu",70,250,green,black);
          gamefinish=1; // indicates that the game is over
     }
     if(p2.points== 5)
     {
          grx_text("!!!!! PLAYER ONE WINS, CONGRATULATIONS !!!!!",20,200,green,black);
          grx_text("Press ESC to exit to menu",70,250,green,black);
          gamefinish=1; // indicates that the game is over
     }

}

/* The ball thread which keeps tracking the new position of the ball and moves the ball */
TASK balls(int i)
{
     while(1){
        /*Delete the ball at previous position*/
      grx_disc(ball.xcur,ball.ycur,R,black);
      if(escapeflag||gamefinish)return 0;

      if( grx_getpixel(ball.xcur-7,ball.ycur) != black ) /*Left hand sensor*/
      {
          ball.path_change = 1;
          ball.xdir = 1;
      }
      else if( grx_getpixel(ball.xcur+7,ball.ycur) != black ) /*Right hand sensor*/
      {
          ball.path_change = 1;
          ball.xdir = 0;
      }
      if( grx_getpixel(ball.xcur,ball.ycur-8) !=black ) /*Top sensor*/
      {
          vertigo = 0;
          horizgo = 0;

          ball.path_change = 1;
          ball.ydir = 1;


          if((grx_getpixel(ball.xcur,ball.ycur-5) == white ) && ( p2.x - ball.xcur > 20 ) && ( ball.xdir == 1 ) )  if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( p2.x - ball.xcur > 20 ) && ( ball.xdir == 1 ) )
          {
               ball.xdir = 0;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur-5) == black) && (abs(p2.x-ball.xcur)<10))
          {
               vertigo = 1;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( ball.xcur - p2.x > 20 ) && ( ball.xdir == 0 ) )
          {
               ball.xdir = 1;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( p2.x - ball.xcur > 20 ) && ( ball.xdir == 0 ) )
          {
               horizgo = 1;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( ball.xcur - p2.x > 20 ) && ( ball.xdir == 1 ) )
          {

               horizgo = 1;
          }
      }
      if( grx_getpixel(ball.xcur,ball.ycur+7) !=black ) /*Bottom sensor*/
      {
          vertigo = 0;
          horizgo = 0;

          ball.path_change = 1;
          ball.ydir = 0;


         if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( p1.x - ball.xcur > 20 ) && ( ball.xdir == 1 ) )
          {
               ball.xdir = 0;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && (abs(p1.x-ball.xcur)<10) )
          {
               vertigo = 1;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( ball.xcur - p1.x > 20 ) && ( ball.xdir == 0 ) )
          {
               ball.xdir = 1;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( p1.x - ball.xcur > 20 ) && ( ball.xdir == 0 ) )
          {
               horizgo = 1;
          }
          else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( ball.xcur - p1.x > 20 ) && ( ball.xdir == 1 ) )
          {
               horizgo = 1;
          }
     }

     score_keeper(); /* call the score keeper to update the scores */

     /*calculate the direction of the ball*/
     if ( (vertigo == 0) && (horizgo == 0) )
     {
          if(ball.xdir == 1) ball.xcur=ball.xcur+2; else ball.xcur=ball.xcur-2;
          if(ball.ydir == 1) ball.ycur=ball.ycur+2; else ball.ycur=ball.ycur-2;
     }
     else if (vertigo == 1)
     {
          if(ball.xdir == 1) ball.xcur=ball.xcur+2; else ball.xcur=ball.xcur-2;
          if(ball.ydir == 1) ball.ycur=ball.ycur+3; else ball.ycur=ball.ycur-3;
     }
     else if (horizgo == 1)
     {
          if(ball.xdir == 1) ball.xcur=ball.xcur+3; else ball.xcur=ball.xcur-3;
          if(ball.ydir == 1) ball.ycur=ball.ycur+2; else ball.ycur=ball.ycur-2;
     }

     /*draw the ball at new position*/

     grx_disc(ball.xcur,ball.ycur,R,white);



    task_endcycle();
   }

}

/* called if the escape key is pressed during the game, it sets the flag */
void EscapeToMenu(KEY_EVT *k){
      escapeflag=1;
}

/* the bat thread for player 1. */
/* it controls the movement of the bat by reading the keystrokes of the player */
TASK movebatP1(int k){

     while(1){
       if(escapeflag||gamefinish){
          return 0;
       }

       if((getKeyFlag()&CNTL_BIT)||getKeyFlag()&SHFL_BIT){
         /*delete the bat at old position*/
         grx_box( p1.x-30, p1.y, p1.x+30, p1.y+5,black);
         if( (getKeyFlag()&CNTL_BIT) && (p1.x > 42) )          /*moves the bat to the left*/   
         {
                 p1.x-=1;
         }
         else if( (getKeyFlag()&SHFL_BIT) && (p1.x < 343) )   /*moves the bat to the right*/
         {
                p1.x+=1;
         }

         /*draw the bat at the new position*/
         grx_box(p1.x-30, p1.y, p1.x+30, p1.y+5,blue);
         batflagP1=0;
       }
     task_endcycle();
    }
}

/* the bat thread for player 2. */
/* it controls the movement of the bat by reading the keystrokes of the player */
TASK movebatP2(int k){
     /*delete the bat at old position*/
     while(1){
        if(escapeflag||gamefinish){
         return 0;
        }
       if((getKeyFlag()&CNTR_BIT)||(getKeyFlag()&SHFR_BIT)){
        grx_box( p2.x-30, p2.y, p2.x+30, p2.y+5,black);
        if( (getKeyFlag()&CNTR_BIT) && (p2.x > 42) )       /*moves the bat to the left*/   
        {
                p2.x-=1;
        }
        else if((getKeyFlag()&SHFR_BIT)&&(p2.x < 343))     /*moves the bat to the right*/
         {
                p2.x+=1;
         }

         /*draw the bat at the new position*/
         grx_box( p2.x-30, p2.y, p2.x+30, p2.y+5,yellow);
         batflagP2=0;
       }
       task_endcycle();
     }
}

/* the cpu player to be used in single player game*/
TASK movebatCPU(){
     while(1){
          /*delete the bat at old position*/
         if(escapeflag||gamefinish){
           return 0;
          }
          grx_box(p2.x-30, p2.y, p2.x+30, p2.y+5,black);

          if( (ball.xdir == 1) && (p2.x < 345) && (p2.x-ball.xcur < 15) && (ball.ydir == 0) )
          {
               p2.x = p2.x + jump;
          }
          else if( (ball.xdir == 0) && (p2.x > 45) && (ball.xcur-p2.x < 15) && (ball.ydir == 0) )
          {
               p2.x = p2.x - jump;
          }

          grx_box(p2.x-30, p2.y, p2.x+30, p2.y+5,yellow);
          task_endcycle();
      }

}

/*Creates a hard task for the player p1*/
void hardbatP1()
{
  HARD_TASK_MODEL mp;
  PID pid;

  hard_task_default_model(mp);
  hard_task_def_ctrl_jet(mp);
  hard_task_def_arg(mp,NULL);
  hard_task_def_wcet(mp,500);
  hard_task_def_mit(mp,5000);
  hard_task_def_usemath(mp);
  pid= task_create("batEDF1", movebatP1, &mp, NULL);
  if (pid== NIL) {
        grx_close();
        perror("Could not create task <batEDF1>");
        sys_end();
  }
  else
    task_activate(pid);
}

/*Creates a hard task for the player p2*/
void hardbatP2(){

  HARD_TASK_MODEL mp;
  PID pid;

  hard_task_default_model(mp);
  hard_task_def_ctrl_jet(mp);
  hard_task_def_arg(mp,NULL);
  hard_task_def_wcet(mp, 500);
  hard_task_def_mit(mp, 5000);
  hard_task_def_usemath(mp);
  pid = task_create("batEDF2", movebatP2, &mp, NULL);
  if (pid == NIL) {
        grx_close();
        perror("Could not create task <batEDF2>");
        sys_end();
  }
  else
    task_activate(pid);

}

/*Creates a hard task for the ball*/
void hardball()
{

  HARD_TASK_MODEL mp;
  PID pid;

  hard_task_default_model(mp);
  hard_task_def_ctrl_jet(mp);
  hard_task_def_arg(mp,NULL);
  hard_task_def_wcet(mp, 500);
  hard_task_def_mit(mp,PERIOD_BALL);
  hard_task_def_usemath(mp);
  pid = task_create("ballEDF", balls, &mp, NULL);
  if (pid == NIL) {
        grx_close();
        perror("Could not create task <ballEDF>");
        sys_end();
  }
  else
    task_activate(pid);

}

/*Creates a hard task for the cpu bat*/
void cpubat(){
  HARD_TASK_MODEL mp;
  PID pid;

  hard_task_default_model(mp);
  hard_task_def_ctrl_jet(mp);
  hard_task_def_arg(mp,NULL);
  hard_task_def_wcet(mp, 500);
  hard_task_def_mit(mp, 10000);
  hard_task_def_usemath(mp);
  pid = task_create("cpuBatEDF1", movebatCPU, &mp, NULL);
  if (pid == NIL) {
        grx_close();
        perror("Could not create task <cpuBatEDF1>");
        sys_end();
  }
  else
    task_activate(pid);
}


/*--------------------------------------------------------------*/
/*                      The Game initializations                */
/*--------------------------------------------------------------*/

void scenario_game()
{
  int i;
  ball.xcur = 185; /*place ball in the horizontal centre of screen*/

  /*randomly choose the y cordinate of the ball
  this makes the game less predictable*/

  ball.ycur = myrand(400)+30;
  ball.xdir = myrand(1);        /*randomly choose the direction of ball*/

  ball.path_change = 0;

  dir = '\0';

  p1.points = 0; p2.points = 0;
  jump=2;

  for(i=0; i<=3; i++)
  {
        points[i] = '\0';
  }

  mutex_lock(&mutex);
  p1.x = 185; p1.y = 452;
  p2.x = 185; p2.y = 18;

/* This draws the two bats at their initial positions */
  grx_box(155,452,215,457,blue);
  grx_box(155,18,215,23,yellow);
  mutex_unlock(&mutex);

  ball.xcur=185;
  ball.ycur=185;
  mutex_lock(&mutex);
  grx_disc(ball.xcur, ball.ycur, R, white);
  mutex_unlock(&mutex);
}


void init_ball(int choice)
{
    KEY_EVT k;

    hardball();
    hardbatP1();
    if(choice==2)
        hardbatP2();
    else if(choice==1)
         cpubat();

}



/*--------------------------------------------------------------*/