Ejemplo n.º 1
0
int main(void)
{
	char *parent, *c;

	plan_tests(21);

	/* We can take NULL. */
	ok1(take(NULL) == NULL);
	ok1(is_taken(NULL));
	ok1(taken(NULL)); /* Undoes take() */
	ok1(!is_taken(NULL));
	ok1(!taken(NULL));

	parent = tal(NULL, char);
	ok1(parent);

	ok1(take(parent) == parent);
	ok1(is_taken(parent));
	ok1(taken(parent)); /* Undoes take() */
	ok1(!is_taken(parent));
	ok1(!taken(parent));

	c = tal(parent, char);
	*c = 'h';
	c = tal_dup(parent, char, take(c), 1, 0);
	ok1(c[0] == 'h');
	ok1(tal_parent(c) == parent);

	c = tal_dup(parent, char, take(c), 1, 2);
	ok1(c[0] == 'h');
	strcpy(c, "hi");
	ok1(tal_parent(c) == parent);

	/* dup must reparent child. */
	c = tal_dup(NULL, char, take(c), 1, 0);
	ok1(c[0] == 'h');
	ok1(tal_parent(c) == NULL);

	/* No leftover allocations. */
	tal_free(c);
	ok1(talloc_total_blocks(parent) == 1);

	tal_free(parent);
	ok1(!taken_any());

	/* NULL pass-through. */
	c = NULL;
	ok1(tal_dup(NULL, char, take(c), 5, 5) == NULL);
	ok1(!taken_any());

	return exit_status();
}
Ejemplo n.º 2
0
int turnports_is_allocated(turnports* tp, u16bits port) {
  if(!tp) return 0;
  else {
    TURN_MUTEX_LOCK(&tp->mutex);
    int ret = is_taken(tp->status[port]);
    TURN_MUTEX_UNLOCK(&tp->mutex);
    return ret;
  }
}
Ejemplo n.º 3
0
void turnports_release(turnports* tp, u16bits port) {
  TURN_MUTEX_LOCK(&tp->mutex);
  if(tp && port>=tp->range_start && port<=tp->range_stop) {
    u16bits position=(u16bits)(tp->high & 0x0000FFFF);
    if(is_taken(tp->status[port])) {
      tp->status[port]=tp->high;
      tp->ports[position]=port;
      ++(tp->high);
    }
  }
  TURN_MUTEX_UNLOCK(&tp->mutex);
}
Ejemplo n.º 4
0
int		first_choice(t_player *player, int left, int top, char **map)
{

  if (is_taken(player->x + left, player->y + top, map))
    return 0;
  map[player->y][player->x] = 0;
  map[player->y + top][player->x + left] = player->team;
  GUI_do(MOVE, player, player->x + left, player->y + top);
  player->x += left;
  player->y += top;
  return 1;
}
Ejemplo n.º 5
0
int		test_move(t_player *player, char **map, int x, int y)
{
  if (!is_taken(x, y, map))
    {
      map[player->y][player->x] = 0;
      map[y][x] = player->team;
      GUI_do(MOVE, player, x, y);
      player->y = y;
      player->x = x;
      return 1;
    }
  return 0;
}
Ejemplo n.º 6
0
int turnports_is_available(turnports* tp, u16bits port) {
  if(tp) {
    TURN_MUTEX_LOCK(&tp->mutex);
    u64bits status = tp->status[port];
    if((status!=TPS_OUT_OF_RANGE) && !is_taken(status)) {
      u16bits position=(u16bits)(status & 0x0000FFFF);
      if((int)(tp->ports[position])==(int)port) {
	TURN_MUTEX_UNLOCK(&tp->mutex);
	return 1;
      }
    }
    TURN_MUTEX_UNLOCK(&tp->mutex);
  }
  return 0;
}
Ejemplo n.º 7
0
void hardcore_mapgen(int (*map)[mapX][mapY], int *endX, int *endY) {
    int x, y;
    
    // reset map
    for (y=0; y<mapY; y++)
        for (x=0; x<mapX; x++)
            (*map)[x][y] = 0;
    
    // generate path starting from every empty block
    for (y = 0; y<mapY/3; y++)
        for (x = 0; x<mapX/3; x++)
            if (is_taken(x, y, map) == 0) // check if block is empty
                mapgen(map,x,y,map, endX, endY); // generate path
    
    // generate final route
    int final_map[mapX][mapY] = {0};
    mapgen(map, 0, 0, &final_map, endX, endY);
    
    // remove walls between paths to merge them
    join_paths(map);
}
Ejemplo n.º 8
0
int test_gen_direction(int (*map)[mapX][mapY], int direction, int x, int y) {
    int newX = x; int newY = y;
        
    switch (direction) {
        case 0: return 0;
        case L_UP   : newY -= 1; break;
        case L_LEFT : newX -= 1; break; 
        case L_DOWN : newY += 1; break;
        case L_RIGHT: newX += 1; break;
    }
    
    // check if is in range
    if (newX < 0 || newX*3 >= mapX) return 0;
    if (newY < 0 || newY*3 >= mapY) return 0;
        
    // avoid overwriting blocks
    if (is_taken(newX, newY, map)) return 0;
    
    return 1;
    
}
Ejemplo n.º 9
0
int turnports_allocate(turnports* tp) {

  int port=-1;

  TURN_MUTEX_LOCK(&tp->mutex);

  if(tp) {

    while(1) {
      
      if(tp->high <= tp->low) {
	TURN_MUTEX_UNLOCK(&tp->mutex);
	return -1;
      }
      
      int position=(u16bits)(tp->low & 0x0000FFFF);
      
      port=tp->ports[position];
      if(port<(int)(tp->range_start) || port>((int)(tp->range_stop))) {
	TURN_MUTEX_UNLOCK(&tp->mutex);
	return -1;
      }
      if(is_taken(tp->status[port])) {
	++(tp->low);
	continue;
      } 
      if(tp->status[port]!=tp->low) {
	++(tp->low);
	continue;
      }
      tp->status[port]=TPS_TAKEN_SINGLE;
      ++(tp->low);
      break;
    }
  }

  TURN_MUTEX_UNLOCK(&tp->mutex);

  return port;
}
Ejemplo n.º 10
0
void calculate_happiness_difference(int level) {
	int i;
	
	if (level == guest_count) {
		/* calculate happiness */
		int diff = 0;
		
		for (i = 0; i < guest_count; i++) {
			diff += guests[combination[i]][combination[(i + 1) % guest_count]];
			diff += guests[combination[(i + 1) % guest_count]][combination[i]];
		}
		
		max_happiness = MAX(max_happiness, diff);
	}
	
	for (i = 0; i < guest_count; i++) {
		if (!is_taken(i, level)) {
			combination[level] = i;
			calculate_happiness_difference(level + 1);
		}
	}
}