Пример #1
0
bool VirtualKey::VK_MOUSEKEY::handle(const Params_KeyboardEventCallBack& params) {
  if (handle_button(params)) return true;
  if (handle_move(params)) return true;
  if (handle_fixeddistancemove(params)) return true;
  if (handle_lock_button(params)) return true;
  return false;
}
Пример #2
0
//////ENGINE_____________________________________________________________________________________
bool tg_tick(tetris_game *obj, tetris_move move)
{
  int lines_cleared;
  handle_move(obj, move);
  lines_cleared = checa_linha(obj);
  atualiza_placar(obj, lines_cleared);
  // Fala se o jogo ira continuar ou não
  return !jogo_terminou(obj);
}
Пример #3
0
bool VirtualKey::VK_MOUSEKEY::handle(const Params_KeyboardEventCallBack& params, AutogenId autogenId, PhysicalEventType physicalEventType) {
  lastPhysicalEventType_ = physicalEventType;

  if (handle_button(params, autogenId, physicalEventType)) return true;
  if (handle_move(params, autogenId, physicalEventType)) return true;
  if (handle_fixeddistancemove(params, autogenId, physicalEventType)) return true;
  if (handle_fixeddistancescroll(params, autogenId, physicalEventType)) return true;
  if (handle_lock_button(params, autogenId, physicalEventType)) return true;
  return false;
}
Пример #4
0
	void combo_drag::mouse_motion(const SDL_MouseMotionEvent& event)
	{
		if (drag_ == PRESSED
				|| drag_ == MOVED
				|| drag_ == PRESSED_MOVE)
		{
			handle_move(event);
		} else {
			button::mouse_motion(event);
		}
	}
Пример #5
0
static int main_loop(int sock) {
    char buf[1000];
    for (;;) {
        //printf("Rx: \"%s\"\n", buf);

        int result = client_rx_line(sock, buf, 999);
        if (result) break;

        char* args;

        //  printf("CMD: \"%s\"\n", buf);

        isolate_verb(buf, &args);

        //printf("ISO: \"%s\"(\"%s\")\n\n", buf, args);

        if (0 == strcasecmp(buf, "NAME")) {
            handle_name(sock, args);
            continue;
        }

        if (0 == strcasecmp(buf, "NEWGAME")) {
            handle_newgame(sock, args);
            continue;
        }

        if (0 == strcasecmp(buf, "MOVE")) {
            handle_move(sock, args);
            continue;
        }

        if (0 == strcasecmp(buf, "YOURMOVE")) {
            handle_yourmove(sock, args);
            continue;
        }

        if (0 == strcasecmp(buf, "GAMEOVER")) {
            handle_gameover(sock, args);
            continue;
        }

        if (0 == strcasecmp(buf, "ERROR") || 0 == strcasecmp(buf, "BADPROT")) {
            handle_error(sock, args);
            continue;
        }

        printf("Server error: unknown verb \"%s\"\n", buf);
        return 1;

    }
    return 1;
}
Пример #6
0
/**
 * Handles selection of a tile on the board.
 * Performs corresponding move according to stated by
 * current game rules.
 */
