Ejemplo n.º 1
0
/* The "joyrumble" command-line tool */
int main(int argc, char *argv[]){
	
  if (argc < 5)
    {
	  printf("JOYRUMBLE\nUsage: joyrumble [ joystick (1-8) ]  [strong motor magnitude (%%) ]  [weak motor magnitude (%%) ]  [duration (miliseconds) ]\n");
	return 0;
    }

 
    int arg_joy=atoi(argv[1]);
    short arg_strong=atoi(argv[2]);
    short arg_weak=atoi(argv[3]);
    int arg_time=atoi(argv[4]);
    
	if (arg_joy < 1)
		arg_joy=1;

	if (arg_joy > MAXJOY)
		arg_joy=MAXJOY;
	
	if (arg_time < 0)
		arg_time=0;
	
	if (arg_weak < 0)
		arg_weak=0;
	
	if (arg_weak > 100)
		arg_weak=100;
	
	if (arg_strong < 0)
		arg_strong=0;
	
	if (arg_strong > 100)
		arg_strong=100;


	joyrumble ( arg_joy, arg_strong, arg_weak, arg_time);
	
	usleep((arg_time+100)*1000);
	
	
	return 0;
	
}
Ejemplo n.º 2
0
int main(int argc, char * argv[])
{
    struct playfield * field = malloc(HEIGHT * WIDTH *
                                      sizeof(struct playfield));

    struct player pl[4];
    struct monster mons[MONSTERS];
    struct bomb bm[(3 * WIDTH * HEIGHT) / 4];
    struct controller_handle * ch;
    struct command cmd;
    struct bl_timer * timer;
    int frame = 0;
    int n_bombs = 0;
    int n_monsters = MONSTERS;
    int mx[4], my[4];
    int last_ctype = 0;
    int p = 0;
    int i, j, k, l;

    ch = open_controller(CONTROLLER_TYPE_STDIN);

    if (!ch) {
        fprintf(stderr, "No joysticks/controllers found.\n");
        return -1;
    }

    srand(time(NULL));

    for (i=0; i<PLAYERS; i++) {
        mx[i] = 0;
        my[i] = 0;
        pl[i].fire = BEGIN_FIRE;
        pl[i].max_bombs = BEGIN_BOMBS;
        pl[i].cur_bombs = 0;
        pl[i].has_red_bombs = 0;
        pl[i].alive = 1;
        pl[i].last_move = 0;
    }

    for (i=0; i<MONSTERS; i++) {
        mons[i].x = WIDTH-1;
        mons[i].y = 0;
        mons[i].speed = 10;
        mons[i].alive = 1;
    }

    pl[0].x = 0;
    pl[0].y = 0;
    pl[1].x = WIDTH - 1 - ((WIDTH+1) % 2);
    pl[1].y = HEIGHT - 1;
    pl[2].x = WIDTH - 1 - ((WIDTH+1) % 2);
    pl[2].y = 0;
    pl[3].x = ((HEIGHT+1) % 2);
    pl[3].y = HEIGHT - 1;

    populate_playfield(field, mons, n_monsters);

    timer = bl_timer_create();

    while(1) {
        cmd = read_command(ch, DELAY);

        if (cmd.controller >= 0)
            p = cmd.controller;

        if ((cmd.type == 2 && cmd.number == KEY_QUIT) ||
            (cmd.type == IN_TYPE_STDIN && cmd.number == KEY_QUIT_K))
            goto exit;

        if (cmd.type > 0)
            last_ctype = cmd.type;

        if (cmd.controller >= 0) {
            switch (cmd.number) {
            case KEY_UP_K1:
            case KEY_UP_K2:
            case KEY_UP:
                if (cmd.value > 0) {
                    mx[p] = 0;
                    my[p] = -1;
                } else {
                    mx[p] = 0;
                    my[p] = 0;
                    pl[p].last_move = 0;
                }
                break;
            case KEY_DOWN_K1:
            case KEY_DOWN_K2:
            case KEY_DOWN:
                if (cmd.value > 0) {
                    mx[p] = 0;
                    my[p] = 1;
                } else {
                    mx[p] = 0;
                    my[p] = 0;
                    pl[p].last_move = 0;
                }
                break;
            case KEY_LEFT_K1:
            case KEY_LEFT_K2:
            case KEY_LEFT:
                if (cmd.value > 0) {
                    mx[p] = -1;
                    my[p] = 0;
                } else {
                    mx[p] = 0;
                    my[p] = 0;
                    pl[p].last_move = 0;
                }
                break;
            case KEY_RIGHT_K1:
            case KEY_RIGHT_K2:
            case KEY_RIGHT:
                if (cmd.value > 0) {
                    mx[p] = 1;
                    my[p] = 0;
                } else {
                    mx[p] = 0;
                    my[p] = 0;
                    pl[p].last_move = 0;
                }
                break;
            case KEY_B1_K1:
            case KEY_B1_K2:
            case KEY_B1:
                if (pl[p].alive && pl[p].cur_bombs < pl[p].max_bombs &&
                    !is_bomb(pl[p].x, pl[p].y, bm, n_bombs)) {
                    bm[n_bombs].x = pl[p].x;
                    bm[n_bombs].y = pl[p].y;
                    bm[n_bombs].ticks = DEFAULT_BOMB_TICKS;
                    bm[n_bombs].is_red = pl[p].has_red_bombs;
                    bm[n_bombs].length = pl[p].fire;
                    bm[n_bombs].max_length[0] = bm[n_bombs].length;
                    bm[n_bombs].max_length[1] = bm[n_bombs].length;
                    bm[n_bombs].max_length[2] = bm[n_bombs].length;
                    bm[n_bombs].max_length[3] = bm[n_bombs].length;
                    bm[n_bombs].current_length = 0;
                    bm[n_bombs].from_player = p;
                    pl[p].cur_bombs++;
                    n_bombs++;
                }
                break;
            default:
                break;     
            }
        }

        if (bl_timer_elapsed(timer) > DELAY) {

            for (i=0; i<n_monsters; i++) {
                if (frame % mons[i].speed == 0) {
                    int dx, dy, nx, ny, npos;
                    int dir = rand() % 4;
                    npos = get_newpos_from_i(mons[i].x, mons[i].y, &dx, &dy, dir);
                    nx = mons[i].x + dx;
                    ny = mons[i].y + dy;
                    if (nx >= 0 && nx < WIDTH &&
                        ny >= 0 && ny < HEIGHT &&
                        field[npos].type == FIELD_TYPE_EMPTY) {
                        if (!is_bomb(nx, ny, bm, n_bombs)) {
                            mons[i].x = nx;
                            mons[i].y = ny;
                        }
                    }
                }
            }

            for (i=0; i<PLAYERS; i++) {
                if (frame > pl[i].last_move + 1) {
                    int nx, ny, npos;
                    nx = pl[i].x + mx[i];
                    ny = pl[i].y + my[i];
                    npos = ny * WIDTH + nx;
                    if (nx >= 0 && nx < WIDTH &&
                        ny >= 0 && ny < HEIGHT &&
                        field[npos].type == FIELD_TYPE_EMPTY) {
                        if (!is_bomb(nx, ny, bm, n_bombs)) {
                            if (pl[i].alive &&
                                field[npos].special != FIELD_SPECIAL_NONE) {
                                switch (field[npos].special) {
                                case FIELD_SPECIAL_FIRE:
                                    pl[i].fire++;
                                    break;
                                case FIELD_SPECIAL_BOMB:
                                    pl[i].max_bombs++;
                                    break;
                                case FIELD_SPECIAL_RED:
                                    pl[i].has_red_bombs = 1;
                                    break;
                                default:
                                    break;
                                }
                                field[npos].special = FIELD_SPECIAL_NONE;
                            }

                            pl[i].x = nx;
                            pl[i].y = ny;
                        }
                    }
                    if (mx[i] != 0 || my[i] != 0) {
                        pl[i].last_move = frame;
                    }
                }
            }

            for (i=0; i<n_bombs; i++) {
                if (bm[i].current_length == bm[i].length) {
                    pl[bm[i].from_player].cur_bombs--;
                    n_bombs--;
                    for (j=i; j<n_bombs; j++) {
                        bm[j] = bm[j+1];
                    }
                }
            }

            for (i=0; i<n_bombs; i++)
            {
                if (bm[i].ticks == 0) {
                    if (bm[i].current_length < bm[i].length) {
                        bm[i].current_length++;
                    }
                } else {
                    if (frame % 3 == 0) {
                        bm[i].ticks--;
#ifdef RUMBLE
                        if (bm[i].ticks == 0) {
                            for (j=0; j<PLAYERS; j++) {
                                double power;
                                double dist = sqrt(pow((bm[i].x - pl[j].x), 2) +
                                                   pow((bm[i].y - pl[j].y), 2));
                                power = dist / MAX_DISTANCE;
                                if (power > 0.2) {
                                    joyrumble(j+1, 100, 100, 500);
                                }
                            }
                        }
#endif
                    }
                }

                if (bm[i].ticks == 0)
                {
                    for (j=0; j<4; j++) {
                        int dx, dy;
                        int pos = get_newpos_from_i(bm[i].x, bm[i].y, &dx, &dy, j);
                        if (field[pos].type != FIELD_TYPE_HARDWALL) {
                            for (k=0; k<bm[i].current_length; k++) {
                                int nx = bm[i].x + k * dx;
                                int ny = bm[i].y + k * dy;
                                if (nx >= 0 && nx < WIDTH &&
                                    ny >= 0 && ny < HEIGHT) {
                                    int npos = ny * WIDTH + nx;

                                    if (k >= bm[i].max_length[j])
                                        break;

                                    if (!bm[i].is_red &&
                                        field[npos].type == FIELD_TYPE_WALL) {
                                        bm[i].max_length[j] = k + 1;
                                    }

                                    for (l=0; l<PLAYERS; l++) {
                                        if (pl[l].x == nx && pl[l].y == ny) {
                                            pl[l].alive = 0;
                                        }
                                    }
                                    for (l=0; l<n_monsters; l++) {
                                        if (mons[l].x == nx && mons[l].y == ny) {
                                            mons[l].alive = 0;
                                        }
                                    }
                                    for (l=0; l<n_bombs; l++)
                                    {
                                        if (bm[l].x == nx && bm[l].y == ny &&
                                            bm[l].ticks > 1) {
                                            bm[l].ticks = 1;
                                        }
                                    }

                                    if (field[npos].type == FIELD_TYPE_EMPTY &&
                                        k == bm[i].current_length - 1) {
                                        field[npos].special = FIELD_SPECIAL_NONE;
                                    }
                                    field[npos].type = FIELD_TYPE_EMPTY;
                                }
                            }
                        }
                    }
                }
            }

            for (i=0; i<n_monsters; i++) {
                for (j=0; j<PLAYERS;j++) {
                    if (mons[i].alive &&
                        mons[i].x == pl[j].x &&
                        mons[i].y == pl[j].y)
                    {
                        pl[j].alive = 0;
                    }
                }
            }

            draw_field(field, pl, bm, n_bombs, mons, n_monsters);
            frame++;
            bl_timer_start(timer);

            if (last_ctype == IN_TYPE_STDIN) {
                mx[p] = 0;
                my[p] = 0;
                pl[p].last_move = 0;
            }
            last_ctype = 0;
        }
    }

exit:
    free(field);
    close_controller(ch);

    return 0;
}