/************************************************************************
  Generate random number.
************************************************************************/
int api_utilities_random(lua_State *L, int min, int max)
{
  double roll;

  LUASCRIPT_CHECK_STATE(L, 0);

  roll = ((double) (fc_rand(MAX_UINT32) % MAX_UINT32) / MAX_UINT32);

  return (min + floor(roll * (max - min + 1)));
}
Exemple #2
0
/*************************************************************************
  generate a random string.
*************************************************************************/
static void randomize_string(char *str, size_t n)
{
  const char chars[] =
    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  int i;

  for (i = 0; i < n - 1; i++) {
    str[i] = chars[fc_rand(sizeof(chars) - 1)];
  }
  str[i] = '\0';
}
Exemple #3
0
/****************************************************************
  Call ggz_embed_ensure_server() if ggz enabled.
*****************************************************************/
void gui_ggz_embed_ensure_server(void)
{
#ifdef GGZ_GTK
  {
    char buf[128];

    user_username(buf, sizeof(buf));
    cat_snprintf(buf, sizeof(buf), "%d", fc_rand(100));
    ggz_embed_ensure_server("Pubserver", "freeciv.ggzgamingzone.org",
                           5688, buf);
  }
#endif /* GGZ_GTK */
}
Exemple #4
0
/****************************************************************
 ...
*****************************************************************/
static void select_random_race(void)
{
  /* try to find a free nation */
  /* FIXME: this code should be done another way. -ev */
  while (1) {
    unsigned int race_toggle_index = fc_rand(nation_count());

    if (!is_nation_playable(nation_by_number(race_toggle_index))
	|| !nation_by_number(race_toggle_index)->is_available
	|| nation_by_number(race_toggle_index)->player) {
      continue;
    }
    if (XtIsSensitive(races_toggles[race_toggle_index])) {
      x_simulate_button_click(races_toggles[race_toggle_index]);
      break;
    }
  }
}
Exemple #5
0
void citizens_update(struct city *pcity)
{
  int delta;

  fc_assert_ret(pcity);

  if (pcity->server.debug) {
    /* before */
    citizens_print(pcity);
  }

  if (game.info.citizen_nationality != TRUE) {
    return;
  }

  if (pcity->nationality == NULL) {
    /* If nationalities are not set (virtual cities) do nothing. */
    return;
  }

  delta = city_size_get(pcity) - citizens_count(pcity);

  if (delta == 0) {
    /* No change of the city size */
    return;
  }

  if (delta > 0) {
    /* Add new citizens with the nationality of the current owner. */
    citizens_nation_add(pcity, city_owner(pcity)->slot, delta);
    log_citizens_add(pcity, delta, city_owner(pcity));
  } else {
    /* Removed citizens. */
    struct player_slot *city_nations[MAX_NUM_PLAYER_SLOTS];
    int count = 0;

    /* Create a list of foreign nationalities. */
    citizens_foreign_iterate(pcity, pslot, nationality) {
      city_nations[count] = pslot;
      count++;
    } citizens_foreign_iterate_end;

    /* First remove from foreign nationalities. */
    while (count > 0 && delta < 0) {
      int select = fc_rand(count);
      struct player_slot *pslot = city_nations[select];
      struct player *pplayer = player_slot_get_player(pslot);
      citizens nationality = citizens_nation_get(pcity, pslot);

      fc_assert_ret(nationality != 0);
      fc_assert_ret(pplayer != NULL);

      if (nationality == 1) {
        /* Remove one citizen. */
        delta++;
        citizens_nation_set(pcity, pslot, 0);
        /* Remove this nation from the list of nationalities. */
        if (select != count) {
          city_nations[select] = city_nations[count - 1];
        }
        count--;

        log_citizens_add(pcity, -1, pplayer);
      } else {
        /* Get the minimal reduction = the maximum value of two negative
         * numbers. */
        int diff = MAX(delta, - nationality / 2);
        delta -= diff;
        citizens_nation_add(pcity, pslot, diff);
        log_citizens_add(pcity, diff, pplayer);
      }
    }

    if (delta < 0) {
      /* Now take the remaining citizens loss from the nation of the owner. */
      citizens_nation_add(pcity, city_owner(pcity)->slot, delta);
      log_citizens_add(pcity, delta, city_owner(pcity));
    }
  }