Пример #1
0
void game_event(SDL_Event *event) {
    SDL_PollEvent(event);
    switch (event->type) {
        case SDL_QUIT:
            quit_flag = 1;
            break;
        case SDL_KEYDOWN:
            switch (event->key.keysym.sym) {
                case SDLK_w:
                    player_move(KEY_UP);
                    break;
                case SDLK_s:
                    player_move(KEY_DOWN);
                    break;
                case SDLK_a:
                    player_move(KEY_LEFT);
                    break;
                case SDLK_d:
                    player_move(KEY_RIGHT);
                    break;
            }
            break;
        case SDL_KEYUP:
            break;
    }
}
Пример #2
0
//Handles input relating to in-game.
void game_controller_game_input(GameController self) {
	hidScanInput();
	u32 kDown = hidKeysDown();
	u32 kHeld = hidKeysHeld();


	if (kHeld & KEY_A) { //A
		player_shoot(self->player, self->bullet_storage);
	}
	if (kHeld & KEY_B) { //B
	}
	if (kHeld & KEY_B) { //B
	}
	if (kDown & KEY_X) { //X
	}
	if (kDown & KEY_Y) { //Y
	} 
	
	
	
	if (kHeld & KEY_L) { //Controls player focus mode.
		self->player->flag_focus = true;
	} else {
		self->player->flag_focus = false;
	}
	
	
	
	if (kHeld & KEY_UP) { //up
		player_move(self->player, NORTH);
	} 
	if (kHeld & KEY_DOWN) { //down
		player_move(self->player, SOUTH);
	} 
	if (kHeld & KEY_LEFT) { //left
		player_move(self->player, WEST);
	} 
	if (kHeld & KEY_RIGHT) { //right
		player_move(self->player, EAST);
	} 
	
	
	
	if (kDown & KEY_START) { //start
		self->is_paused = !self->is_paused;
	} 
	if (kDown & KEY_SELECT) { //select
		//ZZZ
		self->flag_external_state = MAIN_MENU;
	}

}
Пример #3
0
void play_game() {
	char name[50];
	printf("Hi!\n\nI am Z3TA.\n\nWhat's your name: ");
	scanf("%s", name);
	int play_first = 0;
	printf("\n\nDo you want to play first? 1 - Yes, 0 - No:  ");
	scanf("%d", &play_first);
	printf("\n\n\nX: Z3TA\nO: %s\n\n\n",name);
	if(!play_first) {
			while(!board_full(board) && !check_win(board)) {
			computer_move();
			printf("Z3TA just made a move...\n");
			print_board();
			if(board_full(board) || check_win(board)) {
				break;
			}
			player_move();
			printf("%s just made a move...\n", name);
			print_board();
		}
	}

	else {
			while(!board_full(board) && !check_win(board)) {
			player_move();
			printf("%s just made a move...\n", name);
			print_board();
			if(board_full(board) || check_win(board)) {
				break;
			}
			computer_move();
			printf("Z3TA just made a move...\n");
			print_board();
		}
	}

	printf("\n\n------------------------------\n\n");
	if(check_win(board) == 0) {
		printf("It's a DRAW!\n");
	}
	else if(check_win(board) == 1) {
		printf("%s WON!\n", name);
	}
	else {
		printf("Z3TA WON!\n");
	}
	printf("\n------------------------------\n");
}
Пример #4
0
double computer_move(const Grid& grid, int depth)
{
	double total_score = 0;
	double total_weight =0;
	
	// Pruning trackers
	Cache cache;
	for(int x = 0; x < 4; x++)
	{
		for(int y = 0; y < 4; y++)
		{
			if (grid.cells[index(x, y)] == 0)
			{
				for(int i = 0; i < MOVES_COUNT; i++)
				{
					Grid player_grid = grid;
					player_grid.cells[index(x, y)] = MOVES[i];

					double score = player_move(player_grid, cache, depth - 1);
					total_score += PROBABILITIES[i] * score; // Weighted average. This is the essence of expectimax.
					total_weight += PROBABILITIES[i]; // Weighted average. This is the essence of expectimax.
				}
			}
		}
	}

	return total_weight == 0 ? 0 : total_score / total_weight;
}
Пример #5
0
static void		player_play(t_game *game, int turn)
{
	if ((turn % 2) + 1 == game->ai)
		ai_move(game, (turn % 2) + 1);
	else
		player_move(game, (turn % 2) + 1);
}
Пример #6
0
int main(void){
	
	int choice;
	char done;
	printmenu();
	printf("play??\n");
	scanf("%d, &choice");
	if(choice == 2)
		return;
	done =  ' ';
	init_matrix();

	do{
        	display_matrix();
        	player_move();
  		/* see if player is winner */
    		done = check(); 
    		if(done != ' ') 
			break; /* player is winner!*/
    		computer_move();
    		done = check(); /* see if computer is winner */	
	} 
	while(done == ' ');
	if(done == 'X') 
   		printf("-----YOU WON!!!-----\n");
        else{
		printf("-----I WON!!!!-----\n");
		printf("-----OOPS!!! YOU LOSE...TRY NEXT TIME-----\n");
		printmenu();
	}
   		display_matrix(); /* show final positions */

        return 0;
}
Пример #7
0
void play(node *node_start){

    printf("\t-------------Welcome to Cube Game-------------\n\n\n");
    printf("CPU is playing first\n\n");
    while(isTerminalNode(node_start)==0){

            if(node_start->score==10){
                if(node_start->left->score==10){
                        node_start=node_start->left;
                }
                else{
                        node_start=node_start->right;
                }
            }
            else
                node_start=node_start->left;

            printf("CPU remove cubes\n");
            if(node_start->num_of_cubes>=0)
                printf("Now the number of cubes is %d\n\n\n",node_start->num_of_cubes);

            if(isTerminalNode(node_start)==0)
                node_start=player_move(node_start);

    }
    if(node_start->previous->type==0){
        printf("Winner is CPU\n");

    }
    else{
        printf("Winner is PLAYER\n");
    }
}
int main(void)
{
    TicTacToe game;
    int size;

    printf("This is the game of Tic Tac Toe.\n");
    printf("You will be playing against the computer.\n");

    printf("Enter the size of the board: ");
    scanf("%d", &size);
    printf("The game board is %d by %d.\n", size, size);

    init_game(&game, size); // initialise the board

    int done;
    do {
        print_game(game);
        do {
            done = player_move(&game);
        } while (!done); // loop until valid move
        if(check(&game) != FALSE) break; // was a winner or a draw
        computer_move(&game);
        if(check(&game) != FALSE) break; // was a winner or a draw
    } while (TRUE);

    print_result(game);
    print_game(game); // show final positions

    free_game(&game);

    return 0;
}
Пример #9
0
int main()
{
   int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = { {0} };
   int player_num, computer_num;
   int last_move; 


   /* Ask Alice if she wants to go first */
   player_num = print_welcome();
   if (player_num == 1) computer_num = 2;
   else computer_num = 1;

   /* If Alice wants to go first, let her make a move */
   if (player_num == 1)
   {
      display_board(board);
      last_move = player_move(board,player_num);
      display_board(board);
   }


   /* The main loop */
   
   while (1)
   {
      /* Make a computer move, then display the board */
      last_move = random_move(board,computer_num);
      printf("Computer moved in column: %d\n", last_move);
      display_board(board);

      /* Check whether the computer has won */
      if (check_win_or_tie(board,last_move)) return 0;


      /* Let Alice make a move, then display the board */
      last_move = player_move(board,player_num);
      display_board(board);

      /* Check whether Alice has won */
      if (check_win_or_tie(board,last_move)) return 0;


   } /* end of while (1) */

} /* end of main() */
Пример #10
0
void Player::move(Map *map)
{
    if (is_morphing()) {
        m_morph->move(map);
    }
    else {
        player_move(map);
    }
}
Пример #11
0
void game_tick(Game *game) {
    int ch = 0, move_result = BALL_MOVED;

    long elapsed = elapsed_since(&game->start_time);
    static long last_event = 0;

    if ((elapsed - last_event) >= (BALL_DELAY * MICROSECONDS_PER_SECOND)) {
        move_result = ball_move(game->ball, game->field, game->player);

        if (move_result == BALL_SCORE) {
          game->player->score += 1;
        } else if (move_result == BALL_MISS) {
          game->player->score = 0;
          ball_reset(game->ball, game->field);
        }

        last_event = elapsed;
    }

    if ((ch = getch()) != ERR) {
        if (ch == MOVE_UP) {
            player_move(game->field, game->player, -1);
        }

        if (ch == MOVE_DOWN) {
            player_move(game->field, game->player, 1);
        }
    }

    // field_refresh(game->field);

    field_redraw(game->field);

    wclear(game->field->game);

    field_draw_score(game->field, game->player);
    wrefresh(game->field->score);

    player_draw(game->player, game->field);
    ball_draw(game->ball, game->field);

    wrefresh(game->field->game);
}
Пример #12
0
static sexp sexp_player_move_stub (sexp ctx, sexp self, sexp_sint_t n, sexp arg0, sexp arg1, sexp arg2) {
  sexp res;
  if (! (sexp_pointerp(arg0) && (sexp_pointer_tag(arg0) == sexp_unbox_fixnum(sexp_opcode_arg1_type(self)))))
    return sexp_type_exception(ctx, self, sexp_unbox_fixnum(sexp_opcode_arg1_type(self)), arg0);
  if (! sexp_exact_integerp(arg1))
    return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg1);
  if (! sexp_exact_integerp(arg2))
    return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg2);
  res = ((player_move((struct player**)sexp_cpointer_value(arg0), sexp_uint_value(arg1), sexp_uint_value(arg2))), SEXP_VOID);
  return res;
}
Пример #13
0
/* Loop through 9 turns or until somebody wins. */
void play_game(void)
{
  int turn;

  for (turn = 1; turn <= 9; turn++)
    {
      /* Check if turn is even or odd to determine which player should move. */
      if (turn % 2 == 1)
	{
	  if (computer == 'X')
	    computer_move();
	  else
	    player_move();
	}
      else
	{
	  if (computer == 'O')
	    computer_move();
	  else
	    player_move();
	}

      draw_board();

      if (symbol_won(computer)) {
	printf("\nI WIN!!!\n\n");
	return;
      }
      else if (symbol_won(user)) {
	printf("\nCongratulations, you win!\n\n");
	return;
      }
    }

  printf("\nThe game is a draw.\n\n");
  return;
}
Пример #14
0
int pong_game_tick(struct pong_game *g, int cmd[2]) {
    player_move(&g->players[0], cmd[0], g->grid.height);
    player_move(&g->players[1], cmd[1], g->grid.height);
    ball_move(&g->ball);
    ball_hits_player(&g->ball, &g->players[0]);
    ball_hits_player(&g->ball, &g->players[1]);
    switch (ball_hits_wall(&g->ball, g->grid)) {
    case -1:
        g->players[1].score++;
        g->ball.pos = g->ball.init;
        break;
    case 1:
        g->players[0].score++;
        g->ball.pos = g->ball.init;
        break;
    default:
        return 0;
    }
    if (g->players[0].score >= g->target_score)
        return -1;
    if (g->players[1].score >= g->target_score)
        return 1;
    return 0;
}
Пример #15
0
void player_applymovement(Player* plr) {
	if(plr->deathtime < -1)
		return;
	
	plr->moving = False;
	
	int up		=	plr->moveflags & MOVEFLAG_UP,
		down	=	plr->moveflags & MOVEFLAG_DOWN,
		left	=	plr->moveflags & MOVEFLAG_LEFT,
		right	=	plr->moveflags & MOVEFLAG_RIGHT;
	
	if(left && !right) {
		plr->moving = True;
		plr->dir = 1;
	} else if(right && !left) {
		plr->moving = True;
		plr->dir = 0;
	}	
	
	complex direction = 0;
	
	if(up)		direction -= 1I;
	if(down)	direction += 1I;
	if(left)	direction -= 1;
	if(right)	direction += 1;
		
	if(cabs(direction))
		direction /= cabs(direction);
	
	if(direction)
		player_move(&global.plr, direction);
	
	// workaround
	if(global.replaymode == REPLAY_RECORD) {
		Uint8 *keys = SDL_GetKeyState(NULL);
		
		if(!keys[tconfig.intval[KEY_SHOT]] && plr->fire) {
			player_event(plr, EV_RELEASE, KEY_SHOT);
			replay_event(&global.replay, EV_RELEASE, KEY_SHOT);
		}
		
		if(!keys[tconfig.intval[KEY_FOCUS]] && plr->focus > 0) {
			player_event(plr, EV_RELEASE, KEY_FOCUS);
			replay_event(&global.replay, EV_RELEASE, KEY_FOCUS);
		}
	}
}
Пример #16
0
void update_player()
{
    switch(get_last_input()) {
        case INPUT_DIRECTIONAL:
            player_move(get_last_direction());
            break;
        case INPUT_ACTION:
            player_act();
            break;
    }
    if(hp_current < hp_max)
        hp_current++;
    if(hp_current > hp_max)
        hp_current = hp_max;
    if(ep_current > ep_max)
        ep_current = ep_max;
}
Пример #17
0
// implement a very simple client that sends and receives
// all other logic, just send and receive strings
// extension is to add extra clients
int main(void)
{
    // define our variables related to pipes
    int clientfd, serverfd;
    char *clientpipe = "clientpipe";
    char *serverpipe = "serverpipe";
    char buf[MAX_BUF];
    int size;

    // create the FIFO (named pipe) and open for reading
    mkfifo(clientpipe, 0666);
    mkfifo(serverpipe, 0666);
    serverfd = open(serverpipe, O_WRONLY);
    clientfd = open(clientpipe, O_RDONLY);

    printf("This is the game of Tic Tac Toe.\n");
    printf("You will be playing against the computer\n");

    read(clientfd, buf, MAX_BUF);
    printf("%s", buf);

    size = init_game(serverfd);

    int done;  // used to check validity of move
    int game_over; // used to check if game has completed

    // client must send commands and wait for responses
    // client exits loop if game is over
    // client should continue if receives continue message
    do {
        print_game(clientfd, size);
        do {
            done = player_move(clientfd, serverfd);
        } while (!done); // loop until valid move
        if (check(clientfd) != FALSE) break;
    } while (TRUE);

    print_game(clientfd, size);
    print_winner(clientfd);

    // tidy up
    close(clientfd);
    unlink(clientpipe);

    return 0;
}
Пример #18
0
/* Requirement 3 - controls the flow of play in the game */
void play_game(void)
{
	do
	{
		enum move_result move_result = SUCCESSFUL_MOVE;
		enum cell_contents board[BOARD_HEIGHT][BOARD_WIDTH];
		init_board(board);

		/* game loop */
		while (!is_game_over(board) && move_result != QUIT_GAME)
			move_result = player_move(board);

		/* if result is QUIT_GAME, break prematurely */
		if (move_result == QUIT_GAME)
			break;

		display_result(board);
	}
	while (process_ask_play_again());
}
Пример #19
0
// free-axis movement
int player_applymovement_gamepad(Player *plr) {
	if(!plr->axis_lr && !plr->axis_ud)
		return False;
	
	complex direction = (plr->axis_lr + plr->axis_ud*I) / (double)GAMEPAD_AXIS_RANGE;
	if(cabs(direction) > 1)
		direction /= cabs(direction);
	
	double real = creal(direction);
	double imag = cimag(direction);
	int sr = SIGN(real);
	int si = SIGN(imag);
	
	player_setmoveflag(plr, KEY_UP,		si == -1);
	player_setmoveflag(plr, KEY_DOWN,	si ==  1);
	player_setmoveflag(plr, KEY_LEFT,	sr == -1);
	player_setmoveflag(plr, KEY_RIGHT,	sr ==  1);
	
	if(direction)
		player_move(&global.plr, direction);
	
	return True;
}
Пример #20
0
void player_applymovement(Player *plr) {
	if(plr->deathtime < -1)
		return;
	
	int gamepad = player_applymovement_gamepad(plr);
	plr->moving = False;
	
	int up		=	plr->moveflags & MOVEFLAG_UP,
		down	=	plr->moveflags & MOVEFLAG_DOWN,
		left	=	plr->moveflags & MOVEFLAG_LEFT,
		right	=	plr->moveflags & MOVEFLAG_RIGHT;
	
	if(left && !right) {
		plr->moving = True;
		plr->dir = 1;
	} else if(right && !left) {
		plr->moving = True;
		plr->dir = 0;
	}
	
	if(gamepad)
		return;
	
	complex direction = 0;
	
	if(up)		direction -= 1I;
	if(down)	direction += 1I;
	if(left)	direction -= 1;
	if(right)	direction += 1;
	
	if(cabs(direction))
		direction /= cabs(direction);
	
	if(direction)
		player_move(&global.plr, direction);
}
Пример #21
0
void game_display(struct game* game) {
	assert(game);

	int map_w, map_h;

	window_clear();

	if(game->nb_player == 1) {
		map_w = map_get_width(level_get_curr_map(game->curr_level));
		map_h = map_get_height(level_get_curr_map(game->curr_level));
		for(int i = 0; i < map_w; i++)
			for(int j = 0; j < map_h+2; j++)
				window_display_image(sprite_get_empty(), i * SIZE_BLOC, j * SIZE_BLOC);
	}

	level_display(game_get_curr_level(game));

	monster_display(level_get_curr_map(game->curr_level));

	bomb_display(game, level_get_curr_map(game->curr_level));

	game_banner_display(game);

	if(game->nb_player == 1) { // Single player
		struct player* player = game->players[0];

		// Always display
		player_display(player);

		if(game->game_state == PLAYING) {
			player_move(game, player, level_get_curr_map(game->curr_level));
			monster_move(game, level_get_curr_map(game->curr_level), player);

			player_update(player);
			monster_update(level_get_curr_map(game->curr_level));
		}
	}
	else { // Multi player
		struct player* players_in_order[game->nb_player];
		for(int i=0; i<game->nb_player; i++)
			players_in_order[i] = game->players[i];
		game_order_players_array(game, players_in_order);

		for(int i = 0; i < game->nb_player; i++) {

			player_display(players_in_order[i]);

			if(game->game_state == PLAYING) {
				player_move(game, players_in_order[i], level_get_curr_map(game->curr_level));

				player_update(players_in_order[i]);
			}
		} // end for each player


	} // end Multi player

	if(game->game_state == PLAYING) {

		bomb_update(level_get_curr_map(game->curr_level));

	}
	else if(game->game_state == PAUSED) {
		map_w = map_get_width(level_get_curr_map(game->curr_level));
		map_h = map_get_height(level_get_curr_map(game->curr_level));
		int mid_w = map_w / 2 * SIZE_BLOC + map_w%2 * SIZE_BLOC / 2;
		int mid_h = map_h / 2 * SIZE_BLOC + map_h%2 * SIZE_BLOC / 2;
		menu_display(mid_w, mid_h);

	}
	window_refresh();
}
Пример #22
0
void tick()
{
	double dt = delta_time();
	physics_tick(dt);
	player_move(dt);
}
Пример #23
0
/*! \brief Actions for one entity
 * \date    20040310 PH added TARGET movemode, broke out chase into separate function
 *
 * Process an individual active entity.  If the entity in question
 * is #0 (main character) and the party is not automated, then allow
 * for player input.
 *
 * \param   target_entity Index of entity
 * \date    20040310 PH added TARGET movemode, broke out chase into separate function
 */
