Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1664 pj 1
//////////////////////////////////////////////////////////////////
2
// This file is submitted to Prof. Buttazzo, Prof. G.Lipari and //
3
// Prof. P. Gai, as project submission on RTOS SHaRK programming//
4
//                                                              //
5
// Description: SHIP - BULLETS - ASTEROIDS simulation           //
6
// This file creates bullets and asteroids tasks. It Creates a  //
7
// ship which is controlled by keyboard. The ship fires bullets //
8
// and hence tries to destroy the asteroids. When the bullets   //
9
// hits asteroids, they are destroyed. Moreover, when an        //
10
// asteroid hits the ship, the game ends.                       //
11
//                                                              //
12
// Future work: More work has to be done to enhance this game to//
13
// be user friendly game.                                       //
14
//////////////////////////////////////////////////////////////////
15
/*
16
Project group members:
17
1. Kailash Kumar Sharma
18
2. RamaKrishnan  S.
19
3. Rekh Rao
20
*/
21
/**
22
 ------------
23
 Created on :2 nd June 2002
24
 File Name  :Asteroids.C
25
 Modified on:<none>
26
 Last update:<none>
27
 ------------
28
**/
29
 
30
/*
31
 */
32
 
33
/*--------------------------------------------------------------*/
34
/*              SIMULATION OF BULLETS                           */
35
/*--------------------------------------------------------------*/
36
 
37
#include <kernel/kern.h>
38
#include <drivers/glib.h>
39
#include <drivers/keyb.h>
40
#include <semaphore.h>
41
#include <stdlib.h>
42
#include <math.h>
43
 
44
/****************Beginning of Global Variable definition**************/
45
#define YMENU    10             /* menu level                   */
46
#define XMIN     50
47
#define XMAX     600
48
#define YMIN     100
49
#define YMAX     450
50
#define VEL      5              /* velocity of bullet           */
51
#define AD        4              /* The diameter of asteroid    */
52
#define BD        2              /* The diameter of bullet      */
53
#define ESC      27             /* ASCII code of ESCAPE key     */
54
#define MAX_B    35             /* max number of bullets        */
55
#define MAX_A    35             /* max number of asteroids      */
56
#define BULLETGROUP 1
57
#define ASTEROIDGROUP 1
58
 
59
double  tick = 1.0;                       /* system tick = 1 ms           */
60
int     bullet_period = 40000;            /* bullet task period           */
61
int     bullet_wcet = 1000;               /* bullet task wcet             */
62
int     asteroid_period = 20000;          /* asteroid task period         */
63
int     asteroid_wcet = 1000;             /* asteroid task wcet           */
64
int     asteroid_gp_period = 400000;      /* asteroid create task period  */
65
int     asteroid_gp_wcet = 1000;          /* asteroid create task wcet    */
66
int     sx1 = 52;         /* X1 coordinate of ship  */
67
int     sx2 = 72;         /* X2 coordinate of ship  */
68
int     sy1 = 300;        /* Y1 coordinate of ship  */
69
int     sy2 = 315;        /* Y2 coordinate of ship  */
70
int     a_num = 0;        /* Number of active asteroids */
71
int     b_num = 0;        /* Number of active bullets   */
72
int     astr_speed = 100000;        /* Delay counter for Asteroid        */
73
int     bult_speed = 10000;         /* Delay counter for Bullet          */
74
int     astr_rate  = 100000;        /* Delay counter for Asteroid create */
75
int     bullet_col = 15;            /* Bullet colour  */
76
int     ship_col = 12;              /* Ship   colour  */
77
int     points = 0;
78
int     game_finish = 0;
79
PID     pid, apid, mapid;           /* Task IDs       */
80
sem_t   mutex;
81
char    retbuf[20];
82
 
83
 
84
 
85
/*-----------------End of Global Variable definition------------------*/
86
/*
87
// This function draws bullet
88
*/
89
void    draw_bullet(int x, int y, int c)
90
{
91
	sem_wait(&mutex);
92
	grx_disc(x, y, BD, c);
93
	sem_post(&mutex);
94
}
95
 