int on_select_tile(Control* btn_tile) {
	if (game->game_over) {
		return 0;
	}
	Control* current;
	int rc = handle_move(game, btn_tile->i, btn_tile->j);
	if (game->won_game(game)) {
		return !show_game_arena(get_root(btn_tile), game, empty_select,
				game_menu_handles, on_select_difficulty, 4);
	}
	if ((rc == 0) && switch_player(game)) {
		current = get_root(btn_tile);
		if (show_game_arena(current, game, empty_select,
				game_menu_handles, on_select_difficulty, 4)) {
			return on_select_tile(current);
		}
	}
	return !show_game_arena(get_root(btn_tile), game, on_select_tile,
			game_menu_handles, on_select_difficulty, 4);
}
void WorldUpdateModule::run()
{
	Uint32 start_time;
       Uint32 start_cmd_time;
	Uint32 end_time;
       Uint32 avg_req;
       Uint32 avg_up;
    Uint32 timeout;
    Uint32 wui, rui;

	Message *m;
	IPaddress addr;
	int type;
	
	Player *p;
    PlayerBucket* bucket = &sd->wm.players[ t_id ];
    
    Serializator *s = NULL;
    MessageWithSerializator *ms = NULL;
    
    Uint32 start_quest = SDL_GetTicks() + sd->quest_between;
    Uint32 end_quest   = start_quest + sd->quest_min + rand() % (sd->quest_max-sd->quest_min+1);
    	
       FILE *req;
       FILE *up;
   	char file_req[] = "requests_";
		sprintf(file_req,"%s%d",file_req,t_id);
		char file_up[] = "updates_";
		sprintf(file_up,"%s%d",file_up,t_id);	
       

	printf("WorldUpdateModule started\n");

	/* main loop */
	while ( true )
	{
req = fopen(file_req,"a");
       up = fopen(file_up,"a");		
timeout	= sd->regular_update_interval;
              start_time= SDL_GetTicks();
              start_cmd_time = SDL_GetTicks();
		
        while( (m = comm->receive( timeout, t_id )) != NULL )
        {
            addr = m->getAddress();
            type = m->getType();
            p = sd->wm.findPlayer( addr, t_id );
             switch ( type )
            {
                case MESSAGE_CS_JOIN:		handleClientJoinRequest(p, addr); 						break;
                case MESSAGE_CS_LEAVE:		handleClientLeaveRequest(p); 							break;
                
                case MESSAGE_CS_MOVE_DOWN:				// dir 0
                case MESSAGE_CS_MOVE_RIGHT:				// dir 1
                case MESSAGE_CS_MOVE_UP:				// dir 2
                case MESSAGE_CS_MOVE_LEFT:				// dir 3                
                							handle_move( p, type - MESSAGE_CS_MOVE_DOWN );			break;
                
                case MESSAGE_CS_ATTACK_DOWN:			// dir 0
                case MESSAGE_CS_ATTACK_RIGHT:			// dir 1
                case MESSAGE_CS_ATTACK_UP:				// dir 2
                case MESSAGE_CS_ATTACK_LEFT:			// dir 3                
                							sd->wm.attackPlayer(p, type - MESSAGE_CS_ATTACK_DOWN);	break;
				case MESSAGE_CS_USE:		sd->wm.useGameObject(p);								break;
				
                default: printf("[WARNING] Unknown message (%d) received from %u.%u.%u.%u:%d\n", m->getType(), 
                		addr.host & 0xFF,  (addr.host >> 8) & 0xFF, (addr.host >> 16) & 0xFF, addr.host >> 24, addr.port );
            }

            delete m;
            end_time = SDL_GetTicks();
            timeout = sd->regular_update_interval - (SDL_GetTicks() - start_time);
            avg_req = ( avg_req < 0 ) ? (end_time - start_cmd_time) : ( avg_req * 0.95 + (double)(end_time-start_cmd_time) * 0.05 );
            
 				
			 //file_req="requests_";
			 //strcat(file_req,(char*)t_id);

			//req = fopen(file_req,"a");	    
			fprintf(req, "%d,%s,%d,%d,%d\n",t_id, p->name, type, avg_req,start_time );
				//fclose(req);

            if( ((int)timeout) < 0 )	timeout = 0;
        }
        
        SDL_WaitBarrier(barrier);
        
        if( t_id == 0 )
        {
        	for (int i=0; i<sd->num_threads; i++)
         {
              printf("%d: %d ,",i,sd->wm.players[i].size());
          }
          printf("\n");
          sd->wm.balance();
        	
        	if( rand() % 100 < 10 )		sd->wm.regenerateObjects();
        	
        	sd->send_start_quest = 0; sd->send_end_quest = 0;        	
			if( start_time > start_quest )
			{
				start_quest = end_quest + sd->quest_between;
				sd->quest_pos.x = (rand() % sd->wm.n_regs.x) * CLIENT_MATRIX_SIZE + MAX_CLIENT_VIEW;
				sd->quest_pos.y = (rand() % sd->wm.n_regs.y) * CLIENT_MATRIX_SIZE + MAX_CLIENT_VIEW;
				sd->send_start_quest = 1;
				if( sd->display_quests )		printf("New quest %d,%d\n", sd->quest_pos.x, sd->quest_pos.y);
			}			
			if( start_time > end_quest )
			{
				sd->wm.rewardPlayers( sd->quest_pos );
				end_quest = start_quest + sd->quest_min + rand() % (sd->quest_max-sd->quest_min+1);
				sd->send_end_quest = 1;
				if( sd->display_quests )		printf("Quest over\n");				
			}
        }
        
        SDL_WaitBarrier(barrier);
        
        int msgtype;
        wui = SDL_GetTicks() - start_time;
        avg_wui = ( avg_wui < 0 ) ? wui : ( avg_wui * 0.95 + (double)wui * 0.05 );               
		/* send updates to clients (map state) */
	    bucket->start();
	    while ( ( p = bucket->next() ) != NULL )
	    {
	    	ms = new MessageWithSerializator( MESSAGE_SC_REGULAR_UPDATE, t_id, p->address );	assert(ms);
		    s = ms->getSerializator();															assert(s);
		     msgtype=MESSAGE_SC_REGULAR_UPDATE;
		    sd->wm.updatePlayer( p, s );
	    	
	    	ms->prepare();
             

	    	comm->send( ms, t_id );
	    	
	    	if( sd->send_start_quest ){	
                      msgtype=MESSAGE_SC_NEW_QUEST;	
                     comm->send( new MessageXY(MESSAGE_SC_NEW_QUEST, t_id, p->address, sd->quest_pos), t_id );
              }
	    	if( sd->send_end_quest ){
                  msgtype=MESSAGE_SC_QUEST_OVER;
                  comm->send( new Message(MESSAGE_SC_QUEST_OVER, t_id, p->address), t_id );
                }

              end_time=SDL_GetTicks();
            avg_up = ( avg_up < 0 ) ? (end_time-start_cmd_time) : ( avg_up * 0.95 + (double)(end_time-start_cmd_time) * 0.05);
            
				 //file_up="updates_";
				 //strcat(file_up,(char*)t_id);
				//sprintf(file_req,"%s%d",file_up,t_id);					
				//up = fopen(file_up,"a");
				fprintf(up, "%d,%s,%d,%d, %d\n",t_id,p->name, msgtype, avg_up, start_time);
				//fclose(up);
	    }
	
	    SDL_WaitBarrier(barrier);
	    rui = SDL_GetTicks() - start_time;    
	    avg_rui = ( avg_rui < 0 ) ? rui : ( avg_rui * 0.95 + (double)rui * 0.05 );
              fclose(req);
		fclose(up);	    
	}

      
      
}
Пример #8
0
// Note: changes here should be documented in G-code.md as well.
void gcodep_parse_line(struct GCodeParser *p, const char *line,
                       FILE *err_stream) {
  ++p->line_number;
  void *const userdata = p->callbacks.user_data;
  struct GCodeParserCb *cb = &p->callbacks;
  p->err_msg = err_stream;  // remember as 'instance' variable.
  char letter;
  float value;
  while ((line = gparse_pair(p, line, &letter, &value))) {
    if (!p->program_in_progress) {
      cb->gcode_start(userdata);
      p->program_in_progress = 1;
    }
    char processed_command = 1;
    if (letter == 'G') {
      switch ((int) value) {
      case  0: p->modal_g0_g1 = 0; line = handle_move(p, line, 0); break;
      case  1: p->modal_g0_g1 = 1; line = handle_move(p, line, 0); break;
      case  2: line = handle_arc(p, line, 1); break;
      case  3: line = handle_arc(p, line, 0); break;
      case  4: line = set_param(p, 'P', cb->dwell, 1.0f, line); break;
      case 17: p->arc_normal = AXIS_Z; break;
      case 18: p->arc_normal = AXIS_Y; break;
      case 19: p->arc_normal = AXIS_X; break;
      case 20: p->unit_to_mm_factor = 25.4f; break;
      case 21: p->unit_to_mm_factor = 1.0f; break;
      case 28: line = handle_home(p, line); break;
      case 30: line = handle_z_probe(p, line); break;
      case 70: p->unit_to_mm_factor = 25.4f; break;
      case 71: p->unit_to_mm_factor = 1.0f; break;
      case 90: set_all_axis_to_absolute(p, 1); break;
      case 91: set_all_axis_to_absolute(p, 0); break;
      case 92: line = handle_G92(value, p, line); break;
      default: line = cb->unprocessed(userdata, letter, value, line); break;
      }
    }
    else if (letter == 'M') {
      switch ((int) value) {
      case  2: gcodep_finish_program_and_reset(p); break;
      case 17: cb->motors_enable(userdata, 1); break;
      case 18: cb->motors_enable(userdata, 0); break;
      case 30: gcodep_finish_program_and_reset(p); break;
      case 82: p->axis_is_absolute[AXIS_E] = 1; break;
      case 83: p->axis_is_absolute[AXIS_E] = 0; break;
      case 84: cb->motors_enable(userdata, 0); break;
      case 104: line = set_param(p, 'S', cb->set_temperature, 1.0f, line); break;
      case 106: line = set_param(p, 'S', cb->set_fanspeed, 1.0f, line); break;
      case 107: cb->set_fanspeed(userdata, 0); break;
      case 109:
	line = set_param(p, 'S', cb->set_temperature, 1.0f, line);
	cb->wait_temperature(userdata);
	break;
      case 116: cb->wait_temperature(userdata); break;
      case 220:
	line = set_param(p, 'S', cb->set_speed_factor, 0.01f, line);
	break;
      default: line = cb->unprocessed(userdata, letter, value, line); break;
      }
    }
    else if (letter == 'N') {
      // Line number? Yeah, ignore for now :)
      processed_command = 0;
    }
    else {
      const enum GCodeParserAxis axis = gcodep_letter2axis(letter);
      if (axis == GCODE_NUM_AXES) {
        line = cb->unprocessed(userdata, letter, value, line);
      } else {
        // This line must be a continuation of a previous G0/G1 command.
        // Update the axis position then handle the move.
        const float unit_value = value * p->unit_to_mm_factor;
        p->axes_pos[axis] = abs_axis_pos(p, axis, unit_value);
	line = handle_move(p, line, 1);
	// make gcode_command_done() think this was a 'G0/G1' command
	letter = 'G';
	value = p->modal_g0_g1;
      }
    }
    if (processed_command) {
      cb->gcode_command_done(userdata, letter, value);
    }
  }
  p->err_msg = NULL;
}
Пример #9
0
int main (int argc, const char* argv[])
{
    char *line, *cmd, *args;
    ChessGame* game;
    ChessGameIterator iter;
    int quit = 0;

    chess_generate_init();

    game = chess_game_new();
    chess_game_iterator_init(&iter, game);
    print_board(&iter);

    line = 0;

    for (;;)
    {
        if (line)
            free(line);

        if (quit)
            break;

        line = read_line("> ");
        if (!parse_line(line, &cmd, &args))
            continue;

        if (!strcmp(cmd, "quit") || !strcmp(cmd, "q"))
        {
            quit = 1;
        }
        else if (!strcmp(cmd, "new"))
        {
            chess_game_iterator_cleanup(&iter);
            chess_game_reset(game);
            chess_game_iterator_init(&iter, game);
            print_board(&iter);
        }
        else if (!strcmp(cmd, "fen"))
        {
            load_fen(game, args);
        }
        else if (!strcmp(cmd, "pgn"))
        {
            save_pgn(game);
        }
        else if (!strcmp(cmd, "ls"))
        {
            list_moves(&iter);
        }
        else if (!strcmp(cmd, "moves"))
        {
            game_moves(game);
        }
        else if (!strcmp(cmd, "bd"))
        {
            print_board(&iter);
        }
        else if (!strcmp(cmd, "undo"))
        {
            undo_move(&iter);
        }
        else if (!strcmp(cmd, "event"))
        {
            set_event(game, args);
        }
        else if (!strcmp(cmd, "site"))
        {
            set_site(game, args);
        }
        else if (!strcmp(cmd, "date"))
        {
            set_date(game, args);
        }
        else if (!strcmp(cmd, "round"))
        {
            set_round(game, args);
        }
        else if (!strcmp(cmd, "white"))
        {
            set_white(game, args);
        }
        else if (!strcmp(cmd, "black"))
        {
            set_black(game, args);
        }
        else if (!strcmp(cmd, "result"))
        {
            set_result(game, args);
        }
        else
        {
            handle_move(&iter, cmd);
        }
    }

    chess_game_iterator_cleanup(&iter);
    chess_game_destroy(game);

    return 0;
}