static void process_entity(t_entity target_entity)
{
    s_entity *ent = &g_ent[target_entity];
    s_player *player = 0;

    ent->scount = 0;

    if (!ent->active)
    {
        return;
    }

    if (!ent->moving)
    {
        if (target_entity == 0 && !autoparty)
        {
            player_move();
            if (ent->moving && display_desc == 1)
            {
                display_desc = 0;
            }
            return;
        }
        switch (ent->movemode)
        {
            case MM_STAND:
                return;
            case MM_WANDER:
                wander(target_entity);
                break;
            case MM_SCRIPT:
                entscript(target_entity);
                break;
            case MM_CHASE:
                chase(target_entity);
                break;
            case MM_TARGET:
                target(target_entity);
                break;
        }
    }
    else                         /* if (.moving==0) */
    {
        if (ent->tilex * TILE_W > ent->x)
        {
            ++ent->x;
        }
        if (ent->tilex * TILE_W < ent->x)
        {
            --ent->x;
        }
        if (ent->tiley * TILE_H > ent->y)
        {
            ++ent->y;
        }
        if (ent->tiley * TILE_H < ent->y)
        {
            --ent->y;
        }
        ent->movcnt--;

        if (ent->framectr < 20)
        {
            ent->framectr++;
        }
        else
        {
            ent->framectr = 0;
        }

        if (ent->movcnt == 0)
        {
            ent->moving = 0;
            if (target_entity < PSIZE)
            {
                player = &party[pidx[target_entity]];
                if (steps < STEPS_NEEDED)
                {
                    steps++;
                }
                if (player->sts[S_POISON] > 0)
                {
                    if (player->hp > 1)
                    {
                        player->hp--;
                    }
                    play_effect(21, 128);
                }
                if (player->eqp[EQP_SPECIAL] == I_REGENERATOR)
                {
                    if (player->hp < player->mhp)
                    {
                        player->hp++;
                    }
                }
            }
            if (target_entity == 0)
            {
                zone_check();
            }
        }

        if (target_entity == 0 && vfollow == 1)
        {
            calc_viewport(0);
        }
    }
}
Пример #24
0
int main(int argc, char *argv[])
{
	int k;
	struct coord c;
	unsigned int i;
	unsigned char turn_taken = 1, running = 0;

	seed_rng();

	display_init();

	if(player_init() == -1) return 0;

	level_init(&levels[0], 8);
	player_set_level(&levels[0]);

	while(1) {
		main_clear();
		player_see();
		player_status();

		turn_taken = 0;

		if(!running) k = display_getch();

		msg_clear();

		switch(k) {
		case '8':
		case '9':
		case '6':
		case '3':
		case '2':
		case '1':
		case '4':
		case '7':
		case KEY_UP:
		case KEY_DOWN:
		case KEY_LEFT:
		case KEY_RIGHT:
			c = key_to_direction(k);
			if(!player_move(c.x, c.y)) running = 0;
			turn_taken = 1;
			break;
		case 'c':
			c = key_to_direction(ask_key("In which direction?"));
			msg_clear();
			player_close(c.x, c.y);
			turn_taken = 1;
			break;
		case 'o':
			c = key_to_direction(ask_key("In which direction?"));
			msg_clear();
			player_open(c.x, c.y);
			turn_taken = 1;
			break;
		case 'k':
			c = key_to_direction(ask_key("In which direction?"));
			msg_clear();
			player_kick(c.x, c.y);
			turn_taken = 1;
			break;
		case 'g':
		case 'G':
			k = ask_key("In which direction?");
			c = key_to_direction(k);
			if((c.x || c.y) && player_move(c.x, c.y)) running = 1;
			turn_taken = 1;
			break;
		case ':':
			player_look();
			break;
		case ';':
			msg_printf("Pick an object...");
			c = player_select_square();
			msg_clear();
			player_remote_look(c.x, c.y);
			break;
		case '.':
			turn_taken = 1;
			break;
		}


		if(turn_taken) {
			if(player_poll()) running = 0;

			for(i = 0; i < levels[0].nmonst; i++)
				if(monster_poll(levels[0].monsters[i]))
					running = 0;
		}
	}

	return 0;
}
Пример #25
0
int main_deathmatch()
{
	SDL_Event e;

	SDL_ShowCursor(SDL_DISABLE);

	entity_initialize_list(1024);
	tank_initialize_list(15);

	g_current_level = level_load("leveloneconfig");
	render_set_background(g_current_level->background->image);

	g_music = audio_load_music("sounds/music/backgroundmusic.wav");
	g_sound = audio_load_sound("sounds/digital/digital.wav");
	if(g_music->file.music != NULL)
	{
		slog("Playing Music");
		//audio_play_music(g_music->file.music);
	}
	else
		slog("Could not load music file");

	player = player_spawn("Player", PLAYER);
	v2d_set(player->tank->tracks->body->position, SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
	v2d_set(player->tank->turret->body->position, SCREEN_WIDTH/2, SCREEN_HEIGHT/2);

	g_now = SDL_GetTicks();

	ui_initialize_deathmatch(rtn_renderer());

	do
	{
		//calculate deltatime
		g_last = g_now;
		g_now = SDL_GetTicks();
		g_deltatime = g_now - g_last;

		render_update(1);
		render_clear();
		entity_all_think();
		entity_all_update();
		player_move(player);
		while(SDL_PollEvent(&e))
		{
			if(e.type == SDL_KEYDOWN)
			{
				if(e.type == SDL_QUIT)
					SDL_Quit();
				if(e.key.keysym.sym == SDLK_w)
					player->keysHeld.W = 1;
				if(e.key.keysym.sym == SDLK_s)
					player->keysHeld.S = 1;
				if(e.key.keysym.sym == SDLK_a)
					player->keysHeld.A = 1;
				if(e.key.keysym.sym == SDLK_d)
					player->keysHeld.D = 1;
				if(e.key.keysym.sym == SDLK_e)
				{
					tank_weapon_change(player->tank);
					slog("Current weapon: %s", player->tank->currentweapon->name);
				}
				if(e.key.keysym.sym == SDLK_p)
				{
					audio_play_sound(g_sound->file.sound);
				}
				if(e.key.keysym.sym == SDLK_f)
				{
					Entity *sonar;
					sonar = sonar_new(sonar = entity_new(SONAR, NULL), 3.0, player->tank);
				}
				if(e.key.keysym.sym == SDLK_g)
				{
					tank_weapon_fire(player->tank);
				}
				if(e.key.keysym.sym == SDLK_h)
					player->tank->health -= 5;
				if(e.key.keysym.sym == SDLK_j)
					player->tank->armour -= 5;
				if(e.key.keysym.sym == SDLK_SPACE)
				{
					if(player->tank->is_hidden)
						player->tank->is_hidden = 0;
					else
						player->tank->is_hidden = 1;

					tank_sprite_change(player->tank);
				}
				if(e.key.keysym.sym == SDLK_ESCAPE)
				{
					playclicked = 0;
					entity_close_list();
					tank_close_list();
					player_close_list();
					currentstate = MAINMENU;
				}
			}
			if(e.type == SDL_KEYUP)
			{
				if(e.key.keysym.sym == SDLK_w)
					player->keysHeld.W = 0;
				if(e.key.keysym.sym == SDLK_s)
					player->keysHeld.S = 0;
				if(e.key.keysym.sym == SDLK_a)
					player->keysHeld.A = 0;
				if(e.key.keysym.sym == SDLK_d)
					player->keysHeld.D = 0;
			}
		}
		if(g_deltatime < 32)
			SDL_Delay(32-g_deltatime);
	} while(currentstate == DEATHMATCH);

	return 0;
}
Пример #26
0
static int
proto_server_move_handler(Proto_Session *s){
  int i,rx,ry,id,rc, winner;
  dir_t dir;
  Cell *cell = malloc(sizeof(Cell));
  Proto_Msg_Hdr sh;
  Proto_Msg_Hdr rh;
  Player* p;
  Proto_Session *us;
  int valid;
  int flagindex;
  object_t flag = -1;
  Proto_Session *fs;
  bzero(&sh, sizeof(sh));
  bzero(&rh, sizeof(rh));

  sh.type = proto_session_hdr_unmarshall_type(s);
  sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST;

  proto_session_hdr_unmarshall(s, &rh);
  id = rh.pstate.v0.raw;
  dir = rh.pstate.v1.raw;
  p = gamestate_get_player(Server_Gamestate,id);
  valid = 0;
  flagindex = -1;
  valid = player_move(dir,p,Server_ObjectMap, Server_Gamestate);
  //  printf("Valid bit%d\n", valid);
  if (valid>0) {
    sh.pstate.v3.raw = 1;
    printf("Player %d is moving to (%d,%d)\n",id,p->pcell->x,p->pcell->y);
    if(!DEBUG_MAP){
      flagindex = objectmap_flag_visible(p,Server_ObjectMap);
      
      if(flagindex>=0)
	flag = Server_ObjectMap->objects[flagindex]->obj;
      
      if(flag == FLAG1){
	if(!flag1found)
	  flag1found = 1;
	else
	  flagindex = -1;
      }
      if(flag == FLAG2){
	if(!flag2found)
	  flag2found = 1;
	else
	  flagindex = -1;
      }
    }
  } else {
    sh.pstate.v3.raw = 0;    
    printf("Player %d attemped an invalid move\n",id);
  }
  sh.pstate.v0.raw = p->id;
  sh.pstate.v1.raw = p->pcell->x;
  sh.pstate.v2.raw = p->pcell->y;
 
  proto_session_hdr_marshall(s, &sh);
  
  rc = proto_session_send_msg(s,1);
  
  if(valid){
    us = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_MOVE;
    proto_session_hdr_marshall(us,&sh);
    proto_server_post_event();
  }
  
  bzero(&sh, sizeof(sh));
  if(flagindex>=0){
    fs = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_FLAG;
    
    //gstate.v0 holds the flag itself
    //gstate.v1 holds the index in the objectmap where that flag is located
    sh.gstate.v0.raw = Server_ObjectMap->objects[flagindex]->obj;
    sh.gstate.v1.raw = flagindex;
    proto_session_hdr_marshall(fs,&sh);
    proto_server_post_event();
  }
  
  bzero(&sh, sizeof(sh));
  if((winner = gamestate_team_wins()) >= 0){
    sh.type = PROTO_MT_EVENT_BASE_WIN;
    sh.gstate.v0.raw = winner;
    proto_session_hdr_marshall(fs,&sh);
    proto_server_post_event();
  }

  return rc;
}
Пример #27
0
bool ai_move(Player *player, Board *board, enum State *state, Players *players, PairStack *fields_adjacent_enemies,
             PairStack *fields_adjacent_neutrals) {
    PairStack *actionable_neutral_fields = create_pair_stack();
    PairStack *actionable_enemy_fields = create_pair_stack();

    Field *field;

    for (int x = 0; x < board->width; x++) {
        for (int y = 0; y < board->height; y++) {
            if (is_actionable(board, x, y, player, *state)) {
                if (board->fields[x][y].owner == 0) {
                    push(actionable_neutral_fields, create_pair(x, y));
                }
                else if (board->fields[x][y].owner != player->id) {
                    push(actionable_enemy_fields, create_pair(x, y));
                }
            }
        }
    }

    if ((rand() / (double) RAND_MAX) < 0.75 || actionable_enemy_fields->size == 0) {
        if (actionable_enemy_fields->size > 0 &&
            ((rand() / (double) RAND_MAX) < 0.35 || actionable_neutral_fields->size == 0)) {
            Triple actionable_enemy_fields_tab[actionable_enemy_fields->size];

            int index = 0;
            PairItem *pair_item = actionable_enemy_fields->top;
            while (pair_item != NULL) {
                actionable_enemy_fields_tab[index] = create_triple(pair_item->pair.x, pair_item->pair.y,
                                                                   board->fields[pair_item->pair.x][pair_item->pair.y].force);
                pair_item = pair_item->prev;
                index++;
            }

            qsort(actionable_enemy_fields_tab, (size_t) actionable_enemy_fields->size, sizeof(Triple),
                  actionable_enemy_fields_cmp);

            if ((rand() / (double) RAND_MAX) < 0.5) {
                field = pair_to_field(create_pair(actionable_enemy_fields_tab[0].x, actionable_enemy_fields_tab[0].y),
                                      board);
            }
            else {
                index = rand() % actionable_enemy_fields->size;
                field = pair_to_field(
                        create_pair(actionable_enemy_fields_tab[index].x, actionable_enemy_fields_tab[index].y), board);
            }
        }
        else if (actionable_neutral_fields->size > 0) {
            field = random_field(actionable_neutral_fields, board);
        }
        else {
            field = random_field(player->fields_stack, board);
        }

        player_move(player, field, board, state, players);
    }
    else {
        if (fields_adjacent_enemies->size > 0) {
            field = random_field(fields_adjacent_enemies, board);
        }
        else if (fields_adjacent_neutrals->size > 0) {
            field = random_field(fields_adjacent_neutrals, board);
        }
        else {
            field = random_field(player->fields_stack, board);
        }

        player_move(player, field, board, state, players);
    }

    delete_pair_stack(actionable_enemy_fields);
    delete_pair_stack(actionable_neutral_fields);

    return true;
}
Пример #28
0
Файл: main.c Проект: Anz/ztg_old
int main(int argc, char* argv[]) {
    printf("hello combine\n");

    SDL_Surface* window;
    SDL_Event event;
    bool running = true;

    // create window
    int width =  480, height = 360;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    window = SDL_SetVideoMode(width, height, 24, SDL_OPENGL | SDL_GL_DOUBLEBUFFER);

    // init render
    render_init();

    // load all textures
    void* textures = textures_load("img");

    // scene
    scene_t scene;
    scene_init(&scene);

    sprite_t draft;
    sprite_init(&draft, map_get(textures, "draft"));
    draft.layer = 60;

    sprite_t hud;
    sprite_init(&hud, map_get(textures, "draft"));
    hud.x = (hud.texture->width - width) / 2.0f;
    hud.y = (height - hud.texture->height) / 2.0f;

    // player
    player_t player;
    player_init(&player, &scene, textures, -50, -40);

    scene.space = array_add(scene.space, &draft);

    scene.hud = array_add(scene.hud, &hud);

    level_load(&scene, textures, "cave.lvl");

    float counter = 0;

    int speed = 0;

    // main
    while (running) {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_QUIT: running = false; break;
                case SDL_KEYUP:
                case SDL_KEYDOWN: {
                    switch(event.key.keysym.sym) {
                        case SDLK_a: { if (speed == -1) { speed = 0; } else { speed = -1; }  break; }
                        case SDLK_d: { if (speed == 1) { speed = 0; } else { speed = 1; } break; }
                        default: break;
                    }
                }
            }
        }

        player_move(&player, speed * 2);

        if (running) {
            render(&scene, &player.camera);
            SDL_GL_SwapBuffers();
        }
        draft.rotation += 0.5f;
        draft.transparency = sin(counter) / 2.0f + 0.5f;

        counter += 0.05f;
    }

    // clean up
    scene_release(&scene);
    textures_release(textures);
    SDL_Quit();

    return 0;
}
Пример #29
0
void Game::do_action(Interface_action act)
{
  switch (act) {
    case IACTION_MOVE_N:  player_move( 0, -1);  break;
    case IACTION_MOVE_S:  player_move( 0,  1);  break;
    case IACTION_MOVE_W:  player_move(-1,  0);  break;
    case IACTION_MOVE_E:  player_move( 1,  0);  break;
    case IACTION_MOVE_NW: player_move(-1, -1);  break;
    case IACTION_MOVE_NE: player_move( 1, -1);  break;
    case IACTION_MOVE_SW: player_move(-1,  1);  break;
    case IACTION_MOVE_SE: player_move( 1,  1);  break;
    case IACTION_PAUSE:   player->pause();      break;

    case IACTION_MOVE_UP:
      if (!map->has_flag(TF_STAIRS_UP, player->pos)) {
        add_msg("You cannot go up here.");
        player_move_vertical(1);
      } else {
        player_move_vertical(1);
      }
      break;

    case IACTION_MOVE_DOWN:
      if (!map->has_flag(TF_STAIRS_DOWN, player->pos)) {
        add_msg("You cannot go down here.");
        player_move_vertical(-1);
      } else {
        player_move_vertical(-1);
      }
      break;

    case IACTION_PICK_UP:
// TODO: Interface for picking up >1 item
      if (map->item_count(player->pos) == 0) {
        add_msg("No items here.");
      } else if (map->item_count(player->pos) == 1) {
        std::vector<Item> *items = map->items_at(player->pos);
        std::string message = "You pick up " + list_items(items);
        player->add_item( (*items)[0] );
        items->clear();
        add_msg(message);
      } else {
        pickup_items(player->pos);
      }
      break;

    case IACTION_OPEN: {
      Point dir = input_direction(input());
      if (dir.x == -2) { // Error
        add_msg("Invalid direction.");
      } else {
        Tripoint open = player->pos + dir;
        std::string tername = map->get_name(open);
        if (map->open(open)) {
          add_msg("You open the %s.", tername.c_str());
          player->use_ap(100);
        } else {
          add_msg("You cannot open a %s.", tername.c_str());
        }
      }
    } break;

    case IACTION_CLOSE: {
      Point dir = input_direction(input());
      if (dir.x == -2) { // Error
        add_msg("Invalid direction.");
      } else {
        Tripoint close = player->pos + dir;
        std::string tername = map->get_name(close);
        if (map->close(close)) {
          add_msg("You close the %s.", tername.c_str());
          player->use_ap(100);
        } else {
          add_msg("You cannot close a %s.", tername.c_str());
        }
      }
    } break;

    case IACTION_SMASH: {
      Point dir = input_direction(input());
      if (dir.x == -2) { // Error
        add_msg("Invalid direction.");
      } else {
        Tripoint sm = player->pos + dir;
        add_msg("You smash the %s.", map->get_name(sm).c_str());
        map->smash(sm, player->std_attack().roll_damage());
        player->use_ap(100);
      }
    } break;

    case IACTION_INVENTORY: {
      Item it = player->inventory_single();
      Item_action act = it.show_info();
      if (act == IACT_DROP) {
        add_msg( player->drop_item_message(it) );
        player->remove_item_uid(it.get_uid());
        map->add_item(it, player->pos);
      } else if (act == IACT_WIELD) {
        add_msg( player->wield_item_message(it) );
        player->wield_item_uid(it.get_uid());
      } else if (act == IACT_WEAR) {
        add_msg( player->wear_item_message(it) );
        player->wear_item_uid(it.get_uid());
      }
    } break;

    case IACTION_DROP: {
      std::vector<Item> dropped = player->drop_items();
      std::stringstream message;
      message << "You drop " << list_items(&dropped);
      for (int i = 0; i < dropped.size(); i++) {
        map->add_item(dropped[i], player->pos);
      }
      add_msg( message.str() );
    } break;

    case IACTION_WIELD: {
      Item it = player->inventory_single();
      add_msg( player->sheath_weapon_message() );
      player->sheath_weapon();
      add_msg( player->wield_item_message(it) );
      player->wield_item_uid(it.get_uid());
    } break;

    case IACTION_WEAR: {
      Item it = player->inventory_single();
      add_msg( player->wear_item_message(it) );
      player->wear_item_uid(it.get_uid());
    } break;

    case IACTION_RELOAD: {
      Item it = player->inventory_single();
      player->reload_prep(it.get_uid());
    } break;

    case IACTION_RELOAD_EQUIPPED:
      player->reload_prep(player->weapon.get_uid());
      break;

    case IACTION_THROW: {
      Item it = player->inventory_single();
      if (!it.is_real()) {
        add_msg("Never mind.");
      } else {
        Point target = target_selector();
        if (target.x == -1) { // We canceled
          add_msg("Never mind.");
        } else {
          player->remove_item_uid(it.get_uid(), 1);
          Ranged_attack att = player->throw_item(it);
          launch_projectile(it, att, player->pos, target);
        }
      }
    } break;

    case IACTION_FIRE:
      if (!player->weapon.is_real()) {
        add_msg("You are not wielding anything.");
      } else if (player->weapon.get_item_class() != ITEM_CLASS_LAUNCHER) {
        add_msg("You cannot fire %s.",
                player->weapon.get_name_indefinite().c_str());
      } else if (player->weapon.charges == 0 || !player->weapon.ammo) {
        add_msg("You need to reload %s.",
                player->weapon.get_name_definite().c_str());
      } else {
        Point target = target_selector();
        if (target.x == -1) { // We canceled
          add_msg("Never mind.");
        } else {
          Ranged_attack att = player->fire_weapon();
          launch_projectile(Item(), att, player->pos, target);
        }
      }
      break;

    case IACTION_MESSAGES_SCROLL_BACK:
      i_hud.add_data("text_messages", -1);
      break;
    case IACTION_MESSAGES_SCROLL_FORWARD:
      i_hud.add_data("text_messages",  1);
      break;

    case IACTION_VIEW_WORLDMAP: {
      Point p = map->get_center_point();
      worldmap->draw(p.x, p.y);
    }  break;

    case IACTION_QUIT:
      if (query_yn("Commit suicide?")) {
        game_over = true;
        player->action_points = 0;
      }
      break;
  }
}
Пример #30
0
/*notice the default arguments for main.  SDL expects main to look like that, so don't change it*/
int main(int argc, char *argv[])
{
  SDL_Surface *temp = NULL;
  int done;
  int tx = 0,ty = 0;
  int i;
  const Uint8 *keys;
  char imagepath[512];
  SDL_Rect srcRect={0,0,SCREEN_WIDTH,SCREEN_HEIGHT};
  SDL_Event e;
  last_time = current_time = SDL_GetTicks();
  
  Init_All();
  slog("Finished Init All()");
  done = 0;
  do
  {
	   //render or draw functions go here
	//draw functions should go in order from background first to player draw calls last
    ResetBuffer();
    SDL_RenderClear(__gt_graphics_renderer);//clear screen
	tile_render();	
	player_draw();
	DrawMouse2();
	
	/*monster_spawn(Monster::grue);
	monster_spawn(Monster::spider01);
	monster_spawn(Monster::mino);		
	monster_spawn(Monster::orc);	
	support_spawn(Support::sara);	
	support_spawn(Support::healer);	
	support_spawn(Support::archer);
	
	*/
	entity_update_all();
	entity_think_all();
	entity_check_collision_all();
	particle_em_draw_all();
//	struct_update_all();
	

	G_MONSTER_SPAWN_TIMER -= 1;

	while(SDL_PollEvent(&e) != 0)
		player_move (&e);
    NextFrame();
	//end
    SDL_PumpEvents();
    keys = SDL_GetKeyboardState(NULL);
	//taken from lazyfoo
	//handles generally keyboard inputs	
	
	while( SDL_PollEvent( &e) != 0){
		if(e.type == SDL_QUIT)
			done = 1;
    	else
			player_move(&e);	
	}

		if(keys[SDL_SCANCODE_ESCAPE])
		{
			done = 1;
		}
		
	SDL_RenderPresent(__gt_graphics_renderer);
	last_time = current_time;
	current_time = SDL_GetTicks();
	delta = current_time - last_time;
  }while(!done);
  exit(0);		/*technically this will end the program, but the compiler likes all functions that can return a value TO return a value*/
  return 0;
}