96
/*
97
// This function draws asteroid
98
*/
99
void    draw_asteroid(int x, int y, int c)
100
{
101
	sem_wait(&mutex);
102
	grx_disc(x, y, AD, c);
103
	sem_post(&mutex);
104
}
105
 
106
/*
107
// This function draws ship
108
*/
109
void    draw_ship(int x1, int y1, int x2, int y2, int c)
110
{
111
        sem_wait(&mutex);
112
        grx_box(x1, y1, x2, y2, c);
113
        sem_post(&mutex);
114
}
115
 
116
/*
117
// This is an asteroid task. This task is responsible for controlling
118
// the game. This task checks if it encouters a bullet or ship, while
119
// travelling. If the encoutered object is a bullet, the task gets
120
// destroyed. If the encoutered object is ship, the game terminates!!!
121
*/
122
TASK    asteroid(void *arg)
123
{
124
int     x=0, y=0;
125
int     ox=0, oy=0;
126
int     col=0;
127
int     outx=0;
128
int     i = (int)arg;
129
int     j;
130
int     scan_col1, scan_col2, scan_col3;
131
int     scan_col4, scan_col5, scan_col6;
132
 
133
        x = ox = (XMAX-10);  /* X coordinate of the asteroid   */
134
        y = oy = (YMIN+10+(rand()%(YMAX-YMIN-10)));          /*  y = [YMIN,YMAX] */
135
        col = 2 + i;           /* color of asteroid           */
136
 
137
        while (1) {
138
                x -= 5;
139
                outx = (x<=XMIN);
140
                // **** START for scanning for bullets and ship ****
141
                scan_col1 = grx_getpixel(x-4, y-4);
142
                scan_col2 = grx_getpixel(x-4, y+4);
143
                scan_col3 = grx_getpixel(x-7, y);
144
                scan_col4 = grx_getpixel(x-4, y-4);
145
                scan_col5 = grx_getpixel(x-4, y+4);
146
                scan_col6 = grx_getpixel(x-7, y);
147
                // **** END for scanning for bullets and ship ****
148
 
149
                if((scan_col1 == ship_col) || (scan_col2 == ship_col) ||
150
                              (scan_col3 == ship_col)){
151
                              game_finish = 1;
152
                }
153
                else if((outx) || (game_finish == 1)){
154
                  draw_asteroid(ox, oy, 0);
155
                  a_num--;
156
                  break;
157
                }
158
                else if((scan_col4 == bullet_col) ||
159
                             (scan_col5 == bullet_col) ||
160
                             (scan_col6 == bullet_col)) {
161
                  draw_asteroid(ox, oy, 0);
162
                  a_num--;
163
                  points++;
164
                  break;
165
                }
166
                else {
167
                draw_asteroid(ox, oy, 0);
168
                draw_asteroid(x, y, col);
169
		ox = x; oy = y;
170
                }
171
                for(j=0; j <= astr_speed; j++);
172
		task_endcycle();
173
        }
174
}
175
/*
176
// Function to convert integer to string
177
*/
178
char *itos(int n){
179
sprintf(retbuf," %d ", n);
180
return(retbuf);
181
}
182
 
183
/*
184
// This task is created by main and runs in parallel with it. It
185
// is responsible for creating asteroids at random location periodically.
186
*/
187
TASK    a_gp(void *arg)
188
{
189
int     j;
190
HARD_TASK_MODEL n;
191
 
192
        while (1) {
193
            if (game_finish == 1) break;
194
            else if (a_num < MAX_A) {
195
                grx_text("POINTS", XMAX-150, YMENU+50, 13, 0);
196
                grx_text(itos(points), XMAX-145, YMENU+65, 14, 0);
197
	        hard_task_default_model(n);
198
	        hard_task_def_ctrl_jet (n);
199
	        hard_task_def_arg      (n, (void *)a_num);
200
                hard_task_def_wcet     (n, asteroid_wcet);
201
                hard_task_def_mit      (n, asteroid_period);
202
                hard_task_def_group    (n, ASTEROIDGROUP);
203
	        hard_task_def_usemath  (n);
204
                mapid = task_create("asteroid", asteroid, &n, NULL);
205
	        if (mapid == NIL) {
206
	           grx_close();
207
                   perror("Could not create task <asteroid>");
208
	           sys_abort(1);
209
                }
210
            task_activate(mapid);
211
	    a_num++;
212
            for (j=0; j <= astr_rate; j++);
213
            }
214
	    task_endcycle();
215
        }
216
}
217
 
