void MainWindow::move()
{
    auto s_head = getHead();
    auto s_food = QPoint(food_pos.x(), food_pos.y());
    auto s_foot = getFoot();
    state_now = find_way(s_head, s_food);
    if(state_now == -1){
        state_now = find_way(s_head, s_foot);
    }
    if(state_now == to_left)
    {
        go_left();
    }else if(state_now == to_right){
        go_right();
    }else if(state_now == to_up){
        go_up();
    }else if(state_now == to_down){
        go_down();
    }else{
        state_now = getRandomStep(s_head);
        if(state_now == -1){
            QMessageBox::information(0, "", "end");
            gameover();
        }else if(state_now == to_left){
            go_left();
        }else if(state_now == to_right){
            go_right();
        }else if(state_now == to_up){
            go_up();
        }else if(state_now == to_down){
            go_down();
        }
    }
    update();
}
Beispiel #2
0
static void	special_keys(unsigned int key, t_entlist *l)
{
	if (key == K_SPACE)
	{
		l->list->hl = l->list->hl == 0 ? 1 : 0;
		go_down(l);
	}
	else if (key == K_A)
	{
		l->sel_v = l->sel_v == 0 ? 1 : 0;
		select_all(l, l->sel_v);
	}
	else if (key == K_R)
	{
		l->sel_v = l->sel_v == 0 ? 1 : 0;
		select_specific(l, R, l->sel_v);
	}
	else if (key == K_D)
	{
		l->sel_v = l->sel_v == 0 ? 1 : 0;
		select_specific(l, D, l->sel_v);
	}
	else if (key == K_X)
	{
		l->sel_v = l->sel_v == 0 ? 1 : 0;
		select_specific(l, X, l->sel_v);
	}
}
Beispiel #3
0
static void		is_special_action(char *key)
{
	if (key[0] == 27 && key[1] == 91 && key[2] == 72)
		go_to_start();
	else if (key[0] == 27 && key[1] == 91 && key[2] == 70)
		go_to_end();
	else if (key[0] == 21 && key[1] == 0 && key[2] == 0)
		cut_entire_line();
	else if (key[0] == 11 && key[1] == 0 && key[2] == 0)
		cut_end_of_line();
	else if (key[0] == 16 && key[2] == 0 && key[2] == 0)
		paste();
	else if (key[0] == 12 && key[1] == 0 && key[2] == 0)
		clear();
	else if (key[0] == 3 && key[1] == 0 && key[2] == 0)
		handle_ctrl_c();
	else if (key[0] == 2 && key[1] == 0 && key[2] == 0)
		move_back_word();
	else if (key[0] == 14 && key[1] == 0 && key[2] == 0)
		move_next_word();
	else if (key[0] == 27 && key[1] == 91 && key[2] == 53)
		go_up();
	else if (key[0] == 27 && key[1] == 91 && key[2] == 54)
		go_down();
}
Beispiel #4
0
switch_status_t act(virtual_ip_t *vip)
{
    // if I'm the first in priority list
    if ( vip->node_id == node_first(vip->node_list)) {
        // become master
        vip->state = ST_MASTER;

        // set the ip to bind to
        if (utils_add_vip(vip->config.address,
                          vip->config.device) != SWITCH_STATUS_SUCCESS) {
            goto error;
        }

        // gratuitous arp request
        if (utils_send_gARP(vip->config.mac,
                            vip->config.address,
                            vip->config.device) != SWITCH_STATUS_SUCCESS) {

            utils_remove_vip(vip->config.address, vip->config.device);
            goto error;
        }

        // and I say it to all other nodes
        virtual_ip_send_state(vip);
        return SWITCH_STATUS_SUCCESS;
    }

error:
    switch_log_printf(SWITCH_CHANNEL_LOG,
                      SWITCH_LOG_ERROR,"Failed transition to MASTER!\n");
    go_down(vip);
    return SWITCH_STATUS_FALSE;

}
Beispiel #5
0
void		Monster::up_down(float b, std::deque<AObject*> &map)
{
  if (b > 0) // DOWN
    go_down(map, 1);
  else if (b < 0) // UP
    go_up(map, 1);
}
Beispiel #6
0
void		key_handler(unsigned int key, t_entlist *l, int *running)
{
	if (key == K_ENT)
	{
		clean_screen(l);
		*running = 0;
	}
	else
	{
		if (key == K_ESC)
			quit(l);
		else if (key == K_DOWN)
			go_down(l);
		else if (key == K_UP)
			go_up(l);
		else if (key == K_RIGHT)
			go_right(l);
		else if (key == K_LEFT)
			go_left(l);
		else if (key == K_DEL || key == K_BKSPC)
			delete_key(l);
		else
			special_keys(key, l);
	}
}
Beispiel #7
0
bool AI::try_go_down()
{
  if (is_down_safe_later())
    return go_down();
  else
    return false;
}
Beispiel #8
0
int     move_cursor_end(t_line *rdl)
{
  if (rdl->line == NULL)
    return (FALSE);
  if (rdl->line[rdl->pos])
    {
      if ((get_nb_char_line(rdl)) == get_winsize_x())
	go_down(rdl, 0);
      else
	my_putstr(tgetstr("nd", NULL));
      ++rdl->pos;
    }
  return (TRUE);
}
Beispiel #9
0
switch_status_t react(virtual_ip_t *vip)
{
    // if I'm the first in priority list
    if ( vip->node_id == node_first(vip->node_list)) {
        // become master
        vip->state = ST_MASTER;

        // set the ip to bind to
        if (utils_add_vip(vip->config.address,
                          vip->config.device) != SWITCH_STATUS_SUCCESS) {
            goto error;
        }

        // gratuitous arp request
        if (utils_send_gARP(vip->config.mac,
                            vip->config.address,
                            vip->config.device) != SWITCH_STATUS_SUCCESS) {
            utils_remove_vip(vip->config.address, vip->config.device);
            goto error;
        }

        // sofia recover!!!
        for (int i=0; i< MAX_SOFIA_PROFILES; i++) {
            if (!strcmp(vip->config.profiles[i].name, "")) break;
            if (vip->config.profiles[i].autorecover == SWITCH_TRUE) {
                utils_recover(vip->config.profiles[i].name);
            }
        }

        // and I say it to all other nodes
        virtual_ip_send_state(vip);


    } else {
        // se è cascato il master e non devo reagire mi ripulisco la tabella
        for (int i=0; i< MAX_SOFIA_PROFILES; i++) {
            if (!strcmp(vip->config.profiles[i].name, "")) break;
            utils_clean_up_table(vip->runtime_uuid,
                                 vip->config.profiles[i].name);
        }
    }
    return SWITCH_STATUS_SUCCESS;

error:
    switch_log_printf(SWITCH_CHANNEL_LOG,
                      SWITCH_LOG_ERROR,"Failed transition to MASTER!\n");
    go_down(vip);
    return SWITCH_STATUS_FALSE;

}
Beispiel #10
0
void tick(int * reached_bottom){
	static int filled_lines[4];
	static int counter = 0;
	
	if(*reached_bottom){
		//new piece reached the bottom or gravity activated and things need to be rechecked
		*reached_bottom = 0;
		
		if(counter == 0){//only if call is not coming from gravity stuff
			write_to_world(&current_tetromino, &world);
		}
		
		counter = 0;
		//check for lines right here & gravity
		for(int i = WORLD_HEIGHT-1; i >= world.top_line; i--){
			if(check_line(i)){
				fill_line(i);
				filled_lines[counter] = i;
				counter++;
			}
		}
		
		world.points += 100*counter*counter;
		
		if(counter == 0){
			fill_tetromino(&current_tetromino,rand()%7);
		
			if(check_collision(&current_tetromino, &world)){
				//GAME OVER
				init_game();
			}
		}else{
			current_tetromino.visible = 0;//dont display it while removing lines
		}
		
	}else if(counter != 0){
		//lines to remove, gravity to activate
		//remove from top to bottom
		for(int i = counter-1; i >= 0; i--){
			remove_line(filled_lines[i]);
		}
		set_tick_time();
		*reached_bottom = 1;
	}else{
		//just a normal turn, m8
		*reached_bottom = go_down(&current_tetromino, &world);//in the next tick: block is placed
	}
	
}
Beispiel #11
0
static void		controller_key_bis(t_readline *r, long key)
{
	if (key == 10127586)
		keyboard_paste(r);
	if (key == 0x41323b315b1b)
		go_up(r);
	if (key == 0x42323b315b1b)
		go_down(r);
	if (key == 0x05)
		keyboard_ctrlc(r);
	if (key == 04)
		keyboard_ctrld(r);
	if (key == 9)
		completion(r);
}
    T get(int a, int b)
    {
        if (a > b)
            throw std::exception("Left range should be less or equal to right range.");

        T result = initial_value;

        for(;go_up(a) <= b; a = go_up(a))
        {
            result = op(result, data2up[a]);
        }


        for(;go_down(b) >= a; b = go_down(b))
        {
            result = op(result, data2down[b]);
        }


        result = op(result, data[a]);


        return result;
    }
