示例#1
0
static unsigned int nrand(unsigned int nLimit)
{
  unsigned long result;
  double_t ratio;

  /* Loop to be sure a legal random number is generated */

  do
    {
      /* Get a random integer in the requested range */

#if (CONFIG_LIB_RAND_ORDER == 1)
      ratio = frand1();
#elif (CONFIG_LIB_RAND_ORDER == 2)
      ratio = frand2();
#else /* if (CONFIG_LIB_RAND_ORDER > 2) */
      ratio = frand3();
#endif

      /* Then, produce the return-able value */

      result = (unsigned long)(((double_t)nLimit) * ratio);
    }
  while (result >= (unsigned long)nLimit);

  return (unsigned int)result;
}
示例#2
0
文件: battle.c 项目: scumola/GalaxyNG
group          *
selectTargetGroup(batstat *attackingSide, batstat **targetSide, int *ship)
{
  /* group *targetGroup; */
  int             enemy;
  int             totalNumberOfTargets;

  pdebug(DFULL, "selectTargetGroup\n");

  /* targetGroup = NULL;*/
  *ship = 0;

  /* Compute the total number of targets */
  totalNumberOfTargets = 0;
  for (enemy = 0; enemy < attackingSide->noEnemies; enemy++) {
    totalNumberOfTargets +=
        (attackingSide->enemies[enemy])->numberOfTargets;
  }

  if (totalNumberOfTargets > 0) {
    int             lowerLim;
    int             upperLim;
    int             n;

    lowerLim = 0;

    /* Randomly select one of the targets */
    n = frand3(totalNumberOfTargets);

    /* Find the owner of the target this ship */
    for (enemy = 0; enemy < attackingSide->noEnemies; enemy++) {
      upperLim =
          lowerLim + (attackingSide->enemies[enemy])->numberOfTargets;
      if ((n >= lowerLim) && (n < upperLim)) {
        group          *aGroup;

        *targetSide = attackingSide->enemies[enemy];
        n -= lowerLim;
        lowerLim = 0;

        /* Find the group the ship belongs to, and the number of the ship
         * * within * the group */
        for (aGroup = (*targetSide)->groups; aGroup; aGroup = aGroup->next) {
          upperLim = lowerLim + aGroup->left;
          if ((n >= lowerLim) && (n < upperLim)) {
            int             curShip;

            n -= lowerLim;
            /* targetGroup = aGroup; */
            for (*ship = 0, curShip = 0; *ship < aGroup->ships; (*ship)++) {
              /* Is the ship alive */
              if ((aGroup->alive)[*ship]) {
                if (n == curShip) {
                  return aGroup;
                }
                else {
                  curShip++;
                }
              }
            }
            assert(0);
          }
          lowerLim = upperLim;
        }
      }
      lowerLim = upperLim;
    }
  }
  return NULL;
}
示例#3
0
文件: battle.c 项目: scumola/GalaxyNG
group          *
selectAttackingGroup(batstat *batstats, batstat **attackingSide, int *ship)
{
  int             totalNumberOfAttackersLeft;
  batstat        *aBatstat;

  /* Compute the number of ships that haven't fired yet. */
  totalNumberOfAttackersLeft = 0;
  for (aBatstat = batstats; aBatstat; aBatstat = aBatstat->next) {
    totalNumberOfAttackersLeft += aBatstat->numberOfAttackersLeft;
  }

  if (totalNumberOfAttackersLeft > 0) {
    int             lowerLim;
    int             upperLim;
    int             n;

    lowerLim = 0;
    /* Randomly select one of these ships */
    n = frand3(totalNumberOfAttackersLeft);

    /* Find the the owner and the group the ship belongs to. */
    for (aBatstat = batstats; aBatstat; aBatstat = aBatstat->next) {
      upperLim = lowerLim + aBatstat->numberOfAttackersLeft;
      if ((n >= lowerLim) && (n < upperLim)) {
        group          *aGroup;

        /* Found the owner */
        *attackingSide = aBatstat;
        n -= lowerLim;
        lowerLim = 0;
        /* Now find the group the ship belongs to */
        for (aGroup = (*attackingSide)->groups;
             aGroup; aGroup = aGroup->next) {
          upperLim = lowerLim + aGroup->numberOfAttackersLeft;
          if ((n >= lowerLim) && (n < upperLim)) {
            int             curShip;

            n -= lowerLim;
            /* Now find the number of the ship within the group */
            for (*ship = 0, curShip = 0;
                 *ship < aGroup->numberOfAttackers; (*ship)++) {
              /* Did the ship fire yet? */
              if ((aGroup->canshoot)[*ship]) {
                if (n == curShip) {
                  aGroup->numberOfAttackersLeft--;
                  (*attackingSide)->numberOfAttackersLeft--;
                  (aGroup->canshoot)[*ship] = FALSE;
                  return aGroup;
                }
                else {
                  curShip++;
                }
              }
            }
          }
          lowerLim = upperLim;
        }
      }
      lowerLim = upperLim;
    }
  }
  return NULL;
}