218
/*
219
// This is a bullet task. It traverses it originates from the ship and
220
// and traverses its path from left to right. It hits the asteroids and
221
// destroys them.
222
*/
223
TASK    bullet(void *arg)
224
{
225
int     x=0, y=0;
226
int     ox=0, oy=0;
227
int     outx=0;
228
int     j;
229
 
230
        x = ox = (sx2+(2*BD));  /* Fire bullet from sx2 coordinate of ship */
231
        y = oy = (sy2-8);       /* Fire bullet from sy2 coordinate of ship */
232
//        col = 2 + i;            /* color of bullet*/
233
 
234
        while (1) {
235
                x += 5;
236
                outx = (x>=XMAX);
237
                if((outx) || (game_finish == 1)) {
238
                  draw_bullet(ox, oy, 0);
239
                  b_num--;
240
                  break;
241
                }
242
                else {
243
                draw_bullet(ox, oy, 0);
244
                draw_bullet(x, y, bullet_col);
245
		ox = x; oy = y;
246
                }
247
                for(j=0; j <= bult_speed; j++);
248
		task_endcycle();
249
        }
250
}
251
 
252
/*
253
// This function is called when the system exits
254
*/
255
void byebye(void *arg)
256
{
257
  grx_close();
258
  kern_printf("Your ship is hit!!!Bye Bye!\n");
259
}
260
 