Beispiel #13
0
/*===========================================================================*
 *				do_lookup				     *
 *===========================================================================*/
int do_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node,
	int *is_mountpt)
{
/* Resolve a path string to an inode.
 */
  struct inode *dir_ino, *ino;
  struct sffs_attr attr;
  char path[PATH_MAX];
  int r;

  dprintf(("%s: lookup: got query for %"PRIu64", '%s'\n",
	sffs_name, dir_nr, name));

  if ((dir_ino = find_inode(dir_nr)) == NULL)
	return EINVAL;

  attr.a_mask = SFFS_ATTR_MODE | SFFS_ATTR_SIZE;

  if ((r = verify_inode(dir_ino, path, &attr)) != OK)
	return r;

  if (!IS_DIR(dir_ino))
	return ENOTDIR;

  r = OK;
  if (!strcmp(name, "."))
	get_inode(ino = dir_ino);
  else if (!strcmp(name, ".."))
	r = go_up(path, dir_ino, &ino, &attr);
  else
	r = go_down(path, dir_ino, name, &ino, &attr);

  if (r != OK)
	return r;

  node->fn_ino_nr = INODE_NR(ino);
  node->fn_mode = get_mode(ino, attr.a_mode);
  node->fn_size = attr.a_size;
  node->fn_uid = sffs_params->p_uid;
  node->fn_gid = sffs_params->p_gid;
  node->fn_dev = NO_DEV;

  *is_mountpt = FALSE;

  return OK;
}
Beispiel #14
0
int	move(char *key, t_data *data)
{
	if (key[0] == 27)
	{
		if (key[2] == 'D')
		{
			go_left(data->dlist);
			return (1);
		}
		else if(key[2] == 'A')
		{
			go_up(data->dlist, data->nb_word_line);
			return (1);
		}
		else if (key[2] == 'C')
		{
			go_right(data->dlist);
			return (1);
		}
		else if (key[2] == 'B')
		{
			go_down(data->dlist, data->nb_word_line);
			return (1);
		}
		else if (key[1] == 0)
			return(2);
	}
	else if(key[0] == 127)
	{
		data->dlist = del_word(data->dlist);
		tputs(tgetstr("up", NULL), 0, ft_putchar2);
		return(1);
	}
	else if (key[0] == ' ')
	{
		set_select(data->dlist);
		return (1);
	}
	else if (key[0] == '\n')
	{
		put_dlist(data->dlist, data->old_term);
		return (1);
	}
	return (0);
}
Beispiel #15
0
int			is_a_valid_key(int c)
{
	if (c == 27)
	{
		endwin();
		exit(0);
	}
	else if (c == KEY_DOWN)
		go_down();
	else if (c == KEY_UP)
		go_up();
	else if (c == KEY_RIGHT)
		go_right();
	else if (c == KEY_LEFT)
		go_left();
	else
		return (0);
	return (1);
}
Beispiel #16
0
void		Monster::go_safe_place(std::deque<AObject*> &map)
{
  unsigned int	i = 0;
  bool		is_safe = false;
  int		left = 1, right = 1, up = 1, down = 1;

  while (is_safe == false && i < map.size())
    {
      if ((map[i]->getType() == DANGER || map[i]->getType() == BOMB) &&
	  map[i]->getPosition().x + 160 >= position_.x && map[i]->getPosition().x - 160 <= position_.x &&
	  map[i]->getPosition().z + 160 >= position_.z && map[i]->getPosition().z - 160 <= position_.z)
	{
	  if ((safe(position_.x + (300 * i), position_.z, map, &left) == true) || (safe(position_.x - (300 * i), position_.z, map, &right) == true))
	    {
	      if (left == 1)
		{
		  go_left(map, 0);
		  is_safe = true;
		}
	      else if (right == 1)
		{
		  go_right(map, 0);
		  is_safe = true;
		}
	    }
	  else
	    {
	      if (up == 1)
		{
		  go_up(map, 0);
		  is_safe = true;
		}
	      else if (down == 1)
		{
		  go_down(map, 0);
		  is_safe = true;
		}
	    }
	  i++;
	}
      i++;
    }
}
Beispiel #17
0
// *path must be freed by the caller.
// returns NULL when we're done.
error_t file_find_get_next(file_find_state_t* s, char** path)
{
  error_t err;

  if( s->names_depth == 0 &&
      s->names_stack[0][s->names_i[0]] == NULL ) {
    // We're at the end!
    *path = NULL;
    return ERR_NOERR;
  } else {
    *path = strdup(s->cur_path);
    // Continue to get the next path ready.
  }

  //printf("Before advancing\n");
  //print_stack(s);

  // Advance to the next one there, if we're not already at the end.
  if( s->names_stack[s->names_depth][s->names_i[s->names_depth]] ) {
    s->names_i[s->names_depth]++;
  }

  // Now.. if we need to, go up.
  // Go up a level.
  err = go_up(s);
  if( err ) return err;

  //printf("After going up\n");
  //print_stack(s);
  // We're not at the end.

  // Go down if we're not at the end.
  if( s->names_stack[s->names_depth][s->names_i[s->names_depth]] ) {
    // Go down from here.
    err = go_down(s);
    if( err ) return err;
  }

  //printf("After advancing\n");
  //print_stack(s);

  return ERR_NOERR;
}
Beispiel #18
0
int go_right(char *chaine, char *piece, int chainei, int piecei, int count, int length)
{
  if(count < 4)
    {
  piecei = piecei + 1;
  chainei = chainei + 1;
  count++;  
  piece[piecei] = 'x'; 
  chaine[chainei] = 'x';
 if(piece[piecei + 1] == '#' && chaine[chainei + 1] == '.')
   count = go_right(chaine, piece, chainei, piecei, count, length);
 if(piece[piecei + 5] == '#' && chaine[chainei + length] == '.')
   count = go_down(chaine, piece, chainei, piecei, count, length);
 if(piece[piecei - 5] == '#' && chaine[chainei - length] == '.')
   count = go_up(chaine, piece, chainei, piecei, count, length);
 if(piece[piecei - 1] == '#' && chaine[chainei - 1] == '.')
   count = go_left(chaine, piece, chainei, piecei, count, length);
    }
  return (count);
}
Beispiel #19
0
int main() {
  FILE *fp;
  fp = fopen("euler11.txt", "rb");	
  int array[ARRAY_SIZE][ARRAY_SIZE];
  int line[64];
  int product = 1;
  int number;
  int i, j, k, x, y;
  int greatest = 0;

  for(i = 0; i < ARRAY_SIZE; i++) {
    for(j = 0; j < ARRAY_SIZE; j++) {
      if(fscanf(fp, "%d", &number) == -1) {}		
      array[i][j] = number;
      if((k = fgetc(fp)) == '\n') {
          break;
      }
    }
  }
  for(j = 0; j < ARRAY_SIZE; j++) {
    for(i = 0; i < ARRAY_SIZE; i++) {
      product = go_right(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
      product = go_down(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
      product = go_downRight(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
      product = go_downLeft(array, i, j);
      if(product > greatest) {
        greatest = product;
      }
    }
  }
  printf("greatest product is %d\n", greatest);
}
    void update(int x, int value)
    {
        if (x == 0)
            throw std::exception("Index should be greater than zero.");

        data[x] = op(data[x], value);

        int a = x, b = x;

        while(a <= N)
        {
            data2down[a] = op(data2down[a], value);
            a = go_up(a);
        }

        while(b > 0)
        {
            data2up[b] = op(data2up[b], value);
            b = go_down(b);
        }
    }
Beispiel #21
0
// moves the tank
void move_tank ( Tank *tank, int direction){

    //if this tank moved TANKSPEED earlier, just ignore. else, set move_state to 0
    if (tank->move_state < tank->move_speed ) return;

    tank->move_state = 0;

    switch(direction){
    case LEFT:
        go_left(tank);
        break;
    case RIGHT:
        go_right(tank);
        break;
    case UP:
        go_up(tank);
        break;
    case DOWN:
        go_down(tank);
        break;
    }
}
Beispiel #22
0
error_t reset_file_find(file_find_state_t* s)
{
  error_t err;

  // Move to the end of everything
  for( int d = 0; d <= s->names_depth; d++ ) {
    while( s->names_stack[s->names_depth][s->names_i[d]] ) {
      s->names_i[d]++;
    }
  }

  err = go_up(s);
  if( err ) return err;

  assert( s->names_depth == 0 );

  // now set the root index to 0.
  s->names_i[0] = 0;

  err = go_down(s);
  if( err ) return err;

  return ERR_NOERR;
}
void run(void)
{
    data='w';
    while(1)
    {
        while(data == 'w') //waiting for signal
        {
        }
        while(data == '\0' )
        {
            stop();
            _delay_ms(4000);
            comintersection();
            _delay_ms(100);
        }
        if(data=='f') // forward
        {
            data='\0';
        }
        if(data=='r') // right
        {
            data='\0';
            turn_right();
        }
        if(data=='l') // left
        {
            data='\0';
            turn_left();
        }
        if(data == 'h') // halt
        {
            data='\0';
            stop();
            _delay_ms(100);
            continue;

        }
        if(data == 'o') // origin
        {
            data='w';
            stop();
            _delay_ms(100);
            continue;
        }
        if(data == 'c') //collect item data -> i when the bot is trying to pick up an rfid
        {
            data='w';
            arm_down();
            _delay_ms(4200);
            stop_arm();
            _delay_ms(2000);
            grab();
            arm_up();
            _delay_ms(4750);
            stop_arm();
            _delay_ms(2000);
            lcd_cursor(2,1);
            lcd_string("Collecting");
            _delay_ms(3500);
            readrfidtag();
            clearrfid();
            continue;

        }
        if(data == 'd') //drop_item
        {
            data='w';
            go_down();
            release();
            go_up();
            lcd_cursor(2,1);
            lcd_string("Dropping");
            _delay_ms(3500);
            senddroppedsig();
            continue;
        }

        while(1)
        {
            if(move_bot() == 1)
            {
                continue;
            }
            else
            {
                break;
            }
        }
    }
}
Beispiel #24
0
void Babar::update_state()
{
    if (locked() )
        m_lock--;
    if (m_lock == 0)
    {
        unlock();
    }
    if (!Keyboard::GetInstance()->time_pressed (k_jump) )
        m_jump = false;
    if (m_jump)
    {
        if ( Keyboard::GetInstance()->time_pressed (k_jump) > 1 )
        {
            m_ready_double_jump = true;
            if ( Keyboard::GetInstance()->time_pressed (k_jump) > JUMP_TIME)
            {
                m_jump = false;
                Keyboard::GetInstance()->disable_key (k_jump);
            }
        }
    }

    update_direction();

    if ( CollisionsManager::is_down_coll (gCollision->get_matrix()->down_collision_type (position() ) ) )
    {
        interrupt_jump();
    }

    if (can_fire() )
    {
        m_fire = true;
        gProj->add_proj (fire_old(), PLAYER1);
        m_fire_phase = 0;
    }
    else
    {
        m_fire = false;
        m_fire_phase++;
    }

    if (can_crouch() )
    {
        crouch();
    }
    else
    {
        /* si on se releve */
        if ( m_crouch_time )
        {
            interrupt_crouch();
        }
    }

    if (can_jump() )
        jump();

    if (can_double_jump() )
        double_jump();

    if (can_go_down() )
        go_down();

    if (m_invincible > 0)
    {
        m_invincible --;
        if (!locked() )
        {
            if ( m_invincible % 2)
            {
                m_spriteGrid->no_pic();
            }
            else
            {
                m_spriteGrid->set_pic();
            }
        }
    }

    m_weapons_armory.update();

    if (Keyboard::GetInstance()->time_pressed (k_prev_weapon) == 1)
        m_weapons_armory.previous_weapon();

    if (Keyboard::GetInstance()->time_pressed (k_next_weapon) == 1)
        m_weapons_armory.next_weapon();

    switch (get_state() )
    {
    case STATIC:
        m_spriteGrid->setPictures (0);
        break;
    case WALK:
        m_spriteGrid->setPictures (1, 0, 2, 0);
        break;
    case JUMP:
        m_spriteGrid->setPictures (1);
        break;
    case CROUCH:
        m_spriteGrid->setPictures (12);
        break;
    case CROUCH_WALKING:
        m_spriteGrid->setPictures (13, 12, 14, 12);
        break;
    default:
        m_spriteGrid->setPictures (0);
        break;
    }

    //m_spriteGrid->change_anim(get_state(), m_dir, Keyboard::GetInstance()->key_down(k_fire));
    m_spriteGrid->set_pos (position() );
    set_h (m_spriteGrid->h() );
    set_w (m_spriteGrid->w() );
}
Beispiel #25
0
error_t init_file_find(file_find_state_t* s, int num_paths, const char** paths)
{
  int total_len;
  error_t err;
  char** root_paths = NULL;
  int idx;

  memset(s, 0, sizeof(file_find_state_t));

  s->names_stack_size = 0;
  s->names_stack = NULL;
  s->names_i_size = 0;
  s->names_i = NULL;

  // always make sure there is a null at the end.
  root_paths = calloc(num_paths+1, sizeof(char*));
  if( ! root_paths ) {
    err = ERR_MEM;
    goto error;
  } 
  total_len = 0;
  for( int i = 0; i < num_paths; i++ ) {
    total_len += strlen(paths[i]);
    root_paths[i] = strdup(paths[i]);
    if( ! root_paths[i] ) {
      err = ERR_MEM;
      goto free_root_paths;
    }
    // Remove trailing slash from root_paths[i].
    {
      ssize_t len = strlen(root_paths[i]);
      len--;
      while( len > 0 && root_paths[i][len] == '/' ) {
        root_paths[i][len] = '\0';
        len--;
      }
    }
  }

  err = append_array(&s->names_stack_size, &s->names_stack, sizeof(char**), &root_paths);
  if( err ) goto free_root_paths;
  idx = 0;
  err = append_array(&s->names_i_size, &s->names_i, sizeof(int), &idx);
  if( err ) goto error;
 
  // Allocate cur_path.
  total_len += 1024; // leave a bunch of extra room.
  s->path_max = total_len;
  s->cur_path = malloc(total_len);
  if( ! s->cur_path ) goto error;
  s->cur_path[0] = '\0';

  s->names_depth = 0;

  return go_down(s);

free_root_paths:
  for( int i = 0; i < num_paths; i++ ) {
    free(root_paths[i]);
  }
  free(root_paths);

error:
  free_file_find(s);
  return err;
}
Beispiel #26
0
/*==============================================================================
 * gen_refgrid: 
 *--------------------
 * 1. test current grid for refinement, and generate adaptive grid (if required) 
 * 2. re-link particles 
 * 3. ajdust densities as neccessary.
 *==============================================================================*/
boolean gen_refgrid(gridls **grid_list, int *no_grids)
{
  gridls *coa_grid, *fin_grid;     /* old/new grid pointer                    */
  boolean refined;                 /* refined YES/NO flag                     */
  boolean mg;                      /* does refinement hold enough particles   */
  int     idim;
  
#ifdef REF_TEST
  fprintf(stderr,"\ngen_refgrid:         current coarse grid         = %ld\n",
          (*grid_list + (*no_grids-1))->l1dim);
#endif
  
  /* set coarse grid pointer */
  coa_grid = *grid_list + *no_grids - 1;
  fin_grid = *grid_list + *no_grids;
  
  /* timing... */
  coa_grid->time.grid -= time(NULL);
  
  if(coa_grid->next == FALSE)  /* fin_grid does not exist ? */
    {
      /* reallocate the grid list array (including space for one additional grid) */
      (*grid_list) = (gridls *) realloc(*grid_list, (*no_grids + 1) * sizeof(gridls));
      
      if((*grid_list) == NULL)
        {
          fprintf(io.logfile,"gen_refgrid: error reallocating grid list\n");
          fflush(io.logfile);
          fclose(io.logfile);
          exit(1);
        }
      
      /* grid_list block might have moved in memory */
      global.dom_grid = *grid_list + global.domgrid_no;
      coa_grid        = *grid_list + *no_grids - 1;
      fin_grid        = *grid_list + *no_grids;
      
      /* fill in new grid's entries */
      fin_grid->masstodens     = coa_grid->masstodens     * CRITMULTI;
      fin_grid->masstopartdens = coa_grid->masstopartdens * CRITMULTI;
#ifdef MULTIMASS
      fin_grid->critdens       = simu.Nth_ref*fin_grid->masstopartdens;
#else
      fin_grid->critdens       = simu.Nth_ref*fin_grid->masstodens;
#endif
      fin_grid->timecounter    = coa_grid->timecounter;
      fin_grid->timestep       = coa_grid->timestep/2;
      fin_grid->l1dim          = coa_grid->l1dim * 2;
      fin_grid->spacing        = (double)1.0 / (double)fin_grid->l1dim;
      fin_grid->spacing2       = fin_grid->spacing * fin_grid->spacing;
      
      fin_grid->no_pquad       = 0;
      fin_grid->pquad_array    = NULL;

      /* we measure the time throughout the two DKD cycles of this fin_grid */
      fin_grid->time.potential = 0;
      fin_grid->time.density   = 0;
      fin_grid->time.DK        = 0;
      fin_grid->time.grid      = 0;
      fin_grid->time.hydro     = 0;
       
      fin_grid->no_sweeps      = 0;
      fin_grid->cur_resid      = 0.;
      
      /* remember that fin_grid exists from now on */
      fin_grid->next = FALSE;
      coa_grid->next = TRUE;
    }
  
  
  /* (re-)set values that are changing within the two DKD cycles the grid lives through */
  fin_grid->old_resid             = 0.0;
  fin_grid->cur_resid             = 0.0;
  
  fin_grid->multistep             = 0;
  
  
  /*------------------------------
   * now call refinement rountine 
   *------------------------------*/
  refined = refine_grid(fin_grid, coa_grid);
  
  if(refined)
    {
#ifdef REF_TEST
      fprintf(stderr,"                     placed new refinement       = %ld\n", fin_grid->l1dim);
#endif
      
      /* update number of grids */
      *no_grids += 1;
      
      /* now relink particles to new grid */
      mg = relink(coa_grid, fin_grid);        /* performs: unassign_part */
      
      /* now assign mass to new grid: THIS IS NEEDED AS IT RETURNS "mg" ! */
      zero_dens        (fin_grid);
      mg = assign_npart(fin_grid);
      
#ifdef REF_TEST
      fprintf(stderr,"             grid %6ld:  nnodes=%ld npart=%ld => %g (<! %g)\n",
              fin_grid->l1dim,fin_grid->size.no_nodes,fin_grid->size.no_part,
              (double)fin_grid->size.no_nodes/(double)fin_grid->size.no_part, CRITMULTI);
#endif
      
      /* only use grid if it holds enough particles */
      if(mg == FALSE)
        {
          refined = FALSE;
          relink_back(coa_grid, fin_grid);
          fin_grid->size.no_nodes         = 0;
          fin_grid->size.no_part          = 0;
          
          free_grid(fin_grid, no_grids);
          
#ifdef REF_TEST
          fprintf(stderr,"                     destroyed new refinement    = %ld (%g)\n",
                  fin_grid->l1dim, fin_grid->critdens);
#endif
        }
      else
        {
          /* get potential onto fine grid (important for boundaries !) */
          go_down(coa_grid);          
        }
    }
  
  else
    {
#ifdef REF_TEST
      fprintf(stderr,"                     NO new refinement grid !\n\n");
#endif
      /*
       * no need to call free_grid() 
       * -> temporary pquads, cquads, nquads already destroyed within ref_grid() 
       *
       * no need to realloc grid_list
       * -> grid will be kept in memory from now on...
       */
    }
  
  /* grid_list block might have changed position in memory */
  global.dom_grid = *grid_list + global.domgrid_no;
  
  /* ...timing */
  coa_grid->time.grid += time(NULL);
  
  return(refined);
}
Beispiel #27
0
void game_loop(int * input_key){
	//init game data
	init_game();
	
	//control data
	struct timeval *begin, *end, *tmp;
	unsigned long diff;//in us
	int key = 0; //for temporary storage of input_key, b/c of race conditions
	int reached_bottom = 0;
	
	begin=malloc(sizeof(struct timeval));
	end=malloc(sizeof(struct timeval));
	gettimeofday(begin, NULL);
	diff = 0;
	
	draw();
	while(1){
		//input
		key=*input_key;
		if(key != -1){
			*input_key = -1;//input read->reset
			switch(key){//TODO move block
				case KEY_DOWN:	
					reached_bottom = go_down(&current_tetromino, &world);
					break;
				case KEY_LEFT: 	
					if(!reached_bottom){
						go_left(&current_tetromino, &world);
					}
					break;
				case KEY_RIGHT: 
					if(!reached_bottom){
						go_right(&current_tetromino, &world);
					}
					break;
				case 'x':
					if(!reached_bottom){
						rotate(&current_tetromino, &world, 1);
					}
					break;
				case 'c':
					if(!reached_bottom){
						rotate(&current_tetromino, &world, 0);
					}
					break;
				case 27: /*ESC*/
					switch(menu(input_key)){
						case 0:break;//continue game
						case 1:init_game();break;//new game
						case -1://quit
							free(begin);
							free(end);
							return;
						default:break;
					} 
					gettimeofday(begin, NULL);
					break;
				case 32: /*Space*/
					while(!go_down(&current_tetromino, &world));
					reached_bottom=1;
					diff=tick_time;
					break;
				case -2: 		
					free(begin);
					free(end);
					return;
				default: break;
			}
		}
		
		//game tick & time
		gettimeofday(end, NULL);
		diff+=(end->tv_sec-begin->tv_sec)*1000000 + end->tv_usec-begin->tv_usec;
		tmp=begin;
		begin=end;
		end=tmp;
		
		if(diff >= tick_time){
			diff -= tick_time;
			tick(&reached_bottom);
		}
		
		//gui update
		draw();
		
		//sleep
		usleep(50*1000);
	}
}
Beispiel #28
0
int		main(int argc, char **argv)
{
	int key;
	t_map *g_map;
	t_game *snake;

	srand(time(NULL));
	if (argc < 3)
		return (-1);
	snake = malloc(sizeof(t_game));
	g_map = malloc(sizeof(t_map));

	g_map->x_max = ft_atoi(argv[2]);
	g_map->y_max = ft_atoi(argv[1]);
	if (g_map->x_max < 3 || g_map->y_max < 3)
		return (-1);
	g_map->x_map = rand_a_b(3, g_map->x_max + 1);
	g_map->y_map = rand_a_b(3, g_map->y_max + 1);
	// ft_putnbr(g_map->x_map);
	// ft_putchar('\n');
	// ft_putnbr(g_map->y_map);
	// ft_putchar('\n');
	snake->x_s = 1;
	snake->y_s = 1;
	// ft_putnbr(snake->x_s);
	// ft_putchar('\n');
	// ft_putnbr(snake->y_s);
	// ft_putchar('\n');
	snake->pv_s = 13;

	g_map->map = map_create(g_map->x_map, g_map->y_map);
	ft_putendl("placement du heros");
	g_map->map[snake->x_s][snake->y_s] = 's';
	ft_putendl("affichage");
	
	//free_all(snake, g_map);
	while (1)
	{
		key = 2;
		key = getarrowkey();
		aff_map(g_map);
		aff_pv(snake);
		ft_putstr("POSITION:");
		ft_putnbr(snake->x_s);
		ft_putchar(' ');
		ft_putnbr(snake->y_s);
		ft_putchar('\n');
		ft_putstr("MAP");
		ft_putnbr(g_map->x_map);
		ft_putchar(' ');
		ft_putnbr(g_map->x_map);
		if (key == 1)
		{
			if (go_left(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}
		else if (key == 2)
		{
			if (go_up(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}
		else if (key == 3)
		{
			if (go_down(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}	
		else if (key == 4)
		{
			if (go_right(snake, g_map))
			{
				add_room(g_map);
				g_map = g_map->next;
			}
		}
		else if (key == 5)
		{
			free_all(snake, g_map);
			return (0);
		}
		usleep(100000);
		system("CLEAR");
	}
	return (0);
}