261
/****************************** MAIN ******************************/
262
/*
263
// MAIN is responsible for creating ship. It controls the motion of
264
// ship and fires bullets (creates bullet task) to destroy asteroids.
265
*/
266
int main(int argc, char **argv)
267
{
268
    HARD_TASK_MODEL m;
269
    HARD_TASK_MODEL a;
270
    char c;             /* character from keyboard      */
271
    TIME seme;          /* used to init the random seed */
272
 
273
    /* Set the exception handler */
274
    set_exchandler_grx();
275
 
276
    /* Set the closing function */
277
    sys_atrunlevel(byebye, NULL, RUNLEVEL_BEFORE_EXIT);
278
 
279
    /* graphic card Initialization */
280
    if (grx_init() < 1) {
281
       sys_abort(1);
282
    }
283
 
284
    if (grx_open(640, 480, 8) < 0) {
285
	kern_printf("GRX Err\n");
286
	sys_abort(1);
287
    }
288
    kern_printf("Video card ok!\n");
289
 
290
    /* Intialization of graphics window */
291
    grx_rect(XMIN-BD-1, YMIN-BD-1, XMAX+BD+1, YMAX+BD+1, 14);
292
    grx_text("Simulation of a SHIP-BULLETS-ASTEROIDS",
293
                         XMIN, YMENU+10, 13, 0);
294
    grx_text("SPACE create a bullet", XMIN, YMENU+20, 12, 0);
295
    grx_text("ESC   exit to DOS"    , XMIN, YMENU+30, 14, 0);
296
    grx_text("Developers: Kailash Kumar Sharma",
297
                         XMIN, YMENU+40, 12, 0);
298
    grx_text("            Ramakrishnana S.    ",
299
                         XMIN, YMENU+50, 12, 0);
300
    grx_text("            Rekha Rao            ",
301
                         XMIN, YMENU+60, 12, 0);
302
    grx_text("UP    :Press E", XMIN+40, YMAX+10, 12, 0);
303
    grx_text("DOWN  :PRESS C", XMIN+40, YMAX+20, 12, 0);
304
    grx_text("LEFT  :PRESS S", XMAX-200, YMAX+10, 12, 0);
305
    grx_text("RIGHT :PRESS F", XMAX-200, YMAX+20, 12, 0);
306
 
307
    /* Draws a default ship */
308
    draw_ship(sx1, sy1,  sx2, sy2, ship_col);
309
 
310
    /* randomize!!!! */
311
    seme = sys_gettime(NULL);
312
    srand(seme);
313
 
314
    /******Creating a task that creates Asteroids randomly*******/
315
    hard_task_default_model(a);
316
    hard_task_def_ctrl_jet (a);
317
    hard_task_def_arg      (a, (void *)a_num);
318
    hard_task_def_wcet     (a, asteroid_wcet);
319
    hard_task_def_mit      (a, asteroid_gp_period);
320
    hard_task_def_usemath  (a);
321
    apid = task_create("a_gp", a_gp, &a, NULL);
322
    if (apid == NIL) {
323
        grx_close();
324
        perror("Could not create task <asteroid>");
325
        sys_abort(1);
326
    }
327
    task_activate(apid);
328
    /******END of Creating a task that creates Asteroids randomly*******/
329
 
330
    /******* Infinite loop to create bullets and move the ship *******/
331
    do {
332
        if(game_finish == 1) {
333
        grx_text("GAME OVER !!! ", 300, 300, 13, 0);
334
        break;
335
        }
336
        c = keyb_getch(BLOCK);
337
        // **** Begin of code to fire bullet ****
338
 	if ((c == ' ') && (b_num < MAX_B)) {
339
	    hard_task_default_model(m);
340
	    hard_task_def_ctrl_jet (m);
341
	    hard_task_def_arg      (m, (void *)b_num);
342
            hard_task_def_wcet     (m, bullet_wcet);
343
            hard_task_def_mit      (m, bullet_period);
344
            hard_task_def_group    (m, BULLETGROUP);
345
	    hard_task_def_usemath  (m);
346
 
347
            pid = task_create("bullet", bullet, &m, NULL);
348
	    if (pid == NIL) {
349
	      grx_close();
350
              perror("Could not create task <bullet>");
351
	      sys_abort(1);
352
	    }
353
	    task_activate(pid);
354
	    b_num++;
355
 
356
        }
357
        //**** END of code to fire bullet ****
358
        //**** BEGIN of code to control the position of ship ****
359
        else if ((c == 's') && (sx1 > (XMIN+10))) {
360
            draw_ship(sx1,sy1,sx2,sy2,0);
361
            sx1 -=5; sx2-=5;
362
            draw_ship(sx1,sy1,sx2,sy2,12);
363
        }
364
        else if ((c == 'f') && (sx2 < (XMAX-10))) {
365
            draw_ship(sx1,sy1,sx2,sy2,0);
366
            sx1+=5; sx2+=5;
367
            draw_ship(sx1,sy1,sx2,sy2,12);
368
        }
369
        else if ((c == 'e') && (sy1 > (YMIN+10))) {
370
            draw_ship(sx1,sy1,sx2,sy2,0);
371
            sy1-=5; sy2-=5;
372
            draw_ship(sx1,sy1,sx2,sy2,12);
373
        }
374
        else if ((c == 'c') && (sy2 < (YMAX-10))) {
375
            draw_ship(sx1,sy1,sx2,sy2,0);
376
            sy1+=5; sy2+=5;
377
            draw_ship(sx1,sy1,sx2,sy2,12);
378
        }
379
        //**** END of code to control the position of ship ****
380
 
381
    } while (c != ESC);
382
    /******* END of Infinite loop to create bullets *******/
383
    do{
384
    c = keyb_getch(BLOCK);
385
    } while (c != ESC);
386
    sys_end();
387
    return 0;
388
}
389
 
390
/*----------------END OF PROGRAM--------------------------------------*/